Merge "Change anomalyType to ArraySet in AppInfo" into pi-dev

This commit is contained in:
Lei Yu
2018-03-09 17:38:53 +00:00
committed by Android (Google) Code Review
7 changed files with 87 additions and 46 deletions

View File

@@ -40,7 +40,6 @@ import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class BatteryDatabaseManagerTest {
private static String PACKAGE_NAME_NEW = "com.android.app1";
private static int UID_NEW = 345;
private static int TYPE_NEW = 1;
@@ -53,6 +52,9 @@ public class BatteryDatabaseManagerTest {
private Context mContext;
private BatteryDatabaseManager mBatteryDatabaseManager;
private AppInfo mNewAppInfo;
private AppInfo mOldAppInfo;
private AppInfo mCombinedAppInfo;
@Before
public void setUp() {
@@ -60,6 +62,23 @@ public class BatteryDatabaseManagerTest {
mContext = RuntimeEnvironment.application;
mBatteryDatabaseManager = spy(BatteryDatabaseManager.getInstance(mContext));
mNewAppInfo = new AppInfo.Builder()
.setUid(UID_NEW)
.setPackageName(PACKAGE_NAME_NEW)
.addAnomalyType(TYPE_NEW)
.build();
mOldAppInfo = new AppInfo.Builder()
.setUid(UID_OLD)
.setPackageName(PACKAGE_NAME_OLD)
.addAnomalyType(TYPE_OLD)
.build();
mCombinedAppInfo = new AppInfo.Builder()
.setUid(UID_NEW)
.setPackageName(PACKAGE_NAME_NEW)
.addAnomalyType(TYPE_NEW)
.addAnomalyType(TYPE_OLD)
.build();
}
@After
@@ -77,23 +96,19 @@ public class BatteryDatabaseManagerTest {
// In database, it contains two record
List<AppInfo> totalAppInfos = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */,
AnomalyDatabaseHelper.State.NEW);
assertThat(totalAppInfos).hasSize(2);
assertAppInfo(totalAppInfos.get(0), UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW);
assertAppInfo(totalAppInfos.get(1), UID_OLD, PACKAGE_NAME_OLD, TYPE_OLD);
assertThat(totalAppInfos).containsExactly(mNewAppInfo, mOldAppInfo);
// Only one record shows up if we query by timestamp
List<AppInfo> appInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
AnomalyDatabaseHelper.State.NEW);
assertThat(appInfos).hasSize(1);
assertAppInfo(appInfos.get(0), UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW);
assertThat(appInfos).containsExactly(mNewAppInfo);
mBatteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(ONE_DAY_BEFORE);
// The obsolete record is removed from database
List<AppInfo> appInfos1 = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */,
AnomalyDatabaseHelper.State.NEW);
assertThat(appInfos1).hasSize(1);
assertAppInfo(appInfos1.get(0), UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW);
assertThat(appInfos1).containsExactly(mNewAppInfo);
}
@Test
@@ -113,19 +128,24 @@ public class BatteryDatabaseManagerTest {
// The state of PACKAGE_NAME_NEW is still new
List<AppInfo> newAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
AnomalyDatabaseHelper.State.NEW);
assertThat(newAppInfos).hasSize(1);
assertAppInfo(newAppInfos.get(0), UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW);
assertThat(newAppInfos).containsExactly(mNewAppInfo);
// The state of PACKAGE_NAME_OLD is changed to handled
List<AppInfo> handledAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
AnomalyDatabaseHelper.State.HANDLED);
assertThat(handledAppInfos).hasSize(1);
assertAppInfo(handledAppInfos.get(0), UID_OLD, PACKAGE_NAME_OLD, TYPE_OLD);
assertThat(handledAppInfos).containsExactly(mOldAppInfo);
}
private void assertAppInfo(final AppInfo appInfo, int uid, String packageName, int type) {
assertThat(appInfo.packageName).isEqualTo(packageName);
assertThat(appInfo.anomalyType).isEqualTo(type);
assertThat(appInfo.uid).isEqualTo(uid);
@Test
public void testQueryAnomalies_removeDuplicateByUid() {
mBatteryDatabaseManager.insertAnomaly(UID_NEW, PACKAGE_NAME_NEW, TYPE_NEW,
AnomalyDatabaseHelper.State.NEW, NOW);
mBatteryDatabaseManager.insertAnomaly(UID_NEW, PACKAGE_NAME_NEW, TYPE_OLD,
AnomalyDatabaseHelper.State.NEW, NOW);
// Only contain one AppInfo with multiple types
List<AppInfo> newAppInfos = mBatteryDatabaseManager.queryAllAnomalies(ONE_DAY_BEFORE,
AnomalyDatabaseHelper.State.NEW);
assertThat(newAppInfos).containsExactly(mCombinedAppInfo);
}
}

