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