Merge changes from topic "feature0"
* changes: Refactor processBatteryDiffData() from DataProcessor to BatteryDiffData class. Refactor PowerUsageFeatureProvider: Cache the config set to avoid generating the set again.
This commit is contained in:
@@ -64,7 +64,7 @@ public class PowerUsageFeatureProviderImplTest {
|
||||
|
||||
@Test
|
||||
public void testIsBatteryUsageEnabled_returnFalse() {
|
||||
assertThat(mPowerFeatureProvider.isBatteryUsageEnabled(mContext)).isTrue();
|
||||
assertThat(mPowerFeatureProvider.isBatteryUsageEnabled()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -101,7 +101,7 @@ public final class BatteryChartPreferenceControllerTest {
|
||||
doReturn(resources).when(mContext).getResources();
|
||||
doReturn(Set.of("com.android.gms.persistent"))
|
||||
.when(mFeatureFactory.powerUsageFeatureProvider)
|
||||
.getHideApplicationSet(mContext);
|
||||
.getHideApplicationSet();
|
||||
doReturn(mLayoutParams).when(mDailyChartView).getLayoutParams();
|
||||
doReturn(mIntent).when(mContext).registerReceiver(any(), any());
|
||||
doReturn(100).when(mIntent).getIntExtra(eq(BatteryManager.EXTRA_SCALE), anyInt());
|
||||
|
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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.batteryusage;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.anyInt;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BatteryDiffDataTest {
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Mock private ApplicationsState mApplicationsState;
|
||||
@Mock private ApplicationsState.AppEntry mAppEntry;
|
||||
@Mock private ApplicationInfo mApplicationInfo;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void needsCombineInSystemApp_isHidden_returnTrue() {
|
||||
final int currentUserId = mContext.getUserId();
|
||||
final BatteryHistEntry hiddenHistEntry = createBatteryHistEntry(
|
||||
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
|
||||
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, true);
|
||||
final BatteryDiffEntry hiddenDiffEntry = new BatteryDiffEntry(
|
||||
mContext,
|
||||
/*foregroundUsageTimeInMs=*/ 0,
|
||||
/*backgroundUsageTimeInMs=*/ 0,
|
||||
/*screenOnTimeInMs=*/ 0,
|
||||
/*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0,
|
||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0,
|
||||
/*cachedUsageConsumePower=*/ 0,
|
||||
hiddenHistEntry);
|
||||
|
||||
boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
|
||||
hiddenDiffEntry, List.of(), mApplicationsState);
|
||||
|
||||
assertThat(needsCombineInSystemApp).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void needsCombineInSystemApp_isSystemApp_returnTrue() {
|
||||
final int currentUserId = mContext.getUserId();
|
||||
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
|
||||
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
|
||||
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, false);
|
||||
final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry(
|
||||
mContext,
|
||||
/*foregroundUsageTimeInMs=*/ 0,
|
||||
/*backgroundUsageTimeInMs=*/ 0,
|
||||
/*screenOnTimeInMs=*/ 0,
|
||||
/*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0,
|
||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0,
|
||||
/*cachedUsageConsumePower=*/ 0,
|
||||
batteryHistEntry);
|
||||
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
|
||||
|
||||
boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
|
||||
batteryDiffEntry, List.of(), mApplicationsState);
|
||||
|
||||
assertThat(needsCombineInSystemApp).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void needsCombineInSystemApp_notSystemApp_returnFalse() {
|
||||
final int currentUserId = mContext.getUserId();
|
||||
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
|
||||
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
|
||||
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, false);
|
||||
final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry(
|
||||
mContext,
|
||||
/*foregroundUsageTimeInMs=*/ 0,
|
||||
/*backgroundUsageTimeInMs=*/ 0,
|
||||
/*screenOnTimeInMs=*/ 0,
|
||||
/*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0,
|
||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0,
|
||||
/*cachedUsageConsumePower=*/ 0,
|
||||
batteryHistEntry);
|
||||
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
mApplicationInfo.flags = 0;
|
||||
|
||||
boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
|
||||
batteryDiffEntry, List.of(), mApplicationsState);
|
||||
|
||||
assertThat(needsCombineInSystemApp).isFalse();
|
||||
}
|
||||
|
||||
private static BatteryHistEntry createBatteryHistEntry(
|
||||
final String packageName, final String appLabel, final double consumePower,
|
||||
final double foregroundUsageConsumePower,
|
||||
final double foregroundServiceUsageConsumePower,
|
||||
final double backgroundUsageConsumePower, final double cachedUsageConsumePower,
|
||||
final long uid, final long userId, final int consumerType,
|
||||
final long foregroundUsageTimeInMs, final long backgroundUsageTimeInMs,
|
||||
final boolean isHidden) {
|
||||
// Only insert required fields.
|
||||
final BatteryInformation batteryInformation =
|
||||
BatteryInformation
|
||||
.newBuilder()
|
||||
.setAppLabel(appLabel)
|
||||
.setConsumePower(consumePower)
|
||||
.setForegroundUsageConsumePower(foregroundUsageConsumePower)
|
||||
.setForegroundServiceUsageConsumePower(foregroundServiceUsageConsumePower)
|
||||
.setBackgroundUsageConsumePower(backgroundUsageConsumePower)
|
||||
.setCachedUsageConsumePower(cachedUsageConsumePower)
|
||||
.setForegroundUsageTimeInMs(foregroundUsageTimeInMs)
|
||||
.setBackgroundUsageTimeInMs(backgroundUsageTimeInMs)
|
||||
.setIsHidden(isHidden)
|
||||
.build();
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, packageName);
|
||||
values.put(BatteryHistEntry.KEY_UID, uid);
|
||||
values.put(BatteryHistEntry.KEY_USER_ID, userId);
|
||||
values.put(BatteryHistEntry.KEY_CONSUMER_TYPE, consumerType);
|
||||
values.put(BatteryHistEntry.KEY_BATTERY_INFORMATION,
|
||||
ConvertUtils.convertBatteryInformationToString(batteryInformation));
|
||||
return new BatteryHistEntry(values);
|
||||
}
|
||||
}
|
@@ -90,7 +90,7 @@ public final class BatteryUsageBreakdownControllerTest {
|
||||
doReturn(resources).when(mContext).getResources();
|
||||
doReturn(Set.of("com.android.gms.persistent"))
|
||||
.when(mFeatureFactory.powerUsageFeatureProvider)
|
||||
.getHideApplicationSet(mContext);
|
||||
.getHideApplicationSet();
|
||||
mBatteryUsageBreakdownController = createController();
|
||||
mBatteryUsageBreakdownController.mAppListPreferenceGroup = mAppListPreferenceGroup;
|
||||
mBatteryDiffEntry = new BatteryDiffEntry(
|
||||
@@ -105,10 +105,8 @@ public final class BatteryUsageBreakdownControllerTest {
|
||||
/*cachedUsageConsumePower=*/ 0,
|
||||
mBatteryHistEntry);
|
||||
mBatteryDiffEntry = spy(mBatteryDiffEntry);
|
||||
mBatteryUsageBreakdownController.mBatteryDiffData =
|
||||
new BatteryDiffData(Arrays.asList(mBatteryDiffEntry), Arrays.asList());
|
||||
mBatteryUsageBreakdownController.mBatteryDiffData.setTotalConsumePower();
|
||||
mBatteryUsageBreakdownController.mBatteryDiffData.sortEntries();
|
||||
mBatteryUsageBreakdownController.mBatteryDiffData = new BatteryDiffData(mContext,
|
||||
Arrays.asList(mBatteryDiffEntry), Arrays.asList(), /* isAccumulated= */ false);
|
||||
// Adds fake testing data.
|
||||
BatteryDiffEntry.sResourceCache.put(
|
||||
"fakeBatteryDiffEntryKey",
|
||||
|
@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.anyInt;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.spy;
|
||||
@@ -33,7 +32,6 @@ import android.app.usage.UsageEvents.Event;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.BatteryConsumer;
|
||||
import android.os.BatteryManager;
|
||||
@@ -45,7 +43,6 @@ import android.text.format.DateUtils;
|
||||
|
||||
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -78,9 +75,6 @@ public final class DataProcessorTest {
|
||||
@Mock private BatteryUsageStats mBatteryUsageStats;
|
||||
@Mock private UserManager mUserManager;
|
||||
@Mock private IUsageStatsManager mUsageStatsManager;
|
||||
@Mock private ApplicationsState mApplicationsState;
|
||||
@Mock private ApplicationsState.AppEntry mAppEntry;
|
||||
@Mock private ApplicationInfo mApplicationInfo;
|
||||
@Mock private BatteryEntry mMockBatteryEntry1;
|
||||
@Mock private BatteryEntry mMockBatteryEntry2;
|
||||
@Mock private BatteryEntry mMockBatteryEntry3;
|
||||
@@ -1237,7 +1231,7 @@ public final class DataProcessorTest {
|
||||
final List<Integer> levels = List.of(100, 100);
|
||||
hourlyBatteryLevelsPerDay.add(
|
||||
new BatteryLevelData.PeriodBatteryLevelData(timestamps, levels));
|
||||
when(mPowerUsageFeatureProvider.getHideApplicationSet(mContext))
|
||||
when(mPowerUsageFeatureProvider.getHideApplicationSet())
|
||||
.thenReturn(Set.of("package1"));
|
||||
|
||||
final Map<Integer, Map<Integer, BatteryDiffData>> resultMap =
|
||||
@@ -1330,7 +1324,7 @@ public final class DataProcessorTest {
|
||||
final List<Integer> levels = List.of(100, 100);
|
||||
hourlyBatteryLevelsPerDay.add(
|
||||
new BatteryLevelData.PeriodBatteryLevelData(timestamps, levels));
|
||||
when(mPowerUsageFeatureProvider.getHideBackgroundUsageTimeSet(mContext))
|
||||
when(mPowerUsageFeatureProvider.getHideBackgroundUsageTimeSet())
|
||||
.thenReturn(new HashSet(Arrays.asList((CharSequence) "package2")));
|
||||
|
||||
final Map<Integer, Map<Integer, BatteryDiffData>> resultMap =
|
||||
@@ -1403,8 +1397,6 @@ public final class DataProcessorTest {
|
||||
|
||||
final BatteryDiffData batteryDiffData = DataProcessor.generateBatteryDiffData(mContext,
|
||||
DataProcessor.convertToBatteryHistEntry(batteryEntryList, mBatteryUsageStats));
|
||||
batteryDiffData.setTotalConsumePower();
|
||||
batteryDiffData.sortEntries();
|
||||
|
||||
assertBatteryDiffEntry(
|
||||
batteryDiffData.getAppDiffEntryList().get(0), 0, /*uid=*/ 2L,
|
||||
@@ -1611,93 +1603,6 @@ public final class DataProcessorTest {
|
||||
assertThat(DataProcessor.getScreenOnTime(appUsageMap, userId, packageName)).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void needsCombineInSystemApp_isHidden_returnTrue() {
|
||||
final int currentUserId = mContext.getUserId();
|
||||
final BatteryHistEntry hiddenHistEntry = createBatteryHistEntry(
|
||||
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
|
||||
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, true);
|
||||
final BatteryDiffEntry hiddenDiffEntry = new BatteryDiffEntry(
|
||||
mContext,
|
||||
/*foregroundUsageTimeInMs=*/ 0,
|
||||
/*backgroundUsageTimeInMs=*/ 0,
|
||||
/*screenOnTimeInMs=*/ 0,
|
||||
/*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0,
|
||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0,
|
||||
/*cachedUsageConsumePower=*/ 0,
|
||||
hiddenHistEntry);
|
||||
|
||||
boolean needsCombineInSystemApp = DataProcessor.needsCombineInSystemApp(
|
||||
hiddenDiffEntry, List.of(), mApplicationsState);
|
||||
|
||||
assertThat(needsCombineInSystemApp).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void needsCombineInSystemApp_isSystemApp_returnTrue() {
|
||||
final int currentUserId = mContext.getUserId();
|
||||
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
|
||||
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
|
||||
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, false);
|
||||
final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry(
|
||||
mContext,
|
||||
/*foregroundUsageTimeInMs=*/ 0,
|
||||
/*backgroundUsageTimeInMs=*/ 0,
|
||||
/*screenOnTimeInMs=*/ 0,
|
||||
/*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0,
|
||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0,
|
||||
/*cachedUsageConsumePower=*/ 0,
|
||||
batteryHistEntry);
|
||||
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
|
||||
|
||||
boolean needsCombineInSystemApp = DataProcessor.needsCombineInSystemApp(
|
||||
batteryDiffEntry, List.of(), mApplicationsState);
|
||||
|
||||
assertThat(needsCombineInSystemApp).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void needsCombineInSystemApp_notSystemApp_returnFalse() {
|
||||
final int currentUserId = mContext.getUserId();
|
||||
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
|
||||
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
|
||||
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, false);
|
||||
final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry(
|
||||
mContext,
|
||||
/*foregroundUsageTimeInMs=*/ 0,
|
||||
/*backgroundUsageTimeInMs=*/ 0,
|
||||
/*screenOnTimeInMs=*/ 0,
|
||||
/*consumePower=*/ 0,
|
||||
/*foregroundUsageConsumePower=*/ 0,
|
||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
||||
/*backgroundUsageConsumePower=*/ 0,
|
||||
/*cachedUsageConsumePower=*/ 0,
|
||||
batteryHistEntry);
|
||||
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
mApplicationInfo.flags = 0;
|
||||
|
||||
boolean needsCombineInSystemApp = DataProcessor.needsCombineInSystemApp(
|
||||
batteryDiffEntry, List.of(), mApplicationsState);
|
||||
|
||||
assertThat(needsCombineInSystemApp).isFalse();
|
||||
}
|
||||
|
||||
private static Map<Long, Map<String, BatteryHistEntry>> createHistoryMap(
|
||||
final long[] timestamps, final int[] levels) {
|
||||
final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
|
||||
|
Reference in New Issue
Block a user