blob: 4d49a12661e2ef50ba68fc018d884b0792ba0fbd [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
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100115inline bool isUnknownOrUnsupported(Status status) {
116 return status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
117 status.transactionError() == android::UNKNOWN_TRANSACTION;
118}
119
Vince Leung823cf5f2021-02-11 02:21:57 +0000120static float getResonantFrequencyHz(sp<IVibrator> vibrator, int32_t capabilities) {
121 float resonantFrequencyHz;
122 Status status = vibrator->getResonantFrequency(&resonantFrequencyHz);
123 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
124 EXPECT_GT(resonantFrequencyHz, 0);
125 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
126 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100127 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000128 }
129 return resonantFrequencyHz;
130}
131
132static float getFrequencyResolutionHz(sp<IVibrator> vibrator, int32_t capabilities) {
133 float freqResolutionHz;
134 Status status = vibrator->getFrequencyResolution(&freqResolutionHz);
135 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
136 EXPECT_GT(freqResolutionHz, 0);
137 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
138 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100139 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000140 }
141 return freqResolutionHz;
142}
143
144static float getFrequencyMinimumHz(sp<IVibrator> vibrator, int32_t capabilities) {
145 float freqMinimumHz;
146 Status status = vibrator->getFrequencyMinimum(&freqMinimumHz);
147 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
148 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
149
150 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
151
152 EXPECT_GT(freqMinimumHz, 0);
153 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
154 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100155 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000156 }
157 return freqMinimumHz;
158}
159
160static float getFrequencyMaximumHz(sp<IVibrator> vibrator, int32_t capabilities) {
161 std::vector<float> bandwidthAmplitudeMap;
162 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
163 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
164 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
165 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100166 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000167 }
168
169 float freqMaximumHz =
170 (bandwidthAmplitudeMap.size() * getFrequencyResolutionHz(vibrator, capabilities)) +
171 getFrequencyMinimumHz(vibrator, capabilities);
172 return freqMaximumHz;
173}
174
175static float getAmplitudeMin() {
176 return 0.0;
177}
178
179static float getAmplitudeMax() {
180 return 1.0;
181}
182
183static ActivePwle composeValidActivePwle(sp<IVibrator> vibrator, int32_t capabilities) {
184 float frequencyHz;
185 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
186 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
187 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
188 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
189 } else {
190 frequencyHz = 150.0; // default value commonly used
191 }
192
193 ActivePwle active;
194 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
195 active.startFrequency = frequencyHz;
196 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
197 active.endFrequency = frequencyHz;
198 active.duration = 1000;
199
200 return active;
201}
202
Steven Morelandd44007e2019-10-24 18:12:46 -0700203TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
204 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
205 sleep(1);
206 EXPECT_TRUE(vibrator->off().isOk());
207}
208
209TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000210 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
211 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700212
213 std::promise<void> completionPromise;
214 std::future<void> completionFuture{completionPromise.get_future()};
215 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000216 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700217 uint32_t durationMs = 250;
218 std::chrono::milliseconds timeout{durationMs * 2};
219 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
220 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
221 EXPECT_TRUE(vibrator->off().isOk());
222}
223
224TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800225 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700226 sp<CompletionCallback> callback = new CompletionCallback([] {});
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100227 Status status = vibrator->on(250, callback);
228 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700229 }
230}
231
232TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800233 std::vector<Effect> supported;
234 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
235
Steven Morelandd44007e2019-10-24 18:12:46 -0700236 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800237 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000238 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800239
Steven Morelandd44007e2019-10-24 18:12:46 -0700240 for (EffectStrength strength : kEffectStrengths) {
241 int32_t lengthMs = 0;
242 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800243
244 if (isEffectSupported) {
Harpreet \"Eli\" Sanghae1723a42019-12-06 14:09:33 +0900245 EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700246 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800247 usleep(lengthMs * 1000);
Steven Morelandd44007e2019-10-24 18:12:46 -0700248 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100249 EXPECT_TRUE(isUnknownOrUnsupported(status))
250 << status << " " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700251 }
252 }
253 }
254}
255
256TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000257 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
258 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700259
Steven Moreland2932b222019-11-05 14:30:17 -0800260 std::vector<Effect> supported;
261 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
262
Steven Morelandd44007e2019-10-24 18:12:46 -0700263 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800264 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000265 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800266
Steven Morelandd44007e2019-10-24 18:12:46 -0700267 for (EffectStrength strength : kEffectStrengths) {
268 std::promise<void> completionPromise;
269 std::future<void> completionFuture{completionPromise.get_future()};
270 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000271 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800272 int lengthMs = 0;
Steven Morelandd44007e2019-10-24 18:12:46 -0700273 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800274
275 if (isEffectSupported) {
276 EXPECT_TRUE(status.isOk());
277 EXPECT_GT(lengthMs, 0);
278 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100279 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Moreland2932b222019-11-05 14:30:17 -0800280 }
281
Vince Leung823cf5f2021-02-11 02:21:57 +0000282 if (!status.isOk())
283 continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700284
Vince Leung7b8606e2021-05-04 13:56:48 -0700285 //TODO(b/187207798): revert back to conservative timeout values once
286 //latencies have been fixed
287 std::chrono::milliseconds timeout{lengthMs * 8};
Steven Morelandd44007e2019-10-24 18:12:46 -0700288 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
289 }
290 }
291}
292
293TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000294 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
295 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700296
297 for (Effect effect : kEffects) {
298 for (EffectStrength strength : kEffectStrengths) {
299 sp<CompletionCallback> callback = new CompletionCallback([] {});
300 int lengthMs;
301 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100302 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700303 }
304 }
305}
306
307TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
308 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800309 for (EffectStrength strength : kEffectStrengths) {
310 int32_t lengthMs;
311 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
312 EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION)
Vince Leung823cf5f2021-02-11 02:21:57 +0000313 << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800314 }
315 }
316 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700317 for (EffectStrength strength : kInvalidEffectStrengths) {
318 int32_t lengthMs;
319 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100320 EXPECT_TRUE(isUnknownOrUnsupported(status))
321 << status << " " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700322 }
323 }
324}
325
326TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
327 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900328 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700329 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900330 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700331 sleep(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900332 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700333 sleep(1);
334 }
335}
336
337TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
338 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
339 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
340 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900341 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
Steven Morelandd44007e2019-10-24 18:12:46 -0700342 }
343}
344
345TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
346 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100347 Status status = vibrator->setAmplitude(1);
348 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700349 }
350}
351
352TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
353 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
354 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
355 sleep(1);
356 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
357 sleep(1);
358 }
359}
360
Steven Morelandc0b92d52019-11-05 13:31:41 -0800361TEST_P(VibratorAidl, ExternalAmplitudeControl) {
362 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000363 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800364
365 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
366 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
367
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900368 Status amplitudeStatus = vibrator->setAmplitude(0.5);
Steven Morelandc0b92d52019-11-05 13:31:41 -0800369 if (supportsExternalAmplitudeControl) {
370 EXPECT_TRUE(amplitudeStatus.isOk());
371 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100372 EXPECT_TRUE(isUnknownOrUnsupported(amplitudeStatus)) << amplitudeStatus;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800373 }
374 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
375 } else {
376 EXPECT_FALSE(supportsExternalAmplitudeControl);
377 }
378}
379
Steven Morelandd44007e2019-10-24 18:12:46 -0700380TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
381 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100382 Status status = vibrator->setExternalControl(true);
383 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Steven Morelandd44007e2019-10-24 18:12:46 -0700384 }
385}
386
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900387TEST_P(VibratorAidl, GetSupportedPrimitives) {
388 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
389 std::vector<CompositePrimitive> supported;
390
391 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
392
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900393 for (auto primitive : kCompositePrimitives) {
394 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000395 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900396 bool isPrimitiveOptional =
Vince Leung823cf5f2021-02-11 02:21:57 +0000397 std::find(kOptionalPrimitives.begin(), kOptionalPrimitives.end(), primitive) !=
398 kOptionalPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900399
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900400 EXPECT_TRUE(isPrimitiveSupported || isPrimitiveOptional) << toString(primitive);
401 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900402 }
403}
404
405TEST_P(VibratorAidl, GetPrimitiveDuration) {
406 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900407 std::vector<CompositePrimitive> supported;
408 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900409
410 for (auto primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900411 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000412 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900413 int32_t duration;
414
415 Status status = vibrator->getPrimitiveDuration(primitive, &duration);
416
417 if (isPrimitiveSupported) {
418 EXPECT_EQ(Status::EX_NONE, status.exceptionCode());
419 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100420 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900421 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900422 }
423 }
424}
425
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900426TEST_P(VibratorAidl, ComposeValidPrimitives) {
427 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900428 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900429 int32_t maxDelay, maxSize;
430
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900431 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900432 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
433 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
434
435 std::vector<CompositeEffect> composite;
436
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900437 for (auto primitive : supported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900438 CompositeEffect effect;
439
440 effect.delayMs = std::rand() % (maxDelay + 1);
441 effect.primitive = primitive;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900442 effect.scale = static_cast<float>(std::rand()) / RAND_MAX;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900443 composite.emplace_back(effect);
444
445 if (composite.size() == maxSize) {
446 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
447 composite.clear();
448 vibrator->off();
449 }
450 }
451
452 if (composite.size() != 0) {
453 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
454 vibrator->off();
455 }
456 }
457}
458
459TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
460 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900461 auto unsupported = kInvalidPrimitives;
462 std::vector<CompositePrimitive> supported;
463
464 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
465
466 for (auto primitive : kCompositePrimitives) {
467 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000468 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900469
470 if (!isPrimitiveSupported) {
471 unsupported.push_back(primitive);
472 }
473 }
474
475 for (auto primitive : unsupported) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900476 std::vector<CompositeEffect> composite(1);
477
Vince Leung823cf5f2021-02-11 02:21:57 +0000478 for (auto &effect : composite) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900479 effect.delayMs = 0;
480 effect.primitive = primitive;
481 effect.scale = 1.0f;
482 }
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100483 Status status = vibrator->compose(composite, nullptr);
484 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900485 vibrator->off();
486 }
487 }
488}
489
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900490TEST_P(VibratorAidl, ComposeScaleBoundary) {
491 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
492 std::vector<CompositeEffect> composite(1);
Vince Leung823cf5f2021-02-11 02:21:57 +0000493 CompositeEffect &effect = composite[0];
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900494
495 effect.delayMs = 0;
496 effect.primitive = CompositePrimitive::CLICK;
497
498 effect.scale = std::nextafter(0.0f, -1.0f);
499 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
500 vibrator->compose(composite, nullptr).exceptionCode());
501
502 effect.scale = 0.0f;
503 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
504
505 effect.scale = 1.0f;
506 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
507
508 effect.scale = std::nextafter(1.0f, 2.0f);
509 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
510 vibrator->compose(composite, nullptr).exceptionCode());
511
512 vibrator->off();
513 }
514}
515
516TEST_P(VibratorAidl, ComposeDelayBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900517 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
518 int32_t maxDelay;
519
520 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
521
522 std::vector<CompositeEffect> composite(1);
523 CompositeEffect effect;
524
525 effect.delayMs = 1;
526 effect.primitive = CompositePrimitive::CLICK;
527 effect.scale = 1.0f;
528
529 std::fill(composite.begin(), composite.end(), effect);
530 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
531
532 effect.delayMs = maxDelay + 1;
533
534 std::fill(composite.begin(), composite.end(), effect);
535 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
536 vibrator->compose(composite, nullptr).exceptionCode());
537 vibrator->off();
538 }
539}
540
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900541TEST_P(VibratorAidl, ComposeSizeBoundary) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900542 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
543 int32_t maxSize;
544
545 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
546
547 std::vector<CompositeEffect> composite(maxSize);
548 CompositeEffect effect;
549
550 effect.delayMs = 1;
551 effect.primitive = CompositePrimitive::CLICK;
552 effect.scale = 1.0f;
553
554 std::fill(composite.begin(), composite.end(), effect);
555 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
556
557 composite.emplace_back(effect);
558 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
559 vibrator->compose(composite, nullptr).exceptionCode());
560 vibrator->off();
561 }
562}
563
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900564TEST_P(VibratorAidl, ComposeCallback) {
565 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900566 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900567
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900568 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900569
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900570 for (auto primitive : supported) {
571 if (primitive == CompositePrimitive::NOOP) {
572 continue;
573 }
574
575 std::promise<void> completionPromise;
576 std::future<void> completionFuture{completionPromise.get_future()};
577 sp<CompletionCallback> callback =
Vince Leung823cf5f2021-02-11 02:21:57 +0000578 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900579 CompositeEffect effect;
580 std::vector<CompositeEffect> composite;
581 int32_t durationMs;
582 std::chrono::milliseconds duration;
583 std::chrono::time_point<high_resolution_clock> start, end;
584 std::chrono::milliseconds elapsed;
585
586 effect.delayMs = 0;
587 effect.primitive = primitive;
588 effect.scale = 1.0f;
589 composite.emplace_back(effect);
590
591 EXPECT_EQ(Status::EX_NONE,
592 vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000593 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900594 duration = std::chrono::milliseconds(durationMs);
595
Vince Leung36f70d62021-04-09 21:37:22 +0000596 start = high_resolution_clock::now();
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900597 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000598 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900599
Vince Leung7b8606e2021-05-04 13:56:48 -0700600 //TODO(b/187207798): revert back to conservative timeout values once
601 //latencies have been fixed
602 EXPECT_EQ(completionFuture.wait_for(duration * 4), std::future_status::ready)
Vince Leung823cf5f2021-02-11 02:21:57 +0000603 << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900604 end = high_resolution_clock::now();
605
606 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Vince Leung36f70d62021-04-09 21:37:22 +0000607 EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900608 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900609 }
610}
611
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900612TEST_P(VibratorAidl, AlwaysOn) {
613 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
614 std::vector<Effect> supported;
615 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
616
617 for (Effect effect : kEffects) {
618 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000619 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900620
621 for (EffectStrength strength : kEffectStrengths) {
622 Status status = vibrator->alwaysOnEnable(0, effect, strength);
623
624 if (isEffectSupported) {
625 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
Vince Leung823cf5f2021-02-11 02:21:57 +0000626 << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900627 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100628 EXPECT_TRUE(isUnknownOrUnsupported(status))
629 << status << " " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900630 }
631 }
632 }
633
634 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
635 }
636}
637
Vince Leung4bae4f92021-02-03 06:21:58 +0000638TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000639 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000640}
641
642TEST_P(VibratorAidl, GetQFactor) {
643 float qFactor;
644 Status status = vibrator->getQFactor(&qFactor);
645 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000646 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000647 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
648 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100649 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung4bae4f92021-02-03 06:21:58 +0000650 }
651}
652
Vince Leung823cf5f2021-02-11 02:21:57 +0000653TEST_P(VibratorAidl, GetFrequencyResolution) {
654 getFrequencyResolutionHz(vibrator, capabilities);
655}
656
657TEST_P(VibratorAidl, GetFrequencyMinimum) {
658 getFrequencyMinimumHz(vibrator, capabilities);
659}
660
661TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
662 std::vector<float> bandwidthAmplitudeMap;
663 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
664 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
665 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
666 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
667
668 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
669 getFrequencyMinimumHz(vibrator, capabilities)) /
670 getFrequencyResolutionHz(vibrator, capabilities);
671 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
672
673 for (float e : bandwidthAmplitudeMap) {
674 ASSERT_GE(e, 0.0);
675 ASSERT_LE(e, 1.0);
676 }
677 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100678 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000679 }
680}
681
682TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
683 int32_t durationMs;
684 Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
685 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
686 ASSERT_NE(durationMs, 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, GetPwleCompositionSizeMax) {
694 int32_t maxSize;
695 Status status = vibrator->getPwleCompositionSizeMax(&maxSize);
696 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
697 ASSERT_NE(maxSize, 0);
698 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
699 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100700 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000701 }
702}
703
704TEST_P(VibratorAidl, GetSupportedBraking) {
705 std::vector<Braking> supported;
706 Status status = vibrator->getSupportedBraking(&supported);
707 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
708 bool isDefaultNoneSupported =
709 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
710 ASSERT_TRUE(isDefaultNoneSupported);
711 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
712 } else {
Lais Andrade4b54b1f2021-06-10 16:38:34 +0100713 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
Vince Leung823cf5f2021-02-11 02:21:57 +0000714 }
715}
716
717TEST_P(VibratorAidl, ComposeValidPwle) {
718 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
719 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
720
721 std::vector<Braking> supported;
722 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
723 bool isClabSupported =
724 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
725 BrakingPwle braking;
726 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
727 braking.duration = 100;
728
729 std::vector<PrimitivePwle> pwleQueue;
730 PrimitivePwle pwle;
731 pwle = active;
732 pwleQueue.emplace_back(std::move(pwle));
733 pwle = braking;
734 pwleQueue.emplace_back(std::move(pwle));
735 pwle = active;
736 pwleQueue.emplace_back(std::move(pwle));
737
738 EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
739 vibrator->off();
740 }
741}
742
743TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
744 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
745 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
746 return;
747
748 std::promise<void> completionPromise;
749 std::future<void> completionFuture{completionPromise.get_future()};
750 sp<CompletionCallback> callback =
751 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
752 uint32_t durationMs = 2100; // Sum of 2 active and 1 braking below
Vince Leung7b8606e2021-05-04 13:56:48 -0700753 //TODO(b/187207798): revert back to conservative timeout values once
754 //latencies have been fixed
755 std::chrono::milliseconds timeout{durationMs * 4};
Vince Leung823cf5f2021-02-11 02:21:57 +0000756
757 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
758
759 std::vector<Braking> supported;
760 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
761 bool isClabSupported =
762 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
763 BrakingPwle braking;
764 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
765 braking.duration = 100;
766
767 std::vector<PrimitivePwle> pwleQueue;
768 PrimitivePwle pwle;
769 pwle = active;
770 pwleQueue.emplace_back(std::move(pwle));
771 pwle = braking;
772 pwleQueue.emplace_back(std::move(pwle));
773 pwle = active;
774 pwleQueue.emplace_back(std::move(pwle));
775
776 EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk());
777 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
778 EXPECT_TRUE(vibrator->off().isOk());
779}
780
781TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
782 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
783 std::vector<PrimitivePwle> pwleQueue;
784 // test empty queue
785 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
786 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
787 vibrator->off();
788
789 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
790
791 PrimitivePwle pwle;
792 pwle = active;
793 int segmentCountMax;
794 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
795
796 // Create PWLE queue with more segments than allowed
797 for (int i = 0; i < segmentCountMax + 10; i++) {
798 pwleQueue.emplace_back(std::move(pwle));
799 }
800
801 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
802 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
803 vibrator->off();
804 }
805}
806
807TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
808 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
809 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
810 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
811 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
812
813 std::vector<PrimitivePwle> pwleQueueGreater;
814 PrimitivePwle pwle;
815 pwle = active;
816 pwleQueueGreater.emplace_back(std::move(pwle));
817
818 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
819 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
820 vibrator->off();
821
822 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
823 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
824
825 std::vector<PrimitivePwle> pwleQueueLess;
826 pwle = active;
827 pwleQueueLess.emplace_back(std::move(pwle));
828
829 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
830 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
831 vibrator->off();
832 }
833}
834
835TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
836 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
837 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
838 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
839 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
840 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
841
842 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
843 active.startFrequency =
844 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
845 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
846
847 std::vector<PrimitivePwle> pwleQueueGreater;
848 PrimitivePwle pwle;
849 pwle = active;
850 pwleQueueGreater.emplace_back(std::move(pwle));
851
852 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
853 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
854 vibrator->off();
855
856 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
857 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
858
859 std::vector<PrimitivePwle> pwleQueueLess;
860 pwle = active;
861 pwleQueueLess.emplace_back(std::move(pwle));
862
863 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
864 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
865 vibrator->off();
866 }
867}
868
869TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
870 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
871 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
872
873 int segmentDurationMaxMs;
874 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
875 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
876
877 std::vector<PrimitivePwle> pwleQueue;
878 PrimitivePwle pwle;
879 pwle = active;
880 pwleQueue.emplace_back(std::move(pwle));
881
882 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
883 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
884 vibrator->off();
885 }
886}
887
Lais Andrade80b18612020-10-12 18:44:40 +0000888std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
889 std::vector<std::tuple<int32_t, int32_t>> tuples;
890 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
891 std::vector<int32_t> vibratorIds;
892
893 for (int i = 0; i < managerAidlNames.size(); i++) {
894 auto managerName = String16(managerAidlNames[i].c_str());
895 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
896 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000897 for (auto &vibratorId : vibratorIds) {
Lais Andrade80b18612020-10-12 18:44:40 +0000898 tuples.push_back(std::make_tuple(i, vibratorId));
899 }
900 }
901 }
902
903 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
904 for (int i = 0; i < vibratorAidlNames.size(); i++) {
905 tuples.push_back(std::make_tuple(-1, i));
906 }
907
908 return tuples;
909}
910
Vince Leung823cf5f2021-02-11 02:21:57 +0000911std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
912 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +0000913 if (managerIdx < 0) {
914 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
915 }
916 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
917 std::to_string(vibratorId);
918}
919
Dan Shiba4d5322020-07-28 13:09:30 -0700920GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +0000921INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
922 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -0700923
Vince Leung823cf5f2021-02-11 02:21:57 +0000924int main(int argc, char **argv) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700925 ::testing::InitGoogleTest(&argc, argv);
926 ProcessState::self()->setThreadPoolMaxThreadCount(1);
927 ProcessState::self()->startThreadPool();
928 return RUN_ALL_TESTS();
929}