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
__anon714a623c0102(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(Status::Exception::EX_SECURITY))))
258 .WillRepeatedly(DoAll(SetArgPointee<1>(mMockVibrator), Return(Status())));
259
260 EXPECT_CALL(*mMockVibrator.get(), off())
261 .Times(Exactly(3))
262 .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
263 .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
264 .WillRepeatedly(Return(Status()));
265
266 // Get vibrator controller is successful even if first getVibrator.
267 auto result = mWrapper->getVibrator(kVibratorId);
268 ASSERT_TRUE(result.isOk());
269 ASSERT_NE(nullptr, result.value().get());
270
271 auto vibrator = result.value();
272 // First getVibrator call fails.
273 ASSERT_FALSE(vibrator->init());
274 // First and second off() calls fail, reload IVibrator with getVibrator.
275 ASSERT_TRUE(vibrator->doWithRetry<void>(OFF_FN, "off").isFailed());
276 // Third call to off() worked after IVibrator reloaded.
277 ASSERT_TRUE(vibrator->doWithRetry<void>(OFF_FN, "off").isOk());
278 }
279
TEST_F(VibratorManagerHalWrapperAidlTest,TestPrepareSynced)280 TEST_F(VibratorManagerHalWrapperAidlTest, TestPrepareSynced) {
281 EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
282 .Times(Exactly(1))
283 .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
284
285 EXPECT_CALL(*mMockHal.get(), getVibrator(_, _))
286 .Times(Exactly(2))
287 .WillRepeatedly(DoAll(SetArgPointee<1>(mMockVibrator), Return(Status())));
288
289 EXPECT_CALL(*mMockHal.get(), prepareSynced(Eq(kVibratorIds)))
290 .Times(Exactly(3))
291 .WillOnce(
292 Return(Status::fromExceptionCode(Status::Exception::EX_UNSUPPORTED_OPERATION)))
293 .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
294 .WillRepeatedly(Return(Status()));
295
296 ASSERT_TRUE(mWrapper->getVibratorIds().isOk());
297 ASSERT_TRUE(mWrapper->prepareSynced(kVibratorIds).isUnsupported());
298 ASSERT_TRUE(mWrapper->prepareSynced(kVibratorIds).isFailed());
299 ASSERT_TRUE(mWrapper->prepareSynced(kVibratorIds).isOk());
300 }
301
TEST_F(VibratorManagerHalWrapperAidlTest,TestTriggerSyncedWithCallbackSupport)302 TEST_F(VibratorManagerHalWrapperAidlTest, TestTriggerSyncedWithCallbackSupport) {
303 {
304 InSequence seq;
305 EXPECT_CALL(*mMockHal.get(), getCapabilities(_))
306 .Times(Exactly(1))
307 .WillRepeatedly(DoAll(SetArgPointee<0>(IVibratorManager::CAP_TRIGGER_CALLBACK),
308 Return(Status())));
309 EXPECT_CALL(*mMockHal.get(), triggerSynced(_))
310 .Times(Exactly(3))
311 .WillOnce(Return(Status::fromStatusT(UNKNOWN_TRANSACTION)))
312 .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
313 .WillRepeatedly(DoAll(TriggerCallback(), Return(Status())));
314 }
315
316 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
317 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
318
319 ASSERT_TRUE(mWrapper->triggerSynced(callback).isUnsupported());
320 ASSERT_TRUE(mWrapper->triggerSynced(callback).isFailed());
321 ASSERT_TRUE(mWrapper->triggerSynced(callback).isOk());
322 ASSERT_EQ(1, *callbackCounter.get());
323 }
324
TEST_F(VibratorManagerHalWrapperAidlTest,TestTriggerSyncedWithoutCallbackSupport)325 TEST_F(VibratorManagerHalWrapperAidlTest, TestTriggerSyncedWithoutCallbackSupport) {
326 {
327 InSequence seq;
328 EXPECT_CALL(*mMockHal.get(), getCapabilities(_))
329 .Times(Exactly(1))
330 .WillRepeatedly(
331 DoAll(SetArgPointee<0>(IVibratorManager::CAP_SYNC), Return(Status())));
332 EXPECT_CALL(*mMockHal.get(), triggerSynced(Eq(nullptr)))
333 .Times(Exactly(1))
334 .WillRepeatedly(Return(Status()));
335 }
336
337 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
338 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
339
340 ASSERT_TRUE(mWrapper->triggerSynced(callback).isOk());
341 ASSERT_EQ(0, *callbackCounter.get());
342 }
343
TEST_F(VibratorManagerHalWrapperAidlTest,TestCancelSynced)344 TEST_F(VibratorManagerHalWrapperAidlTest, TestCancelSynced) {
345 EXPECT_CALL(*mMockHal.get(), cancelSynced())
346 .Times(Exactly(3))
347 .WillOnce(Return(Status::fromStatusT(UNKNOWN_TRANSACTION)))
348 .WillOnce(Return(Status::fromExceptionCode(Status::Exception::EX_SECURITY)))
349 .WillRepeatedly(Return(Status()));
350
351 ASSERT_TRUE(mWrapper->cancelSynced().isUnsupported());
352 ASSERT_TRUE(mWrapper->cancelSynced().isFailed());
353 ASSERT_TRUE(mWrapper->cancelSynced().isOk());
354 }
355
TEST_F(VibratorManagerHalWrapperAidlTest,TestCancelSyncedReloadsAllControllers)356 TEST_F(VibratorManagerHalWrapperAidlTest, TestCancelSyncedReloadsAllControllers) {
357 EXPECT_CALL(*mMockHal.get(), getVibratorIds(_))
358 .Times(Exactly(1))
359 .WillRepeatedly(DoAll(SetArgPointee<0>(kVibratorIds), Return(Status())));
360
361 EXPECT_CALL(*mMockHal.get(), getVibrator(_, _))
362 .Times(Exactly(2))
363 .WillRepeatedly(DoAll(SetArgPointee<1>(mMockVibrator), Return(Status())));
364
365 EXPECT_CALL(*mMockHal.get(), cancelSynced()).Times(Exactly(1)).WillRepeatedly(Return(Status()));
366
367 ASSERT_TRUE(mWrapper->getVibratorIds().isOk());
368 ASSERT_TRUE(mWrapper->cancelSynced().isOk());
369 }
370