Merge "Update wakeup anomaly to exclude blacklisted wakeups." into oc-mr1-dev
am: 8bd54d4592
Change-Id: If099f1d66dcb83817d5b6c3e22d4ca3e550d5e71
This commit is contained in:
@@ -40,11 +40,13 @@ import org.robolectric.annotation.Config;
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AnomalyDetectionPolicyTest {
|
||||
private static final String ANOMALY_DETECTION_CONSTANTS_VALUE = "anomaly_detection_enabled=true"
|
||||
private static final String ANOMALY_DETECTION_CONSTANTS_VALUE =
|
||||
"anomaly_detection_enabled=true"
|
||||
+ ",wakelock_enabled=false"
|
||||
+ ",wakelock_threshold=3000"
|
||||
+ ",wakeup_alarm_enabled=true"
|
||||
+ ",wakeup_alarm_threshold=100"
|
||||
+ ",wakeup_blacklisted_tags=tag1:tag2:with%2Ccomma:with%3Acolon"
|
||||
+ ",bluetooth_scan_enabled=true"
|
||||
+ ",bluetooth_scan_threshold=2000";
|
||||
private Context mContext;
|
||||
@@ -58,7 +60,7 @@ public class AnomalyDetectionPolicyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit_containsDataFromSettings() {
|
||||
public void testInit_usesConfigValues() {
|
||||
AnomalyDetectionPolicy anomalyDetectionPolicy = createAnomalyPolicyWithConfig();
|
||||
|
||||
assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue();
|
||||
@@ -66,12 +68,14 @@ public class AnomalyDetectionPolicyTest {
|
||||
assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(3000);
|
||||
assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(100);
|
||||
assertThat(anomalyDetectionPolicy.wakeupBlacklistedTags)
|
||||
.containsExactly("tag1", "tag2", "with,comma", "with:colon");
|
||||
assertThat(anomalyDetectionPolicy.bluetoothScanDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.bluetoothScanThreshold).isEqualTo(2000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit_containsDefaultData() {
|
||||
public void testInit_defaultValues() {
|
||||
Settings.Global.putString(mContext.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS, "");
|
||||
// Mock it to avoid noSuchMethodError
|
||||
@@ -81,18 +85,19 @@ public class AnomalyDetectionPolicyTest {
|
||||
AnomalyDetectionPolicy anomalyDetectionPolicy = new AnomalyDetectionPolicy(mContext,
|
||||
mKeyValueListParserWrapper);
|
||||
|
||||
assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isFalse();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isFalse();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(DateUtils.HOUR_IN_MILLIS);
|
||||
assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isFalse();
|
||||
assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(60);
|
||||
assertThat(anomalyDetectionPolicy.bluetoothScanDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(10);
|
||||
assertThat(anomalyDetectionPolicy.wakeupBlacklistedTags).isNull();
|
||||
assertThat(anomalyDetectionPolicy.bluetoothScanDetectionEnabled).isFalse();
|
||||
assertThat(anomalyDetectionPolicy.bluetoothScanThreshold).isEqualTo(
|
||||
30 * DateUtils.MINUTE_IN_MILLIS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAnomalyDetectorEnabled() {
|
||||
public void testIsAnomalyDetectorEnabled_usesConfigValues() {
|
||||
AnomalyDetectionPolicy anomalyDetectionPolicy = createAnomalyPolicyWithConfig();
|
||||
|
||||
assertThat(anomalyDetectionPolicy.isAnomalyDetectorEnabled(
|
||||
@@ -103,18 +108,37 @@ public class AnomalyDetectionPolicyTest {
|
||||
Anomaly.AnomalyType.BLUETOOTH_SCAN)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAnomalyDetectorEnabled_usesDefaultValues() {
|
||||
Settings.Global.putString(mContext.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS, "");
|
||||
// Mock it to avoid noSuchMethodError
|
||||
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(true));
|
||||
doReturn(false).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(false));
|
||||
|
||||
AnomalyDetectionPolicy anomalyDetectionPolicy = new AnomalyDetectionPolicy(mContext,
|
||||
mKeyValueListParserWrapper);
|
||||
|
||||
assertThat(anomalyDetectionPolicy.isAnomalyDetectorEnabled(
|
||||
Anomaly.AnomalyType.WAKE_LOCK)).isFalse();
|
||||
assertThat(anomalyDetectionPolicy.isAnomalyDetectorEnabled(
|
||||
Anomaly.AnomalyType.WAKEUP_ALARM)).isFalse();
|
||||
assertThat(anomalyDetectionPolicy.isAnomalyDetectorEnabled(
|
||||
Anomaly.AnomalyType.BLUETOOTH_SCAN)).isFalse();
|
||||
}
|
||||
|
||||
private AnomalyDetectionPolicy createAnomalyPolicyWithConfig() {
|
||||
Settings.Global.putString(mContext.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS, ANOMALY_DETECTION_CONSTANTS_VALUE);
|
||||
// Mock it to avoid noSuchMethodError
|
||||
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
|
||||
AnomalyDetectionPolicy.KEY_ANOMALY_DETECTION_ENABLED, true);
|
||||
AnomalyDetectionPolicy.KEY_ANOMALY_DETECTION_ENABLED, false);
|
||||
doReturn(false).when(mKeyValueListParserWrapper).getBoolean(
|
||||
AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, true);
|
||||
AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, false);
|
||||
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
|
||||
AnomalyDetectionPolicy.KEY_WAKEUP_ALARM_DETECTION_ENABLED, false);
|
||||
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
|
||||
AnomalyDetectionPolicy.KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, true);
|
||||
AnomalyDetectionPolicy.KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, false);
|
||||
|
||||
return new AnomalyDetectionPolicy(mContext, mKeyValueListParserWrapper);
|
||||
}
|
||||
|
@@ -30,6 +30,7 @@ import android.os.BatteryStats;
|
||||
import android.os.Build;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import com.android.internal.os.BatterySipper;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
@@ -52,6 +53,7 @@ import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
@@ -69,6 +71,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 BLACKLISTED_WAKEUP_COUNT = 37;
|
||||
private static final int ANOMALY_WAKEUP_FREQUENCY = 428; // count per hour
|
||||
@Mock
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
@@ -87,12 +90,12 @@ public class WakeupAlarmAnomalyDetectorTest {
|
||||
@Mock
|
||||
private BatteryUtils mBatteryUtils;
|
||||
@Mock
|
||||
private ApplicationInfo mApplicationInfo;
|
||||
@Mock
|
||||
private BatteryStats.Uid.Pkg mPkg;
|
||||
@Mock
|
||||
private BatteryStats.Counter mCounter;
|
||||
@Mock
|
||||
private BatteryStats.Counter mCounter2;
|
||||
@Mock
|
||||
private AnomalyDetectionPolicy mPolicy;
|
||||
@Mock
|
||||
private AnomalyAction mAnomalyAction;
|
||||
@@ -111,6 +114,9 @@ public class WakeupAlarmAnomalyDetectorTest {
|
||||
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
ReflectionHelpers.setField(mPolicy, "wakeupAlarmThreshold", 60);
|
||||
final Set<String> blacklistedTags = new ArraySet<>();
|
||||
blacklistedTags.add("blacklistedTag");
|
||||
ReflectionHelpers.setField(mPolicy, "wakeupBlacklistedTags", blacklistedTags);
|
||||
|
||||
doReturn(false).when(mBatteryUtils).shouldHideSipper(any());
|
||||
doReturn(RUNNING_TIME_MS).when(mBatteryUtils).calculateRunningTimeBasedOnStatsType(any(),
|
||||
@@ -207,4 +213,20 @@ public class WakeupAlarmAnomalyDetectorTest {
|
||||
assertThat(mWakeupAlarmAnomalyDetector.getWakeupAlarmCountFromUid(mAnomalyUid)).isEqualTo(
|
||||
2 * NORMAL_WAKEUP_COUNT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWakeupAlarmCountFromUid_filterOutBlacklistedTags() {
|
||||
final ArrayMap<String, BatteryStats.Uid.Pkg> packageStats = new ArrayMap<>();
|
||||
final ArrayMap<String, BatteryStats.Counter> alarms = new ArrayMap<>();
|
||||
doReturn(alarms).when(mPkg).getWakeupAlarmStats();
|
||||
doReturn(NORMAL_WAKEUP_COUNT).when(mCounter).getCountLocked(anyInt());
|
||||
doReturn(BLACKLISTED_WAKEUP_COUNT).when(mCounter2).getCountLocked(anyInt());
|
||||
doReturn(packageStats).when(mAnomalyUid).getPackageStats();
|
||||
packageStats.put("", mPkg);
|
||||
alarms.put("allowedTag", mCounter);
|
||||
alarms.put("blacklistedTag", mCounter2);
|
||||
|
||||
assertThat(mWakeupAlarmAnomalyDetector.getWakeupAlarmCountFromUid(mAnomalyUid)).isEqualTo(
|
||||
NORMAL_WAKEUP_COUNT);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user