• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.content.pm.PackageManager;
25 import android.os.Process;
26 
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Answers;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.RobolectricTestRunner;
35 
36 @RunWith(RobolectricTestRunner.class)
37 public class PowerUsageFeatureProviderImplTest {
38 
39     private static final int UID_OTHER = Process.FIRST_APPLICATION_UID + 2;
40     private static final int UID_CALENDAR = Process.FIRST_APPLICATION_UID + 3;
41     private static final int UID_MEDIA = Process.FIRST_APPLICATION_UID + 4;
42     private static final int UID_SYSTEMUI = Process.FIRST_APPLICATION_UID + 5;
43     private static final String[] PACKAGES_CALENDAR = {"com.android.providers.calendar"};
44     private static final String[] PACKAGES_MEDIA = {"com.android.providers.media"};
45     private static final String[] PACKAGES_SYSTEMUI = {"com.android.systemui"};
46 
47     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
48     private Context mContext;
49     @Mock
50     private PackageManager mPackageManager;
51     private PowerUsageFeatureProviderImpl mPowerFeatureProvider;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56 
57         when(mContext.getApplicationContext()).thenReturn(mContext);
58         mPowerFeatureProvider = new PowerUsageFeatureProviderImpl(mContext);
59         when(mPackageManager.getPackagesForUid(UID_CALENDAR)).thenReturn(PACKAGES_CALENDAR);
60         when(mPackageManager.getPackagesForUid(UID_MEDIA)).thenReturn(PACKAGES_MEDIA);
61         when(mPackageManager.getPackagesForUid(UID_SYSTEMUI)).thenReturn(PACKAGES_SYSTEMUI);
62         mPowerFeatureProvider.mPackageManager = mPackageManager;
63     }
64 
65     @Test
testIsBatteryUsageEnabled_returnFalse()66     public void testIsBatteryUsageEnabled_returnFalse() {
67         assertThat(mPowerFeatureProvider.isBatteryUsageEnabled()).isTrue();
68     }
69 
70     @Test
testIsBatteryTipsEnabled_returnFalse()71     public void testIsBatteryTipsEnabled_returnFalse() {
72         assertThat(mPowerFeatureProvider.isBatteryTipsEnabled()).isFalse();
73     }
74 
75     @Test
testGetBatteryUsageListConsumePowerThreshold_return0()76     public void testGetBatteryUsageListConsumePowerThreshold_return0() {
77         assertThat(mPowerFeatureProvider.getBatteryUsageListConsumePowerThreshold()).isEqualTo(0.0);
78     }
79 
80     @Test
testIsTypeSystem_uidRoot_returnTrue()81     public void testIsTypeSystem_uidRoot_returnTrue() {
82         assertThat(mPowerFeatureProvider.isTypeSystem(Process.ROOT_UID, null)).isTrue();
83     }
84 
85     @Test
testIsTypeSystem_uidSystem_returnTrue()86     public void testIsTypeSystem_uidSystem_returnTrue() {
87         assertThat(mPowerFeatureProvider.isTypeSystem(Process.SYSTEM_UID, null)).isTrue();
88     }
89 
90     @Test
testIsTypeSystem_uidMedia_returnTrue()91     public void testIsTypeSystem_uidMedia_returnTrue() {
92         assertThat(mPowerFeatureProvider.isTypeSystem(Process.MEDIA_UID, null)).isTrue();
93     }
94 
95     @Test
96     @Ignore
testIsTypeSystem_appCalendar_returnTrue()97     public void testIsTypeSystem_appCalendar_returnTrue() {
98         assertThat(mPowerFeatureProvider.isTypeSystem(UID_CALENDAR, null)).isTrue();
99     }
100 
101     @Test
102     @Ignore
testIsTypeSystem_appMedia_returnTrue()103     public void testIsTypeSystem_appMedia_returnTrue() {
104         assertThat(mPowerFeatureProvider.isTypeSystem(UID_MEDIA, null)).isTrue();
105     }
106 
107     @Test
108     @Ignore
testIsTypeSystem_appSystemUi_returnTrue()109     public void testIsTypeSystem_appSystemUi_returnTrue() {
110         assertThat(mPowerFeatureProvider.isTypeSystem(UID_SYSTEMUI, null)).isTrue();
111     }
112 
113     @Test
testIsTypeSystem_uidOther_returnFalse()114     public void testIsTypeSystem_uidOther_returnFalse() {
115         assertThat(mPowerFeatureProvider.isTypeSystem(UID_OTHER, null)).isFalse();
116     }
117 
118     @Test
testIsSmartBatterySupported_smartBatterySupported_returnTrue()119     public void testIsSmartBatterySupported_smartBatterySupported_returnTrue() {
120         when(mContext.getResources().getBoolean(
121                 com.android.internal.R.bool.config_smart_battery_available)).thenReturn(true);
122 
123         assertThat(mPowerFeatureProvider.isSmartBatterySupported()).isTrue();
124     }
125 
126     @Test
testIsSmartBatterySupported_smartBatteryNotSupported_returnFalse()127     public void testIsSmartBatterySupported_smartBatteryNotSupported_returnFalse() {
128         when(mContext.getResources().getBoolean(
129                 com.android.internal.R.bool.config_smart_battery_available)).thenReturn(false);
130 
131         assertThat(mPowerFeatureProvider.isSmartBatterySupported()).isFalse();
132     }
133 
134     @Test
testGetResumeChargeIntentWithoutDockDefender_returnNull()135     public void testGetResumeChargeIntentWithoutDockDefender_returnNull() {
136         assertThat(mPowerFeatureProvider.getResumeChargeIntent(false)).isNull();
137     }
138 
139     @Test
testGetResumeChargeIntentWithDockDefender_returnNull()140     public void testGetResumeChargeIntentWithDockDefender_returnNull() {
141         assertThat(mPowerFeatureProvider.getResumeChargeIntent(true)).isNull();
142     }
143 }
144