summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jay Thomas Sullivan <jaysullivan@google.com> 2023-11-30 18:05:55 -0800
committer Jay Sullivan <jaysullivan@google.com> 2023-12-01 19:34:34 +0000
commitfa6726c5ed9dc6652eb989151f13376262edd254 (patch)
tree119c5e821a6769437553fbd5fac5962bb684aed0
parent45ff302be0117b7e0c7ec04593f6d053d8d3d238 (diff)
Possibly fix NearbyDevicesRenouncePermissionTest
This test has a few rare flakes (that only occur on certain targets), so: 1. Fix one of them, by setting a high scan_quota_count for Bluetooth; this config defaults to 5 and denotes the max number of Bluetooth scans allowed per every 30 seconds. Change it from 5 to 1000. 2. Possibly fix the other, by using AtomicInteger for variables used across threads, and by ignoring async noteOps that originated before the test began (possibly from previous tests). Since I can't reproduce this error locally, I don't know if this fixes it: it's just a guess. In case this doesn't work, add some log statements to help diagnose the issue. Bug: 304474384 Test: atest NearbyDevicesRenouncePermissionTest Change-Id: Ie7405cada8bbc33f5d844c3851e196f7dfe049be
-rw-r--r--tests/cts/permission/src/android/permission/cts/NearbyDevicesRenouncePermissionTest.java86
1 files changed, 63 insertions, 23 deletions
diff --git a/tests/cts/permission/src/android/permission/cts/NearbyDevicesRenouncePermissionTest.java b/tests/cts/permission/src/android/permission/cts/NearbyDevicesRenouncePermissionTest.java
index a196cedfd..aeb4d1d28 100644
--- a/tests/cts/permission/src/android/permission/cts/NearbyDevicesRenouncePermissionTest.java
+++ b/tests/cts/permission/src/android/permission/cts/NearbyDevicesRenouncePermissionTest.java
@@ -40,15 +40,19 @@ import android.content.pm.PackageManager;
import android.os.Process;
import android.os.SystemClock;
import android.platform.test.annotations.AppModeFull;
+import android.provider.DeviceConfig;
import android.util.ArraySet;
import android.util.Base64;
import android.util.Log;
import androidx.test.InstrumentationRegistry;
+import com.android.compatibility.common.util.DeviceConfigStateChangerRule;
import com.android.compatibility.common.util.EnableLocationRule;
import com.android.compatibility.common.util.SystemUtil;
+import com.google.common.util.concurrent.Uninterruptibles;
+
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
@@ -58,6 +62,8 @@ import org.junit.Test;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
/**
* Tests behaviour when performing bluetooth scans with renounced location permission.
@@ -73,11 +79,22 @@ public class NearbyDevicesRenouncePermissionTest {
public static final EnableBluetoothRule sEnableBluetoothRule = new EnableBluetoothRule(true);
@Rule
+ public DeviceConfigStateChangerRule safetyLabelChangeNotificationsEnabledConfig =
+ new DeviceConfigStateChangerRule(
+ mContext,
+ DeviceConfig.NAMESPACE_BLUETOOTH,
+ "scan_quota_count",
+ Integer.toString(1000)
+ );
+
+ @Rule
public final EnableLocationRule enableLocationRule = new EnableLocationRule();
private AppOpsManager mAppOpsManager;
- private int mLocationNoteCount;
- private int mScanNoteCount;
+
+ private volatile long mTestStartTimestamp;
+ private final AtomicInteger mLocationNoteCount = new AtomicInteger(0);
+ private final AtomicInteger mScanNoteCount = new AtomicInteger(0);
private enum Result {
UNKNOWN, EXCEPTION, EMPTY, FILTERED, FULL
@@ -89,6 +106,13 @@ public class NearbyDevicesRenouncePermissionTest {
@Before
public void setUp() throws Exception {
+ // Sleep to guarantee that past noteOp timestamps are less than mTestStartTimestamp
+ Uninterruptibles.sleepUninterruptibly(2, TimeUnit.MILLISECONDS);
+ mTestStartTimestamp = System.currentTimeMillis();
+
+ mLocationNoteCount.set(0);
+ mScanNoteCount.set(0);
+
mAppOpsManager = getApplicationContext().getSystemService(AppOpsManager.class);
mAppOpsManager.setOnOpNotedCallback(getApplicationContext().getMainExecutor(),
new AppOpsManager.OnOpNotedCallback() {
@@ -96,10 +120,12 @@ public class NearbyDevicesRenouncePermissionTest {
public void onNoted(SyncNotedAppOp op) {
switch (op.getOp()) {
case OPSTR_FINE_LOCATION:
- mLocationNoteCount++;
+ logNoteOp(op);
+ mLocationNoteCount.incrementAndGet();
break;
case OPSTR_BLUETOOTH_SCAN:
- mScanNoteCount++;
+ logNoteOp(op);
+ mScanNoteCount.incrementAndGet();
break;
default:
}
@@ -113,10 +139,22 @@ public class NearbyDevicesRenouncePermissionTest {
public void onAsyncNoted(AsyncNotedAppOp asyncOp) {
switch (asyncOp.getOp()) {
case OPSTR_FINE_LOCATION:
- mLocationNoteCount++;
+ logNoteOp(asyncOp);
+ if (asyncOp.getTime() < mTestStartTimestamp) {
+ Log.i(TAG, "ignoring asyncOp that originated before test "
+ + "start");
+ return;
+ }
+ mLocationNoteCount.incrementAndGet();
break;
case OPSTR_BLUETOOTH_SCAN:
- mScanNoteCount++;
+ logNoteOp(asyncOp);
+ if (asyncOp.getTime() < mTestStartTimestamp) {
+ Log.i(TAG, "ignoring asyncOp that originated before test "
+ + "start");
+ return;
+ }
+ mScanNoteCount.incrementAndGet();
break;
default:
}
@@ -124,26 +162,31 @@ public class NearbyDevicesRenouncePermissionTest {
});
}
+ private void logNoteOp(SyncNotedAppOp op) {
+ Log.i(TAG, "OnOpNotedCallback::onNoted(op=" + op.getOp() + ")");
+ }
+
+ private void logNoteOp(AsyncNotedAppOp asyncOp) {
+ Log.i(TAG, "OnOpNotedCallback::"
+ + "onAsyncNoted(op=" + asyncOp.getOp()
+ + ", testStartTimestamp=" + mTestStartTimestamp
+ + ", noteOpTimestamp=" + asyncOp.getTime() + ")");
+ }
+
@After
public void tearDown() throws Exception {
mAppOpsManager.setOnOpNotedCallback(null, null);
}
- private void clearNoteCounts() {
- mLocationNoteCount = 0;
- mScanNoteCount = 0;
- }
-
@AppModeFull
@Test
public void scanWithoutRenouncingNotesBluetoothAndLocation() throws Exception {
assumeTrue(supportsBluetoothLe());
- clearNoteCounts();
assertThat(performScan(Scenario.DEFAULT)).isEqualTo(Result.FULL);
SystemUtil.eventually(() -> {
- assertThat(mLocationNoteCount).isGreaterThan(0);
- assertThat(mScanNoteCount).isGreaterThan(0);
+ assertThat(mLocationNoteCount.get()).isGreaterThan(0);
+ assertThat(mScanNoteCount.get()).isGreaterThan(0);
});
}
@@ -152,11 +195,10 @@ public class NearbyDevicesRenouncePermissionTest {
public void scanRenouncingLocationNotesBluetoothButNotLocation() throws Exception {
assumeTrue(supportsBluetoothLe());
- clearNoteCounts();
assertThat(performScan(Scenario.RENOUNCE)).isEqualTo(Result.FILTERED);
SystemUtil.eventually(() -> {
- assertThat(mLocationNoteCount).isEqualTo(0);
- assertThat(mScanNoteCount).isGreaterThan(0);
+ assertThat(mLocationNoteCount.get()).isEqualTo(0);
+ assertThat(mScanNoteCount.get()).isGreaterThan(0);
});
}
@@ -165,22 +207,20 @@ public class NearbyDevicesRenouncePermissionTest {
public void scanRenouncingInMiddleOfChainNotesBluetoothButNotLocation() throws Exception {
assumeTrue(supportsBluetoothLe());
- clearNoteCounts();
assertThat(performScan(Scenario.RENOUNCE_MIDDLE)).isEqualTo(Result.FILTERED);
SystemUtil.eventually(() -> {
- assertThat(mLocationNoteCount).isEqualTo(0);
- assertThat(mScanNoteCount).isGreaterThan(0);
+ assertThat(mLocationNoteCount.get()).isEqualTo(0);
+ assertThat(mScanNoteCount.get()).isGreaterThan(0);
});
}
@AppModeFull
@Test
public void scanRenouncingAtEndOfChainNotesBluetoothButNotLocation() throws Exception {
- clearNoteCounts();
assertThat(performScan(Scenario.RENOUNCE_END)).isEqualTo(Result.FILTERED);
SystemUtil.eventually(() -> {
- assertThat(mLocationNoteCount).isEqualTo(0);
- assertThat(mScanNoteCount).isGreaterThan(0);
+ assertThat(mLocationNoteCount.get()).isEqualTo(0);
+ assertThat(mScanNoteCount.get()).isGreaterThan(0);
});
}