1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.jobscheduler.cts; 17 18 import android.annotation.TargetApi; 19 import android.app.job.JobInfo; 20 import android.app.job.JobParameters; 21 22 /** 23 * Schedules jobs with various timing constraints and ensures that they are executed when 24 * appropriate. 25 */ 26 @TargetApi(21) 27 public class TimingConstraintsTest extends ConstraintTest { 28 private static final int TIMING_JOB_ID = TimingConstraintsTest.class.hashCode() + 0; 29 private static final int CANCEL_JOB_ID = TimingConstraintsTest.class.hashCode() + 1; 30 private static final int EXPIRED_JOB_ID = TimingConstraintsTest.class.hashCode() + 2; 31 private static final int UNEXPIRED_JOB_ID = TimingConstraintsTest.class.hashCode() + 3; 32 testScheduleOnce()33 public void testScheduleOnce() throws Exception { 34 JobInfo oneTimeJob = new JobInfo.Builder(TIMING_JOB_ID, kJobServiceComponent) 35 .setOverrideDeadline(5000) // 5 secs 36 .build(); 37 38 kTestEnvironment.setExpectedExecutions(1); 39 mJobScheduler.schedule(oneTimeJob); 40 final boolean executed = kTestEnvironment.awaitExecution(); 41 assertTrue("Timed out waiting for override deadline.", executed); 42 } 43 testCancel()44 public void testCancel() throws Exception { 45 JobInfo cancelJob = new JobInfo.Builder(CANCEL_JOB_ID, kJobServiceComponent) 46 .setMinimumLatency(5000L) // make sure it doesn't actually run immediately 47 .setOverrideDeadline(7000L) 48 .build(); 49 50 kTestEnvironment.setExpectedExecutions(0); 51 mJobScheduler.schedule(cancelJob); 52 // Now cancel it. 53 mJobScheduler.cancel(CANCEL_JOB_ID); 54 assertTrue("Cancel failed: job executed when it shouldn't have.", 55 kTestEnvironment.awaitTimeout()); 56 } 57 58 /** 59 * Ensure that when a job is executed because its deadline has expired, that 60 * {@link JobParameters#isOverrideDeadlineExpired()} returns the correct value. 61 */ testJobParameters_expiredDeadline()62 public void testJobParameters_expiredDeadline() throws Exception { 63 // It is expected that the "device idle" constraint will *not* be met 64 // for the duration of the override deadline. 65 JobInfo deadlineJob = 66 new JobInfo.Builder(EXPIRED_JOB_ID, kJobServiceComponent) 67 .setRequiresDeviceIdle(true) 68 .setOverrideDeadline(2000L) 69 .build(); 70 kTestEnvironment.setExpectedExecutions(1); 71 mJobScheduler.schedule(deadlineJob); 72 assertTrue("Failed to execute deadline job", kTestEnvironment.awaitExecution()); 73 assertTrue("Job does not show its deadline as expired", 74 kTestEnvironment.getLastJobParameters().isOverrideDeadlineExpired()); 75 } 76 77 78 /** 79 * Ensure that when a job is executed and its deadline hasn't expired, that 80 * {@link JobParameters#isOverrideDeadlineExpired()} returns the correct value. 81 */ testJobParameters_unexpiredDeadline()82 public void testJobParameters_unexpiredDeadline() throws Exception { 83 84 JobInfo deadlineJob = 85 new JobInfo.Builder(UNEXPIRED_JOB_ID, kJobServiceComponent) 86 .setMinimumLatency(500L) 87 .setRequiresCharging(true) 88 .build(); 89 kTestEnvironment.setExpectedExecutions(1); 90 mJobScheduler.schedule(deadlineJob); 91 // Run everything by pretending the device was just plugged in. 92 sendExpediteStableChargingBroadcast(); 93 assertTrue("Failed to execute non-deadline job", kTestEnvironment.awaitExecution()); 94 assertFalse("Job that ran early (unexpired) didn't have" + 95 " JobParameters#isOverrideDeadlineExpired=false", 96 kTestEnvironment.getLastJobParameters().isOverrideDeadlineExpired()); 97 } 98 } 99