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.JobScheduler; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.jobscheduler.MockJobService; 24 import android.jobscheduler.TriggerContentJobService; 25 import android.test.AndroidTestCase; 26 27 /** 28 * Common functionality from which the other test case classes derive. 29 */ 30 @TargetApi(21) 31 public abstract class ConstraintTest extends AndroidTestCase { 32 /** Force the scheduler to consider the device to be on stable charging. */ 33 private static final Intent EXPEDITE_STABLE_CHARGING = 34 new Intent("com.android.server.task.controllers.BatteryController.ACTION_CHARGING_STABLE"); 35 36 /** Environment that notifies of JobScheduler callbacks. */ 37 static MockJobService.TestEnvironment kTestEnvironment = 38 MockJobService.TestEnvironment.getTestEnvironment(); 39 static TriggerContentJobService.TestEnvironment kTriggerTestEnvironment = 40 TriggerContentJobService.TestEnvironment.getTestEnvironment(); 41 /** Handle for the service which receives the execution callbacks from the JobScheduler. */ 42 static ComponentName kJobServiceComponent; 43 static ComponentName kTriggerContentServiceComponent; 44 JobScheduler mJobScheduler; 45 46 @Override setUp()47 public void setUp() throws Exception { 48 super.setUp(); 49 kTestEnvironment.setUp(); 50 kTriggerTestEnvironment.setUp(); 51 kJobServiceComponent = new ComponentName(getContext(), MockJobService.class); 52 kTriggerContentServiceComponent = new ComponentName(getContext(), 53 TriggerContentJobService.class); 54 mJobScheduler = (JobScheduler) getContext().getSystemService(Context.JOB_SCHEDULER_SERVICE); 55 mJobScheduler.cancelAll(); 56 } 57 58 /** 59 * The scheduler will usually only flush its queue of unexpired jobs when the device is 60 * considered to be on stable power - that is, plugged in for a period of 2 minutes. 61 * Rather than wait for this to happen, we cheat and send this broadcast instead. 62 */ sendExpediteStableChargingBroadcast()63 protected void sendExpediteStableChargingBroadcast() { 64 getContext().sendBroadcast(EXPEDITE_STABLE_CHARGING); 65 } 66 } 67