MockedCarTestBase.java revision 57de61296cc8f29d8740fc7e6983af9553e7a410
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.support.car.Car; 25import android.support.car.ServiceConnectionCallback; 26import android.test.AndroidTestCase; 27import android.util.Log; 28 29import java.util.Arrays; 30import java.util.concurrent.Semaphore; 31import java.util.concurrent.TimeUnit; 32 33/** 34 * Base class for testing with mocked vehicle HAL (=car). 35 * It is up to each app to start emulation by getVehicleHalEmulator().start() as there will be 36 * per test set up that should be done before starting. 37 */ 38public class MockedCarTestBase extends AndroidTestCase { 39 private static final String TAG = MockedCarTestBase.class.getSimpleName(); 40 private static final long DEFAULT_WAIT_TIMEOUT_MS = 3000; 41 42 private Car mSupportCar; 43 private android.car.Car mCar; 44 private VehicleHalEmulator mVehicleHalEmulator; 45 46 private final Semaphore mConnectionWaitForSupportCar = new Semaphore(0); 47 private final Semaphore mConnectionWaitForCar = new Semaphore(0); 48 private final Semaphore mWaitForMain = new Semaphore(0); 49 private final Handler mMainHalder = new Handler(Looper.getMainLooper()); 50 51 private final ServiceConnectionCallback mConnectionCallbacks = new ServiceConnectionCallback() { 52 53 @Override 54 public void onServiceSuspended(int cause) { 55 } 56 57 @Override 58 public void onServiceDisconnected(ComponentName name) { 59 } 60 61 @Override 62 public void onServiceConnectionFailed(int cause) { 63 } 64 65 @Override 66 public void onServiceConnected(ComponentName name) { 67 Log.i(TAG, "onServiceConnected, component" + name); 68 mConnectionWaitForSupportCar.release(); 69 } 70 }; 71 72 public static <T> void assertArrayEquals(T[] expected, T[] actual) { 73 if (!Arrays.equals(expected, actual)) { 74 fail("expected:<" + Arrays.toString(expected) + 75 "> but was:<" + Arrays.toString(actual) + ">"); 76 } 77 } 78 79 public static void assertArrayEquals(int[] expected, int[] actual) { 80 if (!Arrays.equals(expected, actual)) { 81 fail("expected:<" + Arrays.toString(expected) + 82 "> but was:<" + Arrays.toString(actual) + ">"); 83 } 84 } 85 86 @Override 87 protected synchronized void setUp() throws Exception { 88 super.setUp(); 89 mSupportCar = Car.createCar(getContext(), mConnectionCallbacks); 90 mSupportCar.connect(); 91 mCar = android.car.Car.createCar(getContext(), new ServiceConnection() { 92 93 @Override 94 public void onServiceConnected(ComponentName name, IBinder service) { 95 mConnectionWaitForCar.release(); 96 } 97 98 @Override 99 public void onServiceDisconnected(ComponentName name) { 100 } 101 }); 102 mCar.connect(); 103 assertTrue(waitForConnection(DEFAULT_WAIT_TIMEOUT_MS)); 104 assertTrue(mConnectionWaitForCar.tryAcquire(DEFAULT_WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS) 105 ); 106 mVehicleHalEmulator = new VehicleHalEmulator(mCar); 107 } 108 109 @Override 110 protected synchronized void tearDown() throws Exception { 111 super.tearDown(); 112 if (mVehicleHalEmulator.isStarted()) { 113 mVehicleHalEmulator.stop(); 114 } 115 mSupportCar.disconnect(); 116 mCar.disconnect(); 117 } 118 119 protected synchronized Car getSupportCar() { 120 return mSupportCar; 121 } 122 123 protected synchronized android.car.Car getCar() { 124 return mCar; 125 } 126 127 protected void runOnMain(final Runnable r) { 128 mMainHalder.post(r); 129 } 130 131 protected void runOnMainSync(final Runnable r) throws Exception { 132 mMainHalder.post(new Runnable() { 133 @Override 134 public void run() { 135 r.run(); 136 mWaitForMain.release(); 137 } 138 }); 139 mWaitForMain.acquire(); 140 } 141 142 protected synchronized VehicleHalEmulator getVehicleHalEmulator() { 143 return mVehicleHalEmulator; 144 } 145 146 private boolean waitForConnection(long timeoutMs) throws InterruptedException { 147 return mConnectionWaitForSupportCar.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS); 148 } 149} 150