blob: 713ec75a204ad152ea7618e511bbc470622b2e5e [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
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090063const std::vector<CompositePrimitive> kOptionalPrimitives = {
Vince Leung823cf5f2021-02-11 02:21:57 +000064 CompositePrimitive::THUD,
65 CompositePrimitive::SPIN,
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090066};
67
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090068const std::vector<CompositePrimitive> kInvalidPrimitives = {
Vince Leung823cf5f2021-02-11 02:21:57 +000069 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
70 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090071};
72
Steven Morelandd44007e2019-10-24 18:12:46 -070073class CompletionCallback : public BnVibratorCallback {
74 public:
Vince Leung823cf5f2021-02-11 02:21:57 +000075 CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {}
Steven Morelandd44007e2019-10-24 18:12:46 -070076 Status onComplete() override {
77 mCallback();
78 return Status::ok();
79 }
80
81 private:
82 std::function<void()> mCallback;
83};
84
Lais Andrade80b18612020-10-12 18:44:40 +000085class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> {
Steven Morelandd44007e2019-10-24 18:12:46 -070086 public:
87 virtual void SetUp() override {
Lais Andrade80b18612020-10-12 18:44:40 +000088 int32_t managerIdx = std::get<0>(GetParam());
89 int32_t vibratorId = std::get<1>(GetParam());
90 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
91
92 if (managerIdx < 0) {
93 // Testing a unmanaged vibrator, using vibratorId as index from registered HALs
94 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
95 ASSERT_LT(vibratorId, vibratorAidlNames.size());
96 auto vibratorName = String16(vibratorAidlNames[vibratorId].c_str());
97 vibrator = android::waitForDeclaredService<IVibrator>(vibratorName);
98 } else {
99 // Testing a managed vibrator, using vibratorId to retrieve it from the manager
100 ASSERT_LT(managerIdx, managerAidlNames.size());
101 auto managerName = String16(managerAidlNames[managerIdx].c_str());
102 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
103 auto vibratorResult = vibratorManager->getVibrator(vibratorId, &vibrator);
104 ASSERT_TRUE(vibratorResult.isOk());
105 }
106
Steven Morelandd44007e2019-10-24 18:12:46 -0700107 ASSERT_NE(vibrator, nullptr);
108 ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk());
109 }
110
111 sp<IVibrator> vibrator;
112 int32_t capabilities;
113};
114
Vince Leung823cf5f2021-02-11 02:21:57 +0000115static float getResonantFrequencyHz(sp<IVibrator> vibrator, int32_t capabilities) {
116 float resonantFrequencyHz;
117 Status status = vibrator->getResonantFrequency(&resonantFrequencyHz);
118 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
119 EXPECT_GT(resonantFrequencyHz, 0);
120 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
121 } else {
122 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
123 }
124 return resonantFrequencyHz;
125}
126
127static float getFrequencyResolutionHz(sp<IVibrator> vibrator, int32_t capabilities) {
128 float freqResolutionHz;
129 Status status = vibrator->getFrequencyResolution(&freqResolutionHz);
130 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
131 EXPECT_GT(freqResolutionHz, 0);
132 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
133 } else {
134 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
135 }
136 return freqResolutionHz;
137}
138
139static float getFrequencyMinimumHz(sp<IVibrator> vibrator, int32_t capabilities) {
140 float freqMinimumHz;
141 Status status = vibrator->getFrequencyMinimum(&freqMinimumHz);
142 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
143 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
144
145 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
146
147 EXPECT_GT(freqMinimumHz, 0);
148 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
149 } else {
150 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
151 }
152 return freqMinimumHz;
153}
154
155static float getFrequencyMaximumHz(sp<IVibrator> vibrator, int32_t capabilities) {
156 std::vector<float> bandwidthAmplitudeMap;
157 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
158 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
159 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
160 } else {
161 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
162 }
163
164 float freqMaximumHz =
165 (bandwidthAmplitudeMap.size() * getFrequencyResolutionHz(vibrator, capabilities)) +
166 getFrequencyMinimumHz(vibrator, capabilities);
167 return freqMaximumHz;
168}
169
170static float getAmplitudeMin() {
171 return 0.0;
172}
173
174static float getAmplitudeMax() {
175 return 1.0;
176}
177
178static ActivePwle composeValidActivePwle(sp<IVibrator> vibrator, int32_t capabilities) {
179 float frequencyHz;
180 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
181 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
182 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
183 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
184 } else {
185 frequencyHz = 150.0; // default value commonly used
186 }
187
188 ActivePwle active;
189 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
190 active.startFrequency = frequencyHz;
191 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
192 active.endFrequency = frequencyHz;
193 active.duration = 1000;
194
195 return active;
196}
197
Steven Morelandd44007e2019-10-24 18:12:46 -0700198TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
199 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
200 sleep(1);
201 EXPECT_TRUE(vibrator->off().isOk());
202}
203
204TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000205 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
206 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700207
208 std::promise<void> completionPromise;
209 std::future<void> completionFuture{completionPromise.get_future()};
210 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000211 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700212 uint32_t durationMs = 250;
213 std::chrono::milliseconds timeout{durationMs * 2};
214 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
215 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
216 EXPECT_TRUE(vibrator->off().isOk());
217}
218
219TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800220 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700221 sp<CompletionCallback> callback = new CompletionCallback([] {});
222 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->on(250, callback).exceptionCode());
223 }
224}
225
226TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800227 std::vector<Effect> supported;
228 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
229
Steven Morelandd44007e2019-10-24 18:12:46 -0700230 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800231 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000232 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800233
Steven Morelandd44007e2019-10-24 18:12:46 -0700234 for (EffectStrength strength : kEffectStrengths) {
235 int32_t lengthMs = 0;
236 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800237
238 if (isEffectSupported) {
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900239 EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700240 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800241 usleep(lengthMs * 1000);
Steven Morelandd44007e2019-10-24 18:12:46 -0700242 } else {
Steven Morelandf3353882019-11-07 17:02:43 -0800243 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Vince Leung823cf5f2021-02-11 02:21:57 +0000244 << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700245 }
246 }
247 }
248}
249
250TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000251 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
252 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700253
Steven Moreland2932b222019-11-05 14:30:17 -0800254 std::vector<Effect> supported;
255 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
256
Steven Morelandd44007e2019-10-24 18:12:46 -0700257 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800258 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000259 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800260
Steven Morelandd44007e2019-10-24 18:12:46 -0700261 for (EffectStrength strength : kEffectStrengths) {
262 std::promise<void> completionPromise;
263 std::future<void> completionFuture{completionPromise.get_future()};
264 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000265 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800266 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700267 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800268
269 if (isEffectSupported) {
270 EXPECT_TRUE(status.isOk());
271 EXPECT_GT(lengthMs, 0);
272 } else {
273 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
Steven Moreland2932b222019-11-05 14:30:17 -0800274 }
275
Vince Leung823cf5f2021-02-11 02:21:57 +0000276 if (!status.isOk())
277 continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700278
Vince Leung7b8606e2021-05-04 13:56:48 -0700279 //TODO(b/187207798): revert back to conservative timeout values once
280 //latencies have been fixed
281 std::chrono::milliseconds timeout{lengthMs * 8};
Steven Morelandd44007e2019-10-24 18:12:46 -0700282 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
283 }
284 }
285}
286
287TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000288 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
289 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700290
291 for (Effect effect : kEffects) {
292 for (EffectStrength strength : kEffectStrengths) {
293 sp<CompletionCallback> callback = new CompletionCallback([] {});
294 int lengthMs;
295 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
296 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700297 }
298 }
299}
300
301TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
302 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800303 for (EffectStrength strength : kEffectStrengths) {
304 int32_t lengthMs;
305 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
306 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Vince Leung823cf5f2021-02-11 02:21:57 +0000307 << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800308 }
309 }
310 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700311 for (EffectStrength strength : kInvalidEffectStrengths) {
312 int32_t lengthMs;
313 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Morelandf3353882019-11-07 17:02:43 -0800314 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Vince Leung823cf5f2021-02-11 02:21:57 +0000315 << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700316 }
317 }
318}
319
320TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
321 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900322 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700323 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900324 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700325 sleep(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900326 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700327 sleep(1);
328 }
329}
330
331TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
332 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
333 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
334 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900335 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700336 }
337}
338
339TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
340 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
341 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode());
342 }
343}
344
345TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
346 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
347 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
348 sleep(1);
349 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
350 sleep(1);
351 }
352}
353
Steven Morelandc0b92d52019-11-05 13:31:41 -0800354TEST_P(VibratorAidl, ExternalAmplitudeControl) {
355 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000356 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800357
358 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
359 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
360
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900361 Status amplitudeStatus = vibrator->setAmplitude(0.5);
Steven Morelandc0b92d52019-11-05 13:31:41 -0800362 if (supportsExternalAmplitudeControl) {
363 EXPECT_TRUE(amplitudeStatus.isOk());
364 } else {
365 EXPECT_EQ(amplitudeStatus.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
366 }
367 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
368 } else {
369 EXPECT_FALSE(supportsExternalAmplitudeControl);
370 }
371}
372
Steven Morelandd44007e2019-10-24 18:12:46 -0700373TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
374 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
375 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
376 vibrator->setExternalControl(true).exceptionCode());
377 }
378}
379
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900380TEST_P(VibratorAidl, GetSupportedPrimitives) {
381 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
382 std::vector<CompositePrimitive> supported;
383
384 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
385
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900386 for (auto primitive : kCompositePrimitives) {
387 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000388 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900389 bool isPrimitiveOptional =
Vince Leung823cf5f2021-02-11 02:21:57 +0000390 std::find(kOptionalPrimitives.begin(), kOptionalPrimitives.end(), primitive) !=
391 kOptionalPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900392
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900393 EXPECT_TRUE(isPrimitiveSupported || isPrimitiveOptional) << toString(primitive);
394 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900395 }
396}
397
398TEST_P(VibratorAidl, GetPrimitiveDuration) {
399 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900400 std::vector<CompositePrimitive> supported;
401 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900402
403 for (auto primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900404 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000405 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900406 int32_t duration;
407
408 Status status = vibrator->getPrimitiveDuration(primitive, &duration);
409
410 if (isPrimitiveSupported) {
411 EXPECT_EQ(Status::EX_NONE, status.exceptionCode());
412 } else {
413 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
414 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900415 }
416 }
417}
418
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900419TEST_P(VibratorAidl, ComposeValidPrimitives) {
420 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900421 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900422 int32_t maxDelay, maxSize;
423
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900424 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900425 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
426 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
427
428 std::vector<CompositeEffect> composite;
429
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900430 for (auto primitive : supported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900431 CompositeEffect effect;
432
433 effect.delayMs = std::rand() % (maxDelay + 1);
434 effect.primitive = primitive;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900435 effect.scale = static_cast<float>(std::rand()) / RAND_MAX;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900436 composite.emplace_back(effect);
437
438 if (composite.size() == maxSize) {
439 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
440 composite.clear();
441 vibrator->off();
442 }
443 }
444
445 if (composite.size() != 0) {
446 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
447 vibrator->off();
448 }
449 }
450}
451
452TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
453 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900454 auto unsupported = kInvalidPrimitives;
455 std::vector<CompositePrimitive> supported;
456
457 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
458
459 for (auto primitive : kCompositePrimitives) {
460 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000461 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900462
463 if (!isPrimitiveSupported) {
464 unsupported.push_back(primitive);
465 }
466 }
467
468 for (auto primitive : unsupported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900469 std::vector<CompositeEffect> composite(1);
470
Vince Leung823cf5f2021-02-11 02:21:57 +0000471 for (auto &effect : composite) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900472 effect.delayMs = 0;
473 effect.primitive = primitive;
474 effect.scale = 1.0f;
475 }
476 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
477 vibrator->compose(composite, nullptr).exceptionCode());
478 vibrator->off();
479 }
480 }
481}
482
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900483TEST_P(VibratorAidl, ComposeScaleBoundary) {
484 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
485 std::vector<CompositeEffect> composite(1);
Vince Leung823cf5f2021-02-11 02:21:57 +0000486 CompositeEffect &effect = composite[0];
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900487
488 effect.delayMs = 0;
489 effect.primitive = CompositePrimitive::CLICK;
490
491 effect.scale = std::nextafter(0.0f, -1.0f);
492 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
493 vibrator->compose(composite, nullptr).exceptionCode());
494
495 effect.scale = 0.0f;
496 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
497
498 effect.scale = 1.0f;
499 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
500
501 effect.scale = std::nextafter(1.0f, 2.0f);
502 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
503 vibrator->compose(composite, nullptr).exceptionCode());
504
505 vibrator->off();
506 }
507}
508
509TEST_P(VibratorAidl, ComposeDelayBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900510 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
511 int32_t maxDelay;
512
513 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
514
515 std::vector<CompositeEffect> composite(1);
516 CompositeEffect effect;
517
518 effect.delayMs = 1;
519 effect.primitive = CompositePrimitive::CLICK;
520 effect.scale = 1.0f;
521
522 std::fill(composite.begin(), composite.end(), effect);
523 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
524
525 effect.delayMs = maxDelay + 1;
526
527 std::fill(composite.begin(), composite.end(), effect);
528 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
529 vibrator->compose(composite, nullptr).exceptionCode());
530 vibrator->off();
531 }
532}
533
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900534TEST_P(VibratorAidl, ComposeSizeBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900535 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
536 int32_t maxSize;
537
538 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
539
540 std::vector<CompositeEffect> composite(maxSize);
541 CompositeEffect effect;
542
543 effect.delayMs = 1;
544 effect.primitive = CompositePrimitive::CLICK;
545 effect.scale = 1.0f;
546
547 std::fill(composite.begin(), composite.end(), effect);
548 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
549
550 composite.emplace_back(effect);
551 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
552 vibrator->compose(composite, nullptr).exceptionCode());
553 vibrator->off();
554 }
555}
556
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900557TEST_P(VibratorAidl, ComposeCallback) {
558 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900559 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900560
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900561 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900562
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900563 for (auto primitive : supported) {
564 if (primitive == CompositePrimitive::NOOP) {
565 continue;
566 }
567
568 std::promise<void> completionPromise;
569 std::future<void> completionFuture{completionPromise.get_future()};
570 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000571 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900572 CompositeEffect effect;
573 std::vector<CompositeEffect> composite;
574 int32_t durationMs;
575 std::chrono::milliseconds duration;
576 std::chrono::time_point<high_resolution_clock> start, end;
577 std::chrono::milliseconds elapsed;
578
579 effect.delayMs = 0;
580 effect.primitive = primitive;
581 effect.scale = 1.0f;
582 composite.emplace_back(effect);
583
584 EXPECT_EQ(Status::EX_NONE,
585 vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000586 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900587 duration = std::chrono::milliseconds(durationMs);
588
Vince Leung36f70d62021-04-09 21:37:22 +0000589 start = high_resolution_clock::now();
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900590 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000591 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900592
Vince Leung7b8606e2021-05-04 13:56:48 -0700593 //TODO(b/187207798): revert back to conservative timeout values once
594 //latencies have been fixed
595 EXPECT_EQ(completionFuture.wait_for(duration * 4), std::future_status::ready)
Vince Leung823cf5f2021-02-11 02:21:57 +0000596 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900597 end = high_resolution_clock::now();
598
599 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Vince Leung36f70d62021-04-09 21:37:22 +0000600 EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900601 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900602 }
603}
604
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900605TEST_P(VibratorAidl, AlwaysOn) {
606 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
607 std::vector<Effect> supported;
608 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
609
610 for (Effect effect : kEffects) {
611 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000612 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900613
614 for (EffectStrength strength : kEffectStrengths) {
615 Status status = vibrator->alwaysOnEnable(0, effect, strength);
616
617 if (isEffectSupported) {
618 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000619 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900620 } else {
621 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000622 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900623 }
624 }
625 }
626
627 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
628 }
629}
630
Vince Leung4bae4f92021-02-03 06:21:58 +0000631TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000632 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000633}
634
635TEST_P(VibratorAidl, GetQFactor) {
636 float qFactor;
637 Status status = vibrator->getQFactor(&qFactor);
638 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000639 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000640 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
641 } else {
642 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
643 }
644}
645
Vince Leung823cf5f2021-02-11 02:21:57 +0000646TEST_P(VibratorAidl, GetFrequencyResolution) {
647 getFrequencyResolutionHz(vibrator, capabilities);
648}
649
650TEST_P(VibratorAidl, GetFrequencyMinimum) {
651 getFrequencyMinimumHz(vibrator, capabilities);
652}
653
654TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
655 std::vector<float> bandwidthAmplitudeMap;
656 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
657 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
658 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
659 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
660
661 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
662 getFrequencyMinimumHz(vibrator, capabilities)) /
663 getFrequencyResolutionHz(vibrator, capabilities);
664 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
665
666 for (float e : bandwidthAmplitudeMap) {
667 ASSERT_GE(e, 0.0);
668 ASSERT_LE(e, 1.0);
669 }
670 } else {
671 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
672 }
673}
674
675TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
676 int32_t durationMs;
677 Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
678 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
679 ASSERT_NE(durationMs, 0);
680 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
681 } else {
682 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
683 }
684}
685
686TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
687 int32_t maxSize;
688 Status status = vibrator->getPwleCompositionSizeMax(&maxSize);
689 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
690 ASSERT_NE(maxSize, 0);
691 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
692 } else {
693 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
694 }
695}
696
697TEST_P(VibratorAidl, GetSupportedBraking) {
698 std::vector<Braking> supported;
699 Status status = vibrator->getSupportedBraking(&supported);
700 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
701 bool isDefaultNoneSupported =
702 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
703 ASSERT_TRUE(isDefaultNoneSupported);
704 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
705 } else {
706 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
707 }
708}
709
710TEST_P(VibratorAidl, ComposeValidPwle) {
711 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
712 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
713
714 std::vector<Braking> supported;
715 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
716 bool isClabSupported =
717 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
718 BrakingPwle braking;
719 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
720 braking.duration = 100;
721
722 std::vector<PrimitivePwle> pwleQueue;
723 PrimitivePwle pwle;
724 pwle = active;
725 pwleQueue.emplace_back(std::move(pwle));
726 pwle = braking;
727 pwleQueue.emplace_back(std::move(pwle));
728 pwle = active;
729 pwleQueue.emplace_back(std::move(pwle));
730
731 EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
732 vibrator->off();
733 }
734}
735
736TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
737 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
738 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
739 return;
740
741 std::promise<void> completionPromise;
742 std::future<void> completionFuture{completionPromise.get_future()};
743 sp<CompletionCallback> callback =
744 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
745 uint32_t durationMs = 2100; // Sum of 2 active and 1 braking below
Vince Leung7b8606e2021-05-04 13:56:48 -0700746 //TODO(b/187207798): revert back to conservative timeout values once
747 //latencies have been fixed
748 std::chrono::milliseconds timeout{durationMs * 4};
Vince Leung823cf5f2021-02-11 02:21:57 +0000749
750 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
751
752 std::vector<Braking> supported;
753 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
754 bool isClabSupported =
755 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
756 BrakingPwle braking;
757 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
758 braking.duration = 100;
759
760 std::vector<PrimitivePwle> pwleQueue;
761 PrimitivePwle pwle;
762 pwle = active;
763 pwleQueue.emplace_back(std::move(pwle));
764 pwle = braking;
765 pwleQueue.emplace_back(std::move(pwle));
766 pwle = active;
767 pwleQueue.emplace_back(std::move(pwle));
768
769 EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk());
770 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
771 EXPECT_TRUE(vibrator->off().isOk());
772}
773
774TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
775 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
776 std::vector<PrimitivePwle> pwleQueue;
777 // test empty queue
778 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
779 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
780 vibrator->off();
781
782 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
783
784 PrimitivePwle pwle;
785 pwle = active;
786 int segmentCountMax;
787 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
788
789 // Create PWLE queue with more segments than allowed
790 for (int i = 0; i < segmentCountMax + 10; i++) {
791 pwleQueue.emplace_back(std::move(pwle));
792 }
793
794 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
795 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
796 vibrator->off();
797 }
798}
799
800TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
801 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
802 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
803 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
804 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
805
806 std::vector<PrimitivePwle> pwleQueueGreater;
807 PrimitivePwle pwle;
808 pwle = active;
809 pwleQueueGreater.emplace_back(std::move(pwle));
810
811 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
812 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
813 vibrator->off();
814
815 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
816 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
817
818 std::vector<PrimitivePwle> pwleQueueLess;
819 pwle = active;
820 pwleQueueLess.emplace_back(std::move(pwle));
821
822 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
823 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
824 vibrator->off();
825 }
826}
827
828TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
829 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
830 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
831 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
832 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
833 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
834
835 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
836 active.startFrequency =
837 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
838 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
839
840 std::vector<PrimitivePwle> pwleQueueGreater;
841 PrimitivePwle pwle;
842 pwle = active;
843 pwleQueueGreater.emplace_back(std::move(pwle));
844
845 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
846 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
847 vibrator->off();
848
849 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
850 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
851
852 std::vector<PrimitivePwle> pwleQueueLess;
853 pwle = active;
854 pwleQueueLess.emplace_back(std::move(pwle));
855
856 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
857 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
858 vibrator->off();
859 }
860}
861
862TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
863 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
864 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
865
866 int segmentDurationMaxMs;
867 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
868 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
869
870 std::vector<PrimitivePwle> pwleQueue;
871 PrimitivePwle pwle;
872 pwle = active;
873 pwleQueue.emplace_back(std::move(pwle));
874
875 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
876 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
877 vibrator->off();
878 }
879}
880
Lais Andrade80b18612020-10-12 18:44:40 +0000881std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
882 std::vector<std::tuple<int32_t, int32_t>> tuples;
883 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
884 std::vector<int32_t> vibratorIds;
885
886 for (int i = 0; i < managerAidlNames.size(); i++) {
887 auto managerName = String16(managerAidlNames[i].c_str());
888 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
889 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000890 for (auto &vibratorId : vibratorIds) {
Lais Andrade80b18612020-10-12 18:44:40 +0000891 tuples.push_back(std::make_tuple(i, vibratorId));
892 }
893 }
894 }
895
896 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
897 for (int i = 0; i < vibratorAidlNames.size(); i++) {
898 tuples.push_back(std::make_tuple(-1, i));
899 }
900
901 return tuples;
902}
903
Vince Leung823cf5f2021-02-11 02:21:57 +0000904std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
905 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +0000906 if (managerIdx < 0) {
907 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
908 }
909 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
910 std::to_string(vibratorId);
911}
912
Dan Shiba4d5322020-07-28 13:09:30 -0700913GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +0000914INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
915 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -0700916
Vince Leung823cf5f2021-02-11 02:21:57 +0000917int main(int argc, char **argv) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700918 ::testing::InitGoogleTest(&argc, argv);
919 ProcessState::self()->setThreadPoolMaxThreadCount(1);
920 ProcessState::self()->startThreadPool();
921 return RUN_ALL_TESTS();
922}