Merge changes from topic "anomaly_system_app" into pi-dev am: dc8fff76f3
am: 9122efebac
Change-Id: I384476edcb6ca9702a2895f6f5f4b55eb11cc86f
This commit is contained in:
@@ -37,8 +37,10 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
@@ -53,6 +55,7 @@ import com.android.settings.R;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -81,7 +84,7 @@ public class BatteryUtilsTest {
|
||||
private static final long TIME_SINCE_LAST_FULL_CHARGE_US =
|
||||
TIME_SINCE_LAST_FULL_CHARGE_MS * 1000;
|
||||
|
||||
private static final int UID = 123;
|
||||
private static final int UID = 12345;
|
||||
private static final long TIME_EXPECTED_FOREGROUND = 1500;
|
||||
private static final long TIME_EXPECTED_BACKGROUND = 6000;
|
||||
private static final long TIME_EXPECTED_ALL = 7500;
|
||||
@@ -141,6 +144,8 @@ public class BatteryUtilsTest {
|
||||
private ApplicationInfo mHighApplicationInfo;
|
||||
@Mock
|
||||
private ApplicationInfo mLowApplicationInfo;
|
||||
@Mock
|
||||
private PowerWhitelistBackend mPowerWhitelistBackend;
|
||||
private BatteryUtils mBatteryUtils;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private PowerUsageFeatureProvider mProvider;
|
||||
@@ -166,9 +171,9 @@ public class BatteryUtilsTest {
|
||||
when(mBatteryStatsHelper.getStats().computeBatteryRealtime(anyLong(), anyInt())).thenReturn(
|
||||
TIME_SINCE_LAST_FULL_CHARGE_US);
|
||||
|
||||
when(mPackageManager.getApplicationInfo(HIGH_SDK_PACKAGE, PackageManager.GET_META_DATA))
|
||||
when(mPackageManager.getApplicationInfo(eq(HIGH_SDK_PACKAGE), anyInt()))
|
||||
.thenReturn(mHighApplicationInfo);
|
||||
when(mPackageManager.getApplicationInfo(LOW_SDK_PACKAGE, PackageManager.GET_META_DATA))
|
||||
when(mPackageManager.getApplicationInfo(eq(LOW_SDK_PACKAGE), anyInt()))
|
||||
.thenReturn(mLowApplicationInfo);
|
||||
mHighApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O;
|
||||
mLowApplicationInfo.targetSdkVersion = Build.VERSION_CODES.L;
|
||||
@@ -570,4 +575,42 @@ public class BatteryUtilsTest {
|
||||
assertThat(mBatteryUtils.isAppHeavilyUsed(mBatteryStatsHelper, mUserManager, UID,
|
||||
DISCHARGE_AMOUNT /* threshold */ )).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldHideAnomaly_systemAppWithLauncher_returnTrue() {
|
||||
final List<ResolveInfo> resolveInfos = new ArrayList<>();
|
||||
final ResolveInfo resolveInfo = new ResolveInfo();
|
||||
resolveInfo.activityInfo = new ActivityInfo();
|
||||
resolveInfo.activityInfo.packageName = HIGH_SDK_PACKAGE;
|
||||
|
||||
doReturn(resolveInfos).when(mPackageManager).queryIntentActivities(any(), anyInt());
|
||||
doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(UID);
|
||||
mHighApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
|
||||
|
||||
assertThat(mBatteryUtils.shouldHideAnomaly(mPowerWhitelistBackend, UID)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldHideAnomaly_systemAppWithoutLauncher_returnTrue() {
|
||||
doReturn(new ArrayList<>()).when(mPackageManager).queryIntentActivities(any(), anyInt());
|
||||
doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(UID);
|
||||
mHighApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
|
||||
|
||||
assertThat(mBatteryUtils.shouldHideAnomaly(mPowerWhitelistBackend, UID)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldHideAnomaly_systemUid_returnTrue() {
|
||||
final int systemUid = Process.ROOT_UID;
|
||||
doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(systemUid);
|
||||
|
||||
assertThat(mBatteryUtils.shouldHideAnomaly(mPowerWhitelistBackend, systemUid)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldHideAnomaly_normalApp_returnFalse() {
|
||||
doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(UID);
|
||||
|
||||
assertThat(mBatteryUtils.shouldHideAnomaly(mPowerWhitelistBackend, UID)).isFalse();
|
||||
}
|
||||
}
|
||||
|
@@ -141,6 +141,31 @@ public class AnomalyDetectionJobServiceTest {
|
||||
anyInt(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveAnomalyToDatabase_systemApp_doNotSaveButLog() {
|
||||
final ArrayList<String> cookies = new ArrayList<>();
|
||||
cookies.add(SUBSCRIBER_COOKIES_AUTO_RESTRICTION);
|
||||
mBundle.putStringArrayList(StatsManager.EXTRA_STATS_BROADCAST_SUBSCRIBER_COOKIES, cookies);
|
||||
doReturn(SYSTEM_PACKAGE).when(mBatteryUtils).getPackageName(anyInt());
|
||||
doReturn(false).when(mPowerWhitelistBackend).isSysWhitelisted(SYSTEM_PACKAGE);
|
||||
doReturn(Process.FIRST_APPLICATION_UID).when(
|
||||
mAnomalyDetectionJobService).extractUidFromStatsDimensionsValue(any());
|
||||
doReturn(true).when(mBatteryUtils).shouldHideAnomaly(any(), anyInt());
|
||||
|
||||
mAnomalyDetectionJobService.saveAnomalyToDatabase(mContext, mBatteryStatsHelper,
|
||||
mUserManager, mBatteryDatabaseManager, mBatteryUtils, mPolicy,
|
||||
mPowerWhitelistBackend, mContext.getContentResolver(),
|
||||
mFeatureFactory.powerUsageFeatureProvider,
|
||||
mFeatureFactory.metricsFeatureProvider, mBundle);
|
||||
|
||||
verify(mBatteryDatabaseManager, never()).insertAnomaly(anyInt(), anyString(), anyInt(),
|
||||
anyInt(), anyLong());
|
||||
verify(mFeatureFactory.metricsFeatureProvider).action(mContext,
|
||||
MetricsProto.MetricsEvent.ACTION_ANOMALY_IGNORED,
|
||||
SYSTEM_PACKAGE,
|
||||
Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, ANOMALY_TYPE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveAnomalyToDatabase_systemUid_doNotSave() {
|
||||
doReturn(Process.SYSTEM_UID).when(
|
||||
|
Reference in New Issue
Block a user