@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_force_26: JobConfig.forceApi(JobApi.V_26); return true; case R.id.action_force_24: JobConfig.forceApi(JobApi.V_24); return true; case R.id.action_force_21: JobConfig.forceApi(JobApi.V_21); return true; case R.id.action_force_19: JobConfig.forceApi(JobApi.V_19); return true; case R.id.action_force_14: JobConfig.forceApi(JobApi.V_14); return true; case R.id.action_force_gcm: JobConfig.forceApi(JobApi.GCM); return true; default: return super.onOptionsItemSelected(item); } }
private void testAllImpl() { for (JobApi api : JobApi.values()) { if (api.isSupported(this)) { JobConfig.forceApi(api); testSimple(); } else { Log.w("Demo", String.format("%s is not supported", api)); } } JobConfig.reset(); }
@Test public void verifyAlarmIsCanceledAfterStart() throws Exception { assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); JobConfig.forceApi(JobApi.V_21); int jobId = scheduleJob(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); boolean started = TransientBundleCompat.startWithTransientBundle(context(), mJobManagerRule.getManager().getJobRequest(jobId)); assertThat(started).isTrue(); pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
@Test public void verifyAlarmIsCanceled() throws Exception { assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); JobConfig.forceApi(JobApi.V_21); int jobId = scheduleJob(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); mJobManagerRule.getManager().cancel(jobId); pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
@Test public void verifyNativeImplementationIsUsedWithO() throws Exception { // ignore test if not supported assumeTrue(JobApi.V_26.isSupported(InstrumentationRegistry.getTargetContext())); JobConfig.forceApi(JobApi.V_26); int jobId = scheduleJob(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
@Test public void verifyForceApiDisabledOtherApis() { JobApi forcedApi = JobApi.GCM; for (JobApi api : JobApi.values()) { assertThat(JobConfig.isApiEnabled(api)).isTrue(); } JobConfig.forceApi(forcedApi); for (JobApi api : JobApi.values()) { assertThat(JobConfig.isApiEnabled(api)).isEqualTo(api == forcedApi); } }
@Test public void verifyAlarmNotCanceledForPeriodicAfterStart() throws Exception { assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); JobConfig.forceApi(JobApi.V_21); Bundle extras = new Bundle(); extras.putString("key", "value"); int jobId = new JobRequest.Builder("tag") .setPeriodic(TimeUnit.DAYS.toMillis(1)) .setTransientExtras(extras) .build() .schedule(); assertThat(mJobManagerRule.getAllPendingJobsFromScheduler()).isNotNull().isNotEmpty(); JobRequest request = mJobManagerRule.getManager().getJobRequest(jobId); assertThat(request.isTransient()).isTrue(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); boolean started = TransientBundleCompat.startWithTransientBundle(context(), request); assertThat(started).isTrue(); pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); }
@Override protected void before() { Context context = InstrumentationRegistry.getTargetContext(); Executor executor = new Executor() { @Override public void execute(@NonNull Runnable command) { command.run(); } }; WorkManagerTestInitHelper.initializeTestWorkManager(context, new Configuration.Builder().setExecutor(executor).build()); JobConfig.setJobReschedulePause(0, TimeUnit.MILLISECONDS); JobConfig.setSkipJobReschedule(true); JobConfig.forceApi(JobApi.WORK_MANAGER); mManager = JobManager.create(context); mManager.cancelAll(); }
@Test public void testFlex() { JobConfig.forceApi(JobApi.V_14); long interval = JobRequest.MIN_INTERVAL * 5; long flex = JobRequest.MIN_FLEX * 5; JobRequest request = getBuilder() .setPeriodic(interval, flex) .build(); JobManager.instance().schedule(request); assertThat(request.getJobId()).isGreaterThan(0); assertThat(request.getTag()).isEqualTo(DummyJobs.SuccessJob.TAG); assertThat(request.getIntervalMs()).isEqualTo(interval); assertThat(request.getFlexMs()).isEqualTo(flex); assertThat(request.isPeriodic()).isTrue(); assertThat(request.isFlexSupport()).isTrue(); }