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.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.os.Process; 28 29 import org.junit.Before; 30 import org.junit.Ignore; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Answers; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class PowerUsageFeatureProviderImplTest { 40 41 private static final int UID_OTHER = Process.FIRST_APPLICATION_UID + 2; 42 private static final int UID_CALENDAR = Process.FIRST_APPLICATION_UID + 3; 43 private static final int UID_MEDIA = Process.FIRST_APPLICATION_UID + 4; 44 private static final int UID_SYSTEMUI = Process.FIRST_APPLICATION_UID + 5; 45 private static final String[] PACKAGES_CALENDAR = {"com.android.providers.calendar"}; 46 private static final String[] PACKAGES_MEDIA = {"com.android.providers.media"}; 47 private static final String[] PACKAGES_SYSTEMUI = {"com.android.systemui"}; 48 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private Context mContext; 51 52 @Mock private PackageManager mPackageManager; 53 @Mock private BatteryInfo mBatteryInfo; 54 55 private PowerUsageFeatureProviderImpl mPowerFeatureProvider; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 61 when(mContext.getApplicationContext()).thenReturn(mContext); 62 mPowerFeatureProvider = spy(new PowerUsageFeatureProviderImpl(mContext)); 63 when(mPackageManager.getPackagesForUid(UID_CALENDAR)).thenReturn(PACKAGES_CALENDAR); 64 when(mPackageManager.getPackagesForUid(UID_MEDIA)).thenReturn(PACKAGES_MEDIA); 65 when(mPackageManager.getPackagesForUid(UID_SYSTEMUI)).thenReturn(PACKAGES_SYSTEMUI); 66 mPowerFeatureProvider.mPackageManager = mPackageManager; 67 mBatteryInfo.discharging = false; 68 } 69 70 @Test isBatteryUsageEnabled_returnFalse()71 public void isBatteryUsageEnabled_returnFalse() { 72 assertThat(mPowerFeatureProvider.isBatteryUsageEnabled()).isTrue(); 73 } 74 75 @Test isBatteryTipsEnabled_returnFalse()76 public void isBatteryTipsEnabled_returnFalse() { 77 assertThat(mPowerFeatureProvider.isBatteryTipsEnabled()).isFalse(); 78 } 79 80 @Test isRestrictedModeOverwriteEnabled_returnFalse()81 public void isRestrictedModeOverwriteEnabled_returnFalse() { 82 assertThat(mPowerFeatureProvider.isRestrictedModeOverwriteEnabled()).isFalse(); 83 } 84 85 @Test isForceExpireAppOptimizationModeEnabled_returnFalse()86 public void isForceExpireAppOptimizationModeEnabled_returnFalse() { 87 assertThat(mPowerFeatureProvider.isForceExpireAppOptimizationModeEnabled()).isFalse(); 88 } 89 90 @Test isAppOptimizationModeLogged_returnFalse()91 public void isAppOptimizationModeLogged_returnFalse() { 92 assertThat(mPowerFeatureProvider.isAppOptimizationModeLogged()).isFalse(); 93 } 94 95 @Test getBatteryUsageListConsumePowerThreshold_return0()96 public void getBatteryUsageListConsumePowerThreshold_return0() { 97 assertThat(mPowerFeatureProvider.getBatteryUsageListConsumePowerThreshold()).isEqualTo(0.0); 98 } 99 100 @Test isTypeSystem_uidRoot_returnTrue()101 public void isTypeSystem_uidRoot_returnTrue() { 102 assertThat(mPowerFeatureProvider.isTypeSystem(Process.ROOT_UID, null)).isTrue(); 103 } 104 105 @Test isTypeSystem_uidSystem_returnTrue()106 public void isTypeSystem_uidSystem_returnTrue() { 107 assertThat(mPowerFeatureProvider.isTypeSystem(Process.SYSTEM_UID, null)).isTrue(); 108 } 109 110 @Test isTypeSystem_uidMedia_returnTrue()111 public void isTypeSystem_uidMedia_returnTrue() { 112 assertThat(mPowerFeatureProvider.isTypeSystem(Process.MEDIA_UID, null)).isTrue(); 113 } 114 115 @Test 116 @Ignore isTypeSystem_appCalendar_returnTrue()117 public void isTypeSystem_appCalendar_returnTrue() { 118 assertThat(mPowerFeatureProvider.isTypeSystem(UID_CALENDAR, null)).isTrue(); 119 } 120 121 @Test 122 @Ignore isTypeSystem_appMedia_returnTrue()123 public void isTypeSystem_appMedia_returnTrue() { 124 assertThat(mPowerFeatureProvider.isTypeSystem(UID_MEDIA, null)).isTrue(); 125 } 126 127 @Test 128 @Ignore isTypeSystem_appSystemUi_returnTrue()129 public void isTypeSystem_appSystemUi_returnTrue() { 130 assertThat(mPowerFeatureProvider.isTypeSystem(UID_SYSTEMUI, null)).isTrue(); 131 } 132 133 @Test isTypeSystem_uidOther_returnFalse()134 public void isTypeSystem_uidOther_returnFalse() { 135 assertThat(mPowerFeatureProvider.isTypeSystem(UID_OTHER, null)).isFalse(); 136 } 137 138 @Test isSmartBatterySupported_smartBatterySupported_returnTrue()139 public void isSmartBatterySupported_smartBatterySupported_returnTrue() { 140 when(mContext.getResources() 141 .getBoolean(com.android.internal.R.bool.config_smart_battery_available)) 142 .thenReturn(true); 143 144 assertThat(mPowerFeatureProvider.isSmartBatterySupported()).isTrue(); 145 } 146 147 @Test isSmartBatterySupported_smartBatteryNotSupported_returnFalse()148 public void isSmartBatterySupported_smartBatteryNotSupported_returnFalse() { 149 when(mContext.getResources() 150 .getBoolean(com.android.internal.R.bool.config_smart_battery_available)) 151 .thenReturn(false); 152 153 assertThat(mPowerFeatureProvider.isSmartBatterySupported()).isFalse(); 154 } 155 156 @Test isAdaptiveChargingSupported_returnFalse()157 public void isAdaptiveChargingSupported_returnFalse() { 158 assertThat(mPowerFeatureProvider.isAdaptiveChargingSupported()).isFalse(); 159 } 160 161 @Test getResumeChargeIntentWithoutDockDefender_returnNull()162 public void getResumeChargeIntentWithoutDockDefender_returnNull() { 163 assertThat(mPowerFeatureProvider.getResumeChargeIntent(false)).isNull(); 164 } 165 166 @Test getResumeChargeIntentWithDockDefender_returnNull()167 public void getResumeChargeIntentWithDockDefender_returnNull() { 168 assertThat(mPowerFeatureProvider.getResumeChargeIntent(true)).isNull(); 169 } 170 171 @Test isBatteryDefend_defenderModeAndExtraDefendAreFalse_returnFalse()172 public void isBatteryDefend_defenderModeAndExtraDefendAreFalse_returnFalse() { 173 mBatteryInfo.isLongLife = false; 174 doReturn(false).when(mPowerFeatureProvider).isExtraDefend(); 175 176 assertThat(mPowerFeatureProvider.isBatteryDefend(mBatteryInfo)).isFalse(); 177 } 178 179 @Test isBatteryDefend_defenderModeIsFalse_returnFalse()180 public void isBatteryDefend_defenderModeIsFalse_returnFalse() { 181 mBatteryInfo.isLongLife = false; 182 doReturn(true).when(mPowerFeatureProvider).isExtraDefend(); 183 184 assertThat(mPowerFeatureProvider.isBatteryDefend(mBatteryInfo)).isFalse(); 185 } 186 187 @Test isBatteryDefend_defenderModeAndExtraDefendAreTrue_returnFalse()188 public void isBatteryDefend_defenderModeAndExtraDefendAreTrue_returnFalse() { 189 mBatteryInfo.isLongLife = true; 190 doReturn(true).when(mPowerFeatureProvider).isExtraDefend(); 191 192 assertThat(mPowerFeatureProvider.isBatteryDefend(mBatteryInfo)).isFalse(); 193 } 194 195 @Test isBatteryDefend_extraDefendIsFalse_returnTrue()196 public void isBatteryDefend_extraDefendIsFalse_returnTrue() { 197 mBatteryInfo.isLongLife = true; 198 doReturn(false).when(mPowerFeatureProvider).isExtraDefend(); 199 200 assertThat(mPowerFeatureProvider.isBatteryDefend(mBatteryInfo)).isTrue(); 201 } 202 } 203