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 = 30000L; // 30 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); 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 JobParameters mExecutedJobParameters; 70 getTestEnvironment()71 public static TestEnvironment getTestEnvironment() { 72 if (kTestEnvironment == null) { 73 kTestEnvironment = new TestEnvironment(); 74 } 75 return kTestEnvironment; 76 } 77 getLastJobParameters()78 public JobParameters getLastJobParameters() { 79 return mExecutedJobParameters; 80 } 81 82 /** 83 * Block the test thread, waiting on the JobScheduler to execute some previously scheduled 84 * job on this service. 85 */ awaitExecution()86 public boolean awaitExecution() throws InterruptedException { 87 final boolean executed = mLatch.await(DEFAULT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 88 return executed; 89 } 90 91 /** 92 * Block the test thread, expecting to timeout but still listening to ensure that no jobs 93 * land in the interim. 94 * @return True if the latch timed out waiting on an execution. 95 */ awaitTimeout()96 public boolean awaitTimeout() throws InterruptedException { 97 return !mLatch.await(DEFAULT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 98 } 99 notifyExecution(JobParameters params)100 private void notifyExecution(JobParameters params) { 101 Log.d(TAG, "Job executed:" + params.getJobId()); 102 mExecutedJobParameters = params; 103 mLatch.countDown(); 104 } 105 setExpectedExecutions(int numExecutions)106 public void setExpectedExecutions(int numExecutions) { 107 // For no executions expected, set count to 1 so we can still block for the timeout. 108 if (numExecutions == 0) { 109 mLatch = new CountDownLatch(1); 110 } else { 111 mLatch = new CountDownLatch(numExecutions); 112 } 113 } 114 115 /** Called in each testCase#setup */ setUp()116 public void setUp() { 117 mLatch = null; 118 mExecutedJobParameters = null; 119 } 120 121 } 122 }