use of org.robolectric.shadows.ShadowApplication.Wrapper in project robolectric by robolectric.
the class ShadowInstrumentation method unregisterReceiver.
void unregisterReceiver(BroadcastReceiver broadcastReceiver) {
boolean found = false;
synchronized (registeredReceivers) {
Iterator<Wrapper> iterator = registeredReceivers.iterator();
while (iterator.hasNext()) {
Wrapper wrapper = iterator.next();
if (wrapper.broadcastReceiver == broadcastReceiver) {
iterator.remove();
found = true;
}
}
}
if (!found) {
throw new IllegalArgumentException("Receiver not registered: " + broadcastReceiver);
}
}
use of org.robolectric.shadows.ShadowApplication.Wrapper in project robolectric by robolectric.
the class ShadowContextWrapperTest method getReceiverOfClass.
private <T> T getReceiverOfClass(Class<T> receiverClass) {
ShadowApplication app = shadowOf((Application) context);
List<Wrapper> receivers = app.getRegisteredReceivers();
for (Wrapper wrapper : receivers) {
if (receiverClass.isInstance(wrapper.getBroadcastReceiver())) {
return receiverClass.cast(wrapper.getBroadcastReceiver());
}
}
return null;
}
use of org.robolectric.shadows.ShadowApplication.Wrapper in project robolectric by robolectric.
the class ShadowInstrumentation method assertNoBroadcastListenersOfActionRegistered.
void assertNoBroadcastListenersOfActionRegistered(ContextWrapper context, String action) {
synchronized (registeredReceivers) {
for (Wrapper registeredReceiver : registeredReceivers) {
if (registeredReceiver.context == context.getBaseContext()) {
Iterator<String> actions = registeredReceiver.intentFilter.actionsIterator();
while (actions.hasNext()) {
if (actions.next().equals(action)) {
RuntimeException e = new IllegalStateException("Unexpected BroadcastReceiver on " + context + " with action " + action + " " + registeredReceiver.broadcastReceiver + " that was originally registered here:");
e.setStackTrace(registeredReceiver.exception.getStackTrace());
throw e;
}
}
}
}
}
}
use of org.robolectric.shadows.ShadowApplication.Wrapper in project robolectric by robolectric.
the class ShadowInstrumentation method getAppropriateWrappers.
/**
* Returns the BroadcaseReceivers wrappers, matching intent's action and permissions.
*/
private List<Wrapper> getAppropriateWrappers(Context context, @Nullable UserHandle userHandle, Intent intent, String receiverPermission, @Nullable Bundle broadcastOptions) {
broadcastIntents.add(intent);
this.broadcastOptions.put(intent, broadcastOptions);
if (userHandle != null) {
List<Intent> intentsForUser = broadcastIntentsForUser.get(userHandle);
if (intentsForUser == null) {
intentsForUser = new ArrayList<>();
broadcastIntentsForUser.put(userHandle, intentsForUser);
}
intentsForUser.add(intent);
}
List<Wrapper> result = new ArrayList<>();
List<Wrapper> copy = new ArrayList<>();
synchronized (registeredReceivers) {
copy.addAll(registeredReceivers);
}
for (Wrapper wrapper : copy) {
if (broadcastReceiverMatchesIntent(context, wrapper, intent, receiverPermission)) {
result.add(wrapper);
}
}
System.err.format("Intent = %s; Matching wrappers: %s\n", intent, result);
return result;
}
use of org.robolectric.shadows.ShadowApplication.Wrapper in project robolectric by robolectric.
the class ShadowInstrumentation method postOrderedToWrappers.
private void postOrderedToWrappers(List<Wrapper> wrappers, final Intent intent, int initialCode, String data, Bundle extras, final Context context) {
final AtomicBoolean abort = // abort state is shared among all broadcast receivers
new AtomicBoolean(false);
ListenableFuture<BroadcastResultHolder> future = immediateFuture(new BroadcastResultHolder(initialCode, data, extras));
for (final Wrapper wrapper : wrappers) {
future = postIntent(wrapper, intent, future, abort, context);
}
final ListenableFuture<?> finalFuture = future;
future.addListener(new Runnable() {
@Override
public void run() {
getMainHandler(context).post(new Runnable() {
@Override
public void run() {
try {
finalFuture.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
});
}
}, directExecutor());
}
Aggregations