DailyJob extends Job { public static int schedule(@NonNull JobRequest.Builder baseBuilder, long startMs, long endMs) { return schedule(baseBuilder, true, startMs, endMs, false); }  static int schedule(@NonNull JobRequest.Builder baseBuilder, long startMs, long endMs); static void scheduleAsync(@NonNull JobRequest.Builder baseBuilder, long startMs, long endMs); static void scheduleAsync(@NonNull final JobRequest.Builder baseBuilder, final long startMs, final long endMs,
                                     @NonNull final JobRequest.JobScheduledCallback callback); static int startNowOnce(@NonNull JobRequest.Builder baseBuilder);  }
@Test public void verifyExtraValuesAreOverwritten() { long start = 0; long end = 1L; PersistableBundleCompat extras = new PersistableBundleCompat(); extras.putLong("something", 9L); extras.putLong(DailyJob.EXTRA_START_MS, 9L); extras.putLong(DailyJob.EXTRA_END_MS, 9L); int jobId = DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessJob.class).setExtras(extras), start, end); JobRequest request = manager().getJobRequest(jobId); assertThat(request).isNotNull(); assertThat(request.getExtras().getLong(DailyJob.EXTRA_START_MS, -1)).isEqualTo(0L); assertThat(request.getExtras().getLong(DailyJob.EXTRA_END_MS, -1)).isEqualTo(1L); assertThat(request.getExtras().size()).isEqualTo(3); }
@Test public void verifyDailyJobIsNotExact() { long time = 1L; int jobId = DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessJob.class), time, time); JobRequest request = manager().getJobRequest(jobId); assertThat(request).isNotNull(); assertThat(request.isExact()).isFalse(); }
@Test public void verifyScheduledTwiceOverridesExisting() { long time = 1L; DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessJob.class), time, time); DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessJob.class), time, time); Set<JobRequest> requests = manager().getAllJobRequests(); assertThat(requests).hasSize(1); assertThat(requests.iterator().next().getTag()).isEqualTo(DummyJobs.SuccessJob.TAG); }
@SuppressWarnings("ConstantConditions") @Test public void verifyEarlyExecution() { TestClock clock = new TestClock(); clock.setTime(13, 0); JobRequest request = verifyExecutionAndSuccessfulReschedule(clock, TimeUnit.HOURS.toMillis(14), TimeUnit.HOURS.toMillis(15)); assertThat(request.getStartMs()).isEqualTo(TimeUnit.HOURS.toMillis(25)); assertThat(request.getEndMs()).isEqualTo(TimeUnit.HOURS.toMillis(26)); int id = DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessDailyJob.class), TimeUnit.HOURS.toMillis(14), TimeUnit.HOURS.toMillis(15)); request = manager().getJobRequest(id); assertThat(request.getStartMs()).isEqualTo(TimeUnit.HOURS.toMillis(1)); assertThat(request.getEndMs()).isEqualTo(TimeUnit.HOURS.toMillis(2)); }
@Test public void verifyRequirementsEnforcedSkipsJob() { long time = 1L; final AtomicBoolean atomicBoolean = new AtomicBoolean(true); manager().addJobCreator(new JobCreator() { @Override public Job create(@NonNull String tag) { return new DailyJob() { @NonNull @Override protected DailyJobResult onRunDailyJob(@NonNull Params params) { atomicBoolean.set(false); return DailyJobResult.SUCCESS; } }; } }); int jobId = DailyJob.schedule(new JobRequest.Builder("any").setRequiresCharging(true).setRequirementsEnforced(true), time, time); assertThat(manager().getAllJobRequests()).hasSize(1); executeJob(jobId, Job.Result.SUCCESS); assertThat(manager().getAllJobRequests()).hasSize(1); assertThat(atomicBoolean.get()).isTrue(); manager().cancelAll(); jobId = DailyJob.schedule(new JobRequest.Builder("any").setRequiresCharging(false).setRequirementsEnforced(true), time, time); assertThat(manager().getAllJobRequests()).hasSize(1); executeJob(jobId, Job.Result.SUCCESS); assertThat(manager().getAllJobRequests()).hasSize(1); assertThat(atomicBoolean.get()).isFalse(); }
@Test(expected = IllegalArgumentException.class) public void verifyTooLargeValue() { long start = TimeUnit.HOURS.toMillis(24); long end = 1L; DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessJob.class), start, end); }
@Test public void verifyScheduledAtMidnight() { long start = 0; long end = 1L; DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessJob.class), start, end); assertThat(manager().getAllJobRequests()).hasSize(1); }
@Test public void verifyHasExtras() { long start = 0; long end = 1L; int jobId = DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessJob.class), start, end); JobRequest request = manager().getJobRequest(jobId); assertThat(request).isNotNull(); assertThat(request.getExtras().getLong(DailyJob.EXTRA_START_MS, -1)).isEqualTo(0L); assertThat(request.getExtras().getLong(DailyJob.EXTRA_END_MS, -1)).isEqualTo(1L); assertThat(request.getExtras().size()).isEqualTo(2); }