• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 
17 package android.jobscheduler.cts;
18 
19 import android.app.job.JobInfo;
20 import android.content.pm.PackageManager;
21 
22 /**
23  * Schedules jobs with various component-enabled states.
24  */
25 public class ComponentConstraintTest extends BaseJobSchedulerTest {
26     private static final String TAG = "ComponentConstraintTest";
27     /** Unique identifier for the job scheduled by this suite of tests. */
28     private static final int COMPONENT_JOB_ID = ComponentConstraintTest.class.hashCode();
29 
30     private JobInfo.Builder mBuilder;
31 
32     @Override
setUp()33     public void setUp() throws Exception {
34         super.setUp();
35         mBuilder = new JobInfo.Builder(COMPONENT_JOB_ID, kJobServiceComponent);
36     }
37 
38     @Override
tearDown()39     public void tearDown() throws Exception {
40         setJobServiceEnabled(true);
41         super.tearDown();
42     }
43 
testScheduleAfterComponentEnabled()44     public void testScheduleAfterComponentEnabled() throws Exception {
45         setJobServiceEnabled(true);
46         kTestEnvironment.setExpectedExecutions(1);
47         mJobScheduler.schedule(mBuilder.setOverrideDeadline(0).build());
48 
49         assertTrue("Job with enabled service didn't fire.", kTestEnvironment.awaitExecution());
50     }
51 
52     /*
53         Test intentionally disabled but kept here to acknowledge the case wasn't accidentally
54         forgotten. Historically, JobScheduler has thrown an exception when an app called schedule()
55         with a disabled service. That behavior cannot be changed easily.
56 
57         public void testScheduleAfterComponentDisabled() throws Exception {
58             setJobServiceEnabled(false);
59             kTestEnvironment.setExpectedExecutions(0);
60             mJobScheduler.schedule(mBuilder.setOverrideDeadline(0).build());
61 
62             assertTrue("Job with disabled service fired.", kTestEnvironment.awaitTimeout());
63         }
64     */
65 
testComponentDisabledAfterSchedule()66     public void testComponentDisabledAfterSchedule() throws Exception {
67         setJobServiceEnabled(true);
68         kTestEnvironment.setExpectedExecutions(0);
69         mJobScheduler.schedule(mBuilder.setMinimumLatency(1000).setOverrideDeadline(2000).build());
70         setJobServiceEnabled(false);
71 
72         assertTrue("Job with disabled service fired.", kTestEnvironment.awaitTimeout());
73     }
74 
testComponentDisabledAndReenabledAfterSchedule()75     public void testComponentDisabledAndReenabledAfterSchedule() throws Exception {
76         setJobServiceEnabled(true);
77         kTestEnvironment.setExpectedExecutions(1);
78         mJobScheduler.schedule(mBuilder.setMinimumLatency(1000).setOverrideDeadline(2000).build());
79 
80         setJobServiceEnabled(false);
81         assertTrue("Job with disabled service fired.", kTestEnvironment.awaitTimeout());
82 
83         setJobServiceEnabled(true);
84         assertTrue("Job with enabled service didn't fire.", kTestEnvironment.awaitExecution());
85     }
86 
setJobServiceEnabled(boolean enabled)87     private void setJobServiceEnabled(boolean enabled) {
88         final int state = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
89                 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
90         getContext().getPackageManager().setComponentEnabledSetting(
91                 kJobServiceComponent, state, PackageManager.DONT_KILL_APP);
92     }
93 }
94