1 /* 2 * Copyright 2016, 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 com.android.managedprovisioning.provisioning; 18 19 import static com.android.managedprovisioning.provisioning.AbstractProvisioningController.MSG_RUN_TASK; 20 21 import android.os.Handler; 22 import android.os.HandlerThread; 23 import android.os.Looper; 24 import android.os.Message; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.test.AndroidTestCase; 28 29 import com.android.managedprovisioning.task.AbstractProvisioningTask; 30 import com.android.managedprovisioning.task.InstallExistingPackageTask; 31 import com.android.managedprovisioning.task.InstallPackageTask; 32 33 import org.mockito.MockitoAnnotations; 34 35 import java.util.concurrent.ArrayBlockingQueue; 36 import java.util.concurrent.BlockingQueue; 37 import java.util.concurrent.TimeUnit; 38 39 /** 40 * Base class for provisioning controller tests. 41 */ 42 public abstract class ProvisioningControllerBaseTest extends AndroidTestCase { 43 44 private HandlerThread mHandlerThread; 45 protected FakeTaskHandler mHandler; 46 protected AbstractProvisioningController mController; 47 48 @Override setUp()49 public void setUp() throws Exception { 50 super.setUp(); 51 52 // this is necessary for mockito to work 53 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 54 MockitoAnnotations.initMocks(this); 55 mHandlerThread = new HandlerThread("TestHandler"); 56 mHandlerThread.start(); 57 mHandler = new FakeTaskHandler(mHandlerThread.getLooper()); 58 } 59 60 @Override tearDown()61 public void tearDown() throws Exception { 62 mHandlerThread.quitSafely(); 63 super.tearDown(); 64 } 65 taskSucceeded(Class expected)66 protected void taskSucceeded(Class expected) throws Exception { 67 AbstractProvisioningTask task = verifyTaskRun(expected); 68 // WHEN the task completes successfully 69 mController.onSuccess(task); 70 } 71 tasksDownloadAndInstallDeviceOwnerPackageSucceeded(int userId)72 protected void tasksDownloadAndInstallDeviceOwnerPackageSucceeded(int userId) 73 throws Exception { 74 // the install package task should be run 75 taskSucceeded(InstallPackageTask.class); 76 77 // additional task for headless system user mode 78 if (UserManager.isHeadlessSystemUserMode() && userId != UserHandle.USER_SYSTEM) { 79 taskSucceeded(InstallExistingPackageTask.class); 80 } 81 } 82 verifyTaskRun(Class expected)83 protected AbstractProvisioningTask verifyTaskRun(Class expected) throws Exception { 84 AbstractProvisioningTask task = mHandler.getLastTask(); 85 assertNotNull(task); 86 assertEquals(expected, task.getClass()); 87 return task; 88 } 89 90 protected class FakeTaskHandler extends Handler { 91 FakeTaskHandler(Looper looper)92 FakeTaskHandler(Looper looper) { 93 super(looper); 94 } 95 96 private BlockingQueue<AbstractProvisioningTask> mBlockingQueue 97 = new ArrayBlockingQueue<>(1); 98 getLastTask()99 public AbstractProvisioningTask getLastTask() throws Exception { 100 return mBlockingQueue.poll(10, TimeUnit.SECONDS); 101 } 102 handleMessage(Message msg)103 public void handleMessage(Message msg) { 104 if (msg.what == MSG_RUN_TASK) { 105 assertTrue(mBlockingQueue.add((AbstractProvisioningTask) msg.obj)); 106 } else { 107 fail("Unknown message " + msg.what); 108 } 109 } 110 } 111 } 112