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 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 com.android.internal.os.BatterySipper; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Answers; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RobolectricTestRunner; 36 37 @RunWith(RobolectricTestRunner.class) 38 public class PowerUsageFeatureProviderImplTest { 39 40 private static final int UID_OTHER = Process.FIRST_APPLICATION_UID + 2; 41 private static final int UID_CALENDAR = Process.FIRST_APPLICATION_UID + 3; 42 private static final int UID_MEDIA = Process.FIRST_APPLICATION_UID + 4; 43 private static final int UID_SYSTEMUI = Process.FIRST_APPLICATION_UID + 5; 44 private static final String[] PACKAGES_CALENDAR = {"com.android.providers.calendar"}; 45 private static final String[] PACKAGES_MEDIA = {"com.android.providers.media"}; 46 private static final String[] PACKAGES_SYSTEMUI = {"com.android.systemui"}; 47 48 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 49 private Context mContext; 50 @Mock 51 private BatterySipper mBatterySipper; 52 @Mock 53 private PackageManager mPackageManager; 54 private PowerUsageFeatureProviderImpl mPowerFeatureProvider; 55 56 @Before setUp()57 public void setUp() { 58 MockitoAnnotations.initMocks(this); 59 60 when(mContext.getApplicationContext()).thenReturn(mContext); 61 mPowerFeatureProvider = new PowerUsageFeatureProviderImpl(mContext); 62 when(mPackageManager.getPackagesForUid(UID_CALENDAR)).thenReturn(PACKAGES_CALENDAR); 63 when(mPackageManager.getPackagesForUid(UID_MEDIA)).thenReturn(PACKAGES_MEDIA); 64 when(mPackageManager.getPackagesForUid(UID_SYSTEMUI)).thenReturn(PACKAGES_SYSTEMUI); 65 mPowerFeatureProvider.mPackageManager = mPackageManager; 66 mBatterySipper.uidObj = new FakeUid(UID_OTHER); 67 } 68 69 @Test testIsTypeSystem_uidRoot_returnTrue()70 public void testIsTypeSystem_uidRoot_returnTrue() { 71 mBatterySipper.drainType = BatterySipper.DrainType.APP; 72 when(mBatterySipper.getUid()).thenReturn(Process.ROOT_UID); 73 74 assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue(); 75 } 76 77 @Test testIsTypeSystem_uidSystem_returnTrue()78 public void testIsTypeSystem_uidSystem_returnTrue() { 79 mBatterySipper.drainType = BatterySipper.DrainType.APP; 80 when(mBatterySipper.getUid()).thenReturn(Process.SYSTEM_UID); 81 82 assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue(); 83 } 84 85 @Test testIsTypeSystem_uidMedia_returnTrue()86 public void testIsTypeSystem_uidMedia_returnTrue() { 87 mBatterySipper.drainType = BatterySipper.DrainType.APP; 88 when(mBatterySipper.getUid()).thenReturn(Process.MEDIA_UID); 89 90 assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue(); 91 } 92 93 @Test testIsTypeSystem_appCalendar_returnTrue()94 public void testIsTypeSystem_appCalendar_returnTrue() { 95 mBatterySipper.drainType = BatterySipper.DrainType.APP; 96 when(mBatterySipper.getUid()).thenReturn(UID_CALENDAR); 97 98 assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue(); 99 } 100 101 @Test testIsTypeSystem_appMedia_returnTrue()102 public void testIsTypeSystem_appMedia_returnTrue() { 103 mBatterySipper.drainType = BatterySipper.DrainType.APP; 104 when(mBatterySipper.getUid()).thenReturn(UID_MEDIA); 105 106 assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue(); 107 } 108 109 @Test testIsTypeSystem_appSystemUi_returnTrue()110 public void testIsTypeSystem_appSystemUi_returnTrue() { 111 mBatterySipper.drainType = BatterySipper.DrainType.APP; 112 when(mBatterySipper.getUid()).thenReturn(UID_SYSTEMUI); 113 114 assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isTrue(); 115 } 116 117 @Test testIsTypeSystem_uidOther_returnFalse()118 public void testIsTypeSystem_uidOther_returnFalse() { 119 mBatterySipper.drainType = BatterySipper.DrainType.APP; 120 when(mBatterySipper.getUid()).thenReturn(UID_OTHER); 121 122 assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isFalse(); 123 } 124 125 @Test testIsTypeSystem_uidObjNull_returnFalse()126 public void testIsTypeSystem_uidObjNull_returnFalse() { 127 mBatterySipper.drainType = BatterySipper.DrainType.APP; 128 mBatterySipper.uidObj = null; 129 130 assertThat(mPowerFeatureProvider.isTypeSystem(mBatterySipper)).isFalse(); 131 } 132 133 @Test testIsAdvancedUiEnabled_returnTrue()134 public void testIsAdvancedUiEnabled_returnTrue() { 135 assertThat(mPowerFeatureProvider.isAdvancedUiEnabled()).isTrue(); 136 } 137 138 @Test testIsPowerAccountingToggleEnabled_returnTrue()139 public void testIsPowerAccountingToggleEnabled_returnTrue() { 140 assertThat(mPowerFeatureProvider.isPowerAccountingToggleEnabled()).isTrue(); 141 } 142 143 @Test testIsSmartBatterySupported_smartBatterySupported_returnTrue()144 public void testIsSmartBatterySupported_smartBatterySupported_returnTrue() { 145 when(mContext.getResources().getBoolean( 146 com.android.internal.R.bool.config_smart_battery_available)).thenReturn(true); 147 148 assertThat(mPowerFeatureProvider.isSmartBatterySupported()).isTrue(); 149 } 150 151 @Test testIsSmartBatterySupported_smartBatteryNotSupported_returnFalse()152 public void testIsSmartBatterySupported_smartBatteryNotSupported_returnFalse() { 153 when(mContext.getResources().getBoolean( 154 com.android.internal.R.bool.config_smart_battery_available)).thenReturn(false); 155 156 assertThat(mPowerFeatureProvider.isSmartBatterySupported()).isFalse(); 157 } 158 } 159