Search in sources :

Example 1 with Wrapper

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);
    }
}
Also used : Wrapper(org.robolectric.shadows.ShadowApplication.Wrapper) ContextWrapper(android.content.ContextWrapper)

Example 2 with Wrapper

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;
}
Also used : Wrapper(org.robolectric.shadows.ShadowApplication.Wrapper) ContextWrapper(android.content.ContextWrapper)

Example 3 with Wrapper

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;
                    }
                }
            }
        }
    }
}
Also used : Wrapper(org.robolectric.shadows.ShadowApplication.Wrapper) ContextWrapper(android.content.ContextWrapper)

Example 4 with Wrapper

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;
}
Also used : Wrapper(org.robolectric.shadows.ShadowApplication.Wrapper) ContextWrapper(android.content.ContextWrapper) ArrayList(java.util.ArrayList) Intent(android.content.Intent)

Example 5 with Wrapper

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Wrapper(org.robolectric.shadows.ShadowApplication.Wrapper) ContextWrapper(android.content.ContextWrapper) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Wrapper (org.robolectric.shadows.ShadowApplication.Wrapper)7 ContextWrapper (android.content.ContextWrapper)6 Intent (android.content.Intent)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1