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 public void onJobFinished_unknownJobCallbackIsNotPresent_ignoreNoException() { receiver.onJobFinished(jobInvocationBuilder.build(), JobService.RESULT_SUCCESS); verifyZeroInteractions(driverMock); }
@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); }
@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); } };
@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 contract_hashCode_equals_triggerShouldBeIgnored() { JobInvocation jobInvocation = builder.build(); JobInvocation periodic = builder.setTrigger(Trigger.executionWindow(0, 1)).build(); assertEquals(jobInvocation, periodic); assertEquals(jobInvocation.hashCode(), periodic.hashCode()); } }
@Test public void contract_hashCode_equals() { JobInvocation jobInvocation = builder.build(); assertEquals(jobInvocation, builder.build()); assertEquals(jobInvocation.hashCode(), builder.build().hashCode()); JobInvocation jobInvocationNew = builder.setTag("new").build(); assertNotEquals(jobInvocation, jobInvocationNew); assertNotEquals(jobInvocation.hashCode(), jobInvocationNew.hashCode()); }
@Test public void testAreConstraintsSatisfied_nonNetworkConstraint() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.DEVICE_IDLE)).build(); assertThat(constraintChecker.areConstraintsSatisfied(job)).isTrue(); }
@Test public void testAreConstraintsSatisfied_unmeteredNetworkRequired_unsatisfied_networkMetered() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_UNMETERED_NETWORK)).build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ true); setNetworkMetered(true); assertThat(constraintChecker.areConstraintsSatisfied(job)).isFalse(); }
@Test public void testCodingIsLossless() { for (JobParameters input : TestUtil.getJobCombinations(builder)) { TestUtil.assertJobsEqual(input, coder.decode(coder.encode(input, new Bundle())).build()); } }
@Test public void testAreConstraintsSatisfied_nonNetworkConstraints() { JobInvocation job = jobBuilder .setConstraints( Constraint.uncompact(Constraint.DEVICE_IDLE | Constraint.DEVICE_CHARGING)) .build(); assertThat(constraintChecker.areConstraintsSatisfied(job)).isTrue(); }
@Test public void testAreConstraintsSatisfied_unmeteredNetworkRequired_unsatisfied_networkDisconnected() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_UNMETERED_NETWORK)).build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ false); setNetworkMetered(false); assertThat(constraintChecker.areConstraintsSatisfied(job)).isFalse(); }
@Override public void start(Bundle invocationData, IJobCallback callback) { jobParametersAtomicReference.set( GooglePlayReceiver.getJobCoder().decode(invocationData).build()); }
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 testAreConstraintsSatisfied_anyNetworkRequired_unsatisfied_nullNetworkInfo() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_ANY_NETWORK)).build(); shadowConnectivityManager.setActiveNetworkInfo(null); assertThat(constraintChecker.areConstraintsSatisfied(job)).isFalse(); }
@Test public void testAreConstraintsSatisfied_anyNetworkRequired_unsatisfied_notConnected() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_ANY_NETWORK)).build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ false); assertThat(constraintChecker.areConstraintsSatisfied(job)).isFalse(); }
@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); } };
@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")); }