1 /*
2 * Copyright (C) 2022 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 #include <aidl/android/hardware/vibrator/BnVibratorCallback.h>
18 #include <android-base/logging.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <linux/input.h>
22 #include <linux/uinput.h>
23
24 #include <future>
25
26 #include "Vibrator.h"
27 #include "mocks.h"
28 #include "types.h"
29 #include "utils.h"
30
31 namespace aidl {
32 namespace android {
33 namespace hardware {
34 namespace vibrator {
35
36 using ::testing::_;
37 using ::testing::AnyNumber;
38 using ::testing::Assign;
39 using ::testing::AtLeast;
40 using ::testing::AtMost;
41 using ::testing::Combine;
42 using ::testing::DoAll;
43 using ::testing::DoDefault;
44 using ::testing::Exactly;
45 using ::testing::Expectation;
46 using ::testing::ExpectationSet;
47 using ::testing::Ge;
48 using ::testing::Mock;
49 using ::testing::MockFunction;
50 using ::testing::Range;
51 using ::testing::Return;
52 using ::testing::Sequence;
53 using ::testing::SetArgPointee;
54 using ::testing::SetArgReferee;
55 using ::testing::Test;
56 using ::testing::TestParamInfo;
57 using ::testing::ValuesIn;
58 using ::testing::WithParamInterface;
59
60 // Forward Declarations
61
62 static EffectQueue Queue(const QueueEffect &effect);
63 static EffectQueue Queue(const QueueDelay &delay);
64 template <typename T, typename U, typename... Args>
65 static EffectQueue Queue(const T &first, const U &second, Args... rest);
66
67 static EffectLevel Level(float intensity, float levelLow, float levelHigh);
68 static EffectScale Scale(float intensity, float levelLow, float levelHigh);
69
70 // Constants With Arbitrary Values
71
72 static constexpr uint32_t CAL_VERSION = 2;
73 static constexpr std::array<EffectLevel, 2> V_TICK_DEFAULT = {1, 100};
74 static constexpr std::array<EffectLevel, 2> V_CLICK_DEFAULT{1, 100};
75 static constexpr std::array<EffectLevel, 2> V_LONG_DEFAULT{1, 100};
76 static constexpr std::array<EffectDuration, 14> EFFECT_DURATIONS{
77 0, 100, 30, 1000, 300, 130, 150, 500, 100, 15, 20, 1000, 1000, 1000};
78
79 // Constants With Prescribed Values
80
81 static const std::map<Effect, EffectIndex> EFFECT_INDEX{
82 {Effect::CLICK, 2},
83 {Effect::TICK, 2},
84 {Effect::HEAVY_CLICK, 2},
85 {Effect::TEXTURE_TICK, 9},
86 };
87 static constexpr uint32_t MIN_ON_OFF_INTERVAL_US = 8500;
88 static constexpr uint8_t VOLTAGE_SCALE_MAX = 100;
89 static constexpr int8_t MAX_COLD_START_LATENCY_MS = 6; // I2C Transaction + DSP Return-From-Standby
90 static constexpr auto POLLING_TIMEOUT = 20;
91 enum WaveformIndex : uint16_t {
92 /* Physical waveform */
93 WAVEFORM_LONG_VIBRATION_EFFECT_INDEX = 0,
94 WAVEFORM_RESERVED_INDEX_1 = 1,
95 WAVEFORM_CLICK_INDEX = 2,
96 WAVEFORM_SHORT_VIBRATION_EFFECT_INDEX = 3,
97 WAVEFORM_THUD_INDEX = 4,
98 WAVEFORM_SPIN_INDEX = 5,
99 WAVEFORM_QUICK_RISE_INDEX = 6,
100 WAVEFORM_SLOW_RISE_INDEX = 7,
101 WAVEFORM_QUICK_FALL_INDEX = 8,
102 WAVEFORM_LIGHT_TICK_INDEX = 9,
103 WAVEFORM_LOW_TICK_INDEX = 10,
104 WAVEFORM_RESERVED_MFG_1,
105 WAVEFORM_RESERVED_MFG_2,
106 WAVEFORM_RESERVED_MFG_3,
107 WAVEFORM_MAX_PHYSICAL_INDEX,
108 /* OWT waveform */
109 WAVEFORM_COMPOSE = WAVEFORM_MAX_PHYSICAL_INDEX,
110 WAVEFORM_PWLE,
111 /*
112 * Refer to <linux/input.h>, the WAVEFORM_MAX_INDEX must not exceed 96.
113 * #define FF_GAIN 0x60 // 96 in decimal
114 * #define FF_MAX_EFFECTS FF_GAIN
115 */
116 WAVEFORM_MAX_INDEX,
117 };
118
119 static const EffectScale ON_GLOBAL_SCALE{levelToScale(V_LONG_DEFAULT[1])};
120 static const EffectIndex ON_EFFECT_INDEX{0};
121
122 static const std::map<EffectTuple, EffectScale> EFFECT_SCALE{
123 {{Effect::TICK, EffectStrength::LIGHT},
124 Scale(0.5f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
125 {{Effect::TICK, EffectStrength::MEDIUM},
126 Scale(0.5f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
127 {{Effect::TICK, EffectStrength::STRONG},
128 Scale(0.5f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
129 {{Effect::CLICK, EffectStrength::LIGHT},
130 Scale(0.7f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
131 {{Effect::CLICK, EffectStrength::MEDIUM},
132 Scale(0.7f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
133 {{Effect::CLICK, EffectStrength::STRONG},
134 Scale(0.7f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
135 {{Effect::HEAVY_CLICK, EffectStrength::LIGHT},
136 Scale(1.0f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
137 {{Effect::HEAVY_CLICK, EffectStrength::MEDIUM},
138 Scale(1.0f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
139 {{Effect::HEAVY_CLICK, EffectStrength::STRONG},
140 Scale(1.0f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
141 {{Effect::TEXTURE_TICK, EffectStrength::LIGHT},
142 Scale(0.5f * 0.5f, V_TICK_DEFAULT[0], V_TICK_DEFAULT[1])},
143 {{Effect::TEXTURE_TICK, EffectStrength::MEDIUM},
144 Scale(0.5f * 0.7f, V_TICK_DEFAULT[0], V_TICK_DEFAULT[1])},
145 {{Effect::TEXTURE_TICK, EffectStrength::STRONG},
146 Scale(0.5f * 1.0f, V_TICK_DEFAULT[0], V_TICK_DEFAULT[1])},
147 };
148
149 static const std::map<EffectTuple, EffectQueue> EFFECT_QUEUE{
150 {{Effect::DOUBLE_CLICK, EffectStrength::LIGHT},
151 Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
152 Level(0.7f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
153 100,
154 QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
155 Level(1.0f * 0.5f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])})},
156 {{Effect::DOUBLE_CLICK, EffectStrength::MEDIUM},
157 Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
158 Level(0.7f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
159 100,
160 QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
161 Level(1.0f * 0.7f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])})},
162 {{Effect::DOUBLE_CLICK, EffectStrength::STRONG},
163 Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
164 Level(0.7f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])},
165 100,
166 QueueEffect{EFFECT_INDEX.at(Effect::CLICK),
167 Level(1.0f * 1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])})},
168 };
169
Queue(const QueueEffect & effect)170 EffectQueue Queue(const QueueEffect &effect) {
171 auto index = std::get<0>(effect);
172 auto level = std::get<1>(effect);
173 auto string = std::to_string(index) + "." + std::to_string(level);
174 auto duration = EFFECT_DURATIONS[index];
175 return {string, duration};
176 }
177
Queue(const QueueDelay & delay)178 EffectQueue Queue(const QueueDelay &delay) {
179 auto string = std::to_string(delay);
180 return {string, delay};
181 }
182
183 template <typename T, typename U, typename... Args>
Queue(const T & first,const U & second,Args...rest)184 EffectQueue Queue(const T &first, const U &second, Args... rest) {
185 auto head = Queue(first);
186 auto tail = Queue(second, rest...);
187 auto string = std::get<0>(head) + "," + std::get<0>(tail);
188 auto duration = std::get<1>(head) + std::get<1>(tail);
189 return {string, duration};
190 }
191
Level(float intensity,float levelLow,float levelHigh)192 static EffectLevel Level(float intensity, float levelLow, float levelHigh) {
193 return std::lround(intensity * (levelHigh - levelLow)) + levelLow;
194 }
195
Scale(float intensity,float levelLow,float levelHigh)196 static EffectScale Scale(float intensity, float levelLow, float levelHigh) {
197 return levelToScale(Level(intensity, levelLow, levelHigh));
198 }
199
200 class VibratorTest : public Test {
201 public:
SetUp()202 void SetUp() override {
203 setenv("INPUT_EVENT_NAME", "CS40L26TestSuite", true);
204 std::unique_ptr<MockApi> mockapi;
205 std::unique_ptr<MockCal> mockcal;
206
207 createMock(&mockapi, &mockcal);
208 createVibrator(std::move(mockapi), std::move(mockcal));
209 }
210
TearDown()211 void TearDown() override { deleteVibrator(); }
212
213 protected:
createMock(std::unique_ptr<MockApi> * mockapi,std::unique_ptr<MockCal> * mockcal)214 void createMock(std::unique_ptr<MockApi> *mockapi, std::unique_ptr<MockCal> *mockcal) {
215 *mockapi = std::make_unique<MockApi>();
216 *mockcal = std::make_unique<MockCal>();
217
218 mMockApi = mockapi->get();
219 mMockCal = mockcal->get();
220
221 ON_CALL(*mMockApi, destructor()).WillByDefault(Assign(&mMockApi, nullptr));
222
223 ON_CALL(*mMockApi, setFFGain(_, _)).WillByDefault(Return(true));
224 ON_CALL(*mMockApi, setFFEffect(_, _, _)).WillByDefault(Return(true));
225 ON_CALL(*mMockApi, setFFPlay(_, _, _)).WillByDefault(Return(true));
226 ON_CALL(*mMockApi, pollVibeState(_, _)).WillByDefault(Return(true));
227 ON_CALL(*mMockApi, uploadOwtEffect(_, _, _, _, _, _)).WillByDefault(Return(true));
228 ON_CALL(*mMockApi, eraseOwtEffect(_, _, _)).WillByDefault(Return(true));
229
230 ON_CALL(*mMockApi, getOwtFreeSpace(_))
231 .WillByDefault(DoAll(SetArgPointee<0>(11504), Return(true)));
232
233 ON_CALL(*mMockCal, destructor()).WillByDefault(Assign(&mMockCal, nullptr));
234
235 ON_CALL(*mMockCal, getVersion(_))
236 .WillByDefault(DoAll(SetArgPointee<0>(CAL_VERSION), Return(true)));
237
238 ON_CALL(*mMockCal, getTickVolLevels(_))
239 .WillByDefault(DoAll(SetArgPointee<0>(V_TICK_DEFAULT), Return(true)));
240 ON_CALL(*mMockCal, getClickVolLevels(_))
241 .WillByDefault(DoAll(SetArgPointee<0>(V_CLICK_DEFAULT), Return(true)));
242 ON_CALL(*mMockCal, getLongVolLevels(_))
243 .WillByDefault(DoAll(SetArgPointee<0>(V_LONG_DEFAULT), Return(true)));
244
245 relaxMock(false);
246 }
247
createVibrator(std::unique_ptr<MockApi> mockapi,std::unique_ptr<MockCal> mockcal,bool relaxed=true)248 void createVibrator(std::unique_ptr<MockApi> mockapi, std::unique_ptr<MockCal> mockcal,
249 bool relaxed = true) {
250 if (relaxed) {
251 relaxMock(true);
252 }
253 mVibrator = ndk::SharedRefBase::make<Vibrator>(std::move(mockapi), std::move(mockcal));
254 if (relaxed) {
255 relaxMock(false);
256 }
257 }
258
deleteVibrator(bool relaxed=true)259 void deleteVibrator(bool relaxed = true) {
260 if (relaxed) {
261 relaxMock(true);
262 }
263 mVibrator.reset();
264 }
265
266 private:
relaxMock(bool relax)267 void relaxMock(bool relax) {
268 auto times = relax ? AnyNumber() : Exactly(0);
269
270 Mock::VerifyAndClearExpectations(mMockApi);
271 Mock::VerifyAndClearExpectations(mMockCal);
272
273 EXPECT_CALL(*mMockApi, destructor()).Times(times);
274 EXPECT_CALL(*mMockApi, setF0(_)).Times(times);
275 EXPECT_CALL(*mMockApi, setF0Offset(_)).Times(times);
276 EXPECT_CALL(*mMockApi, setRedc(_)).Times(times);
277 EXPECT_CALL(*mMockApi, setQ(_)).Times(times);
278 EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).Times(times);
279 EXPECT_CALL(*mMockApi, getOwtFreeSpace(_)).Times(times);
280 EXPECT_CALL(*mMockApi, setF0CompEnable(_)).Times(times);
281 EXPECT_CALL(*mMockApi, setRedcCompEnable(_)).Times(times);
282 EXPECT_CALL(*mMockApi, pollVibeState(_, _)).Times(times);
283 EXPECT_CALL(*mMockApi, setFFGain(_, _)).Times(times);
284 EXPECT_CALL(*mMockApi, setFFEffect(_, _, _)).Times(times);
285 EXPECT_CALL(*mMockApi, setFFPlay(_, _, _)).Times(times);
286 EXPECT_CALL(*mMockApi, setMinOnOffInterval(_)).Times(times);
287 EXPECT_CALL(*mMockApi, getContextScale()).Times(times);
288 EXPECT_CALL(*mMockApi, getContextEnable()).Times(times);
289 EXPECT_CALL(*mMockApi, getContextSettlingTime()).Times(times);
290 EXPECT_CALL(*mMockApi, getContextCooldownTime()).Times(times);
291 EXPECT_CALL(*mMockApi, getContextFadeEnable()).Times(times);
292 EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).Times(times);
293 EXPECT_CALL(*mMockApi, setHapticPcmAmp(_, _, _, _)).Times(times);
294
295 EXPECT_CALL(*mMockApi, debug(_)).Times(times);
296
297 EXPECT_CALL(*mMockCal, destructor()).Times(times);
298 EXPECT_CALL(*mMockCal, getF0(_)).Times(times);
299 EXPECT_CALL(*mMockCal, getRedc(_)).Times(times);
300 EXPECT_CALL(*mMockCal, getQ(_)).Times(times);
301 EXPECT_CALL(*mMockCal, getTickVolLevels(_)).Times(times);
302 EXPECT_CALL(*mMockCal, getClickVolLevels(_)).Times(times);
303 EXPECT_CALL(*mMockCal, getLongVolLevels(_)).Times(times);
304 EXPECT_CALL(*mMockCal, isChirpEnabled()).Times(times);
305 EXPECT_CALL(*mMockCal, getLongFrequencyShift(_)).Times(times);
306 EXPECT_CALL(*mMockCal, isF0CompEnabled()).Times(times);
307 EXPECT_CALL(*mMockCal, isRedcCompEnabled()).Times(times);
308 EXPECT_CALL(*mMockCal, debug(_)).Times(times);
309 }
310
311 protected:
312 MockApi *mMockApi;
313 MockCal *mMockCal;
314 std::shared_ptr<IVibrator> mVibrator;
315 uint32_t mEffectIndex;
316 };
317
TEST_F(VibratorTest,Constructor)318 TEST_F(VibratorTest, Constructor) {
319 std::unique_ptr<MockApi> mockapi;
320 std::unique_ptr<MockCal> mockcal;
321 std::string f0Val = std::to_string(std::rand());
322 std::string redcVal = std::to_string(std::rand());
323 std::string qVal = std::to_string(std::rand());
324 uint32_t calVer;
325 uint32_t supportedPrimitivesBits = 0x0;
326 Expectation volGet;
327 Sequence f0Seq, redcSeq, qSeq, supportedPrimitivesSeq;
328
329 EXPECT_CALL(*mMockApi, destructor()).WillOnce(DoDefault());
330 EXPECT_CALL(*mMockCal, destructor()).WillOnce(DoDefault());
331
332 deleteVibrator(false);
333
334 createMock(&mockapi, &mockcal);
335
336 EXPECT_CALL(*mMockCal, getF0(_))
337 .InSequence(f0Seq)
338 .WillOnce(DoAll(SetArgReferee<0>(f0Val), Return(true)));
339 EXPECT_CALL(*mMockApi, setF0(f0Val)).InSequence(f0Seq).WillOnce(Return(true));
340
341 EXPECT_CALL(*mMockCal, getRedc(_))
342 .InSequence(redcSeq)
343 .WillOnce(DoAll(SetArgReferee<0>(redcVal), Return(true)));
344 EXPECT_CALL(*mMockApi, setRedc(redcVal)).InSequence(redcSeq).WillOnce(Return(true));
345
346 EXPECT_CALL(*mMockCal, getQ(_))
347 .InSequence(qSeq)
348 .WillOnce(DoAll(SetArgReferee<0>(qVal), Return(true)));
349 EXPECT_CALL(*mMockApi, setQ(qVal)).InSequence(qSeq).WillOnce(Return(true));
350
351 EXPECT_CALL(*mMockCal, getLongFrequencyShift(_)).WillOnce(Return(true));
352
353 mMockCal->getVersion(&calVer);
354 if (calVer == 2) {
355 volGet = EXPECT_CALL(*mMockCal, getTickVolLevels(_)).WillOnce(DoDefault());
356 volGet = EXPECT_CALL(*mMockCal, getClickVolLevels(_)).WillOnce(DoDefault());
357 volGet = EXPECT_CALL(*mMockCal, getLongVolLevels(_)).WillOnce(DoDefault());
358 }
359
360 EXPECT_CALL(*mMockCal, isF0CompEnabled()).WillOnce(Return(true));
361 EXPECT_CALL(*mMockApi, setF0CompEnable(true)).WillOnce(Return(true));
362 EXPECT_CALL(*mMockCal, isRedcCompEnabled()).WillOnce(Return(true));
363 EXPECT_CALL(*mMockApi, setRedcCompEnable(true)).WillOnce(Return(true));
364
365 EXPECT_CALL(*mMockCal, isChirpEnabled()).WillOnce(Return(true));
366 EXPECT_CALL(*mMockCal, getSupportedPrimitives(_))
367 .InSequence(supportedPrimitivesSeq)
368 .WillOnce(DoAll(SetArgPointee<0>(supportedPrimitivesBits), Return(true)));
369
370 EXPECT_CALL(*mMockApi, setMinOnOffInterval(MIN_ON_OFF_INTERVAL_US)).WillOnce(Return(true));
371 EXPECT_CALL(*mMockApi, getContextScale()).WillOnce(Return(0));
372 EXPECT_CALL(*mMockApi, getContextEnable()).WillOnce(Return(false));
373 EXPECT_CALL(*mMockApi, getContextSettlingTime()).WillOnce(Return(0));
374 EXPECT_CALL(*mMockApi, getContextCooldownTime()).WillOnce(Return(0));
375 EXPECT_CALL(*mMockApi, getContextFadeEnable()).WillOnce(Return(false));
376 createVibrator(std::move(mockapi), std::move(mockcal), false);
377 }
378
TEST_F(VibratorTest,on)379 TEST_F(VibratorTest, on) {
380 Sequence s1, s2;
381 uint16_t duration = std::rand() + 1;
382
383 EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE)).InSequence(s1).WillOnce(DoDefault());
384 EXPECT_CALL(*mMockApi, setFFEffect(_, _, duration + MAX_COLD_START_LATENCY_MS))
385 .InSequence(s2)
386 .WillOnce(DoDefault());
387 EXPECT_CALL(*mMockApi, setFFPlay(_, ON_EFFECT_INDEX, true))
388 .InSequence(s1, s2)
389 .WillOnce(DoDefault());
390 EXPECT_TRUE(mVibrator->on(duration, nullptr).isOk());
391 }
392
TEST_F(VibratorTest,off)393 TEST_F(VibratorTest, off) {
394 Sequence s1;
395 EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE)).InSequence(s1).WillOnce(DoDefault());
396 EXPECT_TRUE(mVibrator->off().isOk());
397 }
398
TEST_F(VibratorTest,supportsAmplitudeControl_supported)399 TEST_F(VibratorTest, supportsAmplitudeControl_supported) {
400 int32_t capabilities;
401 EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).WillOnce(Return(true));
402 EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).WillOnce(Return(true));
403
404 EXPECT_TRUE(mVibrator->getCapabilities(&capabilities).isOk());
405 EXPECT_GT(capabilities & IVibrator::CAP_AMPLITUDE_CONTROL, 0);
406 }
407
TEST_F(VibratorTest,supportsExternalAmplitudeControl_unsupported)408 TEST_F(VibratorTest, supportsExternalAmplitudeControl_unsupported) {
409 int32_t capabilities;
410 EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).WillOnce(Return(true));
411 EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).WillOnce(Return(true));
412
413 EXPECT_TRUE(mVibrator->getCapabilities(&capabilities).isOk());
414 EXPECT_EQ(capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL, 0);
415 }
416
TEST_F(VibratorTest,setAmplitude_supported)417 TEST_F(VibratorTest, setAmplitude_supported) {
418 EffectAmplitude amplitude = static_cast<float>(std::rand()) / RAND_MAX ?: 1.0f;
419
420 EXPECT_CALL(*mMockApi, setFFGain(_, amplitudeToScale(amplitude))).WillOnce(Return(true));
421
422 EXPECT_TRUE(mVibrator->setAmplitude(amplitude).isOk());
423 }
424
TEST_F(VibratorTest,supportsExternalControl_supported)425 TEST_F(VibratorTest, supportsExternalControl_supported) {
426 int32_t capabilities;
427 EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).WillOnce(Return(true));
428 EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).WillOnce(Return(true));
429
430 EXPECT_TRUE(mVibrator->getCapabilities(&capabilities).isOk());
431 EXPECT_GT(capabilities & IVibrator::CAP_EXTERNAL_CONTROL, 0);
432 }
433
TEST_F(VibratorTest,supportsExternalControl_unsupported)434 TEST_F(VibratorTest, supportsExternalControl_unsupported) {
435 int32_t capabilities;
436 EXPECT_CALL(*mMockApi, hasOwtFreeSpace()).WillOnce(Return(true));
437 EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).WillOnce(Return(false));
438
439 EXPECT_TRUE(mVibrator->getCapabilities(&capabilities).isOk());
440 EXPECT_EQ(capabilities & IVibrator::CAP_EXTERNAL_CONTROL, 0);
441 }
442
TEST_F(VibratorTest,setExternalControl_enable)443 TEST_F(VibratorTest, setExternalControl_enable) {
444 Sequence s1, s2;
445 EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE)).InSequence(s1).WillOnce(DoDefault());
446 EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).InSequence(s2).WillOnce(Return(true));
447 EXPECT_CALL(*mMockApi, setHapticPcmAmp(_, true, _, _))
448 .InSequence(s1, s2)
449 .WillOnce(Return(true));
450
451 EXPECT_TRUE(mVibrator->setExternalControl(true).isOk());
452 }
453
TEST_F(VibratorTest,setExternalControl_disable)454 TEST_F(VibratorTest, setExternalControl_disable) {
455 Sequence s1, s2, s3, s4;
456
457 // The default mIsUnderExternalControl is false, so it needs to turn on the External Control
458 // to make mIsUnderExternalControl become true.
459 EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE))
460 .InSequence(s1)
461 .InSequence(s1)
462 .WillOnce(DoDefault());
463 EXPECT_CALL(*mMockApi, getHapticAlsaDevice(_, _)).InSequence(s2).WillOnce(Return(true));
464 EXPECT_CALL(*mMockApi, setHapticPcmAmp(_, true, _, _)).InSequence(s3).WillOnce(Return(true));
465
466 EXPECT_TRUE(mVibrator->setExternalControl(true).isOk());
467
468 EXPECT_CALL(*mMockApi, setFFGain(_, levelToScale(VOLTAGE_SCALE_MAX)))
469 .InSequence(s4)
470 .WillOnce(DoDefault());
471 EXPECT_CALL(*mMockApi, setHapticPcmAmp(_, false, _, _))
472 .InSequence(s1, s2, s3, s4)
473 .WillOnce(Return(true));
474
475 EXPECT_TRUE(mVibrator->setExternalControl(false).isOk());
476 }
477
478 class EffectsTest : public VibratorTest, public WithParamInterface<EffectTuple> {
479 public:
PrintParam(const TestParamInfo<ParamType> & info)480 static auto PrintParam(const TestParamInfo<ParamType> &info) {
481 auto param = info.param;
482 auto effect = std::get<0>(param);
483 auto strength = std::get<1>(param);
484 return toString(effect) + "_" + toString(strength);
485 }
486 };
487
TEST_P(EffectsTest,perform)488 TEST_P(EffectsTest, perform) {
489 auto param = GetParam();
490 auto effect = std::get<0>(param);
491 auto strength = std::get<1>(param);
492 auto scale = EFFECT_SCALE.find(param);
493 auto queue = EFFECT_QUEUE.find(param);
494 EffectDuration duration;
495 auto callback = ndk::SharedRefBase::make<MockVibratorCallback>();
496 std::promise<void> promise;
497 std::future<void> future{promise.get_future()};
498 auto complete = [&promise] {
499 promise.set_value();
500 return ndk::ScopedAStatus::ok();
501 };
502 bool composeEffect;
503
504 ExpectationSet eSetup;
505 Expectation eActivate, ePollHaptics, ePollStop, eEraseDone;
506
507 if (scale != EFFECT_SCALE.end()) {
508 EffectIndex index = EFFECT_INDEX.at(effect);
509 duration = EFFECT_DURATIONS[index];
510
511 eSetup += EXPECT_CALL(*mMockApi, setFFGain(_, levelToScale(scale->second)))
512 .WillOnce(DoDefault());
513 eActivate = EXPECT_CALL(*mMockApi, setFFPlay(_, index, true))
514 .After(eSetup)
515 .WillOnce(DoDefault());
516 } else if (queue != EFFECT_QUEUE.end()) {
517 duration = std::get<1>(queue->second);
518 eSetup += EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE))
519 .After(eSetup)
520 .WillOnce(DoDefault());
521 eSetup += EXPECT_CALL(*mMockApi, getOwtFreeSpace(_)).WillOnce(DoDefault());
522 eSetup += EXPECT_CALL(*mMockApi, uploadOwtEffect(_, _, _, _, _, _))
523 .After(eSetup)
524 .WillOnce(DoDefault());
525 eActivate = EXPECT_CALL(*mMockApi, setFFPlay(_, WAVEFORM_COMPOSE, true))
526 .After(eSetup)
527 .WillOnce(DoDefault());
528 composeEffect = true;
529 } else {
530 duration = 0;
531 }
532
533 if (duration) {
534 ePollHaptics = EXPECT_CALL(*mMockApi, pollVibeState(1, POLLING_TIMEOUT))
535 .After(eActivate)
536 .WillOnce(DoDefault());
537 ePollStop = EXPECT_CALL(*mMockApi, pollVibeState(0, -1))
538 .After(ePollHaptics)
539 .WillOnce(DoDefault());
540 if (composeEffect) {
541 eEraseDone = EXPECT_CALL(*mMockApi, eraseOwtEffect(_, _, _))
542 .After(ePollStop)
543 .WillOnce(DoDefault());
544 EXPECT_CALL(*callback, onComplete()).After(eEraseDone).WillOnce(complete);
545 } else {
546 EXPECT_CALL(*callback, onComplete()).After(ePollStop).WillOnce(complete);
547 }
548 }
549
550 int32_t lengthMs;
551 ndk::ScopedAStatus status = mVibrator->perform(effect, strength, callback, &lengthMs);
552 if (status.isOk()) {
553 EXPECT_LE(duration, lengthMs);
554 } else {
555 EXPECT_EQ(EX_UNSUPPORTED_OPERATION, status.getExceptionCode());
556 EXPECT_EQ(0, lengthMs);
557 }
558
559 if (duration) {
560 EXPECT_EQ(future.wait_for(std::chrono::milliseconds(100)), std::future_status::ready);
561 }
562 }
563
564 const std::vector<Effect> kEffects{ndk::enum_range<Effect>().begin(),
565 ndk::enum_range<Effect>().end()};
566 const std::vector<EffectStrength> kEffectStrengths{ndk::enum_range<EffectStrength>().begin(),
567 ndk::enum_range<EffectStrength>().end()};
568
569 INSTANTIATE_TEST_CASE_P(VibratorTests, EffectsTest,
570 Combine(ValuesIn(kEffects.begin(), kEffects.end()),
571 ValuesIn(kEffectStrengths.begin(), kEffectStrengths.end())),
572 EffectsTest::PrintParam);
573
574 struct PrimitiveParam {
575 CompositePrimitive primitive;
576 EffectIndex index;
577 };
578
579 class PrimitiveTest : public VibratorTest, public WithParamInterface<PrimitiveParam> {
580 public:
PrintParam(const TestParamInfo<ParamType> & info)581 static auto PrintParam(const TestParamInfo<ParamType> &info) {
582 return toString(info.param.primitive);
583 }
584 };
585
586 const std::vector<PrimitiveParam> kPrimitiveParams = {
587 {CompositePrimitive::CLICK, 2}, {CompositePrimitive::THUD, 4},
588 {CompositePrimitive::SPIN, 5}, {CompositePrimitive::QUICK_RISE, 6},
589 {CompositePrimitive::SLOW_RISE, 7}, {CompositePrimitive::QUICK_FALL, 8},
590 {CompositePrimitive::LIGHT_TICK, 9}, {CompositePrimitive::LOW_TICK, 10},
591 };
592
TEST_P(PrimitiveTest,getPrimitiveDuration)593 TEST_P(PrimitiveTest, getPrimitiveDuration) {
594 auto param = GetParam();
595 auto primitive = param.primitive;
596 auto index = param.index;
597 int32_t duration;
598
599 EXPECT_EQ(EX_NONE, mVibrator->getPrimitiveDuration(primitive, &duration).getExceptionCode());
600 EXPECT_EQ(EFFECT_DURATIONS[index], duration);
601 }
602
603 INSTANTIATE_TEST_CASE_P(VibratorTests, PrimitiveTest,
604 ValuesIn(kPrimitiveParams.begin(), kPrimitiveParams.end()),
605 PrimitiveTest::PrintParam);
606
607 struct ComposeParam {
608 std::string name;
609 std::vector<CompositeEffect> composite;
610 EffectQueue queue;
611 };
612
613 class ComposeTest : public VibratorTest, public WithParamInterface<ComposeParam> {
614 public:
PrintParam(const TestParamInfo<ParamType> & info)615 static auto PrintParam(const TestParamInfo<ParamType> &info) { return info.param.name; }
616 };
617
TEST_P(ComposeTest,compose)618 TEST_P(ComposeTest, compose) {
619 auto param = GetParam();
620 auto composite = param.composite;
621 auto queue = std::get<0>(param.queue);
622 ExpectationSet eSetup;
623 Expectation eActivate, ePollHaptics, ePollStop, eEraseDone;
624 auto callback = ndk::SharedRefBase::make<MockVibratorCallback>();
625 std::promise<void> promise;
626 std::future<void> future{promise.get_future()};
627 auto complete = [&promise] {
628 promise.set_value();
629 return ndk::ScopedAStatus::ok();
630 };
631
632 eSetup += EXPECT_CALL(*mMockApi, setFFGain(_, ON_GLOBAL_SCALE))
633 .After(eSetup)
634 .WillOnce(DoDefault());
635 eSetup += EXPECT_CALL(*mMockApi, getOwtFreeSpace(_)).WillOnce(DoDefault());
636 eSetup += EXPECT_CALL(*mMockApi, uploadOwtEffect(_, _, _, _, _, _))
637 .After(eSetup)
638 .WillOnce(DoDefault());
639 eActivate = EXPECT_CALL(*mMockApi, setFFPlay(_, WAVEFORM_COMPOSE, true))
640 .After(eSetup)
641 .WillOnce(DoDefault());
642
643 ePollHaptics = EXPECT_CALL(*mMockApi, pollVibeState(1, POLLING_TIMEOUT))
644 .After(eActivate)
645 .WillOnce(DoDefault());
646 ePollStop =
647 EXPECT_CALL(*mMockApi, pollVibeState(0, -1)).After(ePollHaptics).WillOnce(DoDefault());
648 eEraseDone =
649 EXPECT_CALL(*mMockApi, eraseOwtEffect(_, _, _)).After(ePollStop).WillOnce(DoDefault());
650 EXPECT_CALL(*callback, onComplete()).After(eEraseDone).WillOnce(complete);
651
652 EXPECT_EQ(EX_NONE, mVibrator->compose(composite, callback).getExceptionCode());
653
654 EXPECT_EQ(future.wait_for(std::chrono::milliseconds(100)), std::future_status::ready);
655 }
656
657 const std::vector<ComposeParam> kComposeParams = {
658 {"click",
659 {{0, CompositePrimitive::CLICK, 1.0f}},
660 Queue(QueueEffect(2, Level(1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
661 {"thud",
662 {{1, CompositePrimitive::THUD, 0.8f}},
663 Queue(1, QueueEffect(4, Level(0.8f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
664 {"spin",
665 {{2, CompositePrimitive::SPIN, 0.6f}},
666 Queue(2, QueueEffect(5, Level(0.6f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
667 {"quick_rise",
668 {{3, CompositePrimitive::QUICK_RISE, 0.4f}},
669 Queue(3, QueueEffect(6, Level(0.4f, V_LONG_DEFAULT[0], V_LONG_DEFAULT[1])), 0)},
670 {"slow_rise",
671 {{4, CompositePrimitive::SLOW_RISE, 0.0f}},
672 Queue(4, QueueEffect(7, Level(0.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
673 {"quick_fall",
674 {{5, CompositePrimitive::QUICK_FALL, 1.0f}},
675 Queue(5, QueueEffect(8, Level(1.0f, V_LONG_DEFAULT[0], V_LONG_DEFAULT[1])), 0)},
676 {"pop",
677 {{6, CompositePrimitive::SLOW_RISE, 1.0f}, {50, CompositePrimitive::THUD, 1.0f}},
678 Queue(6, QueueEffect(7, Level(1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 50,
679 QueueEffect(4, Level(1.0f, V_CLICK_DEFAULT[0], V_CLICK_DEFAULT[1])), 0)},
680 {"snap",
681 {{7, CompositePrimitive::QUICK_RISE, 1.0f}, {0, CompositePrimitive::QUICK_FALL, 1.0f}},
682 Queue(7, QueueEffect(6, Level(1.0f, V_LONG_DEFAULT[0], V_LONG_DEFAULT[1])),
683 QueueEffect(8, Level(1.0f, V_LONG_DEFAULT[0], V_LONG_DEFAULT[1])), 0)},
684 };
685
686 INSTANTIATE_TEST_CASE_P(VibratorTests, ComposeTest,
687 ValuesIn(kComposeParams.begin(), kComposeParams.end()),
688 ComposeTest::PrintParam);
689 } // namespace vibrator
690 } // namespace hardware
691 } // namespace android
692 } // namespace aidl
693