blob: 03ecb1a04ec35040d2cd7f0da72ba62c4f9e5da1 [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>
Ahmad Khalila788bed2024-08-05 14:43:38 +000030#include <iomanip>
31#include <iostream>
32#include <random>
Steven Morelandd44007e2019-10-24 18:12:46 -070033
Lais Andradea3c332f2024-06-20 10:46:06 +010034#include "persistable_bundle_utils.h"
Ahmad Khalila788bed2024-08-05 14:43:38 +000035#include "pwle_v2_utils.h"
Lais Andrade28c81f12024-06-24 14:32:22 +010036#include "test_utils.h"
37
38using aidl::android::hardware::vibrator::ActivePwle;
39using aidl::android::hardware::vibrator::BnVibratorCallback;
40using aidl::android::hardware::vibrator::Braking;
41using aidl::android::hardware::vibrator::BrakingPwle;
42using aidl::android::hardware::vibrator::CompositeEffect;
43using aidl::android::hardware::vibrator::CompositePrimitive;
Ahmad Khalilbb669e12024-09-28 20:08:14 +000044using aidl::android::hardware::vibrator::CompositePwleV2;
Lais Andrade28c81f12024-06-24 14:32:22 +010045using aidl::android::hardware::vibrator::Effect;
46using aidl::android::hardware::vibrator::EffectStrength;
Ahmad Khalilbb669e12024-09-28 20:08:14 +000047using aidl::android::hardware::vibrator::FrequencyAccelerationMapEntry;
Lais Andrade28c81f12024-06-24 14:32:22 +010048using aidl::android::hardware::vibrator::IVibrator;
49using aidl::android::hardware::vibrator::IVibratorManager;
50using aidl::android::hardware::vibrator::PrimitivePwle;
Ahmad Khalila788bed2024-08-05 14:43:38 +000051using aidl::android::hardware::vibrator::PwleV2Primitive;
Lais Andradea3c332f2024-06-20 10:46:06 +010052using aidl::android::hardware::vibrator::VendorEffect;
53using aidl::android::os::PersistableBundle;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +090054using std::chrono::high_resolution_clock;
Steven Morelandd44007e2019-10-24 18:12:46 -070055
Lais Andradec689ba52024-04-10 11:07:11 +010056using namespace ::std::chrono_literals;
57
Ahmad Khalila788bed2024-08-05 14:43:38 +000058namespace pwle_v2_utils = aidl::android::hardware::vibrator::testing::pwlev2;
59
Lais Andrade28c81f12024-06-24 14:32:22 +010060const std::vector<Effect> kEffects{ndk::enum_range<Effect>().begin(),
61 ndk::enum_range<Effect>().end()};
62const std::vector<EffectStrength> kEffectStrengths{ndk::enum_range<EffectStrength>().begin(),
63 ndk::enum_range<EffectStrength>().end()};
Steven Morelandd44007e2019-10-24 18:12:46 -070064
65const std::vector<Effect> kInvalidEffects = {
Vince Leung823cf5f2021-02-11 02:21:57 +000066 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
67 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070068};
69
70const std::vector<EffectStrength> kInvalidEffectStrengths = {
Vince Leung823cf5f2021-02-11 02:21:57 +000071 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
72 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070073};
74
Steven Moreland1c269782020-01-09 11:16:05 -080075const std::vector<CompositePrimitive> kCompositePrimitives{
Lais Andrade28c81f12024-06-24 14:32:22 +010076 ndk::enum_range<CompositePrimitive>().begin(), ndk::enum_range<CompositePrimitive>().end()};
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090077
Lais Andrade7e643772021-07-09 14:59:44 +010078const std::vector<CompositePrimitive> kRequiredPrimitives = {
79 CompositePrimitive::CLICK, CompositePrimitive::LIGHT_TICK,
80 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
81 CompositePrimitive::QUICK_FALL,
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090082};
83
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090084const std::vector<CompositePrimitive> kInvalidPrimitives = {
Vince Leung823cf5f2021-02-11 02:21:57 +000085 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
86 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090087};
88
Lais Andradec689ba52024-04-10 11:07:11 +010089// Timeout to wait for vibration callback completion.
Lais Andrade28c81f12024-06-24 14:32:22 +010090static constexpr std::chrono::milliseconds VIBRATION_CALLBACK_TIMEOUT = 100ms;
91
Ahmad Khalila788bed2024-08-05 14:43:38 +000092static constexpr int32_t VENDOR_EFFECTS_MIN_VERSION = 3;
Ahmad Khalil7eacaad2024-08-22 12:25:36 +000093static constexpr int32_t PWLE_V2_MIN_VERSION = 3;
Ahmad Khalila788bed2024-08-05 14:43:38 +000094
Lais Andrade28c81f12024-06-24 14:32:22 +010095static std::vector<std::string> findVibratorManagerNames() {
96 std::vector<std::string> names;
97 constexpr auto callback = [](const char* instance, void* context) {
98 auto fullName = std::string(IVibratorManager::descriptor) + "/" + instance;
99 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
100 };
101 AServiceManager_forEachDeclaredInstance(IVibratorManager::descriptor,
102 static_cast<void*>(&names), callback);
103 return names;
104}
105
106static std::vector<std::string> findUnmanagedVibratorNames() {
107 std::vector<std::string> names;
108 constexpr auto callback = [](const char* instance, void* context) {
109 auto fullName = std::string(IVibrator::descriptor) + "/" + instance;
110 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
111 };
112 AServiceManager_forEachDeclaredInstance(IVibrator::descriptor, static_cast<void*>(&names),
113 callback);
114 return names;
115}
Lais Andradec689ba52024-04-10 11:07:11 +0100116
Steven Morelandd44007e2019-10-24 18:12:46 -0700117class CompletionCallback : public BnVibratorCallback {
118 public:
Vince Leung823cf5f2021-02-11 02:21:57 +0000119 CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {}
Lais Andrade28c81f12024-06-24 14:32:22 +0100120 ndk::ScopedAStatus onComplete() override {
Steven Morelandd44007e2019-10-24 18:12:46 -0700121 mCallback();
Lais Andrade28c81f12024-06-24 14:32:22 +0100122 return ndk::ScopedAStatus::ok();
Steven Morelandd44007e2019-10-24 18:12:46 -0700123 }
124
125 private:
126 std::function<void()> mCallback;
127};
128
Lais Andrade80b18612020-10-12 18:44:40 +0000129class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> {
Steven Morelandd44007e2019-10-24 18:12:46 -0700130 public:
131 virtual void SetUp() override {
Lais Andrade80b18612020-10-12 18:44:40 +0000132 int32_t managerIdx = std::get<0>(GetParam());
133 int32_t vibratorId = std::get<1>(GetParam());
Lais Andrade80b18612020-10-12 18:44:40 +0000134
135 if (managerIdx < 0) {
136 // Testing a unmanaged vibrator, using vibratorId as index from registered HALs
Lais Andrade28c81f12024-06-24 14:32:22 +0100137 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
138 ASSERT_LT(vibratorId, vibratorNames.size());
139 vibrator = IVibrator::fromBinder(ndk::SpAIBinder(
140 AServiceManager_waitForService(vibratorNames[vibratorId].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +0000141 } else {
142 // Testing a managed vibrator, using vibratorId to retrieve it from the manager
Lais Andrade28c81f12024-06-24 14:32:22 +0100143 std::vector<std::string> managerNames = findVibratorManagerNames();
144 ASSERT_LT(managerIdx, managerNames.size());
145 auto vibratorManager = IVibratorManager::fromBinder(ndk::SpAIBinder(
146 AServiceManager_waitForService(managerNames[managerIdx].c_str())));
147 EXPECT_OK(vibratorManager->getVibrator(vibratorId, &vibrator))
148 << "\n For vibrator id: " << vibratorId;
Lais Andrade80b18612020-10-12 18:44:40 +0000149 }
150
Steven Morelandd44007e2019-10-24 18:12:46 -0700151 ASSERT_NE(vibrator, nullptr);
Ahmad Khalila788bed2024-08-05 14:43:38 +0000152 EXPECT_OK(vibrator->getInterfaceVersion(&version));
Lais Andrade28c81f12024-06-24 14:32:22 +0100153 EXPECT_OK(vibrator->getCapabilities(&capabilities));
Steven Morelandd44007e2019-10-24 18:12:46 -0700154 }
155
Lais Andrade38d054e2024-02-09 12:42:09 +0000156 virtual void TearDown() override {
157 // Reset vibrator state between tests.
Lais Andrade28c81f12024-06-24 14:32:22 +0100158 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000159 }
160
Lais Andrade28c81f12024-06-24 14:32:22 +0100161 std::shared_ptr<IVibrator> vibrator;
Ahmad Khalila788bed2024-08-05 14:43:38 +0000162 int32_t version;
Steven Morelandd44007e2019-10-24 18:12:46 -0700163 int32_t capabilities;
164};
165
Lais Andrade28c81f12024-06-24 14:32:22 +0100166static float getResonantFrequencyHz(const std::shared_ptr<IVibrator>& vibrator,
167 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000168 float resonantFrequencyHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100169 ndk::ScopedAStatus status = vibrator->getResonantFrequency(&resonantFrequencyHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000170 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100171 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000172 EXPECT_GT(resonantFrequencyHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000173 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100174 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000175 }
176 return resonantFrequencyHz;
177}
178
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000179static bool shouldValidateLegacyFrequencyControlResult(int32_t capabilities, int32_t version,
180 ndk::ScopedAStatus& status) {
181 bool hasFrequencyControl = capabilities & IVibrator::CAP_FREQUENCY_CONTROL;
182 // Legacy frequency control APIs deprecated with PWLE V2 feature.
183 bool isDeprecated = version >= PWLE_V2_MIN_VERSION;
184 bool isUnknownOrUnsupported = status.getExceptionCode() == EX_UNSUPPORTED_OPERATION ||
185 status.getStatus() == STATUS_UNKNOWN_TRANSACTION;
186
187 // Validate if older HAL or if result is provided, even after deprecation.
188 return hasFrequencyControl && (!isDeprecated || !isUnknownOrUnsupported);
189}
190
Lais Andrade28c81f12024-06-24 14:32:22 +0100191static float getFrequencyResolutionHz(const std::shared_ptr<IVibrator>& vibrator,
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000192 int32_t capabilities, int32_t version) {
193 float freqResolutionHz = -1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100194 ndk::ScopedAStatus status = vibrator->getFrequencyResolution(&freqResolutionHz);
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000195 if (shouldValidateLegacyFrequencyControlResult(capabilities, version, status)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100196 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000197 EXPECT_GT(freqResolutionHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000198 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100199 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000200 }
201 return freqResolutionHz;
202}
203
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000204static float getFrequencyMinimumHz(const std::shared_ptr<IVibrator>& vibrator, int32_t capabilities,
205 int32_t version) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000206 float freqMinimumHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100207 ndk::ScopedAStatus status = vibrator->getFrequencyMinimum(&freqMinimumHz);
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000208 if (shouldValidateLegacyFrequencyControlResult(capabilities, version, status)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100209 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000210
211 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
212
213 EXPECT_GT(freqMinimumHz, 0);
214 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
215 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100216 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000217 }
218 return freqMinimumHz;
219}
220
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000221static float getFrequencyMaximumHz(const std::shared_ptr<IVibrator>& vibrator, int32_t capabilities,
222 int32_t version) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000223 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100224 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000225 if (shouldValidateLegacyFrequencyControlResult(capabilities, version, status)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100226 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000227 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100228 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000229 }
230
chasewu70da3cc2022-03-15 15:16:04 +0800231 float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) *
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000232 getFrequencyResolutionHz(vibrator, capabilities, version)) +
233 getFrequencyMinimumHz(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +0000234 return freqMaximumHz;
235}
236
237static float getAmplitudeMin() {
238 return 0.0;
239}
240
241static float getAmplitudeMax() {
242 return 1.0;
243}
244
Lais Andrade28c81f12024-06-24 14:32:22 +0100245static ActivePwle composeValidActivePwle(const std::shared_ptr<IVibrator>& vibrator,
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000246 int32_t capabilities, int32_t version) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000247 float frequencyHz;
248 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
249 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
250 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000251 if (version < PWLE_V2_MIN_VERSION) {
252 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities, version);
253 } else {
254 frequencyHz = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator);
255 }
Vince Leung823cf5f2021-02-11 02:21:57 +0000256 } else {
257 frequencyHz = 150.0; // default value commonly used
258 }
259
260 ActivePwle active;
261 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
262 active.startFrequency = frequencyHz;
263 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
264 active.endFrequency = frequencyHz;
chasewu22cb9012022-03-31 23:23:27 +0800265 vibrator->getPwlePrimitiveDurationMax(&(active.duration));
Vince Leung823cf5f2021-02-11 02:21:57 +0000266
267 return active;
268}
269
Steven Morelandd44007e2019-10-24 18:12:46 -0700270TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100271 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
Steven Morelandd44007e2019-10-24 18:12:46 -0700272 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100273 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700274}
275
276TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000277 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
278 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700279
280 std::promise<void> completionPromise;
281 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100282 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
283 [&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700284 uint32_t durationMs = 250;
Lais Andradec689ba52024-04-10 11:07:11 +0100285 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Lais Andrade28c81f12024-06-24 14:32:22 +0100286 EXPECT_OK(vibrator->on(durationMs, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700287 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100288 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700289}
290
291TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800292 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100293 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
294 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->on(250, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700295 }
296}
297
298TEST_P(VibratorAidl, ValidateEffect) {
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 int32_t lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100308 ndk::ScopedAStatus status =
309 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800310
311 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100312 EXPECT_OK(std::move(status))
313 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700314 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800315 usleep(lengthMs * 1000);
Lais Andrade28c81f12024-06-24 14:32:22 +0100316 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700317 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100318 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
319 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700320 }
321 }
322 }
323}
324
325TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000326 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
327 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700328
Steven Moreland2932b222019-11-05 14:30:17 -0800329 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100330 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800331
Steven Morelandd44007e2019-10-24 18:12:46 -0700332 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800333 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000334 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800335
Steven Morelandd44007e2019-10-24 18:12:46 -0700336 for (EffectStrength strength : kEffectStrengths) {
337 std::promise<void> completionPromise;
338 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100339 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
340 [&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800341 int lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100342 ndk::ScopedAStatus status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800343
344 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100345 EXPECT_OK(std::move(status))
346 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800347 EXPECT_GT(lengthMs, 0);
348 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100349 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
350 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800351 }
352
Lais Andrade28c81f12024-06-24 14:32:22 +0100353 if (lengthMs <= 0) continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700354
Lais Andradec689ba52024-04-10 11:07:11 +0100355 auto timeout = std::chrono::milliseconds(lengthMs) + VIBRATION_CALLBACK_TIMEOUT;
Steven Morelandd44007e2019-10-24 18:12:46 -0700356 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andradec689ba52024-04-10 11:07:11 +0100357
Lais Andrade28c81f12024-06-24 14:32:22 +0100358 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700359 }
360 }
361}
362
363TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000364 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
365 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700366
367 for (Effect effect : kEffects) {
368 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100369 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
Steven Morelandd44007e2019-10-24 18:12:46 -0700370 int lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100371 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->perform(effect, strength, callback, &lengthMs))
372 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700373 }
374 }
375}
376
377TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
378 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800379 for (EffectStrength strength : kEffectStrengths) {
380 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100381 EXPECT_UNKNOWN_OR_UNSUPPORTED(
382 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
383 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800384 }
385 }
386 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700387 for (EffectStrength strength : kInvalidEffectStrengths) {
388 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100389 EXPECT_UNKNOWN_OR_UNSUPPORTED(
390 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
391 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700392 }
393 }
394}
395
Lais Andradea3c332f2024-06-20 10:46:06 +0100396TEST_P(VibratorAidl, PerformVendorEffectSupported) {
397 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
398
Lais Andrade4526faf2024-08-09 11:57:20 +0100399 float scale = 0.0f;
400 float vendorScale = 0.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100401 for (EffectStrength strength : kEffectStrengths) {
402 PersistableBundle vendorData;
403 ::aidl::android::hardware::vibrator::testing::fillBasicData(&vendorData);
404
405 PersistableBundle nestedData;
406 ::aidl::android::hardware::vibrator::testing::fillBasicData(&nestedData);
407 vendorData.putPersistableBundle("test_nested_bundle", nestedData);
408
409 VendorEffect effect;
410 effect.vendorData = vendorData;
411 effect.strength = strength;
412 effect.scale = scale;
Lais Andrade4526faf2024-08-09 11:57:20 +0100413 effect.vendorScale = vendorScale;
414 scale += 0.5f;
415 vendorScale += 0.2f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100416
417 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
418 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
419
420 // No expectations on the actual status, the effect might be refused with illegal argument
421 // or the vendor might return a service-specific error code.
422 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
423 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
424 << status << "\n For vendor effect with strength" << toString(strength)
425 << " and scale " << effect.scale;
426
427 if (status.isOk()) {
428 // Generic vendor data should not trigger vibrations, but if it does trigger one
429 // then we make sure the vibrator is reset by triggering off().
430 EXPECT_OK(vibrator->off());
431 }
432 }
433}
434
435TEST_P(VibratorAidl, PerformVendorEffectStability) {
436 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
437
438 // Run some iterations of performVendorEffect with randomized vendor data to check basic
439 // stability of the implementation.
440 uint8_t iterations = 200;
441
442 for (EffectStrength strength : kEffectStrengths) {
443 float scale = 0.5f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100444 float vendorScale = 0.2f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100445 for (uint8_t i = 0; i < iterations; i++) {
446 PersistableBundle vendorData;
447 ::aidl::android::hardware::vibrator::testing::fillRandomData(&vendorData);
448
449 VendorEffect effect;
450 effect.vendorData = vendorData;
451 effect.strength = strength;
452 effect.scale = scale;
Lais Andrade4526faf2024-08-09 11:57:20 +0100453 effect.vendorScale = vendorScale;
Lais Andradea3c332f2024-06-20 10:46:06 +0100454 scale *= 2;
Lais Andrade4526faf2024-08-09 11:57:20 +0100455 vendorScale *= 1.5f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100456
457 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
458 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
459
460 // No expectations on the actual status, the effect might be refused with illegal
461 // argument or the vendor might return a service-specific error code.
462 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
463 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
464 << status << "\n For random vendor effect with strength " << toString(strength)
465 << " and scale " << effect.scale;
466
467 if (status.isOk()) {
468 // Random vendor data should not trigger vibrations, but if it does trigger one
469 // then we make sure the vibrator is reset by triggering off().
470 EXPECT_OK(vibrator->off());
471 }
472 }
473 }
474}
475
476TEST_P(VibratorAidl, PerformVendorEffectEmptyVendorData) {
477 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
478
479 for (EffectStrength strength : kEffectStrengths) {
480 VendorEffect effect;
481 effect.strength = strength;
482 effect.scale = 1.0f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100483 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100484
485 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, nullptr /*callback*/);
486
487 EXPECT_TRUE(status.getExceptionCode() == EX_SERVICE_SPECIFIC)
488 << status << "\n For vendor effect with strength " << toString(strength)
489 << " and scale " << effect.scale;
490 }
491}
492
493TEST_P(VibratorAidl, PerformVendorEffectInvalidScale) {
494 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
495
496 VendorEffect effect;
497 effect.strength = EffectStrength::MEDIUM;
498
Lais Andrade4526faf2024-08-09 11:57:20 +0100499 effect.scale = -1.0f;
500 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100501 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
502
Lais Andrade4526faf2024-08-09 11:57:20 +0100503 effect.scale = 1.0f;
504 effect.vendorScale = -1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100505 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
506}
507
508TEST_P(VibratorAidl, PerformVendorEffectUnsupported) {
Ahmad Khalila788bed2024-08-05 14:43:38 +0000509 if (version < VENDOR_EFFECTS_MIN_VERSION) {
510 EXPECT_EQ(capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS, 0)
511 << "Vibrator version " << version << " should not report vendor effects capability";
512 }
Lais Andradea3c332f2024-06-20 10:46:06 +0100513 if (capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) return;
514
515 for (EffectStrength strength : kEffectStrengths) {
516 VendorEffect effect;
517 effect.strength = strength;
518 effect.scale = 1.0f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100519 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100520
521 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->performVendorEffect(effect, nullptr /*callback*/))
522 << "\n For vendor effect with strength " << toString(strength);
523 }
524}
525
Steven Morelandd44007e2019-10-24 18:12:46 -0700526TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
527 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100528 EXPECT_OK(vibrator->setAmplitude(0.1f));
529 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
530 EXPECT_OK(vibrator->setAmplitude(0.5f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700531 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100532 EXPECT_OK(vibrator->setAmplitude(1.0f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700533 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100534 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700535 }
536}
537
538TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
539 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100540 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(-1));
541 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(0));
542 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(1.1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700543 }
544}
545
546TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
547 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100548 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700549 }
550}
551
552TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
553 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100554 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700555 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100556 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandd44007e2019-10-24 18:12:46 -0700557 sleep(1);
558 }
559}
560
Steven Morelandc0b92d52019-11-05 13:31:41 -0800561TEST_P(VibratorAidl, ExternalAmplitudeControl) {
562 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000563 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800564
565 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100566 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800567
Steven Morelandc0b92d52019-11-05 13:31:41 -0800568 if (supportsExternalAmplitudeControl) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100569 EXPECT_OK(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800570 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100571 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800572 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100573
574 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800575 } else {
576 EXPECT_FALSE(supportsExternalAmplitudeControl);
577 }
578}
579
Steven Morelandd44007e2019-10-24 18:12:46 -0700580TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
581 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100582 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700583 }
584}
585
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900586TEST_P(VibratorAidl, GetSupportedPrimitives) {
587 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
588 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100589 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900590
Lais Andrade28c81f12024-06-24 14:32:22 +0100591 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900592 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000593 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Lais Andrade7e643772021-07-09 14:59:44 +0100594 bool isPrimitiveRequired =
595 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
596 kRequiredPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900597
Lais Andrade7e643772021-07-09 14:59:44 +0100598 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900599 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900600 }
601}
602
603TEST_P(VibratorAidl, GetPrimitiveDuration) {
604 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900605 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100606 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900607
Lais Andrade28c81f12024-06-24 14:32:22 +0100608 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900609 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000610 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900611 int32_t duration;
612
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900613 if (isPrimitiveSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100614 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &duration))
615 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000616 if (primitive != CompositePrimitive::NOOP) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100617 ASSERT_GT(duration, 0)
618 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000619 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900620 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100621 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->getPrimitiveDuration(primitive, &duration))
622 << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900623 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900624 }
625 }
626}
627
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900628TEST_P(VibratorAidl, ComposeValidPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000629 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
630 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
631 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900632
Lais Andrade38d054e2024-02-09 12:42:09 +0000633 std::vector<CompositePrimitive> supported;
634 int32_t maxDelay, maxSize;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900635
Lais Andrade28c81f12024-06-24 14:32:22 +0100636 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
637 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
638 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900639
Lais Andrade38d054e2024-02-09 12:42:09 +0000640 std::vector<CompositeEffect> composite;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900641
Lais Andrade661481e2024-02-12 10:33:09 +0000642 for (int i = 0; i < supported.size(); i++) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100643 CompositePrimitive primitive = supported[i];
Lais Andrade661481e2024-02-12 10:33:09 +0000644 float t = static_cast<float>(i + 1) / supported.size();
Lais Andrade38d054e2024-02-09 12:42:09 +0000645 CompositeEffect effect;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900646
Lais Andrade661481e2024-02-12 10:33:09 +0000647 effect.delayMs = maxDelay * t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000648 effect.primitive = primitive;
Lais Andrade661481e2024-02-12 10:33:09 +0000649 effect.scale = t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000650
651 if (composite.size() == maxSize) {
652 break;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900653 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000654 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900655
Lais Andrade38d054e2024-02-09 12:42:09 +0000656 if (composite.size() != 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100657 EXPECT_OK(vibrator->compose(composite, nullptr));
658 EXPECT_OK(vibrator->off());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900659 }
660}
661
662TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000663 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
664 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
665 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900666
Lais Andrade28c81f12024-06-24 14:32:22 +0100667 std::vector<CompositePrimitive> unsupported(kInvalidPrimitives);
Lais Andrade38d054e2024-02-09 12:42:09 +0000668 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900669
Lais Andrade28c81f12024-06-24 14:32:22 +0100670 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Lais Andrade38d054e2024-02-09 12:42:09 +0000671
Lais Andrade28c81f12024-06-24 14:32:22 +0100672 for (CompositePrimitive primitive : kCompositePrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000673 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000674 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900675
Lais Andrade38d054e2024-02-09 12:42:09 +0000676 if (!isPrimitiveSupported) {
677 unsupported.push_back(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900678 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000679 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900680
Lais Andrade28c81f12024-06-24 14:32:22 +0100681 for (CompositePrimitive primitive : unsupported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000682 std::vector<CompositeEffect> composite(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900683
Lais Andrade28c81f12024-06-24 14:32:22 +0100684 for (CompositeEffect& effect : composite) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000685 effect.delayMs = 0;
686 effect.primitive = primitive;
687 effect.scale = 1.0f;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900688 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100689 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900690 }
691}
692
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900693TEST_P(VibratorAidl, ComposeScaleBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000694 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
695 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900696 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000697
698 std::vector<CompositeEffect> composite(1);
699 CompositeEffect& effect = composite[0];
700
701 effect.delayMs = 0;
702 effect.primitive = CompositePrimitive::CLICK;
703
704 effect.scale = std::nextafter(0.0f, -1.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100705 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Lais Andrade38d054e2024-02-09 12:42:09 +0000706
707 effect.scale = 0.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100708 EXPECT_OK(vibrator->compose(composite, nullptr));
709 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000710
711 effect.scale = 1.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100712 EXPECT_OK(vibrator->compose(composite, nullptr));
713 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000714
715 effect.scale = std::nextafter(1.0f, 2.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100716 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900717}
718
719TEST_P(VibratorAidl, ComposeDelayBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000720 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
721 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900722 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000723
724 int32_t maxDelay;
725
Lais Andrade28c81f12024-06-24 14:32:22 +0100726 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
Lais Andrade38d054e2024-02-09 12:42:09 +0000727
728 std::vector<CompositeEffect> composite(1);
Lais Andrade661481e2024-02-12 10:33:09 +0000729 CompositeEffect& effect = composite[0];
Lais Andrade38d054e2024-02-09 12:42:09 +0000730
Lais Andrade38d054e2024-02-09 12:42:09 +0000731 effect.primitive = CompositePrimitive::CLICK;
732 effect.scale = 1.0f;
733
Lais Andrade661481e2024-02-12 10:33:09 +0000734 effect.delayMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100735 EXPECT_OK(vibrator->compose(composite, nullptr));
736 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000737
738 effect.delayMs = 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100739 EXPECT_OK(vibrator->compose(composite, nullptr));
740 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000741
742 effect.delayMs = maxDelay;
Lais Andrade28c81f12024-06-24 14:32:22 +0100743 EXPECT_OK(vibrator->compose(composite, nullptr));
744 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000745
746 effect.delayMs = maxDelay + 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100747 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900748}
749
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900750TEST_P(VibratorAidl, ComposeSizeBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000751 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
752 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900753 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000754
755 int32_t maxSize;
756
Lais Andrade28c81f12024-06-24 14:32:22 +0100757 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Lais Andrade38d054e2024-02-09 12:42:09 +0000758
759 std::vector<CompositeEffect> composite(maxSize);
760 CompositeEffect effect;
761
762 effect.delayMs = 1;
763 effect.primitive = CompositePrimitive::CLICK;
764 effect.scale = 1.0f;
765
766 std::fill(composite.begin(), composite.end(), effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100767 EXPECT_OK(vibrator->compose(composite, nullptr));
768 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000769
770 composite.emplace_back(effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100771 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900772}
773
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900774TEST_P(VibratorAidl, ComposeCallback) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000775 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
776 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
777 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900778
Lais Andrade38d054e2024-02-09 12:42:09 +0000779 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100780 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900781
Lais Andrade28c81f12024-06-24 14:32:22 +0100782 for (CompositePrimitive primitive : supported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000783 if (primitive == CompositePrimitive::NOOP) {
784 continue;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900785 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000786
787 std::promise<void> completionPromise;
788 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100789 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
790 [&completionPromise] { completionPromise.set_value(); });
Lais Andrade38d054e2024-02-09 12:42:09 +0000791 CompositeEffect effect;
792 std::vector<CompositeEffect> composite;
793 int32_t durationMs;
794 std::chrono::milliseconds duration;
795 std::chrono::time_point<high_resolution_clock> start, end;
796 std::chrono::milliseconds elapsed;
797
798 effect.delayMs = 0;
799 effect.primitive = primitive;
800 effect.scale = 1.0f;
801 composite.emplace_back(effect);
802
Lais Andrade28c81f12024-06-24 14:32:22 +0100803 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &durationMs))
804 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000805 duration = std::chrono::milliseconds(durationMs);
806
807 start = high_resolution_clock::now();
Lais Andrade28c81f12024-06-24 14:32:22 +0100808 EXPECT_OK(vibrator->compose(composite, callback))
809 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000810
Lais Andradec689ba52024-04-10 11:07:11 +0100811 EXPECT_EQ(completionFuture.wait_for(duration + VIBRATION_CALLBACK_TIMEOUT),
Lais Andrade38d054e2024-02-09 12:42:09 +0000812 std::future_status::ready)
Lais Andrade28c81f12024-06-24 14:32:22 +0100813 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000814 end = high_resolution_clock::now();
815
816 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Lais Andrade28c81f12024-06-24 14:32:22 +0100817 EXPECT_GE(elapsed.count(), duration.count())
818 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000819
Lais Andrade28c81f12024-06-24 14:32:22 +0100820 EXPECT_OK(vibrator->off()) << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900821 }
822}
823
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900824TEST_P(VibratorAidl, AlwaysOn) {
825 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
826 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100827 EXPECT_OK(vibrator->getSupportedAlwaysOnEffects(&supported));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900828
829 for (Effect effect : kEffects) {
830 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000831 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900832
833 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100834 ndk::ScopedAStatus status = vibrator->alwaysOnEnable(0, effect, strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900835
836 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100837 EXPECT_OK(std::move(status))
838 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900839 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100840 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
841 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900842 }
843 }
844 }
845
Lais Andrade28c81f12024-06-24 14:32:22 +0100846 EXPECT_OK(vibrator->alwaysOnDisable(0));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900847 }
848}
849
Vince Leung4bae4f92021-02-03 06:21:58 +0000850TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000851 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000852}
853
854TEST_P(VibratorAidl, GetQFactor) {
855 float qFactor;
Lais Andrade28c81f12024-06-24 14:32:22 +0100856 ndk::ScopedAStatus status = vibrator->getQFactor(&qFactor);
Vince Leung4bae4f92021-02-03 06:21:58 +0000857 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100858 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000859 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000860 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100861 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung4bae4f92021-02-03 06:21:58 +0000862 }
863}
864
Vince Leung823cf5f2021-02-11 02:21:57 +0000865TEST_P(VibratorAidl, GetFrequencyResolution) {
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000866 getFrequencyResolutionHz(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +0000867}
868
869TEST_P(VibratorAidl, GetFrequencyMinimum) {
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000870 getFrequencyMinimumHz(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +0000871}
872
873TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
874 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100875 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000876
877 if (shouldValidateLegacyFrequencyControlResult(capabilities, version, status)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100878 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000879 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
880
881 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000882 getFrequencyMinimumHz(vibrator, capabilities, version)) /
883 getFrequencyResolutionHz(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +0000884 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
885
886 for (float e : bandwidthAmplitudeMap) {
887 ASSERT_GE(e, 0.0);
888 ASSERT_LE(e, 1.0);
889 }
890 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100891 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000892 }
893}
894
895TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
896 int32_t durationMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100897 ndk::ScopedAStatus status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
Vince Leung823cf5f2021-02-11 02:21:57 +0000898 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100899 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000900 ASSERT_NE(durationMs, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000901 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100902 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000903 }
904}
905
906TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
907 int32_t maxSize;
Lais Andrade28c81f12024-06-24 14:32:22 +0100908 ndk::ScopedAStatus status = vibrator->getPwleCompositionSizeMax(&maxSize);
Vince Leung823cf5f2021-02-11 02:21:57 +0000909 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100910 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000911 ASSERT_NE(maxSize, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000912 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100913 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000914 }
915}
916
917TEST_P(VibratorAidl, GetSupportedBraking) {
918 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100919 ndk::ScopedAStatus status = vibrator->getSupportedBraking(&supported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000920 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
921 bool isDefaultNoneSupported =
922 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
Lais Andrade28c81f12024-06-24 14:32:22 +0100923 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000924 ASSERT_TRUE(isDefaultNoneSupported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000925 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100926 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000927 }
928}
929
930TEST_P(VibratorAidl, ComposeValidPwle) {
931 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000932 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +0000933
934 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100935 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000936 bool isClabSupported =
937 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000938 BrakingPwle firstBraking;
939 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
940 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000941
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000942 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities, version);
Lais Andrade22754c52021-09-14 12:21:59 +0000943 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000944 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities, version);
945 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities, version);
946 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities, version);
947 // As of API 16 these APIs are deprecated and no longer required to be implemented
948 // with frequency control capability.
949 if (minFrequencyHz >= 0 && maxFrequencyHz >= 0 && freqResolutionHz >= 0) {
950 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
951 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
952 }
Lais Andrade22754c52021-09-14 12:21:59 +0000953 }
954 BrakingPwle secondBraking;
955 secondBraking.braking = Braking::NONE;
956 secondBraking.duration = 10;
957
Lais Andrade28c81f12024-06-24 14:32:22 +0100958 std::vector<PrimitivePwle> pwleQueue = {firstActive, firstBraking, secondActive,
959 secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000960
Lais Andrade28c81f12024-06-24 14:32:22 +0100961 EXPECT_OK(vibrator->composePwle(pwleQueue, nullptr));
962 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000963 }
964}
965
966TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
967 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
968 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
969 return;
970
971 std::promise<void> completionPromise;
972 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100973 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
974 [&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800975 int32_t segmentDurationMaxMs;
976 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
977 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Lais Andradec689ba52024-04-10 11:07:11 +0100978 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Vince Leung823cf5f2021-02-11 02:21:57 +0000979
Ahmad Khalilbb669e12024-09-28 20:08:14 +0000980 ActivePwle active = composeValidActivePwle(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +0000981
982 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100983 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000984 bool isClabSupported =
985 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
986 BrakingPwle braking;
987 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
988 braking.duration = 100;
989
Lais Andrade28c81f12024-06-24 14:32:22 +0100990 std::vector<PrimitivePwle> pwleQueue = {active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000991
Lais Andrade28c81f12024-06-24 14:32:22 +0100992 EXPECT_OK(vibrator->composePwle(pwleQueue, callback));
Vince Leung823cf5f2021-02-11 02:21:57 +0000993 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100994 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000995}
996
997TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
998 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
999 std::vector<PrimitivePwle> pwleQueue;
1000 // test empty queue
Lais Andrade28c81f12024-06-24 14:32:22 +01001001 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001002
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001003 ActivePwle active = composeValidActivePwle(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +00001004
1005 PrimitivePwle pwle;
1006 pwle = active;
1007 int segmentCountMax;
1008 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
1009
1010 // Create PWLE queue with more segments than allowed
1011 for (int i = 0; i < segmentCountMax + 10; i++) {
1012 pwleQueue.emplace_back(std::move(pwle));
1013 }
1014
Lais Andrade28c81f12024-06-24 14:32:22 +01001015 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001016 }
1017}
1018
1019TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
1020 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001021 ActivePwle active = composeValidActivePwle(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +00001022 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
1023 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
1024
Lais Andrade28c81f12024-06-24 14:32:22 +01001025 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001026
Lais Andrade28c81f12024-06-24 14:32:22 +01001027 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001028
1029 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
1030 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
1031
Lais Andrade28c81f12024-06-24 14:32:22 +01001032 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001033
Lais Andrade28c81f12024-06-24 14:32:22 +01001034 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001035 }
1036}
1037
1038TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
1039 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
1040 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001041 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities, version);
1042 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities, version);
1043 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +00001044
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001045 // As of API 16 these APIs are deprecated and no longer required to be implemented with
1046 // frequency control capability.
1047 if (freqMinimumHz < 0 || freqMaximumHz < 0 || freqResolutionHz < 0) {
1048 GTEST_SKIP() << "PWLE V1 is not supported, skipping test";
1049 return;
1050 }
1051
1052 ActivePwle active = composeValidActivePwle(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +00001053 active.startFrequency =
1054 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1055 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1056
Lais Andrade28c81f12024-06-24 14:32:22 +01001057 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001058
Lais Andrade28c81f12024-06-24 14:32:22 +01001059 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001060
1061 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1062 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1063
Lais Andrade28c81f12024-06-24 14:32:22 +01001064 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001065
Lais Andrade28c81f12024-06-24 14:32:22 +01001066 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001067 }
1068}
1069
1070TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
1071 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001072 ActivePwle active = composeValidActivePwle(vibrator, capabilities, version);
Vince Leung823cf5f2021-02-11 02:21:57 +00001073
chasewu22cb9012022-03-31 23:23:27 +08001074 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +00001075 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
1076 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
1077
Lais Andrade28c81f12024-06-24 14:32:22 +01001078 std::vector<PrimitivePwle> pwleQueue = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001079
Lais Andrade28c81f12024-06-24 14:32:22 +01001080 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001081 }
1082}
1083
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001084TEST_P(VibratorAidl, FrequencyToOutputAccelerationMapHasValidFrequencyRange) {
1085 if (version < PWLE_V2_MIN_VERSION || !(capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
1086 GTEST_SKIP() << "Frequency control is not supported, skipping test";
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001087 return;
1088 }
1089
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001090 std::vector<FrequencyAccelerationMapEntry> frequencyToOutputAccelerationMap;
Ahmad Khalila788bed2024-08-05 14:43:38 +00001091 ndk::ScopedAStatus status =
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001092 vibrator->getFrequencyToOutputAccelerationMap(&frequencyToOutputAccelerationMap);
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001093 EXPECT_OK(std::move(status));
1094 ASSERT_FALSE(frequencyToOutputAccelerationMap.empty());
1095 auto sharpnessRange =
1096 pwle_v2_utils::getPwleV2SharpnessRange(vibrator, frequencyToOutputAccelerationMap);
1097 // Validate the curve provides a usable sharpness range, which is a range of frequencies
1098 // that are supported by the device.
1099 ASSERT_TRUE(sharpnessRange.first >= 0);
1100 // Validate that the sharpness range is a valid interval, not a single point.
1101 ASSERT_TRUE(sharpnessRange.first < sharpnessRange.second);
Ahmad Khalila788bed2024-08-05 14:43:38 +00001102}
1103
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001104TEST_P(VibratorAidl, FrequencyToOutputAccelerationMapUnsupported) {
1105 if ((capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) return;
1106
1107 std::vector<FrequencyAccelerationMapEntry> frequencyToOutputAccelerationMap;
1108
1109 EXPECT_UNKNOWN_OR_UNSUPPORTED(
1110 vibrator->getFrequencyToOutputAccelerationMap(&frequencyToOutputAccelerationMap));
1111}
1112
Ahmad Khalila788bed2024-08-05 14:43:38 +00001113TEST_P(VibratorAidl, GetPwleV2PrimitiveDurationMaxMillis) {
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001114 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1115 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1116 return;
1117 }
1118
Ahmad Khalila788bed2024-08-05 14:43:38 +00001119 int32_t durationMs;
1120 ndk::ScopedAStatus status = vibrator->getPwleV2PrimitiveDurationMaxMillis(&durationMs);
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001121 EXPECT_OK(std::move(status));
1122 ASSERT_GT(durationMs, 0); // Ensure greater than zero
1123 ASSERT_GE(durationMs, pwle_v2_utils::COMPOSE_PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS);
Ahmad Khalila788bed2024-08-05 14:43:38 +00001124}
1125
1126TEST_P(VibratorAidl, GetPwleV2CompositionSizeMax) {
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001127 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1128 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1129 return;
1130 }
1131
Ahmad Khalila788bed2024-08-05 14:43:38 +00001132 int32_t maxSize;
1133 ndk::ScopedAStatus status = vibrator->getPwleV2CompositionSizeMax(&maxSize);
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001134 EXPECT_OK(std::move(status));
1135 ASSERT_GT(maxSize, 0); // Ensure greater than zero
1136 ASSERT_GE(maxSize, pwle_v2_utils::COMPOSE_PWLE_V2_MIN_REQUIRED_SIZE);
Ahmad Khalila788bed2024-08-05 14:43:38 +00001137}
1138
1139TEST_P(VibratorAidl, GetPwleV2PrimitiveDurationMinMillis) {
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001140 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1141 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1142 return;
1143 }
1144
Ahmad Khalila788bed2024-08-05 14:43:38 +00001145 int32_t durationMs;
1146 ndk::ScopedAStatus status = vibrator->getPwleV2PrimitiveDurationMinMillis(&durationMs);
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001147 EXPECT_OK(std::move(status));
1148 ASSERT_GT(durationMs, 0); // Ensure greater than zero
1149 ASSERT_LE(durationMs, pwle_v2_utils::COMPOSE_PWLE_V2_MAX_ALLOWED_PRIMITIVE_MIN_DURATION_MS);
Ahmad Khalila788bed2024-08-05 14:43:38 +00001150}
1151
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001152TEST_P(VibratorAidl, ValidatePwleV2DependencyOnFrequencyControl) {
1153 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1154 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1155 return;
1156 }
1157
1158 // Check if frequency control is supported
1159 bool hasFrequencyControl = (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) != 0;
1160 ASSERT_TRUE(hasFrequencyControl) << "Frequency control MUST be supported when PWLE V2 is.";
1161}
1162
Ahmad Khalila788bed2024-08-05 14:43:38 +00001163TEST_P(VibratorAidl, ComposeValidPwleV2Effect) {
1164 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1165 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1166 return;
1167 }
1168
1169 EXPECT_OK(vibrator->composePwleV2(pwle_v2_utils::composeValidPwleV2Effect(vibrator), nullptr));
1170 EXPECT_OK(vibrator->off());
1171}
1172
Ahmad Khalil7eacaad2024-08-22 12:25:36 +00001173TEST_P(VibratorAidl, ComposePwleV2Unsupported) {
1174 if (version < PWLE_V2_MIN_VERSION) {
1175 EXPECT_EQ(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2, 0)
1176 << "Vibrator version " << version << " should not report PWLE V2 capability.";
1177 }
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001178 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) return;
Ahmad Khalil7eacaad2024-08-22 12:25:36 +00001179
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001180 CompositePwleV2 composite;
1181 composite.pwlePrimitives.emplace_back(/*amplitude=*/1.0f, /*frequencyHz=*/100.0f,
1182 /*timeMillis=*/50);
Ahmad Khalild73dbc62024-08-27 20:08:08 +00001183
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001184 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->composePwleV2(composite, nullptr));
Ahmad Khalil7eacaad2024-08-22 12:25:36 +00001185}
1186
Ahmad Khalila788bed2024-08-05 14:43:38 +00001187TEST_P(VibratorAidl, ComposeValidPwleV2EffectWithCallback) {
1188 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1189 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1190 return;
1191 }
1192
1193 std::promise<void> completionPromise;
1194 std::future<void> completionFuture{completionPromise.get_future()};
1195 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
1196 [&completionPromise] { completionPromise.set_value(); });
1197
1198 int32_t minDuration;
1199 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMinMillis(&minDuration));
1200 auto timeout = std::chrono::milliseconds(minDuration) + VIBRATION_CALLBACK_TIMEOUT;
1201 float minFrequency = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator);
1202
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001203 CompositePwleV2 composite;
1204 composite.pwlePrimitives.emplace_back(/*amplitude=*/0.5, minFrequency, minDuration);
1205
1206 EXPECT_OK(vibrator->composePwleV2(composite, callback));
Ahmad Khalila788bed2024-08-05 14:43:38 +00001207 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
1208 EXPECT_OK(vibrator->off());
1209}
1210
1211TEST_P(VibratorAidl, composePwleV2EffectWithTooManyPoints) {
1212 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1213 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1214 return;
1215 }
1216
1217 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(
1218 pwle_v2_utils::composePwleV2EffectWithTooManyPoints(vibrator), nullptr));
1219}
1220
1221TEST_P(VibratorAidl, composeInvalidPwleV2Effect) {
1222 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1223 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1224 return;
1225 }
1226
1227 // Retrieve min and max durations
1228 int32_t minDurationMs, maxDurationMs;
1229 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMinMillis(&minDurationMs));
1230 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMaxMillis(&maxDurationMs));
1231
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001232 CompositePwleV2 composePwle;
Ahmad Khalila788bed2024-08-05 14:43:38 +00001233
1234 // Negative amplitude
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001235 composePwle.pwlePrimitives.push_back(
1236 PwleV2Primitive(/*amplitude=*/-0.8f, /*frequency=*/100, minDurationMs));
Ahmad Khalila788bed2024-08-05 14:43:38 +00001237 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1238 << "Composing PWLE V2 effect with negative amplitude should fail";
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001239 composePwle.pwlePrimitives.clear();
Ahmad Khalila788bed2024-08-05 14:43:38 +00001240
1241 // Amplitude exceeding 1.0
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001242 composePwle.pwlePrimitives.push_back(
1243 PwleV2Primitive(/*amplitude=*/1.2f, /*frequency=*/100, minDurationMs));
Ahmad Khalila788bed2024-08-05 14:43:38 +00001244 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1245 << "Composing PWLE V2 effect with amplitude greater than 1.0 should fail";
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001246 composePwle.pwlePrimitives.clear();
Ahmad Khalila788bed2024-08-05 14:43:38 +00001247
1248 // Duration exceeding maximum
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001249 composePwle.pwlePrimitives.push_back(
Ahmad Khalila788bed2024-08-05 14:43:38 +00001250 PwleV2Primitive(/*amplitude=*/0.2f, /*frequency=*/100, maxDurationMs + 10));
1251 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1252 << "Composing PWLE V2 effect with duration exceeding maximum should fail";
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001253 composePwle.pwlePrimitives.clear();
Ahmad Khalila788bed2024-08-05 14:43:38 +00001254
1255 // Negative duration
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001256 composePwle.pwlePrimitives.push_back(
1257 PwleV2Primitive(/*amplitude=*/0.2f, /*frequency=*/100, /*time=*/-1));
Ahmad Khalila788bed2024-08-05 14:43:38 +00001258 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1259 << "Composing PWLE V2 effect with negative duration should fail";
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001260 composePwle.pwlePrimitives.clear();
Ahmad Khalila788bed2024-08-05 14:43:38 +00001261
1262 // Frequency below minimum
1263 float minFrequency = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator);
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001264 composePwle.pwlePrimitives.push_back(
1265 PwleV2Primitive(/*amplitude=*/0.2f, minFrequency - 1, minDurationMs));
Ahmad Khalila788bed2024-08-05 14:43:38 +00001266 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1267 << "Composing PWLE V2 effect with frequency below minimum should fail";
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001268 composePwle.pwlePrimitives.clear();
Ahmad Khalila788bed2024-08-05 14:43:38 +00001269
1270 // Frequency above maximum
1271 float maxFrequency = pwle_v2_utils::getPwleV2FrequencyMaxHz(vibrator);
Ahmad Khalilbb669e12024-09-28 20:08:14 +00001272 composePwle.pwlePrimitives.push_back(
1273 PwleV2Primitive(/*amplitude=*/0.2f, maxFrequency + 1, minDurationMs));
Ahmad Khalila788bed2024-08-05 14:43:38 +00001274 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1275 << "Composing PWLE V2 effect with frequency above maximum should fail";
1276}
1277
Lais Andrade80b18612020-10-12 18:44:40 +00001278std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
1279 std::vector<std::tuple<int32_t, int32_t>> tuples;
Lais Andrade80b18612020-10-12 18:44:40 +00001280
Lais Andrade28c81f12024-06-24 14:32:22 +01001281 std::vector<std::string> managerNames = findVibratorManagerNames();
1282 std::vector<int32_t> vibratorIds;
1283 for (int i = 0; i < managerNames.size(); i++) {
1284 auto vibratorManager = IVibratorManager::fromBinder(
1285 ndk::SpAIBinder(AServiceManager_waitForService(managerNames[i].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +00001286 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Lais Andrade28c81f12024-06-24 14:32:22 +01001287 for (int32_t vibratorId : vibratorIds) {
1288 tuples.emplace_back(i, vibratorId);
Lais Andrade80b18612020-10-12 18:44:40 +00001289 }
1290 }
1291 }
1292
Lais Andrade28c81f12024-06-24 14:32:22 +01001293 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
1294 for (int i = 0; i < vibratorNames.size(); i++) {
1295 tuples.emplace_back(-1, i);
Lais Andrade80b18612020-10-12 18:44:40 +00001296 }
1297
1298 return tuples;
1299}
1300
Vince Leung823cf5f2021-02-11 02:21:57 +00001301std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
1302 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +00001303 if (managerIdx < 0) {
1304 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
1305 }
1306 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
1307 std::to_string(vibratorId);
1308}
1309
Dan Shiba4d5322020-07-28 13:09:30 -07001310GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +00001311INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
1312 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -07001313
Vince Leung823cf5f2021-02-11 02:21:57 +00001314int main(int argc, char **argv) {
Lais Andradea3c332f2024-06-20 10:46:06 +01001315 // Random values are used in the implementation.
1316 std::srand(std::time(nullptr));
1317
Steven Morelandd44007e2019-10-24 18:12:46 -07001318 ::testing::InitGoogleTest(&argc, argv);
Lais Andrade28c81f12024-06-24 14:32:22 +01001319 ABinderProcess_setThreadPoolMaxThreadCount(1);
1320 ABinderProcess_startThreadPool();
Steven Morelandd44007e2019-10-24 18:12:46 -07001321 return RUN_ALL_TESTS();
1322}