blob: 65a1e84177739661320cd9c388eb267443771fc3 [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>
Steven Morelandd44007e2019-10-24 18:12:46 -070024
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090025#include <cmath>
Steven Morelandd44007e2019-10-24 18:12:46 -070026#include <future>
27
Lais Andrade28c81f12024-06-24 14:32:22 +010028#include "test_utils.h"
29
30using aidl::android::hardware::vibrator::ActivePwle;
31using aidl::android::hardware::vibrator::BnVibratorCallback;
32using aidl::android::hardware::vibrator::Braking;
33using aidl::android::hardware::vibrator::BrakingPwle;
34using aidl::android::hardware::vibrator::CompositeEffect;
35using aidl::android::hardware::vibrator::CompositePrimitive;
36using aidl::android::hardware::vibrator::Effect;
37using aidl::android::hardware::vibrator::EffectStrength;
38using aidl::android::hardware::vibrator::IVibrator;
39using aidl::android::hardware::vibrator::IVibratorManager;
40using aidl::android::hardware::vibrator::PrimitivePwle;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +090041using std::chrono::high_resolution_clock;
Steven Morelandd44007e2019-10-24 18:12:46 -070042
Lais Andradec689ba52024-04-10 11:07:11 +010043using namespace ::std::chrono_literals;
44
Lais Andrade28c81f12024-06-24 14:32:22 +010045const std::vector<Effect> kEffects{ndk::enum_range<Effect>().begin(),
46 ndk::enum_range<Effect>().end()};
47const std::vector<EffectStrength> kEffectStrengths{ndk::enum_range<EffectStrength>().begin(),
48 ndk::enum_range<EffectStrength>().end()};
Steven Morelandd44007e2019-10-24 18:12:46 -070049
50const std::vector<Effect> kInvalidEffects = {
Vince Leung823cf5f2021-02-11 02:21:57 +000051 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
52 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070053};
54
55const std::vector<EffectStrength> kInvalidEffectStrengths = {
Vince Leung823cf5f2021-02-11 02:21:57 +000056 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
57 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070058};
59
Steven Moreland1c269782020-01-09 11:16:05 -080060const std::vector<CompositePrimitive> kCompositePrimitives{
Lais Andrade28c81f12024-06-24 14:32:22 +010061 ndk::enum_range<CompositePrimitive>().begin(), ndk::enum_range<CompositePrimitive>().end()};
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090062
Lais Andrade7e643772021-07-09 14:59:44 +010063const std::vector<CompositePrimitive> kRequiredPrimitives = {
64 CompositePrimitive::CLICK, CompositePrimitive::LIGHT_TICK,
65 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
66 CompositePrimitive::QUICK_FALL,
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090067};
68
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090069const std::vector<CompositePrimitive> kInvalidPrimitives = {
Vince Leung823cf5f2021-02-11 02:21:57 +000070 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
71 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090072};
73
Lais Andradec689ba52024-04-10 11:07:11 +010074// Timeout to wait for vibration callback completion.
Lais Andrade28c81f12024-06-24 14:32:22 +010075static constexpr std::chrono::milliseconds VIBRATION_CALLBACK_TIMEOUT = 100ms;
76
77static std::vector<std::string> findVibratorManagerNames() {
78 std::vector<std::string> names;
79 constexpr auto callback = [](const char* instance, void* context) {
80 auto fullName = std::string(IVibratorManager::descriptor) + "/" + instance;
81 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
82 };
83 AServiceManager_forEachDeclaredInstance(IVibratorManager::descriptor,
84 static_cast<void*>(&names), callback);
85 return names;
86}
87
88static std::vector<std::string> findUnmanagedVibratorNames() {
89 std::vector<std::string> names;
90 constexpr auto callback = [](const char* instance, void* context) {
91 auto fullName = std::string(IVibrator::descriptor) + "/" + instance;
92 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
93 };
94 AServiceManager_forEachDeclaredInstance(IVibrator::descriptor, static_cast<void*>(&names),
95 callback);
96 return names;
97}
Lais Andradec689ba52024-04-10 11:07:11 +010098
Steven Morelandd44007e2019-10-24 18:12:46 -070099class CompletionCallback : public BnVibratorCallback {
100 public:
Vince Leung823cf5f2021-02-11 02:21:57 +0000101 CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {}
Lais Andrade28c81f12024-06-24 14:32:22 +0100102 ndk::ScopedAStatus onComplete() override {
Steven Morelandd44007e2019-10-24 18:12:46 -0700103 mCallback();
Lais Andrade28c81f12024-06-24 14:32:22 +0100104 return ndk::ScopedAStatus::ok();
Steven Morelandd44007e2019-10-24 18:12:46 -0700105 }
106
107 private:
108 std::function<void()> mCallback;
109};
110
Lais Andrade80b18612020-10-12 18:44:40 +0000111class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> {
Steven Morelandd44007e2019-10-24 18:12:46 -0700112 public:
113 virtual void SetUp() override {
Lais Andrade80b18612020-10-12 18:44:40 +0000114 int32_t managerIdx = std::get<0>(GetParam());
115 int32_t vibratorId = std::get<1>(GetParam());
Lais Andrade80b18612020-10-12 18:44:40 +0000116
117 if (managerIdx < 0) {
118 // Testing a unmanaged vibrator, using vibratorId as index from registered HALs
Lais Andrade28c81f12024-06-24 14:32:22 +0100119 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
120 ASSERT_LT(vibratorId, vibratorNames.size());
121 vibrator = IVibrator::fromBinder(ndk::SpAIBinder(
122 AServiceManager_waitForService(vibratorNames[vibratorId].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +0000123 } else {
124 // Testing a managed vibrator, using vibratorId to retrieve it from the manager
Lais Andrade28c81f12024-06-24 14:32:22 +0100125 std::vector<std::string> managerNames = findVibratorManagerNames();
126 ASSERT_LT(managerIdx, managerNames.size());
127 auto vibratorManager = IVibratorManager::fromBinder(ndk::SpAIBinder(
128 AServiceManager_waitForService(managerNames[managerIdx].c_str())));
129 EXPECT_OK(vibratorManager->getVibrator(vibratorId, &vibrator))
130 << "\n For vibrator id: " << vibratorId;
Lais Andrade80b18612020-10-12 18:44:40 +0000131 }
132
Steven Morelandd44007e2019-10-24 18:12:46 -0700133 ASSERT_NE(vibrator, nullptr);
Lais Andrade28c81f12024-06-24 14:32:22 +0100134 EXPECT_OK(vibrator->getCapabilities(&capabilities));
Steven Morelandd44007e2019-10-24 18:12:46 -0700135 }
136
Lais Andrade38d054e2024-02-09 12:42:09 +0000137 virtual void TearDown() override {
138 // Reset vibrator state between tests.
Lais Andrade28c81f12024-06-24 14:32:22 +0100139 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000140 }
141
Lais Andrade28c81f12024-06-24 14:32:22 +0100142 std::shared_ptr<IVibrator> vibrator;
Steven Morelandd44007e2019-10-24 18:12:46 -0700143 int32_t capabilities;
144};
145
Lais Andrade28c81f12024-06-24 14:32:22 +0100146static float getResonantFrequencyHz(const std::shared_ptr<IVibrator>& vibrator,
147 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000148 float resonantFrequencyHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100149 ndk::ScopedAStatus status = vibrator->getResonantFrequency(&resonantFrequencyHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000150 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100151 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000152 EXPECT_GT(resonantFrequencyHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000153 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100154 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000155 }
156 return resonantFrequencyHz;
157}
158
Lais Andrade28c81f12024-06-24 14:32:22 +0100159static float getFrequencyResolutionHz(const std::shared_ptr<IVibrator>& vibrator,
160 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000161 float freqResolutionHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100162 ndk::ScopedAStatus status = vibrator->getFrequencyResolution(&freqResolutionHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000163 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100164 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000165 EXPECT_GT(freqResolutionHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000166 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100167 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000168 }
169 return freqResolutionHz;
170}
171
Lais Andrade28c81f12024-06-24 14:32:22 +0100172static float getFrequencyMinimumHz(const std::shared_ptr<IVibrator>& vibrator,
173 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000174 float freqMinimumHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100175 ndk::ScopedAStatus status = vibrator->getFrequencyMinimum(&freqMinimumHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000176 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100177 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000178
179 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
180
181 EXPECT_GT(freqMinimumHz, 0);
182 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
183 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100184 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000185 }
186 return freqMinimumHz;
187}
188
Lais Andrade28c81f12024-06-24 14:32:22 +0100189static float getFrequencyMaximumHz(const std::shared_ptr<IVibrator>& vibrator,
190 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000191 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100192 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000193 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100194 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000195 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100196 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000197 }
198
chasewu70da3cc2022-03-15 15:16:04 +0800199 float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) *
200 getFrequencyResolutionHz(vibrator, capabilities)) +
201 getFrequencyMinimumHz(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000202 return freqMaximumHz;
203}
204
205static float getAmplitudeMin() {
206 return 0.0;
207}
208
209static float getAmplitudeMax() {
210 return 1.0;
211}
212
Lais Andrade28c81f12024-06-24 14:32:22 +0100213static ActivePwle composeValidActivePwle(const std::shared_ptr<IVibrator>& vibrator,
214 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000215 float frequencyHz;
216 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
217 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
218 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
219 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
220 } else {
221 frequencyHz = 150.0; // default value commonly used
222 }
223
224 ActivePwle active;
225 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
226 active.startFrequency = frequencyHz;
227 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
228 active.endFrequency = frequencyHz;
chasewu22cb9012022-03-31 23:23:27 +0800229 vibrator->getPwlePrimitiveDurationMax(&(active.duration));
Vince Leung823cf5f2021-02-11 02:21:57 +0000230
231 return active;
232}
233
Steven Morelandd44007e2019-10-24 18:12:46 -0700234TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100235 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
Steven Morelandd44007e2019-10-24 18:12:46 -0700236 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100237 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700238}
239
240TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000241 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
242 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700243
244 std::promise<void> completionPromise;
245 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100246 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
247 [&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700248 uint32_t durationMs = 250;
Lais Andradec689ba52024-04-10 11:07:11 +0100249 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Lais Andrade28c81f12024-06-24 14:32:22 +0100250 EXPECT_OK(vibrator->on(durationMs, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700251 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100252 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700253}
254
255TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800256 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100257 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
258 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->on(250, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700259 }
260}
261
262TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800263 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100264 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800265
Steven Morelandd44007e2019-10-24 18:12:46 -0700266 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800267 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000268 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800269
Steven Morelandd44007e2019-10-24 18:12:46 -0700270 for (EffectStrength strength : kEffectStrengths) {
271 int32_t lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100272 ndk::ScopedAStatus status =
273 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800274
275 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100276 EXPECT_OK(std::move(status))
277 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700278 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800279 usleep(lengthMs * 1000);
Lais Andrade28c81f12024-06-24 14:32:22 +0100280 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700281 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100282 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
283 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700284 }
285 }
286 }
287}
288
289TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000290 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
291 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700292
Steven Moreland2932b222019-11-05 14:30:17 -0800293 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100294 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800295
Steven Morelandd44007e2019-10-24 18:12:46 -0700296 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800297 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000298 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800299
Steven Morelandd44007e2019-10-24 18:12:46 -0700300 for (EffectStrength strength : kEffectStrengths) {
301 std::promise<void> completionPromise;
302 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100303 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
304 [&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800305 int lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100306 ndk::ScopedAStatus status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800307
308 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100309 EXPECT_OK(std::move(status))
310 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800311 EXPECT_GT(lengthMs, 0);
312 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100313 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
314 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800315 }
316
Lais Andrade28c81f12024-06-24 14:32:22 +0100317 if (lengthMs <= 0) continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700318
Lais Andradec689ba52024-04-10 11:07:11 +0100319 auto timeout = std::chrono::milliseconds(lengthMs) + VIBRATION_CALLBACK_TIMEOUT;
Steven Morelandd44007e2019-10-24 18:12:46 -0700320 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andradec689ba52024-04-10 11:07:11 +0100321
Lais Andrade28c81f12024-06-24 14:32:22 +0100322 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700323 }
324 }
325}
326
327TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000328 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
329 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700330
331 for (Effect effect : kEffects) {
332 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100333 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
Steven Morelandd44007e2019-10-24 18:12:46 -0700334 int lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100335 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->perform(effect, strength, callback, &lengthMs))
336 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700337 }
338 }
339}
340
341TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
342 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800343 for (EffectStrength strength : kEffectStrengths) {
344 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100345 EXPECT_UNKNOWN_OR_UNSUPPORTED(
346 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
347 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800348 }
349 }
350 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700351 for (EffectStrength strength : kInvalidEffectStrengths) {
352 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100353 EXPECT_UNKNOWN_OR_UNSUPPORTED(
354 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
355 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700356 }
357 }
358}
359
360TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
361 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100362 EXPECT_OK(vibrator->setAmplitude(0.1f));
363 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
364 EXPECT_OK(vibrator->setAmplitude(0.5f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700365 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100366 EXPECT_OK(vibrator->setAmplitude(1.0f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700367 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100368 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700369 }
370}
371
372TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
373 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100374 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(-1));
375 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(0));
376 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(1.1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700377 }
378}
379
380TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
381 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100382 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700383 }
384}
385
386TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
387 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100388 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700389 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100390 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandd44007e2019-10-24 18:12:46 -0700391 sleep(1);
392 }
393}
394
Steven Morelandc0b92d52019-11-05 13:31:41 -0800395TEST_P(VibratorAidl, ExternalAmplitudeControl) {
396 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000397 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800398
399 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100400 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800401
Steven Morelandc0b92d52019-11-05 13:31:41 -0800402 if (supportsExternalAmplitudeControl) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100403 EXPECT_OK(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800404 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100405 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800406 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100407
408 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800409 } else {
410 EXPECT_FALSE(supportsExternalAmplitudeControl);
411 }
412}
413
Steven Morelandd44007e2019-10-24 18:12:46 -0700414TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
415 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100416 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700417 }
418}
419
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900420TEST_P(VibratorAidl, GetSupportedPrimitives) {
421 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
422 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100423 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900424
Lais Andrade28c81f12024-06-24 14:32:22 +0100425 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900426 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000427 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Lais Andrade7e643772021-07-09 14:59:44 +0100428 bool isPrimitiveRequired =
429 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
430 kRequiredPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900431
Lais Andrade7e643772021-07-09 14:59:44 +0100432 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900433 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900434 }
435}
436
437TEST_P(VibratorAidl, GetPrimitiveDuration) {
438 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900439 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100440 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900441
Lais Andrade28c81f12024-06-24 14:32:22 +0100442 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900443 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000444 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900445 int32_t duration;
446
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900447 if (isPrimitiveSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100448 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &duration))
449 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000450 if (primitive != CompositePrimitive::NOOP) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100451 ASSERT_GT(duration, 0)
452 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000453 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900454 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100455 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->getPrimitiveDuration(primitive, &duration))
456 << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900457 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900458 }
459 }
460}
461
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900462TEST_P(VibratorAidl, ComposeValidPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000463 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
464 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
465 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900466
Lais Andrade38d054e2024-02-09 12:42:09 +0000467 std::vector<CompositePrimitive> supported;
468 int32_t maxDelay, maxSize;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900469
Lais Andrade28c81f12024-06-24 14:32:22 +0100470 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
471 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
472 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900473
Lais Andrade38d054e2024-02-09 12:42:09 +0000474 std::vector<CompositeEffect> composite;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900475
Lais Andrade661481e2024-02-12 10:33:09 +0000476 for (int i = 0; i < supported.size(); i++) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100477 CompositePrimitive primitive = supported[i];
Lais Andrade661481e2024-02-12 10:33:09 +0000478 float t = static_cast<float>(i + 1) / supported.size();
Lais Andrade38d054e2024-02-09 12:42:09 +0000479 CompositeEffect effect;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900480
Lais Andrade661481e2024-02-12 10:33:09 +0000481 effect.delayMs = maxDelay * t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000482 effect.primitive = primitive;
Lais Andrade661481e2024-02-12 10:33:09 +0000483 effect.scale = t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000484
485 if (composite.size() == maxSize) {
486 break;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900487 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000488 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900489
Lais Andrade38d054e2024-02-09 12:42:09 +0000490 if (composite.size() != 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100491 EXPECT_OK(vibrator->compose(composite, nullptr));
492 EXPECT_OK(vibrator->off());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900493 }
494}
495
496TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000497 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
498 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
499 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900500
Lais Andrade28c81f12024-06-24 14:32:22 +0100501 std::vector<CompositePrimitive> unsupported(kInvalidPrimitives);
Lais Andrade38d054e2024-02-09 12:42:09 +0000502 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900503
Lais Andrade28c81f12024-06-24 14:32:22 +0100504 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Lais Andrade38d054e2024-02-09 12:42:09 +0000505
Lais Andrade28c81f12024-06-24 14:32:22 +0100506 for (CompositePrimitive primitive : kCompositePrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000507 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000508 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900509
Lais Andrade38d054e2024-02-09 12:42:09 +0000510 if (!isPrimitiveSupported) {
511 unsupported.push_back(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900512 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000513 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900514
Lais Andrade28c81f12024-06-24 14:32:22 +0100515 for (CompositePrimitive primitive : unsupported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000516 std::vector<CompositeEffect> composite(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900517
Lais Andrade28c81f12024-06-24 14:32:22 +0100518 for (CompositeEffect& effect : composite) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000519 effect.delayMs = 0;
520 effect.primitive = primitive;
521 effect.scale = 1.0f;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900522 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100523 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900524 }
525}
526
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900527TEST_P(VibratorAidl, ComposeScaleBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000528 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
529 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900530 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000531
532 std::vector<CompositeEffect> composite(1);
533 CompositeEffect& effect = composite[0];
534
535 effect.delayMs = 0;
536 effect.primitive = CompositePrimitive::CLICK;
537
538 effect.scale = std::nextafter(0.0f, -1.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100539 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Lais Andrade38d054e2024-02-09 12:42:09 +0000540
541 effect.scale = 0.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100542 EXPECT_OK(vibrator->compose(composite, nullptr));
543 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000544
545 effect.scale = 1.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100546 EXPECT_OK(vibrator->compose(composite, nullptr));
547 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000548
549 effect.scale = std::nextafter(1.0f, 2.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100550 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900551}
552
553TEST_P(VibratorAidl, ComposeDelayBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000554 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
555 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900556 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000557
558 int32_t maxDelay;
559
Lais Andrade28c81f12024-06-24 14:32:22 +0100560 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
Lais Andrade38d054e2024-02-09 12:42:09 +0000561
562 std::vector<CompositeEffect> composite(1);
Lais Andrade661481e2024-02-12 10:33:09 +0000563 CompositeEffect& effect = composite[0];
Lais Andrade38d054e2024-02-09 12:42:09 +0000564
Lais Andrade38d054e2024-02-09 12:42:09 +0000565 effect.primitive = CompositePrimitive::CLICK;
566 effect.scale = 1.0f;
567
Lais Andrade661481e2024-02-12 10:33:09 +0000568 effect.delayMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100569 EXPECT_OK(vibrator->compose(composite, nullptr));
570 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000571
572 effect.delayMs = 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100573 EXPECT_OK(vibrator->compose(composite, nullptr));
574 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000575
576 effect.delayMs = maxDelay;
Lais Andrade28c81f12024-06-24 14:32:22 +0100577 EXPECT_OK(vibrator->compose(composite, nullptr));
578 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000579
580 effect.delayMs = maxDelay + 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100581 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900582}
583
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900584TEST_P(VibratorAidl, ComposeSizeBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000585 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
586 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900587 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000588
589 int32_t maxSize;
590
Lais Andrade28c81f12024-06-24 14:32:22 +0100591 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Lais Andrade38d054e2024-02-09 12:42:09 +0000592
593 std::vector<CompositeEffect> composite(maxSize);
594 CompositeEffect effect;
595
596 effect.delayMs = 1;
597 effect.primitive = CompositePrimitive::CLICK;
598 effect.scale = 1.0f;
599
600 std::fill(composite.begin(), composite.end(), effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100601 EXPECT_OK(vibrator->compose(composite, nullptr));
602 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000603
604 composite.emplace_back(effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100605 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900606}
607
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900608TEST_P(VibratorAidl, ComposeCallback) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000609 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
610 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
611 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900612
Lais Andrade38d054e2024-02-09 12:42:09 +0000613 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100614 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900615
Lais Andrade28c81f12024-06-24 14:32:22 +0100616 for (CompositePrimitive primitive : supported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000617 if (primitive == CompositePrimitive::NOOP) {
618 continue;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900619 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000620
621 std::promise<void> completionPromise;
622 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100623 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
624 [&completionPromise] { completionPromise.set_value(); });
Lais Andrade38d054e2024-02-09 12:42:09 +0000625 CompositeEffect effect;
626 std::vector<CompositeEffect> composite;
627 int32_t durationMs;
628 std::chrono::milliseconds duration;
629 std::chrono::time_point<high_resolution_clock> start, end;
630 std::chrono::milliseconds elapsed;
631
632 effect.delayMs = 0;
633 effect.primitive = primitive;
634 effect.scale = 1.0f;
635 composite.emplace_back(effect);
636
Lais Andrade28c81f12024-06-24 14:32:22 +0100637 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &durationMs))
638 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000639 duration = std::chrono::milliseconds(durationMs);
640
641 start = high_resolution_clock::now();
Lais Andrade28c81f12024-06-24 14:32:22 +0100642 EXPECT_OK(vibrator->compose(composite, callback))
643 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000644
Lais Andradec689ba52024-04-10 11:07:11 +0100645 EXPECT_EQ(completionFuture.wait_for(duration + VIBRATION_CALLBACK_TIMEOUT),
Lais Andrade38d054e2024-02-09 12:42:09 +0000646 std::future_status::ready)
Lais Andrade28c81f12024-06-24 14:32:22 +0100647 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000648 end = high_resolution_clock::now();
649
650 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Lais Andrade28c81f12024-06-24 14:32:22 +0100651 EXPECT_GE(elapsed.count(), duration.count())
652 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000653
Lais Andrade28c81f12024-06-24 14:32:22 +0100654 EXPECT_OK(vibrator->off()) << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900655 }
656}
657
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900658TEST_P(VibratorAidl, AlwaysOn) {
659 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
660 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100661 EXPECT_OK(vibrator->getSupportedAlwaysOnEffects(&supported));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900662
663 for (Effect effect : kEffects) {
664 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000665 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900666
667 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100668 ndk::ScopedAStatus status = vibrator->alwaysOnEnable(0, effect, strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900669
670 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100671 EXPECT_OK(std::move(status))
672 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900673 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100674 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
675 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900676 }
677 }
678 }
679
Lais Andrade28c81f12024-06-24 14:32:22 +0100680 EXPECT_OK(vibrator->alwaysOnDisable(0));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900681 }
682}
683
Vince Leung4bae4f92021-02-03 06:21:58 +0000684TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000685 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000686}
687
688TEST_P(VibratorAidl, GetQFactor) {
689 float qFactor;
Lais Andrade28c81f12024-06-24 14:32:22 +0100690 ndk::ScopedAStatus status = vibrator->getQFactor(&qFactor);
Vince Leung4bae4f92021-02-03 06:21:58 +0000691 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100692 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000693 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000694 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100695 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung4bae4f92021-02-03 06:21:58 +0000696 }
697}
698
Vince Leung823cf5f2021-02-11 02:21:57 +0000699TEST_P(VibratorAidl, GetFrequencyResolution) {
700 getFrequencyResolutionHz(vibrator, capabilities);
701}
702
703TEST_P(VibratorAidl, GetFrequencyMinimum) {
704 getFrequencyMinimumHz(vibrator, capabilities);
705}
706
707TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
708 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100709 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000710 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100711 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000712 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
713
714 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
715 getFrequencyMinimumHz(vibrator, capabilities)) /
716 getFrequencyResolutionHz(vibrator, capabilities);
717 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
718
719 for (float e : bandwidthAmplitudeMap) {
720 ASSERT_GE(e, 0.0);
721 ASSERT_LE(e, 1.0);
722 }
723 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100724 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000725 }
726}
727
728TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
729 int32_t durationMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100730 ndk::ScopedAStatus status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
Vince Leung823cf5f2021-02-11 02:21:57 +0000731 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100732 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000733 ASSERT_NE(durationMs, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000734 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100735 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000736 }
737}
738
739TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
740 int32_t maxSize;
Lais Andrade28c81f12024-06-24 14:32:22 +0100741 ndk::ScopedAStatus status = vibrator->getPwleCompositionSizeMax(&maxSize);
Vince Leung823cf5f2021-02-11 02:21:57 +0000742 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100743 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000744 ASSERT_NE(maxSize, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000745 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100746 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000747 }
748}
749
750TEST_P(VibratorAidl, GetSupportedBraking) {
751 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100752 ndk::ScopedAStatus status = vibrator->getSupportedBraking(&supported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000753 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
754 bool isDefaultNoneSupported =
755 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
Lais Andrade28c81f12024-06-24 14:32:22 +0100756 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000757 ASSERT_TRUE(isDefaultNoneSupported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000758 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100759 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000760 }
761}
762
763TEST_P(VibratorAidl, ComposeValidPwle) {
764 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade22754c52021-09-14 12:21:59 +0000765 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000766
767 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100768 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000769 bool isClabSupported =
770 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000771 BrakingPwle firstBraking;
772 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
773 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000774
Lais Andrade22754c52021-09-14 12:21:59 +0000775 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities);
776 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
777 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
778 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities);
779 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
780 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
781 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
782 }
783 BrakingPwle secondBraking;
784 secondBraking.braking = Braking::NONE;
785 secondBraking.duration = 10;
786
Lais Andrade28c81f12024-06-24 14:32:22 +0100787 std::vector<PrimitivePwle> pwleQueue = {firstActive, firstBraking, secondActive,
788 secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000789
Lais Andrade28c81f12024-06-24 14:32:22 +0100790 EXPECT_OK(vibrator->composePwle(pwleQueue, nullptr));
791 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000792 }
793}
794
795TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
796 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
797 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
798 return;
799
800 std::promise<void> completionPromise;
801 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100802 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
803 [&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800804 int32_t segmentDurationMaxMs;
805 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
806 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Lais Andradec689ba52024-04-10 11:07:11 +0100807 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Vince Leung823cf5f2021-02-11 02:21:57 +0000808
809 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
810
811 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100812 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000813 bool isClabSupported =
814 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
815 BrakingPwle braking;
816 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
817 braking.duration = 100;
818
Lais Andrade28c81f12024-06-24 14:32:22 +0100819 std::vector<PrimitivePwle> pwleQueue = {active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000820
Lais Andrade28c81f12024-06-24 14:32:22 +0100821 EXPECT_OK(vibrator->composePwle(pwleQueue, callback));
Vince Leung823cf5f2021-02-11 02:21:57 +0000822 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100823 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000824}
825
826TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
827 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
828 std::vector<PrimitivePwle> pwleQueue;
829 // test empty queue
Lais Andrade28c81f12024-06-24 14:32:22 +0100830 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000831
832 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
833
834 PrimitivePwle pwle;
835 pwle = active;
836 int segmentCountMax;
837 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
838
839 // Create PWLE queue with more segments than allowed
840 for (int i = 0; i < segmentCountMax + 10; i++) {
841 pwleQueue.emplace_back(std::move(pwle));
842 }
843
Lais Andrade28c81f12024-06-24 14:32:22 +0100844 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000845 }
846}
847
848TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
849 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
850 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
851 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
852 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
853
Lais Andrade28c81f12024-06-24 14:32:22 +0100854 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000855
Lais Andrade28c81f12024-06-24 14:32:22 +0100856 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000857
858 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
859 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
860
Lais Andrade28c81f12024-06-24 14:32:22 +0100861 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000862
Lais Andrade28c81f12024-06-24 14:32:22 +0100863 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000864 }
865}
866
867TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
868 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
869 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
870 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
871 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
872 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
873
874 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
875 active.startFrequency =
876 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
877 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
878
Lais Andrade28c81f12024-06-24 14:32:22 +0100879 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000880
Lais Andrade28c81f12024-06-24 14:32:22 +0100881 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000882
883 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
884 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
885
Lais Andrade28c81f12024-06-24 14:32:22 +0100886 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000887
Lais Andrade28c81f12024-06-24 14:32:22 +0100888 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000889 }
890}
891
892TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
893 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
894 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
895
chasewu22cb9012022-03-31 23:23:27 +0800896 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +0000897 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
898 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
899
Lais Andrade28c81f12024-06-24 14:32:22 +0100900 std::vector<PrimitivePwle> pwleQueue = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000901
Lais Andrade28c81f12024-06-24 14:32:22 +0100902 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000903 }
904}
905
Lais Andrade80b18612020-10-12 18:44:40 +0000906std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
907 std::vector<std::tuple<int32_t, int32_t>> tuples;
Lais Andrade80b18612020-10-12 18:44:40 +0000908
Lais Andrade28c81f12024-06-24 14:32:22 +0100909 std::vector<std::string> managerNames = findVibratorManagerNames();
910 std::vector<int32_t> vibratorIds;
911 for (int i = 0; i < managerNames.size(); i++) {
912 auto vibratorManager = IVibratorManager::fromBinder(
913 ndk::SpAIBinder(AServiceManager_waitForService(managerNames[i].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +0000914 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100915 for (int32_t vibratorId : vibratorIds) {
916 tuples.emplace_back(i, vibratorId);
Lais Andrade80b18612020-10-12 18:44:40 +0000917 }
918 }
919 }
920
Lais Andrade28c81f12024-06-24 14:32:22 +0100921 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
922 for (int i = 0; i < vibratorNames.size(); i++) {
923 tuples.emplace_back(-1, i);
Lais Andrade80b18612020-10-12 18:44:40 +0000924 }
925
926 return tuples;
927}
928
Vince Leung823cf5f2021-02-11 02:21:57 +0000929std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
930 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +0000931 if (managerIdx < 0) {
932 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
933 }
934 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
935 std::to_string(vibratorId);
936}
937
Dan Shiba4d5322020-07-28 13:09:30 -0700938GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +0000939INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
940 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -0700941
Vince Leung823cf5f2021-02-11 02:21:57 +0000942int main(int argc, char **argv) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700943 ::testing::InitGoogleTest(&argc, argv);
Lais Andrade28c81f12024-06-24 14:32:22 +0100944 ABinderProcess_setThreadPoolMaxThreadCount(1);
945 ABinderProcess_startThreadPool();
Steven Morelandd44007e2019-10-24 18:12:46 -0700946 return RUN_ALL_TESTS();
947}