• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private static final int ZERO_DELAY_JOB_ID = TimingConstraintsTest.class.hashCode() + 4;
33 
testScheduleOnce()34     public void testScheduleOnce() throws Exception {
35         JobInfo oneTimeJob = new JobInfo.Builder(TIMING_JOB_ID, kJobServiceComponent)
36                         .setOverrideDeadline(5000)  // 5 secs
37                         .build();
38 
39         kTestEnvironment.setExpectedExecutions(1);
40         mJobScheduler.schedule(oneTimeJob);
41         final boolean executed = kTestEnvironment.awaitExecution();
42         assertTrue("Timed out waiting for override deadline.", executed);
43     }
44 
testCancel()45     public void testCancel() throws Exception {
46         JobInfo cancelJob = new JobInfo.Builder(CANCEL_JOB_ID, kJobServiceComponent)
47                 .setMinimumLatency(5000L) // make sure it doesn't actually run immediately
48                 .setOverrideDeadline(7000L)
49                 .setRequiresDeviceIdle(true)
50                 .build();
51 
52         kTestEnvironment.setExpectedExecutions(0);
53         mJobScheduler.schedule(cancelJob);
54         // Now cancel it.
55         mJobScheduler.cancel(CANCEL_JOB_ID);
56         assertTrue("Cancel failed: job executed when it shouldn't have.",
57                 kTestEnvironment.awaitTimeout());
58     }
59 
testExplicitZeroLatency()60     public void testExplicitZeroLatency() throws Exception {
61         JobInfo job = new JobInfo.Builder(ZERO_DELAY_JOB_ID, kJobServiceComponent)
62                 .setMinimumLatency(0L)
63                 .setRequiresDeviceIdle(true)
64                 .setOverrideDeadline(10_000L)
65                 .build();
66         kTestEnvironment.setExpectedExecutions(1);
67         mJobScheduler.schedule(job);
68         final boolean executed = kTestEnvironment.awaitExecution();
69         assertTrue("Failed to execute job with explicit zero min latency",
70                 kTestEnvironment.awaitExecution());
71     }
72 
73     /**
74      * Ensure that when a job is executed because its deadline has expired, that
75      * {@link JobParameters#isOverrideDeadlineExpired()} returns the correct value.
76      */
testJobParameters_expiredDeadline()77     public void testJobParameters_expiredDeadline() throws Exception {
78         // It is expected that the "device idle" constraint will *not* be met
79         // for the duration of the override deadline.
80         JobInfo deadlineJob =
81                 new JobInfo.Builder(EXPIRED_JOB_ID, kJobServiceComponent)
82                         .setRequiresDeviceIdle(true)
83                         .setOverrideDeadline(2000L)
84                         .build();
85         kTestEnvironment.setExpectedExecutions(1);
86         mJobScheduler.schedule(deadlineJob);
87         assertTrue("Failed to execute deadline job", kTestEnvironment.awaitExecution());
88         assertTrue("Job does not show its deadline as expired",
89                 kTestEnvironment.getLastJobParameters().isOverrideDeadlineExpired());
90     }
91 
92 
93     /**
94      * Ensure that when a job is executed and its deadline hasn't expired, that
95      * {@link JobParameters#isOverrideDeadlineExpired()} returns the correct value.
96      */
testJobParameters_unexpiredDeadline()97     public void testJobParameters_unexpiredDeadline() throws Exception {
98         JobInfo deadlineJob =
99                 new JobInfo.Builder(UNEXPIRED_JOB_ID, kJobServiceComponent)
100                         .setMinimumLatency(500L)
101                         .setRequiresStorageNotLow(true)
102                         .build();
103         kTestEnvironment.setExpectedExecutions(1);
104         setStorageState(true);
105         mJobScheduler.schedule(deadlineJob);
106         // Run everything by making storage state not-low.
107         setStorageState(false);
108         assertTrue("Failed to execute non-deadline job", kTestEnvironment.awaitExecution());
109         assertFalse("Job that ran early (unexpired) didn't have" +
110                         " JobParameters#isOverrideDeadlineExpired=false",
111                 kTestEnvironment.getLastJobParameters().isOverrideDeadlineExpired());
112     }
113 }
114