blob: 6c6846f636c9d9baa37a05e403aab52eb4616b6a [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>
Steven Morelandd44007e2019-10-24 18:12:46 -070018#include <android/hardware/vibrator/BnVibratorCallback.h>
19#include <android/hardware/vibrator/IVibrator.h>
Lais Andrade80b18612020-10-12 18:44:40 +000020#include <android/hardware/vibrator/IVibratorManager.h>
Steven Morelandd44007e2019-10-24 18:12:46 -070021#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;
Vince Leung823cf5f2021-02-11 02:21:57 +000031using android::hardware::vibrator::ActivePwle;
Steven Morelandd44007e2019-10-24 18:12:46 -070032using android::hardware::vibrator::BnVibratorCallback;
Vince Leung823cf5f2021-02-11 02:21:57 +000033using android::hardware::vibrator::Braking;
34using android::hardware::vibrator::BrakingPwle;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090035using android::hardware::vibrator::CompositeEffect;
36using android::hardware::vibrator::CompositePrimitive;
Steven Morelandd44007e2019-10-24 18:12:46 -070037using android::hardware::vibrator::Effect;
38using android::hardware::vibrator::EffectStrength;
39using android::hardware::vibrator::IVibrator;
Lais Andrade80b18612020-10-12 18:44:40 +000040using android::hardware::vibrator::IVibratorManager;
Vince Leung823cf5f2021-02-11 02:21:57 +000041using android::hardware::vibrator::PrimitivePwle;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +090042using std::chrono::high_resolution_clock;
Steven Morelandd44007e2019-10-24 18:12:46 -070043
Jooyung Han716648d2019-12-17 14:17:48 +000044const std::vector<Effect> kEffects{android::enum_range<Effect>().begin(),
45 android::enum_range<Effect>().end()};
46const std::vector<EffectStrength> kEffectStrengths{android::enum_range<EffectStrength>().begin(),
47 android::enum_range<EffectStrength>().end()};
Steven Morelandd44007e2019-10-24 18:12:46 -070048
49const std::vector<Effect> kInvalidEffects = {
Vince Leung823cf5f2021-02-11 02:21:57 +000050 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
51 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070052};
53
54const std::vector<EffectStrength> kInvalidEffectStrengths = {
Vince Leung823cf5f2021-02-11 02:21:57 +000055 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
56 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070057};
58
Steven Moreland1c269782020-01-09 11:16:05 -080059const std::vector<CompositePrimitive> kCompositePrimitives{
Vince Leung823cf5f2021-02-11 02:21:57 +000060 android::enum_range<CompositePrimitive>().begin(),
61 android::enum_range<CompositePrimitive>().end()};
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090062
Lais Andrade7e643772021-07-09 14:59:44 +010063const std::vector<CompositePrimitive> kRequiredPrimitives = {
64 CompositePrimitive::CLICK, CompositePrimitive::LIGHT_TICK,
65 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
66 CompositePrimitive::QUICK_FALL,
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090067};
68
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090069const std::vector<CompositePrimitive> kInvalidPrimitives = {
Vince Leung823cf5f2021-02-11 02:21:57 +000070 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
71 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090072};
73
Steven Morelandd44007e2019-10-24 18:12:46 -070074class CompletionCallback : public BnVibratorCallback {
75 public:
Vince Leung823cf5f2021-02-11 02:21:57 +000076 CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {}
Steven Morelandd44007e2019-10-24 18:12:46 -070077 Status onComplete() override {
78 mCallback();
79 return Status::ok();
80 }
81
82 private:
83 std::function<void()> mCallback;
84};
85
Lais Andrade80b18612020-10-12 18:44:40 +000086class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> {
Steven Morelandd44007e2019-10-24 18:12:46 -070087 public:
88 virtual void SetUp() override {
Lais Andrade80b18612020-10-12 18:44:40 +000089 int32_t managerIdx = std::get<0>(GetParam());
90 int32_t vibratorId = std::get<1>(GetParam());
91 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
92
93 if (managerIdx < 0) {
94 // Testing a unmanaged vibrator, using vibratorId as index from registered HALs
95 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
96 ASSERT_LT(vibratorId, vibratorAidlNames.size());
97 auto vibratorName = String16(vibratorAidlNames[vibratorId].c_str());
98 vibrator = android::waitForDeclaredService<IVibrator>(vibratorName);
99 } else {
100 // Testing a managed vibrator, using vibratorId to retrieve it from the manager
101 ASSERT_LT(managerIdx, managerAidlNames.size());
102 auto managerName = String16(managerAidlNames[managerIdx].c_str());
103 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
104 auto vibratorResult = vibratorManager->getVibrator(vibratorId, &vibrator);
105 ASSERT_TRUE(vibratorResult.isOk());
106 }
107
Steven Morelandd44007e2019-10-24 18:12:46 -0700108 ASSERT_NE(vibrator, nullptr);
109 ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk());
110 }
111
Lais Andrade38d054e2024-02-09 12:42:09 +0000112 virtual void TearDown() override {
113 // Reset vibrator state between tests.
114 EXPECT_TRUE(vibrator->off().isOk());
115 }
116
Steven Morelandd44007e2019-10-24 18:12:46 -0700117 sp<IVibrator> vibrator;
118 int32_t capabilities;
119};
120
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100121inline bool isUnknownOrUnsupported(Status status) {
122 return status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
123 status.transactionError() == android::UNKNOWN_TRANSACTION;
124}
125
Vince Leung823cf5f2021-02-11 02:21:57 +0000126static float getResonantFrequencyHz(sp<IVibrator> vibrator, int32_t capabilities) {
127 float resonantFrequencyHz;
128 Status status = vibrator->getResonantFrequency(&resonantFrequencyHz);
129 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
130 EXPECT_GT(resonantFrequencyHz, 0);
131 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
132 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100133 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000134 }
135 return resonantFrequencyHz;
136}
137
138static float getFrequencyResolutionHz(sp<IVibrator> vibrator, int32_t capabilities) {
139 float freqResolutionHz;
140 Status status = vibrator->getFrequencyResolution(&freqResolutionHz);
141 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
142 EXPECT_GT(freqResolutionHz, 0);
143 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
144 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100145 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000146 }
147 return freqResolutionHz;
148}
149
150static float getFrequencyMinimumHz(sp<IVibrator> vibrator, int32_t capabilities) {
151 float freqMinimumHz;
152 Status status = vibrator->getFrequencyMinimum(&freqMinimumHz);
153 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
154 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
155
156 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
157
158 EXPECT_GT(freqMinimumHz, 0);
159 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
160 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100161 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000162 }
163 return freqMinimumHz;
164}
165
166static float getFrequencyMaximumHz(sp<IVibrator> vibrator, int32_t capabilities) {
167 std::vector<float> bandwidthAmplitudeMap;
168 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
169 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
170 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
171 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100172 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000173 }
174
chasewu70da3cc2022-03-15 15:16:04 +0800175 float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) *
176 getFrequencyResolutionHz(vibrator, capabilities)) +
177 getFrequencyMinimumHz(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000178 return freqMaximumHz;
179}
180
181static float getAmplitudeMin() {
182 return 0.0;
183}
184
185static float getAmplitudeMax() {
186 return 1.0;
187}
188
189static ActivePwle composeValidActivePwle(sp<IVibrator> vibrator, int32_t capabilities) {
190 float frequencyHz;
191 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
192 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
193 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
194 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
195 } else {
196 frequencyHz = 150.0; // default value commonly used
197 }
198
199 ActivePwle active;
200 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
201 active.startFrequency = frequencyHz;
202 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
203 active.endFrequency = frequencyHz;
chasewu22cb9012022-03-31 23:23:27 +0800204 vibrator->getPwlePrimitiveDurationMax(&(active.duration));
Vince Leung823cf5f2021-02-11 02:21:57 +0000205
206 return active;
207}
208
Steven Morelandd44007e2019-10-24 18:12:46 -0700209TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
210 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
211 sleep(1);
212 EXPECT_TRUE(vibrator->off().isOk());
213}
214
215TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000216 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
217 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700218
219 std::promise<void> completionPromise;
220 std::future<void> completionFuture{completionPromise.get_future()};
221 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000222 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700223 uint32_t durationMs = 250;
224 std::chrono::milliseconds timeout{durationMs * 2};
225 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
226 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
227 EXPECT_TRUE(vibrator->off().isOk());
228}
229
230TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800231 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700232 sp<CompletionCallback> callback = new CompletionCallback([] {});
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100233 Status status = vibrator->on(250, callback);
234 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700235 }
236}
237
238TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800239 std::vector<Effect> supported;
240 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
241
Steven Morelandd44007e2019-10-24 18:12:46 -0700242 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800243 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000244 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800245
Steven Morelandd44007e2019-10-24 18:12:46 -0700246 for (EffectStrength strength : kEffectStrengths) {
247 int32_t lengthMs = 0;
248 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800249
250 if (isEffectSupported) {
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900251 EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700252 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800253 usleep(lengthMs * 1000);
Steven Morelandd44007e2019-10-24 18:12:46 -0700254 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100255 EXPECT_TRUE(isUnknownOrUnsupported(status))
256 << status << " " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700257 }
258 }
259 }
260}
261
262TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000263 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
264 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700265
Steven Moreland2932b222019-11-05 14:30:17 -0800266 std::vector<Effect> supported;
267 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
268
Steven Morelandd44007e2019-10-24 18:12:46 -0700269 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800270 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000271 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800272
Steven Morelandd44007e2019-10-24 18:12:46 -0700273 for (EffectStrength strength : kEffectStrengths) {
274 std::promise<void> completionPromise;
275 std::future<void> completionFuture{completionPromise.get_future()};
276 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000277 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800278 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700279 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800280
281 if (isEffectSupported) {
282 EXPECT_TRUE(status.isOk());
283 EXPECT_GT(lengthMs, 0);
284 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100285 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Moreland2932b222019-11-05 14:30:17 -0800286 }
287
Vince Leung823cf5f2021-02-11 02:21:57 +0000288 if (!status.isOk())
289 continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700290
Vince Leung7b8606e2021-05-04 13:56:48 -0700291 //TODO(b/187207798): revert back to conservative timeout values once
292 //latencies have been fixed
293 std::chrono::milliseconds timeout{lengthMs * 8};
Steven Morelandd44007e2019-10-24 18:12:46 -0700294 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
295 }
296 }
297}
298
299TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000300 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
301 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700302
303 for (Effect effect : kEffects) {
304 for (EffectStrength strength : kEffectStrengths) {
305 sp<CompletionCallback> callback = new CompletionCallback([] {});
306 int lengthMs;
307 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100308 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700309 }
310 }
311}
312
313TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
314 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800315 for (EffectStrength strength : kEffectStrengths) {
316 int32_t lengthMs;
317 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Lais Andrade14932002021-06-16 13:37:37 +0100318 EXPECT_TRUE(isUnknownOrUnsupported(status))
319 << status << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800320 }
321 }
322 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700323 for (EffectStrength strength : kInvalidEffectStrengths) {
324 int32_t lengthMs;
325 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100326 EXPECT_TRUE(isUnknownOrUnsupported(status))
327 << status << " " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700328 }
329 }
330}
331
332TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
333 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900334 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700335 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900336 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700337 sleep(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900338 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700339 sleep(1);
Fenglin Wua464fb42022-05-12 10:25:58 +0800340 EXPECT_TRUE(vibrator->off().isOk());
Steven Morelandd44007e2019-10-24 18:12:46 -0700341 }
342}
343
344TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
345 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
346 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
347 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900348 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700349 }
350}
351
352TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
353 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100354 Status status = vibrator->setAmplitude(1);
355 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700356 }
357}
358
359TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
360 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
361 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
362 sleep(1);
363 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
364 sleep(1);
365 }
366}
367
Steven Morelandc0b92d52019-11-05 13:31:41 -0800368TEST_P(VibratorAidl, ExternalAmplitudeControl) {
369 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000370 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800371
372 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
373 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
374
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900375 Status amplitudeStatus = vibrator->setAmplitude(0.5);
Steven Morelandc0b92d52019-11-05 13:31:41 -0800376 if (supportsExternalAmplitudeControl) {
377 EXPECT_TRUE(amplitudeStatus.isOk());
378 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100379 EXPECT_TRUE(isUnknownOrUnsupported(amplitudeStatus)) << amplitudeStatus;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800380 }
381 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
382 } else {
383 EXPECT_FALSE(supportsExternalAmplitudeControl);
384 }
385}
386
Steven Morelandd44007e2019-10-24 18:12:46 -0700387TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
388 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100389 Status status = vibrator->setExternalControl(true);
390 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700391 }
392}
393
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900394TEST_P(VibratorAidl, GetSupportedPrimitives) {
395 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
396 std::vector<CompositePrimitive> supported;
397
398 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
399
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900400 for (auto primitive : kCompositePrimitives) {
401 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000402 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Lais Andrade7e643772021-07-09 14:59:44 +0100403 bool isPrimitiveRequired =
404 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
405 kRequiredPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900406
Lais Andrade7e643772021-07-09 14:59:44 +0100407 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900408 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900409 }
410}
411
412TEST_P(VibratorAidl, GetPrimitiveDuration) {
413 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900414 std::vector<CompositePrimitive> supported;
415 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900416
417 for (auto primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900418 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000419 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900420 int32_t duration;
421
422 Status status = vibrator->getPrimitiveDuration(primitive, &duration);
423
424 if (isPrimitiveSupported) {
425 EXPECT_EQ(Status::EX_NONE, status.exceptionCode());
Lais Andradef1b4dd32021-11-03 16:47:32 +0000426 if (primitive != CompositePrimitive::NOOP) {
427 ASSERT_GT(duration, 0) << toString(primitive) << " " << duration;
428 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900429 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100430 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900431 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900432 }
433 }
434}
435
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900436TEST_P(VibratorAidl, ComposeValidPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000437 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
438 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
439 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900440
Lais Andrade38d054e2024-02-09 12:42:09 +0000441 std::vector<CompositePrimitive> supported;
442 int32_t maxDelay, maxSize;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900443
Lais Andrade38d054e2024-02-09 12:42:09 +0000444 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
445 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
446 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900447
Lais Andrade38d054e2024-02-09 12:42:09 +0000448 std::vector<CompositeEffect> composite;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900449
Lais Andrade661481e2024-02-12 10:33:09 +0000450 for (int i = 0; i < supported.size(); i++) {
451 auto primitive = supported[i];
452 float t = static_cast<float>(i + 1) / supported.size();
Lais Andrade38d054e2024-02-09 12:42:09 +0000453 CompositeEffect effect;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900454
Lais Andrade661481e2024-02-12 10:33:09 +0000455 effect.delayMs = maxDelay * t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000456 effect.primitive = primitive;
Lais Andrade661481e2024-02-12 10:33:09 +0000457 effect.scale = t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000458
459 if (composite.size() == maxSize) {
460 break;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900461 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000462 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900463
Lais Andrade38d054e2024-02-09 12:42:09 +0000464 if (composite.size() != 0) {
465 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
466 EXPECT_TRUE(vibrator->off().isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900467 }
468}
469
470TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000471 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
472 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
473 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900474
Lais Andrade38d054e2024-02-09 12:42:09 +0000475 auto unsupported = kInvalidPrimitives;
476 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900477
Lais Andrade38d054e2024-02-09 12:42:09 +0000478 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
479
480 for (auto primitive : kCompositePrimitives) {
481 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000482 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900483
Lais Andrade38d054e2024-02-09 12:42:09 +0000484 if (!isPrimitiveSupported) {
485 unsupported.push_back(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900486 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000487 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900488
Lais Andrade38d054e2024-02-09 12:42:09 +0000489 for (auto primitive : unsupported) {
490 std::vector<CompositeEffect> composite(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900491
Lais Andrade38d054e2024-02-09 12:42:09 +0000492 for (auto& effect : composite) {
493 effect.delayMs = 0;
494 effect.primitive = primitive;
495 effect.scale = 1.0f;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900496 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000497 Status status = vibrator->compose(composite, nullptr);
498 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900499 }
500}
501
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900502TEST_P(VibratorAidl, ComposeScaleBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000503 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
504 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900505 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000506
507 std::vector<CompositeEffect> composite(1);
508 CompositeEffect& effect = composite[0];
509
510 effect.delayMs = 0;
511 effect.primitive = CompositePrimitive::CLICK;
512
513 effect.scale = std::nextafter(0.0f, -1.0f);
514 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode());
515
516 effect.scale = 0.0f;
517 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
518 EXPECT_TRUE(vibrator->off().isOk());
519
520 effect.scale = 1.0f;
521 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
522 EXPECT_TRUE(vibrator->off().isOk());
523
524 effect.scale = std::nextafter(1.0f, 2.0f);
525 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode());
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900526}
527
528TEST_P(VibratorAidl, ComposeDelayBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000529 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
530 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900531 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000532
533 int32_t maxDelay;
534
535 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
536
537 std::vector<CompositeEffect> composite(1);
Lais Andrade661481e2024-02-12 10:33:09 +0000538 CompositeEffect& effect = composite[0];
Lais Andrade38d054e2024-02-09 12:42:09 +0000539
Lais Andrade38d054e2024-02-09 12:42:09 +0000540 effect.primitive = CompositePrimitive::CLICK;
541 effect.scale = 1.0f;
542
Lais Andrade661481e2024-02-12 10:33:09 +0000543 effect.delayMs = 0;
544 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
545 EXPECT_TRUE(vibrator->off().isOk());
546
547 effect.delayMs = 1;
548 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
549 EXPECT_TRUE(vibrator->off().isOk());
550
551 effect.delayMs = maxDelay;
Lais Andrade38d054e2024-02-09 12:42:09 +0000552 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
553 EXPECT_TRUE(vibrator->off().isOk());
554
555 effect.delayMs = maxDelay + 1;
Lais Andrade38d054e2024-02-09 12:42:09 +0000556 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900557}
558
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900559TEST_P(VibratorAidl, ComposeSizeBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000560 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
561 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900562 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000563
564 int32_t maxSize;
565
566 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
567
568 std::vector<CompositeEffect> composite(maxSize);
569 CompositeEffect effect;
570
571 effect.delayMs = 1;
572 effect.primitive = CompositePrimitive::CLICK;
573 effect.scale = 1.0f;
574
575 std::fill(composite.begin(), composite.end(), effect);
576 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
577 EXPECT_TRUE(vibrator->off().isOk());
578
579 composite.emplace_back(effect);
580 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900581}
582
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900583TEST_P(VibratorAidl, ComposeCallback) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000584 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
585 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
586 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900587
Lais Andrade38d054e2024-02-09 12:42:09 +0000588 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900589
Lais Andrade38d054e2024-02-09 12:42:09 +0000590 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900591
Lais Andrade38d054e2024-02-09 12:42:09 +0000592 for (auto primitive : supported) {
593 if (primitive == CompositePrimitive::NOOP) {
594 continue;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900595 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000596
597 std::promise<void> completionPromise;
598 std::future<void> completionFuture{completionPromise.get_future()};
599 sp<CompletionCallback> callback =
600 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
601 CompositeEffect effect;
602 std::vector<CompositeEffect> composite;
603 int32_t durationMs;
604 std::chrono::milliseconds duration;
605 std::chrono::time_point<high_resolution_clock> start, end;
606 std::chrono::milliseconds elapsed;
607
608 effect.delayMs = 0;
609 effect.primitive = primitive;
610 effect.scale = 1.0f;
611 composite.emplace_back(effect);
612
613 EXPECT_EQ(Status::EX_NONE,
614 vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode())
615 << toString(primitive);
616 duration = std::chrono::milliseconds(durationMs);
617
618 start = high_resolution_clock::now();
619 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
620 << toString(primitive);
621
622 // TODO(b/261130361): Investigate why latency from driver and hardware will cause test
623 // to fail when wait duration is ~40ms or less.
624 EXPECT_EQ(completionFuture.wait_for(duration + std::chrono::milliseconds(50)),
625 std::future_status::ready)
626 << toString(primitive);
627 end = high_resolution_clock::now();
628
629 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
630 EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive);
631
632 EXPECT_TRUE(vibrator->off().isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900633 }
634}
635
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900636TEST_P(VibratorAidl, AlwaysOn) {
637 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
638 std::vector<Effect> supported;
639 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
640
641 for (Effect effect : kEffects) {
642 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000643 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900644
645 for (EffectStrength strength : kEffectStrengths) {
646 Status status = vibrator->alwaysOnEnable(0, effect, strength);
647
648 if (isEffectSupported) {
649 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000650 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900651 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100652 EXPECT_TRUE(isUnknownOrUnsupported(status))
653 << status << " " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900654 }
655 }
656 }
657
658 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
659 }
660}
661
Vince Leung4bae4f92021-02-03 06:21:58 +0000662TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000663 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000664}
665
666TEST_P(VibratorAidl, GetQFactor) {
667 float qFactor;
668 Status status = vibrator->getQFactor(&qFactor);
669 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000670 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000671 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
672 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100673 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung4bae4f92021-02-03 06:21:58 +0000674 }
675}
676
Vince Leung823cf5f2021-02-11 02:21:57 +0000677TEST_P(VibratorAidl, GetFrequencyResolution) {
678 getFrequencyResolutionHz(vibrator, capabilities);
679}
680
681TEST_P(VibratorAidl, GetFrequencyMinimum) {
682 getFrequencyMinimumHz(vibrator, capabilities);
683}
684
685TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
686 std::vector<float> bandwidthAmplitudeMap;
687 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
688 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
689 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
690 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
691
692 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
693 getFrequencyMinimumHz(vibrator, capabilities)) /
694 getFrequencyResolutionHz(vibrator, capabilities);
695 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
696
697 for (float e : bandwidthAmplitudeMap) {
698 ASSERT_GE(e, 0.0);
699 ASSERT_LE(e, 1.0);
700 }
701 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100702 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000703 }
704}
705
706TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
707 int32_t durationMs;
708 Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
709 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
710 ASSERT_NE(durationMs, 0);
711 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
712 } else {
Lais Andrade14932002021-06-16 13:37:37 +0100713 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000714 }
715}
716
717TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
718 int32_t maxSize;
719 Status status = vibrator->getPwleCompositionSizeMax(&maxSize);
720 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
721 ASSERT_NE(maxSize, 0);
722 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
723 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100724 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000725 }
726}
727
728TEST_P(VibratorAidl, GetSupportedBraking) {
729 std::vector<Braking> supported;
730 Status status = vibrator->getSupportedBraking(&supported);
731 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
732 bool isDefaultNoneSupported =
733 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
734 ASSERT_TRUE(isDefaultNoneSupported);
735 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
736 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100737 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000738 }
739}
740
741TEST_P(VibratorAidl, ComposeValidPwle) {
742 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade22754c52021-09-14 12:21:59 +0000743 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000744
745 std::vector<Braking> supported;
746 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
747 bool isClabSupported =
748 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000749 BrakingPwle firstBraking;
750 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
751 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000752
Lais Andrade22754c52021-09-14 12:21:59 +0000753 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities);
754 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
755 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
756 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities);
757 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
758 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
759 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
760 }
761 BrakingPwle secondBraking;
762 secondBraking.braking = Braking::NONE;
763 secondBraking.duration = 10;
764
765 auto pwleQueue =
766 std::vector<PrimitivePwle>{firstActive, firstBraking, secondActive, secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000767
768 EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000769 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000770 }
771}
772
773TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
774 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
775 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
776 return;
777
778 std::promise<void> completionPromise;
779 std::future<void> completionFuture{completionPromise.get_future()};
780 sp<CompletionCallback> callback =
781 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800782 int32_t segmentDurationMaxMs;
783 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
784 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Vince Leung7b8606e2021-05-04 13:56:48 -0700785 //TODO(b/187207798): revert back to conservative timeout values once
786 //latencies have been fixed
787 std::chrono::milliseconds timeout{durationMs * 4};
Vince Leung823cf5f2021-02-11 02:21:57 +0000788
789 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
790
791 std::vector<Braking> supported;
792 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
793 bool isClabSupported =
794 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
795 BrakingPwle braking;
796 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
797 braking.duration = 100;
798
Lais Andrade22754c52021-09-14 12:21:59 +0000799 auto pwleQueue = std::vector<PrimitivePwle>{active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000800
801 EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk());
802 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
803 EXPECT_TRUE(vibrator->off().isOk());
804}
805
806TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
807 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
808 std::vector<PrimitivePwle> pwleQueue;
809 // test empty queue
810 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
811 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000812 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000813
814 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
815
816 PrimitivePwle pwle;
817 pwle = active;
818 int segmentCountMax;
819 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
820
821 // Create PWLE queue with more segments than allowed
822 for (int i = 0; i < segmentCountMax + 10; i++) {
823 pwleQueue.emplace_back(std::move(pwle));
824 }
825
826 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
827 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000828 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000829 }
830}
831
832TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
833 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
834 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
835 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
836 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
837
Lais Andrade22754c52021-09-14 12:21:59 +0000838 auto pwleQueueGreater = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000839
840 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
841 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000842 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000843
844 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
845 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
846
Lais Andrade22754c52021-09-14 12:21:59 +0000847 auto pwleQueueLess = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000848
849 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
850 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000851 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000852 }
853}
854
855TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
856 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
857 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
858 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
859 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
860 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
861
862 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
863 active.startFrequency =
864 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
865 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
866
Lais Andrade22754c52021-09-14 12:21:59 +0000867 auto pwleQueueGreater = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000868
869 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
870 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000871 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000872
873 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
874 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
875
Lais Andrade22754c52021-09-14 12:21:59 +0000876 auto pwleQueueLess = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000877
878 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
879 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000880 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000881 }
882}
883
884TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
885 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
886 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
887
chasewu22cb9012022-03-31 23:23:27 +0800888 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +0000889 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
890 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
891
Lais Andrade22754c52021-09-14 12:21:59 +0000892 auto pwleQueue = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000893
894 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
895 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000896 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000897 }
898}
899
Lais Andrade80b18612020-10-12 18:44:40 +0000900std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
901 std::vector<std::tuple<int32_t, int32_t>> tuples;
902 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
903 std::vector<int32_t> vibratorIds;
904
905 for (int i = 0; i < managerAidlNames.size(); i++) {
906 auto managerName = String16(managerAidlNames[i].c_str());
907 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
908 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000909 for (auto &vibratorId : vibratorIds) {
Lais Andrade80b18612020-10-12 18:44:40 +0000910 tuples.push_back(std::make_tuple(i, vibratorId));
911 }
912 }
913 }
914
915 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
916 for (int i = 0; i < vibratorAidlNames.size(); i++) {
917 tuples.push_back(std::make_tuple(-1, i));
918 }
919
920 return tuples;
921}
922
Vince Leung823cf5f2021-02-11 02:21:57 +0000923std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
924 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +0000925 if (managerIdx < 0) {
926 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
927 }
928 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
929 std::to_string(vibratorId);
930}
931
Dan Shiba4d5322020-07-28 13:09:30 -0700932GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +0000933INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
934 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -0700935
Vince Leung823cf5f2021-02-11 02:21:57 +0000936int main(int argc, char **argv) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700937 ::testing::InitGoogleTest(&argc, argv);
938 ProcessState::self()->setThreadPoolMaxThreadCount(1);
939 ProcessState::self()->startThreadPool();
940 return RUN_ALL_TESTS();
941}