Can get all of the receivers 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 29542e9..8a07a83 100644
--- a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowApplication.java
+++ b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowApplication.java
@@ -2,7 +2,13 @@
import android.app.Application;
import android.appwidget.AppWidgetManager;
-import android.content.*;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.ServiceConnection;
import android.content.res.Resources;
import android.os.IBinder;
import android.os.Looper;
@@ -16,7 +22,11 @@
import com.xtremelabs.robolectric.tester.org.apache.http.FakeHttpLayer;
import com.xtremelabs.robolectric.util.Scheduler;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
import static com.xtremelabs.robolectric.Robolectric.newInstanceOf;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
@@ -425,6 +435,16 @@
return false;
}
+ public List<BroadcastReceiver> getReceiversForIntent(Intent intent) {
+ ArrayList<BroadcastReceiver> broadcastReceivers = new ArrayList<BroadcastReceiver>();
+ for (Wrapper wrapper : registeredReceivers) {
+ if (wrapper.intentFilter.matchAction(intent.getAction())) {
+ broadcastReceivers.add(wrapper.getBroadcastReceiver());
+ }
+ }
+ return broadcastReceivers;
+ }
+
/**
* 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 8723efb..e9b7358 100644
--- a/src/test/java/com/xtremelabs/robolectric/shadows/ApplicationTest.java
+++ b/src/test/java/com/xtremelabs/robolectric/shadows/ApplicationTest.java
@@ -32,7 +32,9 @@
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -168,52 +170,52 @@
assertNull(service.service);
assertNull(shadowApplication.peekNextStartedService());
}
-
- @Test
+
+ @Test
public void shouldHaveStoppedServiceIntentAndIndicateServiceWasntRunning() {
- ShadowApplication shadowApplication = Robolectric.shadowOf(Robolectric.application);
-
- Activity activity = new Activity();
-
- Intent intent = getSomeActionIntent("some.action");
-
- boolean wasRunning = activity.stopService(intent);
-
- assertFalse(wasRunning);
- assertEquals(intent, shadowApplication.getNextStoppedService());
+ ShadowApplication shadowApplication = Robolectric.shadowOf(Robolectric.application);
+
+ Activity activity = new Activity();
+
+ Intent intent = getSomeActionIntent("some.action");
+
+ boolean wasRunning = activity.stopService(intent);
+
+ assertFalse(wasRunning);
+ assertEquals(intent, shadowApplication.getNextStoppedService());
}
-
+
private Intent getSomeActionIntent(String action) {
- Intent intent = new Intent();
- intent.setAction(action);
- return intent;
+ Intent intent = new Intent();
+ intent.setAction(action);
+ return intent;
}
-
+
@Test
public void shouldHaveStoppedServiceIntentAndIndicateServiceWasRunning() {
- ShadowApplication shadowApplication = shadowOf(Robolectric.application);
-
- Activity activity = new Activity();
-
- Intent intent = getSomeActionIntent("some.action");
-
- activity.startService(intent);
-
- boolean wasRunning = activity.stopService(intent);
-
- assertTrue(wasRunning);
- assertEquals(intent, shadowApplication.getNextStoppedService());
+ ShadowApplication shadowApplication = shadowOf(Robolectric.application);
+
+ Activity activity = new Activity();
+
+ Intent intent = getSomeActionIntent("some.action");
+
+ activity.startService(intent);
+
+ boolean wasRunning = activity.stopService(intent);
+
+ assertTrue(wasRunning);
+ assertEquals(intent, shadowApplication.getNextStoppedService());
}
-
+
@Test
public void shouldClearStartedServiceIntents() {
- ShadowApplication shadowApplication = shadowOf(Robolectric.application);
- shadowApplication.startService(getSomeActionIntent("some.action"));
- shadowApplication.startService(getSomeActionIntent("another.action"));
-
- shadowApplication.clearStartedServices();
-
- assertNull(shadowApplication.getNextStartedService());
+ ShadowApplication shadowApplication = shadowOf(Robolectric.application);
+ shadowApplication.startService(getSomeActionIntent("some.action"));
+ shadowApplication.startService(getSomeActionIntent("another.action"));
+
+ shadowApplication.clearStartedServices();
+
+ assertNull(shadowApplication.getNextStartedService());
}
@Test(expected = IllegalStateException.class)
@@ -243,14 +245,25 @@
}
@Test
- public void broadcasts_shouldBeLogged() {
- Intent broadcastIntent = new Intent("foo");
- Robolectric.application.sendBroadcast(broadcastIntent);
-
- List<Intent> broadcastIntents = shadowOf(Robolectric.application).getBroadcastIntents();
- assertTrue(broadcastIntents.size() == 1);
- assertEquals(broadcastIntent, broadcastIntents.get(0));
- }
+ public void canFindAllReceiversForAnIntent() throws Exception {
+ BroadcastReceiver expectedReceiver = new TestBroadcastReceiver();
+ ShadowApplication shadowApplication = shadowOf(Robolectric.application);
+ assertFalse(shadowApplication.hasReceiverForIntent(new Intent("Foo")));
+ Robolectric.application.registerReceiver(expectedReceiver, new IntentFilter("Foo"));
+ Robolectric.application.registerReceiver(expectedReceiver, new IntentFilter("Foo"));
+
+ assertTrue(shadowApplication.getReceiversForIntent(new Intent("Foo")).size() == 2);
+ }
+
+ @Test
+ public void broadcasts_shouldBeLogged() {
+ Intent broadcastIntent = new Intent("foo");
+ Robolectric.application.sendBroadcast(broadcastIntent);
+
+ List<Intent> broadcastIntents = shadowOf(Robolectric.application).getBroadcastIntents();
+ assertTrue(broadcastIntents.size() == 1);
+ assertEquals(broadcastIntent, broadcastIntents.get(0));
+ }
private static class NullBinder implements IBinder {
@Override
@@ -291,8 +304,8 @@
return false;
}
- @Override
- public void dumpAsync(FileDescriptor fd, String[] args) throws RemoteException {
- }
+ @Override
+ public void dumpAsync(FileDescriptor fd, String[] args) throws RemoteException {
+ }
}
}