blob: 40b67c82de6fcca3be38b9129c5b91438d129df8 [file] [log] [blame]
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.jobscheduler.cts;
import android.annotation.TargetApi;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
/**
* Schedules jobs with various timing constraints and ensures that they are executed when
* appropriate.
*/
@TargetApi(21)
public class TimingConstraintsTest extends ConstraintTest {
private static final int TIMING_JOB_ID = TimingConstraintsTest.class.hashCode() + 0;
private static final int CANCEL_JOB_ID = TimingConstraintsTest.class.hashCode() + 1;
private static final int EXPIRED_JOB_ID = TimingConstraintsTest.class.hashCode() + 2;
private static final int UNEXPIRED_JOB_ID = TimingConstraintsTest.class.hashCode() + 3;
public void testScheduleOnce() throws Exception {
JobInfo oneTimeJob = new JobInfo.Builder(TIMING_JOB_ID, kJobServiceComponent)
.setOverrideDeadline(5000) // 5 secs
.build();
kTestEnvironment.setExpectedExecutions(1);
mJobScheduler.schedule(oneTimeJob);
final boolean executed = kTestEnvironment.awaitExecution();
assertTrue("Timed out waiting for override deadline.", executed);
}
public void testSchedulePeriodic() throws Exception {
JobInfo periodicJob =
new JobInfo.Builder(TIMING_JOB_ID, kJobServiceComponent)
.setPeriodic(5000L) // 5 second period.
.build();
kTestEnvironment.setExpectedExecutions(3);
mJobScheduler.schedule(periodicJob);
final boolean countedDown = kTestEnvironment.awaitExecution();
assertTrue("Timed out waiting for periodic jobs to execute", countedDown);
}
public void testCancel() throws Exception {
JobInfo cancelJob = new JobInfo.Builder(CANCEL_JOB_ID, kJobServiceComponent)
.setMinimumLatency(5000L) // make sure it doesn't actually run immediately
.setOverrideDeadline(7000L)
.build();
kTestEnvironment.setExpectedExecutions(0);
mJobScheduler.schedule(cancelJob);
// Now cancel it.
mJobScheduler.cancel(CANCEL_JOB_ID);
assertTrue("Cancel failed: job executed when it shouldn't have.",
kTestEnvironment.awaitTimeout());
}
/**
* Ensure that when a job is executed because its deadline has expired, that
* {@link JobParameters#isOverrideDeadlineExpired()} returns the correct value.
*/
public void testJobParameters_expiredDeadline() throws Exception {
JobInfo deadlineJob =
new JobInfo.Builder(EXPIRED_JOB_ID, kJobServiceComponent)
.setOverrideDeadline(2000L)
.build();
kTestEnvironment.setExpectedExecutions(1);
mJobScheduler.schedule(deadlineJob);
assertTrue("Failed to execute deadline job", kTestEnvironment.awaitExecution());
assertTrue("Job that had its deadline expire didn't have" +
" JobParameters#isOverrideDeadlineExpired=true",
kTestEnvironment.getLastJobParameters().isOverrideDeadlineExpired());
}
/**
* Ensure that when a job is executed and its deadline hasn't expired, that
* {@link JobParameters#isOverrideDeadlineExpired()} returns the correct value.
*/
public void testJobParameters_unexpiredDeadline() throws Exception {
JobInfo deadlineJob =
new JobInfo.Builder(UNEXPIRED_JOB_ID, kJobServiceComponent)
.setMinimumLatency(500L)
.setRequiresCharging(true)
.build();
kTestEnvironment.setExpectedExecutions(1);
mJobScheduler.schedule(deadlineJob);
// Run everything by pretending the device was just plugged in.
sendExpediteStableChargingBroadcast();
assertTrue("Failed to execute non-deadline job", kTestEnvironment.awaitExecution());
assertFalse("Job that ran early (unexpired) didn't have" +
" JobParameters#isOverrideDeadlineExpired=false",
kTestEnvironment.getLastJobParameters().isOverrideDeadlineExpired());
}
}