blob: a9d1ed5a51c6d80203346c727f327141b0365589 [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
279 std::chrono::milliseconds timeout{lengthMs * 2};
280 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
281 }
282 }
283}
284
285TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000286 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
287 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700288
289 for (Effect effect : kEffects) {
290 for (EffectStrength strength : kEffectStrengths) {
291 sp<CompletionCallback> callback = new CompletionCallback([] {});
292 int lengthMs;
293 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
294 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700295 }
296 }
297}
298
299TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
300 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800301 for (EffectStrength strength : kEffectStrengths) {
302 int32_t lengthMs;
303 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
304 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Vince Leung823cf5f2021-02-11 02:21:57 +0000305 << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800306 }
307 }
308 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700309 for (EffectStrength strength : kInvalidEffectStrengths) {
310 int32_t lengthMs;
311 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Morelandf3353882019-11-07 17:02:43 -0800312 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Vince Leung823cf5f2021-02-11 02:21:57 +0000313 << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700314 }
315 }
316}
317
318TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
319 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900320 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700321 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900322 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700323 sleep(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900324 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700325 sleep(1);
326 }
327}
328
329TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
330 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
331 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
332 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900333 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700334 }
335}
336
337TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
338 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
339 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode());
340 }
341}
342
343TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
344 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
345 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
346 sleep(1);
347 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
348 sleep(1);
349 }
350}
351
Steven Morelandc0b92d52019-11-05 13:31:41 -0800352TEST_P(VibratorAidl, ExternalAmplitudeControl) {
353 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000354 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800355
356 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
357 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
358
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900359 Status amplitudeStatus = vibrator->setAmplitude(0.5);
Steven Morelandc0b92d52019-11-05 13:31:41 -0800360 if (supportsExternalAmplitudeControl) {
361 EXPECT_TRUE(amplitudeStatus.isOk());
362 } else {
363 EXPECT_EQ(amplitudeStatus.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
364 }
365 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
366 } else {
367 EXPECT_FALSE(supportsExternalAmplitudeControl);
368 }
369}
370
Steven Morelandd44007e2019-10-24 18:12:46 -0700371TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
372 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
373 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
374 vibrator->setExternalControl(true).exceptionCode());
375 }
376}
377
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900378TEST_P(VibratorAidl, GetSupportedPrimitives) {
379 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
380 std::vector<CompositePrimitive> supported;
381
382 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
383
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900384 for (auto primitive : kCompositePrimitives) {
385 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000386 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900387 bool isPrimitiveOptional =
Vince Leung823cf5f2021-02-11 02:21:57 +0000388 std::find(kOptionalPrimitives.begin(), kOptionalPrimitives.end(), primitive) !=
389 kOptionalPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900390
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900391 EXPECT_TRUE(isPrimitiveSupported || isPrimitiveOptional) << toString(primitive);
392 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900393 }
394}
395
396TEST_P(VibratorAidl, GetPrimitiveDuration) {
397 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900398 std::vector<CompositePrimitive> supported;
399 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900400
401 for (auto primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900402 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000403 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900404 int32_t duration;
405
406 Status status = vibrator->getPrimitiveDuration(primitive, &duration);
407
408 if (isPrimitiveSupported) {
409 EXPECT_EQ(Status::EX_NONE, status.exceptionCode());
410 } else {
411 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode());
412 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900413 }
414 }
415}
416
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900417TEST_P(VibratorAidl, ComposeValidPrimitives) {
418 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900419 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900420 int32_t maxDelay, maxSize;
421
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900422 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900423 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
424 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
425
426 std::vector<CompositeEffect> composite;
427
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900428 for (auto primitive : supported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900429 CompositeEffect effect;
430
431 effect.delayMs = std::rand() % (maxDelay + 1);
432 effect.primitive = primitive;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900433 effect.scale = static_cast<float>(std::rand()) / RAND_MAX;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900434 composite.emplace_back(effect);
435
436 if (composite.size() == maxSize) {
437 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
438 composite.clear();
439 vibrator->off();
440 }
441 }
442
443 if (composite.size() != 0) {
444 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
445 vibrator->off();
446 }
447 }
448}
449
450TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
451 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900452 auto unsupported = kInvalidPrimitives;
453 std::vector<CompositePrimitive> supported;
454
455 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
456
457 for (auto primitive : kCompositePrimitives) {
458 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000459 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900460
461 if (!isPrimitiveSupported) {
462 unsupported.push_back(primitive);
463 }
464 }
465
466 for (auto primitive : unsupported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900467 std::vector<CompositeEffect> composite(1);
468
Vince Leung823cf5f2021-02-11 02:21:57 +0000469 for (auto &effect : composite) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900470 effect.delayMs = 0;
471 effect.primitive = primitive;
472 effect.scale = 1.0f;
473 }
474 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION,
475 vibrator->compose(composite, nullptr).exceptionCode());
476 vibrator->off();
477 }
478 }
479}
480
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900481TEST_P(VibratorAidl, ComposeScaleBoundary) {
482 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
483 std::vector<CompositeEffect> composite(1);
Vince Leung823cf5f2021-02-11 02:21:57 +0000484 CompositeEffect &effect = composite[0];
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900485
486 effect.delayMs = 0;
487 effect.primitive = CompositePrimitive::CLICK;
488
489 effect.scale = std::nextafter(0.0f, -1.0f);
490 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
491 vibrator->compose(composite, nullptr).exceptionCode());
492
493 effect.scale = 0.0f;
494 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
495
496 effect.scale = 1.0f;
497 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
498
499 effect.scale = std::nextafter(1.0f, 2.0f);
500 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
501 vibrator->compose(composite, nullptr).exceptionCode());
502
503 vibrator->off();
504 }
505}
506
507TEST_P(VibratorAidl, ComposeDelayBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900508 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
509 int32_t maxDelay;
510
511 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
512
513 std::vector<CompositeEffect> composite(1);
514 CompositeEffect effect;
515
516 effect.delayMs = 1;
517 effect.primitive = CompositePrimitive::CLICK;
518 effect.scale = 1.0f;
519
520 std::fill(composite.begin(), composite.end(), effect);
521 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
522
523 effect.delayMs = maxDelay + 1;
524
525 std::fill(composite.begin(), composite.end(), effect);
526 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
527 vibrator->compose(composite, nullptr).exceptionCode());
528 vibrator->off();
529 }
530}
531
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900532TEST_P(VibratorAidl, ComposeSizeBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900533 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
534 int32_t maxSize;
535
536 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
537
538 std::vector<CompositeEffect> composite(maxSize);
539 CompositeEffect effect;
540
541 effect.delayMs = 1;
542 effect.primitive = CompositePrimitive::CLICK;
543 effect.scale = 1.0f;
544
545 std::fill(composite.begin(), composite.end(), effect);
546 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
547
548 composite.emplace_back(effect);
549 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
550 vibrator->compose(composite, nullptr).exceptionCode());
551 vibrator->off();
552 }
553}
554
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900555TEST_P(VibratorAidl, ComposeCallback) {
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900556 constexpr std::chrono::milliseconds allowedLatency{10};
557
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900558 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
589 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000590 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900591 start = high_resolution_clock::now();
592
Vince Leung823cf5f2021-02-11 02:21:57 +0000593 EXPECT_EQ(completionFuture.wait_for(duration + allowedLatency), std::future_status::ready)
594 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900595 end = high_resolution_clock::now();
596
597 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
598 EXPECT_LE(elapsed.count(), (duration + allowedLatency).count()) << toString(primitive);
599 EXPECT_GE(elapsed.count(), (duration - allowedLatency).count()) << toString(primitive);
600 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900601 }
602}
603
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900604TEST_P(VibratorAidl, AlwaysOn) {
605 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
606 std::vector<Effect> supported;
607 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
608
609 for (Effect effect : kEffects) {
610 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000611 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900612
613 for (EffectStrength strength : kEffectStrengths) {
614 Status status = vibrator->alwaysOnEnable(0, effect, strength);
615
616 if (isEffectSupported) {
617 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000618 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900619 } else {
620 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000621 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900622 }
623 }
624 }
625
626 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
627 }
628}
629
Vince Leung4bae4f92021-02-03 06:21:58 +0000630TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000631 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000632}
633
634TEST_P(VibratorAidl, GetQFactor) {
635 float qFactor;
636 Status status = vibrator->getQFactor(&qFactor);
637 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000638 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000639 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
640 } else {
641 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
642 }
643}
644
Vince Leung823cf5f2021-02-11 02:21:57 +0000645TEST_P(VibratorAidl, GetFrequencyResolution) {
646 getFrequencyResolutionHz(vibrator, capabilities);
647}
648
649TEST_P(VibratorAidl, GetFrequencyMinimum) {
650 getFrequencyMinimumHz(vibrator, capabilities);
651}
652
653TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
654 std::vector<float> bandwidthAmplitudeMap;
655 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
656 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
657 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
658 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
659
660 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
661 getFrequencyMinimumHz(vibrator, capabilities)) /
662 getFrequencyResolutionHz(vibrator, capabilities);
663 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
664
665 for (float e : bandwidthAmplitudeMap) {
666 ASSERT_GE(e, 0.0);
667 ASSERT_LE(e, 1.0);
668 }
669 } else {
670 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
671 }
672}
673
674TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
675 int32_t durationMs;
676 Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
677 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
678 ASSERT_NE(durationMs, 0);
679 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
680 } else {
681 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
682 }
683}
684
685TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
686 int32_t maxSize;
687 Status status = vibrator->getPwleCompositionSizeMax(&maxSize);
688 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
689 ASSERT_NE(maxSize, 0);
690 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
691 } else {
692 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
693 }
694}
695
696TEST_P(VibratorAidl, GetSupportedBraking) {
697 std::vector<Braking> supported;
698 Status status = vibrator->getSupportedBraking(&supported);
699 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
700 bool isDefaultNoneSupported =
701 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
702 ASSERT_TRUE(isDefaultNoneSupported);
703 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
704 } else {
705 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
706 }
707}
708
709TEST_P(VibratorAidl, ComposeValidPwle) {
710 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
711 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
712
713 std::vector<Braking> supported;
714 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
715 bool isClabSupported =
716 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
717 BrakingPwle braking;
718 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
719 braking.duration = 100;
720
721 std::vector<PrimitivePwle> pwleQueue;
722 PrimitivePwle pwle;
723 pwle = active;
724 pwleQueue.emplace_back(std::move(pwle));
725 pwle = braking;
726 pwleQueue.emplace_back(std::move(pwle));
727 pwle = active;
728 pwleQueue.emplace_back(std::move(pwle));
729
730 EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
731 vibrator->off();
732 }
733}
734
735TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
736 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
737 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
738 return;
739
740 std::promise<void> completionPromise;
741 std::future<void> completionFuture{completionPromise.get_future()};
742 sp<CompletionCallback> callback =
743 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
744 uint32_t durationMs = 2100; // Sum of 2 active and 1 braking below
745 std::chrono::milliseconds timeout{durationMs * 2};
746
747 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
748
749 std::vector<Braking> supported;
750 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
751 bool isClabSupported =
752 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
753 BrakingPwle braking;
754 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
755 braking.duration = 100;
756
757 std::vector<PrimitivePwle> pwleQueue;
758 PrimitivePwle pwle;
759 pwle = active;
760 pwleQueue.emplace_back(std::move(pwle));
761 pwle = braking;
762 pwleQueue.emplace_back(std::move(pwle));
763 pwle = active;
764 pwleQueue.emplace_back(std::move(pwle));
765
766 EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk());
767 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
768 EXPECT_TRUE(vibrator->off().isOk());
769}
770
771TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
772 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
773 std::vector<PrimitivePwle> pwleQueue;
774 // test empty queue
775 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
776 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
777 vibrator->off();
778
779 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
780
781 PrimitivePwle pwle;
782 pwle = active;
783 int segmentCountMax;
784 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
785
786 // Create PWLE queue with more segments than allowed
787 for (int i = 0; i < segmentCountMax + 10; i++) {
788 pwleQueue.emplace_back(std::move(pwle));
789 }
790
791 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
792 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
793 vibrator->off();
794 }
795}
796
797TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
798 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
799 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
800 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
801 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
802
803 std::vector<PrimitivePwle> pwleQueueGreater;
804 PrimitivePwle pwle;
805 pwle = active;
806 pwleQueueGreater.emplace_back(std::move(pwle));
807
808 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
809 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
810 vibrator->off();
811
812 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
813 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
814
815 std::vector<PrimitivePwle> pwleQueueLess;
816 pwle = active;
817 pwleQueueLess.emplace_back(std::move(pwle));
818
819 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
820 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
821 vibrator->off();
822 }
823}
824
825TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
826 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
827 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
828 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
829 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
830 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
831
832 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
833 active.startFrequency =
834 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
835 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
836
837 std::vector<PrimitivePwle> pwleQueueGreater;
838 PrimitivePwle pwle;
839 pwle = active;
840 pwleQueueGreater.emplace_back(std::move(pwle));
841
842 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
843 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
844 vibrator->off();
845
846 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
847 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
848
849 std::vector<PrimitivePwle> pwleQueueLess;
850 pwle = active;
851 pwleQueueLess.emplace_back(std::move(pwle));
852
853 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
854 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
855 vibrator->off();
856 }
857}
858
859TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
860 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
861 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
862
863 int segmentDurationMaxMs;
864 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
865 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
866
867 std::vector<PrimitivePwle> pwleQueue;
868 PrimitivePwle pwle;
869 pwle = active;
870 pwleQueue.emplace_back(std::move(pwle));
871
872 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
873 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
874 vibrator->off();
875 }
876}
877
Lais Andrade80b18612020-10-12 18:44:40 +0000878std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
879 std::vector<std::tuple<int32_t, int32_t>> tuples;
880 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
881 std::vector<int32_t> vibratorIds;
882
883 for (int i = 0; i < managerAidlNames.size(); i++) {
884 auto managerName = String16(managerAidlNames[i].c_str());
885 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
886 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000887 for (auto &vibratorId : vibratorIds) {
Lais Andrade80b18612020-10-12 18:44:40 +0000888 tuples.push_back(std::make_tuple(i, vibratorId));
889 }
890 }
891 }
892
893 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
894 for (int i = 0; i < vibratorAidlNames.size(); i++) {
895 tuples.push_back(std::make_tuple(-1, i));
896 }
897
898 return tuples;
899}
900
Vince Leung823cf5f2021-02-11 02:21:57 +0000901std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
902 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +0000903 if (managerIdx < 0) {
904 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
905 }
906 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
907 std::to_string(vibratorId);
908}
909
Dan Shiba4d5322020-07-28 13:09:30 -0700910GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +0000911INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
912 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -0700913
Vince Leung823cf5f2021-02-11 02:21:57 +0000914int main(int argc, char **argv) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700915 ::testing::InitGoogleTest(&argc, argv);
916 ProcessState::self()->setThreadPoolMaxThreadCount(1);
917 ProcessState::self()->startThreadPool();
918 return RUN_ALL_TESTS();
919}