MockedCarTestBase.java revision 1488ef2171bbc3bf25fc00c424a9979843f5ec6e
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.ServiceConnectionListener;
26import android.test.AndroidTestCase;
27import android.util.Log;
28
29import java.util.concurrent.Semaphore;
30import 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 */
37public 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, IBinder service) {
66            Log.i(TAG, "onServiceConnected, component" + name + " binder " + service);
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