Implement method that checks whether a BroadcastReceiver is registered for an intent.
diff --git a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowApplication.java b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowApplication.java
index 77864d3..29542e9 100644
--- a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowApplication.java
+++ b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowApplication.java
@@ -416,6 +416,15 @@
}
}
+ public boolean hasReceiverForIntent(Intent intent) {
+ for (Wrapper wrapper : registeredReceivers) {
+ if (wrapper.intentFilter.matchAction(intent.getAction())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
/**
* Non-Android accessor.
*
diff --git a/src/test/java/com/xtremelabs/robolectric/shadows/ApplicationTest.java b/src/test/java/com/xtremelabs/robolectric/shadows/ApplicationTest.java
index ab3afad..8723efb 100644
--- a/src/test/java/com/xtremelabs/robolectric/shadows/ApplicationTest.java
+++ b/src/test/java/com/xtremelabs/robolectric/shadows/ApplicationTest.java
@@ -2,6 +2,7 @@
import android.app.Activity;
import android.app.Application;
+import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
@@ -28,6 +29,7 @@
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static com.xtremelabs.robolectric.util.TestUtil.newConfig;
import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.*;
@@ -229,7 +231,17 @@
shadowOf(Robolectric.application).assertNoBroadcastListenersOfActionRegistered(activity, "Bar");
}
-
+
+ @Test
+ public void canAnswerIfReceiverIsRegisteredForIntent() throws Exception {
+ BroadcastReceiver expectedReceiver = new TestBroadcastReceiver();
+ ShadowApplication shadowApplication = shadowOf(Robolectric.application);
+ assertFalse(shadowApplication.hasReceiverForIntent(new Intent("Foo")));
+ Robolectric.application.registerReceiver(expectedReceiver, new IntentFilter("Foo"));
+
+ assertTrue(shadowApplication.hasReceiverForIntent(new Intent("Foo")));
+ }
+
@Test
public void broadcasts_shouldBeLogged() {
Intent broadcastIntent = new Intent("foo");