blob: 4d03ebf9f9938d2ad3258c2f9a7f639c08026181 [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) {
556 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900557 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900558
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900559 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900560
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900561 for (auto primitive : supported) {
562 if (primitive == CompositePrimitive::NOOP) {
563 continue;
564 }
565
566 std::promise<void> completionPromise;
567 std::future<void> completionFuture{completionPromise.get_future()};
568 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000569 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900570 CompositeEffect effect;
571 std::vector<CompositeEffect> composite;
572 int32_t durationMs;
573 std::chrono::milliseconds duration;
574 std::chrono::time_point<high_resolution_clock> start, end;
575 std::chrono::milliseconds elapsed;
576
577 effect.delayMs = 0;
578 effect.primitive = primitive;
579 effect.scale = 1.0f;
580 composite.emplace_back(effect);
581
582 EXPECT_EQ(Status::EX_NONE,
583 vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000584 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900585 duration = std::chrono::milliseconds(durationMs);
586
Vince Leung36f70d62021-04-09 21:37:22 +0000587 start = high_resolution_clock::now();
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900588 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000589 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900590
Vince Leung36f70d62021-04-09 21:37:22 +0000591 EXPECT_EQ(completionFuture.wait_for(duration * 2), std::future_status::ready)
Vince Leung823cf5f2021-02-11 02:21:57 +0000592 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900593 end = high_resolution_clock::now();
594
595 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Vince Leung36f70d62021-04-09 21:37:22 +0000596 EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900597 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900598 }
599}
600
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900601TEST_P(VibratorAidl, AlwaysOn) {
602 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
603 std::vector<Effect> supported;
604 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
605
606 for (Effect effect : kEffects) {
607 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000608 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900609
610 for (EffectStrength strength : kEffectStrengths) {
611 Status status = vibrator->alwaysOnEnable(0, effect, strength);
612
613 if (isEffectSupported) {
614 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000615 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900616 } else {
617 EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000618 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900619 }
620 }
621 }
622
623 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
624 }
625}
626
Vince Leung4bae4f92021-02-03 06:21:58 +0000627TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000628 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000629}
630
631TEST_P(VibratorAidl, GetQFactor) {
632 float qFactor;
633 Status status = vibrator->getQFactor(&qFactor);
634 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000635 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000636 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
637 } else {
638 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
639 }
640}
641
Vince Leung823cf5f2021-02-11 02:21:57 +0000642TEST_P(VibratorAidl, GetFrequencyResolution) {
643 getFrequencyResolutionHz(vibrator, capabilities);
644}
645
646TEST_P(VibratorAidl, GetFrequencyMinimum) {
647 getFrequencyMinimumHz(vibrator, capabilities);
648}
649
650TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
651 std::vector<float> bandwidthAmplitudeMap;
652 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
653 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
654 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
655 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
656
657 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
658 getFrequencyMinimumHz(vibrator, capabilities)) /
659 getFrequencyResolutionHz(vibrator, capabilities);
660 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
661
662 for (float e : bandwidthAmplitudeMap) {
663 ASSERT_GE(e, 0.0);
664 ASSERT_LE(e, 1.0);
665 }
666 } else {
667 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
668 }
669}
670
671TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
672 int32_t durationMs;
673 Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
674 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
675 ASSERT_NE(durationMs, 0);
676 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
677 } else {
678 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
679 }
680}
681
682TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
683 int32_t maxSize;
684 Status status = vibrator->getPwleCompositionSizeMax(&maxSize);
685 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
686 ASSERT_NE(maxSize, 0);
687 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
688 } else {
689 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
690 }
691}
692
693TEST_P(VibratorAidl, GetSupportedBraking) {
694 std::vector<Braking> supported;
695 Status status = vibrator->getSupportedBraking(&supported);
696 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
697 bool isDefaultNoneSupported =
698 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
699 ASSERT_TRUE(isDefaultNoneSupported);
700 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
701 } else {
702 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
703 }
704}
705
706TEST_P(VibratorAidl, ComposeValidPwle) {
707 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
708 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
709
710 std::vector<Braking> supported;
711 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
712 bool isClabSupported =
713 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
714 BrakingPwle braking;
715 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
716 braking.duration = 100;
717
718 std::vector<PrimitivePwle> pwleQueue;
719 PrimitivePwle pwle;
720 pwle = active;
721 pwleQueue.emplace_back(std::move(pwle));
722 pwle = braking;
723 pwleQueue.emplace_back(std::move(pwle));
724 pwle = active;
725 pwleQueue.emplace_back(std::move(pwle));
726
727 EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
728 vibrator->off();
729 }
730}
731
732TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
733 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
734 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
735 return;
736
737 std::promise<void> completionPromise;
738 std::future<void> completionFuture{completionPromise.get_future()};
739 sp<CompletionCallback> callback =
740 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
741 uint32_t durationMs = 2100; // Sum of 2 active and 1 braking below
742 std::chrono::milliseconds timeout{durationMs * 2};
743
744 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
745
746 std::vector<Braking> supported;
747 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
748 bool isClabSupported =
749 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
750 BrakingPwle braking;
751 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
752 braking.duration = 100;
753
754 std::vector<PrimitivePwle> pwleQueue;
755 PrimitivePwle pwle;
756 pwle = active;
757 pwleQueue.emplace_back(std::move(pwle));
758 pwle = braking;
759 pwleQueue.emplace_back(std::move(pwle));
760 pwle = active;
761 pwleQueue.emplace_back(std::move(pwle));
762
763 EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk());
764 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
765 EXPECT_TRUE(vibrator->off().isOk());
766}
767
768TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
769 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
770 std::vector<PrimitivePwle> pwleQueue;
771 // test empty queue
772 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
773 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
774 vibrator->off();
775
776 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
777
778 PrimitivePwle pwle;
779 pwle = active;
780 int segmentCountMax;
781 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
782
783 // Create PWLE queue with more segments than allowed
784 for (int i = 0; i < segmentCountMax + 10; i++) {
785 pwleQueue.emplace_back(std::move(pwle));
786 }
787
788 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
789 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
790 vibrator->off();
791 }
792}
793
794TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
795 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
796 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
797 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
798 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
799
800 std::vector<PrimitivePwle> pwleQueueGreater;
801 PrimitivePwle pwle;
802 pwle = active;
803 pwleQueueGreater.emplace_back(std::move(pwle));
804
805 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
806 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
807 vibrator->off();
808
809 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
810 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
811
812 std::vector<PrimitivePwle> pwleQueueLess;
813 pwle = active;
814 pwleQueueLess.emplace_back(std::move(pwle));
815
816 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
817 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
818 vibrator->off();
819 }
820}
821
822TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
823 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
824 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
825 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
826 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
827 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
828
829 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
830 active.startFrequency =
831 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
832 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
833
834 std::vector<PrimitivePwle> pwleQueueGreater;
835 PrimitivePwle pwle;
836 pwle = active;
837 pwleQueueGreater.emplace_back(std::move(pwle));
838
839 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
840 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
841 vibrator->off();
842
843 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
844 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
845
846 std::vector<PrimitivePwle> pwleQueueLess;
847 pwle = active;
848 pwleQueueLess.emplace_back(std::move(pwle));
849
850 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
851 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
852 vibrator->off();
853 }
854}
855
856TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
857 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
858 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
859
860 int segmentDurationMaxMs;
861 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
862 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
863
864 std::vector<PrimitivePwle> pwleQueue;
865 PrimitivePwle pwle;
866 pwle = active;
867 pwleQueue.emplace_back(std::move(pwle));
868
869 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
870 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
871 vibrator->off();
872 }
873}
874
Lais Andrade80b18612020-10-12 18:44:40 +0000875std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
876 std::vector<std::tuple<int32_t, int32_t>> tuples;
877 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
878 std::vector<int32_t> vibratorIds;
879
880 for (int i = 0; i < managerAidlNames.size(); i++) {
881 auto managerName = String16(managerAidlNames[i].c_str());
882 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
883 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000884 for (auto &vibratorId : vibratorIds) {
Lais Andrade80b18612020-10-12 18:44:40 +0000885 tuples.push_back(std::make_tuple(i, vibratorId));
886 }
887 }
888 }
889
890 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
891 for (int i = 0; i < vibratorAidlNames.size(); i++) {
892 tuples.push_back(std::make_tuple(-1, i));
893 }
894
895 return tuples;
896}
897
Vince Leung823cf5f2021-02-11 02:21:57 +0000898std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
899 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +0000900 if (managerIdx < 0) {
901 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
902 }
903 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
904 std::to_string(vibratorId);
905}
906
Dan Shiba4d5322020-07-28 13:09:30 -0700907GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +0000908INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
909 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -0700910
Vince Leung823cf5f2021-02-11 02:21:57 +0000911int main(int argc, char **argv) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700912 ::testing::InitGoogleTest(&argc, argv);
913 ProcessState::self()->setThreadPoolMaxThreadCount(1);
914 ProcessState::self()->startThreadPool();
915 return RUN_ALL_TESTS();
916}