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 17 package android.jobscheduler; 18 19 import android.annotation.TargetApi; 20 import android.app.job.JobParameters; 21 import android.app.job.JobService; 22 import android.util.Log; 23 24 import java.util.concurrent.CountDownLatch; 25 import java.util.concurrent.TimeUnit; 26 27 /** 28 * Handles callback from the framework {@link android.app.job.JobScheduler}. The behaviour of this 29 * class is configured through the static 30 * {@link TestEnvironment}. 31 */ 32 @TargetApi(21) 33 public class MockJobService extends JobService { 34 private static final String TAG = "MockJobService"; 35 36 /** Wait this long before timing out the test. */ 37 private static final long DEFAULT_TIMEOUT_MILLIS = 5000L; // 5 seconds. 38 39 @Override onCreate()40 public void onCreate() { 41 super.onCreate(); 42 Log.e(TAG, "Created test service."); 43 } 44 45 @Override onStartJob(JobParameters params)46 public boolean onStartJob(JobParameters params) { 47 Log.i(TAG, "Test job executing: " + params.getJobId()); 48 49 TestEnvironment.getTestEnvironment().notifyExecution(params.getJobId()); 50 return false; // No work to do. 51 } 52 53 @Override onStopJob(JobParameters params)54 public boolean onStopJob(JobParameters params) { 55 return false; 56 } 57 58 /** 59 * Configures the expected behaviour for each test. This object is shared across consecutive 60 * tests, so to clear state each test is responsible for calling 61 * {@link TestEnvironment#setUp()}. 62 */ 63 public static final class TestEnvironment { 64 65 private static TestEnvironment kTestEnvironment; 66 public static final int INVALID_JOB_ID = -1; 67 68 private CountDownLatch mLatch; 69 private int mExecutedJobId; 70 getTestEnvironment()71 public static TestEnvironment getTestEnvironment() { 72 if (kTestEnvironment == null) { 73 kTestEnvironment = new TestEnvironment(); 74 } 75 return kTestEnvironment; 76 } 77 78 /** 79 * Block the test thread, waiting on the JobScheduler to execute some previously scheduled 80 * job on this service. 81 */ awaitExecution()82 public boolean awaitExecution() throws InterruptedException { 83 final boolean executed = mLatch.await(DEFAULT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 84 return executed; 85 } 86 87 /** 88 * Block the test thread, expecting to timeout but still listening to ensure that no jobs 89 * land in the interim. 90 * @return True if the latch timed out waiting on an execution. 91 */ awaitTimeout()92 public boolean awaitTimeout() throws InterruptedException { 93 return !mLatch.await(DEFAULT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 94 } 95 notifyExecution(int jobId)96 private void notifyExecution(int jobId) { 97 Log.d(TAG, "Job executed:" + jobId); 98 mExecutedJobId = jobId; 99 mLatch.countDown(); 100 } 101 setExpectedExecutions(int numExecutions)102 public void setExpectedExecutions(int numExecutions) { 103 // For no executions expected, set count to 1 so we can still block for the timeout. 104 if (numExecutions == 0) { 105 mLatch = new CountDownLatch(1); 106 } else { 107 mLatch = new CountDownLatch(numExecutions); 108 } 109 } 110 111 /** Called in each testCase#setup */ setUp()112 public void setUp() { 113 mLatch = null; 114 mExecutedJobId = INVALID_JOB_ID; 115 } 116 117 } 118 }