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