private void requestRetryForJob(JobInvocation job) { try { callback.jobFinished(encodeJob(job), JobService.RESULT_FAIL_RETRY); } catch (RemoteException e) { Log.e(TAG, "Error sending result for job " + job.getTag() + ": " + e); } } }
@Nullable JobInvocation prepareJob(JobCallback callback, Bundle bundle) { JobInvocation job = prefixedCoder.decodeIntentBundle(bundle); if (job == null) { Log.e(TAG, "unable to decode job"); sendResultSafely(callback, JobService.RESULT_FAIL_NORETRY); return null; } synchronized (callbacks) { SimpleArrayMap<String, JobCallback> map = callbacks.get(job.getService()); if (map == null) { map = new SimpleArrayMap<>(1); callbacks.put(job.getService(), map); } map.put(job.getTag(), callback); } return job; }
@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())); }
@Test public void decodeIntentBundle() { Bundle bundle = new Bundle(); ContentUriTrigger uriTrigger = getContentUriTrigger(); Bundle encode = encodeContentUriJob(uriTrigger, coder); bundle.putBundle(GooglePlayJobWriter.REQUEST_PARAM_EXTRAS, encode); ArrayList<Uri> uris = new ArrayList<>(); uris.add(ContactsContract.AUTHORITY_URI); uris.add(Media.EXTERNAL_CONTENT_URI); bundle.putParcelableArrayList(BundleProtocol.PACKED_PARAM_TRIGGERED_URIS, uris); JobInvocation jobInvocation = coder.decodeIntentBundle(bundle); assertEquals(uris, jobInvocation.getTriggerReason().getTriggeredContentUris()); assertEquals("TAG", jobInvocation.getTag()); assertEquals(uriTrigger.getUris(), ((ContentUriTrigger) jobInvocation.getTrigger()).getUris()); assertEquals(TestJobService.class.getName(), jobInvocation.getService()); assertEquals( RetryStrategy.DEFAULT_EXPONENTIAL.getPolicy(), jobInvocation.getRetryStrategy().getPolicy()); } }