private void handleStopMessage(Message message) { Builder builder = GooglePlayReceiver.getJobCoder().decode(message.getData()); if (builder == null) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Invalid stop execution message."); } return; } JobInvocation job = builder.build(); ExecutionDelegator.stopJob(job, true); } }
@Test(expected = IllegalArgumentException.class) public void testDecode_throwsOnNullBundle() { coder.decode(null); }
JobInvocation decodeIntentBundle(@NonNull Bundle bundle) { if (bundle == null) { Log.e(TAG, "Unexpected null Bundle provided"); return null; } Bundle taskExtras = bundle.getBundle(GooglePlayJobWriter.REQUEST_PARAM_EXTRAS); if (taskExtras == null) { return null; } JobInvocation.Builder builder = decode(taskExtras); List<Uri> triggeredContentUris = bundle.getParcelableArrayList(BundleProtocol.PACKED_PARAM_TRIGGERED_URIS); if (triggeredContentUris != null) { builder.setTriggerReason(new TriggerReason(triggeredContentUris)); } return builder.build(); }
@Override @BinderThread public void start(Bundle invocationData, IJobCallback callback) { JobInvocation.Builder invocation = getJobCoder().decode(invocationData); if (invocation == null) { Log.wtf(TAG, "start: unknown invocation provided"); return; } JobService.this.handleStartJobRequest(invocation.build(), callback); }
@Override @BinderThread public void stop(Bundle invocationData, boolean needToSendResult) { JobInvocation.Builder invocation = getJobCoder().decode(invocationData); if (invocation == null) { Log.wtf(TAG, "stop: unknown invocation provided"); return; } JobService.this.handleStopJobRequest(invocation.build(), needToSendResult); } };
@Override public void jobFinished(Bundle invocationData, @JobService.JobResult int result) { JobInvocation.Builder invocation = getJobCoder().decode(invocationData); if (invocation == null) { Log.wtf(TAG, "jobFinished: unknown invocation provided"); return; } ExecutionDelegator.this.onJobFinishedMessage(invocation.build(), result); } };
@Override public void start(Bundle invocationData, IJobCallback callback) { jobParametersAtomicReference.set( GooglePlayReceiver.getJobCoder().decode(invocationData).build()); }
@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(expected = IllegalArgumentException.class) public void testDecode_failsUnsupportedTrigger() { coder.decode( coder.encode(setValidBuilderDefaults(builder).setTrigger(null).build(), new Bundle())); }
@Test public void testCodingIsLossless() { for (JobParameters input : TestUtil.getJobCombinations(builder)) { TestUtil.assertJobsEqual(input, coder.decode(coder.encode(input, new Bundle())).build()); } }
@Test public void testCodingForExtras() { Bundle extras = new Bundle(); extras.putString("foo", "bar"); builder.setExtras(extras); Bundle deserializedExtras = coder .decode(coder.encode(setValidBuilderDefaults(builder).build(), new Bundle())) .build() .getExtras(); assertBundlesEqual(extras, deserializedExtras); }
void verifyCalledWithJobAndResult(JobParameters job, int result) throws Exception { Pair<Bundle, Integer> jobFinishedResult = getJobFinishedFuture().get(TIMEOUT_MS, TimeUnit.MILLISECONDS); assertNotNull(jobFinishedResult); JobCoder jc = getJobCoder(); assertEquals( // re-encode so they're the same class jc.decode(jc.encode(job, new Bundle())).build(), jc.decode(jobFinishedResult.first).build()); assertEquals(result, (int) jobFinishedResult.second); }
@Test public void testDecode_failsWhenMissingFields() { assertNull( "Expected null tag to cause decoding to fail", coder.decode( coder.encode(setValidBuilderDefaults(builder).setTag(null).build(), new Bundle()))); assertNull( "Expected null service to cause decoding to fail", coder.decode( coder.encode(setValidBuilderDefaults(builder).setService(null).build(), new Bundle()))); }
JobCoder jobCoder = GooglePlayReceiver.getJobCoder(); Bundle data = TestUtil.encodeContentUriJob(TestUtil.getContentUriTrigger(), jobCoder); JobInvocation jobInvocation = jobCoder.decode(data).build(); message.setData(data); message.replyTo = messengerMock;
@Test public void testDecode_ignoresMissingRetryStrategy() { assertNotNull( "Expected null retry strategy to cause decode to use a default", coder.decode( coder.encode( setValidBuilderDefaults(builder).setRetryStrategy(null).build(), new Bundle()))); }
@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()); }