@Test @Config(minSdk = VERSION_CODES.O) public void startForegroundService() { Intent intent = new Intent(); context.startForegroundService(intent); assertThat(ShadowApplication.getInstance().getNextStartedService()).isEqualTo(intent); }
@Test public void startIntentSender_serviceIntent() throws IntentSender.SendIntentException { PendingIntent intent = PendingIntent.getService( context, 0, new Intent().setClassName(context, "ServiceIntent"), PendingIntent.FLAG_UPDATE_CURRENT); context.startIntentSender(intent.getIntentSender(), null, 0, 0, 0); assertThat(shadowOf(context).getNextStartedService().getComponent().getClassName()) .isEqualTo("ServiceIntent"); }
@Test public void bindServiceDelegatesToShadowApplication() { contextWrapper.bindService(new Intent("foo"), new TestService(), Context.BIND_AUTO_CREATE); assertEquals( "foo", shadowOf((Application) ApplicationProvider.getApplicationContext()) .getNextStartedService() .getAction()); }
@Test public void shouldClearStartedServiceIntents() { context.startService(getSomeActionIntent("some.action")); context.startService(getSomeActionIntent("another.action")); shadowOf(context).clearStartedServices(); assertNull(shadowOf(context).getNextStartedService()); }
/** * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the * started services stack. * * @return the next started {@code Intent} for a service */ public Intent getNextStartedService() { return ShadowApplication.getInstance().getNextStartedService(); }
/** * Delegates to the application to consume and return the next {@code Intent} on the * started services stack. * * @return the next started {@code Intent} for a service */ public Intent getNextStartedService() { return ShadowApplication.getInstance().getNextStartedService(); }
/** * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the * started services stack. * * @return the next started {@code Intent} for a service */ public Intent getNextStartedService() { return ShadowApplication.getInstance().getNextStartedService(); }
@Test public void testScheduleRunsService() { scheduler.schedule(job); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); }
@Test public void testJobFinishedRunsService() { scheduler.onJobCompleted(0, false); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); scheduler.onJobCompleted(0, true); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); }
@Test public void testCancelRunsService() { scheduler.cancel(0); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); }
@Test public void testCancelAllRunsService() { scheduler.cancelAll(); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); }
@Test public void testReceiversStartService() { new AlarmReceiver().onReceive(context, new Intent(Intent.ACTION_BOOT_COMPLETED)); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); new AlarmReceiver.BatteryReceiver().onReceive(context, new Intent(Intent.ACTION_POWER_CONNECTED)); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); new AlarmReceiver.StorageReceiver().onReceive(context, new Intent(Intent.ACTION_DEVICE_STORAGE_LOW)); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); new AlarmReceiver.ConnectivityReceiver().onReceive( context, new Intent(ConnectivityManager.CONNECTIVITY_ACTION)); assertEquals(ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName(), AlarmJobService.class.getName()); } }
@Test public void testContentTriggerConstraint() { jobStore.add(JobStatus.createFromJobInfo( JobCreator.create(context, 0, DELAY_MS) .addTriggerContentUri(new JobInfo.TriggerContentUri(Uri.parse("com.doist"), 0)) .build(), AlarmScheduler.TAG)); service.startCommand(0, 0); assertEquals(ContentObserverService.class.getCanonicalName(), ShadowApplication.getInstance().getNextStartedService().getComponent().getClassName()); }
@Test public void testObserversFire() { Uri[] uris = new Uri[]{Uri.parse("doist.com"), Uri.parse("todoist.com"), Uri.parse("twistapp.com")}; for (int i = 0; i < uris.length; i++) { jobStore.add(JobStatus.createFromJobInfo( JobCreator.create(context, i, 5000) .addTriggerContentUri(new JobInfo.TriggerContentUri(uris[i], 0)) .build(), AlarmScheduler.TAG)); } service.startCommand(0, 0); ShadowApplication application = ShadowApplication.getInstance(); ShadowContentResolver contentResolver = shadowOf(context.getContentResolver()); for (Uri uri : uris) { assertEquals(0, application.getBoundServiceConnections().size()); assertEquals(1, contentResolver.getContentObservers(uri).size()); contentResolver.notifyChange(uri, null); DeviceTestUtils.advanceTime(JobStatus.DEFAULT_TRIGGER_MAX_DELAY); assertEquals(1, contentResolver.getContentObservers(uri).size()); assertEquals(AlarmJobService.class.getCanonicalName(), application.getNextStartedService().getComponent().getClassName()); } } }