View File

@@ -36,7 +36,8 @@ import java.util.List;
public class AppInfoTest {
private static final String PACKAGE_NAME = "com.android.app";
private static final int ANOMALY_TYPE = Anomaly.AnomalyType.WAKE_LOCK;
private static final int TYPE_WAKELOCK = Anomaly.AnomalyType.WAKE_LOCK;
private static final int TYPE_WAKEUP = Anomaly.AnomalyType.WAKEUP_ALARM;
private static final long SCREEN_TIME_MS = DateUtils.HOUR_IN_MILLIS;
private static final int UID = 3452;
@@ -46,7 +47,8 @@ public class AppInfoTest {
public void setUp() {
mAppInfo = new AppInfo.Builder()
.setPackageName(PACKAGE_NAME)
.setAnomalyType(ANOMALY_TYPE)
.addAnomalyType(TYPE_WAKELOCK)
.addAnomalyType(TYPE_WAKEUP)
.setScreenOnTimeMs(SCREEN_TIME_MS)
.setUid(UID)
.build();
@@ -61,7 +63,7 @@ public class AppInfoTest {
final AppInfo appInfo = new AppInfo(parcel);
assertThat(appInfo.packageName).isEqualTo(PACKAGE_NAME);
assertThat(appInfo.anomalyType).isEqualTo(ANOMALY_TYPE);
assertThat(appInfo.anomalyTypes).containsExactly(TYPE_WAKELOCK, TYPE_WAKEUP);
assertThat(appInfo.screenOnTimeMs).isEqualTo(SCREEN_TIME_MS);
assertThat(appInfo.uid).isEqualTo(UID);
}
@@ -70,7 +72,7 @@ public class AppInfoTest {
public void testCompareTo_hasCorrectOrder() {
final AppInfo appInfo = new AppInfo.Builder()
.setPackageName(PACKAGE_NAME)
.setAnomalyType(ANOMALY_TYPE)
.addAnomalyType(TYPE_WAKELOCK)
.setScreenOnTimeMs(SCREEN_TIME_MS + 100)
.build();
@@ -85,7 +87,7 @@ public class AppInfoTest {
@Test
public void testBuilder() {
assertThat(mAppInfo.packageName).isEqualTo(PACKAGE_NAME);
assertThat(mAppInfo.anomalyType).isEqualTo(ANOMALY_TYPE);
assertThat(mAppInfo.anomalyTypes).containsExactly(TYPE_WAKELOCK, TYPE_WAKEUP);
assertThat(mAppInfo.screenOnTimeMs).isEqualTo(SCREEN_TIME_MS);
assertThat(mAppInfo.uid).isEqualTo(UID);
}

View File

@@ -77,6 +77,7 @@ public class HighUsageTipTest {
@Test
public void toString_containsAppData() {
assertThat(mBatteryTip.toString()).isEqualTo(
"type=2 state=0 { packageName=com.android.app,anomalyType=0,screenTime=1800000 }");
"type=2 state=0 { packageName=com.android.app,anomalyTypes={},screenTime=1800000 "
+ "}");
}
}

View File

@@ -39,9 +39,10 @@ import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
public class RestrictAppTipTest {
private static final String PACKAGE_NAME = "com.android.app";
private static final String DISPLAY_NAME = "app";
private static final int ANOMALY_WAKEUP = 0;
private static final int ANOMALY_WAKELOCK = 1;
private Context mContext;
private RestrictAppTip mNewBatteryTip;
@@ -64,7 +65,11 @@ public class RestrictAppTipTest {
doReturn(DISPLAY_NAME).when(mApplicationInfo).loadLabel(mPackageManager);
mUsageAppList = new ArrayList<>();
mUsageAppList.add(new AppInfo.Builder().setPackageName(PACKAGE_NAME).build());
mUsageAppList.add(new AppInfo.Builder()
.setPackageName(PACKAGE_NAME)
.addAnomalyType(ANOMALY_WAKEUP)
.addAnomalyType(ANOMALY_WAKELOCK)
.build());
mNewBatteryTip = new RestrictAppTip(BatteryTip.StateType.NEW, mUsageAppList);
mHandledBatteryTip = new RestrictAppTip(BatteryTip.StateType.HANDLED, mUsageAppList);
mInvisibleBatteryTip = new RestrictAppTip(BatteryTip.StateType.INVISIBLE, mUsageAppList);
@@ -125,6 +130,6 @@ public class RestrictAppTipTest {
@Test
public void toString_containsAppData() {
assertThat(mNewBatteryTip.toString()).isEqualTo(
"type=1 state=0 { packageName=com.android.app,anomalyType=0,screenTime=0 }");
"type=1 state=0 { packageName=com.android.app,anomalyTypes={0, 1},screenTime=0 }");
}
}