blob: 8b16025b5d692c88df2f6ed6e8e34790d85bc652 [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 }
132 }
133 }
134}
135
136TEST_P(VibratorAidl, ValidateEffectWithCallback) {
137 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;
138
Steven Moreland2932b222019-11-05 14:30:17 -0800139 std::vector<Effect> supported;
140 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
141
Steven Morelandd44007e2019-10-24 18:12:46 -0700142 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800143 bool isEffectSupported =
144 std::find(supported.begin(), supported.end(), effect) != supported.end();
145
Steven Morelandd44007e2019-10-24 18:12:46 -0700146 for (EffectStrength strength : kEffectStrengths) {
147 std::promise<void> completionPromise;
148 std::future<void> completionFuture{completionPromise.get_future()};
149 sp<CompletionCallback> callback =
150 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800151 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700152 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800153
154 if (isEffectSupported) {
155 EXPECT_TRUE(status.isOk());
156 EXPECT_GT(lengthMs, 0);
157 } else {
158 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
Steven Moreland2932b222019-11-05 14:30:17 -0800159 }
160
Steven Morelandd44007e2019-10-24 18:12:46 -0700161 if (!status.isOk()) continue;
162
163 std::chrono::milliseconds timeout{lengthMs * 2};
164 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
165 }
166 }
167}
168
169TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
170 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return;
171
172 for (Effect effect : kEffects) {
173 for (EffectStrength strength : kEffectStrengths) {
174 sp<CompletionCallback> callback = new CompletionCallback([] {});
175 int lengthMs;
176 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
177 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700178 }
179 }
180}
181
182TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
183 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800184 for (EffectStrength strength : kEffectStrengths) {
185 int32_t lengthMs;
186 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
187 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900188 << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800189 }
190 }
191 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700192 for (EffectStrength strength : kInvalidEffectStrengths) {
193 int32_t lengthMs;
194 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Morelandf3353882019-11-07 17:02:43 -0800195 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900196 << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700197 }
198 }
199}
200
201TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
202 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900203 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700204 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900205 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700206 sleep(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900207 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700208 sleep(1);
209 }
210}
211
212TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
213 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
214 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
215 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900216 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700217 }
218}
219
220TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
221 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
222 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode());
223 }
224}
225
226TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
227 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
228 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
229 sleep(1);
230 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
231 sleep(1);
232 }
233}
234
Steven Morelandc0b92d52019-11-05 13:31:41 -0800235TEST_P(VibratorAidl, ExternalAmplitudeControl) {
236 const bool supportsExternalAmplitudeControl =
237 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
238
239 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
240 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
241
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900242 Status amplitudeStatus = vibrator->setAmplitude(0.5);
Steven Morelandc0b92d52019-11-05 13:31:41 -0800243 if (supportsExternalAmplitudeControl) {
244 EXPECT_TRUE(amplitudeStatus.isOk());
245 } else {
246 EXPECT_EQ(amplitudeStatus.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
247 }
248 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
249 } else {
250 EXPECT_FALSE(supportsExternalAmplitudeControl);
251 }
252}
253
Steven Morelandd44007e2019-10-24 18:12:46 -0700254TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
255 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
256 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
257 vibrator->setExternalControl(true).exceptionCode());
258 }
259}
260
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900261TEST_P(VibratorAidl, GetSupportedPrimitives) {
262 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
263 std::vector<CompositePrimitive> supported;
264
265 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
266
267 std::sort(supported.begin(), supported.end());
268
269 EXPECT_EQ(kCompositePrimitives, supported);
270 }
271}
272
273TEST_P(VibratorAidl, GetPrimitiveDuration) {
274 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
275 int32_t duration;
276
277 for (auto primitive : kCompositePrimitives) {
278 EXPECT_EQ(Status::EX_NONE,
279 vibrator->getPrimitiveDuration(primitive, &duration).exceptionCode());
280 }
281 }
282}
283
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900284TEST_P(VibratorAidl, ComposeValidPrimitives) {
285 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
286 int32_t maxDelay, maxSize;
287
288 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
289 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
290
291 std::vector<CompositeEffect> composite;
292
293 for (auto primitive : kCompositePrimitives) {
294 CompositeEffect effect;
295
296 effect.delayMs = std::rand() % (maxDelay + 1);
297 effect.primitive = primitive;
298 effect.scale = static_cast<float>(std::rand()) / RAND_MAX ?: 1.0f;
299 composite.emplace_back(effect);
300
301 if (composite.size() == maxSize) {
302 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
303 composite.clear();
304 vibrator->off();
305 }
306 }
307
308 if (composite.size() != 0) {
309 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
310 vibrator->off();
311 }
312 }
313}
314
315TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
316 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
317 for (auto primitive : kInvalidPrimitives) {
318 std::vector<CompositeEffect> composite(1);
319
320 for (auto& effect : composite) {
321 effect.delayMs = 0;
322 effect.primitive = primitive;
323 effect.scale = 1.0f;
324 }
325 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
326 vibrator->compose(composite, nullptr).exceptionCode());
327 vibrator->off();
328 }
329 }
330}
331
332TEST_P(VibratorAidl, CompseDelayBoundary) {
333 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
334 int32_t maxDelay;
335
336 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
337
338 std::vector<CompositeEffect> composite(1);
339 CompositeEffect effect;
340
341 effect.delayMs = 1;
342 effect.primitive = CompositePrimitive::CLICK;
343 effect.scale = 1.0f;
344
345 std::fill(composite.begin(), composite.end(), effect);
346 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
347
348 effect.delayMs = maxDelay + 1;
349
350 std::fill(composite.begin(), composite.end(), effect);
351 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
352 vibrator->compose(composite, nullptr).exceptionCode());
353 vibrator->off();
354 }
355}
356
357TEST_P(VibratorAidl, CompseSizeBoundary) {
358 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
359 int32_t maxSize;
360
361 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
362
363 std::vector<CompositeEffect> composite(maxSize);
364 CompositeEffect effect;
365
366 effect.delayMs = 1;
367 effect.primitive = CompositePrimitive::CLICK;
368 effect.scale = 1.0f;
369
370 std::fill(composite.begin(), composite.end(), effect);
371 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
372
373 composite.emplace_back(effect);
374 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
375 vibrator->compose(composite, nullptr).exceptionCode());
376 vibrator->off();
377 }
378}
379
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900380TEST_P(VibratorAidl, ComposeCallback) {
381 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
382 std::promise<void> completionPromise;
383 std::future<void> completionFuture{completionPromise.get_future()};
384 sp<CompletionCallback> callback =
385 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
386 CompositePrimitive primitive = CompositePrimitive::CLICK;
387 CompositeEffect effect;
388 std::vector<CompositeEffect> composite;
389 int32_t duration;
390
391 effect.delayMs = 0;
392 effect.primitive = primitive;
393 effect.scale = 1.0f;
394 composite.emplace_back(effect);
395
396 EXPECT_EQ(Status::EX_NONE,
397 vibrator->getPrimitiveDuration(primitive, &duration).exceptionCode());
398 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode());
399 EXPECT_EQ(completionFuture.wait_for(std::chrono::milliseconds(duration * 2)),
400 std::future_status::ready);
401 }
402}
403
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900404TEST_P(VibratorAidl, AlwaysOn) {
405 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
406 std::vector<Effect> supported;
407 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
408
409 for (Effect effect : kEffects) {
410 bool isEffectSupported =
411 std::find(supported.begin(), supported.end(), effect) != supported.end();
412
413 for (EffectStrength strength : kEffectStrengths) {
414 Status status = vibrator->alwaysOnEnable(0, effect, strength);
415
416 if (isEffectSupported) {
417 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
418 << toString(effect) << " " << toString(strength);
419 } else {
420 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode())
421 << toString(effect) << " " << toString(strength);
422 }
423 }
424 }
425
426 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
427 }
428}
429
Haibo Huang83b4c1e2019-11-08 11:59:40 -0800430INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl,
Steven Morelandd44007e2019-10-24 18:12:46 -0700431 testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)),
432 android::PrintInstanceNameToString);
433
434int main(int argc, char** argv) {
435 ::testing::InitGoogleTest(&argc, argv);
436 ProcessState::self()->setThreadPoolMaxThreadCount(1);
437 ProcessState::self()->startThreadPool();
438 return RUN_ALL_TESTS();
439}