1 /* 2 * Copyright (C) 2017 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 20 import android.annotation.TargetApi; 21 import android.app.job.JobInfo; 22 23 /** 24 * Schedules jobs with the {@link android.app.job.JobScheduler} that have storage constraints. 25 */ 26 @TargetApi(26) 27 public class StorageConstraintTest extends BaseJobSchedulerTest { 28 private static final String TAG = "StorageConstraintTest"; 29 30 /** Unique identifier for the job scheduled by this suite of tests. */ 31 public static final int STORAGE_JOB_ID = StorageConstraintTest.class.hashCode(); 32 33 private JobInfo.Builder mBuilder; 34 35 @Override setUp()36 public void setUp() throws Exception { 37 super.setUp(); 38 39 mBuilder = new JobInfo.Builder(STORAGE_JOB_ID, kJobServiceComponent); 40 } 41 42 @Override tearDown()43 public void tearDown() throws Exception { 44 mJobScheduler.cancel(STORAGE_JOB_ID); 45 super.tearDown(); 46 } 47 getJobState()48 String getJobState() throws Exception { 49 return getJobState(STORAGE_JOB_ID); 50 } 51 assertJobReady()52 void assertJobReady() throws Exception { 53 assertJobReady(STORAGE_JOB_ID); 54 } 55 assertJobWaiting()56 void assertJobWaiting() throws Exception { 57 assertJobWaiting(STORAGE_JOB_ID); 58 } 59 assertJobNotReady()60 void assertJobNotReady() throws Exception { 61 assertJobNotReady(STORAGE_JOB_ID); 62 } 63 64 // -------------------------------------------------------------------------------------------- 65 // Positives - schedule jobs under conditions that require them to pass. 66 // -------------------------------------------------------------------------------------------- 67 68 /** 69 * Schedule a job that requires the device storage is not low, when it is actually not low. 70 */ testNotLowConstraintExecutes()71 public void testNotLowConstraintExecutes() throws Exception { 72 setStorageStateLow(false); 73 74 kTestEnvironment.setExpectedExecutions(1); 75 kTestEnvironment.setExpectedWaitForRun(); 76 mJobScheduler.schedule(mBuilder.setRequiresStorageNotLow(true).build()); 77 assertJobReady(); 78 kTestEnvironment.readyToRun(); 79 80 assertTrue("Job with storage not low constraint did not fire when storage not low.", 81 kTestEnvironment.awaitExecution()); 82 } 83 84 // -------------------------------------------------------------------------------------------- 85 // Negatives - schedule jobs under conditions that require that they fail. 86 // -------------------------------------------------------------------------------------------- 87 88 /** 89 * Schedule a job that requires the device storage is not low, when it actually is low. 90 */ testNotLowConstraintFails()91 public void testNotLowConstraintFails() throws Exception { 92 setStorageStateLow(true); 93 94 kTestEnvironment.setExpectedExecutions(0); 95 kTestEnvironment.setExpectedWaitForRun(); 96 mJobScheduler.schedule(mBuilder.setRequiresStorageNotLow(true).build()); 97 assertJobWaiting(); 98 assertJobNotReady(); 99 kTestEnvironment.readyToRun(); 100 101 assertFalse("Job with storage now low constraint fired while low.", 102 kTestEnvironment.awaitExecution(250)); 103 104 // And for good measure, ensure the job runs once storage is okay. 105 kTestEnvironment.setExpectedExecutions(1); 106 kTestEnvironment.setExpectedWaitForRun(); 107 setStorageStateLow(false); 108 assertJobReady(); 109 kTestEnvironment.readyToRun(); 110 assertTrue("Job with storage not low constraint did not fire when storage not low.", 111 kTestEnvironment.awaitExecution()); 112 } 113 114 /** 115 * Test that a job that requires the device storage is not low is stopped when it becomes low. 116 */ testJobStoppedWhenStorageLow()117 public void testJobStoppedWhenStorageLow() throws Exception { 118 setStorageStateLow(false); 119 120 kTestEnvironment.setExpectedExecutions(1); 121 kTestEnvironment.setContinueAfterStart(); 122 kTestEnvironment.setExpectedWaitForRun(); 123 kTestEnvironment.setExpectedStopped(); 124 mJobScheduler.schedule(mBuilder.setRequiresStorageNotLow(true).build()); 125 assertJobReady(); 126 kTestEnvironment.readyToRun(); 127 128 assertTrue("Job with storage not low constraint did not fire when storage not low.", 129 kTestEnvironment.awaitExecution()); 130 131 setStorageStateLow(true); 132 assertTrue("Job with storage not low constraint was not stopped when storage became low.", 133 kTestEnvironment.awaitStopped()); 134 } 135 } 136