blob: 706ab416b3ab2f7bf25eab2c0295ca5c7ebe156f [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>
Lais Andrade28c81f12024-06-24 14:32:22 +010018#include <aidl/android/hardware/vibrator/BnVibratorCallback.h>
19#include <aidl/android/hardware/vibrator/IVibrator.h>
20#include <aidl/android/hardware/vibrator/IVibratorManager.h>
21
22#include <android/binder_manager.h>
23#include <android/binder_process.h>
Lais Andradea3c332f2024-06-20 10:46:06 +010024#include <android/persistable_bundle_aidl.h>
Steven Morelandd44007e2019-10-24 18:12:46 -070025
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090026#include <cmath>
Lais Andradea3c332f2024-06-20 10:46:06 +010027#include <cstdlib>
28#include <ctime>
Steven Morelandd44007e2019-10-24 18:12:46 -070029#include <future>
30
Lais Andradea3c332f2024-06-20 10:46:06 +010031#include "persistable_bundle_utils.h"
Lais Andrade28c81f12024-06-24 14:32:22 +010032#include "test_utils.h"
33
34using aidl::android::hardware::vibrator::ActivePwle;
35using aidl::android::hardware::vibrator::BnVibratorCallback;
36using aidl::android::hardware::vibrator::Braking;
37using aidl::android::hardware::vibrator::BrakingPwle;
38using aidl::android::hardware::vibrator::CompositeEffect;
39using aidl::android::hardware::vibrator::CompositePrimitive;
40using aidl::android::hardware::vibrator::Effect;
41using aidl::android::hardware::vibrator::EffectStrength;
42using aidl::android::hardware::vibrator::IVibrator;
43using aidl::android::hardware::vibrator::IVibratorManager;
44using aidl::android::hardware::vibrator::PrimitivePwle;
Lais Andradea3c332f2024-06-20 10:46:06 +010045using aidl::android::hardware::vibrator::VendorEffect;
46using aidl::android::os::PersistableBundle;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +090047using std::chrono::high_resolution_clock;
Steven Morelandd44007e2019-10-24 18:12:46 -070048
Lais Andradec689ba52024-04-10 11:07:11 +010049using namespace ::std::chrono_literals;
50
Lais Andrade28c81f12024-06-24 14:32:22 +010051const std::vector<Effect> kEffects{ndk::enum_range<Effect>().begin(),
52 ndk::enum_range<Effect>().end()};
53const std::vector<EffectStrength> kEffectStrengths{ndk::enum_range<EffectStrength>().begin(),
54 ndk::enum_range<EffectStrength>().end()};
Steven Morelandd44007e2019-10-24 18:12:46 -070055
56const std::vector<Effect> kInvalidEffects = {
Vince Leung823cf5f2021-02-11 02:21:57 +000057 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
58 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070059};
60
61const std::vector<EffectStrength> kInvalidEffectStrengths = {
Vince Leung823cf5f2021-02-11 02:21:57 +000062 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
63 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070064};
65
Steven Moreland1c269782020-01-09 11:16:05 -080066const std::vector<CompositePrimitive> kCompositePrimitives{
Lais Andrade28c81f12024-06-24 14:32:22 +010067 ndk::enum_range<CompositePrimitive>().begin(), ndk::enum_range<CompositePrimitive>().end()};
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090068
Lais Andrade7e643772021-07-09 14:59:44 +010069const std::vector<CompositePrimitive> kRequiredPrimitives = {
70 CompositePrimitive::CLICK, CompositePrimitive::LIGHT_TICK,
71 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
72 CompositePrimitive::QUICK_FALL,
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090073};
74
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090075const std::vector<CompositePrimitive> kInvalidPrimitives = {
Vince Leung823cf5f2021-02-11 02:21:57 +000076 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
77 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090078};
79
Lais Andradec689ba52024-04-10 11:07:11 +010080// Timeout to wait for vibration callback completion.
Lais Andrade28c81f12024-06-24 14:32:22 +010081static constexpr std::chrono::milliseconds VIBRATION_CALLBACK_TIMEOUT = 100ms;
82
83static std::vector<std::string> findVibratorManagerNames() {
84 std::vector<std::string> names;
85 constexpr auto callback = [](const char* instance, void* context) {
86 auto fullName = std::string(IVibratorManager::descriptor) + "/" + instance;
87 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
88 };
89 AServiceManager_forEachDeclaredInstance(IVibratorManager::descriptor,
90 static_cast<void*>(&names), callback);
91 return names;
92}
93
94static std::vector<std::string> findUnmanagedVibratorNames() {
95 std::vector<std::string> names;
96 constexpr auto callback = [](const char* instance, void* context) {
97 auto fullName = std::string(IVibrator::descriptor) + "/" + instance;
98 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
99 };
100 AServiceManager_forEachDeclaredInstance(IVibrator::descriptor, static_cast<void*>(&names),
101 callback);
102 return names;
103}
Lais Andradec689ba52024-04-10 11:07:11 +0100104
Steven Morelandd44007e2019-10-24 18:12:46 -0700105class CompletionCallback : public BnVibratorCallback {
106 public:
Vince Leung823cf5f2021-02-11 02:21:57 +0000107 CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {}
Lais Andrade28c81f12024-06-24 14:32:22 +0100108 ndk::ScopedAStatus onComplete() override {
Steven Morelandd44007e2019-10-24 18:12:46 -0700109 mCallback();
Lais Andrade28c81f12024-06-24 14:32:22 +0100110 return ndk::ScopedAStatus::ok();
Steven Morelandd44007e2019-10-24 18:12:46 -0700111 }
112
113 private:
114 std::function<void()> mCallback;
115};
116
Lais Andrade80b18612020-10-12 18:44:40 +0000117class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> {
Steven Morelandd44007e2019-10-24 18:12:46 -0700118 public:
119 virtual void SetUp() override {
Lais Andrade80b18612020-10-12 18:44:40 +0000120 int32_t managerIdx = std::get<0>(GetParam());
121 int32_t vibratorId = std::get<1>(GetParam());
Lais Andrade80b18612020-10-12 18:44:40 +0000122
123 if (managerIdx < 0) {
124 // Testing a unmanaged vibrator, using vibratorId as index from registered HALs
Lais Andrade28c81f12024-06-24 14:32:22 +0100125 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
126 ASSERT_LT(vibratorId, vibratorNames.size());
127 vibrator = IVibrator::fromBinder(ndk::SpAIBinder(
128 AServiceManager_waitForService(vibratorNames[vibratorId].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +0000129 } else {
130 // Testing a managed vibrator, using vibratorId to retrieve it from the manager
Lais Andrade28c81f12024-06-24 14:32:22 +0100131 std::vector<std::string> managerNames = findVibratorManagerNames();
132 ASSERT_LT(managerIdx, managerNames.size());
133 auto vibratorManager = IVibratorManager::fromBinder(ndk::SpAIBinder(
134 AServiceManager_waitForService(managerNames[managerIdx].c_str())));
135 EXPECT_OK(vibratorManager->getVibrator(vibratorId, &vibrator))
136 << "\n For vibrator id: " << vibratorId;
Lais Andrade80b18612020-10-12 18:44:40 +0000137 }
138
Steven Morelandd44007e2019-10-24 18:12:46 -0700139 ASSERT_NE(vibrator, nullptr);
Lais Andrade28c81f12024-06-24 14:32:22 +0100140 EXPECT_OK(vibrator->getCapabilities(&capabilities));
Steven Morelandd44007e2019-10-24 18:12:46 -0700141 }
142
Lais Andrade38d054e2024-02-09 12:42:09 +0000143 virtual void TearDown() override {
144 // Reset vibrator state between tests.
Lais Andrade28c81f12024-06-24 14:32:22 +0100145 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000146 }
147
Lais Andrade28c81f12024-06-24 14:32:22 +0100148 std::shared_ptr<IVibrator> vibrator;
Steven Morelandd44007e2019-10-24 18:12:46 -0700149 int32_t capabilities;
150};
151
Lais Andrade28c81f12024-06-24 14:32:22 +0100152static float getResonantFrequencyHz(const std::shared_ptr<IVibrator>& vibrator,
153 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000154 float resonantFrequencyHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100155 ndk::ScopedAStatus status = vibrator->getResonantFrequency(&resonantFrequencyHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000156 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100157 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000158 EXPECT_GT(resonantFrequencyHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000159 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100160 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000161 }
162 return resonantFrequencyHz;
163}
164
Lais Andrade28c81f12024-06-24 14:32:22 +0100165static float getFrequencyResolutionHz(const std::shared_ptr<IVibrator>& vibrator,
166 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000167 float freqResolutionHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100168 ndk::ScopedAStatus status = vibrator->getFrequencyResolution(&freqResolutionHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000169 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100170 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000171 EXPECT_GT(freqResolutionHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000172 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100173 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000174 }
175 return freqResolutionHz;
176}
177
Lais Andrade28c81f12024-06-24 14:32:22 +0100178static float getFrequencyMinimumHz(const std::shared_ptr<IVibrator>& vibrator,
179 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000180 float freqMinimumHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100181 ndk::ScopedAStatus status = vibrator->getFrequencyMinimum(&freqMinimumHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000182 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100183 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000184
185 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
186
187 EXPECT_GT(freqMinimumHz, 0);
188 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
189 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100190 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000191 }
192 return freqMinimumHz;
193}
194
Lais Andrade28c81f12024-06-24 14:32:22 +0100195static float getFrequencyMaximumHz(const std::shared_ptr<IVibrator>& vibrator,
196 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000197 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100198 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000199 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100200 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000201 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100202 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000203 }
204
chasewu70da3cc2022-03-15 15:16:04 +0800205 float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) *
206 getFrequencyResolutionHz(vibrator, capabilities)) +
207 getFrequencyMinimumHz(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000208 return freqMaximumHz;
209}
210
211static float getAmplitudeMin() {
212 return 0.0;
213}
214
215static float getAmplitudeMax() {
216 return 1.0;
217}
218
Lais Andrade28c81f12024-06-24 14:32:22 +0100219static ActivePwle composeValidActivePwle(const std::shared_ptr<IVibrator>& vibrator,
220 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000221 float frequencyHz;
222 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
223 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
224 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
225 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
226 } else {
227 frequencyHz = 150.0; // default value commonly used
228 }
229
230 ActivePwle active;
231 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
232 active.startFrequency = frequencyHz;
233 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
234 active.endFrequency = frequencyHz;
chasewu22cb9012022-03-31 23:23:27 +0800235 vibrator->getPwlePrimitiveDurationMax(&(active.duration));
Vince Leung823cf5f2021-02-11 02:21:57 +0000236
237 return active;
238}
239
Steven Morelandd44007e2019-10-24 18:12:46 -0700240TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100241 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
Steven Morelandd44007e2019-10-24 18:12:46 -0700242 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100243 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700244}
245
246TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000247 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
248 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700249
250 std::promise<void> completionPromise;
251 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100252 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
253 [&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700254 uint32_t durationMs = 250;
Lais Andradec689ba52024-04-10 11:07:11 +0100255 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Lais Andrade28c81f12024-06-24 14:32:22 +0100256 EXPECT_OK(vibrator->on(durationMs, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700257 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100258 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700259}
260
261TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800262 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100263 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
264 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->on(250, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700265 }
266}
267
268TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800269 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100270 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800271
Steven Morelandd44007e2019-10-24 18:12:46 -0700272 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800273 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000274 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800275
Steven Morelandd44007e2019-10-24 18:12:46 -0700276 for (EffectStrength strength : kEffectStrengths) {
277 int32_t lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100278 ndk::ScopedAStatus status =
279 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800280
281 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100282 EXPECT_OK(std::move(status))
283 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700284 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800285 usleep(lengthMs * 1000);
Lais Andrade28c81f12024-06-24 14:32:22 +0100286 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700287 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100288 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
289 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700290 }
291 }
292 }
293}
294
295TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000296 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
297 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700298
Steven Moreland2932b222019-11-05 14:30:17 -0800299 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100300 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800301
Steven Morelandd44007e2019-10-24 18:12:46 -0700302 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800303 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000304 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800305
Steven Morelandd44007e2019-10-24 18:12:46 -0700306 for (EffectStrength strength : kEffectStrengths) {
307 std::promise<void> completionPromise;
308 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100309 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
310 [&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800311 int lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100312 ndk::ScopedAStatus status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800313
314 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100315 EXPECT_OK(std::move(status))
316 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800317 EXPECT_GT(lengthMs, 0);
318 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100319 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
320 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800321 }
322
Lais Andrade28c81f12024-06-24 14:32:22 +0100323 if (lengthMs <= 0) continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700324
Lais Andradec689ba52024-04-10 11:07:11 +0100325 auto timeout = std::chrono::milliseconds(lengthMs) + VIBRATION_CALLBACK_TIMEOUT;
Steven Morelandd44007e2019-10-24 18:12:46 -0700326 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andradec689ba52024-04-10 11:07:11 +0100327
Lais Andrade28c81f12024-06-24 14:32:22 +0100328 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700329 }
330 }
331}
332
333TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000334 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
335 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700336
337 for (Effect effect : kEffects) {
338 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100339 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
Steven Morelandd44007e2019-10-24 18:12:46 -0700340 int lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100341 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->perform(effect, strength, callback, &lengthMs))
342 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700343 }
344 }
345}
346
347TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
348 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800349 for (EffectStrength strength : kEffectStrengths) {
350 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100351 EXPECT_UNKNOWN_OR_UNSUPPORTED(
352 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
353 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800354 }
355 }
356 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700357 for (EffectStrength strength : kInvalidEffectStrengths) {
358 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100359 EXPECT_UNKNOWN_OR_UNSUPPORTED(
360 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
361 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700362 }
363 }
364}
365
Lais Andradea3c332f2024-06-20 10:46:06 +0100366TEST_P(VibratorAidl, PerformVendorEffectSupported) {
367 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
368
369 float scale = 0.5f;
370 for (EffectStrength strength : kEffectStrengths) {
371 PersistableBundle vendorData;
372 ::aidl::android::hardware::vibrator::testing::fillBasicData(&vendorData);
373
374 PersistableBundle nestedData;
375 ::aidl::android::hardware::vibrator::testing::fillBasicData(&nestedData);
376 vendorData.putPersistableBundle("test_nested_bundle", nestedData);
377
378 VendorEffect effect;
379 effect.vendorData = vendorData;
380 effect.strength = strength;
381 effect.scale = scale;
382 scale *= 1.5f;
383
384 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
385 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
386
387 // No expectations on the actual status, the effect might be refused with illegal argument
388 // or the vendor might return a service-specific error code.
389 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
390 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
391 << status << "\n For vendor effect with strength" << toString(strength)
392 << " and scale " << effect.scale;
393
394 if (status.isOk()) {
395 // Generic vendor data should not trigger vibrations, but if it does trigger one
396 // then we make sure the vibrator is reset by triggering off().
397 EXPECT_OK(vibrator->off());
398 }
399 }
400}
401
402TEST_P(VibratorAidl, PerformVendorEffectStability) {
403 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
404
405 // Run some iterations of performVendorEffect with randomized vendor data to check basic
406 // stability of the implementation.
407 uint8_t iterations = 200;
408
409 for (EffectStrength strength : kEffectStrengths) {
410 float scale = 0.5f;
411 for (uint8_t i = 0; i < iterations; i++) {
412 PersistableBundle vendorData;
413 ::aidl::android::hardware::vibrator::testing::fillRandomData(&vendorData);
414
415 VendorEffect effect;
416 effect.vendorData = vendorData;
417 effect.strength = strength;
418 effect.scale = scale;
419 scale *= 2;
420
421 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
422 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
423
424 // No expectations on the actual status, the effect might be refused with illegal
425 // argument or the vendor might return a service-specific error code.
426 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
427 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
428 << status << "\n For random vendor effect with strength " << toString(strength)
429 << " and scale " << effect.scale;
430
431 if (status.isOk()) {
432 // Random vendor data should not trigger vibrations, but if it does trigger one
433 // then we make sure the vibrator is reset by triggering off().
434 EXPECT_OK(vibrator->off());
435 }
436 }
437 }
438}
439
440TEST_P(VibratorAidl, PerformVendorEffectEmptyVendorData) {
441 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
442
443 for (EffectStrength strength : kEffectStrengths) {
444 VendorEffect effect;
445 effect.strength = strength;
446 effect.scale = 1.0f;
447
448 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, nullptr /*callback*/);
449
450 EXPECT_TRUE(status.getExceptionCode() == EX_SERVICE_SPECIFIC)
451 << status << "\n For vendor effect with strength " << toString(strength)
452 << " and scale " << effect.scale;
453 }
454}
455
456TEST_P(VibratorAidl, PerformVendorEffectInvalidScale) {
457 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
458
459 VendorEffect effect;
460 effect.strength = EffectStrength::MEDIUM;
461
462 effect.scale = 0.0f;
463 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
464
465 effect.scale = -1.0f;
466 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
467}
468
469TEST_P(VibratorAidl, PerformVendorEffectUnsupported) {
470 if (capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) return;
471
472 for (EffectStrength strength : kEffectStrengths) {
473 VendorEffect effect;
474 effect.strength = strength;
475 effect.scale = 1.0f;
476
477 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->performVendorEffect(effect, nullptr /*callback*/))
478 << "\n For vendor effect with strength " << toString(strength);
479 }
480}
481
Steven Morelandd44007e2019-10-24 18:12:46 -0700482TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
483 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100484 EXPECT_OK(vibrator->setAmplitude(0.1f));
485 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
486 EXPECT_OK(vibrator->setAmplitude(0.5f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700487 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100488 EXPECT_OK(vibrator->setAmplitude(1.0f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700489 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100490 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700491 }
492}
493
494TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
495 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100496 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(-1));
497 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(0));
498 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(1.1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700499 }
500}
501
502TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
503 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100504 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700505 }
506}
507
508TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
509 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100510 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700511 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100512 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandd44007e2019-10-24 18:12:46 -0700513 sleep(1);
514 }
515}
516
Steven Morelandc0b92d52019-11-05 13:31:41 -0800517TEST_P(VibratorAidl, ExternalAmplitudeControl) {
518 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000519 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800520
521 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100522 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800523
Steven Morelandc0b92d52019-11-05 13:31:41 -0800524 if (supportsExternalAmplitudeControl) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100525 EXPECT_OK(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800526 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100527 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800528 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100529
530 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800531 } else {
532 EXPECT_FALSE(supportsExternalAmplitudeControl);
533 }
534}
535
Steven Morelandd44007e2019-10-24 18:12:46 -0700536TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
537 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100538 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700539 }
540}
541
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900542TEST_P(VibratorAidl, GetSupportedPrimitives) {
543 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
544 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100545 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900546
Lais Andrade28c81f12024-06-24 14:32:22 +0100547 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900548 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000549 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Lais Andrade7e643772021-07-09 14:59:44 +0100550 bool isPrimitiveRequired =
551 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
552 kRequiredPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900553
Lais Andrade7e643772021-07-09 14:59:44 +0100554 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900555 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900556 }
557}
558
559TEST_P(VibratorAidl, GetPrimitiveDuration) {
560 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900561 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100562 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900563
Lais Andrade28c81f12024-06-24 14:32:22 +0100564 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900565 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000566 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900567 int32_t duration;
568
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900569 if (isPrimitiveSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100570 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &duration))
571 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000572 if (primitive != CompositePrimitive::NOOP) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100573 ASSERT_GT(duration, 0)
574 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000575 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900576 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100577 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->getPrimitiveDuration(primitive, &duration))
578 << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900579 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900580 }
581 }
582}
583
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900584TEST_P(VibratorAidl, ComposeValidPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000585 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
586 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
587 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900588
Lais Andrade38d054e2024-02-09 12:42:09 +0000589 std::vector<CompositePrimitive> supported;
590 int32_t maxDelay, maxSize;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900591
Lais Andrade28c81f12024-06-24 14:32:22 +0100592 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
593 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
594 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900595
Lais Andrade38d054e2024-02-09 12:42:09 +0000596 std::vector<CompositeEffect> composite;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900597
Lais Andrade661481e2024-02-12 10:33:09 +0000598 for (int i = 0; i < supported.size(); i++) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100599 CompositePrimitive primitive = supported[i];
Lais Andrade661481e2024-02-12 10:33:09 +0000600 float t = static_cast<float>(i + 1) / supported.size();
Lais Andrade38d054e2024-02-09 12:42:09 +0000601 CompositeEffect effect;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900602
Lais Andrade661481e2024-02-12 10:33:09 +0000603 effect.delayMs = maxDelay * t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000604 effect.primitive = primitive;
Lais Andrade661481e2024-02-12 10:33:09 +0000605 effect.scale = t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000606
607 if (composite.size() == maxSize) {
608 break;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900609 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000610 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900611
Lais Andrade38d054e2024-02-09 12:42:09 +0000612 if (composite.size() != 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100613 EXPECT_OK(vibrator->compose(composite, nullptr));
614 EXPECT_OK(vibrator->off());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900615 }
616}
617
618TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000619 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
620 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
621 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900622
Lais Andrade28c81f12024-06-24 14:32:22 +0100623 std::vector<CompositePrimitive> unsupported(kInvalidPrimitives);
Lais Andrade38d054e2024-02-09 12:42:09 +0000624 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900625
Lais Andrade28c81f12024-06-24 14:32:22 +0100626 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Lais Andrade38d054e2024-02-09 12:42:09 +0000627
Lais Andrade28c81f12024-06-24 14:32:22 +0100628 for (CompositePrimitive primitive : kCompositePrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000629 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000630 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900631
Lais Andrade38d054e2024-02-09 12:42:09 +0000632 if (!isPrimitiveSupported) {
633 unsupported.push_back(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900634 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000635 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900636
Lais Andrade28c81f12024-06-24 14:32:22 +0100637 for (CompositePrimitive primitive : unsupported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000638 std::vector<CompositeEffect> composite(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900639
Lais Andrade28c81f12024-06-24 14:32:22 +0100640 for (CompositeEffect& effect : composite) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000641 effect.delayMs = 0;
642 effect.primitive = primitive;
643 effect.scale = 1.0f;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900644 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100645 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900646 }
647}
648
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900649TEST_P(VibratorAidl, ComposeScaleBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000650 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
651 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900652 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000653
654 std::vector<CompositeEffect> composite(1);
655 CompositeEffect& effect = composite[0];
656
657 effect.delayMs = 0;
658 effect.primitive = CompositePrimitive::CLICK;
659
660 effect.scale = std::nextafter(0.0f, -1.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100661 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Lais Andrade38d054e2024-02-09 12:42:09 +0000662
663 effect.scale = 0.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100664 EXPECT_OK(vibrator->compose(composite, nullptr));
665 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000666
667 effect.scale = 1.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100668 EXPECT_OK(vibrator->compose(composite, nullptr));
669 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000670
671 effect.scale = std::nextafter(1.0f, 2.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100672 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900673}
674
675TEST_P(VibratorAidl, ComposeDelayBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000676 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
677 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900678 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000679
680 int32_t maxDelay;
681
Lais Andrade28c81f12024-06-24 14:32:22 +0100682 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
Lais Andrade38d054e2024-02-09 12:42:09 +0000683
684 std::vector<CompositeEffect> composite(1);
Lais Andrade661481e2024-02-12 10:33:09 +0000685 CompositeEffect& effect = composite[0];
Lais Andrade38d054e2024-02-09 12:42:09 +0000686
Lais Andrade38d054e2024-02-09 12:42:09 +0000687 effect.primitive = CompositePrimitive::CLICK;
688 effect.scale = 1.0f;
689
Lais Andrade661481e2024-02-12 10:33:09 +0000690 effect.delayMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100691 EXPECT_OK(vibrator->compose(composite, nullptr));
692 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000693
694 effect.delayMs = 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100695 EXPECT_OK(vibrator->compose(composite, nullptr));
696 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000697
698 effect.delayMs = maxDelay;
Lais Andrade28c81f12024-06-24 14:32:22 +0100699 EXPECT_OK(vibrator->compose(composite, nullptr));
700 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000701
702 effect.delayMs = maxDelay + 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100703 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900704}
705
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900706TEST_P(VibratorAidl, ComposeSizeBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000707 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
708 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900709 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000710
711 int32_t maxSize;
712
Lais Andrade28c81f12024-06-24 14:32:22 +0100713 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Lais Andrade38d054e2024-02-09 12:42:09 +0000714
715 std::vector<CompositeEffect> composite(maxSize);
716 CompositeEffect effect;
717
718 effect.delayMs = 1;
719 effect.primitive = CompositePrimitive::CLICK;
720 effect.scale = 1.0f;
721
722 std::fill(composite.begin(), composite.end(), effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100723 EXPECT_OK(vibrator->compose(composite, nullptr));
724 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000725
726 composite.emplace_back(effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100727 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900728}
729
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900730TEST_P(VibratorAidl, ComposeCallback) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000731 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
732 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
733 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900734
Lais Andrade38d054e2024-02-09 12:42:09 +0000735 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100736 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900737
Lais Andrade28c81f12024-06-24 14:32:22 +0100738 for (CompositePrimitive primitive : supported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000739 if (primitive == CompositePrimitive::NOOP) {
740 continue;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900741 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000742
743 std::promise<void> completionPromise;
744 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100745 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
746 [&completionPromise] { completionPromise.set_value(); });
Lais Andrade38d054e2024-02-09 12:42:09 +0000747 CompositeEffect effect;
748 std::vector<CompositeEffect> composite;
749 int32_t durationMs;
750 std::chrono::milliseconds duration;
751 std::chrono::time_point<high_resolution_clock> start, end;
752 std::chrono::milliseconds elapsed;
753
754 effect.delayMs = 0;
755 effect.primitive = primitive;
756 effect.scale = 1.0f;
757 composite.emplace_back(effect);
758
Lais Andrade28c81f12024-06-24 14:32:22 +0100759 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &durationMs))
760 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000761 duration = std::chrono::milliseconds(durationMs);
762
763 start = high_resolution_clock::now();
Lais Andrade28c81f12024-06-24 14:32:22 +0100764 EXPECT_OK(vibrator->compose(composite, callback))
765 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000766
Lais Andradec689ba52024-04-10 11:07:11 +0100767 EXPECT_EQ(completionFuture.wait_for(duration + VIBRATION_CALLBACK_TIMEOUT),
Lais Andrade38d054e2024-02-09 12:42:09 +0000768 std::future_status::ready)
Lais Andrade28c81f12024-06-24 14:32:22 +0100769 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000770 end = high_resolution_clock::now();
771
772 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Lais Andrade28c81f12024-06-24 14:32:22 +0100773 EXPECT_GE(elapsed.count(), duration.count())
774 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000775
Lais Andrade28c81f12024-06-24 14:32:22 +0100776 EXPECT_OK(vibrator->off()) << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900777 }
778}
779
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900780TEST_P(VibratorAidl, AlwaysOn) {
781 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
782 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100783 EXPECT_OK(vibrator->getSupportedAlwaysOnEffects(&supported));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900784
785 for (Effect effect : kEffects) {
786 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000787 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900788
789 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100790 ndk::ScopedAStatus status = vibrator->alwaysOnEnable(0, effect, strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900791
792 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100793 EXPECT_OK(std::move(status))
794 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900795 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100796 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
797 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900798 }
799 }
800 }
801
Lais Andrade28c81f12024-06-24 14:32:22 +0100802 EXPECT_OK(vibrator->alwaysOnDisable(0));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900803 }
804}
805
Vince Leung4bae4f92021-02-03 06:21:58 +0000806TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000807 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000808}
809
810TEST_P(VibratorAidl, GetQFactor) {
811 float qFactor;
Lais Andrade28c81f12024-06-24 14:32:22 +0100812 ndk::ScopedAStatus status = vibrator->getQFactor(&qFactor);
Vince Leung4bae4f92021-02-03 06:21:58 +0000813 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100814 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000815 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000816 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100817 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung4bae4f92021-02-03 06:21:58 +0000818 }
819}
820
Vince Leung823cf5f2021-02-11 02:21:57 +0000821TEST_P(VibratorAidl, GetFrequencyResolution) {
822 getFrequencyResolutionHz(vibrator, capabilities);
823}
824
825TEST_P(VibratorAidl, GetFrequencyMinimum) {
826 getFrequencyMinimumHz(vibrator, capabilities);
827}
828
829TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
830 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100831 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000832 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100833 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000834 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
835
836 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
837 getFrequencyMinimumHz(vibrator, capabilities)) /
838 getFrequencyResolutionHz(vibrator, capabilities);
839 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
840
841 for (float e : bandwidthAmplitudeMap) {
842 ASSERT_GE(e, 0.0);
843 ASSERT_LE(e, 1.0);
844 }
845 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100846 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000847 }
848}
849
850TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
851 int32_t durationMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100852 ndk::ScopedAStatus status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
Vince Leung823cf5f2021-02-11 02:21:57 +0000853 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100854 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000855 ASSERT_NE(durationMs, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000856 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100857 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000858 }
859}
860
861TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
862 int32_t maxSize;
Lais Andrade28c81f12024-06-24 14:32:22 +0100863 ndk::ScopedAStatus status = vibrator->getPwleCompositionSizeMax(&maxSize);
Vince Leung823cf5f2021-02-11 02:21:57 +0000864 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100865 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000866 ASSERT_NE(maxSize, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000867 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100868 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000869 }
870}
871
872TEST_P(VibratorAidl, GetSupportedBraking) {
873 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100874 ndk::ScopedAStatus status = vibrator->getSupportedBraking(&supported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000875 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
876 bool isDefaultNoneSupported =
877 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
Lais Andrade28c81f12024-06-24 14:32:22 +0100878 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000879 ASSERT_TRUE(isDefaultNoneSupported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000880 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100881 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000882 }
883}
884
885TEST_P(VibratorAidl, ComposeValidPwle) {
886 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade22754c52021-09-14 12:21:59 +0000887 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000888
889 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100890 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000891 bool isClabSupported =
892 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000893 BrakingPwle firstBraking;
894 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
895 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000896
Lais Andrade22754c52021-09-14 12:21:59 +0000897 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities);
898 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
899 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
900 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities);
901 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
902 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
903 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
904 }
905 BrakingPwle secondBraking;
906 secondBraking.braking = Braking::NONE;
907 secondBraking.duration = 10;
908
Lais Andrade28c81f12024-06-24 14:32:22 +0100909 std::vector<PrimitivePwle> pwleQueue = {firstActive, firstBraking, secondActive,
910 secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000911
Lais Andrade28c81f12024-06-24 14:32:22 +0100912 EXPECT_OK(vibrator->composePwle(pwleQueue, nullptr));
913 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000914 }
915}
916
917TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
918 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
919 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
920 return;
921
922 std::promise<void> completionPromise;
923 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100924 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
925 [&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800926 int32_t segmentDurationMaxMs;
927 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
928 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Lais Andradec689ba52024-04-10 11:07:11 +0100929 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Vince Leung823cf5f2021-02-11 02:21:57 +0000930
931 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
932
933 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100934 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000935 bool isClabSupported =
936 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
937 BrakingPwle braking;
938 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
939 braking.duration = 100;
940
Lais Andrade28c81f12024-06-24 14:32:22 +0100941 std::vector<PrimitivePwle> pwleQueue = {active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000942
Lais Andrade28c81f12024-06-24 14:32:22 +0100943 EXPECT_OK(vibrator->composePwle(pwleQueue, callback));
Vince Leung823cf5f2021-02-11 02:21:57 +0000944 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100945 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000946}
947
948TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
949 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
950 std::vector<PrimitivePwle> pwleQueue;
951 // test empty queue
Lais Andrade28c81f12024-06-24 14:32:22 +0100952 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000953
954 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
955
956 PrimitivePwle pwle;
957 pwle = active;
958 int segmentCountMax;
959 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
960
961 // Create PWLE queue with more segments than allowed
962 for (int i = 0; i < segmentCountMax + 10; i++) {
963 pwleQueue.emplace_back(std::move(pwle));
964 }
965
Lais Andrade28c81f12024-06-24 14:32:22 +0100966 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000967 }
968}
969
970TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
971 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
972 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
973 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
974 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
975
Lais Andrade28c81f12024-06-24 14:32:22 +0100976 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000977
Lais Andrade28c81f12024-06-24 14:32:22 +0100978 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000979
980 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
981 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
982
Lais Andrade28c81f12024-06-24 14:32:22 +0100983 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000984
Lais Andrade28c81f12024-06-24 14:32:22 +0100985 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000986 }
987}
988
989TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
990 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
991 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
992 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
993 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
994 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
995
996 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
997 active.startFrequency =
998 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
999 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1000
Lais Andrade28c81f12024-06-24 14:32:22 +01001001 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001002
Lais Andrade28c81f12024-06-24 14:32:22 +01001003 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001004
1005 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1006 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1007
Lais Andrade28c81f12024-06-24 14:32:22 +01001008 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001009
Lais Andrade28c81f12024-06-24 14:32:22 +01001010 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001011 }
1012}
1013
1014TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
1015 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
1016 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
1017
chasewu22cb9012022-03-31 23:23:27 +08001018 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +00001019 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
1020 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
1021
Lais Andrade28c81f12024-06-24 14:32:22 +01001022 std::vector<PrimitivePwle> pwleQueue = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001023
Lais Andrade28c81f12024-06-24 14:32:22 +01001024 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001025 }
1026}
1027
Lais Andrade80b18612020-10-12 18:44:40 +00001028std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
1029 std::vector<std::tuple<int32_t, int32_t>> tuples;
Lais Andrade80b18612020-10-12 18:44:40 +00001030
Lais Andrade28c81f12024-06-24 14:32:22 +01001031 std::vector<std::string> managerNames = findVibratorManagerNames();
1032 std::vector<int32_t> vibratorIds;
1033 for (int i = 0; i < managerNames.size(); i++) {
1034 auto vibratorManager = IVibratorManager::fromBinder(
1035 ndk::SpAIBinder(AServiceManager_waitForService(managerNames[i].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +00001036 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Lais Andrade28c81f12024-06-24 14:32:22 +01001037 for (int32_t vibratorId : vibratorIds) {
1038 tuples.emplace_back(i, vibratorId);
Lais Andrade80b18612020-10-12 18:44:40 +00001039 }
1040 }
1041 }
1042
Lais Andrade28c81f12024-06-24 14:32:22 +01001043 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
1044 for (int i = 0; i < vibratorNames.size(); i++) {
1045 tuples.emplace_back(-1, i);
Lais Andrade80b18612020-10-12 18:44:40 +00001046 }
1047
1048 return tuples;
1049}
1050
Vince Leung823cf5f2021-02-11 02:21:57 +00001051std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
1052 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +00001053 if (managerIdx < 0) {
1054 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
1055 }
1056 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
1057 std::to_string(vibratorId);
1058}
1059
Dan Shiba4d5322020-07-28 13:09:30 -07001060GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +00001061INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
1062 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -07001063
Vince Leung823cf5f2021-02-11 02:21:57 +00001064int main(int argc, char **argv) {
Lais Andradea3c332f2024-06-20 10:46:06 +01001065 // Random values are used in the implementation.
1066 std::srand(std::time(nullptr));
1067
Steven Morelandd44007e2019-10-24 18:12:46 -07001068 ::testing::InitGoogleTest(&argc, argv);
Lais Andrade28c81f12024-06-24 14:32:22 +01001069 ABinderProcess_setThreadPoolMaxThreadCount(1);
1070 ABinderProcess_startThreadPool();
Steven Morelandd44007e2019-10-24 18:12:46 -07001071 return RUN_ALL_TESTS();
1072}