JobDispatcher { public void dispatch(@NonNull JobInfo jobInfo) { try { if (requiresScheduling(jobInfo)) { getScheduler().schedule(context, jobInfo, getScheduleId(jobInfo.getId())); return; } try { getScheduler().cancel(context, jobInfo.getId()); context.startService(AirshipService.createIntent(context, jobInfo, null)); } catch (SecurityException | IllegalStateException ex) { getScheduler().schedule(context, jobInfo, getScheduleId(jobInfo.getId())); } } catch (SchedulerException e) { Logger.error(e, "Scheduler failed to schedule jobInfo"); if (useFallbackScheduler()) { dispatch(jobInfo); } } } private  JobDispatcher(@NonNull Context context); @VisibleForTesting  JobDispatcher(@NonNull Context context, SchedulerFactory schedulerFactory, ActivityMonitor activityMonitor); @NonNull static JobDispatcher shared(@NonNull Context context); void dispatch(@NonNull JobInfo jobInfo); void cancel(int jobId);  }
@Test public void testDispatch() throws SchedulerException { dispatcher.dispatch(jobInfo); verify(mockScheduler).schedule(context, jobInfo, 3000002); }
@Test public void testDispatchImmediately() { activityMonitor.startActivity(); dispatcher.dispatch(jobInfo); Intent intent = shadowOf(RuntimeEnvironment.application).getNextStartedService(); Intent expectedIntent = AirshipService.createIntent(context, jobInfo, null); assertEquals(expectedIntent.getAction(), intent.getAction()); assertEquals(expectedIntent.getComponent(), intent.getComponent()); assertBundlesEquals(expectedIntent.getExtras(), intent.getExtras()); }
@Test public void testDispatchFallback() throws SchedulerException { doThrow(new SchedulerException("test")) .when(mockScheduler) .schedule(context, jobInfo, 3000002); dispatcher.dispatch(jobInfo); verify(mockFallbackScheduler).schedule(context, jobInfo, 3000002); }
@Test public void testCatchesTransactionTooLargeException() throws Exception { Context context = mock(Context.class); when(context.getApplicationContext()).thenReturn(context); when(context.getPackageName()).thenReturn("package"); PackageManager pm = Mockito.mock(PackageManager.class); when(context.getPackageManager()).thenReturn(pm); when(pm.getApplicationInfo(anyString(), anyInt())).thenThrow(new RuntimeException(new TransactionTooLargeException())); JobDispatcher dispatcher = new JobDispatcher(context, new JobDispatcher.SchedulerFactory() { @NonNull @Override public Scheduler createScheduler(Context context) { return mockScheduler; } @NonNull @Override public Scheduler createFallbackScheduler(Context context) { return mockFallbackScheduler; } }, activityMonitor); dispatcher.dispatch(jobInfo); }