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.test.AndroidTestCase; 26 27 import com.android.managedprovisioning.task.AbstractProvisioningTask; 28 29 import org.mockito.MockitoAnnotations; 30 31 import java.util.concurrent.ArrayBlockingQueue; 32 import java.util.concurrent.BlockingQueue; 33 import java.util.concurrent.TimeUnit; 34 35 /** 36 * Base class for provisioning controller tests. 37 */ 38 public abstract class ProvisioningControllerBaseTest extends AndroidTestCase { 39 40 private HandlerThread mHandlerThread; 41 protected FakeTaskHandler mHandler; 42 protected AbstractProvisioningController mController; 43 44 @Override setUp()45 public void setUp() throws Exception { 46 super.setUp(); 47 48 // this is necessary for mockito to work 49 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 50 MockitoAnnotations.initMocks(this); 51 mHandlerThread = new HandlerThread("TestHandler"); 52 mHandlerThread.start(); 53 mHandler = new FakeTaskHandler(mHandlerThread.getLooper()); 54 } 55 56 @Override tearDown()57 public void tearDown() throws Exception { 58 mHandlerThread.quitSafely(); 59 super.tearDown(); 60 } 61 taskSucceeded(Class expected)62 protected void taskSucceeded(Class expected) throws Exception { 63 AbstractProvisioningTask task = verifyTaskRun(expected); 64 // WHEN the task completes successfully 65 mController.onSuccess(task); 66 } 67 verifyTaskRun(Class expected)68 protected AbstractProvisioningTask verifyTaskRun(Class expected) throws Exception { 69 AbstractProvisioningTask task = mHandler.getLastTask(); 70 assertNotNull(task); 71 assertEquals(expected, task.getClass()); 72 return task; 73 } 74 75 protected class FakeTaskHandler extends Handler { 76 FakeTaskHandler(Looper looper)77 FakeTaskHandler(Looper looper) { 78 super(looper); 79 } 80 81 private BlockingQueue<AbstractProvisioningTask> mBlockingQueue 82 = new ArrayBlockingQueue<>(1); 83 getLastTask()84 public AbstractProvisioningTask getLastTask() throws Exception { 85 return mBlockingQueue.poll(10, TimeUnit.SECONDS); 86 } 87 handleMessage(Message msg)88 public void handleMessage(Message msg) { 89 if (msg.what == MSG_RUN_TASK) { 90 assertTrue(mBlockingQueue.add((AbstractProvisioningTask) msg.obj)); 91 } else { 92 fail("Unknown message " + msg.what); 93 } 94 } 95 } 96 } 97