Add unoptimized ble scans to batterystats CTS test
Bug: 38461344
Test: run cts-dev -m CtsIncidentHostTestCases -t com.android.server.cts.BatteryStatsValidationTest#testUnoptimizedBleScans
Test: cts-tradefed run cts-dev -m CtsDumpsysHostTestCases -t android.dumpsys.cts.BatteryStatsDumpsysTest
Change-Id: Idea05de7dbc14412e7f69fe2035310126268ea0a
diff --git a/hostsidetests/dumpsys/src/android/dumpsys/cts/BatteryStatsDumpsysTest.java b/hostsidetests/dumpsys/src/android/dumpsys/cts/BatteryStatsDumpsysTest.java
index 0dec6f7..0640f2c 100644
--- a/hostsidetests/dumpsys/src/android/dumpsys/cts/BatteryStatsDumpsysTest.java
+++ b/hostsidetests/dumpsys/src/android/dumpsys/cts/BatteryStatsDumpsysTest.java
@@ -627,13 +627,18 @@
}
private void checkBluetoothMisc(String[] parts) {
- assertEquals(10, parts.length);
+ assertEquals(15, parts.length);
assertInteger(parts[4]); // totalTime
assertInteger(parts[5]); // count
assertInteger(parts[6]); // countBg
assertInteger(parts[7]); // actualTime
assertInteger(parts[8]); // actualTimeBg
assertInteger(parts[9]); // resultsCount
+ assertInteger(parts[10]); // resultsCountBg
+ assertInteger(parts[11]); // unoptimizedScanTotalTime
+ assertInteger(parts[12]); // unoptimizedScanTotalTimeBg
+ assertInteger(parts[13]); // unoptimizedScanMaxTime
+ assertInteger(parts[14]); // unoptimizedScanMaxTimeBg
}
/**
diff --git a/hostsidetests/incident/apps/batterystatsapp/src/com/android/server/cts/device/batterystats/BatteryStatsBgVsFgActions.java b/hostsidetests/incident/apps/batterystatsapp/src/com/android/server/cts/device/batterystats/BatteryStatsBgVsFgActions.java
index 9b23052..08f19f9 100644
--- a/hostsidetests/incident/apps/batterystatsapp/src/com/android/server/cts/device/batterystats/BatteryStatsBgVsFgActions.java
+++ b/hostsidetests/incident/apps/batterystatsapp/src/com/android/server/cts/device/batterystats/BatteryStatsBgVsFgActions.java
@@ -52,7 +52,8 @@
private static final String TAG = BatteryStatsBgVsFgActions.class.getSimpleName();
public static final String KEY_ACTION = "action";
- public static final String ACTION_BLE_SCAN = "action.ble_scan";
+ public static final String ACTION_BLE_SCAN_OPTIMIZED = "action.ble_scan_optimized";
+ public static final String ACTION_BLE_SCAN_UNOPTIMIZED = "action.ble_scan_unoptimized";
public static final String ACTION_GPS = "action.gps";
public static final String ACTION_JOB_SCHEDULE = "action.jobs";
public static final String ACTION_SYNC = "action.sync";
@@ -70,8 +71,11 @@
}
sleep(100);
switch(actionCode) {
- case ACTION_BLE_SCAN:
- doBleScan(ctx, requestCode);
+ case ACTION_BLE_SCAN_OPTIMIZED:
+ doOptimizedBleScan(ctx, requestCode);
+ break;
+ case ACTION_BLE_SCAN_UNOPTIMIZED:
+ doUnoptimizedBleScan(ctx, requestCode);
break;
case ACTION_GPS:
doGpsUpdate(ctx, requestCode);
@@ -97,23 +101,34 @@
sleep(100);
}
- private static void doBleScan(Context ctx, String requestCode) {
+ private static void doOptimizedBleScan(Context ctx, String requestCode) {
+ ScanSettings scanSettings = new ScanSettings.Builder()
+ .setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC).build();
+ performBleScan(scanSettings);
+ tellHostActionFinished(ACTION_BLE_SCAN_OPTIMIZED, requestCode);
+ }
+
+ private static void doUnoptimizedBleScan(Context ctx, String requestCode) {
+ ScanSettings scanSettings = new ScanSettings.Builder()
+ .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
+ performBleScan(scanSettings);
+ tellHostActionFinished(ACTION_BLE_SCAN_UNOPTIMIZED, requestCode);
+ }
+
+ private static void performBleScan(ScanSettings scanSettings) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Log.e(TAG, "Device does not support Bluetooth");
- tellHostActionFinished(ACTION_BLE_SCAN, requestCode);
return;
}
if (!bluetoothAdapter.isEnabled()) {
Log.e(TAG, "Bluetooth is not enabled");
- tellHostActionFinished(ACTION_BLE_SCAN, requestCode);
return;
}
BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
if (bleScanner == null) {
Log.e(TAG, "Cannot access BLE scanner");
- tellHostActionFinished(ACTION_BLE_SCAN, requestCode);
return;
}
@@ -131,16 +146,12 @@
@Override
public void onBatchScanResults(List<ScanResult> results) {
Log.v(TAG, "called onBatchScanResults");
-
}
};
- ScanSettings scanSettings = new ScanSettings.Builder()
- .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
bleScanner.startScan(null, scanSettings, scanCallback);
sleep(2_000);
bleScanner.stopScan(scanCallback);
- tellHostActionFinished(ACTION_BLE_SCAN, requestCode);
}
private static void doGpsUpdate(Context ctx, String requestCode) {
diff --git a/hostsidetests/incident/src/com/android/server/cts/BatteryStatsValidationTest.java b/hostsidetests/incident/src/com/android/server/cts/BatteryStatsValidationTest.java
index 7ed8722..58606e0 100644
--- a/hostsidetests/incident/src/com/android/server/cts/BatteryStatsValidationTest.java
+++ b/hostsidetests/incident/src/com/android/server/cts/BatteryStatsValidationTest.java
@@ -53,7 +53,8 @@
// Constants from BatteryStatsBgVsFgActions.java (not directly accessible here).
public static final String KEY_ACTION = "action";
- public static final String ACTION_BLE_SCAN = "action.ble_scan";
+ public static final String ACTION_BLE_SCAN_OPTIMIZED = "action.ble_scan_optimized";
+ public static final String ACTION_BLE_SCAN_UNOPTIMIZED = "action.ble_scan_unoptimized";
public static final String ACTION_GPS = "action.gps";
public static final String ACTION_JOB_SCHEDULE = "action.jobs";
public static final String ACTION_SYNC = "action.sync";
@@ -170,18 +171,69 @@
installPackage(DEVICE_SIDE_TEST_APK, true);
// Foreground test.
- executeForeground(ACTION_BLE_SCAN, 30_000);
+ executeForeground(ACTION_BLE_SCAN_UNOPTIMIZED, 30_000);
assertValueRange("blem", "", 5, 1, 1); // ble_scan_count
assertValueRange("blem", "", 6, 0, 0); // ble_scan_count_bg
// Background test.
- executeBackground(ACTION_BLE_SCAN, 30_000);
+ executeBackground(ACTION_BLE_SCAN_UNOPTIMIZED, 30_000);
assertValueRange("blem", "", 5, 2, 2); // ble_scan_count
assertValueRange("blem", "", 6, 1, 1); // ble_scan_count_bg
batteryOffScreenOn();
}
+
+ public void testUnoptimizedBleScans() throws Exception {
+ if (isTV()) {
+ return;
+ }
+ batteryOnScreenOff();
+ installPackage(DEVICE_SIDE_TEST_APK, true);
+
+ // Ble scan time in BatteryStatsBgVsFgActions is 2 seconds, but be lenient.
+ final int minTime = 1500; // min single scan time in ms
+ final int maxTime = 3000; // max single scan time in ms
+
+ // Optimized - Foreground.
+ executeForeground(ACTION_BLE_SCAN_OPTIMIZED, 30_000);
+ assertValueRange("blem", "", 7, 1*minTime, 1*maxTime); // actualTime
+ assertValueRange("blem", "", 8, 0, 0); // actualTimeBg
+ assertValueRange("blem", "", 11, 0, 0); // unoptimizedScanTotalTime
+ assertValueRange("blem", "", 12, 0, 0); // unoptimizedScanTotalTimeBg
+ assertValueRange("blem", "", 13, 0, 0); // unoptimizedScanMaxTime
+ assertValueRange("blem", "", 14, 0, 0); // unoptimizedScanMaxTimeBg
+
+ // Optimized - Background.
+ executeBackground(ACTION_BLE_SCAN_OPTIMIZED, 30_000);
+ assertValueRange("blem", "", 7, 2*minTime, 2*maxTime); // actualTime
+ assertValueRange("blem", "", 8, 1*minTime, 1*maxTime); // actualTimeBg
+ assertValueRange("blem", "", 11, 0, 0); // unoptimizedScanTotalTime
+ assertValueRange("blem", "", 12, 0, 0); // unoptimizedScanTotalTimeBg
+ assertValueRange("blem", "", 13, 0, 0); // unoptimizedScanMaxTime
+ assertValueRange("blem", "", 14, 0, 0); // unoptimizedScanMaxTimeBg
+
+ // Unoptimized - Foreground.
+ executeForeground(ACTION_BLE_SCAN_UNOPTIMIZED, 30_000);
+ assertValueRange("blem", "", 7, 3*minTime, 3*maxTime); // actualTime
+ assertValueRange("blem", "", 8, 1*minTime, 1*maxTime); // actualTimeBg
+ assertValueRange("blem", "", 11, 1*minTime, 1*maxTime); // unoptimizedScanTotalTime
+ assertValueRange("blem", "", 12, 0, 0); // unoptimizedScanTotalTimeBg
+ assertValueRange("blem", "", 13, 1*minTime, 1*maxTime); // unoptimizedScanMaxTime
+ assertValueRange("blem", "", 14, 0, 0); // unoptimizedScanMaxTimeBg
+
+ // Unoptimized - Background.
+ executeBackground(ACTION_BLE_SCAN_UNOPTIMIZED, 30_000);
+ assertValueRange("blem", "", 7, 4*minTime, 4*maxTime); // actualTime
+ assertValueRange("blem", "", 8, 2*minTime, 2*maxTime); // actualTimeBg
+ assertValueRange("blem", "", 11, 2*minTime, 2*maxTime); // unoptimizedScanTotalTime
+ assertValueRange("blem", "", 12, 1*minTime, 1*maxTime); // unoptimizedScanTotalTimeBg
+ assertValueRange("blem", "", 13, 1*minTime, 1*maxTime); // unoptimizedScanMaxTime
+ assertValueRange("blem", "", 14, 1*minTime, 1*maxTime); // unoptimizedScanMaxTimeBg
+
+ batteryOffScreenOn();
+ }
+
public void testGpsUpdates() throws Exception {
final String gpsSensorNumber = "-10000";