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 #define LOG_TAG "VibratorManagerHalWrapperAidlTest"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <utils/Log.h>
23 
24 #include <vibratorservice/VibratorManagerHalWrapper.h>
25 
26 #include "test_utils.h"
27 
28 using android::binder::Status;
29 
30 using android::hardware::vibrator::Braking;
31 using android::hardware::vibrator::CompositeEffect;
32 using android::hardware::vibrator::CompositePrimitive;
33 using android::hardware::vibrator::Effect;
34 using android::hardware::vibrator::EffectStrength;
35 using android::hardware::vibrator::IVibrator;
36 using android::hardware::vibrator::IVibratorCallback;
37 using android::hardware::vibrator::IVibratorManager;
38 using android::hardware::vibrator::PrimitivePwle;
39 
40 using namespace android;
41 using namespace testing;
42 
__anon8f2f9ef50102(vibrator::HalWrapper* hal) 43 static const auto OFF_FN = [](vibrator::HalWrapper* hal) { return hal->off(); };
44 
45 class MockBinder : public BBinder {
46 public:
47     MOCK_METHOD(status_t, linkToDeath,
48                 (const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags), (override));
49     MOCK_METHOD(status_t, unlinkToDeath,
50                 (const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
51                  wp<DeathRecipient>* outRecipient),
52                 (override));
53     MOCK_METHOD(status_t, pingBinder, (), (override));
54 };
55 
56 class MockIVibrator : public IVibrator {
57 public:
58     MOCK_METHOD(Status, getCapabilities, (int32_t * ret), (override));
59     MOCK_METHOD(Status, off, (), (override));
60     MOCK_METHOD(Status, on, (int32_t timeout, const sp<IVibratorCallback>& cb), (override));
61     MOCK_METHOD(Status, perform,
62                 (Effect e, EffectStrength s, const sp<IVibratorCallback>& cb, int32_t* ret),
63                 (override));
64     MOCK_METHOD(Status, getSupportedEffects, (std::vector<Effect> * ret), (override));
65     MOCK_METHOD(Status, setAmplitude, (float amplitude), (override));
66     MOCK_METHOD(Status, setExternalControl, (bool enabled), (override));
67     MOCK_METHOD(Status, getCompositionDelayMax, (int32_t * ret), (override));
68     MOCK_METHOD(Status, getCompositionSizeMax, (int32_t * ret), (override));
69     MOCK_METHOD(Status, getSupportedPrimitives, (std::vector<CompositePrimitive> * ret),
70                 (override));
71     MOCK_METHOD(Status, getPrimitiveDuration, (CompositePrimitive p, int32_t* ret), (override));
72     MOCK_METHOD(Status, compose,
73                 (const std::vector<CompositeEffect>& e, const sp<IVibratorCallback>& cb),
74                 (override));
75     MOCK_METHOD(Status, composePwle,
76                 (const std::vector<PrimitivePwle>& e, const sp<IVibratorCallback>& cb), (override));
77     MOCK_METHOD(Status, getSupportedAlwaysOnEffects, (std::vector<Effect> * ret), (override));
78     MOCK_METHOD(Status, alwaysOnEnable, (int32_t id, Effect e, EffectStrength s), (override));
79     MOCK_METHOD(Status, alwaysOnDisable, (int32_t id), (override));
80     MOCK_METHOD(Status, getQFactor, (float * ret), (override));
81     MOCK_METHOD(Status, getResonantFrequency, (float * ret), (override));
82     MOCK_METHOD(Status, getFrequencyResolution, (float* ret), (override));
83     MOCK_METHOD(Status, getFrequencyMinimum, (float* ret), (override));
84     MOCK_METHOD(Status, getBandwidthAmplitudeMap, (std::vector<float> * ret), (override));
85     MOCK_METHOD(Status, getPwlePrimitiveDurationMax, (int32_t * ret), (override));
86     MOCK_METHOD(Status, getPwleCompositionSizeMax, (int32_t * ret), (override));
87     MOCK_METHOD(Status, getSupportedBraking, (std::vector<Braking> * ret), (override));
88     MOCK_METHOD(int32_t, getInterfaceVersion, (), (override));
89     MOCK_METHOD(std::string, getInterfaceHash, (), (override));
90     MOCK_METHOD(IBinder*, onAsBinder, (), (override));
91 };
92 
93 class MockIVibratorManager : public IVibratorManager {
94 public:
95     MOCK_METHOD(Status, getCapabilities, (int32_t * ret), (override));
96     MOCK_METHOD(Status, getVibratorIds, (std::vector<int32_t> * ret), (override));
97     MOCK_METHOD(Status, getVibrator, (int32_t id, sp<IVibrator>* ret), (override));
98     MOCK_METHOD(Status, prepareSynced, (const std::vector<int32_t>& ids), (override));
99     MOCK_METHOD(Status, triggerSynced, (const sp<IVibratorCallback>& cb), (override));
100     MOCK_METHOD(Status, cancelSynced, (), (override));
101     MOCK_METHOD(int32_t, getInterfaceVersion, (), (override));
102     MOCK_METHOD(std::string, getInterfaceHash, (), (override));
103     MOCK_METHOD(IBinder*, onAsBinder, (), (override));
104 };
105 
106 // -------------------------------------------------------------------------------------------------
107 
108 class VibratorManagerHalWrapperAidlTest : public Test {
109 public:
SetUp()110     void SetUp() override {
111         mMockBinder = new StrictMock<MockBinder>();
112         mMockVibrator = new StrictMock<MockIVibrator>();
113         mMockHal = new StrictMock<MockIVibratorManager>();
114         mMockScheduler = std::make_shared<StrictMock<vibrator::MockCallbackScheduler>>();
115         mWrapper = std::make_unique<vibrator::AidlManagerHalWrapper>(mMockScheduler, mMockHal);
116         ASSERT_NE(mWrapper, nullptr);
117     }
118 
119 protected:
120     std::shared_ptr<StrictMock<vibrator::MockCallbackScheduler>> mMockScheduler = nullptr;
121     std::unique_ptr<vibrator::ManagerHalWrapper> mWrapper = nullptr;
122     sp<StrictMock<MockIVibratorManager>> mMockHal = nullptr;
123     sp<StrictMock<MockIVibrator>> mMockVibrator = nullptr;
124     sp<StrictMock<MockBinder>> mMockBinder = nullptr;
125 };
126 
127 // -------------------------------------------------------------------------------------------------
128 
129 static const std::vector<int32_t> kVibratorIds = {1, 2};
130 static constexpr int kVibratorId = 1;
131 
ACTION(TriggerCallback)132 ACTION(TriggerCallback) {
133     if (arg0 != nullptr) {
134         arg0->onComplete();
135     }
136 }
137 
TEST_F(VibratorManagerHalWrapperAidlTest,TestPing)138 TEST_F(VibratorManagerHalWrapperAidlTest, TestPing) {
139     EXPECT_CALL(*mMockHal.get(), onAsBinder())
140             .Times(Exactly(2))
141             .WillRepeatedly(Return(mMockBinder.get()));
142     EXPECT_CALL(*mMockBinder.get(), pingBinder())
143             .Times(Exactly(2))
144             .WillOnce(Return(android::OK))
145             .WillRepeatedly(Return(android::DEAD_OBJECT));
146 
147     ASSERT_TRUE(mWrapper->ping().isOk());
148     ASSERT_TRUE(mWrapper->ping().isFailed());
149 }
150 
TEST_F(VibratorManagerHalWrapperAidlTest,TestGetCapabilitiesDoesNotCacheFailedResult)151 TEST_F(VibratorManagerHalWrapperAidlTest, TestGetCapabilitiesDoesNotCacheFailedResult) {
152     EXPECT_CALL(*mMockHal.get(), getCapabilities(_))
153             .Times(Exactly(3))
154             .WillOnce(
155                     Return(Status::fromExceptionCode(Status::Exception::EX_UNSUPPORTED_OPERATION)))
156             .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
157             .WillRepeatedly(DoAll(SetArgPointee<0>(IVibratorManager::CAP_SYNC), Return(Status())));
158 
159     ASSERT_TRUE(mWrapper->getCapabilities().isUnsupported());
160     ASSERT_TRUE(mWrapper->getCapabilities().isFailed());
161 
162     auto result = mWrapper->getCapabilities();
163     ASSERT_TRUE(result.isOk());
164     ASSERT_EQ(vibrator::ManagerCapabilities::SYNC, result.value());
165 }
166 
TEST_F(VibratorManagerHalWrapperAidlTest,TestGetCapabilitiesCachesResult)167 TEST_F(VibratorManagerHalWrapperAidlTest, TestGetCapabilitiesCachesResult) {
168     EXPECT_CALL(*mMockHal.get(), getCapabilities(_))
169             .Times(Exactly(1))
170             .WillRepeatedly(DoAll(SetArgPointee<0>(IVibratorManager::CAP_SYNC), Return(Status())));
171 
172     std::vector<std::thread> threads;
173     for (int i = 0; i < 10; i++) {
174         threads.push_back(std::thread([&]() {
175             auto result = mWrapper->getCapabilities();
176             ASSERT_TRUE(result.isOk());
177             ASSERT_EQ(vibrator::ManagerCapabilities::SYNC, result.value());
178         }));
179     }
180     std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
181 
182     auto result = mWrapper->getCapabilities();
183     ASSERT_TRUE(result.isOk());
184     ASSERT_EQ(vibrator::ManagerCapabilities::SYNC, result.value());
185 }
186 
TEST_F(VibratorManagerHalWrapperAidlTest,TestGetVibratorIdsDoesNotCacheFailedResult)187 TEST_F(VibratorManagerHalWrapperAidlTest, TestGetVibratorIdsDoesNotCacheFailedResult) {
188     EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
189             .Times(Exactly(3))
190             .WillOnce(
191                     Return(Status::fromExceptionCode(Status::Exception::EX_UNSUPPORTED_OPERATION)))
192             .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
193             .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
194 
195     ASSERT_TRUE(mWrapper->getVibratorIds().isUnsupported());
196     ASSERT_TRUE(mWrapper->getVibratorIds().isFailed());
197 
198     auto result = mWrapper->getVibratorIds();
199     ASSERT_TRUE(result.isOk());
200     ASSERT_EQ(kVibratorIds, result.value());
201 }
202 
TEST_F(VibratorManagerHalWrapperAidlTest,TestGetVibratorIdsCachesResult)203 TEST_F(VibratorManagerHalWrapperAidlTest, TestGetVibratorIdsCachesResult) {
204     EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
205             .Times(Exactly(1))
206             .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
207 
208     std::vector<std::thread> threads;
209     for (int i = 0; i < 10; i++) {
210         threads.push_back(std::thread([&]() {
211             auto result = mWrapper->getVibratorIds();
212             ASSERT_TRUE(result.isOk());
213             ASSERT_EQ(kVibratorIds, result.value());
214         }));
215     }
216     std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
217 
218     auto result = mWrapper->getVibratorIds();
219     ASSERT_TRUE(result.isOk());
220     ASSERT_EQ(kVibratorIds, result.value());
221 }
222 
TEST_F(VibratorManagerHalWrapperAidlTest,TestGetVibratorWithValidIdReturnsController)223 TEST_F(VibratorManagerHalWrapperAidlTest, TestGetVibratorWithValidIdReturnsController) {
224     {
225         InSequence seq;
226         EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
227                 .Times(Exactly(1))
228                 .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
229 
230         EXPECT_CALL(*mMockHal.get(), getVibrator(Eq(kVibratorId), _))
231                 .Times(Exactly(1))
232                 .WillRepeatedly(DoAll(SetArgPointee<1>(mMockVibrator), Return(Status())));
233     }
234 
235     auto result = mWrapper->getVibrator(kVibratorId);
236     ASSERT_TRUE(result.isOk());
237     ASSERT_NE(nullptr, result.value().get());
238     ASSERT_TRUE(result.value().get()->init());
239 }
240 
TEST_F(VibratorManagerHalWrapperAidlTest,TestGetVibratorWithInvalidIdFails)241 TEST_F(VibratorManagerHalWrapperAidlTest, TestGetVibratorWithInvalidIdFails) {
242     EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
243             .Times(Exactly(1))
244             .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
245 
246     ASSERT_TRUE(mWrapper->getVibrator(0).isFailed());
247 }
248 
TEST_F(VibratorManagerHalWrapperAidlTest,TestGetVibratorRecoversVibratorPointer)249 TEST_F(VibratorManagerHalWrapperAidlTest, TestGetVibratorRecoversVibratorPointer) {
250     EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
251             .Times(Exactly(1))
252             .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
253 
254     EXPECT_CALL(*mMockHal.get(), getVibrator(Eq(kVibratorId), _))
255             .Times(Exactly(3))
256             .WillOnce(DoAll(SetArgPointee<1>(nullptr),
257                             Return(Status::fromExceptionCode(
258                                     Status::Exception::EX_TRANSACTION_FAILED))))
259             .WillRepeatedly(DoAll(SetArgPointee<1>(mMockVibrator), Return(Status())));
260 
261     EXPECT_CALL(*mMockVibrator.get(), off())
262             .Times(Exactly(3))
263             .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_TRANSACTION_FAILED)))
264             .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_TRANSACTION_FAILED)))
265             .WillRepeatedly(Return(Status()));
266 
267     // Get vibrator controller is successful even if first getVibrator.
268     auto result = mWrapper->getVibrator(kVibratorId);
269     ASSERT_TRUE(result.isOk());
270     ASSERT_NE(nullptr, result.value().get());
271 
272     auto vibrator = result.value();
273     // First getVibrator call fails.
274     ASSERT_FALSE(vibrator->init());
275     // First and second off() calls fail, reload IVibrator with getVibrator.
276     ASSERT_TRUE(vibrator->doWithRetry<void>(OFF_FN, "off").isFailed());
277     // Third call to off() worked after IVibrator reloaded.
278     ASSERT_TRUE(vibrator->doWithRetry<void>(OFF_FN, "off").isOk());
279 }
280 
TEST_F(VibratorManagerHalWrapperAidlTest,TestPrepareSynced)281 TEST_F(VibratorManagerHalWrapperAidlTest, TestPrepareSynced) {
282     EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
283             .Times(Exactly(1))
284             .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
285 
286     EXPECT_CALL(*mMockHal.get(), getVibrator(_, _))
287             .Times(Exactly(2))
288             .WillRepeatedly(DoAll(SetArgPointee<1>(mMockVibrator), Return(Status())));
289 
290     EXPECT_CALL(*mMockHal.get(), prepareSynced(Eq(kVibratorIds)))
291             .Times(Exactly(3))
292             .WillOnce(
293                     Return(Status::fromExceptionCode(Status::Exception::EX_UNSUPPORTED_OPERATION)))
294             .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
295             .WillRepeatedly(Return(Status()));
296 
297     ASSERT_TRUE(mWrapper->getVibratorIds().isOk());
298     ASSERT_TRUE(mWrapper->prepareSynced(kVibratorIds).isUnsupported());
299     ASSERT_TRUE(mWrapper->prepareSynced(kVibratorIds).isFailed());
300     ASSERT_TRUE(mWrapper->prepareSynced(kVibratorIds).isOk());
301 }
302 
TEST_F(VibratorManagerHalWrapperAidlTest,TestTriggerSyncedWithCallbackSupport)303 TEST_F(VibratorManagerHalWrapperAidlTest, TestTriggerSyncedWithCallbackSupport) {
304     {
305         InSequence seq;
306         EXPECT_CALL(*mMockHal.get(), getCapabilities(_))
307                 .Times(Exactly(1))
308                 .WillRepeatedly(DoAll(SetArgPointee<0>(IVibratorManager::CAP_TRIGGER_CALLBACK),
309                                       Return(Status())));
310         EXPECT_CALL(*mMockHal.get(), triggerSynced(_))
311                 .Times(Exactly(3))
312                 .WillOnce(Return(Status::fromStatusT(UNKNOWN_TRANSACTION)))
313                 .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
314                 .WillRepeatedly(DoAll(TriggerCallback(), Return(Status())));
315     }
316 
317     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
318     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
319 
320     ASSERT_TRUE(mWrapper->triggerSynced(callback).isUnsupported());
321     ASSERT_TRUE(mWrapper->triggerSynced(callback).isFailed());
322     ASSERT_TRUE(mWrapper->triggerSynced(callback).isOk());
323     ASSERT_EQ(1, *callbackCounter.get());
324 }
325 
TEST_F(VibratorManagerHalWrapperAidlTest,TestTriggerSyncedWithoutCallbackSupport)326 TEST_F(VibratorManagerHalWrapperAidlTest, TestTriggerSyncedWithoutCallbackSupport) {
327     {
328         InSequence seq;
329         EXPECT_CALL(*mMockHal.get(), getCapabilities(_))
330                 .Times(Exactly(1))
331                 .WillRepeatedly(
332                         DoAll(SetArgPointee<0>(IVibratorManager::CAP_SYNC), Return(Status())));
333         EXPECT_CALL(*mMockHal.get(), triggerSynced(Eq(nullptr)))
334                 .Times(Exactly(1))
335                 .WillRepeatedly(Return(Status()));
336     }
337 
338     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
339     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
340 
341     ASSERT_TRUE(mWrapper->triggerSynced(callback).isOk());
342     ASSERT_EQ(0, *callbackCounter.get());
343 }
344 
TEST_F(VibratorManagerHalWrapperAidlTest,TestCancelSynced)345 TEST_F(VibratorManagerHalWrapperAidlTest, TestCancelSynced) {
346     EXPECT_CALL(*mMockHal.get(), cancelSynced())
347             .Times(Exactly(3))
348             .WillOnce(Return(Status::fromStatusT(UNKNOWN_TRANSACTION)))
349             .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
350             .WillRepeatedly(Return(Status()));
351 
352     ASSERT_TRUE(mWrapper->cancelSynced().isUnsupported());
353     ASSERT_TRUE(mWrapper->cancelSynced().isFailed());
354     ASSERT_TRUE(mWrapper->cancelSynced().isOk());
355 }
356 
TEST_F(VibratorManagerHalWrapperAidlTest,TestCancelSyncedReloadsAllControllers)357 TEST_F(VibratorManagerHalWrapperAidlTest, TestCancelSyncedReloadsAllControllers) {
358     EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
359             .Times(Exactly(1))
360             .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
361 
362     EXPECT_CALL(*mMockHal.get(), getVibrator(_, _))
363             .Times(Exactly(2))
364             .WillRepeatedly(DoAll(SetArgPointee<1>(mMockVibrator), Return(Status())));
365 
366     EXPECT_CALL(*mMockHal.get(), cancelSynced()).Times(Exactly(1)).WillRepeatedly(Return(Status()));
367 
368     ASSERT_TRUE(mWrapper->getVibratorIds().isOk());
369     ASSERT_TRUE(mWrapper->cancelSynced().isOk());
370 }
371