GooglePlayDriver implements Driver { @Override @ScheduleResult public int schedule(@NonNull Job job) { GooglePlayReceiver.onSchedule(job); context.sendBroadcast(createScheduleRequest(job)); return FirebaseJobDispatcher.SCHEDULE_RESULT_SUCCESS; } GooglePlayDriver(@NonNull Context context); @Override boolean isAvailable(); @Override @ScheduleResult int schedule(@NonNull Job job); @Override int cancel(@NonNull String tag); @Override int cancelAll(); @NonNull @Override JobValidator getValidator();  }
@Test public void testSchedule_failsWhenPlayServicesIsUnavailable() throws Exception { markBackendUnavailable(); mockPackageManagerInfo(); Job job = null; try { job = dispatcher .newJobBuilder() .setService(TestJobService.class) .setTag("foobar") .setConstraints(Constraint.DEVICE_CHARGING) .setTrigger(Trigger.executionWindow(0, 60)) .build(); } catch (ValidationEnforcer.ValidationException ve) { fail(TextUtils.join("\n", ve.getErrors())); } assertEquals( "Expected schedule() request to fail when backend is unavailable", FirebaseJobDispatcher.SCHEDULE_RESULT_NO_DRIVER_AVAILABLE, dispatcher.schedule(job)); }
@Test public void testSchedule_sendsAppropriateBroadcast() { ArgumentCaptor<Intent> pmQueryIntentCaptor = mockPackageManagerInfo(); Job job = dispatcher .newJobBuilder() .setConstraints(Constraint.DEVICE_CHARGING) .setService(TestJobService.class) .setTrigger(Trigger.executionWindow(0, 60)) .setRecurring(false) .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL) .setTag("foobar") .build(); Intent pmQueryIntent = pmQueryIntentCaptor.getValue(); assertEquals(JobService.ACTION_EXECUTE, pmQueryIntent.getAction()); assertEquals(TestJobService.class.getName(), pmQueryIntent.getComponent().getClassName()); assertEquals( "Expected schedule() request to succeed", FirebaseJobDispatcher.SCHEDULE_RESULT_SUCCESS, dispatcher.schedule(job)); final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); verify(mMockContext).sendBroadcast(captor.capture()); Intent broadcast = captor.getValue(); assertNotNull(broadcast); assertEquals("com.google.android.gms.gcm.ACTION_SCHEDULE", broadcast.getAction()); assertEquals("SCHEDULE_TASK", broadcast.getStringExtra("scheduler_action")); assertEquals("com.google.android.gms", broadcast.getPackage()); assertEquals(8, broadcast.getIntExtra("source", -1)); assertEquals(1, broadcast.getIntExtra("source_version", -1)); final Parcelable parcelablePendingIntent = broadcast.getParcelableExtra("app"); assertTrue( "Expected 'app' value to be a PendingIntent", parcelablePendingIntent instanceof PendingIntent); }
@Test public void schedule_whenRunning_onStopIsCalled() { Bundle bundle = TestUtil.getBundleForContentJobExecution(); JobCoder prefixedCoder = new JobCoder(BundleProtocol.PACKED_PARAM_BUNDLE_PREFIX); JobInvocation invocation = prefixedCoder.decodeIntentBundle(bundle); googlePlayReceiver.prepareJob(jobCallbackMock, bundle); when(mMockContext.bindService( any(Intent.class), serviceConnectionCaptor.capture(), eq(BIND_AUTO_CREATE))) .thenReturn(true); new ExecutionDelegator(mMockContext, callbackMock, constraintCheckerMock) .executeJob(invocation); Job job = TestUtil.getBuilderWithNoopValidator() .setService(TestJobService.class) .setTrigger(invocation.getTrigger()) .setTag(invocation.getTag()) .build(); googlePlayDriver.schedule(job); verify(mMockContext).sendBroadcast(any(Intent.class)); assertTrue(serviceConnectionCaptor.getValue().wasUnbound()); assertNull( "JobServiceConnection should be removed.", ExecutionDelegator.getJobServiceConnection(invocation.getService())); }