MockedCarTestBase.java revision 9cea114837dd6a72d3a4f7f2618fec880fe1aca8
1/* 2 * Copyright (C) 2015 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 */ 16package com.android.car.test; 17 18import android.car.test.VehicleHalEmulator; 19import android.content.ComponentName; 20import android.content.ServiceConnection; 21import android.os.Handler; 22import android.os.IBinder; 23import android.os.Looper; 24import android.test.AndroidTestCase; 25import android.util.Log; 26 27import java.util.Arrays; 28import java.util.concurrent.Semaphore; 29import java.util.concurrent.TimeUnit; 30 31/** 32 * Base class for testing with mocked vehicle HAL (=car). 33 * It is up to each app to start emulation by getVehicleHalEmulator().start() as there will be 34 * per test set up that should be done before starting. 35 */ 36public class MockedCarTestBase extends AndroidTestCase { 37 private static final String TAG = MockedCarTestBase.class.getSimpleName(); 38 private static final long DEFAULT_WAIT_TIMEOUT_MS = 3000; 39 40 private android.car.Car mCar; 41 private VehicleHalEmulator mVehicleHalEmulator; 42 43 private final Semaphore mConnectionWaitForCar = new Semaphore(0); 44 private final Semaphore mWaitForMain = new Semaphore(0); 45 private final Handler mMainHandler = new Handler(Looper.getMainLooper()); 46 47 public static <T> void assertArrayEquals(T[] expected, T[] actual) { 48 if (!Arrays.equals(expected, actual)) { 49 fail("expected:<" + Arrays.toString(expected) + 50 "> but was:<" + Arrays.toString(actual) + ">"); 51 } 52 } 53 54 public static void assertArrayEquals(int[] expected, int[] actual) { 55 if (!Arrays.equals(expected, actual)) { 56 fail("expected:<" + Arrays.toString(expected) + 57 "> but was:<" + Arrays.toString(actual) + ">"); 58 } 59 } 60 61 @Override 62 protected synchronized void setUp() throws Exception { 63 super.setUp(); 64 mCar = android.car.Car.createCar(getContext(), new ServiceConnection() { 65 66 @Override 67 public void onServiceConnected(ComponentName name, IBinder service) { 68 mConnectionWaitForCar.release(); 69 } 70 71 @Override 72 public void onServiceDisconnected(ComponentName name) { 73 } 74 }); 75 mCar.connect(); 76 assertTrue(mConnectionWaitForCar.tryAcquire(DEFAULT_WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS) 77 ); 78 mVehicleHalEmulator = new VehicleHalEmulator(mCar); 79 } 80 81 @Override 82 protected synchronized void tearDown() throws Exception { 83 super.tearDown(); 84 if (mVehicleHalEmulator.isStarted()) { 85 mVehicleHalEmulator.stop(); 86 } 87 mCar.disconnect(); 88 } 89 90 protected synchronized android.car.Car getCar() { 91 return mCar; 92 } 93 94 protected void runOnMain(final Runnable r) { 95 mMainHandler.post(r); 96 } 97 98 protected void runOnMainSync(final Runnable r) throws Exception { 99 mMainHandler.post(new Runnable() { 100 @Override 101 public void run() { 102 r.run(); 103 mWaitForMain.release(); 104 } 105 }); 106 mWaitForMain.acquire(); 107 } 108 109 protected synchronized VehicleHalEmulator getVehicleHalEmulator() { 110 return mVehicleHalEmulator; 111 } 112} 113