blob: 2502589f9f4e3c4ac7ef7f07d0f203e8fc21be30 [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
Lais Andrade4526faf2024-08-09 11:57:20 +0100369 float scale = 0.0f;
370 float vendorScale = 0.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100371 for (EffectStrength strength : kEffectStrengths) {
372 PersistableBundle vendorData;
373 ::aidl::android::hardware::vibrator::testing::fillBasicData(&vendorData);
374
375 PersistableBundle nestedData;
376 ::aidl::android::hardware::vibrator::testing::fillBasicData(&nestedData);
377 vendorData.putPersistableBundle("test_nested_bundle", nestedData);
378
379 VendorEffect effect;
380 effect.vendorData = vendorData;
381 effect.strength = strength;
382 effect.scale = scale;
Lais Andrade4526faf2024-08-09 11:57:20 +0100383 effect.vendorScale = vendorScale;
384 scale += 0.5f;
385 vendorScale += 0.2f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100386
387 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
388 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
389
390 // No expectations on the actual status, the effect might be refused with illegal argument
391 // or the vendor might return a service-specific error code.
392 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
393 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
394 << status << "\n For vendor effect with strength" << toString(strength)
395 << " and scale " << effect.scale;
396
397 if (status.isOk()) {
398 // Generic vendor data should not trigger vibrations, but if it does trigger one
399 // then we make sure the vibrator is reset by triggering off().
400 EXPECT_OK(vibrator->off());
401 }
402 }
403}
404
405TEST_P(VibratorAidl, PerformVendorEffectStability) {
406 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
407
408 // Run some iterations of performVendorEffect with randomized vendor data to check basic
409 // stability of the implementation.
410 uint8_t iterations = 200;
411
412 for (EffectStrength strength : kEffectStrengths) {
413 float scale = 0.5f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100414 float vendorScale = 0.2f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100415 for (uint8_t i = 0; i < iterations; i++) {
416 PersistableBundle vendorData;
417 ::aidl::android::hardware::vibrator::testing::fillRandomData(&vendorData);
418
419 VendorEffect effect;
420 effect.vendorData = vendorData;
421 effect.strength = strength;
422 effect.scale = scale;
Lais Andrade4526faf2024-08-09 11:57:20 +0100423 effect.vendorScale = vendorScale;
Lais Andradea3c332f2024-06-20 10:46:06 +0100424 scale *= 2;
Lais Andrade4526faf2024-08-09 11:57:20 +0100425 vendorScale *= 1.5f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100426
427 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
428 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
429
430 // No expectations on the actual status, the effect might be refused with illegal
431 // argument or the vendor might return a service-specific error code.
432 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
433 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
434 << status << "\n For random vendor effect with strength " << toString(strength)
435 << " and scale " << effect.scale;
436
437 if (status.isOk()) {
438 // Random vendor data should not trigger vibrations, but if it does trigger one
439 // then we make sure the vibrator is reset by triggering off().
440 EXPECT_OK(vibrator->off());
441 }
442 }
443 }
444}
445
446TEST_P(VibratorAidl, PerformVendorEffectEmptyVendorData) {
447 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
448
449 for (EffectStrength strength : kEffectStrengths) {
450 VendorEffect effect;
451 effect.strength = strength;
452 effect.scale = 1.0f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100453 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100454
455 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, nullptr /*callback*/);
456
457 EXPECT_TRUE(status.getExceptionCode() == EX_SERVICE_SPECIFIC)
458 << status << "\n For vendor effect with strength " << toString(strength)
459 << " and scale " << effect.scale;
460 }
461}
462
463TEST_P(VibratorAidl, PerformVendorEffectInvalidScale) {
464 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
465
466 VendorEffect effect;
467 effect.strength = EffectStrength::MEDIUM;
468
Lais Andrade4526faf2024-08-09 11:57:20 +0100469 effect.scale = -1.0f;
470 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100471 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
472
Lais Andrade4526faf2024-08-09 11:57:20 +0100473 effect.scale = 1.0f;
474 effect.vendorScale = -1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100475 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
476}
477
478TEST_P(VibratorAidl, PerformVendorEffectUnsupported) {
479 if (capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) return;
480
481 for (EffectStrength strength : kEffectStrengths) {
482 VendorEffect effect;
483 effect.strength = strength;
484 effect.scale = 1.0f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100485 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100486
487 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->performVendorEffect(effect, nullptr /*callback*/))
488 << "\n For vendor effect with strength " << toString(strength);
489 }
490}
491
Steven Morelandd44007e2019-10-24 18:12:46 -0700492TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
493 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100494 EXPECT_OK(vibrator->setAmplitude(0.1f));
495 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
496 EXPECT_OK(vibrator->setAmplitude(0.5f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700497 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100498 EXPECT_OK(vibrator->setAmplitude(1.0f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700499 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100500 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700501 }
502}
503
504TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
505 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100506 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(-1));
507 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(0));
508 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(1.1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700509 }
510}
511
512TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
513 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100514 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700515 }
516}
517
518TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
519 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100520 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700521 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100522 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandd44007e2019-10-24 18:12:46 -0700523 sleep(1);
524 }
525}
526
Steven Morelandc0b92d52019-11-05 13:31:41 -0800527TEST_P(VibratorAidl, ExternalAmplitudeControl) {
528 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000529 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800530
531 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100532 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800533
Steven Morelandc0b92d52019-11-05 13:31:41 -0800534 if (supportsExternalAmplitudeControl) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100535 EXPECT_OK(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800536 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100537 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800538 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100539
540 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800541 } else {
542 EXPECT_FALSE(supportsExternalAmplitudeControl);
543 }
544}
545
Steven Morelandd44007e2019-10-24 18:12:46 -0700546TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
547 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100548 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700549 }
550}
551
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900552TEST_P(VibratorAidl, GetSupportedPrimitives) {
553 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
554 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100555 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900556
Lais Andrade28c81f12024-06-24 14:32:22 +0100557 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900558 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000559 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Lais Andrade7e643772021-07-09 14:59:44 +0100560 bool isPrimitiveRequired =
561 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
562 kRequiredPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900563
Lais Andrade7e643772021-07-09 14:59:44 +0100564 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900565 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900566 }
567}
568
569TEST_P(VibratorAidl, GetPrimitiveDuration) {
570 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900571 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100572 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900573
Lais Andrade28c81f12024-06-24 14:32:22 +0100574 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900575 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000576 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900577 int32_t duration;
578
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900579 if (isPrimitiveSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100580 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &duration))
581 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000582 if (primitive != CompositePrimitive::NOOP) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100583 ASSERT_GT(duration, 0)
584 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000585 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900586 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100587 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->getPrimitiveDuration(primitive, &duration))
588 << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900589 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900590 }
591 }
592}
593
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900594TEST_P(VibratorAidl, ComposeValidPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000595 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
596 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
597 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900598
Lais Andrade38d054e2024-02-09 12:42:09 +0000599 std::vector<CompositePrimitive> supported;
600 int32_t maxDelay, maxSize;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900601
Lais Andrade28c81f12024-06-24 14:32:22 +0100602 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
603 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
604 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900605
Lais Andrade38d054e2024-02-09 12:42:09 +0000606 std::vector<CompositeEffect> composite;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900607
Lais Andrade661481e2024-02-12 10:33:09 +0000608 for (int i = 0; i < supported.size(); i++) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100609 CompositePrimitive primitive = supported[i];
Lais Andrade661481e2024-02-12 10:33:09 +0000610 float t = static_cast<float>(i + 1) / supported.size();
Lais Andrade38d054e2024-02-09 12:42:09 +0000611 CompositeEffect effect;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900612
Lais Andrade661481e2024-02-12 10:33:09 +0000613 effect.delayMs = maxDelay * t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000614 effect.primitive = primitive;
Lais Andrade661481e2024-02-12 10:33:09 +0000615 effect.scale = t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000616
617 if (composite.size() == maxSize) {
618 break;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900619 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000620 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900621
Lais Andrade38d054e2024-02-09 12:42:09 +0000622 if (composite.size() != 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100623 EXPECT_OK(vibrator->compose(composite, nullptr));
624 EXPECT_OK(vibrator->off());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900625 }
626}
627
628TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
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\" Sangha7aec5022020-03-11 06:00:55 +0900632
Lais Andrade28c81f12024-06-24 14:32:22 +0100633 std::vector<CompositePrimitive> unsupported(kInvalidPrimitives);
Lais Andrade38d054e2024-02-09 12:42:09 +0000634 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900635
Lais Andrade28c81f12024-06-24 14:32:22 +0100636 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Lais Andrade38d054e2024-02-09 12:42:09 +0000637
Lais Andrade28c81f12024-06-24 14:32:22 +0100638 for (CompositePrimitive primitive : kCompositePrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000639 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000640 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900641
Lais Andrade38d054e2024-02-09 12:42:09 +0000642 if (!isPrimitiveSupported) {
643 unsupported.push_back(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900644 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000645 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900646
Lais Andrade28c81f12024-06-24 14:32:22 +0100647 for (CompositePrimitive primitive : unsupported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000648 std::vector<CompositeEffect> composite(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900649
Lais Andrade28c81f12024-06-24 14:32:22 +0100650 for (CompositeEffect& effect : composite) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000651 effect.delayMs = 0;
652 effect.primitive = primitive;
653 effect.scale = 1.0f;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900654 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100655 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900656 }
657}
658
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900659TEST_P(VibratorAidl, ComposeScaleBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000660 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
661 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900662 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000663
664 std::vector<CompositeEffect> composite(1);
665 CompositeEffect& effect = composite[0];
666
667 effect.delayMs = 0;
668 effect.primitive = CompositePrimitive::CLICK;
669
670 effect.scale = std::nextafter(0.0f, -1.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100671 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Lais Andrade38d054e2024-02-09 12:42:09 +0000672
673 effect.scale = 0.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100674 EXPECT_OK(vibrator->compose(composite, nullptr));
675 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000676
677 effect.scale = 1.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100678 EXPECT_OK(vibrator->compose(composite, nullptr));
679 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000680
681 effect.scale = std::nextafter(1.0f, 2.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100682 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900683}
684
685TEST_P(VibratorAidl, ComposeDelayBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000686 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
687 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900688 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000689
690 int32_t maxDelay;
691
Lais Andrade28c81f12024-06-24 14:32:22 +0100692 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
Lais Andrade38d054e2024-02-09 12:42:09 +0000693
694 std::vector<CompositeEffect> composite(1);
Lais Andrade661481e2024-02-12 10:33:09 +0000695 CompositeEffect& effect = composite[0];
Lais Andrade38d054e2024-02-09 12:42:09 +0000696
Lais Andrade38d054e2024-02-09 12:42:09 +0000697 effect.primitive = CompositePrimitive::CLICK;
698 effect.scale = 1.0f;
699
Lais Andrade661481e2024-02-12 10:33:09 +0000700 effect.delayMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100701 EXPECT_OK(vibrator->compose(composite, nullptr));
702 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000703
704 effect.delayMs = 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100705 EXPECT_OK(vibrator->compose(composite, nullptr));
706 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000707
708 effect.delayMs = maxDelay;
Lais Andrade28c81f12024-06-24 14:32:22 +0100709 EXPECT_OK(vibrator->compose(composite, nullptr));
710 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000711
712 effect.delayMs = maxDelay + 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100713 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900714}
715
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900716TEST_P(VibratorAidl, ComposeSizeBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000717 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
718 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900719 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000720
721 int32_t maxSize;
722
Lais Andrade28c81f12024-06-24 14:32:22 +0100723 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Lais Andrade38d054e2024-02-09 12:42:09 +0000724
725 std::vector<CompositeEffect> composite(maxSize);
726 CompositeEffect effect;
727
728 effect.delayMs = 1;
729 effect.primitive = CompositePrimitive::CLICK;
730 effect.scale = 1.0f;
731
732 std::fill(composite.begin(), composite.end(), effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100733 EXPECT_OK(vibrator->compose(composite, nullptr));
734 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000735
736 composite.emplace_back(effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100737 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900738}
739
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900740TEST_P(VibratorAidl, ComposeCallback) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000741 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
742 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
743 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900744
Lais Andrade38d054e2024-02-09 12:42:09 +0000745 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100746 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900747
Lais Andrade28c81f12024-06-24 14:32:22 +0100748 for (CompositePrimitive primitive : supported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000749 if (primitive == CompositePrimitive::NOOP) {
750 continue;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900751 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000752
753 std::promise<void> completionPromise;
754 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100755 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
756 [&completionPromise] { completionPromise.set_value(); });
Lais Andrade38d054e2024-02-09 12:42:09 +0000757 CompositeEffect effect;
758 std::vector<CompositeEffect> composite;
759 int32_t durationMs;
760 std::chrono::milliseconds duration;
761 std::chrono::time_point<high_resolution_clock> start, end;
762 std::chrono::milliseconds elapsed;
763
764 effect.delayMs = 0;
765 effect.primitive = primitive;
766 effect.scale = 1.0f;
767 composite.emplace_back(effect);
768
Lais Andrade28c81f12024-06-24 14:32:22 +0100769 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &durationMs))
770 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000771 duration = std::chrono::milliseconds(durationMs);
772
773 start = high_resolution_clock::now();
Lais Andrade28c81f12024-06-24 14:32:22 +0100774 EXPECT_OK(vibrator->compose(composite, callback))
775 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000776
Lais Andradec689ba52024-04-10 11:07:11 +0100777 EXPECT_EQ(completionFuture.wait_for(duration + VIBRATION_CALLBACK_TIMEOUT),
Lais Andrade38d054e2024-02-09 12:42:09 +0000778 std::future_status::ready)
Lais Andrade28c81f12024-06-24 14:32:22 +0100779 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000780 end = high_resolution_clock::now();
781
782 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Lais Andrade28c81f12024-06-24 14:32:22 +0100783 EXPECT_GE(elapsed.count(), duration.count())
784 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000785
Lais Andrade28c81f12024-06-24 14:32:22 +0100786 EXPECT_OK(vibrator->off()) << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900787 }
788}
789
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900790TEST_P(VibratorAidl, AlwaysOn) {
791 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
792 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100793 EXPECT_OK(vibrator->getSupportedAlwaysOnEffects(&supported));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900794
795 for (Effect effect : kEffects) {
796 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000797 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900798
799 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100800 ndk::ScopedAStatus status = vibrator->alwaysOnEnable(0, effect, strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900801
802 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100803 EXPECT_OK(std::move(status))
804 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900805 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100806 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
807 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900808 }
809 }
810 }
811
Lais Andrade28c81f12024-06-24 14:32:22 +0100812 EXPECT_OK(vibrator->alwaysOnDisable(0));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900813 }
814}
815
Vince Leung4bae4f92021-02-03 06:21:58 +0000816TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000817 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000818}
819
820TEST_P(VibratorAidl, GetQFactor) {
821 float qFactor;
Lais Andrade28c81f12024-06-24 14:32:22 +0100822 ndk::ScopedAStatus status = vibrator->getQFactor(&qFactor);
Vince Leung4bae4f92021-02-03 06:21:58 +0000823 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100824 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000825 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000826 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100827 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung4bae4f92021-02-03 06:21:58 +0000828 }
829}
830
Vince Leung823cf5f2021-02-11 02:21:57 +0000831TEST_P(VibratorAidl, GetFrequencyResolution) {
832 getFrequencyResolutionHz(vibrator, capabilities);
833}
834
835TEST_P(VibratorAidl, GetFrequencyMinimum) {
836 getFrequencyMinimumHz(vibrator, capabilities);
837}
838
839TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
840 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100841 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000842 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100843 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000844 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
845
846 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
847 getFrequencyMinimumHz(vibrator, capabilities)) /
848 getFrequencyResolutionHz(vibrator, capabilities);
849 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
850
851 for (float e : bandwidthAmplitudeMap) {
852 ASSERT_GE(e, 0.0);
853 ASSERT_LE(e, 1.0);
854 }
855 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100856 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000857 }
858}
859
860TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
861 int32_t durationMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100862 ndk::ScopedAStatus status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
Vince Leung823cf5f2021-02-11 02:21:57 +0000863 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100864 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000865 ASSERT_NE(durationMs, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000866 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100867 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000868 }
869}
870
871TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
872 int32_t maxSize;
Lais Andrade28c81f12024-06-24 14:32:22 +0100873 ndk::ScopedAStatus status = vibrator->getPwleCompositionSizeMax(&maxSize);
Vince Leung823cf5f2021-02-11 02:21:57 +0000874 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100875 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000876 ASSERT_NE(maxSize, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000877 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100878 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000879 }
880}
881
882TEST_P(VibratorAidl, GetSupportedBraking) {
883 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100884 ndk::ScopedAStatus status = vibrator->getSupportedBraking(&supported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000885 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
886 bool isDefaultNoneSupported =
887 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
Lais Andrade28c81f12024-06-24 14:32:22 +0100888 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000889 ASSERT_TRUE(isDefaultNoneSupported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000890 } 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, ComposeValidPwle) {
896 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade22754c52021-09-14 12:21:59 +0000897 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000898
899 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100900 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000901 bool isClabSupported =
902 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000903 BrakingPwle firstBraking;
904 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
905 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000906
Lais Andrade22754c52021-09-14 12:21:59 +0000907 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities);
908 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
909 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
910 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities);
911 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
912 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
913 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
914 }
915 BrakingPwle secondBraking;
916 secondBraking.braking = Braking::NONE;
917 secondBraking.duration = 10;
918
Lais Andrade28c81f12024-06-24 14:32:22 +0100919 std::vector<PrimitivePwle> pwleQueue = {firstActive, firstBraking, secondActive,
920 secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000921
Lais Andrade28c81f12024-06-24 14:32:22 +0100922 EXPECT_OK(vibrator->composePwle(pwleQueue, nullptr));
923 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000924 }
925}
926
927TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
928 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
929 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
930 return;
931
932 std::promise<void> completionPromise;
933 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100934 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
935 [&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800936 int32_t segmentDurationMaxMs;
937 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
938 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Lais Andradec689ba52024-04-10 11:07:11 +0100939 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Vince Leung823cf5f2021-02-11 02:21:57 +0000940
941 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
942
943 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100944 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000945 bool isClabSupported =
946 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
947 BrakingPwle braking;
948 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
949 braking.duration = 100;
950
Lais Andrade28c81f12024-06-24 14:32:22 +0100951 std::vector<PrimitivePwle> pwleQueue = {active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000952
Lais Andrade28c81f12024-06-24 14:32:22 +0100953 EXPECT_OK(vibrator->composePwle(pwleQueue, callback));
Vince Leung823cf5f2021-02-11 02:21:57 +0000954 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100955 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000956}
957
958TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
959 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
960 std::vector<PrimitivePwle> pwleQueue;
961 // test empty queue
Lais Andrade28c81f12024-06-24 14:32:22 +0100962 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000963
964 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
965
966 PrimitivePwle pwle;
967 pwle = active;
968 int segmentCountMax;
969 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
970
971 // Create PWLE queue with more segments than allowed
972 for (int i = 0; i < segmentCountMax + 10; i++) {
973 pwleQueue.emplace_back(std::move(pwle));
974 }
975
Lais Andrade28c81f12024-06-24 14:32:22 +0100976 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000977 }
978}
979
980TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
981 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
982 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
983 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
984 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
985
Lais Andrade28c81f12024-06-24 14:32:22 +0100986 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000987
Lais Andrade28c81f12024-06-24 14:32:22 +0100988 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000989
990 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
991 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
992
Lais Andrade28c81f12024-06-24 14:32:22 +0100993 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000994
Lais Andrade28c81f12024-06-24 14:32:22 +0100995 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000996 }
997}
998
999TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
1000 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
1001 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
1002 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
1003 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
1004 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
1005
1006 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
1007 active.startFrequency =
1008 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1009 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1010
Lais Andrade28c81f12024-06-24 14:32:22 +01001011 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001012
Lais Andrade28c81f12024-06-24 14:32:22 +01001013 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001014
1015 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1016 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1017
Lais Andrade28c81f12024-06-24 14:32:22 +01001018 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001019
Lais Andrade28c81f12024-06-24 14:32:22 +01001020 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001021 }
1022}
1023
1024TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
1025 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
1026 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
1027
chasewu22cb9012022-03-31 23:23:27 +08001028 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +00001029 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
1030 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
1031
Lais Andrade28c81f12024-06-24 14:32:22 +01001032 std::vector<PrimitivePwle> pwleQueue = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001033
Lais Andrade28c81f12024-06-24 14:32:22 +01001034 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001035 }
1036}
1037
Lais Andrade80b18612020-10-12 18:44:40 +00001038std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
1039 std::vector<std::tuple<int32_t, int32_t>> tuples;
Lais Andrade80b18612020-10-12 18:44:40 +00001040
Lais Andrade28c81f12024-06-24 14:32:22 +01001041 std::vector<std::string> managerNames = findVibratorManagerNames();
1042 std::vector<int32_t> vibratorIds;
1043 for (int i = 0; i < managerNames.size(); i++) {
1044 auto vibratorManager = IVibratorManager::fromBinder(
1045 ndk::SpAIBinder(AServiceManager_waitForService(managerNames[i].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +00001046 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Lais Andrade28c81f12024-06-24 14:32:22 +01001047 for (int32_t vibratorId : vibratorIds) {
1048 tuples.emplace_back(i, vibratorId);
Lais Andrade80b18612020-10-12 18:44:40 +00001049 }
1050 }
1051 }
1052
Lais Andrade28c81f12024-06-24 14:32:22 +01001053 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
1054 for (int i = 0; i < vibratorNames.size(); i++) {
1055 tuples.emplace_back(-1, i);
Lais Andrade80b18612020-10-12 18:44:40 +00001056 }
1057
1058 return tuples;
1059}
1060
Vince Leung823cf5f2021-02-11 02:21:57 +00001061std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
1062 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +00001063 if (managerIdx < 0) {
1064 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
1065 }
1066 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
1067 std::to_string(vibratorId);
1068}
1069
Dan Shiba4d5322020-07-28 13:09:30 -07001070GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +00001071INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
1072 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -07001073
Vince Leung823cf5f2021-02-11 02:21:57 +00001074int main(int argc, char **argv) {
Lais Andradea3c332f2024-06-20 10:46:06 +01001075 // Random values are used in the implementation.
1076 std::srand(std::time(nullptr));
1077
Steven Morelandd44007e2019-10-24 18:12:46 -07001078 ::testing::InitGoogleTest(&argc, argv);
Lais Andrade28c81f12024-06-24 14:32:22 +01001079 ABinderProcess_setThreadPoolMaxThreadCount(1);
1080 ABinderProcess_startThreadPool();
Steven Morelandd44007e2019-10-24 18:12:46 -07001081 return RUN_ALL_TESTS();
1082}