blob: ffd38b1cf429c02ec3026a4cd79df39f02db9ed5 [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;
92
Lais Andrade28c81f12024-06-24 14:32:22 +010093static std::vector<std::string> findVibratorManagerNames() {
94 std::vector<std::string> names;
95 constexpr auto callback = [](const char* instance, void* context) {
96 auto fullName = std::string(IVibratorManager::descriptor) + "/" + instance;
97 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
98 };
99 AServiceManager_forEachDeclaredInstance(IVibratorManager::descriptor,
100 static_cast<void*>(&names), callback);
101 return names;
102}
103
104static std::vector<std::string> findUnmanagedVibratorNames() {
105 std::vector<std::string> names;
106 constexpr auto callback = [](const char* instance, void* context) {
107 auto fullName = std::string(IVibrator::descriptor) + "/" + instance;
108 static_cast<std::vector<std::string>*>(context)->emplace_back(fullName);
109 };
110 AServiceManager_forEachDeclaredInstance(IVibrator::descriptor, static_cast<void*>(&names),
111 callback);
112 return names;
113}
Lais Andradec689ba52024-04-10 11:07:11 +0100114
Steven Morelandd44007e2019-10-24 18:12:46 -0700115class CompletionCallback : public BnVibratorCallback {
116 public:
Vince Leung823cf5f2021-02-11 02:21:57 +0000117 CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {}
Lais Andrade28c81f12024-06-24 14:32:22 +0100118 ndk::ScopedAStatus onComplete() override {
Steven Morelandd44007e2019-10-24 18:12:46 -0700119 mCallback();
Lais Andrade28c81f12024-06-24 14:32:22 +0100120 return ndk::ScopedAStatus::ok();
Steven Morelandd44007e2019-10-24 18:12:46 -0700121 }
122
123 private:
124 std::function<void()> mCallback;
125};
126
Lais Andrade80b18612020-10-12 18:44:40 +0000127class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> {
Steven Morelandd44007e2019-10-24 18:12:46 -0700128 public:
129 virtual void SetUp() override {
Lais Andrade80b18612020-10-12 18:44:40 +0000130 int32_t managerIdx = std::get<0>(GetParam());
131 int32_t vibratorId = std::get<1>(GetParam());
Lais Andrade80b18612020-10-12 18:44:40 +0000132
133 if (managerIdx < 0) {
134 // Testing a unmanaged vibrator, using vibratorId as index from registered HALs
Lais Andrade28c81f12024-06-24 14:32:22 +0100135 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
136 ASSERT_LT(vibratorId, vibratorNames.size());
137 vibrator = IVibrator::fromBinder(ndk::SpAIBinder(
138 AServiceManager_waitForService(vibratorNames[vibratorId].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +0000139 } else {
140 // Testing a managed vibrator, using vibratorId to retrieve it from the manager
Lais Andrade28c81f12024-06-24 14:32:22 +0100141 std::vector<std::string> managerNames = findVibratorManagerNames();
142 ASSERT_LT(managerIdx, managerNames.size());
143 auto vibratorManager = IVibratorManager::fromBinder(ndk::SpAIBinder(
144 AServiceManager_waitForService(managerNames[managerIdx].c_str())));
145 EXPECT_OK(vibratorManager->getVibrator(vibratorId, &vibrator))
146 << "\n For vibrator id: " << vibratorId;
Lais Andrade80b18612020-10-12 18:44:40 +0000147 }
148
Steven Morelandd44007e2019-10-24 18:12:46 -0700149 ASSERT_NE(vibrator, nullptr);
Ahmad Khalila788bed2024-08-05 14:43:38 +0000150 EXPECT_OK(vibrator->getInterfaceVersion(&version));
Lais Andrade28c81f12024-06-24 14:32:22 +0100151 EXPECT_OK(vibrator->getCapabilities(&capabilities));
Steven Morelandd44007e2019-10-24 18:12:46 -0700152 }
153
Lais Andrade38d054e2024-02-09 12:42:09 +0000154 virtual void TearDown() override {
155 // Reset vibrator state between tests.
Lais Andrade28c81f12024-06-24 14:32:22 +0100156 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000157 }
158
Lais Andrade28c81f12024-06-24 14:32:22 +0100159 std::shared_ptr<IVibrator> vibrator;
Ahmad Khalila788bed2024-08-05 14:43:38 +0000160 int32_t version;
Steven Morelandd44007e2019-10-24 18:12:46 -0700161 int32_t capabilities;
162};
163
Lais Andrade28c81f12024-06-24 14:32:22 +0100164static float getResonantFrequencyHz(const std::shared_ptr<IVibrator>& vibrator,
165 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000166 float resonantFrequencyHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100167 ndk::ScopedAStatus status = vibrator->getResonantFrequency(&resonantFrequencyHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000168 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100169 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000170 EXPECT_GT(resonantFrequencyHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000171 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100172 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000173 }
174 return resonantFrequencyHz;
175}
176
Lais Andrade28c81f12024-06-24 14:32:22 +0100177static float getFrequencyResolutionHz(const std::shared_ptr<IVibrator>& vibrator,
178 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000179 float freqResolutionHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100180 ndk::ScopedAStatus status = vibrator->getFrequencyResolution(&freqResolutionHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000181 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100182 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000183 EXPECT_GT(freqResolutionHz, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000184 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100185 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000186 }
187 return freqResolutionHz;
188}
189
Lais Andrade28c81f12024-06-24 14:32:22 +0100190static float getFrequencyMinimumHz(const std::shared_ptr<IVibrator>& vibrator,
191 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000192 float freqMinimumHz;
Lais Andrade28c81f12024-06-24 14:32:22 +0100193 ndk::ScopedAStatus status = vibrator->getFrequencyMinimum(&freqMinimumHz);
Vince Leung823cf5f2021-02-11 02:21:57 +0000194 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100195 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000196
197 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
198
199 EXPECT_GT(freqMinimumHz, 0);
200 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
201 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100202 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000203 }
204 return freqMinimumHz;
205}
206
Lais Andrade28c81f12024-06-24 14:32:22 +0100207static float getFrequencyMaximumHz(const std::shared_ptr<IVibrator>& vibrator,
208 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000209 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100210 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000211 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100212 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000213 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100214 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000215 }
216
chasewu70da3cc2022-03-15 15:16:04 +0800217 float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) *
218 getFrequencyResolutionHz(vibrator, capabilities)) +
219 getFrequencyMinimumHz(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000220 return freqMaximumHz;
221}
222
223static float getAmplitudeMin() {
224 return 0.0;
225}
226
227static float getAmplitudeMax() {
228 return 1.0;
229}
230
Lais Andrade28c81f12024-06-24 14:32:22 +0100231static ActivePwle composeValidActivePwle(const std::shared_ptr<IVibrator>& vibrator,
232 int32_t capabilities) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000233 float frequencyHz;
234 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
235 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
236 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
237 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
238 } else {
239 frequencyHz = 150.0; // default value commonly used
240 }
241
242 ActivePwle active;
243 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
244 active.startFrequency = frequencyHz;
245 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
246 active.endFrequency = frequencyHz;
chasewu22cb9012022-03-31 23:23:27 +0800247 vibrator->getPwlePrimitiveDurationMax(&(active.duration));
Vince Leung823cf5f2021-02-11 02:21:57 +0000248
249 return active;
250}
251
Steven Morelandd44007e2019-10-24 18:12:46 -0700252TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100253 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
Steven Morelandd44007e2019-10-24 18:12:46 -0700254 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100255 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700256}
257
258TEST_P(VibratorAidl, OnWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000259 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
260 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700261
262 std::promise<void> completionPromise;
263 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100264 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
265 [&completionPromise] { completionPromise.set_value(); });
Steven Morelandd44007e2019-10-24 18:12:46 -0700266 uint32_t durationMs = 250;
Lais Andradec689ba52024-04-10 11:07:11 +0100267 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Lais Andrade28c81f12024-06-24 14:32:22 +0100268 EXPECT_OK(vibrator->on(durationMs, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700269 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100270 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700271}
272
273TEST_P(VibratorAidl, OnCallbackNotSupported) {
Fenglin Wu15b01dc2020-11-23 10:03:10 +0800274 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100275 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
276 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->on(250, callback));
Steven Morelandd44007e2019-10-24 18:12:46 -0700277 }
278}
279
280TEST_P(VibratorAidl, ValidateEffect) {
Steven Moreland2932b222019-11-05 14:30:17 -0800281 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100282 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800283
Steven Morelandd44007e2019-10-24 18:12:46 -0700284 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800285 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000286 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800287
Steven Morelandd44007e2019-10-24 18:12:46 -0700288 for (EffectStrength strength : kEffectStrengths) {
289 int32_t lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100290 ndk::ScopedAStatus status =
291 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800292
293 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100294 EXPECT_OK(std::move(status))
295 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700296 EXPECT_GT(lengthMs, 0);
Steven Morelandf3353882019-11-07 17:02:43 -0800297 usleep(lengthMs * 1000);
Lais Andrade28c81f12024-06-24 14:32:22 +0100298 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700299 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100300 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
301 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700302 }
303 }
304 }
305}
306
307TEST_P(VibratorAidl, ValidateEffectWithCallback) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000308 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
309 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700310
Steven Moreland2932b222019-11-05 14:30:17 -0800311 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100312 EXPECT_OK(vibrator->getSupportedEffects(&supported));
Steven Moreland2932b222019-11-05 14:30:17 -0800313
Steven Morelandd44007e2019-10-24 18:12:46 -0700314 for (Effect effect : kEffects) {
Steven Moreland2932b222019-11-05 14:30:17 -0800315 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000316 std::find(supported.begin(), supported.end(), effect) != supported.end();
Steven Moreland2932b222019-11-05 14:30:17 -0800317
Steven Morelandd44007e2019-10-24 18:12:46 -0700318 for (EffectStrength strength : kEffectStrengths) {
319 std::promise<void> completionPromise;
320 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100321 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
322 [&completionPromise] { completionPromise.set_value(); });
Steven Moreland2932b222019-11-05 14:30:17 -0800323 int lengthMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100324 ndk::ScopedAStatus status = vibrator->perform(effect, strength, callback, &lengthMs);
Steven Moreland2932b222019-11-05 14:30:17 -0800325
326 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100327 EXPECT_OK(std::move(status))
328 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800329 EXPECT_GT(lengthMs, 0);
330 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100331 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
332 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Moreland2932b222019-11-05 14:30:17 -0800333 }
334
Lais Andrade28c81f12024-06-24 14:32:22 +0100335 if (lengthMs <= 0) continue;
Steven Morelandd44007e2019-10-24 18:12:46 -0700336
Lais Andradec689ba52024-04-10 11:07:11 +0100337 auto timeout = std::chrono::milliseconds(lengthMs) + VIBRATION_CALLBACK_TIMEOUT;
Steven Morelandd44007e2019-10-24 18:12:46 -0700338 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andradec689ba52024-04-10 11:07:11 +0100339
Lais Andrade28c81f12024-06-24 14:32:22 +0100340 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700341 }
342 }
343}
344
345TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000346 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
347 return;
Steven Morelandd44007e2019-10-24 18:12:46 -0700348
349 for (Effect effect : kEffects) {
350 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100351 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
Steven Morelandd44007e2019-10-24 18:12:46 -0700352 int lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100353 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->perform(effect, strength, callback, &lengthMs))
354 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700355 }
356 }
357}
358
359TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
360 for (Effect effect : kInvalidEffects) {
Steven Morelandf3353882019-11-07 17:02:43 -0800361 for (EffectStrength strength : kEffectStrengths) {
362 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100363 EXPECT_UNKNOWN_OR_UNSUPPORTED(
364 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
365 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandf3353882019-11-07 17:02:43 -0800366 }
367 }
368 for (Effect effect : kEffects) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700369 for (EffectStrength strength : kInvalidEffectStrengths) {
370 int32_t lengthMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100371 EXPECT_UNKNOWN_OR_UNSUPPORTED(
372 vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs))
373 << "\n For effect: " << toString(effect) << " " << toString(strength);
Steven Morelandd44007e2019-10-24 18:12:46 -0700374 }
375 }
376}
377
Lais Andradea3c332f2024-06-20 10:46:06 +0100378TEST_P(VibratorAidl, PerformVendorEffectSupported) {
379 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
380
Lais Andrade4526faf2024-08-09 11:57:20 +0100381 float scale = 0.0f;
382 float vendorScale = 0.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100383 for (EffectStrength strength : kEffectStrengths) {
384 PersistableBundle vendorData;
385 ::aidl::android::hardware::vibrator::testing::fillBasicData(&vendorData);
386
387 PersistableBundle nestedData;
388 ::aidl::android::hardware::vibrator::testing::fillBasicData(&nestedData);
389 vendorData.putPersistableBundle("test_nested_bundle", nestedData);
390
391 VendorEffect effect;
392 effect.vendorData = vendorData;
393 effect.strength = strength;
394 effect.scale = scale;
Lais Andrade4526faf2024-08-09 11:57:20 +0100395 effect.vendorScale = vendorScale;
396 scale += 0.5f;
397 vendorScale += 0.2f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100398
399 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
400 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
401
402 // No expectations on the actual status, the effect might be refused with illegal argument
403 // or the vendor might return a service-specific error code.
404 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
405 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
406 << status << "\n For vendor effect with strength" << toString(strength)
407 << " and scale " << effect.scale;
408
409 if (status.isOk()) {
410 // Generic vendor data should not trigger vibrations, but if it does trigger one
411 // then we make sure the vibrator is reset by triggering off().
412 EXPECT_OK(vibrator->off());
413 }
414 }
415}
416
417TEST_P(VibratorAidl, PerformVendorEffectStability) {
418 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
419
420 // Run some iterations of performVendorEffect with randomized vendor data to check basic
421 // stability of the implementation.
422 uint8_t iterations = 200;
423
424 for (EffectStrength strength : kEffectStrengths) {
425 float scale = 0.5f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100426 float vendorScale = 0.2f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100427 for (uint8_t i = 0; i < iterations; i++) {
428 PersistableBundle vendorData;
429 ::aidl::android::hardware::vibrator::testing::fillRandomData(&vendorData);
430
431 VendorEffect effect;
432 effect.vendorData = vendorData;
433 effect.strength = strength;
434 effect.scale = scale;
Lais Andrade4526faf2024-08-09 11:57:20 +0100435 effect.vendorScale = vendorScale;
Lais Andradea3c332f2024-06-20 10:46:06 +0100436 scale *= 2;
Lais Andrade4526faf2024-08-09 11:57:20 +0100437 vendorScale *= 1.5f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100438
439 auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {});
440 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback);
441
442 // No expectations on the actual status, the effect might be refused with illegal
443 // argument or the vendor might return a service-specific error code.
444 EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION &&
445 status.getStatus() != STATUS_UNKNOWN_TRANSACTION)
446 << status << "\n For random vendor effect with strength " << toString(strength)
447 << " and scale " << effect.scale;
448
449 if (status.isOk()) {
450 // Random vendor data should not trigger vibrations, but if it does trigger one
451 // then we make sure the vibrator is reset by triggering off().
452 EXPECT_OK(vibrator->off());
453 }
454 }
455 }
456}
457
458TEST_P(VibratorAidl, PerformVendorEffectEmptyVendorData) {
459 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
460
461 for (EffectStrength strength : kEffectStrengths) {
462 VendorEffect effect;
463 effect.strength = strength;
464 effect.scale = 1.0f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100465 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100466
467 ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, nullptr /*callback*/);
468
469 EXPECT_TRUE(status.getExceptionCode() == EX_SERVICE_SPECIFIC)
470 << status << "\n For vendor effect with strength " << toString(strength)
471 << " and scale " << effect.scale;
472 }
473}
474
475TEST_P(VibratorAidl, PerformVendorEffectInvalidScale) {
476 if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return;
477
478 VendorEffect effect;
479 effect.strength = EffectStrength::MEDIUM;
480
Lais Andrade4526faf2024-08-09 11:57:20 +0100481 effect.scale = -1.0f;
482 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100483 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
484
Lais Andrade4526faf2024-08-09 11:57:20 +0100485 effect.scale = 1.0f;
486 effect.vendorScale = -1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100487 EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/));
488}
489
490TEST_P(VibratorAidl, PerformVendorEffectUnsupported) {
Ahmad Khalila788bed2024-08-05 14:43:38 +0000491 if (version < VENDOR_EFFECTS_MIN_VERSION) {
492 EXPECT_EQ(capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS, 0)
493 << "Vibrator version " << version << " should not report vendor effects capability";
494 }
Lais Andradea3c332f2024-06-20 10:46:06 +0100495 if (capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) return;
496
497 for (EffectStrength strength : kEffectStrengths) {
498 VendorEffect effect;
499 effect.strength = strength;
500 effect.scale = 1.0f;
Lais Andrade4526faf2024-08-09 11:57:20 +0100501 effect.vendorScale = 1.0f;
Lais Andradea3c332f2024-06-20 10:46:06 +0100502
503 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->performVendorEffect(effect, nullptr /*callback*/))
504 << "\n For vendor effect with strength " << toString(strength);
505 }
506}
507
Steven Morelandd44007e2019-10-24 18:12:46 -0700508TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
509 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100510 EXPECT_OK(vibrator->setAmplitude(0.1f));
511 EXPECT_OK(vibrator->on(2000, nullptr /*callback*/));
512 EXPECT_OK(vibrator->setAmplitude(0.5f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700513 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100514 EXPECT_OK(vibrator->setAmplitude(1.0f));
Steven Morelandd44007e2019-10-24 18:12:46 -0700515 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100516 EXPECT_OK(vibrator->off());
Steven Morelandd44007e2019-10-24 18:12:46 -0700517 }
518}
519
520TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
521 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100522 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(-1));
523 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(0));
524 EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(1.1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700525 }
526}
527
528TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
529 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100530 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(1));
Steven Morelandd44007e2019-10-24 18:12:46 -0700531 }
532}
533
534TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
535 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100536 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700537 sleep(1);
Lais Andrade28c81f12024-06-24 14:32:22 +0100538 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandd44007e2019-10-24 18:12:46 -0700539 sleep(1);
540 }
541}
542
Steven Morelandc0b92d52019-11-05 13:31:41 -0800543TEST_P(VibratorAidl, ExternalAmplitudeControl) {
544 const bool supportsExternalAmplitudeControl =
Vince Leung823cf5f2021-02-11 02:21:57 +0000545 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
Steven Morelandc0b92d52019-11-05 13:31:41 -0800546
547 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100548 EXPECT_OK(vibrator->setExternalControl(true));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800549
Steven Morelandc0b92d52019-11-05 13:31:41 -0800550 if (supportsExternalAmplitudeControl) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100551 EXPECT_OK(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800552 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100553 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(0.5));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800554 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100555
556 EXPECT_OK(vibrator->setExternalControl(false));
Steven Morelandc0b92d52019-11-05 13:31:41 -0800557 } else {
558 EXPECT_FALSE(supportsExternalAmplitudeControl);
559 }
560}
561
Steven Morelandd44007e2019-10-24 18:12:46 -0700562TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
563 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100564 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setExternalControl(true));
Steven Morelandd44007e2019-10-24 18:12:46 -0700565 }
566}
567
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900568TEST_P(VibratorAidl, GetSupportedPrimitives) {
569 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
570 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100571 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900572
Lais Andrade28c81f12024-06-24 14:32:22 +0100573 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900574 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000575 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Lais Andrade7e643772021-07-09 14:59:44 +0100576 bool isPrimitiveRequired =
577 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
578 kRequiredPrimitives.end();
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900579
Lais Andrade7e643772021-07-09 14:59:44 +0100580 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900581 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900582 }
583}
584
585TEST_P(VibratorAidl, GetPrimitiveDuration) {
586 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900587 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100588 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900589
Lais Andrade28c81f12024-06-24 14:32:22 +0100590 for (CompositePrimitive primitive : kCompositePrimitives) {
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900591 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000592 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900593 int32_t duration;
594
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900595 if (isPrimitiveSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100596 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &duration))
597 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000598 if (primitive != CompositePrimitive::NOOP) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100599 ASSERT_GT(duration, 0)
600 << "\n For primitive: " << toString(primitive) << " " << duration;
Lais Andradef1b4dd32021-11-03 16:47:32 +0000601 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900602 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100603 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->getPrimitiveDuration(primitive, &duration))
604 << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900605 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900606 }
607 }
608}
609
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900610TEST_P(VibratorAidl, ComposeValidPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000611 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
612 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
613 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900614
Lais Andrade38d054e2024-02-09 12:42:09 +0000615 std::vector<CompositePrimitive> supported;
616 int32_t maxDelay, maxSize;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900617
Lais Andrade28c81f12024-06-24 14:32:22 +0100618 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
619 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
620 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900621
Lais Andrade38d054e2024-02-09 12:42:09 +0000622 std::vector<CompositeEffect> composite;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900623
Lais Andrade661481e2024-02-12 10:33:09 +0000624 for (int i = 0; i < supported.size(); i++) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100625 CompositePrimitive primitive = supported[i];
Lais Andrade661481e2024-02-12 10:33:09 +0000626 float t = static_cast<float>(i + 1) / supported.size();
Lais Andrade38d054e2024-02-09 12:42:09 +0000627 CompositeEffect effect;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900628
Lais Andrade661481e2024-02-12 10:33:09 +0000629 effect.delayMs = maxDelay * t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000630 effect.primitive = primitive;
Lais Andrade661481e2024-02-12 10:33:09 +0000631 effect.scale = t;
Lais Andrade38d054e2024-02-09 12:42:09 +0000632
633 if (composite.size() == maxSize) {
634 break;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900635 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000636 }
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900637
Lais Andrade38d054e2024-02-09 12:42:09 +0000638 if (composite.size() != 0) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100639 EXPECT_OK(vibrator->compose(composite, nullptr));
640 EXPECT_OK(vibrator->off());
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900641 }
642}
643
644TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000645 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
646 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
647 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900648
Lais Andrade28c81f12024-06-24 14:32:22 +0100649 std::vector<CompositePrimitive> unsupported(kInvalidPrimitives);
Lais Andrade38d054e2024-02-09 12:42:09 +0000650 std::vector<CompositePrimitive> supported;
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900651
Lais Andrade28c81f12024-06-24 14:32:22 +0100652 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Lais Andrade38d054e2024-02-09 12:42:09 +0000653
Lais Andrade28c81f12024-06-24 14:32:22 +0100654 for (CompositePrimitive primitive : kCompositePrimitives) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000655 bool isPrimitiveSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000656 std::find(supported.begin(), supported.end(), primitive) != supported.end();
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900657
Lais Andrade38d054e2024-02-09 12:42:09 +0000658 if (!isPrimitiveSupported) {
659 unsupported.push_back(primitive);
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900660 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000661 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900662
Lais Andrade28c81f12024-06-24 14:32:22 +0100663 for (CompositePrimitive primitive : unsupported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000664 std::vector<CompositeEffect> composite(1);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900665
Lais Andrade28c81f12024-06-24 14:32:22 +0100666 for (CompositeEffect& effect : composite) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000667 effect.delayMs = 0;
668 effect.primitive = primitive;
669 effect.scale = 1.0f;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900670 }
Lais Andrade28c81f12024-06-24 14:32:22 +0100671 EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900672 }
673}
674
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900675TEST_P(VibratorAidl, ComposeScaleBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000676 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
677 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900678 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000679
680 std::vector<CompositeEffect> composite(1);
681 CompositeEffect& effect = composite[0];
682
683 effect.delayMs = 0;
684 effect.primitive = CompositePrimitive::CLICK;
685
686 effect.scale = std::nextafter(0.0f, -1.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100687 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Lais Andrade38d054e2024-02-09 12:42:09 +0000688
689 effect.scale = 0.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100690 EXPECT_OK(vibrator->compose(composite, nullptr));
691 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000692
693 effect.scale = 1.0f;
Lais Andrade28c81f12024-06-24 14:32:22 +0100694 EXPECT_OK(vibrator->compose(composite, nullptr));
695 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000696
697 effect.scale = std::nextafter(1.0f, 2.0f);
Lais Andrade28c81f12024-06-24 14:32:22 +0100698 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900699}
700
701TEST_P(VibratorAidl, ComposeDelayBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000702 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
703 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900704 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000705
706 int32_t maxDelay;
707
Lais Andrade28c81f12024-06-24 14:32:22 +0100708 EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay));
Lais Andrade38d054e2024-02-09 12:42:09 +0000709
710 std::vector<CompositeEffect> composite(1);
Lais Andrade661481e2024-02-12 10:33:09 +0000711 CompositeEffect& effect = composite[0];
Lais Andrade38d054e2024-02-09 12:42:09 +0000712
Lais Andrade38d054e2024-02-09 12:42:09 +0000713 effect.primitive = CompositePrimitive::CLICK;
714 effect.scale = 1.0f;
715
Lais Andrade661481e2024-02-12 10:33:09 +0000716 effect.delayMs = 0;
Lais Andrade28c81f12024-06-24 14:32:22 +0100717 EXPECT_OK(vibrator->compose(composite, nullptr));
718 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000719
720 effect.delayMs = 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100721 EXPECT_OK(vibrator->compose(composite, nullptr));
722 EXPECT_OK(vibrator->off());
Lais Andrade661481e2024-02-12 10:33:09 +0000723
724 effect.delayMs = maxDelay;
Lais Andrade28c81f12024-06-24 14:32:22 +0100725 EXPECT_OK(vibrator->compose(composite, nullptr));
726 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000727
728 effect.delayMs = maxDelay + 1;
Lais Andrade28c81f12024-06-24 14:32:22 +0100729 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900730}
731
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900732TEST_P(VibratorAidl, ComposeSizeBoundary) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000733 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
734 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900735 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000736
737 int32_t maxSize;
738
Lais Andrade28c81f12024-06-24 14:32:22 +0100739 EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize));
Lais Andrade38d054e2024-02-09 12:42:09 +0000740
741 std::vector<CompositeEffect> composite(maxSize);
742 CompositeEffect effect;
743
744 effect.delayMs = 1;
745 effect.primitive = CompositePrimitive::CLICK;
746 effect.scale = 1.0f;
747
748 std::fill(composite.begin(), composite.end(), effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100749 EXPECT_OK(vibrator->compose(composite, nullptr));
750 EXPECT_OK(vibrator->off());
Lais Andrade38d054e2024-02-09 12:42:09 +0000751
752 composite.emplace_back(effect);
Lais Andrade28c81f12024-06-24 14:32:22 +0100753 EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr));
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900754}
755
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900756TEST_P(VibratorAidl, ComposeCallback) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000757 if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) {
758 GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported";
759 }
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900760
Lais Andrade38d054e2024-02-09 12:42:09 +0000761 std::vector<CompositePrimitive> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100762 EXPECT_OK(vibrator->getSupportedPrimitives(&supported));
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900763
Lais Andrade28c81f12024-06-24 14:32:22 +0100764 for (CompositePrimitive primitive : supported) {
Lais Andrade38d054e2024-02-09 12:42:09 +0000765 if (primitive == CompositePrimitive::NOOP) {
766 continue;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900767 }
Lais Andrade38d054e2024-02-09 12:42:09 +0000768
769 std::promise<void> completionPromise;
770 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100771 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
772 [&completionPromise] { completionPromise.set_value(); });
Lais Andrade38d054e2024-02-09 12:42:09 +0000773 CompositeEffect effect;
774 std::vector<CompositeEffect> composite;
775 int32_t durationMs;
776 std::chrono::milliseconds duration;
777 std::chrono::time_point<high_resolution_clock> start, end;
778 std::chrono::milliseconds elapsed;
779
780 effect.delayMs = 0;
781 effect.primitive = primitive;
782 effect.scale = 1.0f;
783 composite.emplace_back(effect);
784
Lais Andrade28c81f12024-06-24 14:32:22 +0100785 EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &durationMs))
786 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000787 duration = std::chrono::milliseconds(durationMs);
788
789 start = high_resolution_clock::now();
Lais Andrade28c81f12024-06-24 14:32:22 +0100790 EXPECT_OK(vibrator->compose(composite, callback))
791 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000792
Lais Andradec689ba52024-04-10 11:07:11 +0100793 EXPECT_EQ(completionFuture.wait_for(duration + VIBRATION_CALLBACK_TIMEOUT),
Lais Andrade38d054e2024-02-09 12:42:09 +0000794 std::future_status::ready)
Lais Andrade28c81f12024-06-24 14:32:22 +0100795 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000796 end = high_resolution_clock::now();
797
798 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Lais Andrade28c81f12024-06-24 14:32:22 +0100799 EXPECT_GE(elapsed.count(), duration.count())
800 << "\n For primitive: " << toString(primitive);
Lais Andrade38d054e2024-02-09 12:42:09 +0000801
Lais Andrade28c81f12024-06-24 14:32:22 +0100802 EXPECT_OK(vibrator->off()) << "\n For primitive: " << toString(primitive);
Harpreet \"Eli\" Sanghaafa86362020-01-21 16:15:42 +0900803 }
804}
805
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900806TEST_P(VibratorAidl, AlwaysOn) {
807 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
808 std::vector<Effect> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100809 EXPECT_OK(vibrator->getSupportedAlwaysOnEffects(&supported));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900810
811 for (Effect effect : kEffects) {
812 bool isEffectSupported =
Vince Leung823cf5f2021-02-11 02:21:57 +0000813 std::find(supported.begin(), supported.end(), effect) != supported.end();
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900814
815 for (EffectStrength strength : kEffectStrengths) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100816 ndk::ScopedAStatus status = vibrator->alwaysOnEnable(0, effect, strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900817
818 if (isEffectSupported) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100819 EXPECT_OK(std::move(status))
820 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900821 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100822 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status))
823 << "\n For effect: " << toString(effect) << " " << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900824 }
825 }
826 }
827
Lais Andrade28c81f12024-06-24 14:32:22 +0100828 EXPECT_OK(vibrator->alwaysOnDisable(0));
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900829 }
830}
831
Vince Leung4bae4f92021-02-03 06:21:58 +0000832TEST_P(VibratorAidl, GetResonantFrequency) {
Vince Leung823cf5f2021-02-11 02:21:57 +0000833 getResonantFrequencyHz(vibrator, capabilities);
Vince Leung4bae4f92021-02-03 06:21:58 +0000834}
835
836TEST_P(VibratorAidl, GetQFactor) {
837 float qFactor;
Lais Andrade28c81f12024-06-24 14:32:22 +0100838 ndk::ScopedAStatus status = vibrator->getQFactor(&qFactor);
Vince Leung4bae4f92021-02-03 06:21:58 +0000839 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100840 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000841 ASSERT_GT(qFactor, 0);
Vince Leung4bae4f92021-02-03 06:21:58 +0000842 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100843 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung4bae4f92021-02-03 06:21:58 +0000844 }
845}
846
Vince Leung823cf5f2021-02-11 02:21:57 +0000847TEST_P(VibratorAidl, GetFrequencyResolution) {
848 getFrequencyResolutionHz(vibrator, capabilities);
849}
850
851TEST_P(VibratorAidl, GetFrequencyMinimum) {
852 getFrequencyMinimumHz(vibrator, capabilities);
853}
854
855TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
856 std::vector<float> bandwidthAmplitudeMap;
Lais Andrade28c81f12024-06-24 14:32:22 +0100857 ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
Vince Leung823cf5f2021-02-11 02:21:57 +0000858 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100859 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000860 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
861
862 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
863 getFrequencyMinimumHz(vibrator, capabilities)) /
864 getFrequencyResolutionHz(vibrator, capabilities);
865 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
866
867 for (float e : bandwidthAmplitudeMap) {
868 ASSERT_GE(e, 0.0);
869 ASSERT_LE(e, 1.0);
870 }
871 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100872 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000873 }
874}
875
876TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
877 int32_t durationMs;
Lais Andrade28c81f12024-06-24 14:32:22 +0100878 ndk::ScopedAStatus status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
Vince Leung823cf5f2021-02-11 02:21:57 +0000879 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100880 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000881 ASSERT_NE(durationMs, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000882 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100883 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000884 }
885}
886
887TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
888 int32_t maxSize;
Lais Andrade28c81f12024-06-24 14:32:22 +0100889 ndk::ScopedAStatus status = vibrator->getPwleCompositionSizeMax(&maxSize);
Vince Leung823cf5f2021-02-11 02:21:57 +0000890 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade28c81f12024-06-24 14:32:22 +0100891 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000892 ASSERT_NE(maxSize, 0);
Vince Leung823cf5f2021-02-11 02:21:57 +0000893 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100894 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000895 }
896}
897
898TEST_P(VibratorAidl, GetSupportedBraking) {
899 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100900 ndk::ScopedAStatus status = vibrator->getSupportedBraking(&supported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000901 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
902 bool isDefaultNoneSupported =
903 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
Lais Andrade28c81f12024-06-24 14:32:22 +0100904 EXPECT_OK(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000905 ASSERT_TRUE(isDefaultNoneSupported);
Vince Leung823cf5f2021-02-11 02:21:57 +0000906 } else {
Lais Andrade28c81f12024-06-24 14:32:22 +0100907 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
Vince Leung823cf5f2021-02-11 02:21:57 +0000908 }
909}
910
911TEST_P(VibratorAidl, ComposeValidPwle) {
912 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
Lais Andrade22754c52021-09-14 12:21:59 +0000913 ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities);
Vince Leung823cf5f2021-02-11 02:21:57 +0000914
915 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100916 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000917 bool isClabSupported =
918 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
Lais Andrade22754c52021-09-14 12:21:59 +0000919 BrakingPwle firstBraking;
920 firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
921 firstBraking.duration = 100;
Vince Leung823cf5f2021-02-11 02:21:57 +0000922
Lais Andrade22754c52021-09-14 12:21:59 +0000923 ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities);
924 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
925 float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
926 float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities);
927 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
928 secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f);
929 secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f);
930 }
931 BrakingPwle secondBraking;
932 secondBraking.braking = Braking::NONE;
933 secondBraking.duration = 10;
934
Lais Andrade28c81f12024-06-24 14:32:22 +0100935 std::vector<PrimitivePwle> pwleQueue = {firstActive, firstBraking, secondActive,
936 secondBraking};
Vince Leung823cf5f2021-02-11 02:21:57 +0000937
Lais Andrade28c81f12024-06-24 14:32:22 +0100938 EXPECT_OK(vibrator->composePwle(pwleQueue, nullptr));
939 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000940 }
941}
942
943TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
944 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
945 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
946 return;
947
948 std::promise<void> completionPromise;
949 std::future<void> completionFuture{completionPromise.get_future()};
Lais Andrade28c81f12024-06-24 14:32:22 +0100950 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
951 [&completionPromise] { completionPromise.set_value(); });
chasewu22cb9012022-03-31 23:23:27 +0800952 int32_t segmentDurationMaxMs;
953 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
954 uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below
Lais Andradec689ba52024-04-10 11:07:11 +0100955 auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT;
Vince Leung823cf5f2021-02-11 02:21:57 +0000956
957 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
958
959 std::vector<Braking> supported;
Lais Andrade28c81f12024-06-24 14:32:22 +0100960 EXPECT_OK(vibrator->getSupportedBraking(&supported));
Vince Leung823cf5f2021-02-11 02:21:57 +0000961 bool isClabSupported =
962 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
963 BrakingPwle braking;
964 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
965 braking.duration = 100;
966
Lais Andrade28c81f12024-06-24 14:32:22 +0100967 std::vector<PrimitivePwle> pwleQueue = {active, braking, active};
Vince Leung823cf5f2021-02-11 02:21:57 +0000968
Lais Andrade28c81f12024-06-24 14:32:22 +0100969 EXPECT_OK(vibrator->composePwle(pwleQueue, callback));
Vince Leung823cf5f2021-02-11 02:21:57 +0000970 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
Lais Andrade28c81f12024-06-24 14:32:22 +0100971 EXPECT_OK(vibrator->off());
Vince Leung823cf5f2021-02-11 02:21:57 +0000972}
973
974TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
975 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
976 std::vector<PrimitivePwle> pwleQueue;
977 // test empty queue
Lais Andrade28c81f12024-06-24 14:32:22 +0100978 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000979
980 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
981
982 PrimitivePwle pwle;
983 pwle = active;
984 int segmentCountMax;
985 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
986
987 // Create PWLE queue with more segments than allowed
988 for (int i = 0; i < segmentCountMax + 10; i++) {
989 pwleQueue.emplace_back(std::move(pwle));
990 }
991
Lais Andrade28c81f12024-06-24 14:32:22 +0100992 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +0000993 }
994}
995
996TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
997 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
998 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
999 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
1000 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
1001
Lais Andrade28c81f12024-06-24 14:32:22 +01001002 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001003
Lais Andrade28c81f12024-06-24 14:32:22 +01001004 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001005
1006 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
1007 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
1008
Lais Andrade28c81f12024-06-24 14:32:22 +01001009 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001010
Lais Andrade28c81f12024-06-24 14:32:22 +01001011 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001012 }
1013}
1014
1015TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
1016 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
1017 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
1018 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
1019 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
1020 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
1021
1022 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
1023 active.startFrequency =
1024 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1025 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
1026
Lais Andrade28c81f12024-06-24 14:32:22 +01001027 std::vector<PrimitivePwle> pwleQueueGreater = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001028
Lais Andrade28c81f12024-06-24 14:32:22 +01001029 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001030
1031 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1032 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
1033
Lais Andrade28c81f12024-06-24 14:32:22 +01001034 std::vector<PrimitivePwle> pwleQueueLess = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001035
Lais Andrade28c81f12024-06-24 14:32:22 +01001036 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001037 }
1038}
1039
1040TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
1041 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
1042 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
1043
chasewu22cb9012022-03-31 23:23:27 +08001044 int32_t segmentDurationMaxMs;
Vince Leung823cf5f2021-02-11 02:21:57 +00001045 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
1046 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
1047
Lais Andrade28c81f12024-06-24 14:32:22 +01001048 std::vector<PrimitivePwle> pwleQueue = {active};
Vince Leung823cf5f2021-02-11 02:21:57 +00001049
Lais Andrade28c81f12024-06-24 14:32:22 +01001050 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr));
Vince Leung823cf5f2021-02-11 02:21:57 +00001051 }
1052}
1053
Ahmad Khalila788bed2024-08-05 14:43:38 +00001054TEST_P(VibratorAidl, PwleV2FrequencyToOutputAccelerationMapHasValidFrequencyRange) {
1055 std::vector<PwleV2OutputMapEntry> frequencyToOutputAccelerationMap;
1056 ndk::ScopedAStatus status =
1057 vibrator->getPwleV2FrequencyToOutputAccelerationMap(&frequencyToOutputAccelerationMap);
1058 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) {
1059 EXPECT_OK(std::move(status));
1060 ASSERT_FALSE(frequencyToOutputAccelerationMap.empty());
1061 auto sharpnessRange =
1062 pwle_v2_utils::getPwleV2SharpnessRange(vibrator, frequencyToOutputAccelerationMap);
1063 // Validate the curve provides a usable sharpness range, which is a range of frequencies
1064 // that are supported by the device.
1065 ASSERT_TRUE(sharpnessRange.first >= 0);
1066 // Validate that the sharpness range is a valid interval, not a single point.
1067 ASSERT_TRUE(sharpnessRange.first < sharpnessRange.second);
1068 } else {
1069 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
1070 }
1071}
1072
1073TEST_P(VibratorAidl, GetPwleV2PrimitiveDurationMaxMillis) {
1074 int32_t durationMs;
1075 ndk::ScopedAStatus status = vibrator->getPwleV2PrimitiveDurationMaxMillis(&durationMs);
1076 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) {
1077 EXPECT_OK(std::move(status));
1078 ASSERT_GT(durationMs, 0); // Ensure greater than zero
1079 ASSERT_GE(durationMs,
1080 pwle_v2_utils::COMPOSE_PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS);
1081 } else {
1082 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
1083 }
1084}
1085
1086TEST_P(VibratorAidl, GetPwleV2CompositionSizeMax) {
1087 int32_t maxSize;
1088 ndk::ScopedAStatus status = vibrator->getPwleV2CompositionSizeMax(&maxSize);
1089 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) {
1090 EXPECT_OK(std::move(status));
1091 ASSERT_GT(maxSize, 0); // Ensure greater than zero
1092 ASSERT_GE(maxSize, pwle_v2_utils::COMPOSE_PWLE_V2_MIN_REQUIRED_SIZE);
1093 } else {
1094 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
1095 }
1096}
1097
1098TEST_P(VibratorAidl, GetPwleV2PrimitiveDurationMinMillis) {
1099 int32_t durationMs;
1100 ndk::ScopedAStatus status = vibrator->getPwleV2PrimitiveDurationMinMillis(&durationMs);
1101 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2) {
1102 EXPECT_OK(std::move(status));
1103 ASSERT_GT(durationMs, 0); // Ensure greater than zero
1104 ASSERT_LE(durationMs, pwle_v2_utils::COMPOSE_PWLE_V2_MAX_ALLOWED_PRIMITIVE_MIN_DURATION_MS);
1105 } else {
1106 EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status));
1107 }
1108}
1109
1110TEST_P(VibratorAidl, ComposeValidPwleV2Effect) {
1111 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1112 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1113 return;
1114 }
1115
1116 EXPECT_OK(vibrator->composePwleV2(pwle_v2_utils::composeValidPwleV2Effect(vibrator), nullptr));
1117 EXPECT_OK(vibrator->off());
1118}
1119
1120TEST_P(VibratorAidl, ComposeValidPwleV2EffectWithCallback) {
1121 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1122 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1123 return;
1124 }
1125
1126 std::promise<void> completionPromise;
1127 std::future<void> completionFuture{completionPromise.get_future()};
1128 auto callback = ndk::SharedRefBase::make<CompletionCallback>(
1129 [&completionPromise] { completionPromise.set_value(); });
1130
1131 int32_t minDuration;
1132 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMinMillis(&minDuration));
1133 auto timeout = std::chrono::milliseconds(minDuration) + VIBRATION_CALLBACK_TIMEOUT;
1134 float minFrequency = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator);
1135
1136 EXPECT_OK(vibrator->composePwleV2(
1137 {PwleV2Primitive(/*amplitude=*/0.5, minFrequency, minDuration)}, callback));
1138 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
1139 EXPECT_OK(vibrator->off());
1140}
1141
1142TEST_P(VibratorAidl, composePwleV2EffectWithTooManyPoints) {
1143 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1144 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1145 return;
1146 }
1147
1148 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(
1149 pwle_v2_utils::composePwleV2EffectWithTooManyPoints(vibrator), nullptr));
1150}
1151
1152TEST_P(VibratorAidl, composeInvalidPwleV2Effect) {
1153 if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) {
1154 GTEST_SKIP() << "PWLE V2 not supported, skipping test";
1155 return;
1156 }
1157
1158 // Retrieve min and max durations
1159 int32_t minDurationMs, maxDurationMs;
1160 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMinMillis(&minDurationMs));
1161 EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMaxMillis(&maxDurationMs));
1162
1163 std::vector<PwleV2Primitive> composePwle;
1164
1165 // Negative amplitude
1166 composePwle.push_back(PwleV2Primitive(/*amplitude=*/-0.8f, /*frequency=*/100, minDurationMs));
1167 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1168 << "Composing PWLE V2 effect with negative amplitude should fail";
1169 composePwle.clear();
1170
1171 // Amplitude exceeding 1.0
1172 composePwle.push_back(PwleV2Primitive(/*amplitude=*/1.2f, /*frequency=*/100, minDurationMs));
1173 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1174 << "Composing PWLE V2 effect with amplitude greater than 1.0 should fail";
1175 composePwle.clear();
1176
1177 // Duration exceeding maximum
1178 composePwle.push_back(
1179 PwleV2Primitive(/*amplitude=*/0.2f, /*frequency=*/100, maxDurationMs + 10));
1180 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1181 << "Composing PWLE V2 effect with duration exceeding maximum should fail";
1182 composePwle.clear();
1183
1184 // Negative duration
1185 composePwle.push_back(PwleV2Primitive(/*amplitude=*/0.2f, /*frequency=*/100, /*time=*/-1));
1186 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1187 << "Composing PWLE V2 effect with negative duration should fail";
1188 composePwle.clear();
1189
1190 // Frequency below minimum
1191 float minFrequency = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator);
1192 composePwle.push_back(PwleV2Primitive(/*amplitude=*/0.2f, minFrequency - 1, minDurationMs));
1193 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1194 << "Composing PWLE V2 effect with frequency below minimum should fail";
1195 composePwle.clear();
1196
1197 // Frequency above maximum
1198 float maxFrequency = pwle_v2_utils::getPwleV2FrequencyMaxHz(vibrator);
1199 composePwle.push_back(PwleV2Primitive(/*amplitude=*/0.2f, maxFrequency + 1, minDurationMs));
1200 EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr))
1201 << "Composing PWLE V2 effect with frequency above maximum should fail";
1202}
1203
Lais Andrade80b18612020-10-12 18:44:40 +00001204std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
1205 std::vector<std::tuple<int32_t, int32_t>> tuples;
Lais Andrade80b18612020-10-12 18:44:40 +00001206
Lais Andrade28c81f12024-06-24 14:32:22 +01001207 std::vector<std::string> managerNames = findVibratorManagerNames();
1208 std::vector<int32_t> vibratorIds;
1209 for (int i = 0; i < managerNames.size(); i++) {
1210 auto vibratorManager = IVibratorManager::fromBinder(
1211 ndk::SpAIBinder(AServiceManager_waitForService(managerNames[i].c_str())));
Lais Andrade80b18612020-10-12 18:44:40 +00001212 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
Lais Andrade28c81f12024-06-24 14:32:22 +01001213 for (int32_t vibratorId : vibratorIds) {
1214 tuples.emplace_back(i, vibratorId);
Lais Andrade80b18612020-10-12 18:44:40 +00001215 }
1216 }
1217 }
1218
Lais Andrade28c81f12024-06-24 14:32:22 +01001219 std::vector<std::string> vibratorNames = findUnmanagedVibratorNames();
1220 for (int i = 0; i < vibratorNames.size(); i++) {
1221 tuples.emplace_back(-1, i);
Lais Andrade80b18612020-10-12 18:44:40 +00001222 }
1223
1224 return tuples;
1225}
1226
Vince Leung823cf5f2021-02-11 02:21:57 +00001227std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
1228 const auto &[managerIdx, vibratorId] = info.param;
Lais Andrade80b18612020-10-12 18:44:40 +00001229 if (managerIdx < 0) {
1230 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
1231 }
1232 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
1233 std::to_string(vibratorId);
1234}
1235
Dan Shiba4d5322020-07-28 13:09:30 -07001236GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
Lais Andrade80b18612020-10-12 18:44:40 +00001237INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
1238 PrintGeneratedTest);
Steven Morelandd44007e2019-10-24 18:12:46 -07001239
Vince Leung823cf5f2021-02-11 02:21:57 +00001240int main(int argc, char **argv) {
Lais Andradea3c332f2024-06-20 10:46:06 +01001241 // Random values are used in the implementation.
1242 std::srand(std::time(nullptr));
1243
Steven Morelandd44007e2019-10-24 18:12:46 -07001244 ::testing::InitGoogleTest(&argc, argv);
Lais Andrade28c81f12024-06-24 14:32:22 +01001245 ABinderProcess_setThreadPoolMaxThreadCount(1);
1246 ABinderProcess_startThreadPool();
Steven Morelandd44007e2019-10-24 18:12:46 -07001247 return RUN_ALL_TESTS();
1248}