void verifyStartArguments(Bundle invocationData, IJobCallback callback) { assertBundlesEqual(invocationData, startArguments.first); assertSame(callback, startArguments.second); }
public static void assertHasSinglePrivateUnusableConstructor(Class<?> cls) throws Exception { try { assertHasSinglePrivateConstructor(cls); throw new AssertionError("Constructor didn't blow up"); } catch (InvocationTargetException e) { // Good } }
/** Just to get 100% coverage. */ @Test public void testPrivateConstructor() throws Exception { TestUtil.assertHasSinglePrivateUnusableConstructor(Constraint.class); }
@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; }
for (List<Integer> constraintList : getAllConstraintCombinations()) { for (boolean recurring : new boolean[] {true, false}) { for (boolean replaceCurrent : new boolean[] {true, false}) { for (JobTrigger trigger : TRIGGER_COMBINATIONS) { for (Class<TestJobService> service : SERVICE_COMBINATIONS) { for (Bundle extras : getBundleCombinations()) { for (RetryStrategy rs : RETRY_STRATEGY_COMBINATIONS) { result.add(
static void assertJobsEqual(JobParameters input, JobParameters output) { assertNotNull("input", input); assertNotNull("output", output); assertEquals("isRecurring()", input.isRecurring(), output.isRecurring()); assertEquals( "shouldReplaceCurrent()", input.shouldReplaceCurrent(), output.shouldReplaceCurrent()); assertEquals("getLifetime()", input.getLifetime(), output.getLifetime()); assertEquals("getTag()", input.getTag(), output.getTag()); assertEquals("getService()", input.getService(), output.getService()); assertEquals( "getConstraints()", Constraint.compact(input.getConstraints()), Constraint.compact(output.getConstraints())); assertTriggersEqual(input.getTrigger(), output.getTrigger()); assertBundlesEqual(input.getExtras(), output.getExtras()); assertRetryStrategiesEqual(input.getRetryStrategy(), output.getRetryStrategy()); }
@Test public void testCodingIsLossless() { for (JobParameters input : TestUtil.getJobCombinations(builder)) { TestUtil.assertJobsEqual(input, coder.decode(coder.encode(input, new Bundle())).build()); } }
@Test public void testCompactAndUnCompact() { for (List<Integer> combo : TestUtil.getAllConstraintCombinations()) { int[] input = TestUtil.toIntArray(combo); Arrays.sort(input); int[] output = Constraint.uncompact(Constraint.compact(input)); Arrays.sort(output); for (int i = 0; i < combo.size(); i++) { assertEquals("Combination = " + TextUtils.join(", ", combo), input[i], output[i]); } assertEquals("Expected length of two arrays to be the same", input.length, output.length); } }
@Test public void onJobFinished_successRecurringContentJob_reschedule() { JobInvocation jobInvocation = receiver.prepareJob(callbackMock, getBundleForContentJobExecutionRecurring()); receiver.onJobFinished(jobInvocation, JobService.RESULT_SUCCESS); verify(driverMock).schedule(jobArgumentCaptor.capture()); // No need to callback when job finished. // Reschedule request is treated as two events: completion of old job and scheduling of new // job with the same parameters. verifyZeroInteractions(callbackMock); Job rescheduledJob = jobArgumentCaptor.getValue(); TestUtil.assertJobsEqual(jobInvocation, rescheduledJob); }
private static void flush(JobService jobService) throws Exception { flushExecutorService(jobService.backgroundExecutor); ShadowLooper.idleMainLooper(); }
@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); }
private void verifyExecuteJob(JobInvocation input) throws Exception { when(mockContext.getPackageName()).thenReturn("com.example.foo"); receiver.setLatch(new CountDownLatch(1)); when(mockContext.bindService( any(Intent.class), any(JobServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenReturn(true); executionDelegator.executeJob(input); verify(mockContext) .bindService(intentCaptor.capture(), connCaptor.capture(), eq(BIND_AUTO_CREATE)); final Intent result = intentCaptor.getValue(); // verify the intent was sent to the right place assertEquals(input.getService(), result.getComponent().getClassName()); assertEquals(JobService.ACTION_EXECUTE, result.getAction()); final JobServiceConnection connection = connCaptor.getValue(); final AtomicReference<JobParameters> jobParametersAtomicReference = new AtomicReference<>(); IRemoteJobService.Stub jobServiceBinder = new IRemoteJobService.Stub() { @Override public void start(Bundle invocationData, IJobCallback callback) { jobParametersAtomicReference.set( GooglePlayReceiver.getJobCoder().decode(invocationData).build()); } @Override public void stop(Bundle invocationData, boolean needToSendResult) {} }; connection.onServiceConnected(null, jobServiceBinder); TestUtil.assertJobsEqual(input, jobParametersAtomicReference.get()); // Clean up started job. Otherwise new job won't be started. ExecutionDelegator.stopJob(input, false); }
@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")); }
void verifyStopArguments(Bundle invocationData, boolean needToSendResult) { assertBundlesEqual(invocationData, stopArguments.first); assertEquals(needToSendResult, stopArguments.second); }
/** Code coverage. */ @Test public void testPrivateConstructor() throws Exception { TestUtil.assertHasSinglePrivateConstructor(JobTrigger.ImmediateTrigger.class); } }
@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); }
@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); }
@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 finishesJobsQueuedAfterUnbind() throws Exception { final Queue<Pair<Bundle, Integer>> callbackResults = new ArrayDeque<>(); noopCallback = new IJobCallback.Stub() { @Override public void jobFinished(Bundle invocationData, @JobService.JobResult int result) { callbackResults.offer(Pair.create(invocationData, result)); } }; connection = new JobServiceConnection(noopCallback, contextMock); connection.onServiceConnected(null, binderMock); connection.onServiceDisconnected(null); assertThat(callbackResults).isEmpty(); // If the job is queued after the connection has been unbound (regardless of reason) then we // should NOT start it and should instead send a retry message via the callback connection.startJob(job); assertThat(callbackResults).hasSize(1); Pair<Bundle, Integer> result = callbackResults.poll(); assertBundlesEqual(jobData, result.first); assertThat(result.second).isEqualTo(Integer.valueOf(JobService.RESULT_FAIL_RETRY)); } }