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 android.car.test;
17
18import android.annotation.RequiresPermission;
19import android.annotation.SystemApi;
20import android.car.Car;
21import android.car.CarManagerBase;
22import android.os.IBinder;
23import android.os.RemoteException;
24
25/**
26 * API for testing only. Allows mocking vehicle hal.
27 * @hide
28 */
29@SystemApi
30public final class CarTestManager implements CarManagerBase {
31
32    private final ICarTest mService;
33
34
35    public CarTestManager(IBinder carServiceBinder) {
36        mService = ICarTest.Stub.asInterface(carServiceBinder);
37    }
38
39    @Override
40    public void onCarDisconnected() {
41        // should not happen for embedded
42    }
43
44    /**
45     * Releases all car services. This make sense for test purpose when it is necessary to reduce
46     * interference between testing and real instances of Car Service. For example changing audio
47     * focus in CarAudioService may affect framework's AudioManager listeners. AudioManager has a
48     * lot of complex logic which is hard to mock.
49     */
50    @RequiresPermission(Car.PERMISSION_CAR_TEST_SERVICE)
51    public void stopCarService(IBinder token) {
52        try {
53            mService.stopCarService(token);
54        } catch (RemoteException e) {
55            handleRemoteException(e);
56        }
57    }
58
59    /**
60     * Re-initializes previously released car service.
61     *
62     * @see {@link #stopCarService(IBinder)}
63     */
64    @RequiresPermission(Car.PERMISSION_CAR_TEST_SERVICE)
65    public void startCarService(IBinder token) {
66        try {
67            mService.startCarService(token);
68        } catch (RemoteException e) {
69            handleRemoteException(e);
70        }
71    }
72
73    private static void handleRemoteException(RemoteException e) {
74        // let test fail
75        throw new RuntimeException(e);
76    }
77}
78