summaryrefslogtreecommitdiff
path: root/tests/anomaly-tester
diff options
context:
space:
mode:
authorjackqdyulei <jackqdyulei@google.com>2017-08-15 17:02:58 -0700
committerjackqdyulei <jackqdyulei@google.com>2017-08-24 16:15:44 -0700
commit0cc62f860c22d7d0b82716732429e97a3d13361b (patch)
tree4f3e16bc2a6968fd89dec7f67ea52cf1c2ae719c /tests/anomaly-tester
parent9d78df0d301e54fe26fdcd363ddbe70915da8ada (diff)
downloadSettings-0cc62f860c22d7d0b82716732429e97a3d13361b.tar.gz
Add wakelock action in testing app
This cl adds action to hold the wakelock for specific time. With this action we could test whether wakelock detector is effective. Bug: 2731722 Test: make -j40 AnomalyTester Change-Id: I1d44c12363f126ea9b8fc44f4c135e46b898186c
Diffstat (limited to 'tests/anomaly-tester')
-rw-r--r--tests/anomaly-tester/AndroidManifest.xml5
-rw-r--r--tests/anomaly-tester/res/layout/activity_main.xml2
-rw-r--r--tests/anomaly-tester/res/layout/wakelock_anomaly.xml60
-rw-r--r--tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java28
-rw-r--r--tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java17
5 files changed, 110 insertions, 2 deletions
diff --git a/tests/anomaly-tester/AndroidManifest.xml b/tests/anomaly-tester/AndroidManifest.xml
index c057a66ec45..68e2dd71a0f 100644
--- a/tests/anomaly-tester/AndroidManifest.xml
+++ b/tests/anomaly-tester/AndroidManifest.xml
@@ -16,11 +16,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.settings.anomaly.tester">
- <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
- <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
+ <uses-permission android:name="android.permission.WAKE_LOCK"/>
+ <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
+ <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application
android:allowBackup="false"
android:label="@string/app_name"
diff --git a/tests/anomaly-tester/res/layout/activity_main.xml b/tests/anomaly-tester/res/layout/activity_main.xml
index 63d025efbe3..0561cff9fc3 100644
--- a/tests/anomaly-tester/res/layout/activity_main.xml
+++ b/tests/anomaly-tester/res/layout/activity_main.xml
@@ -52,6 +52,8 @@
<include layout="@layout/bluetooth_anomaly"/>
+ <include layout="@layout/wakelock_anomaly"/>
+
</LinearLayout>
</ScrollView>
</LinearLayout> \ No newline at end of file
diff --git a/tests/anomaly-tester/res/layout/wakelock_anomaly.xml b/tests/anomaly-tester/res/layout/wakelock_anomaly.xml
new file mode 100644
index 00000000000..08cb7958419
--- /dev/null
+++ b/tests/anomaly-tester/res/layout/wakelock_anomaly.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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.
+ -->
+<LinearLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical"
+ android:paddingBottom="6dp">
+
+ <TextView
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="Wakelock Anomaly"
+ android:textSize="16sp"/>
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+ <EditText
+ android:id="@+id/wakelock_threshold"
+ android:layout_width="0dp"
+ android:layout_weight="3"
+ android:layout_height="wrap_content"
+ android:hint="Threshold(ms)"
+ android:text="3000"
+ android:inputType="number"/>
+
+ <EditText
+ android:id="@+id/wakelock_run_time"
+ android:layout_width="0dp"
+ android:layout_weight="3"
+ android:layout_height="wrap_content"
+ android:hint="Run time(ms)"
+ android:text="6000"
+ android:inputType="number"/>
+
+ <Button
+ android:id="@+id/wakelock_button"
+ android:layout_width="0dp"
+ android:layout_weight="2"
+ android:layout_height="wrap_content"
+ android:text="START"
+ android:onClick="startWakelockAnomaly"/>
+ </LinearLayout>
+
+</LinearLayout> \ No newline at end of file
diff --git a/tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java b/tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java
index 773dd27bfbd..e4567c2fbc3 100644
--- a/tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java
+++ b/tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java
@@ -74,6 +74,34 @@ public class AnomalyActivity extends Activity {
}
}
+ public void startWakelockAnomaly(View view) {
+ try {
+ // Enable anomaly detection and change the threshold
+ final String config = new AnomalyPolicyBuilder()
+ .addPolicy(AnomalyPolicyBuilder.KEY_ANOMALY_DETECTION_ENABLED, true)
+ .addPolicy(AnomalyPolicyBuilder.KEY_WAKELOCK_DETECTION_ENABLED, true)
+ .addPolicy(AnomalyPolicyBuilder.KEY_WAKELOCK_THRESHOLD,
+ getValueFromEditText(R.id.wakelock_threshold))
+ .build();
+ Settings.Global.putString(getContentResolver(),
+ Settings.Global.ANOMALY_DETECTION_CONSTANTS,
+ config);
+
+ // Start the anomaly service
+ Intent intent = new Intent(this, AnomalyService.class);
+ intent.putExtra(AnomalyActions.KEY_ACTION, AnomalyActions.ACTION_WAKE_LOCK);
+ intent.putExtra(AnomalyActions.KEY_DURATION_MS,
+ getValueFromEditText(R.id.wakelock_run_time));
+ intent.putExtra(AnomalyActions.KEY_RESULT_RECEIVER, mResultReceiver);
+ intent.putExtra(KEY_TARGET_BUTTON, view.getId());
+ startService(intent);
+
+ view.setEnabled(false);
+ } catch (NumberFormatException e) {
+ Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
+ }
+ }
+
private long getValueFromEditText(final int id) throws NumberFormatException {
final EditText editText = findViewById(id);
if (editText != null) {
diff --git a/tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java b/tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java
index a55efdbd4af..58e5a99fb52 100644
--- a/tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java
+++ b/tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java
@@ -22,6 +22,7 @@ import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
+import android.os.PowerManager;
import android.util.Log;
import java.util.List;
@@ -37,6 +38,7 @@ public class AnomalyActions {
public static final String KEY_RESULT_RECEIVER = "result_receiver";
public static final String ACTION_BLE_SCAN_UNOPTIMIZED = "action.ble_scan_unoptimized";
+ public static final String ACTION_WAKE_LOCK = "action.wake_lock";
public static void doAction(Context ctx, String actionCode, long durationMs) {
if (actionCode == null) {
@@ -47,6 +49,8 @@ public class AnomalyActions {
case ACTION_BLE_SCAN_UNOPTIMIZED:
doUnoptimizedBleScan(ctx, durationMs);
break;
+ case ACTION_WAKE_LOCK:
+ doHoldWakelock(ctx, durationMs);
default:
Log.e(TAG, "Intent had invalid action");
}
@@ -93,4 +97,17 @@ public class AnomalyActions {
}
bleScanner.stopScan(scanCallback);
}
+
+ private static void doHoldWakelock(Context ctx, long durationMs) {
+ PowerManager powerManager = ctx.getSystemService(PowerManager.class);
+ PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
+ "AnomalyWakeLock");
+ wl.acquire();
+ try {
+ Thread.sleep(durationMs);
+ } catch (InterruptedException e) {
+ Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
+ }
+ wl.release();
+ }
}