@Test public void executeJob_constraintsUnsatisfied_schedulesJobRetry() throws RemoteException { // Simulate job constraints not being met. JobInvocation jobInvocation = new JobInvocation.Builder() .setTag("tag") .setService("service") .setTrigger(Trigger.NOW) .build(); when(constraintChecker.areConstraintsSatisfied(eq(jobInvocation))).thenReturn(false); executionDelegator.executeJob(jobInvocation); // Confirm that service not bound verify(mockContext, never()) .bindService(any(Intent.class), any(JobServiceConnection.class), anyInt()); assertThat(ExecutionDelegator.getJobServiceConnection("service")).isNull(); // Verify that job is set for a retry later. assertThat(receiver.lastResult).isEqualTo(JobService.RESULT_FAIL_RETRY); }
serviceConnection, ExecutionDelegator.getJobServiceConnection(jobInvocation.getService())); assertFalse(serviceConnection.wasUnbound());
@Test public void jobFinished() throws RemoteException { JobInvocation jobInvocation = new JobInvocation.Builder() .setTag("tag") .setService("service") .setTrigger(Trigger.NOW) .build(); when(mockContext.bindService( any(Intent.class), any(ServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenReturn(true); executionDelegator.executeJob(jobInvocation); verify(mockContext) .bindService(intentCaptor.capture(), connCaptor.capture(), eq(BIND_AUTO_CREATE)); JobServiceConnection connection = connCaptor.getValue(); when(iBinderMock.queryLocalInterface(IRemoteJobService.class.getName())) .thenReturn(jobServiceMock); connection.onServiceConnected(null, iBinderMock); verify(jobServiceMock).start(bundleCaptor.capture(), jobCallbackCaptor.capture()); jobCallbackCaptor .getValue() .jobFinished(bundleCaptor.getValue(), JobService.RESULT_FAIL_NORETRY); assertNull(ExecutionDelegator.getJobServiceConnection("service")); assertEquals(JobService.RESULT_FAIL_NORETRY, receiver.lastResult); assertTrue(connection.wasUnbound()); }
@Test public void onReschedule_stopJob() { Bundle bundle = TestUtil.getBundleForContentJobExecution(); JobCoder prefixedCoder = new JobCoder(BundleProtocol.PACKED_PARAM_BUNDLE_PREFIX); JobInvocation invocation = prefixedCoder.decodeIntentBundle(bundle); Job job = TestUtil.getBuilderWithNoopValidator() .setService(TestJobService.class) .setTrigger(invocation.getTrigger()) .setTag(invocation.getTag()) .build(); receiver.prepareJob(jobCallbackMock, bundle); when(contextMock.bindService( any(Intent.class), any(JobServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenReturn(true); new ExecutionDelegator(contextMock, mock(JobFinishedCallback.class), contraintCheckerMock) .executeJob(invocation); verify(contextMock) .bindService(any(Intent.class), jobServiceConnectionCaptor.capture(), eq(BIND_AUTO_CREATE)); assertTrue(jobServiceConnectionCaptor.getValue().hasJobInvocation(invocation)); GooglePlayReceiver.onSchedule(job); assertFalse(jobServiceConnectionCaptor.getValue().hasJobInvocation(invocation)); assertNull( "JobServiceConnection should be removed.", ExecutionDelegator.getJobServiceConnection(invocation.getService())); }
@Test public void schedule_whenRunning_onStopIsCalled() { // simulate running job 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); // reschedule request during the execution verify(mMockContext).sendBroadcast(any(Intent.class)); assertTrue(serviceConnectionCaptor.getValue().wasUnbound()); assertNull( "JobServiceConnection should be removed.", ExecutionDelegator.getJobServiceConnection(invocation.getService())); }