Add log for anomaly.
This cl adds all the info that need to be logged to Anomaly and add a dump method for log. Bug: 63776178 Test: RunSettingsRoboTests Change-Id: If762bfca725fee07944ffa23998332c81335a37f
This commit is contained in:
@@ -23,6 +23,7 @@ import android.os.Build;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.annotation.Config;
|
||||
@@ -36,10 +37,14 @@ public class AnomalyTest {
|
||||
private static long WAKE_LOCK_TIME_MS = 1500;
|
||||
private static String PACKAGE_NAME = "com.android.settings";
|
||||
private static String DISPLAY_NAME = "settings";
|
||||
private static long BLUETOOTH_TIME_MS = 2555555;
|
||||
private static int WAKEUP_ALARM_COUNT = 100;
|
||||
|
||||
@Test
|
||||
public void testBuilder_buildCorrectly() {
|
||||
Anomaly anomaly = new Anomaly.Builder()
|
||||
private Anomaly mAnomaly;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mAnomaly = new Anomaly.Builder()
|
||||
.setType(TYPE)
|
||||
.setUid(UID)
|
||||
.setWakeLockTimeMs(WAKE_LOCK_TIME_MS)
|
||||
@@ -47,14 +52,28 @@ public class AnomalyTest {
|
||||
.setDisplayName(DISPLAY_NAME)
|
||||
.setTargetSdkVersion(SDK_VERSION)
|
||||
.setBackgroundRestrictionEnabled(true)
|
||||
.setBluetoothScanningTimeMs(BLUETOOTH_TIME_MS)
|
||||
.setWakeupAlarmCount(WAKEUP_ALARM_COUNT)
|
||||
.build();
|
||||
}
|
||||
|
||||
assertThat(anomaly.type).isEqualTo(TYPE);
|
||||
assertThat(anomaly.uid).isEqualTo(UID);
|
||||
assertThat(anomaly.wakelockTimeMs).isEqualTo(WAKE_LOCK_TIME_MS);
|
||||
assertThat(anomaly.packageName).isEqualTo(PACKAGE_NAME);
|
||||
assertThat(anomaly.displayName).isEqualTo(DISPLAY_NAME);
|
||||
assertThat(anomaly.targetSdkVersion).isEqualTo(SDK_VERSION);
|
||||
assertThat(anomaly.backgroundRestrictionEnabled).isTrue();
|
||||
@Test
|
||||
public void testBuilder_buildCorrectly() {
|
||||
assertThat(mAnomaly.type).isEqualTo(TYPE);
|
||||
assertThat(mAnomaly.uid).isEqualTo(UID);
|
||||
assertThat(mAnomaly.wakelockTimeMs).isEqualTo(WAKE_LOCK_TIME_MS);
|
||||
assertThat(mAnomaly.packageName).isEqualTo(PACKAGE_NAME);
|
||||
assertThat(mAnomaly.displayName).isEqualTo(DISPLAY_NAME);
|
||||
assertThat(mAnomaly.targetSdkVersion).isEqualTo(SDK_VERSION);
|
||||
assertThat(mAnomaly.backgroundRestrictionEnabled).isTrue();
|
||||
assertThat(mAnomaly.wakeupAlarmCount).isEqualTo(WAKEUP_ALARM_COUNT);
|
||||
assertThat(mAnomaly.bluetoothScanningTimeMs).isEqualTo(BLUETOOTH_TIME_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
assertThat(mAnomaly.toString()).isEqualTo(
|
||||
"type=wakelock uid=111 package=com.android.settings displayName=settings"
|
||||
+ " wakelockTimeMs=1500 wakeupAlarmCount=100 bluetoothTimeMs=2555555");
|
||||
}
|
||||
}
|
||||
|
@@ -155,6 +155,7 @@ public class BluetoothScanAnomalyDetectorTest {
|
||||
return new Anomaly.Builder()
|
||||
.setUid(uid)
|
||||
.setType(Anomaly.AnomalyType.BLUETOOTH_SCAN)
|
||||
.setBluetoothScanningTimeMs(ANOMALY_BLUETOOTH_SCANNING_TIME)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@@ -99,6 +99,8 @@ public class WakeLockAnomalyDetectorTest {
|
||||
private WakeLockAnomalyDetector mWakelockAnomalyDetector;
|
||||
private Context mContext;
|
||||
private List<BatterySipper> mUsageList;
|
||||
private Anomaly mAnomaly;
|
||||
private Anomaly mTargetAnomaly;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
@@ -152,41 +154,40 @@ public class WakeLockAnomalyDetectorTest {
|
||||
mUsageList.add(mTargetSipper);
|
||||
mUsageList.add(mInactiveSipper);
|
||||
doReturn(mUsageList).when(mBatteryStatsHelper).getUsageList();
|
||||
|
||||
mAnomaly = createWakeLockAnomaly(ANOMALY_UID);
|
||||
mTargetAnomaly = createWakeLockAnomaly(TARGET_UID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetectAnomalies_containsAnomaly_detectIt() {
|
||||
doReturn(BatteryUtils.UID_NULL).when(mBatteryUtils).getPackageUid(nullable(String.class));
|
||||
final Anomaly anomaly = new Anomaly.Builder()
|
||||
.setUid(ANOMALY_UID)
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.build();
|
||||
final Anomaly targetAnomaly = new Anomaly.Builder()
|
||||
.setUid(TARGET_UID)
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.build();
|
||||
|
||||
List<Anomaly> mAnomalies = mWakelockAnomalyDetector.detectAnomalies(mBatteryStatsHelper);
|
||||
|
||||
assertThat(mAnomalies).containsExactly(anomaly, targetAnomaly);
|
||||
assertThat(mAnomalies).containsExactly(mAnomaly, mTargetAnomaly);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetectAnomalies_containsTargetPackage_detectIt() {
|
||||
doReturn(TARGET_UID).when(mBatteryUtils).getPackageUid(TARGET_PACKAGE_NAME);
|
||||
final Anomaly targetAnomaly = new Anomaly.Builder()
|
||||
.setUid(TARGET_UID)
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.build();
|
||||
|
||||
List<Anomaly> mAnomalies = mWakelockAnomalyDetector.detectAnomalies(mBatteryStatsHelper,
|
||||
TARGET_PACKAGE_NAME);
|
||||
|
||||
assertThat(mAnomalies).containsExactly(targetAnomaly);
|
||||
assertThat(mAnomalies).containsExactly(mTargetAnomaly);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsThresholdFromPolicy() {
|
||||
assertThat(mWakelockAnomalyDetector.mWakeLockThresholdMs).isEqualTo(WAKELOCK_THRESHOLD_MS);
|
||||
}
|
||||
|
||||
private Anomaly createWakeLockAnomaly(int uid) {
|
||||
return new Anomaly.Builder()
|
||||
.setUid(uid)
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.setWakeLockTimeMs(ANOMALY_WAKELOCK_TIME_MS)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@@ -69,6 +69,7 @@ public class WakeupAlarmAnomalyDetectorTest {
|
||||
1 * DateUtils.HOUR_IN_MILLIS + 10 * DateUtils.MINUTE_IN_MILLIS;
|
||||
private static final int ANOMALY_WAKEUP_COUNT = 500;
|
||||
private static final int NORMAL_WAKEUP_COUNT = 61;
|
||||
private static final int ANOMALY_WAKEUP_FREQUENCY = 428; // count per hour
|
||||
@Mock
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
@Mock
|
||||
@@ -145,6 +146,7 @@ public class WakeupAlarmAnomalyDetectorTest {
|
||||
.setType(Anomaly.AnomalyType.WAKEUP_ALARM)
|
||||
.setTargetSdkVersion(ANOMALY_SDK)
|
||||
.setBackgroundRestrictionEnabled(ANOMALY_BACKGROUND_RESTRICTION_ON)
|
||||
.setWakeupAlarmCount(ANOMALY_WAKEUP_FREQUENCY)
|
||||
.build();
|
||||
mTargetAnomaly = new Anomaly.Builder()
|
||||
.setUid(TARGET_UID)
|
||||
@@ -152,6 +154,7 @@ public class WakeupAlarmAnomalyDetectorTest {
|
||||
.setType(Anomaly.AnomalyType.WAKEUP_ALARM)
|
||||
.setTargetSdkVersion(TARGET_SDK)
|
||||
.setBackgroundRestrictionEnabled(TARGET_BACKGROUND_RESTRICTION_ON)
|
||||
.setWakeupAlarmCount(ANOMALY_WAKEUP_FREQUENCY)
|
||||
.build();
|
||||
|
||||
mWakeupAlarmAnomalyDetector = spy(
|
||||
|
Reference in New Issue
Block a user