blob: 411fe7a2fe2ce160bd4a49cd6bec9086ea1e29e7 [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
24#include <future>
25
26using android::ProcessState;
27using android::sp;
28using android::String16;
29using android::binder::Status;
30using android::hardware::vibrator::BnVibratorCallback;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090031using android::hardware::vibrator::CompositeEffect;
32using android::hardware::vibrator::CompositePrimitive;
Steven Morelandd44007e2019-10-24 18:12:46 -070033using android::hardware::vibrator::Effect;
34using android::hardware::vibrator::EffectStrength;
35using android::hardware::vibrator::IVibrator;
36
Jooyung Han716648d2019-12-17 14:17:48 +000037const std::vector<Effect> kEffects{android::enum_range<Effect>().begin(),
38 android::enum_range<Effect>().end()};
39const std::vector<EffectStrength> kEffectStrengths{android::enum_range<EffectStrength>().begin(),
40 android::enum_range<EffectStrength>().end()};
Steven Morelandd44007e2019-10-24 18:12:46 -070041
42const std::vector<Effect> kInvalidEffects = {
Steven Morelandf3353882019-11-07 17:02:43 -080043 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
44 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070045};
46
47const std::vector<EffectStrength> kInvalidEffectStrengths = {
Steven Morelandf3353882019-11-07 17:02:43 -080048 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
49 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070050};
51
Steven Moreland1c269782020-01-09 11:16:05 -080052const std::vector<CompositePrimitive> kCompositePrimitives{
53 android::enum_range<CompositePrimitive>().begin(),
54 android::enum_range<CompositePrimitive>().end()};
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090055
56const std::vector<CompositePrimitive> kInvalidPrimitives = {
57 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
58 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
59};
60
Steven Morelandd44007e2019-10-24 18:12:46 -070061class CompletionCallback : public BnVibratorCallback {
62 public:
63 CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {}
64 Status onComplete() override {
65 mCallback();
66 return Status::ok();
67 }
68
69 private:
70 std::function<void()> mCallback;
71};
72
73class VibratorAidl : public testing::TestWithParam<std::string> {
74 public:
75 virtual void SetUp() override {
76 vibrator = android::waitForDeclaredService<IVibrator>(String16(GetParam().c_str()));
77 ASSERT_NE(vibrator, nullptr);
78 ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk());
79 }
80
81 sp<IVibrator> vibrator;
82 int32_t capabilities;
83};
84
85TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
86 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
87 sleep(1);
88 EXPECT_TRUE(vibrator->off().isOk());
89}
90
91TEST_P(VibratorAidl, OnWithCallback) {
92 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
93
94 std::promise<void> completionPromise;
95 std::future<void> completionFuture{completionPromise.get_future()};
96 sp<CompletionCallback> callback =
97 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
98 uint32_t durationMs = 250;
99 std::chrono::milliseconds timeout{durationMs * 2};
100 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
101 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
102 EXPECT_TRUE(vibrator->off().isOk());
103}
104
105TEST_P(VibratorAidl, OnCallbackNotSupported) {
106 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) {
107 sp<CompletionCallback> callback = new CompletionCallback([] {});
108 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->on(250, callback).exceptionCode());
109 }
110}
111
112TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800113 std::vector<Effect> supported;
114 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
115
Steven Morelandd44007e2019-10-24 18:12:46 -0700116 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800117 bool isEffectSupported =
118 std::find(supported.begin(), supported.end(), effect) != supported.end();
119
Steven Morelandd44007e2019-10-24 18:12:46 -0700120 for (EffectStrength strength : kEffectStrengths) {
121 int32_t lengthMs = 0;
122 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800123
124 if (isEffectSupported) {
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900125 EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700126 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800127 usleep(lengthMs * 1000);
Steven Morelandd44007e2019-10-24 18:12:46 -0700128 } else {
Steven Morelandf3353882019-11-07 17:02:43 -0800129 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900130 << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700131 EXPECT_EQ(lengthMs, 0);
132 }
133 }
134 }
135}
136
137TEST_P(VibratorAidl, ValidateEffectWithCallback) {
138 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
139
Steven Moreland2932b222019-11-05 14:30:17 -0800140 std::vector<Effect> supported;
141 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
142
Steven Morelandd44007e2019-10-24 18:12:46 -0700143 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800144 bool isEffectSupported =
145 std::find(supported.begin(), supported.end(), effect) != supported.end();
146
Steven Morelandd44007e2019-10-24 18:12:46 -0700147 for (EffectStrength strength : kEffectStrengths) {
148 std::promise<void> completionPromise;
149 std::future<void> completionFuture{completionPromise.get_future()};
150 sp<CompletionCallback> callback =
151 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800152 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700153 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800154
155 if (isEffectSupported) {
156 EXPECT_TRUE(status.isOk());
157 EXPECT_GT(lengthMs, 0);
158 } else {
159 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
160 EXPECT_EQ(lengthMs, 0);
161 }
162
Steven Morelandd44007e2019-10-24 18:12:46 -0700163 if (!status.isOk()) continue;
164
165 std::chrono::milliseconds timeout{lengthMs * 2};
166 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
167 }
168 }
169}
170
171TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
172 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return;
173
174 for (Effect effect : kEffects) {
175 for (EffectStrength strength : kEffectStrengths) {
176 sp<CompletionCallback> callback = new CompletionCallback([] {});
177 int lengthMs;
178 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
179 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
180 EXPECT_EQ(lengthMs, 0);
181 }
182 }
183}
184
185TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
186 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800187 for (EffectStrength strength : kEffectStrengths) {
188 int32_t lengthMs;
189 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
190 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900191 << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800192 }
193 }
194 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700195 for (EffectStrength strength : kInvalidEffectStrengths) {
196 int32_t lengthMs;
197 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Morelandf3353882019-11-07 17:02:43 -0800198 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900199 << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700200 }
201 }
202}
203
204TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
205 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900206 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700207 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900208 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700209 sleep(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900210 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700211 sleep(1);
212 }
213}
214
215TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
216 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
217 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
218 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900219 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700220 }
221}
222
223TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
224 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
225 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode());
226 }
227}
228
229TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
230 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
231 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
232 sleep(1);
233 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
234 sleep(1);
235 }
236}
237
Steven Morelandc0b92d52019-11-05 13:31:41 -0800238TEST_P(VibratorAidl, ExternalAmplitudeControl) {
239 const bool supportsExternalAmplitudeControl =
240 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
241
242 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
243 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
244
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900245 Status amplitudeStatus = vibrator->setAmplitude(0.5);
Steven Morelandc0b92d52019-11-05 13:31:41 -0800246 if (supportsExternalAmplitudeControl) {
247 EXPECT_TRUE(amplitudeStatus.isOk());
248 } else {
249 EXPECT_EQ(amplitudeStatus.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
250 }
251 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
252 } else {
253 EXPECT_FALSE(supportsExternalAmplitudeControl);
254 }
255}
256
Steven Morelandd44007e2019-10-24 18:12:46 -0700257TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
258 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
259 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
260 vibrator->setExternalControl(true).exceptionCode());
261 }
262}
263
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900264TEST_P(VibratorAidl, GetSupportedPrimitives) {
265 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
266 std::vector<CompositePrimitive> supported;
267
268 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
269
270 std::sort(supported.begin(), supported.end());
271
272 EXPECT_EQ(kCompositePrimitives, supported);
273 }
274}
275
276TEST_P(VibratorAidl, GetPrimitiveDuration) {
277 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
278 int32_t duration;
279
280 for (auto primitive : kCompositePrimitives) {
281 EXPECT_EQ(Status::EX_NONE,
282 vibrator->getPrimitiveDuration(primitive, &duration).exceptionCode());
283 }
284 }
285}
286
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900287TEST_P(VibratorAidl, ComposeValidPrimitives) {
288 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
289 int32_t maxDelay, maxSize;
290
291 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
292 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
293
294 std::vector<CompositeEffect> composite;
295
296 for (auto primitive : kCompositePrimitives) {
297 CompositeEffect effect;
298
299 effect.delayMs = std::rand() % (maxDelay + 1);
300 effect.primitive = primitive;
301 effect.scale = static_cast<float>(std::rand()) / RAND_MAX ?: 1.0f;
302 composite.emplace_back(effect);
303
304 if (composite.size() == maxSize) {
305 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
306 composite.clear();
307 vibrator->off();
308 }
309 }
310
311 if (composite.size() != 0) {
312 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
313 vibrator->off();
314 }
315 }
316}
317
318TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
319 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
320 for (auto primitive : kInvalidPrimitives) {
321 std::vector<CompositeEffect> composite(1);
322
323 for (auto& effect : composite) {
324 effect.delayMs = 0;
325 effect.primitive = primitive;
326 effect.scale = 1.0f;
327 }
328 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
329 vibrator->compose(composite, nullptr).exceptionCode());
330 vibrator->off();
331 }
332 }
333}
334
335TEST_P(VibratorAidl, CompseDelayBoundary) {
336 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
337 int32_t maxDelay;
338
339 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
340
341 std::vector<CompositeEffect> composite(1);
342 CompositeEffect effect;
343
344 effect.delayMs = 1;
345 effect.primitive = CompositePrimitive::CLICK;
346 effect.scale = 1.0f;
347
348 std::fill(composite.begin(), composite.end(), effect);
349 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
350
351 effect.delayMs = maxDelay + 1;
352
353 std::fill(composite.begin(), composite.end(), effect);
354 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
355 vibrator->compose(composite, nullptr).exceptionCode());
356 vibrator->off();
357 }
358}
359
360TEST_P(VibratorAidl, CompseSizeBoundary) {
361 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
362 int32_t maxSize;
363
364 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
365
366 std::vector<CompositeEffect> composite(maxSize);
367 CompositeEffect effect;
368
369 effect.delayMs = 1;
370 effect.primitive = CompositePrimitive::CLICK;
371 effect.scale = 1.0f;
372
373 std::fill(composite.begin(), composite.end(), effect);
374 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
375
376 composite.emplace_back(effect);
377 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
378 vibrator->compose(composite, nullptr).exceptionCode());
379 vibrator->off();
380 }
381}
382
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900383TEST_P(VibratorAidl, ComposeCallback) {
384 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
385 std::promise<void> completionPromise;
386 std::future<void> completionFuture{completionPromise.get_future()};
387 sp<CompletionCallback> callback =
388 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
389 CompositePrimitive primitive = CompositePrimitive::CLICK;
390 CompositeEffect effect;
391 std::vector<CompositeEffect> composite;
392 int32_t duration;
393
394 effect.delayMs = 0;
395 effect.primitive = primitive;
396 effect.scale = 1.0f;
397 composite.emplace_back(effect);
398
399 EXPECT_EQ(Status::EX_NONE,
400 vibrator->getPrimitiveDuration(primitive, &duration).exceptionCode());
401 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode());
402 EXPECT_EQ(completionFuture.wait_for(std::chrono::milliseconds(duration * 2)),
403 std::future_status::ready);
404 }
405}
406
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900407TEST_P(VibratorAidl, AlwaysOn) {
408 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
409 std::vector<Effect> supported;
410 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
411
412 for (Effect effect : kEffects) {
413 bool isEffectSupported =
414 std::find(supported.begin(), supported.end(), effect) != supported.end();
415
416 for (EffectStrength strength : kEffectStrengths) {
417 Status status = vibrator->alwaysOnEnable(0, effect, strength);
418
419 if (isEffectSupported) {
420 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
421 << toString(effect) << " " << toString(strength);
422 } else {
423 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode())
424 << toString(effect) << " " << toString(strength);
425 }
426 }
427 }
428
429 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
430 }
431}
432
Haibo Huang83b4c1e2019-11-08 11:59:40 -0800433INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl,
Steven Morelandd44007e2019-10-24 18:12:46 -0700434 testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)),
435 android::PrintInstanceNameToString);
436
437int main(int argc, char** argv) {
438 ::testing::InitGoogleTest(&argc, argv);
439 ProcessState::self()->setThreadPoolMaxThreadCount(1);
440 ProcessState::self()->startThreadPool();
441 return RUN_ALL_TESTS();
442}