Files
app_Settings/tests/robotests/src/com/android/settings/fuelgauge/BatteryEntryTest.java
Dmitri Plotnikov 036dc189b6 Transition BatteryAppListPreferences to BatteryUsageStats API
Bug: 173745486
Test: make RunSettingsRoboTests
Test: male RunSettingsGoogleRoboTests

Change-Id: I7af8cbcd27433b89cb2184750c6854aa74761d0d
2021-03-17 13:23:25 -07:00

243 lines
9.5 KiB
Java

/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.fuelgauge;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.BatteryConsumer;
import android.os.Handler;
import android.os.Process;
import android.os.SystemBatteryConsumer;
import android.os.UidBatteryConsumer;
import android.os.UserBatteryConsumer;
import android.os.UserManager;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.Locale;
@RunWith(RobolectricTestRunner.class)
public class BatteryEntryTest {
private static final int APP_UID = 123;
private static final int SYSTEM_UID = Process.SYSTEM_UID;
private static final String APP_DEFAULT_PACKAGE_NAME = "com.android.test";
private static final String LABEL_PREFIX = "Label for ";
private static final String HIGH_DRAIN_PACKAGE = "com.android.test.screen";
private static final String ANDROID_PACKAGE = "android";
@Rule public MockitoRule mocks = MockitoJUnit.rule();
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mMockContext;
@Mock private Handler mockHandler;
@Mock private PackageManager mockPackageManager;
@Mock private UserManager mockUserManager;
@Mock private UidBatteryConsumer mUidBatteryConsumer;
@Mock private SystemBatteryConsumer mSystemBatteryConsumer;
@Before
public void stubContextToReturnMockPackageManager() {
when(mMockContext.getPackageManager()).thenReturn(mockPackageManager);
}
@Before
public void stubPackageManagerToReturnAppPackageAndName() throws NameNotFoundException {
when(mockPackageManager.getApplicationInfo(anyString(), eq(0) /* no flags */))
.thenAnswer(invocation -> {
ApplicationInfo info = new ApplicationInfo();
info.packageName = invocation.getArgument(0);
return info;
});
when(mockPackageManager.getApplicationLabel(any(ApplicationInfo.class)))
.thenAnswer(invocation -> LABEL_PREFIX
+ ((ApplicationInfo) invocation.getArgument(0)).packageName);
}
private BatteryEntry createBatteryEntryForApp(String[] packages, String packageName,
String highDrainPackage) {
UidBatteryConsumer consumer = mock(UidBatteryConsumer.class);
when(consumer.getUid()).thenReturn(APP_UID);
when(consumer.getPackageWithHighestDrain()).thenReturn(highDrainPackage);
return new BatteryEntry(mMockContext, mockHandler, mockUserManager,
consumer, false, packages, packageName);
}
private BatteryEntry createSystemBatteryEntry(int drainType) {
SystemBatteryConsumer consumer = mock(SystemBatteryConsumer.class);
when(consumer.getDrainType()).thenReturn(drainType);
return new BatteryEntry(mMockContext, mockHandler, mockUserManager,
consumer, false, null, null);
}
private BatteryEntry createUserBatteryConsumer(int userId) {
UserBatteryConsumer consumer = mock(UserBatteryConsumer.class);
when(consumer.getUserId()).thenReturn(userId);
return new BatteryEntry(mMockContext, mockHandler, mockUserManager,
consumer, false, null, null);
}
@Test
public void batteryEntryForApp_shouldSetDefaultPackageNameAndLabel() throws Exception {
BatteryEntry entry = createBatteryEntryForApp(null, APP_DEFAULT_PACKAGE_NAME,
HIGH_DRAIN_PACKAGE);
assertThat(entry.getDefaultPackageName()).isEqualTo(APP_DEFAULT_PACKAGE_NAME);
assertThat(entry.getLabel()).isEqualTo(LABEL_PREFIX + APP_DEFAULT_PACKAGE_NAME);
}
@Test
public void batteryEntryForApp_shouldSetLabelAsPackageName_whenPackageCannotBeFound()
throws Exception {
when(mockPackageManager.getApplicationInfo(APP_DEFAULT_PACKAGE_NAME, 0 /* no flags */))
.thenThrow(new NameNotFoundException());
BatteryEntry entry = createBatteryEntryForApp(null, APP_DEFAULT_PACKAGE_NAME, null);
assertThat(entry.getLabel()).isEqualTo(APP_DEFAULT_PACKAGE_NAME);
}
@Test
public void batteryEntryForApp_shouldSetHighestDrainPackage_whenPackagesCannotBeFoundForUid() {
when(mockPackageManager.getPackagesForUid(APP_UID)).thenReturn(null);
BatteryEntry entry = createBatteryEntryForApp(null, null, HIGH_DRAIN_PACKAGE);
assertThat(entry.getLabel()).isEqualTo(LABEL_PREFIX + HIGH_DRAIN_PACKAGE);
}
@Test
public void batteryEntryForApp_shouldSetHighestDrainPackage_whenMultiplePackagesFoundForUid() {
BatteryEntry entry = createBatteryEntryForApp(
new String[] {APP_DEFAULT_PACKAGE_NAME, "package2", "package3"}, null,
HIGH_DRAIN_PACKAGE);
assertThat(entry.getLabel()).isEqualTo(LABEL_PREFIX + HIGH_DRAIN_PACKAGE);
}
@Test
public void batteryEntryForAOD_containCorrectInfo() {
final SystemBatteryConsumer systemBatteryConsumer = mock(SystemBatteryConsumer.class);
when(systemBatteryConsumer.getDrainType())
.thenReturn(SystemBatteryConsumer.DRAIN_TYPE_AMBIENT_DISPLAY);
final BatteryEntry entry = new BatteryEntry(RuntimeEnvironment.application, mockHandler,
mockUserManager, systemBatteryConsumer, false, null, null);
assertThat(entry.iconId).isEqualTo(R.drawable.ic_settings_aod);
assertThat(entry.name).isEqualTo("Ambient display");
}
@Test
public void getTimeInForegroundMs_app() {
final BatteryEntry entry = new BatteryEntry(RuntimeEnvironment.application, mockHandler,
mockUserManager, mUidBatteryConsumer, false, null, null);
when(mUidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_FOREGROUND))
.thenReturn(100L);
assertThat(entry.getTimeInForegroundMs()).isEqualTo(100L);
}
@Test
public void getTimeInForegroundMs_systemConsumer() {
final BatteryEntry entry = new BatteryEntry(RuntimeEnvironment.application, mockHandler,
mockUserManager, mSystemBatteryConsumer, false, null, null);
when(mSystemBatteryConsumer.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_USAGE))
.thenReturn(100L);
assertThat(entry.getTimeInForegroundMs()).isEqualTo(100L);
}
@Test
public void getTimeInBackgroundMs_app() {
final BatteryEntry entry = new BatteryEntry(RuntimeEnvironment.application, mockHandler,
mockUserManager, mUidBatteryConsumer, false, null, null);
when(mUidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_BACKGROUND))
.thenReturn(100L);
assertThat(entry.getTimeInBackgroundMs()).isEqualTo(100L);
}
@Test
public void getTimeInBackgroundMs_systemConsumer() {
final BatteryEntry entry = new BatteryEntry(RuntimeEnvironment.application, mockHandler,
mockUserManager, mSystemBatteryConsumer, false, null, null);
when(mSystemBatteryConsumer.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_USAGE))
.thenReturn(100L);
assertThat(entry.getTimeInBackgroundMs()).isEqualTo(0);
}
@Test
public void testUidCache_switchLocale_shouldCleanCache() {
BatteryEntry.stopRequestQueue();
Locale.setDefault(new Locale("en_US"));
BatteryEntry.sUidCache.put(Integer.toString(APP_UID), null);
assertThat(BatteryEntry.sUidCache).isNotEmpty();
Locale.setDefault(new Locale("zh_TW"));
createBatteryEntryForApp(null, null, HIGH_DRAIN_PACKAGE);
assertThat(BatteryEntry.sUidCache).isEmpty(); // check if cache is clear
}
@Test
public void getKey_UidBatteryConsumer() {
final BatteryEntry entry = createBatteryEntryForApp(null, null, null);
final String key = entry.getKey();
assertThat(key).isEqualTo("123");
}
@Test
public void getKey_SystemBatteryConsumer_returnDrainType() {
final BatteryEntry entry =
createSystemBatteryEntry(SystemBatteryConsumer.DRAIN_TYPE_BLUETOOTH);
final String key = entry.getKey();
assertThat(key).isEqualTo("S|2");
}
@Test
public void getKey_UserBatteryConsumer_returnUserId() {
final BatteryEntry entry = createUserBatteryConsumer(2);
final String key = entry.getKey();
assertThat(key).isEqualTo("U|2");
}
}