Update StartedServiceMatcher
diff --git a/src/main/java/com/xtremelabs/robolectric/matchers/StartedServiceMatcher.java b/src/main/java/com/xtremelabs/robolectric/matchers/StartedServiceMatcher.java
index c707cb7..ce1dfe1 100644
--- a/src/main/java/com/xtremelabs/robolectric/matchers/StartedServiceMatcher.java
+++ b/src/main/java/com/xtremelabs/robolectric/matchers/StartedServiceMatcher.java
@@ -1,5 +1,6 @@
package com.xtremelabs.robolectric.matchers;
+import android.app.Service;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
@@ -9,6 +10,7 @@
import java.util.Set;
+import static com.xtremelabs.robolectric.Robolectric.getShadowApplication;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
public class StartedServiceMatcher extends TypeSafeMatcher<Context> {
@@ -20,19 +22,19 @@
this.expectedIntent = expectedIntent;
}
-// public StartedMatcher(String packageName, Class<? extends Activity> expectedActivityClass) {
-// this(createIntent(packageName, expectedActivityClass));
-// }
-//
-// public StartedMatcher(Class<? extends Activity> expectedActivityClass) {
-// this(createIntent(expectedActivityClass));
-// }
-//
-// public StartedMatcher(Class<? extends Activity> expectedActivityClass, String expectedAction) {
-// this(createIntent(expectedActivityClass));
-//
-// expectedIntent.setAction(expectedAction);
-// }
+ public StartedServiceMatcher(String packageName, Class<? extends Service> expectedServiceClass) {
+ this(createIntent(packageName, expectedServiceClass));
+ }
+
+ public StartedServiceMatcher(Class<? extends Service> expectedServiceClass) {
+ this(createIntent(expectedServiceClass));
+ }
+
+ public StartedServiceMatcher(Class<? extends Service> expectedServiceClass, String expectedAction) {
+ this(createIntent(expectedServiceClass));
+
+ expectedIntent.setAction(expectedAction);
+ }
/**
* Check if the class of the intent and the keys of the intent's extras match
@@ -64,14 +66,16 @@
boolean intentsMatch = shadowOf(expectedIntent).getIntentClass().equals(shadowIntent.getIntentClass());
if (!intentsMatch) {
message += "started " + actualStartedIntent;
+ } else {
+ // Test that both intent extras have the same keys
+ Set<String> keys = shadowIntent.getExtras().keySet();
+ Set<String> expectedKeys = shadowOf(expectedIntent).getExtras().keySet();
+ intentsMatch = keys.equals(expectedKeys);
+ if(!intentsMatch){
+ message += "did not get the same extras keys";
+ }
}
-
- Set<String> keys = shadowIntent.getExtras().keySet();
- Set<String> expectedKeys = shadowOf(expectedIntent).getExtras().keySet();
- intentsMatch = keys.equals(expectedKeys);
- if(!intentsMatch){
- message += "did not get the same extras keys";
- }
+
return intentsMatch;
}
@@ -80,26 +84,32 @@
description.appendText(message);
}
-// public static Intent createIntent(Class<? extends Activity> activityClass, String extraKey, String extraValue) {
-// Intent intent = createIntent(activityClass);
-// intent.putExtra(extraKey, extraValue);
-// return intent;
-// }
-//
-// public static Intent createIntent(Class<? extends Activity> activityClass, String action) {
-// Intent intent = createIntent(activityClass);
-// intent.setAction(action);
-// return intent;
-// }
-//
-// public static Intent createIntent(Class<? extends Activity> activityClass) {
-// String packageName = activityClass.getPackage().getName();
-// return createIntent(packageName, activityClass);
-// }
-//
-// public static Intent createIntent(String packageName, Class<? extends Activity> activityClass) {
-// Intent intent = new Intent();
-// intent.setClassName(packageName, activityClass.getName());
-// return intent;
-// }
+ public static Intent createIntent(Class<? extends Service> serviceClass, String extraKey, String extraValue) {
+ Intent intent = createIntent(serviceClass);
+ intent.putExtra(extraKey, extraValue);
+ return intent;
+ }
+
+ public static Intent createIntent(Class<? extends Service> serviceClass, String action) {
+ Intent intent = createIntent(serviceClass);
+ intent.setAction(action);
+ return intent;
+ }
+
+ public static Intent createIntent(Class<? extends Service> serviceClass) {
+ Package pack = serviceClass.getPackage();
+ String packageName = "android.service";
+ // getPackage is returning null when run from tests
+ if(pack != null) {
+ pack.getName();
+ }
+ return createIntent(packageName, serviceClass);
+ }
+
+ public static Intent createIntent(String packageName, Class<? extends Service> serviceClass) {
+ Intent intent = new Intent();
+ intent.setClassName(packageName, serviceClass.getName());
+ intent.setClass(getShadowApplication().getApplicationContext(), serviceClass);
+ return intent;
+ }
}
diff --git a/src/test/java/com/xtremelabs/robolectric/matchers/StartedServiceMatcherTest.java b/src/test/java/com/xtremelabs/robolectric/matchers/StartedServiceMatcherTest.java
new file mode 100644
index 0000000..1af1153
--- /dev/null
+++ b/src/test/java/com/xtremelabs/robolectric/matchers/StartedServiceMatcherTest.java
@@ -0,0 +1,95 @@
+package com.xtremelabs.robolectric.matchers;
+
+import android.app.IntentService;
+import android.content.Context;
+import android.content.Intent;
+import android.service.wallpaper.WallpaperService;
+import com.xtremelabs.robolectric.Robolectric;
+import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.StringDescription;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.internal.matchers.TypeSafeMatcher;
+import org.junit.runner.RunWith;
+import static com.xtremelabs.robolectric.matchers.StartedServiceMatcher.createIntent;
+import static org.junit.Assert.assertThat;
+
+@RunWith(WithTestDefaultsRunner.class)
+public class StartedServiceMatcherTest {
+ private WallpaperService service;
+ private Intent intentWithExtra;
+
+ @Before
+ public void setUp() throws Exception {
+ Robolectric.bindDefaultShadowClasses();
+ Robolectric.resetStaticState();
+
+ service = new WallpaperService() {
+ @Override
+ public Engine onCreateEngine() {
+ return null;
+ }
+ };
+ intentWithExtra = createIntent(WallpaperService.class, "someExtra", "value");
+ }
+//
+ @Test
+ public void shouldSayDidntStartAnythingIfNothingWasStarted() throws Exception {
+ assertThat(new StartedServiceMatcher(WallpaperService.class),
+ givesFailureMessage((Context) service, "to start " + createIntent(WallpaperService.class) + ", but didn't start anything"));
+
+ assertThat(new StartedServiceMatcher(WallpaperService.class, "view"),
+ givesFailureMessage((Context) service, "to start " + createIntent(WallpaperService.class, "view") + ", but didn't start anything"));
+
+ assertThat(new StartedServiceMatcher(intentWithExtra),
+ givesFailureMessage((Context) service, "to start " + intentWithExtra + ", but didn't start anything"));
+ }
+
+ @Test
+ public void shouldSayStartedSomethingIfWrongThingWasStarted() throws Exception {
+ Intent actualIntent = createIntent(WallpaperService.class, "anotherExtra", "anotherValue");
+
+ service.startService(actualIntent);
+ assertThat(new StartedServiceMatcher(IntentService.class),
+ givesFailureMessage((Context) service, "to start " + createIntent(IntentService.class) + ", but started " + actualIntent));
+
+ service.startService(actualIntent);
+ assertThat(new StartedServiceMatcher(IntentService.class, "view"),
+ givesFailureMessage((Context) service, "to start " + createIntent(IntentService.class, "view") + ", but started " + actualIntent));
+
+ service.startService(actualIntent);
+ assertThat(new StartedServiceMatcher(intentWithExtra),
+ givesFailureMessage((Context) service, "to start " + intentWithExtra + ", but did not get the same extras keys"));
+ }
+
+ private <T> Matcher<Matcher<T>> givesFailureMessage(final T actual, final String expectedFailureMessage) {
+ return new TypeSafeMatcher<Matcher<T>>() {
+ public String message;
+
+ @Override
+ public boolean matchesSafely(Matcher<T> tMatcher) {
+ if (tMatcher.matches(actual)) {
+ message = "matcher to fail, but it passed";
+ return false;
+ }
+ StringDescription description = new StringDescription();
+ tMatcher.describeTo(description);
+ String actualFailureMessage = description.toString();
+ if (expectedFailureMessage.equals(actualFailureMessage)) {
+ return true;
+ } else {
+ message = "failure message to be [" + expectedFailureMessage + "] but got [" + actualFailureMessage + "]";
+ return false;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(message);
+ }
+ };
+ }
+
+}