blob: 33e8660b5f6621ed80e090eadc2aa26b29292f56 [file] [log] [blame]
Steven Morelandd44007e2019-10-24 18:12:46 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <aidl/Gtest.h>
17#include <aidl/Vintf.h>
Lais Andrade28c81f12024-06-24 14:32:22 +010018#include <aidl/android/hardware/vibrator/BnVibratorCallback.h>
19#include <aidl/android/hardware/vibrator/IVibrator.h>
20#include <aidl/android/hardware/vibrator/IVibratorManager.h>
21
22#include <android/binder_manager.h>
23#include <android/binder_process.h>
Lais Andradea3c332f2024-06-20 10:46:06 +010024#include <android/persistable_bundle_aidl.h>
Steven Morelandd44007e2019-10-24 18:12:46 -070025
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090026#include <cmath>
Lais Andradea3c332f2024-06-20 10:46:06 +010027#include <cstdlib>
28#include <ctime>
Steven Morelandd44007e2019-10-24 18:12:46 -070029#include <future>
Ahmad Khalila788bed2024-08-05 14:43:38 +000030#include <iomanip>
31#include <iostream>
32#include <random>
Steven Morelandd44007e2019-10-24 18:12:46 -070033
Lais Andradea3c332f2024-06-20 10:46:06 +010034#include "persistable_bundle_utils.h"
Ahmad Khalila788bed2024-08-05 14:43:38 +000035#include "pwle_v2_utils.h"
Lais Andrade28c81f12024-06-24 14:32:22 +010036#include "test_utils.h"
37
38using aidl::android::hardware::vibrator::ActivePwle;
39using aidl::android::hardware::vibrator::BnVibratorCallback;
40using aidl::android::hardware::vibrator::Braking;
41using aidl::android::hardware::vibrator::BrakingPwle;
42using aidl::android::hardware::vibrator::CompositeEffect;
43using aidl::android::hardware::vibrator::CompositePrimitive;
44using aidl::android::hardware::vibrator::Effect;
45using aidl::android::hardware::vibrator::EffectStrength;
46using aidl::android::hardware::vibrator::IVibrator;
47using aidl::android::hardware::vibrator::IVibratorManager;
48using aidl::android::hardware::vibrator::PrimitivePwle;
Ahmad Khalila788bed2024-08-05 14:43:38 +000049using aidl::android::hardware::vibrator::PwleV2OutputMapEntry;
50using aidl::android::hardware::vibrator::PwleV2Primitive;
Lais Andradea3c332f2024-06-20 10:46:06 +010051using aidl::android::hardware::vibrator::VendorEffect;
52using aidl::android::os::PersistableBundle;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +090053using std::chrono::high_resolution_clock;
Steven Morelandd44007e2019-10-24 18:12:46 -070054
Lais Andradec689ba52024-04-10 11:07:11 +010055using namespace ::std::chrono_literals;
56
Ahmad Khalila788bed2024-08-05 14:43:38 +000057namespace pwle_v2_utils = aidl::android::hardware::vibrator::testing::pwlev2;
58
Lais Andrade28c81f12024-06-24 14:32:22 +010059const std::vector<Effect> kEffects{ndk::enum_range<Effect>().begin(),
60 ndk::enum_range<Effect>().end()};
61const std::vector<EffectStrength> kEffectStrengths{ndk::enum_range<EffectStrength>().begin(),
62 ndk::enum_range<EffectStrength>().end()};
Steven Morelandd44007e2019-10-24 18:12:46 -070063
64const std::vector<Effect> kInvalidEffects = {
Vince Leung823cf5f2021-02-11 02:21:57 +000065 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
66 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070067};
68
69const std::vector<EffectStrength> kInvalidEffectStrengths = {
Vince Leung823cf5f2021-02-11 02:21:57 +000070 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
71 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
Steven Morelandd44007e2019-10-24 18:12:46 -070072};
73
Steven Moreland1c269782020-01-09 11:16:05 -080074const std::vector<CompositePrimitive> kCompositePrimitives{
Lais Andrade28c81f12024-06-24 14:32:22 +010075 ndk::enum_range<CompositePrimitive>().begin(), ndk::enum_range<CompositePrimitive>().end()};
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090076
Lais Andrade7e643772021-07-09 14:59:44 +010077const std::vector<CompositePrimitive> kRequiredPrimitives = {
78 CompositePrimitive::CLICK, CompositePrimitive::LIGHT_TICK,
79 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
80 CompositePrimitive::QUICK_FALL,
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +090081};
82
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090083const std::vector<CompositePrimitive> kInvalidPrimitives = {
Vince Leung823cf5f2021-02-11 02:21:57 +000084 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
85 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090086};
87
Lais Andradec689ba52024-04-10 11:07:11 +010088// Timeout to wait for vibration callback completion.
Lais Andrade28c81f12024-06-24 14:32:22 +010089static constexpr std::chrono::milliseconds VIBRATION_CALLBACK_TIMEOUT = 100ms;
90
Ahmad Khalila788bed2024-08-05 14:43:38 +000091static constexpr int32_t VENDOR_EFFECTS_MIN_VERSION = 3;
Ahmad Khalil7eacaad2024-08-22 12:25:36 +000092static constexpr int32_t PWLE_V2_MIN_VERSION = 3;
Ahmad Khalila788bed2024-08-05 14:43:38 +000093
Lais Andrade28c81f12024-06-24 14:32:22 +010094static std::vector<std::string> findVibratorManagerNames() {
95 std::vector<std::string> names;
96 constexpr auto callback = [](const char* instance, void* context) {
97 auto fullName = std::string(IVibratorManager::descriptor) + "/" + instance;
98 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
99 };
100 AServiceManager_forEachDeclaredInstance(IVibratorManager::descriptor,
101 static_cast<void*>(&names), callback);
102 return names;
103}
104
105static std::vector<std::string> findUnmanagedVibratorNames() {
106 std::vector<std::string> names;
107 constexpr auto callback = [](const char* instance, void* context) {
108 auto fullName = std::string(IVibrator::descriptor) + "/" + instance;
109 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
110 };
111 AServiceManager_forEachDeclaredInstance(IVibrator::descriptor, static_cast<void*>(&names),
112 callback);
113 return names;
114}
Lais Andradec689ba52024-04-10 11:07:11 +0100115
Steven Morelandd44007e2019-10-24 18:12:46 -0700116class CompletionCallback : public BnVibratorCallback {
117 public:
Vince Leung823cf5f2021-02-11 02:21:57 +0000118 CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {}
Lais Andrade28c81f12024-06-24 14:32:22 +0100119 ndk::ScopedAStatus onComplete() override {
Steven Morelandd44007e2019-10-24 18:12:46 -0700120 mCallback();
Lais Andrade28c81f12024-06-24 14:32:22 +0100121 return ndk::ScopedAStatus::ok();
Steven Morelandd44007e2019-10-24 18:12:46 -0700122 }
123
124 private:
125 std::function<void()> mCallback;
126};
127
Lais Andrade80b18612020-10-12 18:44:40 +0000128class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> {
Steven Morelandd44007e2019-10-24 18:12:46 -0700129 public:
130 virtual void SetUp() override {
Lais Andrade80b18612020-10-12 18:44:40 +0000131 int32_t managerIdx = std::get<0>(GetParam());
132 int32_t vibratorId = std::get<1>(GetParam());
Lais Andrade80b18612020-10-12 18:44:40 +0000133
134 if (managerIdx < 0) {
135 // Testing a unmanaged vibrator, using vibratorId as index from registered HALs
Lais Andrade28c81f12024-06-24 14:32:22 +0100136 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
137 ASSERT_LT(vibratorId, vibratorNames.size());
138 vibrator = IVibrator::fromBinder(ndk::SpAIBinder(
139 AServiceManager_waitForService(vibratorNames[vibratorId].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +0000140 } else {
141 // Testing a managed vibrator, using vibratorId to retrieve it from the manager
Lais Andrade28c81f12024-06-24 14:32:22 +0100142 std::vector<std::string> managerNames = findVibratorManagerNames();
143 ASSERT_LT(managerIdx, managerNames.size());
144 auto vibratorManager = IVibratorManager::fromBinder(ndk::SpAIBinder(
145 AServiceManager_waitForService(managerNames[managerIdx].c_str())));
146 EXPECT_OK(vibratorManager->getVibrator(vibratorId, &vibrator))
147 << "\n For vibrator id: " << vibratorId;
Lais Andrade80b18612020-10-12 18:44:40 +0000148 }
149
Steven Morelandd44007e2019-10-24 18:12:46 -0700150 ASSERT_NE(vibrator, nullptr);
Ahmad Khalila788bed2024-08-05 14:43:38 +0000151 EXPECT_OK(vibrator->getInterfaceVersion(&version));
Lais Andrade28c81f12024-06-24 14:32:22 +0100152 EXPECT_OK(vibrator->getCapabilities(&capabilities));
Steven Morelandd44007e2019-10-24 18:12:46 -0700153 }
154
Lais Andrade38d054e2024-02-09 12:42:09 +0000155 virtual void TearDown() override {
156 // Reset vibrator state between tests.
Lais Andrade28c81f12024-06-24 14:32:22 +0100157 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000158 }
159
Lais Andrade28c81f12024-06-24 14:32:22 +0100160 std::shared_ptr<IVibrator> vibrator;
Ahmad Khalila788bed2024-08-05 14:43:38 +0000161 int32_t version;
Steven Morelandd44007e2019-10-24 18:12:46 -0700162 int32_t capabilities;
163};
164
Lais Andrade28c81f12024-06-24 14:32:22 +0100165static float getResonantFrequencyHz(const std::shared_ptr<IVibrator>& vibrator,
166 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000167 float resonantFrequencyHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100168 ndk::ScopedAStatus status = vibrator->getResonantFrequency(&resonantFrequencyHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000169 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100170 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000171 EXPECT_GT(resonantFrequencyHz, 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 resonantFrequencyHz;
176}
177
Lais Andrade28c81f12024-06-24 14:32:22 +0100178static float getFrequencyResolutionHz(const std::shared_ptr<IVibrator>& vibrator,
179 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000180 float freqResolutionHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100181 ndk::ScopedAStatus status = vibrator->getFrequencyResolution(&freqResolutionHz);
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 EXPECT_GT(freqResolutionHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000185 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100186 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000187 }
188 return freqResolutionHz;
189}
190
Lais Andrade28c81f12024-06-24 14:32:22 +0100191static float getFrequencyMinimumHz(const std::shared_ptr<IVibrator>& vibrator,
192 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000193 float freqMinimumHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100194 ndk::ScopedAStatus status = vibrator->getFrequencyMinimum(&freqMinimumHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000195 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100196 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000197
198 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
199
200 EXPECT_GT(freqMinimumHz, 0);
201 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
202 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100203 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000204 }
205 return freqMinimumHz;
206}
207
Lais Andrade28c81f12024-06-24 14:32:22 +0100208static float getFrequencyMaximumHz(const std::shared_ptr<IVibrator>& vibrator,
209 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000210 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100211 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000212 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100213 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000214 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100215 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000216 }
217
chasewu70da3cc2022-03-15 15:16:04 +0800218 float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) *
219 getFrequencyResolutionHz(vibrator, capabilities)) +
220 getFrequencyMinimumHz(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000221 return freqMaximumHz;
222}
223
224static float getAmplitudeMin() {
225 return 0.0;
226}
227
228static float getAmplitudeMax() {
229 return 1.0;
230}
231
Lais Andrade28c81f12024-06-24 14:32:22 +0100232static ActivePwle composeValidActivePwle(const std::shared_ptr<IVibrator>& vibrator,
233 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000234 float frequencyHz;
235 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
236 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
237 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
238 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
239 } else {
240 frequencyHz = 150.0; // default value commonly used
241 }
242
243 ActivePwle active;
244 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
245 active.startFrequency = frequencyHz;
246 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
247 active.endFrequency = frequencyHz;
chasewu22cb9012022-03-31 23:23:27 +0800248 vibrator->getPwlePrimitiveDurationMax(&(active.duration));
Vince Leung823cf5f2021-02-11 02:21:57 +0000249
250 return active;
251}
252
Steven Morelandd44007e2019-10-24 18:12:46 -0700253TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100254 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
Steven Morelandd44007e2019-10-24 18:12:46 -0700255 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100256 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700257}
258
259TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000260 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
261 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700262
263 std::promise<void> completionPromise;
264 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100265 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
266 [&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700267 uint32_t durationMs = 250;
Lais Andradec689ba52024-04-10 11:07:11 +0100268 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Lais Andrade28c81f12024-06-24 14:32:22 +0100269 EXPECT_OK(vibrator->on(durationMs, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700270 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100271 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700272}
273
274TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800275 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100276 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
277 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->on(250, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700278 }
279}
280
281TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800282 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100283 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800284
Steven Morelandd44007e2019-10-24 18:12:46 -0700285 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800286 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000287 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800288
Steven Morelandd44007e2019-10-24 18:12:46 -0700289 for (EffectStrength strength : kEffectStrengths) {
290 int32_t lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100291 ndk::ScopedAStatus status =
292 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800293
294 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100295 EXPECT_OK(std::move(status))
296 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700297 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800298 usleep(lengthMs * 1000);
Lais Andrade28c81f12024-06-24 14:32:22 +0100299 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700300 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100301 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
302 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700303 }
304 }
305 }
306}
307
308TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000309 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
310 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700311
Steven Moreland2932b222019-11-05 14:30:17 -0800312 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100313 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800314
Steven Morelandd44007e2019-10-24 18:12:46 -0700315 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800316 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000317 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800318
Steven Morelandd44007e2019-10-24 18:12:46 -0700319 for (EffectStrength strength : kEffectStrengths) {
320 std::promise<void> completionPromise;
321 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100322 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
323 [&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800324 int lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100325 ndk::ScopedAStatus status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800326
327 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100328 EXPECT_OK(std::move(status))
329 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800330 EXPECT_GT(lengthMs, 0);
331 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100332 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
333 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800334 }
335
Lais Andrade28c81f12024-06-24 14:32:22 +0100336 if (lengthMs <= 0) continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700337
Lais Andradec689ba52024-04-10 11:07:11 +0100338 auto timeout = std::chrono::milliseconds(lengthMs) + VIBRATION_CALLBACK_TIMEOUT;
Steven Morelandd44007e2019-10-24 18:12:46 -0700339 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andradec689ba52024-04-10 11:07:11 +0100340
Lais Andrade28c81f12024-06-24 14:32:22 +0100341 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700342 }
343 }
344}
345
346TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000347 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
348 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700349
350 for (Effect effect : kEffects) {
351 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100352 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
Steven Morelandd44007e2019-10-24 18:12:46 -0700353 int lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100354 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->perform(effect, strength, callback, &lengthMs))
355 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700356 }
357 }
358}
359
360TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
361 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800362 for (EffectStrength strength : kEffectStrengths) {
363 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100364 EXPECT_UNKNOWN_OR_UNSUPPORTED(
365 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
366 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800367 }
368 }
369 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700370 for (EffectStrength strength : kInvalidEffectStrengths) {
371 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100372 EXPECT_UNKNOWN_OR_UNSUPPORTED(
373 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
374 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700375 }
376 }
377}
378
Lais Andradea3c332f2024-06-20 10:46:06 +0100379TEST_P(VibratorAidl, PerformVendorEffectSupported) {
380 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
381
Lais Andrade4526faf2024-08-09 11:57:20 +0100382 float scale = 0.0f;
383 float vendorScale = 0.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100384 for (EffectStrength strength : kEffectStrengths) {
385 PersistableBundle vendorData;
386 ::aidl::android::hardware::vibrator::testing::fillBasicData(&vendorData);
387
388 PersistableBundle nestedData;
389 ::aidl::android::hardware::vibrator::testing::fillBasicData(&nestedData);
390 vendorData.putPersistableBundle("test_nested_bundle", nestedData);
391
392 VendorEffect effect;
393 effect.vendorData = vendorData;
394 effect.strength = strength;
395 effect.scale = scale;
Lais Andrade4526faf2024-08-09 11:57:20 +0100396 effect.vendorScale = vendorScale;
397 scale += 0.5f;
398 vendorScale += 0.2f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100399
400 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
401 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
402
403 // No expectations on the actual status, the effect might be refused with illegal argument
404 // or the vendor might return a service-specific error code.
405 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
406 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
407 << status << "\n For vendor effect with strength" << toString(strength)
408 << " and scale " << effect.scale;
409
410 if (status.isOk()) {
411 // Generic vendor data should not trigger vibrations, but if it does trigger one
412 // then we make sure the vibrator is reset by triggering off().
413 EXPECT_OK(vibrator->off());
414 }
415 }
416}
417
418TEST_P(VibratorAidl, PerformVendorEffectStability) {
419 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
420
421 // Run some iterations of performVendorEffect with randomized vendor data to check basic
422 // stability of the implementation.
423 uint8_t iterations = 200;
424
425 for (EffectStrength strength : kEffectStrengths) {
426 float scale = 0.5f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100427 float vendorScale = 0.2f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100428 for (uint8_t i = 0; i < iterations; i++) {
429 PersistableBundle vendorData;
430 ::aidl::android::hardware::vibrator::testing::fillRandomData(&vendorData);
431
432 VendorEffect effect;
433 effect.vendorData = vendorData;
434 effect.strength = strength;
435 effect.scale = scale;
Lais Andrade4526faf2024-08-09 11:57:20 +0100436 effect.vendorScale = vendorScale;
Lais Andradea3c332f2024-06-20 10:46:06 +0100437 scale *= 2;
Lais Andrade4526faf2024-08-09 11:57:20 +0100438 vendorScale *= 1.5f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100439
440 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
441 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
442
443 // No expectations on the actual status, the effect might be refused with illegal
444 // argument or the vendor might return a service-specific error code.
445 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
446 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
447 << status << "\n For random vendor effect with strength " << toString(strength)
448 << " and scale " << effect.scale;
449
450 if (status.isOk()) {
451 // Random vendor data should not trigger vibrations, but if it does trigger one
452 // then we make sure the vibrator is reset by triggering off().
453 EXPECT_OK(vibrator->off());
454 }
455 }
456 }
457}
458
459TEST_P(VibratorAidl, PerformVendorEffectEmptyVendorData) {
460 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
461
462 for (EffectStrength strength : kEffectStrengths) {
463 VendorEffect effect;
464 effect.strength = strength;
465 effect.scale = 1.0f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100466 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100467
468 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, nullptr /*callback*/);
469
470 EXPECT_TRUE(status.getExceptionCode() == EX_SERVICE_SPECIFIC)
471 << status << "\n For vendor effect with strength " << toString(strength)
472 << " and scale " << effect.scale;
473 }
474}
475
476TEST_P(VibratorAidl, PerformVendorEffectInvalidScale) {
477 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
478
479 VendorEffect effect;
480 effect.strength = EffectStrength::MEDIUM;
481
Lais Andrade4526faf2024-08-09 11:57:20 +0100482 effect.scale = -1.0f;
483 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100484 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
485
Lais Andrade4526faf2024-08-09 11:57:20 +0100486 effect.scale = 1.0f;
487 effect.vendorScale = -1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100488 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
489}
490
491TEST_P(VibratorAidl, PerformVendorEffectUnsupported) {
Ahmad Khalila788bed2024-08-05 14:43:38 +0000492 if (version < VENDOR_EFFECTS_MIN_VERSION) {
493 EXPECT_EQ(capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS, 0)
494 << "Vibrator version " << version << " should not report vendor effects capability";
495 }
Lais Andradea3c332f2024-06-20 10:46:06 +0100496 if (capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) return;
497
498 for (EffectStrength strength : kEffectStrengths) {
499 VendorEffect effect;
500 effect.strength = strength;
501 effect.scale = 1.0f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100502 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100503
504 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->performVendorEffect(effect, nullptr /*callback*/))
505 << "\n For vendor effect with strength " << toString(strength);
506 }
507}
508
Steven Morelandd44007e2019-10-24 18:12:46 -0700509TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
510 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100511 EXPECT_OK(vibrator->setAmplitude(0.1f));
512 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
513 EXPECT_OK(vibrator->setAmplitude(0.5f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700514 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100515 EXPECT_OK(vibrator->setAmplitude(1.0f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700516 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100517 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700518 }
519}
520
521TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
522 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100523 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(-1));
524 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(0));
525 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(1.1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700526 }
527}
528
529TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
530 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100531 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700532 }
533}
534
535TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
536 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100537 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700538 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100539 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandd44007e2019-10-24 18:12:46 -0700540 sleep(1);
541 }
542}
543
Steven Morelandc0b92d52019-11-05 13:31:41 -0800544TEST_P(VibratorAidl, ExternalAmplitudeControl) {
545 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000546 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800547
548 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100549 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800550
Steven Morelandc0b92d52019-11-05 13:31:41 -0800551 if (supportsExternalAmplitudeControl) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100552 EXPECT_OK(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800553 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100554 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800555 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100556
557 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800558 } else {
559 EXPECT_FALSE(supportsExternalAmplitudeControl);
560 }
561}
562
Steven Morelandd44007e2019-10-24 18:12:46 -0700563TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
564 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100565 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700566 }
567}
568
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900569TEST_P(VibratorAidl, GetSupportedPrimitives) {
570 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
571 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();
Lais Andrade7e643772021-07-09 14:59:44 +0100577 bool isPrimitiveRequired =
578 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
579 kRequiredPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900580
Lais Andrade7e643772021-07-09 14:59:44 +0100581 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900582 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900583 }
584}
585
586TEST_P(VibratorAidl, GetPrimitiveDuration) {
587 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900588 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100589 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900590
Lais Andrade28c81f12024-06-24 14:32:22 +0100591 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900592 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000593 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900594 int32_t duration;
595
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900596 if (isPrimitiveSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100597 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &duration))
598 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000599 if (primitive != CompositePrimitive::NOOP) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100600 ASSERT_GT(duration, 0)
601 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000602 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900603 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100604 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->getPrimitiveDuration(primitive, &duration))
605 << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900606 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900607 }
608 }
609}
610
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900611TEST_P(VibratorAidl, ComposeValidPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000612 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
613 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
614 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900615
Lais Andrade38d054e2024-02-09 12:42:09 +0000616 std::vector<CompositePrimitive> supported;
617 int32_t maxDelay, maxSize;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900618
Lais Andrade28c81f12024-06-24 14:32:22 +0100619 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
620 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
621 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900622
Lais Andrade38d054e2024-02-09 12:42:09 +0000623 std::vector<CompositeEffect> composite;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900624
Lais Andrade661481e2024-02-12 10:33:09 +0000625 for (int i = 0; i < supported.size(); i++) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100626 CompositePrimitive primitive = supported[i];
Lais Andrade661481e2024-02-12 10:33:09 +0000627 float t = static_cast<float>(i + 1) / supported.size();
Lais Andrade38d054e2024-02-09 12:42:09 +0000628 CompositeEffect effect;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900629
Lais Andrade661481e2024-02-12 10:33:09 +0000630 effect.delayMs = maxDelay * t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000631 effect.primitive = primitive;
Lais Andrade661481e2024-02-12 10:33:09 +0000632 effect.scale = t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000633
634 if (composite.size() == maxSize) {
635 break;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900636 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000637 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900638
Lais Andrade38d054e2024-02-09 12:42:09 +0000639 if (composite.size() != 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100640 EXPECT_OK(vibrator->compose(composite, nullptr));
641 EXPECT_OK(vibrator->off());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900642 }
643}
644
645TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000646 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
647 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
648 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900649
Lais Andrade28c81f12024-06-24 14:32:22 +0100650 std::vector<CompositePrimitive> unsupported(kInvalidPrimitives);
Lais Andrade38d054e2024-02-09 12:42:09 +0000651 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900652
Lais Andrade28c81f12024-06-24 14:32:22 +0100653 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Lais Andrade38d054e2024-02-09 12:42:09 +0000654
Lais Andrade28c81f12024-06-24 14:32:22 +0100655 for (CompositePrimitive primitive : kCompositePrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000656 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000657 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900658
Lais Andrade38d054e2024-02-09 12:42:09 +0000659 if (!isPrimitiveSupported) {
660 unsupported.push_back(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900661 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000662 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900663
Lais Andrade28c81f12024-06-24 14:32:22 +0100664 for (CompositePrimitive primitive : unsupported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000665 std::vector<CompositeEffect> composite(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900666
Lais Andrade28c81f12024-06-24 14:32:22 +0100667 for (CompositeEffect& effect : composite) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000668 effect.delayMs = 0;
669 effect.primitive = primitive;
670 effect.scale = 1.0f;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900671 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100672 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900673 }
674}
675
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900676TEST_P(VibratorAidl, ComposeScaleBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000677 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
678 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900679 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000680
681 std::vector<CompositeEffect> composite(1);
682 CompositeEffect& effect = composite[0];
683
684 effect.delayMs = 0;
685 effect.primitive = CompositePrimitive::CLICK;
686
687 effect.scale = std::nextafter(0.0f, -1.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100688 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Lais Andrade38d054e2024-02-09 12:42:09 +0000689
690 effect.scale = 0.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100691 EXPECT_OK(vibrator->compose(composite, nullptr));
692 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000693
694 effect.scale = 1.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100695 EXPECT_OK(vibrator->compose(composite, nullptr));
696 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000697
698 effect.scale = std::nextafter(1.0f, 2.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100699 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900700}
701
702TEST_P(VibratorAidl, ComposeDelayBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000703 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
704 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900705 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000706
707 int32_t maxDelay;
708
Lais Andrade28c81f12024-06-24 14:32:22 +0100709 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
Lais Andrade38d054e2024-02-09 12:42:09 +0000710
711 std::vector<CompositeEffect> composite(1);
Lais Andrade661481e2024-02-12 10:33:09 +0000712 CompositeEffect& effect = composite[0];
Lais Andrade38d054e2024-02-09 12:42:09 +0000713
Lais Andrade38d054e2024-02-09 12:42:09 +0000714 effect.primitive = CompositePrimitive::CLICK;
715 effect.scale = 1.0f;
716
Lais Andrade661481e2024-02-12 10:33:09 +0000717 effect.delayMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100718 EXPECT_OK(vibrator->compose(composite, nullptr));
719 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000720
721 effect.delayMs = 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100722 EXPECT_OK(vibrator->compose(composite, nullptr));
723 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000724
725 effect.delayMs = maxDelay;
Lais Andrade28c81f12024-06-24 14:32:22 +0100726 EXPECT_OK(vibrator->compose(composite, nullptr));
727 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000728
729 effect.delayMs = maxDelay + 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100730 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900731}
732
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900733TEST_P(VibratorAidl, ComposeSizeBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000734 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
735 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900736 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000737
738 int32_t maxSize;
739
Lais Andrade28c81f12024-06-24 14:32:22 +0100740 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Lais Andrade38d054e2024-02-09 12:42:09 +0000741
742 std::vector<CompositeEffect> composite(maxSize);
743 CompositeEffect effect;
744
745 effect.delayMs = 1;
746 effect.primitive = CompositePrimitive::CLICK;
747 effect.scale = 1.0f;
748
749 std::fill(composite.begin(), composite.end(), effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100750 EXPECT_OK(vibrator->compose(composite, nullptr));
751 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000752
753 composite.emplace_back(effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100754 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900755}
756
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900757TEST_P(VibratorAidl, ComposeCallback) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000758 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
759 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
760 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900761
Lais Andrade38d054e2024-02-09 12:42:09 +0000762 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100763 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900764
Lais Andrade28c81f12024-06-24 14:32:22 +0100765 for (CompositePrimitive primitive : supported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000766 if (primitive == CompositePrimitive::NOOP) {
767 continue;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900768 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000769
770 std::promise<void> completionPromise;
771 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100772 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
773 [&completionPromise] { completionPromise.set_value(); });
Lais Andrade38d054e2024-02-09 12:42:09 +0000774 CompositeEffect effect;
775 std::vector<CompositeEffect> composite;
776 int32_t durationMs;
777 std::chrono::milliseconds duration;
778 std::chrono::time_point<high_resolution_clock> start, end;
779 std::chrono::milliseconds elapsed;
780
781 effect.delayMs = 0;
782 effect.primitive = primitive;
783 effect.scale = 1.0f;
784 composite.emplace_back(effect);
785
Lais Andrade28c81f12024-06-24 14:32:22 +0100786 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &durationMs))
787 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000788 duration = std::chrono::milliseconds(durationMs);
789
790 start = high_resolution_clock::now();
Lais Andrade28c81f12024-06-24 14:32:22 +0100791 EXPECT_OK(vibrator->compose(composite, callback))
792 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000793
Lais Andradec689ba52024-04-10 11:07:11 +0100794 EXPECT_EQ(completionFuture.wait_for(duration + VIBRATION_CALLBACK_TIMEOUT),
Lais Andrade38d054e2024-02-09 12:42:09 +0000795 std::future_status::ready)
Lais Andrade28c81f12024-06-24 14:32:22 +0100796 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000797 end = high_resolution_clock::now();
798
799 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Lais Andrade28c81f12024-06-24 14:32:22 +0100800 EXPECT_GE(elapsed.count(), duration.count())
801 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000802
Lais Andrade28c81f12024-06-24 14:32:22 +0100803 EXPECT_OK(vibrator->off()) << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900804 }
805}
806
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900807TEST_P(VibratorAidl, AlwaysOn) {
808 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
809 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100810 EXPECT_OK(vibrator->getSupportedAlwaysOnEffects(&supported));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900811
812 for (Effect effect : kEffects) {
813 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000814 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900815
816 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100817 ndk::ScopedAStatus status = vibrator->alwaysOnEnable(0, effect, strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900818
819 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100820 EXPECT_OK(std::move(status))
821 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900822 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100823 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
824 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900825 }
826 }
827 }
828
Lais Andrade28c81f12024-06-24 14:32:22 +0100829 EXPECT_OK(vibrator->alwaysOnDisable(0));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900830 }
831}
832
Vince Leung4bae4f92021-02-03 06:21:58 +0000833TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000834 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000835}
836
837TEST_P(VibratorAidl, GetQFactor) {
838 float qFactor;
Lais Andrade28c81f12024-06-24 14:32:22 +0100839 ndk::ScopedAStatus status = vibrator->getQFactor(&qFactor);
Vince Leung4bae4f92021-02-03 06:21:58 +0000840 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100841 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000842 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000843 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100844 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung4bae4f92021-02-03 06:21:58 +0000845 }
846}
847
Vince Leung823cf5f2021-02-11 02:21:57 +0000848TEST_P(VibratorAidl, GetFrequencyResolution) {
849 getFrequencyResolutionHz(vibrator, capabilities);
850}
851
852TEST_P(VibratorAidl, GetFrequencyMinimum) {
853 getFrequencyMinimumHz(vibrator, capabilities);
854}
855
856TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
857 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100858 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000859 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100860 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000861 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
862
863 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
864 getFrequencyMinimumHz(vibrator, capabilities)) /
865 getFrequencyResolutionHz(vibrator, capabilities);
866 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
867
868 for (float e : bandwidthAmplitudeMap) {
869 ASSERT_GE(e, 0.0);
870 ASSERT_LE(e, 1.0);
871 }
872 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100873 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000874 }
875}
876
877TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
878 int32_t durationMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100879 ndk::ScopedAStatus status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
Vince Leung823cf5f2021-02-11 02:21:57 +0000880 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100881 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000882 ASSERT_NE(durationMs, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000883 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100884 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000885 }
886}
887
888TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
889 int32_t maxSize;
Lais Andrade28c81f12024-06-24 14:32:22 +0100890 ndk::ScopedAStatus status = vibrator->getPwleCompositionSizeMax(&maxSize);
Vince Leung823cf5f2021-02-11 02:21:57 +0000891 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100892 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000893 ASSERT_NE(maxSize, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000894 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100895 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000896 }
897}
898
899TEST_P(VibratorAidl, GetSupportedBraking) {
900 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100901 ndk::ScopedAStatus status = vibrator->getSupportedBraking(&supported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000902 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
903 bool isDefaultNoneSupported =
904 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
Lais Andrade28c81f12024-06-24 14:32:22 +0100905 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000906 ASSERT_TRUE(isDefaultNoneSupported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000907 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100908 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000909 }
910}
911
912TEST_P(VibratorAidl, ComposeValidPwle) {
913 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade22754c52021-09-14 12:21:59 +0000914 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000915
916 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100917 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000918 bool isClabSupported =
919 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000920 BrakingPwle firstBraking;
921 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
922 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000923
Lais Andrade22754c52021-09-14 12:21:59 +0000924 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities);
925 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
926 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
927 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities);
928 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
929 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
930 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
931 }
932 BrakingPwle secondBraking;
933 secondBraking.braking = Braking::NONE;
934 secondBraking.duration = 10;
935
Lais Andrade28c81f12024-06-24 14:32:22 +0100936 std::vector<PrimitivePwle> pwleQueue = {firstActive, firstBraking, secondActive,
937 secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000938
Lais Andrade28c81f12024-06-24 14:32:22 +0100939 EXPECT_OK(vibrator->composePwle(pwleQueue, nullptr));
940 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000941 }
942}
943
944TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
945 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
946 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
947 return;
948
949 std::promise<void> completionPromise;
950 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100951 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
952 [&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800953 int32_t segmentDurationMaxMs;
954 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
955 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Lais Andradec689ba52024-04-10 11:07:11 +0100956 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Vince Leung823cf5f2021-02-11 02:21:57 +0000957
958 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
959
960 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100961 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000962 bool isClabSupported =
963 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
964 BrakingPwle braking;
965 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
966 braking.duration = 100;
967
Lais Andrade28c81f12024-06-24 14:32:22 +0100968 std::vector<PrimitivePwle> pwleQueue = {active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000969
Lais Andrade28c81f12024-06-24 14:32:22 +0100970 EXPECT_OK(vibrator->composePwle(pwleQueue, callback));
Vince Leung823cf5f2021-02-11 02:21:57 +0000971 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100972 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000973}
974
975TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
976 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
977 std::vector<PrimitivePwle> pwleQueue;
978 // test empty queue
Lais Andrade28c81f12024-06-24 14:32:22 +0100979 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000980
981 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
982
983 PrimitivePwle pwle;
984 pwle = active;
985 int segmentCountMax;
986 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
987
988 // Create PWLE queue with more segments than allowed
989 for (int i = 0; i < segmentCountMax + 10; i++) {
990 pwleQueue.emplace_back(std::move(pwle));
991 }
992
Lais Andrade28c81f12024-06-24 14:32:22 +0100993 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000994 }
995}
996
997TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
998 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
999 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
1000 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
1001 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
1002
Lais Andrade28c81f12024-06-24 14:32:22 +01001003 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001004
Lais Andrade28c81f12024-06-24 14:32:22 +01001005 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001006
1007 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
1008 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
1009
Lais Andrade28c81f12024-06-24 14:32:22 +01001010 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001011
Lais Andrade28c81f12024-06-24 14:32:22 +01001012 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001013 }
1014}
1015
1016TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
1017 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
1018 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
1019 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
1020 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
1021 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
1022
1023 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
1024 active.startFrequency =
1025 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1026 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1027
Lais Andrade28c81f12024-06-24 14:32:22 +01001028 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001029
Lais Andrade28c81f12024-06-24 14:32:22 +01001030 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001031
1032 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1033 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1034
Lais Andrade28c81f12024-06-24 14:32:22 +01001035 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001036
Lais Andrade28c81f12024-06-24 14:32:22 +01001037 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001038 }
1039}
1040
1041TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
1042 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
1043 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
1044
chasewu22cb9012022-03-31 23:23:27 +08001045 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +00001046 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
1047 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
1048
Lais Andrade28c81f12024-06-24 14:32:22 +01001049 std::vector<PrimitivePwle> pwleQueue = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001050
Lais Andrade28c81f12024-06-24 14:32:22 +01001051 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001052 }
1053}
1054
Ahmad Khalila788bed2024-08-05 14:43:38 +00001055TEST_P(VibratorAidl, PwleV2FrequencyToOutputAccelerationMapHasValidFrequencyRange) {
1056 std::vector<PwleV2OutputMapEntry> frequencyToOutputAccelerationMap;
1057 ndk::ScopedAStatus status =
1058 vibrator->getPwleV2FrequencyToOutputAccelerationMap(&frequencyToOutputAccelerationMap);
1059 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) {
1060 EXPECT_OK(std::move(status));
1061 ASSERT_FALSE(frequencyToOutputAccelerationMap.empty());
1062 auto sharpnessRange =
1063 pwle_v2_utils::getPwleV2SharpnessRange(vibrator, frequencyToOutputAccelerationMap);
1064 // Validate the curve provides a usable sharpness range, which is a range of frequencies
1065 // that are supported by the device.
1066 ASSERT_TRUE(sharpnessRange.first >= 0);
1067 // Validate that the sharpness range is a valid interval, not a single point.
1068 ASSERT_TRUE(sharpnessRange.first < sharpnessRange.second);
1069 } else {
1070 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
1071 }
1072}
1073
1074TEST_P(VibratorAidl, GetPwleV2PrimitiveDurationMaxMillis) {
1075 int32_t durationMs;
1076 ndk::ScopedAStatus status = vibrator->getPwleV2PrimitiveDurationMaxMillis(&durationMs);
1077 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) {
1078 EXPECT_OK(std::move(status));
1079 ASSERT_GT(durationMs, 0); // Ensure greater than zero
1080 ASSERT_GE(durationMs,
1081 pwle_v2_utils::COMPOSE_PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS);
1082 } else {
1083 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
1084 }
1085}
1086
1087TEST_P(VibratorAidl, GetPwleV2CompositionSizeMax) {
1088 int32_t maxSize;
1089 ndk::ScopedAStatus status = vibrator->getPwleV2CompositionSizeMax(&maxSize);
1090 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) {
1091 EXPECT_OK(std::move(status));
1092 ASSERT_GT(maxSize, 0); // Ensure greater than zero
1093 ASSERT_GE(maxSize, pwle_v2_utils::COMPOSE_PWLE_V2_MIN_REQUIRED_SIZE);
1094 } else {
1095 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
1096 }
1097}
1098
1099TEST_P(VibratorAidl, GetPwleV2PrimitiveDurationMinMillis) {
1100 int32_t durationMs;
1101 ndk::ScopedAStatus status = vibrator->getPwleV2PrimitiveDurationMinMillis(&durationMs);
1102 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) {
1103 EXPECT_OK(std::move(status));
1104 ASSERT_GT(durationMs, 0); // Ensure greater than zero
1105 ASSERT_LE(durationMs, pwle_v2_utils::COMPOSE_PWLE_V2_MAX_ALLOWED_PRIMITIVE_MIN_DURATION_MS);
1106 } else {
1107 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
1108 }
1109}
1110
1111TEST_P(VibratorAidl, ComposeValidPwleV2Effect) {
1112 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1113 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1114 return;
1115 }
1116
1117 EXPECT_OK(vibrator->composePwleV2(pwle_v2_utils::composeValidPwleV2Effect(vibrator), nullptr));
1118 EXPECT_OK(vibrator->off());
1119}
1120
Ahmad Khalil7eacaad2024-08-22 12:25:36 +00001121TEST_P(VibratorAidl, ComposePwleV2Unsupported) {
1122 if (version < PWLE_V2_MIN_VERSION) {
1123 EXPECT_EQ(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2, 0)
1124 << "Vibrator version " << version << " should not report PWLE V2 capability.";
1125 }
1126 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) return;
1127
1128 EXPECT_UNKNOWN_OR_UNSUPPORTED(
1129 vibrator->composePwleV2(pwle_v2_utils::composeValidPwleV2Effect(vibrator), nullptr));
1130}
1131
Ahmad Khalila788bed2024-08-05 14:43:38 +00001132TEST_P(VibratorAidl, ComposeValidPwleV2EffectWithCallback) {
1133 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1134 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1135 return;
1136 }
1137
1138 std::promise<void> completionPromise;
1139 std::future<void> completionFuture{completionPromise.get_future()};
1140 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
1141 [&completionPromise] { completionPromise.set_value(); });
1142
1143 int32_t minDuration;
1144 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMinMillis(&minDuration));
1145 auto timeout = std::chrono::milliseconds(minDuration) + VIBRATION_CALLBACK_TIMEOUT;
1146 float minFrequency = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator);
1147
1148 EXPECT_OK(vibrator->composePwleV2(
1149 {PwleV2Primitive(/*amplitude=*/0.5, minFrequency, minDuration)}, callback));
1150 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
1151 EXPECT_OK(vibrator->off());
1152}
1153
1154TEST_P(VibratorAidl, composePwleV2EffectWithTooManyPoints) {
1155 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1156 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1157 return;
1158 }
1159
1160 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(
1161 pwle_v2_utils::composePwleV2EffectWithTooManyPoints(vibrator), nullptr));
1162}
1163
1164TEST_P(VibratorAidl, composeInvalidPwleV2Effect) {
1165 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1166 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1167 return;
1168 }
1169
1170 // Retrieve min and max durations
1171 int32_t minDurationMs, maxDurationMs;
1172 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMinMillis(&minDurationMs));
1173 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMaxMillis(&maxDurationMs));
1174
1175 std::vector<PwleV2Primitive> composePwle;
1176
1177 // Negative amplitude
1178 composePwle.push_back(PwleV2Primitive(/*amplitude=*/-0.8f, /*frequency=*/100, minDurationMs));
1179 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1180 << "Composing PWLE V2 effect with negative amplitude should fail";
1181 composePwle.clear();
1182
1183 // Amplitude exceeding 1.0
1184 composePwle.push_back(PwleV2Primitive(/*amplitude=*/1.2f, /*frequency=*/100, minDurationMs));
1185 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1186 << "Composing PWLE V2 effect with amplitude greater than 1.0 should fail";
1187 composePwle.clear();
1188
1189 // Duration exceeding maximum
1190 composePwle.push_back(
1191 PwleV2Primitive(/*amplitude=*/0.2f, /*frequency=*/100, maxDurationMs + 10));
1192 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1193 << "Composing PWLE V2 effect with duration exceeding maximum should fail";
1194 composePwle.clear();
1195
1196 // Negative duration
1197 composePwle.push_back(PwleV2Primitive(/*amplitude=*/0.2f, /*frequency=*/100, /*time=*/-1));
1198 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1199 << "Composing PWLE V2 effect with negative duration should fail";
1200 composePwle.clear();
1201
1202 // Frequency below minimum
1203 float minFrequency = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator);
1204 composePwle.push_back(PwleV2Primitive(/*amplitude=*/0.2f, minFrequency - 1, minDurationMs));
1205 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1206 << "Composing PWLE V2 effect with frequency below minimum should fail";
1207 composePwle.clear();
1208
1209 // Frequency above maximum
1210 float maxFrequency = pwle_v2_utils::getPwleV2FrequencyMaxHz(vibrator);
1211 composePwle.push_back(PwleV2Primitive(/*amplitude=*/0.2f, maxFrequency + 1, minDurationMs));
1212 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1213 << "Composing PWLE V2 effect with frequency above maximum should fail";
1214}
1215
Lais Andrade80b18612020-10-12 18:44:40 +00001216std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
1217 std::vector<std::tuple<int32_t, int32_t>> tuples;
Lais Andrade80b18612020-10-12 18:44:40 +00001218
Lais Andrade28c81f12024-06-24 14:32:22 +01001219 std::vector<std::string> managerNames = findVibratorManagerNames();
1220 std::vector<int32_t> vibratorIds;
1221 for (int i = 0; i < managerNames.size(); i++) {
1222 auto vibratorManager = IVibratorManager::fromBinder(
1223 ndk::SpAIBinder(AServiceManager_waitForService(managerNames[i].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +00001224 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Lais Andrade28c81f12024-06-24 14:32:22 +01001225 for (int32_t vibratorId : vibratorIds) {
1226 tuples.emplace_back(i, vibratorId);
Lais Andrade80b18612020-10-12 18:44:40 +00001227 }
1228 }
1229 }
1230
Lais Andrade28c81f12024-06-24 14:32:22 +01001231 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
1232 for (int i = 0; i < vibratorNames.size(); i++) {
1233 tuples.emplace_back(-1, i);
Lais Andrade80b18612020-10-12 18:44:40 +00001234 }
1235
1236 return tuples;
1237}
1238
Vince Leung823cf5f2021-02-11 02:21:57 +00001239std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
1240 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +00001241 if (managerIdx < 0) {
1242 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
1243 }
1244 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
1245 std::to_string(vibratorId);
1246}
1247
Dan Shiba4d5322020-07-28 13:09:30 -07001248GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +00001249INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
1250 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -07001251
Vince Leung823cf5f2021-02-11 02:21:57 +00001252int main(int argc, char **argv) {
Lais Andradea3c332f2024-06-20 10:46:06 +01001253 // Random values are used in the implementation.
1254 std::srand(std::time(nullptr));
1255
Steven Morelandd44007e2019-10-24 18:12:46 -07001256 ::testing::InitGoogleTest(&argc, argv);
Lais Andrade28c81f12024-06-24 14:32:22 +01001257 ABinderProcess_setThreadPoolMaxThreadCount(1);
1258 ABinderProcess_startThreadPool();
Steven Morelandd44007e2019-10-24 18:12:46 -07001259 return RUN_ALL_TESTS();
1260}