blob: 2c6add1253cb649d5a8c56588f083b415e137b97 [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 Andrade38d054e2024-02-09 12:42:09 +0000450 for (auto primitive : supported) {
451 CompositeEffect effect;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900452
Lais Andrade38d054e2024-02-09 12:42:09 +0000453 effect.delayMs = std::rand() % (maxDelay + 1);
454 effect.primitive = primitive;
455 effect.scale = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
456 composite.emplace_back(effect);
457
458 if (composite.size() == maxSize) {
459 break;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900460 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000461 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900462
Lais Andrade38d054e2024-02-09 12:42:09 +0000463 if (composite.size() != 0) {
464 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
465 EXPECT_TRUE(vibrator->off().isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900466 }
467}
468
469TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000470 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
471 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
472 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900473
Lais Andrade38d054e2024-02-09 12:42:09 +0000474 auto unsupported = kInvalidPrimitives;
475 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900476
Lais Andrade38d054e2024-02-09 12:42:09 +0000477 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
478
479 for (auto primitive : kCompositePrimitives) {
480 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000481 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900482
Lais Andrade38d054e2024-02-09 12:42:09 +0000483 if (!isPrimitiveSupported) {
484 unsupported.push_back(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900485 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000486 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900487
Lais Andrade38d054e2024-02-09 12:42:09 +0000488 for (auto primitive : unsupported) {
489 std::vector<CompositeEffect> composite(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900490
Lais Andrade38d054e2024-02-09 12:42:09 +0000491 for (auto& effect : composite) {
492 effect.delayMs = 0;
493 effect.primitive = primitive;
494 effect.scale = 1.0f;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900495 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000496 Status status = vibrator->compose(composite, nullptr);
497 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900498 }
499}
500
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900501TEST_P(VibratorAidl, ComposeScaleBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000502 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
503 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900504 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000505
506 std::vector<CompositeEffect> composite(1);
507 CompositeEffect& effect = composite[0];
508
509 effect.delayMs = 0;
510 effect.primitive = CompositePrimitive::CLICK;
511
512 effect.scale = std::nextafter(0.0f, -1.0f);
513 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode());
514
515 effect.scale = 0.0f;
516 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
517 EXPECT_TRUE(vibrator->off().isOk());
518
519 effect.scale = 1.0f;
520 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
521 EXPECT_TRUE(vibrator->off().isOk());
522
523 effect.scale = std::nextafter(1.0f, 2.0f);
524 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode());
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900525}
526
527TEST_P(VibratorAidl, ComposeDelayBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000528 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
529 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900530 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000531
532 int32_t maxDelay;
533
534 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
535
536 std::vector<CompositeEffect> composite(1);
537 CompositeEffect effect;
538
539 effect.delayMs = 1;
540 effect.primitive = CompositePrimitive::CLICK;
541 effect.scale = 1.0f;
542
543 std::fill(composite.begin(), composite.end(), effect);
544 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
545 EXPECT_TRUE(vibrator->off().isOk());
546
547 effect.delayMs = maxDelay + 1;
548
549 std::fill(composite.begin(), composite.end(), effect);
550 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900551}
552
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900553TEST_P(VibratorAidl, ComposeSizeBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000554 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
555 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900556 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000557
558 int32_t maxSize;
559
560 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
561
562 std::vector<CompositeEffect> composite(maxSize);
563 CompositeEffect effect;
564
565 effect.delayMs = 1;
566 effect.primitive = CompositePrimitive::CLICK;
567 effect.scale = 1.0f;
568
569 std::fill(composite.begin(), composite.end(), effect);
570 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
571 EXPECT_TRUE(vibrator->off().isOk());
572
573 composite.emplace_back(effect);
574 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900575}
576
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900577TEST_P(VibratorAidl, ComposeCallback) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000578 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
579 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
580 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900581
Lais Andrade38d054e2024-02-09 12:42:09 +0000582 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900583
Lais Andrade38d054e2024-02-09 12:42:09 +0000584 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900585
Lais Andrade38d054e2024-02-09 12:42:09 +0000586 for (auto primitive : supported) {
587 if (primitive == CompositePrimitive::NOOP) {
588 continue;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900589 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000590
591 std::promise<void> completionPromise;
592 std::future<void> completionFuture{completionPromise.get_future()};
593 sp<CompletionCallback> callback =
594 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
595 CompositeEffect effect;
596 std::vector<CompositeEffect> composite;
597 int32_t durationMs;
598 std::chrono::milliseconds duration;
599 std::chrono::time_point<high_resolution_clock> start, end;
600 std::chrono::milliseconds elapsed;
601
602 effect.delayMs = 0;
603 effect.primitive = primitive;
604 effect.scale = 1.0f;
605 composite.emplace_back(effect);
606
607 EXPECT_EQ(Status::EX_NONE,
608 vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode())
609 << toString(primitive);
610 duration = std::chrono::milliseconds(durationMs);
611
612 start = high_resolution_clock::now();
613 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
614 << toString(primitive);
615
616 // TODO(b/261130361): Investigate why latency from driver and hardware will cause test
617 // to fail when wait duration is ~40ms or less.
618 EXPECT_EQ(completionFuture.wait_for(duration + std::chrono::milliseconds(50)),
619 std::future_status::ready)
620 << toString(primitive);
621 end = high_resolution_clock::now();
622
623 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
624 EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive);
625
626 EXPECT_TRUE(vibrator->off().isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900627 }
628}
629
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900630TEST_P(VibratorAidl, AlwaysOn) {
631 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
632 std::vector<Effect> supported;
633 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
634
635 for (Effect effect : kEffects) {
636 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000637 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900638
639 for (EffectStrength strength : kEffectStrengths) {
640 Status status = vibrator->alwaysOnEnable(0, effect, strength);
641
642 if (isEffectSupported) {
643 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000644 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900645 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100646 EXPECT_TRUE(isUnknownOrUnsupported(status))
647 << status << " " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900648 }
649 }
650 }
651
652 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
653 }
654}
655
Vince Leung4bae4f92021-02-03 06:21:58 +0000656TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000657 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000658}
659
660TEST_P(VibratorAidl, GetQFactor) {
661 float qFactor;
662 Status status = vibrator->getQFactor(&qFactor);
663 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000664 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000665 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
666 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100667 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung4bae4f92021-02-03 06:21:58 +0000668 }
669}
670
Vince Leung823cf5f2021-02-11 02:21:57 +0000671TEST_P(VibratorAidl, GetFrequencyResolution) {
672 getFrequencyResolutionHz(vibrator, capabilities);
673}
674
675TEST_P(VibratorAidl, GetFrequencyMinimum) {
676 getFrequencyMinimumHz(vibrator, capabilities);
677}
678
679TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
680 std::vector<float> bandwidthAmplitudeMap;
681 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
682 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
683 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
684 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
685
686 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
687 getFrequencyMinimumHz(vibrator, capabilities)) /
688 getFrequencyResolutionHz(vibrator, capabilities);
689 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
690
691 for (float e : bandwidthAmplitudeMap) {
692 ASSERT_GE(e, 0.0);
693 ASSERT_LE(e, 1.0);
694 }
695 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100696 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000697 }
698}
699
700TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
701 int32_t durationMs;
702 Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
703 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
704 ASSERT_NE(durationMs, 0);
705 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
706 } else {
Lais Andrade14932002021-06-16 13:37:37 +0100707 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000708 }
709}
710
711TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
712 int32_t maxSize;
713 Status status = vibrator->getPwleCompositionSizeMax(&maxSize);
714 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
715 ASSERT_NE(maxSize, 0);
716 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
717 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100718 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000719 }
720}
721
722TEST_P(VibratorAidl, GetSupportedBraking) {
723 std::vector<Braking> supported;
724 Status status = vibrator->getSupportedBraking(&supported);
725 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
726 bool isDefaultNoneSupported =
727 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
728 ASSERT_TRUE(isDefaultNoneSupported);
729 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
730 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100731 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000732 }
733}
734
735TEST_P(VibratorAidl, ComposeValidPwle) {
736 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade22754c52021-09-14 12:21:59 +0000737 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000738
739 std::vector<Braking> supported;
740 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
741 bool isClabSupported =
742 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000743 BrakingPwle firstBraking;
744 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
745 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000746
Lais Andrade22754c52021-09-14 12:21:59 +0000747 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities);
748 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
749 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
750 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities);
751 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
752 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
753 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
754 }
755 BrakingPwle secondBraking;
756 secondBraking.braking = Braking::NONE;
757 secondBraking.duration = 10;
758
759 auto pwleQueue =
760 std::vector<PrimitivePwle>{firstActive, firstBraking, secondActive, secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000761
762 EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000763 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000764 }
765}
766
767TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
768 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
769 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
770 return;
771
772 std::promise<void> completionPromise;
773 std::future<void> completionFuture{completionPromise.get_future()};
774 sp<CompletionCallback> callback =
775 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800776 int32_t segmentDurationMaxMs;
777 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
778 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Vince Leung7b8606e2021-05-04 13:56:48 -0700779 //TODO(b/187207798): revert back to conservative timeout values once
780 //latencies have been fixed
781 std::chrono::milliseconds timeout{durationMs * 4};
Vince Leung823cf5f2021-02-11 02:21:57 +0000782
783 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
784
785 std::vector<Braking> supported;
786 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
787 bool isClabSupported =
788 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
789 BrakingPwle braking;
790 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
791 braking.duration = 100;
792
Lais Andrade22754c52021-09-14 12:21:59 +0000793 auto pwleQueue = std::vector<PrimitivePwle>{active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000794
795 EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk());
796 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
797 EXPECT_TRUE(vibrator->off().isOk());
798}
799
800TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
801 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
802 std::vector<PrimitivePwle> pwleQueue;
803 // test empty queue
804 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
805 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000806 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000807
808 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
809
810 PrimitivePwle pwle;
811 pwle = active;
812 int segmentCountMax;
813 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
814
815 // Create PWLE queue with more segments than allowed
816 for (int i = 0; i < segmentCountMax + 10; i++) {
817 pwleQueue.emplace_back(std::move(pwle));
818 }
819
820 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
821 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000822 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000823 }
824}
825
826TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
827 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
828 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
829 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
830 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
831
Lais Andrade22754c52021-09-14 12:21:59 +0000832 auto pwleQueueGreater = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000833
834 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
835 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000836 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000837
838 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
839 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
840
Lais Andrade22754c52021-09-14 12:21:59 +0000841 auto pwleQueueLess = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000842
843 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
844 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000845 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000846 }
847}
848
849TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
850 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
851 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
852 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
853 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
854 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
855
856 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
857 active.startFrequency =
858 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
859 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
860
Lais Andrade22754c52021-09-14 12:21:59 +0000861 auto pwleQueueGreater = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000862
863 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
864 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000865 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000866
867 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
868 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
869
Lais Andrade22754c52021-09-14 12:21:59 +0000870 auto pwleQueueLess = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000871
872 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
873 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000874 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000875 }
876}
877
878TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
879 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
880 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
881
chasewu22cb9012022-03-31 23:23:27 +0800882 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +0000883 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
884 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
885
Lais Andrade22754c52021-09-14 12:21:59 +0000886 auto pwleQueue = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000887
888 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
889 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000890 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000891 }
892}
893
Lais Andrade80b18612020-10-12 18:44:40 +0000894std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
895 std::vector<std::tuple<int32_t, int32_t>> tuples;
896 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
897 std::vector<int32_t> vibratorIds;
898
899 for (int i = 0; i < managerAidlNames.size(); i++) {
900 auto managerName = String16(managerAidlNames[i].c_str());
901 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
902 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000903 for (auto &vibratorId : vibratorIds) {
Lais Andrade80b18612020-10-12 18:44:40 +0000904 tuples.push_back(std::make_tuple(i, vibratorId));
905 }
906 }
907 }
908
909 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
910 for (int i = 0; i < vibratorAidlNames.size(); i++) {
911 tuples.push_back(std::make_tuple(-1, i));
912 }
913
914 return tuples;
915}
916
Vince Leung823cf5f2021-02-11 02:21:57 +0000917std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
918 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +0000919 if (managerIdx < 0) {
920 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
921 }
922 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
923 std::to_string(vibratorId);
924}
925
Dan Shiba4d5322020-07-28 13:09:30 -0700926GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +0000927INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
928 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -0700929
Vince Leung823cf5f2021-02-11 02:21:57 +0000930int main(int argc, char **argv) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700931 ::testing::InitGoogleTest(&argc, argv);
932 ProcessState::self()->setThreadPoolMaxThreadCount(1);
933 ProcessState::self()->startThreadPool();
934 return RUN_ALL_TESTS();
935}