blob: e51f5947d82be9ebc7bbe537f706ed09e6a4c452 [file] [log] [blame]
Steven Morelandd44007e2019-10-24 18:12:46 -07001/*
2 * Copyright (C) 2019 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#include <aidl/Gtest.h>
17#include <aidl/Vintf.h>
18
19#include <android/hardware/vibrator/BnVibratorCallback.h>
20#include <android/hardware/vibrator/IVibrator.h>
21#include <binder/IServiceManager.h>
22#include <binder/ProcessState.h>
23
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090024#include <cmath>
Steven Morelandd44007e2019-10-24 18:12:46 -070025#include <future>
26
27using android::ProcessState;
28using android::sp;
29using android::String16;
30using android::binder::Status;
31using android::hardware::vibrator::BnVibratorCallback;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090032using android::hardware::vibrator::CompositeEffect;
33using android::hardware::vibrator::CompositePrimitive;
Steven Morelandd44007e2019-10-24 18:12:46 -070034using android::hardware::vibrator::Effect;
35using android::hardware::vibrator::EffectStrength;
36using android::hardware::vibrator::IVibrator;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +090037using std::chrono::high_resolution_clock;
Steven Morelandd44007e2019-10-24 18:12:46 -070038
Jooyung Han716648d2019-12-17 14:17:48 +000039const std::vector<Effect> kEffects{android::enum_range<Effect>().begin(),
40 android::enum_range<Effect>().end()};
41const std::vector<EffectStrength> kEffectStrengths{android::enum_range<EffectStrength>().begin(),
42 android::enum_range<EffectStrength>().end()};
Steven Morelandd44007e2019-10-24 18:12:46 -070043
44const std::vector<Effect> kInvalidEffects = {
Steven Morelandf3353882019-11-07 17:02:43 -080045 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
46 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070047};
48
49const std::vector<EffectStrength> kInvalidEffectStrengths = {
Steven Morelandf3353882019-11-07 17:02:43 -080050 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
51 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070052};
53
Steven Moreland1c269782020-01-09 11:16:05 -080054const std::vector<CompositePrimitive> kCompositePrimitives{
55 android::enum_range<CompositePrimitive>().begin(),
56 android::enum_range<CompositePrimitive>().end()};
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090057
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090058const std::vector<CompositePrimitive> kOptionalPrimitives = {
59 CompositePrimitive::THUD,
60 CompositePrimitive::SPIN,
61};
62
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090063const std::vector<CompositePrimitive> kInvalidPrimitives = {
64 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
65 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
66};
67
Steven Morelandd44007e2019-10-24 18:12:46 -070068class CompletionCallback : public BnVibratorCallback {
69 public:
70 CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {}
71 Status onComplete() override {
72 mCallback();
73 return Status::ok();
74 }
75
76 private:
77 std::function<void()> mCallback;
78};
79
80class VibratorAidl : public testing::TestWithParam<std::string> {
81 public:
82 virtual void SetUp() override {
83 vibrator = android::waitForDeclaredService<IVibrator>(String16(GetParam().c_str()));
84 ASSERT_NE(vibrator, nullptr);
85 ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk());
86 }
87
88 sp<IVibrator> vibrator;
89 int32_t capabilities;
90};
91
92TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
93 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
94 sleep(1);
95 EXPECT_TRUE(vibrator->off().isOk());
96}
97
98TEST_P(VibratorAidl, OnWithCallback) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +080099 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700100
101 std::promise<void> completionPromise;
102 std::future<void> completionFuture{completionPromise.get_future()};
103 sp<CompletionCallback> callback =
104 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
105 uint32_t durationMs = 250;
106 std::chrono::milliseconds timeout{durationMs * 2};
107 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
108 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
109 EXPECT_TRUE(vibrator->off().isOk());
110}
111
112TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800113 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700114 sp<CompletionCallback> callback = new CompletionCallback([] {});
115 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->on(250, callback).exceptionCode());
116 }
117}
118
119TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800120 std::vector<Effect> supported;
121 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
122
Steven Morelandd44007e2019-10-24 18:12:46 -0700123 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800124 bool isEffectSupported =
125 std::find(supported.begin(), supported.end(), effect) != supported.end();
126
Steven Morelandd44007e2019-10-24 18:12:46 -0700127 for (EffectStrength strength : kEffectStrengths) {
128 int32_t lengthMs = 0;
129 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800130
131 if (isEffectSupported) {
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900132 EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700133 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800134 usleep(lengthMs * 1000);
Steven Morelandd44007e2019-10-24 18:12:46 -0700135 } else {
Steven Morelandf3353882019-11-07 17:02:43 -0800136 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900137 << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700138 }
139 }
140 }
141}
142
143TEST_P(VibratorAidl, ValidateEffectWithCallback) {
144 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
145
Steven Moreland2932b222019-11-05 14:30:17 -0800146 std::vector<Effect> supported;
147 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
148
Steven Morelandd44007e2019-10-24 18:12:46 -0700149 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800150 bool isEffectSupported =
151 std::find(supported.begin(), supported.end(), effect) != supported.end();
152
Steven Morelandd44007e2019-10-24 18:12:46 -0700153 for (EffectStrength strength : kEffectStrengths) {
154 std::promise<void> completionPromise;
155 std::future<void> completionFuture{completionPromise.get_future()};
156 sp<CompletionCallback> callback =
157 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800158 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700159 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800160
161 if (isEffectSupported) {
162 EXPECT_TRUE(status.isOk());
163 EXPECT_GT(lengthMs, 0);
164 } else {
165 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
Steven Moreland2932b222019-11-05 14:30:17 -0800166 }
167
Steven Morelandd44007e2019-10-24 18:12:46 -0700168 if (!status.isOk()) continue;
169
170 std::chrono::milliseconds timeout{lengthMs * 2};
171 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
172 }
173 }
174}
175
176TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
177 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return;
178
179 for (Effect effect : kEffects) {
180 for (EffectStrength strength : kEffectStrengths) {
181 sp<CompletionCallback> callback = new CompletionCallback([] {});
182 int lengthMs;
183 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
184 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700185 }
186 }
187}
188
189TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
190 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800191 for (EffectStrength strength : kEffectStrengths) {
192 int32_t lengthMs;
193 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
194 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900195 << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800196 }
197 }
198 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700199 for (EffectStrength strength : kInvalidEffectStrengths) {
200 int32_t lengthMs;
201 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Morelandf3353882019-11-07 17:02:43 -0800202 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900203 << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700204 }
205 }
206}
207
208TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
209 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900210 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700211 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900212 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700213 sleep(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900214 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700215 sleep(1);
216 }
217}
218
219TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
220 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
221 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
222 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900223 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700224 }
225}
226
227TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
228 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
229 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode());
230 }
231}
232
233TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
234 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
235 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
236 sleep(1);
237 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
238 sleep(1);
239 }
240}
241
Steven Morelandc0b92d52019-11-05 13:31:41 -0800242TEST_P(VibratorAidl, ExternalAmplitudeControl) {
243 const bool supportsExternalAmplitudeControl =
244 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
245
246 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
247 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
248
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900249 Status amplitudeStatus = vibrator->setAmplitude(0.5);
Steven Morelandc0b92d52019-11-05 13:31:41 -0800250 if (supportsExternalAmplitudeControl) {
251 EXPECT_TRUE(amplitudeStatus.isOk());
252 } else {
253 EXPECT_EQ(amplitudeStatus.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
254 }
255 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
256 } else {
257 EXPECT_FALSE(supportsExternalAmplitudeControl);
258 }
259}
260
Steven Morelandd44007e2019-10-24 18:12:46 -0700261TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
262 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
263 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
264 vibrator->setExternalControl(true).exceptionCode());
265 }
266}
267
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900268TEST_P(VibratorAidl, GetSupportedPrimitives) {
269 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
270 std::vector<CompositePrimitive> supported;
271
272 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
273
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900274 for (auto primitive : kCompositePrimitives) {
275 bool isPrimitiveSupported =
276 std::find(supported.begin(), supported.end(), primitive) != supported.end();
277 bool isPrimitiveOptional =
278 std::find(kOptionalPrimitives.begin(), kOptionalPrimitives.end(), primitive) !=
279 kOptionalPrimitives.end();
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900280
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900281 EXPECT_TRUE(isPrimitiveSupported || isPrimitiveOptional) << toString(primitive);
282 }
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900283 }
284}
285
286TEST_P(VibratorAidl, GetPrimitiveDuration) {
287 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900288 std::vector<CompositePrimitive> supported;
289 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900290
291 for (auto primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900292 bool isPrimitiveSupported =
293 std::find(supported.begin(), supported.end(), primitive) != supported.end();
294 int32_t duration;
295
296 Status status = vibrator->getPrimitiveDuration(primitive, &duration);
297
298 if (isPrimitiveSupported) {
299 EXPECT_EQ(Status::EX_NONE, status.exceptionCode());
300 } else {
301 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
302 }
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900303 }
304 }
305}
306
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900307TEST_P(VibratorAidl, ComposeValidPrimitives) {
308 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900309 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900310 int32_t maxDelay, maxSize;
311
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900312 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900313 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
314 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
315
316 std::vector<CompositeEffect> composite;
317
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900318 for (auto primitive : supported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900319 CompositeEffect effect;
320
321 effect.delayMs = std::rand() % (maxDelay + 1);
322 effect.primitive = primitive;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900323 effect.scale = static_cast<float>(std::rand()) / RAND_MAX;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900324 composite.emplace_back(effect);
325
326 if (composite.size() == maxSize) {
327 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
328 composite.clear();
329 vibrator->off();
330 }
331 }
332
333 if (composite.size() != 0) {
334 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
335 vibrator->off();
336 }
337 }
338}
339
340TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
341 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900342 auto unsupported = kInvalidPrimitives;
343 std::vector<CompositePrimitive> supported;
344
345 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
346
347 for (auto primitive : kCompositePrimitives) {
348 bool isPrimitiveSupported =
349 std::find(supported.begin(), supported.end(), primitive) != supported.end();
350
351 if (!isPrimitiveSupported) {
352 unsupported.push_back(primitive);
353 }
354 }
355
356 for (auto primitive : unsupported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900357 std::vector<CompositeEffect> composite(1);
358
359 for (auto& effect : composite) {
360 effect.delayMs = 0;
361 effect.primitive = primitive;
362 effect.scale = 1.0f;
363 }
364 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
365 vibrator->compose(composite, nullptr).exceptionCode());
366 vibrator->off();
367 }
368 }
369}
370
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900371TEST_P(VibratorAidl, ComposeScaleBoundary) {
372 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
373 std::vector<CompositeEffect> composite(1);
374 CompositeEffect& effect = composite[0];
375
376 effect.delayMs = 0;
377 effect.primitive = CompositePrimitive::CLICK;
378
379 effect.scale = std::nextafter(0.0f, -1.0f);
380 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
381 vibrator->compose(composite, nullptr).exceptionCode());
382
383 effect.scale = 0.0f;
384 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
385
386 effect.scale = 1.0f;
387 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
388
389 effect.scale = std::nextafter(1.0f, 2.0f);
390 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
391 vibrator->compose(composite, nullptr).exceptionCode());
392
393 vibrator->off();
394 }
395}
396
397TEST_P(VibratorAidl, ComposeDelayBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900398 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
399 int32_t maxDelay;
400
401 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
402
403 std::vector<CompositeEffect> composite(1);
404 CompositeEffect effect;
405
406 effect.delayMs = 1;
407 effect.primitive = CompositePrimitive::CLICK;
408 effect.scale = 1.0f;
409
410 std::fill(composite.begin(), composite.end(), effect);
411 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
412
413 effect.delayMs = maxDelay + 1;
414
415 std::fill(composite.begin(), composite.end(), effect);
416 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
417 vibrator->compose(composite, nullptr).exceptionCode());
418 vibrator->off();
419 }
420}
421
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900422TEST_P(VibratorAidl, ComposeSizeBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900423 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
424 int32_t maxSize;
425
426 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
427
428 std::vector<CompositeEffect> composite(maxSize);
429 CompositeEffect effect;
430
431 effect.delayMs = 1;
432 effect.primitive = CompositePrimitive::CLICK;
433 effect.scale = 1.0f;
434
435 std::fill(composite.begin(), composite.end(), effect);
436 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
437
438 composite.emplace_back(effect);
439 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
440 vibrator->compose(composite, nullptr).exceptionCode());
441 vibrator->off();
442 }
443}
444
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900445TEST_P(VibratorAidl, ComposeCallback) {
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900446 constexpr std::chrono::milliseconds allowedLatency{10};
447
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900448 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900449 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900450
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900451 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900452
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900453 for (auto primitive : supported) {
454 if (primitive == CompositePrimitive::NOOP) {
455 continue;
456 }
457
458 std::promise<void> completionPromise;
459 std::future<void> completionFuture{completionPromise.get_future()};
460 sp<CompletionCallback> callback =
461 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
462 CompositeEffect effect;
463 std::vector<CompositeEffect> composite;
464 int32_t durationMs;
465 std::chrono::milliseconds duration;
466 std::chrono::time_point<high_resolution_clock> start, end;
467 std::chrono::milliseconds elapsed;
468
469 effect.delayMs = 0;
470 effect.primitive = primitive;
471 effect.scale = 1.0f;
472 composite.emplace_back(effect);
473
474 EXPECT_EQ(Status::EX_NONE,
475 vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode())
476 << toString(primitive);
477 duration = std::chrono::milliseconds(durationMs);
478
479 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
480 << toString(primitive);
481 start = high_resolution_clock::now();
482
483 EXPECT_EQ(completionFuture.wait_for(duration + allowedLatency),
484 std::future_status::ready)
485 << toString(primitive);
486 end = high_resolution_clock::now();
487
488 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
489 EXPECT_LE(elapsed.count(), (duration + allowedLatency).count()) << toString(primitive);
490 EXPECT_GE(elapsed.count(), (duration - allowedLatency).count()) << toString(primitive);
491 }
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900492 }
493}
494
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900495TEST_P(VibratorAidl, AlwaysOn) {
496 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
497 std::vector<Effect> supported;
498 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
499
500 for (Effect effect : kEffects) {
501 bool isEffectSupported =
502 std::find(supported.begin(), supported.end(), effect) != supported.end();
503
504 for (EffectStrength strength : kEffectStrengths) {
505 Status status = vibrator->alwaysOnEnable(0, effect, strength);
506
507 if (isEffectSupported) {
508 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
509 << toString(effect) << " " << toString(strength);
510 } else {
511 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode())
512 << toString(effect) << " " << toString(strength);
513 }
514 }
515 }
516
517 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
518 }
519}
520
Dan Shiba4d5322020-07-28 13:09:30 -0700521GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Haibo Huang83b4c1e2019-11-08 11:59:40 -0800522INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl,
Steven Morelandd44007e2019-10-24 18:12:46 -0700523 testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)),
524 android::PrintInstanceNameToString);
525
526int main(int argc, char** argv) {
527 ::testing::InitGoogleTest(&argc, argv);
528 ProcessState::self()->setThreadPoolMaxThreadCount(1);
529 ProcessState::self()->startThreadPool();
530 return RUN_ALL_TESTS();
531}