@Test @Config(sdk = 21) public void verifyExactJobRescheduled() throws Exception { assertThat(manager().getAllJobRequests()).isEmpty(); ContentValues contentValues = new JobRequest.Builder("tag") .setExact(TimeUnit.HOURS.toMillis(1)) .build() .toContentValues(); manager().getJobStorage().getDatabase() .insert(JobStorage.JOB_TABLE_NAME, null, contentValues); Set<JobRequest> requests = manager().getAllJobRequests(); assertThat(requests).isNotEmpty(); final int jobId = 1; Intent intent = new Intent(context(), PlatformAlarmReceiver.class); assertThat(PendingIntent.getBroadcast(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE)).isNull(); int rescheduledJobs = new JobRescheduleService().rescheduleJobs(manager()); assertThat(rescheduledJobs).isEqualTo(1); assertThat(PendingIntent.getBroadcast(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE)).isNotNull(); }
@Test public void testWarningWhenTooFarInTheFuture() { class TestPrinter implements JobLogger { private final List<String> mMessages = new ArrayList<>(); @Override public void log(int priority, @NonNull String tag, @NonNull String message, @Nullable Throwable t) { mMessages.add(message); } } TestPrinter testPrinter = new TestPrinter(); JobConfig.addLogger(testPrinter); getBuilder().setExecutionWindow(TimeUnit.DAYS.toMillis(366), TimeUnit.DAYS.toMillis(367)).build(); getBuilder().setExact(TimeUnit.DAYS.toMillis(366)).build(); JobConfig.removeLogger(testPrinter); assertThat(testPrinter.mMessages).containsSubsequence( "Warning: job with tag SuccessJob scheduled over a year in the future", "Warning: job with tag SuccessJob scheduled over a year in the future" ); }
@Test public void verifyTransientJobNotRescheduled() throws Exception { assertThat(manager().getAllJobRequests()).isEmpty(); Bundle bundle = new Bundle(); bundle.putString("key", "value"); ContentValues contentValues = new JobRequest.Builder("tag") .setExact(TimeUnit.HOURS.toMillis(1)) .setTransientExtras(bundle) .build() .toContentValues(); manager().getJobStorage().getDatabase() .insert(JobStorage.JOB_TABLE_NAME, null, contentValues); Set<JobRequest> requests = manager().getAllJobRequests(); assertThat(requests).isEmpty(); } }
/** * Specify that the job should start immediately. This is similar to an exact job and has * the same constraints, e.g. no other requirements like a specific network condition * are allowed. This method overrides any specified time window. * * <br> * <br> * * The advantage if a job that starts immediately compared to implementing your own * {@link Service} is that jobs run in parallel and can be rescheduled if necessary. It * also respect the background execution limit introduced in Android O, meaning that if * it's not allowed to start a {@link Service}, then the work is delayed. * * @see #setExact(long) */ public Builder startNow() { return setExact(START_NOW); }
/** * Specify that the job should start immediately. This is similar to an exact job and has * the same constraints, e.g. no other requirements like a specific network condition * are allowed. This method overrides any specified time window. * * <br> * <br> * * The advantage if a job that starts immediately compared to implementing your own * {@link Service} is that jobs run in parallel and can be rescheduled if necessary. It * also respect the background execution limit introduced in Android O, meaning that if * it's not allowed to start a {@link Service}, then the work is delayed. * * @see #setExact(long) */ public Builder startNow() { return setExact(START_NOW); }
@Test public void verifyNotFoundJobCanceledExact() { final String tag = "something"; final int jobId = new JobRequest.Builder(tag) .setExact(TimeUnit.HOURS.toMillis(4)) .build() .schedule(); assertThat(manager().getAllJobRequestsForTag(tag)).hasSize(1); executeJob(jobId, Job.Result.FAILURE); assertThat(manager().getAllJobRequestsForTag(tag)).isEmpty(); }