blob: 38417154ebe0f2eda1cdd59f3928203ffecefcfd [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
112 sp<IVibrator> vibrator;
113 int32_t capabilities;
114};
115
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100116inline bool isUnknownOrUnsupported(Status status) {
117 return status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
118 status.transactionError() == android::UNKNOWN_TRANSACTION;
119}
120
Vince Leung823cf5f2021-02-11 02:21:57 +0000121static float getResonantFrequencyHz(sp<IVibrator> vibrator, int32_t capabilities) {
122 float resonantFrequencyHz;
123 Status status = vibrator->getResonantFrequency(&resonantFrequencyHz);
124 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
125 EXPECT_GT(resonantFrequencyHz, 0);
126 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
127 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100128 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000129 }
130 return resonantFrequencyHz;
131}
132
133static float getFrequencyResolutionHz(sp<IVibrator> vibrator, int32_t capabilities) {
134 float freqResolutionHz;
135 Status status = vibrator->getFrequencyResolution(&freqResolutionHz);
136 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
137 EXPECT_GT(freqResolutionHz, 0);
138 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
139 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100140 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000141 }
142 return freqResolutionHz;
143}
144
145static float getFrequencyMinimumHz(sp<IVibrator> vibrator, int32_t capabilities) {
146 float freqMinimumHz;
147 Status status = vibrator->getFrequencyMinimum(&freqMinimumHz);
148 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
149 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
150
151 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
152
153 EXPECT_GT(freqMinimumHz, 0);
154 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
155 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100156 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000157 }
158 return freqMinimumHz;
159}
160
161static float getFrequencyMaximumHz(sp<IVibrator> vibrator, int32_t capabilities) {
162 std::vector<float> bandwidthAmplitudeMap;
163 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
164 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
165 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
166 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100167 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000168 }
169
chasewu70da3cc2022-03-15 15:16:04 +0800170 float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) *
171 getFrequencyResolutionHz(vibrator, capabilities)) +
172 getFrequencyMinimumHz(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000173 return freqMaximumHz;
174}
175
176static float getAmplitudeMin() {
177 return 0.0;
178}
179
180static float getAmplitudeMax() {
181 return 1.0;
182}
183
184static ActivePwle composeValidActivePwle(sp<IVibrator> vibrator, int32_t capabilities) {
185 float frequencyHz;
186 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
187 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
188 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
189 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
190 } else {
191 frequencyHz = 150.0; // default value commonly used
192 }
193
194 ActivePwle active;
195 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
196 active.startFrequency = frequencyHz;
197 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
198 active.endFrequency = frequencyHz;
chasewu22cb9012022-03-31 23:23:27 +0800199 vibrator->getPwlePrimitiveDurationMax(&(active.duration));
Vince Leung823cf5f2021-02-11 02:21:57 +0000200
201 return active;
202}
203
Steven Morelandd44007e2019-10-24 18:12:46 -0700204TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
205 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
206 sleep(1);
207 EXPECT_TRUE(vibrator->off().isOk());
208}
209
210TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000211 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
212 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700213
214 std::promise<void> completionPromise;
215 std::future<void> completionFuture{completionPromise.get_future()};
216 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000217 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700218 uint32_t durationMs = 250;
219 std::chrono::milliseconds timeout{durationMs * 2};
220 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
221 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
222 EXPECT_TRUE(vibrator->off().isOk());
223}
224
225TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800226 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700227 sp<CompletionCallback> callback = new CompletionCallback([] {});
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100228 Status status = vibrator->on(250, callback);
229 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700230 }
231}
232
233TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800234 std::vector<Effect> supported;
235 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
236
Steven Morelandd44007e2019-10-24 18:12:46 -0700237 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800238 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000239 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800240
Steven Morelandd44007e2019-10-24 18:12:46 -0700241 for (EffectStrength strength : kEffectStrengths) {
242 int32_t lengthMs = 0;
243 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800244
245 if (isEffectSupported) {
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900246 EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700247 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800248 usleep(lengthMs * 1000);
Steven Morelandd44007e2019-10-24 18:12:46 -0700249 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100250 EXPECT_TRUE(isUnknownOrUnsupported(status))
251 << status << " " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700252 }
253 }
254 }
255}
256
257TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000258 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
259 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700260
Steven Moreland2932b222019-11-05 14:30:17 -0800261 std::vector<Effect> supported;
262 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
263
Steven Morelandd44007e2019-10-24 18:12:46 -0700264 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800265 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000266 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800267
Steven Morelandd44007e2019-10-24 18:12:46 -0700268 for (EffectStrength strength : kEffectStrengths) {
269 std::promise<void> completionPromise;
270 std::future<void> completionFuture{completionPromise.get_future()};
271 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000272 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800273 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700274 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800275
276 if (isEffectSupported) {
277 EXPECT_TRUE(status.isOk());
278 EXPECT_GT(lengthMs, 0);
279 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100280 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Moreland2932b222019-11-05 14:30:17 -0800281 }
282
Vince Leung823cf5f2021-02-11 02:21:57 +0000283 if (!status.isOk())
284 continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700285
Vince Leung7b8606e2021-05-04 13:56:48 -0700286 //TODO(b/187207798): revert back to conservative timeout values once
287 //latencies have been fixed
288 std::chrono::milliseconds timeout{lengthMs * 8};
Steven Morelandd44007e2019-10-24 18:12:46 -0700289 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
290 }
291 }
292}
293
294TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000295 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
296 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700297
298 for (Effect effect : kEffects) {
299 for (EffectStrength strength : kEffectStrengths) {
300 sp<CompletionCallback> callback = new CompletionCallback([] {});
301 int lengthMs;
302 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100303 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700304 }
305 }
306}
307
308TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
309 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800310 for (EffectStrength strength : kEffectStrengths) {
311 int32_t lengthMs;
312 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Lais Andrade14932002021-06-16 13:37:37 +0100313 EXPECT_TRUE(isUnknownOrUnsupported(status))
314 << status << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800315 }
316 }
317 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700318 for (EffectStrength strength : kInvalidEffectStrengths) {
319 int32_t lengthMs;
320 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100321 EXPECT_TRUE(isUnknownOrUnsupported(status))
322 << status << " " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700323 }
324 }
325}
326
327TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
328 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900329 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700330 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900331 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700332 sleep(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900333 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700334 sleep(1);
335 }
336}
337
338TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
339 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
340 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
341 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900342 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700343 }
344}
345
346TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
347 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100348 Status status = vibrator->setAmplitude(1);
349 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700350 }
351}
352
353TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
354 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
355 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
356 sleep(1);
357 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
358 sleep(1);
359 }
360}
361
Steven Morelandc0b92d52019-11-05 13:31:41 -0800362TEST_P(VibratorAidl, ExternalAmplitudeControl) {
363 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000364 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800365
366 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
367 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
368
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900369 Status amplitudeStatus = vibrator->setAmplitude(0.5);
Steven Morelandc0b92d52019-11-05 13:31:41 -0800370 if (supportsExternalAmplitudeControl) {
371 EXPECT_TRUE(amplitudeStatus.isOk());
372 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100373 EXPECT_TRUE(isUnknownOrUnsupported(amplitudeStatus)) << amplitudeStatus;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800374 }
375 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
376 } else {
377 EXPECT_FALSE(supportsExternalAmplitudeControl);
378 }
379}
380
Steven Morelandd44007e2019-10-24 18:12:46 -0700381TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
382 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100383 Status status = vibrator->setExternalControl(true);
384 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700385 }
386}
387
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900388TEST_P(VibratorAidl, GetSupportedPrimitives) {
389 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
390 std::vector<CompositePrimitive> supported;
391
392 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
393
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900394 for (auto primitive : kCompositePrimitives) {
395 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000396 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Lais Andrade7e643772021-07-09 14:59:44 +0100397 bool isPrimitiveRequired =
398 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
399 kRequiredPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900400
Lais Andrade7e643772021-07-09 14:59:44 +0100401 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900402 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900403 }
404}
405
406TEST_P(VibratorAidl, GetPrimitiveDuration) {
407 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900408 std::vector<CompositePrimitive> supported;
409 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900410
411 for (auto primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900412 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000413 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900414 int32_t duration;
415
416 Status status = vibrator->getPrimitiveDuration(primitive, &duration);
417
418 if (isPrimitiveSupported) {
419 EXPECT_EQ(Status::EX_NONE, status.exceptionCode());
Lais Andradef1b4dd32021-11-03 16:47:32 +0000420 if (primitive != CompositePrimitive::NOOP) {
421 ASSERT_GT(duration, 0) << toString(primitive) << " " << duration;
422 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900423 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100424 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900425 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900426 }
427 }
428}
429
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900430TEST_P(VibratorAidl, ComposeValidPrimitives) {
431 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900432 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900433 int32_t maxDelay, maxSize;
434
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900435 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900436 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
437 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
438
439 std::vector<CompositeEffect> composite;
440
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900441 for (auto primitive : supported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900442 CompositeEffect effect;
443
444 effect.delayMs = std::rand() % (maxDelay + 1);
445 effect.primitive = primitive;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900446 effect.scale = static_cast<float>(std::rand()) / RAND_MAX;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900447 composite.emplace_back(effect);
448
449 if (composite.size() == maxSize) {
450 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
451 composite.clear();
452 vibrator->off();
453 }
454 }
455
456 if (composite.size() != 0) {
457 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
458 vibrator->off();
459 }
460 }
461}
462
463TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
464 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900465 auto unsupported = kInvalidPrimitives;
466 std::vector<CompositePrimitive> supported;
467
468 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
469
470 for (auto primitive : kCompositePrimitives) {
471 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000472 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900473
474 if (!isPrimitiveSupported) {
475 unsupported.push_back(primitive);
476 }
477 }
478
479 for (auto primitive : unsupported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900480 std::vector<CompositeEffect> composite(1);
481
Vince Leung823cf5f2021-02-11 02:21:57 +0000482 for (auto &effect : composite) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900483 effect.delayMs = 0;
484 effect.primitive = primitive;
485 effect.scale = 1.0f;
486 }
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100487 Status status = vibrator->compose(composite, nullptr);
488 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900489 vibrator->off();
490 }
491 }
492}
493
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900494TEST_P(VibratorAidl, ComposeScaleBoundary) {
495 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
496 std::vector<CompositeEffect> composite(1);
Vince Leung823cf5f2021-02-11 02:21:57 +0000497 CompositeEffect &effect = composite[0];
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900498
499 effect.delayMs = 0;
500 effect.primitive = CompositePrimitive::CLICK;
501
502 effect.scale = std::nextafter(0.0f, -1.0f);
503 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
504 vibrator->compose(composite, nullptr).exceptionCode());
505
506 effect.scale = 0.0f;
507 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
508
509 effect.scale = 1.0f;
510 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
511
512 effect.scale = std::nextafter(1.0f, 2.0f);
513 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
514 vibrator->compose(composite, nullptr).exceptionCode());
515
516 vibrator->off();
517 }
518}
519
520TEST_P(VibratorAidl, ComposeDelayBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900521 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
522 int32_t maxDelay;
523
524 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
525
526 std::vector<CompositeEffect> composite(1);
527 CompositeEffect effect;
528
529 effect.delayMs = 1;
530 effect.primitive = CompositePrimitive::CLICK;
531 effect.scale = 1.0f;
532
533 std::fill(composite.begin(), composite.end(), effect);
534 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
535
536 effect.delayMs = maxDelay + 1;
537
538 std::fill(composite.begin(), composite.end(), effect);
539 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
540 vibrator->compose(composite, nullptr).exceptionCode());
541 vibrator->off();
542 }
543}
544
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900545TEST_P(VibratorAidl, ComposeSizeBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900546 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
547 int32_t maxSize;
548
549 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
550
551 std::vector<CompositeEffect> composite(maxSize);
552 CompositeEffect effect;
553
554 effect.delayMs = 1;
555 effect.primitive = CompositePrimitive::CLICK;
556 effect.scale = 1.0f;
557
558 std::fill(composite.begin(), composite.end(), effect);
559 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
560
561 composite.emplace_back(effect);
562 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
563 vibrator->compose(composite, nullptr).exceptionCode());
564 vibrator->off();
565 }
566}
567
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900568TEST_P(VibratorAidl, ComposeCallback) {
569 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900570 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900571
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900572 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900573
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900574 for (auto primitive : supported) {
575 if (primitive == CompositePrimitive::NOOP) {
576 continue;
577 }
578
579 std::promise<void> completionPromise;
580 std::future<void> completionFuture{completionPromise.get_future()};
581 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000582 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900583 CompositeEffect effect;
584 std::vector<CompositeEffect> composite;
585 int32_t durationMs;
586 std::chrono::milliseconds duration;
587 std::chrono::time_point<high_resolution_clock> start, end;
588 std::chrono::milliseconds elapsed;
589
590 effect.delayMs = 0;
591 effect.primitive = primitive;
592 effect.scale = 1.0f;
593 composite.emplace_back(effect);
594
595 EXPECT_EQ(Status::EX_NONE,
596 vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000597 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900598 duration = std::chrono::milliseconds(durationMs);
599
Vince Leung36f70d62021-04-09 21:37:22 +0000600 start = high_resolution_clock::now();
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900601 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000602 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900603
Vince Leung7b8606e2021-05-04 13:56:48 -0700604 //TODO(b/187207798): revert back to conservative timeout values once
605 //latencies have been fixed
606 EXPECT_EQ(completionFuture.wait_for(duration * 4), std::future_status::ready)
Vince Leung823cf5f2021-02-11 02:21:57 +0000607 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900608 end = high_resolution_clock::now();
609
610 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Vince Leung36f70d62021-04-09 21:37:22 +0000611 EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900612 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900613 }
614}
615
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900616TEST_P(VibratorAidl, AlwaysOn) {
617 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
618 std::vector<Effect> supported;
619 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
620
621 for (Effect effect : kEffects) {
622 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000623 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900624
625 for (EffectStrength strength : kEffectStrengths) {
626 Status status = vibrator->alwaysOnEnable(0, effect, strength);
627
628 if (isEffectSupported) {
629 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000630 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900631 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100632 EXPECT_TRUE(isUnknownOrUnsupported(status))
633 << status << " " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900634 }
635 }
636 }
637
638 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
639 }
640}
641
Vince Leung4bae4f92021-02-03 06:21:58 +0000642TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000643 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000644}
645
646TEST_P(VibratorAidl, GetQFactor) {
647 float qFactor;
648 Status status = vibrator->getQFactor(&qFactor);
649 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000650 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000651 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
652 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100653 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung4bae4f92021-02-03 06:21:58 +0000654 }
655}
656
Vince Leung823cf5f2021-02-11 02:21:57 +0000657TEST_P(VibratorAidl, GetFrequencyResolution) {
658 getFrequencyResolutionHz(vibrator, capabilities);
659}
660
661TEST_P(VibratorAidl, GetFrequencyMinimum) {
662 getFrequencyMinimumHz(vibrator, capabilities);
663}
664
665TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
666 std::vector<float> bandwidthAmplitudeMap;
667 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
668 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
669 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
670 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
671
672 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
673 getFrequencyMinimumHz(vibrator, capabilities)) /
674 getFrequencyResolutionHz(vibrator, capabilities);
675 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
676
677 for (float e : bandwidthAmplitudeMap) {
678 ASSERT_GE(e, 0.0);
679 ASSERT_LE(e, 1.0);
680 }
681 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100682 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000683 }
684}
685
686TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
687 int32_t durationMs;
688 Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
689 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
690 ASSERT_NE(durationMs, 0);
691 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
692 } else {
Lais Andrade14932002021-06-16 13:37:37 +0100693 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000694 }
695}
696
697TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
698 int32_t maxSize;
699 Status status = vibrator->getPwleCompositionSizeMax(&maxSize);
700 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
701 ASSERT_NE(maxSize, 0);
702 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
703 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100704 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000705 }
706}
707
708TEST_P(VibratorAidl, GetSupportedBraking) {
709 std::vector<Braking> supported;
710 Status status = vibrator->getSupportedBraking(&supported);
711 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
712 bool isDefaultNoneSupported =
713 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
714 ASSERT_TRUE(isDefaultNoneSupported);
715 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
716 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100717 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000718 }
719}
720
721TEST_P(VibratorAidl, ComposeValidPwle) {
722 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade22754c52021-09-14 12:21:59 +0000723 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000724
725 std::vector<Braking> supported;
726 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
727 bool isClabSupported =
728 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000729 BrakingPwle firstBraking;
730 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
731 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000732
Lais Andrade22754c52021-09-14 12:21:59 +0000733 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities);
734 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
735 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
736 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities);
737 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
738 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
739 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
740 }
741 BrakingPwle secondBraking;
742 secondBraking.braking = Braking::NONE;
743 secondBraking.duration = 10;
744
745 auto pwleQueue =
746 std::vector<PrimitivePwle>{firstActive, firstBraking, secondActive, secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000747
748 EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000749 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000750 }
751}
752
753TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
754 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
755 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
756 return;
757
758 std::promise<void> completionPromise;
759 std::future<void> completionFuture{completionPromise.get_future()};
760 sp<CompletionCallback> callback =
761 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800762 int32_t segmentDurationMaxMs;
763 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
764 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Vince Leung7b8606e2021-05-04 13:56:48 -0700765 //TODO(b/187207798): revert back to conservative timeout values once
766 //latencies have been fixed
767 std::chrono::milliseconds timeout{durationMs * 4};
Vince Leung823cf5f2021-02-11 02:21:57 +0000768
769 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
770
771 std::vector<Braking> supported;
772 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
773 bool isClabSupported =
774 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
775 BrakingPwle braking;
776 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
777 braking.duration = 100;
778
Lais Andrade22754c52021-09-14 12:21:59 +0000779 auto pwleQueue = std::vector<PrimitivePwle>{active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000780
781 EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk());
782 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
783 EXPECT_TRUE(vibrator->off().isOk());
784}
785
786TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
787 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
788 std::vector<PrimitivePwle> pwleQueue;
789 // test empty queue
790 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
791 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000792 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000793
794 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
795
796 PrimitivePwle pwle;
797 pwle = active;
798 int segmentCountMax;
799 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
800
801 // Create PWLE queue with more segments than allowed
802 for (int i = 0; i < segmentCountMax + 10; i++) {
803 pwleQueue.emplace_back(std::move(pwle));
804 }
805
806 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
807 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000808 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000809 }
810}
811
812TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
813 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
814 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
815 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
816 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
817
Lais Andrade22754c52021-09-14 12:21:59 +0000818 auto pwleQueueGreater = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000819
820 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
821 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000822 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000823
824 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
825 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
826
Lais Andrade22754c52021-09-14 12:21:59 +0000827 auto pwleQueueLess = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000828
829 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
830 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000831 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000832 }
833}
834
835TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
836 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
837 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
838 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
839 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
840 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
841
842 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
843 active.startFrequency =
844 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
845 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
846
Lais Andrade22754c52021-09-14 12:21:59 +0000847 auto pwleQueueGreater = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000848
849 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
850 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000851 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000852
853 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
854 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
855
Lais Andrade22754c52021-09-14 12:21:59 +0000856 auto pwleQueueLess = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000857
858 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
859 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000860 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000861 }
862}
863
864TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
865 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
866 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
867
chasewu22cb9012022-03-31 23:23:27 +0800868 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +0000869 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
870 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
871
Lais Andrade22754c52021-09-14 12:21:59 +0000872 auto pwleQueue = std::vector<PrimitivePwle>{active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000873
874 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
875 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
Lais Andrade22754c52021-09-14 12:21:59 +0000876 EXPECT_TRUE(vibrator->off().isOk());
Vince Leung823cf5f2021-02-11 02:21:57 +0000877 }
878}
879
Lais Andrade80b18612020-10-12 18:44:40 +0000880std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
881 std::vector<std::tuple<int32_t, int32_t>> tuples;
882 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
883 std::vector<int32_t> vibratorIds;
884
885 for (int i = 0; i < managerAidlNames.size(); i++) {
886 auto managerName = String16(managerAidlNames[i].c_str());
887 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
888 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000889 for (auto &vibratorId : vibratorIds) {
Lais Andrade80b18612020-10-12 18:44:40 +0000890 tuples.push_back(std::make_tuple(i, vibratorId));
891 }
892 }
893 }
894
895 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
896 for (int i = 0; i < vibratorAidlNames.size(); i++) {
897 tuples.push_back(std::make_tuple(-1, i));
898 }
899
900 return tuples;
901}
902
Vince Leung823cf5f2021-02-11 02:21:57 +0000903std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
904 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +0000905 if (managerIdx < 0) {
906 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
907 }
908 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
909 std::to_string(vibratorId);
910}
911
Dan Shiba4d5322020-07-28 13:09:30 -0700912GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +0000913INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
914 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -0700915
Vince Leung823cf5f2021-02-11 02:21:57 +0000916int main(int argc, char **argv) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700917 ::testing::InitGoogleTest(&argc, argv);
918 ProcessState::self()->setThreadPoolMaxThreadCount(1);
919 ProcessState::self()->startThreadPool();
920 return RUN_ALL_TESTS();
921}