summaryrefslogtreecommitdiff
path: root/tests/utils
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2021-02-15 09:36:55 +0900
committerLorenzo Colitti <lorenzo@google.com>2021-02-15 10:40:32 +0000
commit8fdfd4621846dcdc4359723bd45052da58d05804 (patch)
tree2537ad17f13b022355a25acb8db57a63bea59d46 /tests/utils
parent74cbf1917b60cec2897348d68ebef467c94bada7 (diff)
BroadcastInterceptingContext: use passed-in broadcast Handler.
Currently, BroadcastInterceptingContext always runs broadcast receivers on the thread that called sendBroadcast. This means: 1. Receivers might run on the wrong thread, making the test less realistic. 2. If any receiver checks what thread it's running on, then either the check needs to be modified or deleted, or the test must call sendBroadcast on the thread that the receiver expects to run on. The latter is impossible when there is more than one receiver that needs to run on more than one thread. This CL adds a setUseRegisteredHandlers method that allows tests to say that they want each receiver to run on the Handler specified at registration time. This CL also enables the new mode for ConnectivityServiceTest, and resolves a TODO to re-enable a disabled thread check. The new mode cannot be enabled by default because it would break most of the tests. All the below tests pass except for the car bluetooth test testAutoConnectAdapterOff_returnsImmediately which was already failing. Bug: 173331190 Test: atest CarInputServiceTest BluetoothProfileDeviceManagerTest Test: atest NetworkPolicyManagerServiceTest Test: atest ConnectivityServiceTest Test: atest TetheringTest TetheringNotificationUpdaterTest Test: atest VersionedBroadcastListenerTest EntitlementManagerTest Test: atest TetheringConfigurationTest Test: atest CachedDeviceStateServiceTest Test: atest EmergencyAffordanceServiceTest Change-Id: I3303bb14516f07a55d82a16b59c111ab3f8b0389
Diffstat (limited to 'tests/utils')
-rw-r--r--tests/utils/testutils/java/com/android/internal/util/test/BroadcastInterceptingContext.java38
1 files changed, 30 insertions, 8 deletions
diff --git a/tests/utils/testutils/java/com/android/internal/util/test/BroadcastInterceptingContext.java b/tests/utils/testutils/java/com/android/internal/util/test/BroadcastInterceptingContext.java
index 1102624da031..4a1f96d145bd 100644
--- a/tests/utils/testutils/java/com/android/internal/util/test/BroadcastInterceptingContext.java
+++ b/tests/utils/testutils/java/com/android/internal/util/test/BroadcastInterceptingContext.java
@@ -42,6 +42,8 @@ public class BroadcastInterceptingContext extends ContextWrapper {
private final List<BroadcastInterceptor> mInterceptors = new ArrayList<>();
+ private boolean mUseRegisteredHandlers;
+
public abstract class FutureIntent extends FutureTask<Intent> {
public FutureIntent() {
super(
@@ -61,17 +63,24 @@ public class BroadcastInterceptingContext extends ContextWrapper {
public class BroadcastInterceptor extends FutureIntent {
private final BroadcastReceiver mReceiver;
private final IntentFilter mFilter;
+ private final Handler mHandler;
- public BroadcastInterceptor(BroadcastReceiver receiver, IntentFilter filter) {
+ public BroadcastInterceptor(BroadcastReceiver receiver, IntentFilter filter,
+ Handler handler) {
mReceiver = receiver;
mFilter = filter;
+ mHandler = mUseRegisteredHandlers ? handler : null;
}
public boolean dispatchBroadcast(Intent intent) {
if (mFilter.match(getContentResolver(), intent, false, TAG) > 0) {
if (mReceiver != null) {
final Context context = BroadcastInterceptingContext.this;
- mReceiver.onReceive(context, intent);
+ if (mHandler == null) {
+ mReceiver.onReceive(context, intent);
+ } else {
+ mHandler.post(() -> mReceiver.onReceive(context, intent));
+ }
return false;
} else {
set(intent);
@@ -116,25 +125,38 @@ public class BroadcastInterceptingContext extends ContextWrapper {
}
public FutureIntent nextBroadcastIntent(IntentFilter filter) {
- final BroadcastInterceptor interceptor = new BroadcastInterceptor(null, filter);
+ final BroadcastInterceptor interceptor = new BroadcastInterceptor(null, filter, null);
synchronized (mInterceptors) {
mInterceptors.add(interceptor);
}
return interceptor;
}
- @Override
- public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
+ /**
+ * Whether to send broadcasts to registered handlers. By default, receivers are called
+ * synchronously by sendBroadcast. If this method is called with {@code true}, the receiver is
+ * instead called by a runnable posted to the Handler specified when the receiver was
+ * registered. This method applies only to future registrations, already-registered receivers
+ * are unaffected.
+ */
+ public void setUseRegisteredHandlers(boolean use) {
synchronized (mInterceptors) {
- mInterceptors.add(new BroadcastInterceptor(receiver, filter));
+ mUseRegisteredHandlers = use;
}
- return null;
+ }
+
+ @Override
+ public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
+ return registerReceiver(receiver, filter, null, null);
}
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
String broadcastPermission, Handler scheduler) {
- return registerReceiver(receiver, filter);
+ synchronized (mInterceptors) {
+ mInterceptors.add(new BroadcastInterceptor(receiver, filter, scheduler));
+ }
+ return null;
}
@Override