JobService extends Service { @Override @MainThread public final int onStartCommand(Intent intent, int flags, int startId) { stopSelf(startId); return START_NOT_STICKY; }  @MainThread abstract boolean onStartJob(@NonNull JobParameters job); @MainThread abstract boolean onStopJob(@NonNull JobParameters job); @AnyThread final void jobFinished(@NonNull JobParameters job, boolean needsReschedule); @Override @MainThread final int onStartCommand(Intent intent, int flags, int startId); @Nullable @Override @MainThread final IBinder onBind(Intent intent); @Override @MainThread final boolean onUnbind(Intent intent); @Override @MainThread final void onRebind(Intent intent); @Override @MainThread final void onStart(Intent intent, int startId); @Override @MainThread final void onConfigurationChanged(Configuration newConfig); @Override @MainThread final void onTaskRemoved(Intent rootIntent); static final int RESULT_SUCCESS; static final int RESULT_FAIL_RETRY; static final int RESULT_FAIL_NORETRY; }
@Test public void testOnStartCommand_handlesNullIntent() { JobService service = spy(new ExampleJobService()); int startId = 7; try { service.onStartCommand(null, 0, startId); verify(service).stopSelf(startId); } catch (NullPointerException npe) { fail("Unexpected NullPointerException after calling onStartCommand with a null Intent."); } }
@Test public void testOnStartCommand_handlesNullAction() { JobService service = spy(new ExampleJobService()); int startId = 7; Intent nullActionIntent = new Intent(); service.onStartCommand(nullActionIntent, 0, startId); verify(service).stopSelf(startId); }
@Test public void testOnStartCommand_handlesEmptyAction() { JobService service = spy(new ExampleJobService()); int startId = 7; Intent emptyActionIntent = new Intent(""); service.onStartCommand(emptyActionIntent, 0, startId); verify(service).stopSelf(startId); }
@Test public void testOnStartCommand_handlesUnknownAction() { JobService service = spy(new ExampleJobService()); int startId = 7; Intent emptyActionIntent = new Intent("foo.bar.baz"); service.onStartCommand(emptyActionIntent, 0, startId); verify(service).stopSelf(startId); }
@Test public void testOnStartCommand_handlesStartJob_nullData() { JobService service = spy(new ExampleJobService()); int startId = 7; Intent executeJobIntent = new Intent(JobService.ACTION_EXECUTE); service.onStartCommand(executeJobIntent, 0, startId); verify(service).stopSelf(startId); }
@Test public void testOnStartCommand_handlesStartJob_noTag() { JobService service = spy(new ExampleJobService()); int startId = 7; Intent executeJobIntent = new Intent(JobService.ACTION_EXECUTE); Parcel p = Parcel.obtain(); p.writeStrongBinder(mock(IBinder.class)); executeJobIntent.putExtra("callback", new PendingCallback(p)); service.onStartCommand(executeJobIntent, 0, startId); verify(service).stopSelf(startId); p.recycle(); }
@Test public void testOnStartCommand_handlesStartJob_noCallback() { JobService service = spy(new ExampleJobService()); int startId = 7; Intent executeJobIntent = new Intent(JobService.ACTION_EXECUTE); executeJobIntent.putExtra("tag", "foobar"); service.onStartCommand(executeJobIntent, 0, startId); verify(service).stopSelf(startId); }