private void networkTest(@NetworkUtil.NetworkStatus int networkType, int expected) { SchedulerConstraint constraint = mock(SchedulerConstraint.class); when(constraint.getNetworkStatus()).thenReturn(networkType); JobInfo jobInfo = schedule(constraint); assertThat(jobInfo.isPersisted(), is(true)); assertThat(jobInfo.getId(), is(1)); assertThat(jobInfo.getMinLatencyMillis(), is(0L)); assertThat(jobInfo.getMaxExecutionDelayMillis(), is(0L)); assertThat(jobInfo.getNetworkType(), is(expected)); }
@Test public void delay() { SchedulerConstraint constraint = mock(SchedulerConstraint.class); when(constraint.getNetworkStatus()).thenReturn(NetworkUtil.DISCONNECTED); when(constraint.getDelayInMs()).thenReturn(133L); JobInfo jobInfo = schedule(constraint); assertThat(jobInfo.isPersisted(), is(true)); assertThat(jobInfo.getId(), is(1)); assertThat(jobInfo.getMinLatencyMillis(), is(133L)); assertThat(jobInfo.getMaxExecutionDelayMillis(), is(0L)); }
@Test public void deadline() { SchedulerConstraint constraint = mock(SchedulerConstraint.class); when(constraint.getNetworkStatus()).thenReturn(NetworkUtil.DISCONNECTED); when(constraint.getOverrideDeadlineInMs()).thenReturn(255L); JobInfo jobInfo = schedule(constraint); assertThat(jobInfo.isPersisted(), is(true)); assertThat(jobInfo.getId(), is(1)); assertThat(jobInfo.getMinLatencyMillis(), is(0L)); assertThat(jobInfo.getMaxExecutionDelayMillis(), is(255L)); }
@Test public void deadlineAndDelay() { SchedulerConstraint constraint = mock(SchedulerConstraint.class); when(constraint.getNetworkStatus()).thenReturn(NetworkUtil.DISCONNECTED); when(constraint.getOverrideDeadlineInMs()).thenReturn(255L); when(constraint.getDelayInMs()).thenReturn(133L); JobInfo jobInfo = schedule(constraint); assertThat(jobInfo.isPersisted(), is(true)); assertThat(jobInfo.getId(), is(1)); assertThat(jobInfo.getMinLatencyMillis(), is(133L)); assertThat(jobInfo.getMaxExecutionDelayMillis(), is(255L)); }
@Override public void onReceive(Context context, Intent intent) { JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); // If there are not pending jobs. Create a sync job and schedule it. List<JobInfo> pendingJobs = jobScheduler.getAllPendingJobs(); if (pendingJobs.isEmpty()) { String inputId = context.getSharedPreferences(EpgSyncJobService.PREFERENCE_EPG_SYNC, Context.MODE_PRIVATE).getString(EpgSyncJobService.BUNDLE_KEY_INPUT_ID, null); if (inputId != null) { // Set up periodic sync only when input has set up. EpgSyncJobService.setUpPeriodicSync(context, inputId, new ComponentName(context, SampleJobService.class)); } return; } // On L/L-MR1, reschedule the pending jobs. if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) { for (JobInfo job : pendingJobs) { if (job.isPersisted()) { jobScheduler.schedule(job); } } } } }
private void assertNativeJobInfoMatchesJobInfo(android.app.job.JobInfo nativeJob, JobInfo job) { assertEquals(nativeJob.getId(), job.getId()); assertEquals(new PersistableBundle(nativeJob.getExtras()).toMap(10), job.getExtras().toMap(10)); assertEquals(nativeJob.getService(), new ComponentName(context, JobSchedulerJobService.class)); assertEquals(nativeJob.isRequireCharging(), job.isRequireCharging()); assertEquals(nativeJob.isRequireDeviceIdle(), job.isRequireDeviceIdle()); assertEquals(nativeJob.getNetworkType(), job.getNetworkType()); assertEquals(nativeJob.getMinLatencyMillis(), job.getMinLatencyMillis()); assertEquals(nativeJob.getMaxExecutionDelayMillis(), job.getMaxExecutionDelayMillis()); assertEquals(nativeJob.isPeriodic(), job.isPeriodic()); assertEquals(nativeJob.isPersisted(), job.isPersisted()); assertEquals(nativeJob.getInitialBackoffMillis(), job.getInitialBackoffMillis()); assertEquals(nativeJob.getBackoffPolicy(), job.getBackoffPolicy()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { assertEquals(nativeJob.getFlexMillis(), job.getFlexMillis()); assertArrayEquals(getUris(nativeJob.getTriggerContentUris()), getUris(job.getTriggerContentUris())); assertEquals(nativeJob.getTriggerContentUpdateDelay(), job.getTriggerContentUpdateDelay()); assertEquals(nativeJob.getTriggerContentMaxDelay(), job.getTriggerContentMaxDelay()); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { assertEquals( BundleUtils.toMap(nativeJob.getTransientExtras(), 10), BundleUtils.toMap(job.getTransientExtras(), 10)); assertEquals(nativeJob.isRequireBatteryNotLow(), job.isRequireBatteryNotLow()); assertEquals(nativeJob.isRequireStorageNotLow(), job.isRequireBatteryNotLow()); } }