1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.fuelgauge;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.os.Process;
22 import com.android.internal.os.BatterySipper;
23 import com.android.settings.SettingsRobolectricTestRunner;
24 import com.android.settings.TestConfig;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.robolectric.annotation.Config;
31 
32 import static org.mockito.Mockito.when;
33 import static com.google.common.truth.Truth.assertThat;
34 
35 @RunWith(SettingsRobolectricTestRunner.class)
36 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
37 public class PowerUsageFeatureProviderImplTest {
38     private static final int UID_OTHER = Process.FIRST_APPLICATION_UID + 2;
39     private static final int UID_CALENDAR = Process.FIRST_APPLICATION_UID + 3;
40     private static final int UID_MEDIA = Process.FIRST_APPLICATION_UID + 4;
41     private static final int UID_SYSTEMUI = Process.FIRST_APPLICATION_UID + 5;
42     private static final String[] PACKAGES_CALENDAR = {"com.android.providers.calendar"};
43     private static final String[] PACKAGES_MEDIA = {"com.android.providers.media"};
44     private static final String[] PACKAGES_SYSTEMUI = {"com.android.systemui"};
45     @Mock
46     private Context mContext;
47     @Mock
48     private BatterySipper mBatterySipper;
49     @Mock
50     private PackageManager mPackageManager;
51     private PowerUsageFeatureProviderImpl mPowerFeatureProvider;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56 
57         mPowerFeatureProvider = new PowerUsageFeatureProviderImpl(mContext);
58         when(mPackageManager.getPackagesForUid(UID_CALENDAR)).thenReturn(PACKAGES_CALENDAR);
59         when(mPackageManager.getPackagesForUid(UID_MEDIA)).thenReturn(PACKAGES_MEDIA);
60         when(mPackageManager.getPackagesForUid(UID_SYSTEMUI)).thenReturn(PACKAGES_SYSTEMUI);
61         mPowerFeatureProvider.mPackageManager = mPackageManager;
62         mBatterySipper.uidObj = new FakeUid(UID_OTHER);
63     }
64 
65     @Test
testIsTypeSystem_uidRoot_returnTrue()66     public void testIsTypeSystem_uidRoot_returnTrue() {
67         mBatterySipper.drainType = BatterySipper.DrainType.APP;
68         when(mBatterySipper.getUid()).thenReturn(Process.ROOT_UID);
69 
70         assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue();
71     }
72 
73     @Test
testIsTypeSystem_uidSystem_returnTrue()74     public void testIsTypeSystem_uidSystem_returnTrue() {
75         mBatterySipper.drainType = BatterySipper.DrainType.APP;
76         when(mBatterySipper.getUid()).thenReturn(Process.SYSTEM_UID);
77 
78         assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue();
79     }
80 
81     @Test
testIsTypeSystem_uidMedia_returnTrue()82     public void testIsTypeSystem_uidMedia_returnTrue() {
83         mBatterySipper.drainType = BatterySipper.DrainType.APP;
84         when(mBatterySipper.getUid()).thenReturn(Process.MEDIA_UID);
85 
86         assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue();
87     }
88 
89     @Test
testIsTypeSystem_appCalendar_returnTrue()90     public void testIsTypeSystem_appCalendar_returnTrue() {
91         mBatterySipper.drainType = BatterySipper.DrainType.APP;
92         when(mBatterySipper.getUid()).thenReturn(UID_CALENDAR);
93 
94         assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue();
95     }
96 
97     @Test
testIsTypeSystem_appMedia_returnTrue()98     public void testIsTypeSystem_appMedia_returnTrue() {
99         mBatterySipper.drainType = BatterySipper.DrainType.APP;
100         when(mBatterySipper.getUid()).thenReturn(UID_MEDIA);
101 
102         assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue();
103     }
104 
105     @Test
testIsTypeSystem_appSystemUi_returnTrue()106     public void testIsTypeSystem_appSystemUi_returnTrue() {
107         mBatterySipper.drainType = BatterySipper.DrainType.APP;
108         when(mBatterySipper.getUid()).thenReturn(UID_SYSTEMUI);
109 
110         assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue();
111     }
112 
113     @Test
testIsTypeSystem_uidOther_returnFalse()114     public void testIsTypeSystem_uidOther_returnFalse() {
115         mBatterySipper.drainType = BatterySipper.DrainType.APP;
116         when(mBatterySipper.getUid()).thenReturn(UID_OTHER);
117 
118         assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isFalse();
119     }
120 
121     @Test
testIsTypeSystem_uidObjNull_returnFalse()122     public void testIsTypeSystem_uidObjNull_returnFalse() {
123         mBatterySipper.drainType = BatterySipper.DrainType.APP;
124         mBatterySipper.uidObj = null;
125 
126         assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isFalse();
127     }
128 
129     @Test
testIsAdvancedUiEnabled_returnTrue()130     public void testIsAdvancedUiEnabled_returnTrue() {
131         assertThat(mPowerFeatureProvider.isAdvancedUiEnabled()).isTrue();
132     }
133 
134     @Test
testIsPowerAccountingToggleEnabled_returnTrue()135     public void testIsPowerAccountingToggleEnabled_returnTrue() {
136         assertThat(mPowerFeatureProvider.isPowerAccountingToggleEnabled()).isTrue();
137     }
138 }
139