Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 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; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 20 | import android.app.job.JobInfo; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 21 | import android.app.job.JobParameters; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 22 | import android.app.job.JobScheduler; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 23 | import android.app.job.JobService; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 24 | import android.app.job.JobWorkItem; |
Dianne Hackborn | 6a38987 | 2017-03-30 14:06:33 -0700 | [diff] [blame] | 25 | import android.content.ClipData; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Dianne Hackborn | 6a38987 | 2017-03-30 14:06:33 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.content.pm.PackageManager; |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Dianne Hackborn | 6a38987 | 2017-03-30 14:06:33 -0700 | [diff] [blame] | 30 | import android.os.Process; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 31 | import android.util.Log; |
| 32 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 33 | import junit.framework.Assert; |
| 34 | |
| 35 | import java.util.ArrayList; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 36 | import java.util.concurrent.CountDownLatch; |
| 37 | import java.util.concurrent.TimeUnit; |
| 38 | |
| 39 | /** |
| 40 | * Handles callback from the framework {@link android.app.job.JobScheduler}. The behaviour of this |
| 41 | * class is configured through the static |
| 42 | * {@link TestEnvironment}. |
| 43 | */ |
| 44 | @TargetApi(21) |
| 45 | public class MockJobService extends JobService { |
| 46 | private static final String TAG = "MockJobService"; |
| 47 | |
| 48 | /** Wait this long before timing out the test. */ |
Christopher Tate | 459fffd | 2015-01-16 15:03:46 -0800 | [diff] [blame] | 49 | private static final long DEFAULT_TIMEOUT_MILLIS = 30000L; // 30 seconds. |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 50 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 51 | private JobParameters mParams; |
| 52 | |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 53 | ArrayList<JobWorkItem> mReceivedWork = new ArrayList<>(); |
| 54 | |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 55 | ArrayList<JobWorkItem> mPendingCompletions = new ArrayList<>(); |
| 56 | |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 57 | private boolean mWaitingForStop; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 58 | |
| 59 | @Override |
| 60 | public void onDestroy() { |
| 61 | super.onDestroy(); |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 62 | Log.i(TAG, "Destroying test service"); |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 63 | if (TestEnvironment.getTestEnvironment().getExpectedWork() != null) { |
| 64 | TestEnvironment.getTestEnvironment().notifyExecution(mParams, 0, 0, mReceivedWork, |
| 65 | null); |
| 66 | } |
| 67 | } |
| 68 | |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public void onCreate() { |
| 71 | super.onCreate(); |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 72 | Log.i(TAG, "Created test service."); |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean onStartJob(JobParameters params) { |
| 77 | Log.i(TAG, "Test job executing: " + params.getJobId()); |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 78 | mParams = params; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 79 | |
Dianne Hackborn | 6a38987 | 2017-03-30 14:06:33 -0700 | [diff] [blame] | 80 | int permCheckRead = PackageManager.PERMISSION_DENIED; |
| 81 | int permCheckWrite = PackageManager.PERMISSION_DENIED; |
| 82 | ClipData clip = params.getClipData(); |
| 83 | if (clip != null) { |
| 84 | permCheckRead = checkUriPermission(clip.getItemAt(0).getUri(), Process.myPid(), |
| 85 | Process.myUid(), Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 86 | permCheckWrite = checkUriPermission(clip.getItemAt(0).getUri(), Process.myPid(), |
| 87 | Process.myUid(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 88 | } |
| 89 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 90 | TestWorkItem[] expectedWork = TestEnvironment.getTestEnvironment().getExpectedWork(); |
| 91 | if (expectedWork != null) { |
| 92 | try { |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 93 | if (!TestEnvironment.getTestEnvironment().awaitDoWork()) { |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 94 | TestEnvironment.getTestEnvironment().notifyExecution(params, permCheckRead, |
| 95 | permCheckWrite, null, "Spent too long waiting to start executing work"); |
| 96 | return false; |
| 97 | } |
| 98 | } catch (InterruptedException e) { |
| 99 | TestEnvironment.getTestEnvironment().notifyExecution(params, permCheckRead, |
| 100 | permCheckWrite, null, "Failed waiting for work: " + e); |
| 101 | return false; |
| 102 | } |
| 103 | JobWorkItem work; |
| 104 | int index = 0; |
| 105 | while ((work = params.dequeueWork()) != null) { |
| 106 | Log.i(TAG, "Received work #" + index + ": " + work.getIntent()); |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 107 | mReceivedWork.add(work); |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 108 | |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 109 | int flags = 0; |
| 110 | |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 111 | if (index < expectedWork.length) { |
| 112 | TestWorkItem expected = expectedWork[index]; |
| 113 | int grantFlags = work.getIntent().getFlags(); |
| 114 | if (expected.requireUrisGranted != null) { |
| 115 | for (int ui = 0; ui < expected.requireUrisGranted.length; ui++) { |
| 116 | if ((grantFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 117 | if (checkUriPermission(expected.requireUrisGranted[ui], |
| 118 | Process.myPid(), Process.myUid(), |
| 119 | Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| 120 | != PackageManager.PERMISSION_GRANTED) { |
| 121 | TestEnvironment.getTestEnvironment().notifyExecution(params, |
| 122 | permCheckRead, permCheckWrite, null, |
| 123 | "Expected read permission but not granted: " |
| 124 | + expected.requireUrisGranted[ui] |
| 125 | + " @ #" + index); |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | if ((grantFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 130 | if (checkUriPermission(expected.requireUrisGranted[ui], |
| 131 | Process.myPid(), Process.myUid(), |
| 132 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| 133 | != PackageManager.PERMISSION_GRANTED) { |
| 134 | TestEnvironment.getTestEnvironment().notifyExecution(params, |
| 135 | permCheckRead, permCheckWrite, null, |
| 136 | "Expected write permission but not granted: " |
| 137 | + expected.requireUrisGranted[ui] |
| 138 | + " @ #" + index); |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | if (expected.requireUrisNotGranted != null) { |
| 145 | // XXX note no delay here, current impl will have fully revoked the |
| 146 | // permission by the time we return from completing the last work. |
| 147 | for (int ui = 0; ui < expected.requireUrisNotGranted.length; ui++) { |
| 148 | if ((grantFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 149 | if (checkUriPermission(expected.requireUrisNotGranted[ui], |
| 150 | Process.myPid(), Process.myUid(), |
| 151 | Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| 152 | != PackageManager.PERMISSION_DENIED) { |
| 153 | TestEnvironment.getTestEnvironment().notifyExecution(params, |
| 154 | permCheckRead, permCheckWrite, null, |
| 155 | "Not expected read permission but granted: " |
| 156 | + expected.requireUrisNotGranted[ui] |
| 157 | + " @ #" + index); |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | if ((grantFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 162 | if (checkUriPermission(expected.requireUrisNotGranted[ui], |
| 163 | Process.myPid(), Process.myUid(), |
| 164 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| 165 | != PackageManager.PERMISSION_DENIED) { |
| 166 | TestEnvironment.getTestEnvironment().notifyExecution(params, |
| 167 | permCheckRead, permCheckWrite, null, |
| 168 | "Not expected write permission but granted: " |
| 169 | + expected.requireUrisNotGranted[ui] |
| 170 | + " @ #" + index); |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | } |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 175 | } |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 176 | |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 177 | flags = expected.flags; |
| 178 | |
| 179 | if ((flags & TestWorkItem.FLAG_WAIT_FOR_STOP) != 0) { |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 180 | Log.i(TAG, "Now waiting to stop"); |
| 181 | mWaitingForStop = true; |
| 182 | TestEnvironment.getTestEnvironment().notifyWaitingForStop(); |
| 183 | return true; |
| 184 | } |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 185 | |
| 186 | if ((flags & TestWorkItem.FLAG_COMPLETE_NEXT) != 0) { |
| 187 | if (!processNextPendingCompletion()) { |
| 188 | TestEnvironment.getTestEnvironment().notifyExecution(params, |
| 189 | 0, 0, null, |
| 190 | "Expected to complete next pending work but there was none: " |
| 191 | + " @ #" + index); |
| 192 | return false; |
| 193 | } |
| 194 | } |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 195 | } |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 196 | |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 197 | if ((flags & TestWorkItem.FLAG_DELAY_COMPLETE_PUSH_BACK) != 0) { |
| 198 | mPendingCompletions.add(work); |
| 199 | } else if ((flags & TestWorkItem.FLAG_DELAY_COMPLETE_PUSH_TOP) != 0) { |
| 200 | mPendingCompletions.add(0, work); |
| 201 | } else { |
| 202 | mParams.completeWork(work); |
| 203 | } |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 204 | |
| 205 | if (index < expectedWork.length) { |
| 206 | TestWorkItem expected = expectedWork[index]; |
| 207 | if (expected.subitems != null) { |
| 208 | final TestWorkItem[] sub = expected.subitems; |
| 209 | final JobInfo ji = expected.jobInfo; |
| 210 | final JobScheduler js = (JobScheduler) getSystemService( |
| 211 | Context.JOB_SCHEDULER_SERVICE); |
| 212 | for (int subi = 0; subi < sub.length; subi++) { |
| 213 | js.enqueue(ji, new JobWorkItem(sub[subi].intent)); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | index++; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 219 | } |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 220 | |
| 221 | if (processNextPendingCompletion()) { |
| 222 | // We had some pending completions, clean them all out... |
| 223 | while (processNextPendingCompletion()) { |
| 224 | } |
| 225 | // ...and we need to do a final dequeue to complete the job, which should not |
| 226 | // return any remaining work. |
| 227 | if ((work = params.dequeueWork()) != null) { |
| 228 | TestEnvironment.getTestEnvironment().notifyExecution(params, |
| 229 | 0, 0, null, |
| 230 | "Expected no remaining work after dequeue pending, but got: " + work); |
| 231 | } |
| 232 | } |
| 233 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 234 | Log.i(TAG, "Done with all work at #" + index); |
| 235 | // We don't notifyExecution here because we want to make sure the job properly |
| 236 | // stops itself. |
| 237 | return true; |
| 238 | } else { |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 239 | boolean continueAfterStart |
| 240 | = TestEnvironment.getTestEnvironment().handleContinueAfterStart(); |
| 241 | try { |
| 242 | if (!TestEnvironment.getTestEnvironment().awaitDoJob()) { |
| 243 | TestEnvironment.getTestEnvironment().notifyExecution(params, permCheckRead, |
| 244 | permCheckWrite, null, "Spent too long waiting to start job"); |
| 245 | return false; |
| 246 | } |
| 247 | } catch (InterruptedException e) { |
| 248 | TestEnvironment.getTestEnvironment().notifyExecution(params, permCheckRead, |
| 249 | permCheckWrite, null, "Failed waiting to start job: " + e); |
| 250 | return false; |
| 251 | } |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 252 | TestEnvironment.getTestEnvironment().notifyExecution(params, permCheckRead, |
| 253 | permCheckWrite, null, null); |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 254 | return continueAfterStart; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 255 | } |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 258 | boolean processNextPendingCompletion() { |
| 259 | if (mPendingCompletions.size() <= 0) { |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | JobWorkItem next = mPendingCompletions.remove(0); |
| 264 | mParams.completeWork(next); |
| 265 | return true; |
| 266 | } |
| 267 | |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 268 | @Override |
| 269 | public boolean onStopJob(JobParameters params) { |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 270 | Log.i(TAG, "Received stop callback"); |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 271 | TestEnvironment.getTestEnvironment().notifyStopped(); |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 272 | return mWaitingForStop; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 275 | public static final class TestWorkItem { |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 276 | /** |
| 277 | * Stop processing work for now, waiting for the service to be stopped. |
| 278 | */ |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 279 | public static final int FLAG_WAIT_FOR_STOP = 1<<0; |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Don't complete this work now, instead push it on the back of the stack of |
| 282 | * pending completions. |
| 283 | */ |
| 284 | public static final int FLAG_DELAY_COMPLETE_PUSH_BACK = 1<<1; |
| 285 | /** |
| 286 | * Don't complete this work now, instead insert to the top of the stack of |
| 287 | * pending completions. |
| 288 | */ |
| 289 | public static final int FLAG_DELAY_COMPLETE_PUSH_TOP = 1<<2; |
| 290 | /** |
| 291 | * Complete next pending completion on the stack before completing this one. |
| 292 | */ |
| 293 | public static final int FLAG_COMPLETE_NEXT = 1<<3; |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 294 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 295 | public final Intent intent; |
| 296 | public final JobInfo jobInfo; |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 297 | public final int flags; |
| 298 | public final int deliveryCount; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 299 | public final TestWorkItem[] subitems; |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 300 | public final Uri[] requireUrisGranted; |
| 301 | public final Uri[] requireUrisNotGranted; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 302 | |
| 303 | public TestWorkItem(Intent _intent) { |
| 304 | intent = _intent; |
| 305 | jobInfo = null; |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 306 | flags = 0; |
| 307 | deliveryCount = 1; |
| 308 | subitems = null; |
| 309 | requireUrisGranted = null; |
| 310 | requireUrisNotGranted = null; |
| 311 | } |
| 312 | |
Dianne Hackborn | 02856c8 | 2017-10-13 17:02:04 -0700 | [diff] [blame] | 313 | public TestWorkItem(Intent _intent, int _flags) { |
| 314 | intent = _intent; |
| 315 | jobInfo = null; |
| 316 | flags = _flags; |
| 317 | deliveryCount = 1; |
| 318 | subitems = null; |
| 319 | requireUrisGranted = null; |
| 320 | requireUrisNotGranted = null; |
| 321 | } |
| 322 | |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 323 | public TestWorkItem(Intent _intent, int _flags, int _deliveryCount) { |
| 324 | intent = _intent; |
| 325 | jobInfo = null; |
| 326 | flags = _flags; |
| 327 | deliveryCount = _deliveryCount; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 328 | subitems = null; |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 329 | requireUrisGranted = null; |
| 330 | requireUrisNotGranted = null; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | public TestWorkItem(Intent _intent, JobInfo _jobInfo, TestWorkItem[] _subitems) { |
| 334 | intent = _intent; |
| 335 | jobInfo = _jobInfo; |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 336 | flags = 0; |
| 337 | deliveryCount = 1; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 338 | subitems = _subitems; |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 339 | requireUrisGranted = null; |
| 340 | requireUrisNotGranted = null; |
| 341 | } |
| 342 | |
| 343 | public TestWorkItem(Intent _intent, Uri[] _requireUrisGranted, |
| 344 | Uri[] _requireUrisNotGranted) { |
| 345 | intent = _intent; |
| 346 | jobInfo = null; |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 347 | flags = 0; |
| 348 | deliveryCount = 1; |
Dianne Hackborn | 9bfd0bf | 2017-04-13 18:07:53 -0700 | [diff] [blame] | 349 | subitems = null; |
| 350 | requireUrisGranted = _requireUrisGranted; |
| 351 | requireUrisNotGranted = _requireUrisNotGranted; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 352 | } |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 353 | |
| 354 | @Override |
| 355 | public String toString() { |
| 356 | return "TestWorkItem { " + intent + " dc=" + deliveryCount + " }"; |
| 357 | } |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 360 | /** |
| 361 | * Configures the expected behaviour for each test. This object is shared across consecutive |
| 362 | * tests, so to clear state each test is responsible for calling |
| 363 | * {@link TestEnvironment#setUp()}. |
| 364 | */ |
| 365 | public static final class TestEnvironment { |
| 366 | |
| 367 | private static TestEnvironment kTestEnvironment; |
Matthew Williams | be8620e | 2015-05-14 17:49:50 -0700 | [diff] [blame] | 368 | //public static final int INVALID_JOB_ID = -1; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 369 | |
| 370 | private CountDownLatch mLatch; |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 371 | private CountDownLatch mWaitingForStopLatch; |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 372 | private CountDownLatch mDoJobLatch; |
| 373 | private CountDownLatch mStoppedLatch; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 374 | private CountDownLatch mDoWorkLatch; |
| 375 | private TestWorkItem[] mExpectedWork; |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 376 | private boolean mContinueAfterStart; |
Matthew Williams | be8620e | 2015-05-14 17:49:50 -0700 | [diff] [blame] | 377 | private JobParameters mExecutedJobParameters; |
Dianne Hackborn | 6a38987 | 2017-03-30 14:06:33 -0700 | [diff] [blame] | 378 | private int mExecutedPermCheckRead; |
| 379 | private int mExecutedPermCheckWrite; |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 380 | private ArrayList<JobWorkItem> mExecutedReceivedWork; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 381 | private String mExecutedErrorMessage; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 382 | |
| 383 | public static TestEnvironment getTestEnvironment() { |
| 384 | if (kTestEnvironment == null) { |
| 385 | kTestEnvironment = new TestEnvironment(); |
| 386 | } |
| 387 | return kTestEnvironment; |
| 388 | } |
| 389 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 390 | public TestWorkItem[] getExpectedWork() { |
| 391 | return mExpectedWork; |
| 392 | } |
| 393 | |
Matthew Williams | be8620e | 2015-05-14 17:49:50 -0700 | [diff] [blame] | 394 | public JobParameters getLastJobParameters() { |
| 395 | return mExecutedJobParameters; |
| 396 | } |
| 397 | |
Dianne Hackborn | 6a38987 | 2017-03-30 14:06:33 -0700 | [diff] [blame] | 398 | public int getLastPermCheckRead() { |
| 399 | return mExecutedPermCheckRead; |
| 400 | } |
| 401 | |
| 402 | public int getLastPermCheckWrite() { |
| 403 | return mExecutedPermCheckWrite; |
| 404 | } |
| 405 | |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 406 | public ArrayList<JobWorkItem> getLastReceivedWork() { |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 407 | return mExecutedReceivedWork; |
| 408 | } |
| 409 | |
| 410 | public String getLastErrorMessage() { |
| 411 | return mExecutedErrorMessage; |
| 412 | } |
| 413 | |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 414 | /** |
| 415 | * Block the test thread, waiting on the JobScheduler to execute some previously scheduled |
| 416 | * job on this service. |
| 417 | */ |
| 418 | public boolean awaitExecution() throws InterruptedException { |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 419 | return awaitExecution(DEFAULT_TIMEOUT_MILLIS); |
| 420 | } |
| 421 | |
| 422 | public boolean awaitExecution(long timeoutMillis) throws InterruptedException { |
| 423 | final boolean executed = mLatch.await(timeoutMillis, TimeUnit.MILLISECONDS); |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 424 | if (getLastErrorMessage() != null) { |
| 425 | Assert.fail(getLastErrorMessage()); |
| 426 | } |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 427 | return executed; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Block the test thread, expecting to timeout but still listening to ensure that no jobs |
| 432 | * land in the interim. |
| 433 | * @return True if the latch timed out waiting on an execution. |
| 434 | */ |
| 435 | public boolean awaitTimeout() throws InterruptedException { |
Christopher Tate | e5dd31a | 2017-06-13 18:32:28 -0700 | [diff] [blame] | 436 | return awaitTimeout(DEFAULT_TIMEOUT_MILLIS); |
| 437 | } |
| 438 | |
| 439 | public boolean awaitTimeout(long timeoutMillis) throws InterruptedException { |
| 440 | return !mLatch.await(timeoutMillis, TimeUnit.MILLISECONDS); |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 443 | public boolean awaitWaitingForStop() throws InterruptedException { |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 444 | return mWaitingForStopLatch.await(DEFAULT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 447 | public boolean awaitDoWork() throws InterruptedException { |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 448 | return mDoWorkLatch.await(DEFAULT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 449 | } |
| 450 | |
| 451 | public boolean awaitDoJob() throws InterruptedException { |
| 452 | if (mDoJobLatch == null) { |
| 453 | return true; |
| 454 | } |
| 455 | return mDoJobLatch.await(DEFAULT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 456 | } |
| 457 | |
| 458 | public boolean awaitStopped() throws InterruptedException { |
| 459 | return mStoppedLatch.await(DEFAULT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | private void notifyExecution(JobParameters params, int permCheckRead, int permCheckWrite, |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 463 | ArrayList<JobWorkItem> receivedWork, String errorMsg) { |
Dianne Hackborn | 6a38987 | 2017-03-30 14:06:33 -0700 | [diff] [blame] | 464 | //Log.d(TAG, "Job executed:" + params.getJobId()); |
Matthew Williams | be8620e | 2015-05-14 17:49:50 -0700 | [diff] [blame] | 465 | mExecutedJobParameters = params; |
Dianne Hackborn | 6a38987 | 2017-03-30 14:06:33 -0700 | [diff] [blame] | 466 | mExecutedPermCheckRead = permCheckRead; |
| 467 | mExecutedPermCheckWrite = permCheckWrite; |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 468 | mExecutedReceivedWork = receivedWork; |
| 469 | mExecutedErrorMessage = errorMsg; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 470 | mLatch.countDown(); |
| 471 | } |
| 472 | |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 473 | private void notifyWaitingForStop() { |
| 474 | mWaitingForStopLatch.countDown(); |
| 475 | } |
| 476 | |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 477 | private void notifyStopped() { |
| 478 | if (mStoppedLatch != null) { |
| 479 | mStoppedLatch.countDown(); |
| 480 | } |
| 481 | } |
| 482 | |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 483 | public void setExpectedExecutions(int numExecutions) { |
| 484 | // For no executions expected, set count to 1 so we can still block for the timeout. |
| 485 | if (numExecutions == 0) { |
| 486 | mLatch = new CountDownLatch(1); |
| 487 | } else { |
| 488 | mLatch = new CountDownLatch(numExecutions); |
| 489 | } |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 490 | mWaitingForStopLatch = null; |
| 491 | mDoJobLatch = null; |
| 492 | mStoppedLatch = null; |
| 493 | mDoWorkLatch = null; |
| 494 | mExpectedWork = null; |
| 495 | mContinueAfterStart = false; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Dianne Hackborn | e686683 | 2017-04-26 14:08:32 -0700 | [diff] [blame] | 498 | public void setExpectedWaitForStop() { |
| 499 | mWaitingForStopLatch = new CountDownLatch(1); |
| 500 | } |
| 501 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 502 | public void setExpectedWork(TestWorkItem[] work) { |
| 503 | mExpectedWork = work; |
| 504 | mDoWorkLatch = new CountDownLatch(1); |
| 505 | } |
| 506 | |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 507 | public void setExpectedStopped() { |
| 508 | mStoppedLatch = new CountDownLatch(1); |
| 509 | } |
| 510 | |
Dianne Hackborn | 555b9c7 | 2017-04-10 15:18:05 -0700 | [diff] [blame] | 511 | public void readyToWork() { |
| 512 | mDoWorkLatch.countDown(); |
| 513 | } |
| 514 | |
Dianne Hackborn | 3338e9a5 | 2017-05-16 13:18:34 -0700 | [diff] [blame] | 515 | public void setExpectedWaitForRun() { |
| 516 | mDoJobLatch = new CountDownLatch(1); |
| 517 | } |
| 518 | |
| 519 | public void readyToRun() { |
| 520 | mDoJobLatch.countDown(); |
| 521 | } |
| 522 | |
| 523 | public void setContinueAfterStart() { |
| 524 | mContinueAfterStart = true; |
| 525 | } |
| 526 | |
| 527 | public boolean handleContinueAfterStart() { |
| 528 | boolean res = mContinueAfterStart; |
| 529 | mContinueAfterStart = false; |
| 530 | return res; |
| 531 | } |
| 532 | |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 533 | /** Called in each testCase#setup */ |
| 534 | public void setUp() { |
| 535 | mLatch = null; |
Matthew Williams | be8620e | 2015-05-14 17:49:50 -0700 | [diff] [blame] | 536 | mExecutedJobParameters = null; |
Matthew Williams | 547b816 | 2014-10-15 10:18:11 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | } |
| 540 | } |