@Test public void receiveShouldStartForwardService() { Context context = Robolectric.application.getApplicationContext(); this.receiver.onReceive(context, null); Intent service = Robolectric.getShadowApplication().getNextStartedService(); assertNotNull(service); assertEquals(ForwardService.class.getName(), service.getComponent().getClassName()); }
@Test public void shouldUnregisterInterestInNetworkChangesOnDestroy() { startService(); xmppService.onDestroy(); List<ShadowApplication.Wrapper> wrappers = Robolectric.getShadowApplication().getRegisteredReceivers(); Assert.assertEquals(0, wrappers.size()); }
@Test public void shouldBeInterestedInNetworkChanges() { List<ShadowApplication.Wrapper> wrappers = Robolectric.getShadowApplication().getRegisteredReceivers(); for (ShadowApplication.Wrapper wrapper : wrappers) { if (wrapper.intentFilter.getAction(0).equals(ConnectivityManager.CONNECTIVITY_ACTION)) { return; } } Assert.fail("No intent found with CONNECTIVITY_ACTION"); }
@RunWith(NovocationTestRunner.class) public class ProviderEnabledOrDisabledTest { final Context context = spy(Robolectric.getShadowApplication().getApplicationContext()); final LocationManager locationManager = mock(LocationManager.class); final LocatorSettings settings = new LocatorSettings("com.example", "com.example.update"); final Locator locator = LocatorFactory.getInstance(); @Before public void setUp() throws Exception { doReturn(LocationManager.GPS_PROVIDER) .when(locationManager) .getBestProvider(any(Criteria.class), anyBoolean()); doReturn(locationManager).when(context).getSystemService(eq(Context.LOCATION_SERVICE)); locator.prepare(context, settings); } private void changeProviderStateTo(boolean enabled) { LocationChanged locationChanged = new LocationChanged(); Intent intent = new Intent(); intent.putExtra(LocationManager.KEY_PROVIDER_ENABLED, enabled); locationChanged.onReceive(context, intent); } private void enableProvider() { changeProviderStateTo(true); } @Test public void locator_should_restart_location_updates_when_a_provider_is_enabled() throws NoProviderAvailable { locator.startLocationUpdates(); enableProvider(); verify(locationManager, times(2)) .requestLocationUpdates(anyString(), anyLong(), anyFloat(), any(PendingIntent.class)); } @Test public void locator_should_disable_location_updates_when_a_provider_is_enabled() throws NoProviderAvailable { locator.startLocationUpdates(); enableProvider(); verify(locationManager, times(3)).removeUpdates(any(PendingIntent.class)); } private void disableProvider() { changeProviderStateTo(false); } @Test public void locator_should_restart_location_updates_when_a_provider_is_disabled() throws NoProviderAvailable { locator.startLocationUpdates(); disableProvider(); verify(locationManager, times(2)) .requestLocationUpdates(anyString(), anyLong(), anyFloat(), any(PendingIntent.class)); } @Test public void locator_should_disable_location_updates_when_a_provider_is_disabled() throws NoProviderAvailable { locator.startLocationUpdates(); disableProvider(); verify(locationManager, times(3)).removeUpdates(any(PendingIntent.class)); } }
@Test public void shouldAttemptXmppConnectionOnNetworkChange() { Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); Robolectric.getShadowApplication().sendBroadcast(intent); assertTrue(xmppConnection.isConnected()); }
/** * Non-Android accessor. * * @return the most recently created {@code AlertDialog}, or null if none has been created during * this test run */ public static AlertDialog getLatestAlertDialog() { ShadowAlertDialog dialog = Robolectric.getShadowApplication().getLatestAlertDialog(); return dialog == null ? null : dialog.realAlertDialog; }