@NonNull private Bundle getBundleForContentJobExecutionRecurring() { Bundle bundle = new Bundle(); Bundle encode = encodeRecurringContentUriJob(getContentUriTrigger(), TestUtil.JOB_CODER); bundle.putBundle(GooglePlayJobWriter.REQUEST_PARAM_EXTRAS, encode); bundle.putParcelableArrayList(BundleProtocol.PACKED_PARAM_TRIGGERED_URIS, TestUtil.URIS); return bundle; }
@NonNull static Bundle getBundleForContentJobExecution() { Bundle bundle = new Bundle(); Bundle encode = encodeContentUriJob(getContentUriTrigger(), JOB_CODER); bundle.putBundle(GooglePlayJobWriter.REQUEST_PARAM_EXTRAS, encode); bundle.putParcelableArrayList(BundleProtocol.PACKED_PARAM_TRIGGERED_URIS, URIS); return bundle; }
@Test public void encode_contentUriTrigger() { Bundle encode = TestUtil.encodeContentUriJob(TestUtil.getContentUriTrigger(), coder); int triggerType = encode.getInt(PREFIX + BundleProtocol.PACKED_PARAM_TRIGGER_TYPE); assertEquals("Trigger type", BundleProtocol.TRIGGER_TYPE_CONTENT_URI, triggerType); String json = encode.getString(PREFIX + BundleProtocol.PACKED_PARAM_OBSERVED_URI); String expectedJson = "{\"uri_flags\":[1,0],\"uris\":[\"content:\\/\\/com.android.contacts" + "\",\"content:\\/\\/media\\/external\\/images\\/media\"]}"; assertEquals("Json trigger", expectedJson, json); }
@Test public void securityException_unbinds() throws Exception { JobInvocation job = new JobInvocation.Builder() .setTag("TAG") .setTrigger(getContentUriTrigger()) .setService(TestJobService.class.getName()) .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL) .build(); when(mockContext.bindService( any(Intent.class), any(ServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenThrow(new SecurityException("should have gracefully handled this security exception")); executionDelegator.executeJob(job); // Verify that bindService was actually called verify(mockContext).bindService(any(Intent.class), connCaptor.capture(), eq(BIND_AUTO_CREATE)); // And that unbindService was called in response to the security exception verify(mockContext).unbindService(connCaptor.getValue()); // And that we should have sent RESULT_FAIL_RETRY back to the calling component assertEquals(JobService.RESULT_FAIL_RETRY, receiver.lastResult); }
@Test public void failedToBind_unbind() throws Exception { JobInvocation job = new JobInvocation.Builder() .setTag("TAG") .setTrigger(getContentUriTrigger()) .setService(TestJobService.class.getName()) .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL) .build(); when(mockContext.bindService( any(Intent.class), any(ServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenReturn(false); executionDelegator.executeJob(job); verify(mockContext) .bindService(intentCaptor.capture(), connCaptor.capture(), eq(BIND_AUTO_CREATE)); verify(mockContext).unbindService(connCaptor.getValue()); assertEquals(JobService.RESULT_FAIL_RETRY, receiver.lastResult); }
@Test public void decode_addBundleAsExtras() { ContentUriTrigger contentUriTrigger = TestUtil.getContentUriTrigger(); Bundle bundle = TestUtil.encodeContentUriJob(contentUriTrigger, coder); bundle.putString("test_key", "test_value"); JobInvocation decode = coder.decode(bundle).build(); assertEquals("test_value", decode.getExtras().getString("test_key")); }
@Test public void prepareJob() { Intent intent = new Intent(); Bundle encode = encodeContentUriJob(getContentUriTrigger(), TestUtil.JOB_CODER); intent.putExtra(GooglePlayJobWriter.REQUEST_PARAM_EXTRAS, encode); Parcel container = Parcel.obtain(); container.writeStrongBinder(new Binder()); PendingCallback pcb = new PendingCallback(container); intent.putExtra("callback", pcb); ArrayList<Uri> uris = new ArrayList<>(); uris.add(ContactsContract.AUTHORITY_URI); uris.add(Media.EXTERNAL_CONTENT_URI); intent.putParcelableArrayListExtra(BundleProtocol.PACKED_PARAM_TRIGGERED_URIS, uris); JobInvocation jobInvocation = receiver.prepareJob(intent); assertEquals(jobInvocation.getTriggerReason().getTriggeredContentUris(), uris); }
message.what = GooglePlayMessageHandler.MSG_STOP_EXEC; JobCoder jobCoder = GooglePlayReceiver.getJobCoder(); Bundle data = TestUtil.encodeContentUriJob(TestUtil.getContentUriTrigger(), jobCoder); JobInvocation jobInvocation = jobCoder.decode(data).build(); message.setData(data);
@Test public void decode_contentUriTrigger() { ContentUriTrigger contentUriTrigger = TestUtil.getContentUriTrigger(); Bundle bundle = TestUtil.encodeContentUriJob(contentUriTrigger, coder); JobInvocation decode = coder.decode(bundle).build(); ContentUriTrigger trigger = (ContentUriTrigger) decode.getTrigger(); assertEquals(contentUriTrigger.getUris(), trigger.getUris()); }
@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()); } }