blob: f197763be3a0ca81aa0f49219904a8917f5560c0 [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\" Sanghaf4de5b02019-10-23 09:25:52 +0900264TEST_P(VibratorAidl, ComposeValidPrimitives) {
265 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
266 int32_t maxDelay, maxSize;
267
268 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
269 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
270
271 std::vector<CompositeEffect> composite;
272
273 for (auto primitive : kCompositePrimitives) {
274 CompositeEffect effect;
275
276 effect.delayMs = std::rand() % (maxDelay + 1);
277 effect.primitive = primitive;
278 effect.scale = static_cast<float>(std::rand()) / RAND_MAX ?: 1.0f;
279 composite.emplace_back(effect);
280
281 if (composite.size() == maxSize) {
282 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
283 composite.clear();
284 vibrator->off();
285 }
286 }
287
288 if (composite.size() != 0) {
289 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
290 vibrator->off();
291 }
292 }
293}
294
295TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
296 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
297 for (auto primitive : kInvalidPrimitives) {
298 std::vector<CompositeEffect> composite(1);
299
300 for (auto& effect : composite) {
301 effect.delayMs = 0;
302 effect.primitive = primitive;
303 effect.scale = 1.0f;
304 }
305 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
306 vibrator->compose(composite, nullptr).exceptionCode());
307 vibrator->off();
308 }
309 }
310}
311
312TEST_P(VibratorAidl, CompseDelayBoundary) {
313 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
314 int32_t maxDelay;
315
316 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
317
318 std::vector<CompositeEffect> composite(1);
319 CompositeEffect effect;
320
321 effect.delayMs = 1;
322 effect.primitive = CompositePrimitive::CLICK;
323 effect.scale = 1.0f;
324
325 std::fill(composite.begin(), composite.end(), effect);
326 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
327
328 effect.delayMs = maxDelay + 1;
329
330 std::fill(composite.begin(), composite.end(), effect);
331 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
332 vibrator->compose(composite, nullptr).exceptionCode());
333 vibrator->off();
334 }
335}
336
337TEST_P(VibratorAidl, CompseSizeBoundary) {
338 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
339 int32_t maxSize;
340
341 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
342
343 std::vector<CompositeEffect> composite(maxSize);
344 CompositeEffect effect;
345
346 effect.delayMs = 1;
347 effect.primitive = CompositePrimitive::CLICK;
348 effect.scale = 1.0f;
349
350 std::fill(composite.begin(), composite.end(), effect);
351 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
352
353 composite.emplace_back(effect);
354 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
355 vibrator->compose(composite, nullptr).exceptionCode());
356 vibrator->off();
357 }
358}
359
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900360TEST_P(VibratorAidl, AlwaysOn) {
361 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
362 std::vector<Effect> supported;
363 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
364
365 for (Effect effect : kEffects) {
366 bool isEffectSupported =
367 std::find(supported.begin(), supported.end(), effect) != supported.end();
368
369 for (EffectStrength strength : kEffectStrengths) {
370 Status status = vibrator->alwaysOnEnable(0, effect, strength);
371
372 if (isEffectSupported) {
373 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
374 << toString(effect) << " " << toString(strength);
375 } else {
376 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode())
377 << toString(effect) << " " << toString(strength);
378 }
379 }
380 }
381
382 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
383 }
384}
385
Haibo Huang83b4c1e2019-11-08 11:59:40 -0800386INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl,
Steven Morelandd44007e2019-10-24 18:12:46 -0700387 testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)),
388 android::PrintInstanceNameToString);
389
390int main(int argc, char** argv) {
391 ::testing::InitGoogleTest(&argc, argv);
392 ProcessState::self()->setThreadPoolMaxThreadCount(1);
393 ProcessState::self()->startThreadPool();
394 return RUN_ALL_TESTS();
395}