1 /* 2 * Copyright (C) 2016 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 package com.android.settings.fuelgauge; 17 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.os.Handler; 24 import android.os.UserManager; 25 26 import com.android.internal.os.BatterySipper; 27 import com.android.internal.os.BatterySipper.DrainType; 28 import com.android.settings.TestConfig; 29 30 import org.junit.Before; 31 import org.junit.Rule; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.junit.MockitoRule; 36 import org.mockito.junit.MockitoJUnit; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.annotation.Config; 39 40 import static com.google.common.truth.Truth.assertThat; 41 import static org.mockito.Mockito.mock; 42 import static org.mockito.Mockito.when; 43 44 @RunWith(RobolectricTestRunner.class) 45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 46 public class BatteryEntryTest { 47 48 private static final int APP_UID = 123; 49 private static final String APP_DEFAULT_PACKAGE_NAME = "com.android.test"; 50 private static final String APP_LABEL = "Test App Name"; 51 private static final String HIGH_DRAIN_PACKAGE = "com.android.test.screen"; 52 53 @Rule public MockitoRule mocks = MockitoJUnit.rule(); 54 55 @Mock private Context mockContext; 56 @Mock private Handler mockHandler; 57 @Mock private PackageManager mockPackageManager; 58 @Mock private UserManager mockUserManager; 59 60 @Before stubContextToReturnMockPackageManager()61 public void stubContextToReturnMockPackageManager() { 62 when(mockContext.getPackageManager()).thenReturn(mockPackageManager); 63 } 64 65 @Before stubPackageManagerToReturnAppPackageAndName()66 public void stubPackageManagerToReturnAppPackageAndName() throws NameNotFoundException { 67 when(mockPackageManager.getPackagesForUid(APP_UID)).thenReturn( 68 new String[]{APP_DEFAULT_PACKAGE_NAME}); 69 70 ApplicationInfo appInfo = mock(ApplicationInfo.class); 71 when(mockPackageManager.getApplicationInfo(APP_DEFAULT_PACKAGE_NAME, 0 /* no flags */)) 72 .thenReturn(appInfo); 73 when(mockPackageManager.getApplicationLabel(appInfo)).thenReturn(APP_LABEL); 74 } 75 createBatteryEntryForApp()76 private BatteryEntry createBatteryEntryForApp() { 77 return new BatteryEntry(mockContext, mockHandler, mockUserManager, createSipperForApp()); 78 } 79 createSipperForApp()80 private BatterySipper createSipperForApp() { 81 BatterySipper sipper = 82 new BatterySipper(DrainType.APP, new FakeUid(APP_UID), 0 /* power use */); 83 sipper.packageWithHighestDrain = HIGH_DRAIN_PACKAGE; 84 return sipper; 85 } 86 87 @Test batteryEntryForApp_shouldSetDefaultPackageNameAndLabel()88 public void batteryEntryForApp_shouldSetDefaultPackageNameAndLabel() throws Exception { 89 BatteryEntry entry = createBatteryEntryForApp(); 90 91 assertThat(entry.defaultPackageName).isEqualTo(APP_DEFAULT_PACKAGE_NAME); 92 assertThat(entry.getLabel()).isEqualTo(APP_LABEL); 93 } 94 95 @Test batteryEntryForApp_shouldSetLabelAsPackageName_whenPackageCannotBeFound()96 public void batteryEntryForApp_shouldSetLabelAsPackageName_whenPackageCannotBeFound() 97 throws Exception { 98 when(mockPackageManager.getApplicationInfo(APP_DEFAULT_PACKAGE_NAME, 0 /* no flags */)) 99 .thenThrow(new NameNotFoundException()); 100 101 BatteryEntry entry = createBatteryEntryForApp(); 102 103 assertThat(entry.getLabel()).isEqualTo(APP_DEFAULT_PACKAGE_NAME); 104 } 105 106 @Test batteryEntryForApp_shouldSetHighestDrainPackage_whenPackagesCannotBeFoundForUid()107 public void batteryEntryForApp_shouldSetHighestDrainPackage_whenPackagesCannotBeFoundForUid() { 108 when(mockPackageManager.getPackagesForUid(APP_UID)).thenReturn(null); 109 110 BatteryEntry entry = createBatteryEntryForApp(); 111 112 assertThat(entry.getLabel()).isEqualTo(HIGH_DRAIN_PACKAGE); 113 } 114 115 @Test batteryEntryForApp_shouldSetHighestDrainPackage_whenMultiplePackagesFoundForUid()116 public void batteryEntryForApp_shouldSetHighestDrainPackage_whenMultiplePackagesFoundForUid() { 117 when(mockPackageManager.getPackagesForUid(APP_UID)).thenReturn( 118 new String[]{APP_DEFAULT_PACKAGE_NAME, "package2", "package3"}); 119 120 BatteryEntry entry = createBatteryEntryForApp(); 121 122 assertThat(entry.getLabel()).isEqualTo(HIGH_DRAIN_PACKAGE); 123 } 124 } 125