1 package com.xtremelabs.robolectric.matchers; 2 3 import android.app.IntentService; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.service.wallpaper.WallpaperService; 7 import com.xtremelabs.robolectric.Robolectric; 8 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 9 import org.hamcrest.Description; 10 import org.hamcrest.Matcher; 11 import org.hamcrest.StringDescription; 12 import org.junit.Before; 13 import org.junit.Test; 14 import org.junit.internal.matchers.TypeSafeMatcher; 15 import org.junit.runner.RunWith; 16 import static com.xtremelabs.robolectric.matchers.StartedServiceMatcher.createIntent; 17 import static org.junit.Assert.assertThat; 18 19 @RunWith(WithTestDefaultsRunner.class) 20 public class StartedServiceMatcherTest { 21 private WallpaperService service; 22 private Intent intentWithExtra; 23 24 @Before 25 public void setUp() throws Exception { 26 Robolectric.bindDefaultShadowClasses(); 27 Robolectric.resetStaticState(); 28 29 service = new WallpaperService() { 30 @Override 31 public Engine onCreateEngine() { 32 return null; 33 } 34 }; 35 intentWithExtra = createIntent(WallpaperService.class, "someExtra", "value"); 36 } 37 // 38 @Test 39 public void shouldSayDidntStartAnythingIfNothingWasStarted() throws Exception { 40 assertThat(new StartedServiceMatcher(WallpaperService.class), 41 givesFailureMessage((Context) service, "to start " + createIntent(WallpaperService.class) + ", but didn't start anything")); 42 43 assertThat(new StartedServiceMatcher(WallpaperService.class, "view"), 44 givesFailureMessage((Context) service, "to start " + createIntent(WallpaperService.class, "view") + ", but didn't start anything")); 45 46 assertThat(new StartedServiceMatcher(intentWithExtra), 47 givesFailureMessage((Context) service, "to start " + intentWithExtra + ", but didn't start anything")); 48 } 49 50 @Test 51 public void shouldSayStartedSomethingIfWrongThingWasStarted() throws Exception { 52 Intent actualIntent = createIntent(WallpaperService.class, "anotherExtra", "anotherValue"); 53 54 service.startService(actualIntent); 55 assertThat(new StartedServiceMatcher(IntentService.class), 56 givesFailureMessage((Context) service, "to start " + createIntent(IntentService.class) + ", but started " + actualIntent)); 57 58 service.startService(actualIntent); 59 assertThat(new StartedServiceMatcher(IntentService.class, "view"), 60 givesFailureMessage((Context) service, "to start " + createIntent(IntentService.class, "view") + ", but started " + actualIntent)); 61 62 service.startService(actualIntent); 63 assertThat(new StartedServiceMatcher(intentWithExtra), 64 givesFailureMessage((Context) service, "to start " + intentWithExtra + ", but did not get the same extras keys")); 65 } 66 67 private <T> Matcher<Matcher<T>> givesFailureMessage(final T actual, final String expectedFailureMessage) { 68 return new TypeSafeMatcher<Matcher<T>>() { 69 public String message; 70 71 @Override 72 public boolean matchesSafely(Matcher<T> tMatcher) { 73 if (tMatcher.matches(actual)) { 74 message = "matcher to fail, but it passed"; 75 return false; 76 } 77 StringDescription description = new StringDescription(); 78 tMatcher.describeTo(description); 79 String actualFailureMessage = description.toString(); 80 if (expectedFailureMessage.equals(actualFailureMessage)) { 81 return true; 82 } else { 83 message = "failure message to be [" + expectedFailureMessage + "] but got [" + actualFailureMessage + "]"; 84 return false; 85 } 86 } 87 88 @Override 89 public void describeTo(Description description) { 90 description.appendText(message); 91 } 92 }; 93 } 94 95 } 96