1 /*
2  * Copyright (C) 2020 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 
17 package com.android.server.vibrator;
18 
19 import android.annotation.NonNull;
20 import android.frameworks.vibrator.IVibratorController;
21 import android.frameworks.vibrator.VibrationParam;
22 import android.os.Handler;
23 import android.os.IBinder;
24 import android.os.Looper;
25 import android.os.RemoteException;
26 import android.os.VibrationAttributes;
27 
28 /**
29  * Provides a fake implementation of {@link android.frameworks.vibrator.IVibratorController} for
30  * testing.
31  */
32 public final class FakeVibratorController extends IVibratorController.Stub {
33 
34     private final Handler mHandler;
35     private VibratorControlService mVibratorControlService;
36     private VibrationParam[] mRequestResult = new VibrationParam[0];
37 
38     public boolean isLinkedToDeath = false;
39     public boolean didRequestVibrationParams = false;
40     public int requestVibrationType = VibrationAttributes.USAGE_UNKNOWN;
41     public long requestTimeoutInMillis = 0;
42 
FakeVibratorController(Looper looper)43     public FakeVibratorController(Looper looper) {
44         mHandler = new Handler(looper);
45     }
46 
setVibratorControlService(VibratorControlService service)47     public void setVibratorControlService(VibratorControlService service) {
48         mVibratorControlService = service;
49     }
50 
setRequestResult(VibrationParam... params)51     public void setRequestResult(VibrationParam... params) {
52         mRequestResult = params;
53     }
54 
55     @Override
requestVibrationParams(int vibrationType, long timeoutInMillis, IBinder token)56     public void requestVibrationParams(int vibrationType, long timeoutInMillis, IBinder token)
57             throws RemoteException {
58         didRequestVibrationParams = true;
59         requestVibrationType = vibrationType;
60         requestTimeoutInMillis = timeoutInMillis;
61         mHandler.post(() -> {
62             if (mVibratorControlService != null) {
63                 mVibratorControlService.onRequestVibrationParamsComplete(token, mRequestResult);
64             }
65         });
66     }
67 
68     @Override
getInterfaceVersion()69     public int getInterfaceVersion() throws RemoteException {
70         return 0;
71     }
72 
73     @Override
getInterfaceHash()74     public String getInterfaceHash() throws RemoteException {
75         return null;
76     }
77 
78     @Override
linkToDeath(@onNull DeathRecipient recipient, int flags)79     public void linkToDeath(@NonNull DeathRecipient recipient, int flags) {
80         super.linkToDeath(recipient, flags);
81         isLinkedToDeath = true;
82     }
83 
84     @Override
unlinkToDeath(@onNull DeathRecipient recipient, int flags)85     public boolean unlinkToDeath(@NonNull DeathRecipient recipient, int flags) {
86         isLinkedToDeath = false;
87         return super.unlinkToDeath(recipient, flags);
88     }
89 }
90