Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 18 | #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 Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 24 | #include <android/persistable_bundle_aidl.h> |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 25 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 26 | #include <cmath> |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 27 | #include <cstdlib> |
| 28 | #include <ctime> |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 29 | #include <future> |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 30 | #include <iomanip> |
| 31 | #include <iostream> |
| 32 | #include <random> |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 33 | |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 34 | #include "persistable_bundle_utils.h" |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 35 | #include "pwle_v2_utils.h" |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 36 | #include "test_utils.h" |
| 37 | |
| 38 | using aidl::android::hardware::vibrator::ActivePwle; |
| 39 | using aidl::android::hardware::vibrator::BnVibratorCallback; |
| 40 | using aidl::android::hardware::vibrator::Braking; |
| 41 | using aidl::android::hardware::vibrator::BrakingPwle; |
| 42 | using aidl::android::hardware::vibrator::CompositeEffect; |
| 43 | using aidl::android::hardware::vibrator::CompositePrimitive; |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 44 | using aidl::android::hardware::vibrator::CompositePwleV2; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 45 | using aidl::android::hardware::vibrator::Effect; |
| 46 | using aidl::android::hardware::vibrator::EffectStrength; |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 47 | using aidl::android::hardware::vibrator::FrequencyAccelerationMapEntry; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 48 | using aidl::android::hardware::vibrator::IVibrator; |
| 49 | using aidl::android::hardware::vibrator::IVibratorManager; |
| 50 | using aidl::android::hardware::vibrator::PrimitivePwle; |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 51 | using aidl::android::hardware::vibrator::PwleV2Primitive; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 52 | using aidl::android::hardware::vibrator::VendorEffect; |
| 53 | using aidl::android::os::PersistableBundle; |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 54 | using std::chrono::high_resolution_clock; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 55 | |
Lais Andrade | c689ba5 | 2024-04-10 11:07:11 +0100 | [diff] [blame] | 56 | using namespace ::std::chrono_literals; |
| 57 | |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 58 | namespace pwle_v2_utils = aidl::android::hardware::vibrator::testing::pwlev2; |
| 59 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 60 | const std::vector<Effect> kEffects{ndk::enum_range<Effect>().begin(), |
| 61 | ndk::enum_range<Effect>().end()}; |
| 62 | const std::vector<EffectStrength> kEffectStrengths{ndk::enum_range<EffectStrength>().begin(), |
| 63 | ndk::enum_range<EffectStrength>().end()}; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 64 | |
| 65 | const std::vector<Effect> kInvalidEffects = { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 66 | static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1), |
| 67 | static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1), |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | const std::vector<EffectStrength> kInvalidEffectStrengths = { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 71 | static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1), |
| 72 | static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1), |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Steven Moreland | 1c26978 | 2020-01-09 11:16:05 -0800 | [diff] [blame] | 75 | const std::vector<CompositePrimitive> kCompositePrimitives{ |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 76 | ndk::enum_range<CompositePrimitive>().begin(), ndk::enum_range<CompositePrimitive>().end()}; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 77 | |
Lais Andrade | 7e64377 | 2021-07-09 14:59:44 +0100 | [diff] [blame] | 78 | const std::vector<CompositePrimitive> kRequiredPrimitives = { |
| 79 | CompositePrimitive::CLICK, CompositePrimitive::LIGHT_TICK, |
| 80 | CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE, |
| 81 | CompositePrimitive::QUICK_FALL, |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 82 | }; |
| 83 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 84 | const std::vector<CompositePrimitive> kInvalidPrimitives = { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 85 | static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1), |
| 86 | static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1), |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 87 | }; |
| 88 | |
Lais Andrade | c689ba5 | 2024-04-10 11:07:11 +0100 | [diff] [blame] | 89 | // Timeout to wait for vibration callback completion. |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 90 | static constexpr std::chrono::milliseconds VIBRATION_CALLBACK_TIMEOUT = 100ms; |
| 91 | |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 92 | static constexpr int32_t VENDOR_EFFECTS_MIN_VERSION = 3; |
Ahmad Khalil | 7eacaad | 2024-08-22 12:25:36 +0000 | [diff] [blame] | 93 | static constexpr int32_t PWLE_V2_MIN_VERSION = 3; |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 94 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 95 | static std::vector<std::string> findVibratorManagerNames() { |
| 96 | std::vector<std::string> names; |
| 97 | constexpr auto callback = [](const char* instance, void* context) { |
| 98 | auto fullName = std::string(IVibratorManager::descriptor) + "/" + instance; |
| 99 | static_cast<std::vector<std::string>*>(context)->emplace_back(fullName); |
| 100 | }; |
| 101 | AServiceManager_forEachDeclaredInstance(IVibratorManager::descriptor, |
| 102 | static_cast<void*>(&names), callback); |
| 103 | return names; |
| 104 | } |
| 105 | |
| 106 | static std::vector<std::string> findUnmanagedVibratorNames() { |
| 107 | std::vector<std::string> names; |
| 108 | constexpr auto callback = [](const char* instance, void* context) { |
| 109 | auto fullName = std::string(IVibrator::descriptor) + "/" + instance; |
| 110 | static_cast<std::vector<std::string>*>(context)->emplace_back(fullName); |
| 111 | }; |
| 112 | AServiceManager_forEachDeclaredInstance(IVibrator::descriptor, static_cast<void*>(&names), |
| 113 | callback); |
| 114 | return names; |
| 115 | } |
Lais Andrade | c689ba5 | 2024-04-10 11:07:11 +0100 | [diff] [blame] | 116 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 117 | class CompletionCallback : public BnVibratorCallback { |
| 118 | public: |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 119 | CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {} |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 120 | ndk::ScopedAStatus onComplete() override { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 121 | mCallback(); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 122 | return ndk::ScopedAStatus::ok(); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | private: |
| 126 | std::function<void()> mCallback; |
| 127 | }; |
| 128 | |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 129 | class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 130 | public: |
| 131 | virtual void SetUp() override { |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 132 | int32_t managerIdx = std::get<0>(GetParam()); |
| 133 | int32_t vibratorId = std::get<1>(GetParam()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 134 | |
| 135 | if (managerIdx < 0) { |
| 136 | // Testing a unmanaged vibrator, using vibratorId as index from registered HALs |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 137 | std::vector<std::string> vibratorNames = findUnmanagedVibratorNames(); |
| 138 | ASSERT_LT(vibratorId, vibratorNames.size()); |
| 139 | vibrator = IVibrator::fromBinder(ndk::SpAIBinder( |
| 140 | AServiceManager_waitForService(vibratorNames[vibratorId].c_str()))); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 141 | } else { |
| 142 | // Testing a managed vibrator, using vibratorId to retrieve it from the manager |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 143 | std::vector<std::string> managerNames = findVibratorManagerNames(); |
| 144 | ASSERT_LT(managerIdx, managerNames.size()); |
| 145 | auto vibratorManager = IVibratorManager::fromBinder(ndk::SpAIBinder( |
| 146 | AServiceManager_waitForService(managerNames[managerIdx].c_str()))); |
| 147 | EXPECT_OK(vibratorManager->getVibrator(vibratorId, &vibrator)) |
| 148 | << "\n For vibrator id: " << vibratorId; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 151 | ASSERT_NE(vibrator, nullptr); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 152 | EXPECT_OK(vibrator->getInterfaceVersion(&version)); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 153 | EXPECT_OK(vibrator->getCapabilities(&capabilities)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 156 | virtual void TearDown() override { |
| 157 | // Reset vibrator state between tests. |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 158 | EXPECT_OK(vibrator->off()); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 161 | std::shared_ptr<IVibrator> vibrator; |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 162 | int32_t version; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 163 | int32_t capabilities; |
| 164 | }; |
| 165 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 166 | static float getResonantFrequencyHz(const std::shared_ptr<IVibrator>& vibrator, |
| 167 | int32_t capabilities) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 168 | float resonantFrequencyHz; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 169 | ndk::ScopedAStatus status = vibrator->getResonantFrequency(&resonantFrequencyHz); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 170 | if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 171 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 172 | EXPECT_GT(resonantFrequencyHz, 0); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 173 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 174 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 175 | } |
| 176 | return resonantFrequencyHz; |
| 177 | } |
| 178 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 179 | static bool shouldValidateLegacyFrequencyControlResult(int32_t capabilities, int32_t version, |
| 180 | ndk::ScopedAStatus& status) { |
| 181 | bool hasFrequencyControl = capabilities & IVibrator::CAP_FREQUENCY_CONTROL; |
| 182 | // Legacy frequency control APIs deprecated with PWLE V2 feature. |
| 183 | bool isDeprecated = version >= PWLE_V2_MIN_VERSION; |
| 184 | bool isUnknownOrUnsupported = status.getExceptionCode() == EX_UNSUPPORTED_OPERATION || |
| 185 | status.getStatus() == STATUS_UNKNOWN_TRANSACTION; |
| 186 | |
| 187 | // Validate if older HAL or if result is provided, even after deprecation. |
| 188 | return hasFrequencyControl && (!isDeprecated || !isUnknownOrUnsupported); |
| 189 | } |
| 190 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 191 | static float getFrequencyResolutionHz(const std::shared_ptr<IVibrator>& vibrator, |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 192 | int32_t capabilities, int32_t version) { |
| 193 | float freqResolutionHz = -1; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 194 | ndk::ScopedAStatus status = vibrator->getFrequencyResolution(&freqResolutionHz); |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 195 | if (shouldValidateLegacyFrequencyControlResult(capabilities, version, status)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 196 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 197 | EXPECT_GT(freqResolutionHz, 0); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 198 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 199 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 200 | } |
| 201 | return freqResolutionHz; |
| 202 | } |
| 203 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 204 | static float getFrequencyMinimumHz(const std::shared_ptr<IVibrator>& vibrator, int32_t capabilities, |
| 205 | int32_t version) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 206 | float freqMinimumHz; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 207 | ndk::ScopedAStatus status = vibrator->getFrequencyMinimum(&freqMinimumHz); |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 208 | if (shouldValidateLegacyFrequencyControlResult(capabilities, version, status)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 209 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 210 | |
| 211 | float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities); |
| 212 | |
| 213 | EXPECT_GT(freqMinimumHz, 0); |
| 214 | EXPECT_LE(freqMinimumHz, resonantFrequencyHz); |
| 215 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 216 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 217 | } |
| 218 | return freqMinimumHz; |
| 219 | } |
| 220 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 221 | static float getFrequencyMaximumHz(const std::shared_ptr<IVibrator>& vibrator, int32_t capabilities, |
| 222 | int32_t version) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 223 | std::vector<float> bandwidthAmplitudeMap; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 224 | ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap); |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 225 | if (shouldValidateLegacyFrequencyControlResult(capabilities, version, status)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 226 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 227 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 228 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 229 | } |
| 230 | |
chasewu | 70da3cc | 2022-03-15 15:16:04 +0800 | [diff] [blame] | 231 | float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) * |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 232 | getFrequencyResolutionHz(vibrator, capabilities, version)) + |
| 233 | getFrequencyMinimumHz(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 234 | return freqMaximumHz; |
| 235 | } |
| 236 | |
| 237 | static float getAmplitudeMin() { |
| 238 | return 0.0; |
| 239 | } |
| 240 | |
| 241 | static float getAmplitudeMax() { |
| 242 | return 1.0; |
| 243 | } |
| 244 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 245 | static ActivePwle composeValidActivePwle(const std::shared_ptr<IVibrator>& vibrator, |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 246 | int32_t capabilities, int32_t version) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 247 | float frequencyHz; |
| 248 | if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) { |
| 249 | frequencyHz = getResonantFrequencyHz(vibrator, capabilities); |
| 250 | } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 251 | if (version < PWLE_V2_MIN_VERSION) { |
| 252 | frequencyHz = getFrequencyMinimumHz(vibrator, capabilities, version); |
| 253 | } else { |
| 254 | frequencyHz = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator); |
| 255 | } |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 256 | } else { |
| 257 | frequencyHz = 150.0; // default value commonly used |
| 258 | } |
| 259 | |
| 260 | ActivePwle active; |
| 261 | active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2; |
| 262 | active.startFrequency = frequencyHz; |
| 263 | active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2; |
| 264 | active.endFrequency = frequencyHz; |
chasewu | 22cb901 | 2022-03-31 23:23:27 +0800 | [diff] [blame] | 265 | vibrator->getPwlePrimitiveDurationMax(&(active.duration)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 266 | |
| 267 | return active; |
| 268 | } |
| 269 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 270 | TEST_P(VibratorAidl, OnThenOffBeforeTimeout) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 271 | EXPECT_OK(vibrator->on(2000, nullptr /*callback*/)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 272 | sleep(1); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 273 | EXPECT_OK(vibrator->off()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | TEST_P(VibratorAidl, OnWithCallback) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 277 | if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) |
| 278 | return; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 279 | |
| 280 | std::promise<void> completionPromise; |
| 281 | std::future<void> completionFuture{completionPromise.get_future()}; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 282 | auto callback = ndk::SharedRefBase::make<CompletionCallback>( |
| 283 | [&completionPromise] { completionPromise.set_value(); }); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 284 | uint32_t durationMs = 250; |
Lais Andrade | c689ba5 | 2024-04-10 11:07:11 +0100 | [diff] [blame] | 285 | auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 286 | EXPECT_OK(vibrator->on(durationMs, callback)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 287 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 288 | EXPECT_OK(vibrator->off()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | TEST_P(VibratorAidl, OnCallbackNotSupported) { |
Fenglin Wu | 15b01dc | 2020-11-23 10:03:10 +0800 | [diff] [blame] | 292 | if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 293 | auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {}); |
| 294 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->on(250, callback)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | TEST_P(VibratorAidl, ValidateEffect) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 299 | std::vector<Effect> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 300 | EXPECT_OK(vibrator->getSupportedEffects(&supported)); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 301 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 302 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 303 | bool isEffectSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 304 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 305 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 306 | for (EffectStrength strength : kEffectStrengths) { |
| 307 | int32_t lengthMs = 0; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 308 | ndk::ScopedAStatus status = |
| 309 | vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 310 | |
| 311 | if (isEffectSupported) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 312 | EXPECT_OK(std::move(status)) |
| 313 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 314 | EXPECT_GT(lengthMs, 0); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 315 | usleep(lengthMs * 1000); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 316 | EXPECT_OK(vibrator->off()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 317 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 318 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)) |
| 319 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | TEST_P(VibratorAidl, ValidateEffectWithCallback) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 326 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) |
| 327 | return; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 328 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 329 | std::vector<Effect> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 330 | EXPECT_OK(vibrator->getSupportedEffects(&supported)); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 331 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 332 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 333 | bool isEffectSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 334 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 335 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 336 | for (EffectStrength strength : kEffectStrengths) { |
| 337 | std::promise<void> completionPromise; |
| 338 | std::future<void> completionFuture{completionPromise.get_future()}; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 339 | auto callback = ndk::SharedRefBase::make<CompletionCallback>( |
| 340 | [&completionPromise] { completionPromise.set_value(); }); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 341 | int lengthMs = 0; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 342 | ndk::ScopedAStatus status = vibrator->perform(effect, strength, callback, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 343 | |
| 344 | if (isEffectSupported) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 345 | EXPECT_OK(std::move(status)) |
| 346 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 347 | EXPECT_GT(lengthMs, 0); |
| 348 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 349 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)) |
| 350 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 353 | if (lengthMs <= 0) continue; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 354 | |
Lais Andrade | c689ba5 | 2024-04-10 11:07:11 +0100 | [diff] [blame] | 355 | auto timeout = std::chrono::milliseconds(lengthMs) + VIBRATION_CALLBACK_TIMEOUT; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 356 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
Lais Andrade | c689ba5 | 2024-04-10 11:07:11 +0100 | [diff] [blame] | 357 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 358 | EXPECT_OK(vibrator->off()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 364 | if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) |
| 365 | return; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 366 | |
| 367 | for (Effect effect : kEffects) { |
| 368 | for (EffectStrength strength : kEffectStrengths) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 369 | auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {}); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 370 | int lengthMs; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 371 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->perform(effect, strength, callback, &lengthMs)) |
| 372 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | TEST_P(VibratorAidl, InvalidEffectsUnsupported) { |
| 378 | for (Effect effect : kInvalidEffects) { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 379 | for (EffectStrength strength : kEffectStrengths) { |
| 380 | int32_t lengthMs; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 381 | EXPECT_UNKNOWN_OR_UNSUPPORTED( |
| 382 | vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs)) |
| 383 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | for (Effect effect : kEffects) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 387 | for (EffectStrength strength : kInvalidEffectStrengths) { |
| 388 | int32_t lengthMs; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 389 | EXPECT_UNKNOWN_OR_UNSUPPORTED( |
| 390 | vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs)) |
| 391 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 396 | TEST_P(VibratorAidl, PerformVendorEffectSupported) { |
| 397 | if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return; |
| 398 | |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 399 | float scale = 0.0f; |
| 400 | float vendorScale = 0.0f; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 401 | for (EffectStrength strength : kEffectStrengths) { |
| 402 | PersistableBundle vendorData; |
| 403 | ::aidl::android::hardware::vibrator::testing::fillBasicData(&vendorData); |
| 404 | |
| 405 | PersistableBundle nestedData; |
| 406 | ::aidl::android::hardware::vibrator::testing::fillBasicData(&nestedData); |
| 407 | vendorData.putPersistableBundle("test_nested_bundle", nestedData); |
| 408 | |
| 409 | VendorEffect effect; |
| 410 | effect.vendorData = vendorData; |
| 411 | effect.strength = strength; |
| 412 | effect.scale = scale; |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 413 | effect.vendorScale = vendorScale; |
| 414 | scale += 0.5f; |
| 415 | vendorScale += 0.2f; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 416 | |
| 417 | auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {}); |
| 418 | ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback); |
| 419 | |
| 420 | // No expectations on the actual status, the effect might be refused with illegal argument |
| 421 | // or the vendor might return a service-specific error code. |
| 422 | EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION && |
| 423 | status.getStatus() != STATUS_UNKNOWN_TRANSACTION) |
| 424 | << status << "\n For vendor effect with strength" << toString(strength) |
| 425 | << " and scale " << effect.scale; |
| 426 | |
| 427 | if (status.isOk()) { |
| 428 | // Generic vendor data should not trigger vibrations, but if it does trigger one |
| 429 | // then we make sure the vibrator is reset by triggering off(). |
| 430 | EXPECT_OK(vibrator->off()); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | TEST_P(VibratorAidl, PerformVendorEffectStability) { |
| 436 | if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return; |
| 437 | |
| 438 | // Run some iterations of performVendorEffect with randomized vendor data to check basic |
| 439 | // stability of the implementation. |
| 440 | uint8_t iterations = 200; |
| 441 | |
| 442 | for (EffectStrength strength : kEffectStrengths) { |
| 443 | float scale = 0.5f; |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 444 | float vendorScale = 0.2f; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 445 | for (uint8_t i = 0; i < iterations; i++) { |
| 446 | PersistableBundle vendorData; |
| 447 | ::aidl::android::hardware::vibrator::testing::fillRandomData(&vendorData); |
| 448 | |
| 449 | VendorEffect effect; |
| 450 | effect.vendorData = vendorData; |
| 451 | effect.strength = strength; |
| 452 | effect.scale = scale; |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 453 | effect.vendorScale = vendorScale; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 454 | scale *= 2; |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 455 | vendorScale *= 1.5f; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 456 | |
| 457 | auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {}); |
| 458 | ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, callback); |
| 459 | |
| 460 | // No expectations on the actual status, the effect might be refused with illegal |
| 461 | // argument or the vendor might return a service-specific error code. |
| 462 | EXPECT_TRUE(status.getExceptionCode() != EX_UNSUPPORTED_OPERATION && |
| 463 | status.getStatus() != STATUS_UNKNOWN_TRANSACTION) |
| 464 | << status << "\n For random vendor effect with strength " << toString(strength) |
| 465 | << " and scale " << effect.scale; |
| 466 | |
| 467 | if (status.isOk()) { |
| 468 | // Random vendor data should not trigger vibrations, but if it does trigger one |
| 469 | // then we make sure the vibrator is reset by triggering off(). |
| 470 | EXPECT_OK(vibrator->off()); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | TEST_P(VibratorAidl, PerformVendorEffectEmptyVendorData) { |
| 477 | if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return; |
| 478 | |
| 479 | for (EffectStrength strength : kEffectStrengths) { |
| 480 | VendorEffect effect; |
| 481 | effect.strength = strength; |
| 482 | effect.scale = 1.0f; |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 483 | effect.vendorScale = 1.0f; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 484 | |
| 485 | ndk::ScopedAStatus status = vibrator->performVendorEffect(effect, nullptr /*callback*/); |
| 486 | |
| 487 | EXPECT_TRUE(status.getExceptionCode() == EX_SERVICE_SPECIFIC) |
| 488 | << status << "\n For vendor effect with strength " << toString(strength) |
| 489 | << " and scale " << effect.scale; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | TEST_P(VibratorAidl, PerformVendorEffectInvalidScale) { |
| 494 | if ((capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) == 0) return; |
| 495 | |
| 496 | VendorEffect effect; |
| 497 | effect.strength = EffectStrength::MEDIUM; |
| 498 | |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 499 | effect.scale = -1.0f; |
| 500 | effect.vendorScale = 1.0f; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 501 | EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/)); |
| 502 | |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 503 | effect.scale = 1.0f; |
| 504 | effect.vendorScale = -1.0f; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 505 | EXPECT_ILLEGAL_ARGUMENT(vibrator->performVendorEffect(effect, nullptr /*callback*/)); |
| 506 | } |
| 507 | |
| 508 | TEST_P(VibratorAidl, PerformVendorEffectUnsupported) { |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 509 | if (version < VENDOR_EFFECTS_MIN_VERSION) { |
| 510 | EXPECT_EQ(capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS, 0) |
| 511 | << "Vibrator version " << version << " should not report vendor effects capability"; |
| 512 | } |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 513 | if (capabilities & IVibrator::CAP_PERFORM_VENDOR_EFFECTS) return; |
| 514 | |
| 515 | for (EffectStrength strength : kEffectStrengths) { |
| 516 | VendorEffect effect; |
| 517 | effect.strength = strength; |
| 518 | effect.scale = 1.0f; |
Lais Andrade | 4526faf | 2024-08-09 11:57:20 +0100 | [diff] [blame] | 519 | effect.vendorScale = 1.0f; |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 520 | |
| 521 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->performVendorEffect(effect, nullptr /*callback*/)) |
| 522 | << "\n For vendor effect with strength " << toString(strength); |
| 523 | } |
| 524 | } |
| 525 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 526 | TEST_P(VibratorAidl, ChangeVibrationAmplitude) { |
| 527 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 528 | EXPECT_OK(vibrator->setAmplitude(0.1f)); |
| 529 | EXPECT_OK(vibrator->on(2000, nullptr /*callback*/)); |
| 530 | EXPECT_OK(vibrator->setAmplitude(0.5f)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 531 | sleep(1); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 532 | EXPECT_OK(vibrator->setAmplitude(1.0f)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 533 | sleep(1); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 534 | EXPECT_OK(vibrator->off()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) { |
| 539 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 540 | EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(-1)); |
| 541 | EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(0)); |
| 542 | EXPECT_ILLEGAL_ARGUMENT(vibrator->setAmplitude(1.1)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) { |
| 547 | if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 548 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(1)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | TEST_P(VibratorAidl, ChangeVibrationExternalControl) { |
| 553 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 554 | EXPECT_OK(vibrator->setExternalControl(true)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 555 | sleep(1); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 556 | EXPECT_OK(vibrator->setExternalControl(false)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 557 | sleep(1); |
| 558 | } |
| 559 | } |
| 560 | |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 561 | TEST_P(VibratorAidl, ExternalAmplitudeControl) { |
| 562 | const bool supportsExternalAmplitudeControl = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 563 | (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0; |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 564 | |
| 565 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 566 | EXPECT_OK(vibrator->setExternalControl(true)); |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 567 | |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 568 | if (supportsExternalAmplitudeControl) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 569 | EXPECT_OK(vibrator->setAmplitude(0.5)); |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 570 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 571 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setAmplitude(0.5)); |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 572 | } |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 573 | |
| 574 | EXPECT_OK(vibrator->setExternalControl(false)); |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 575 | } else { |
| 576 | EXPECT_FALSE(supportsExternalAmplitudeControl); |
| 577 | } |
| 578 | } |
| 579 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 580 | TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) { |
| 581 | if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 582 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->setExternalControl(true)); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 586 | TEST_P(VibratorAidl, GetSupportedPrimitives) { |
| 587 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
| 588 | std::vector<CompositePrimitive> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 589 | EXPECT_OK(vibrator->getSupportedPrimitives(&supported)); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 590 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 591 | for (CompositePrimitive primitive : kCompositePrimitives) { |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 592 | bool isPrimitiveSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 593 | std::find(supported.begin(), supported.end(), primitive) != supported.end(); |
Lais Andrade | 7e64377 | 2021-07-09 14:59:44 +0100 | [diff] [blame] | 594 | bool isPrimitiveRequired = |
| 595 | std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) != |
| 596 | kRequiredPrimitives.end(); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 597 | |
Lais Andrade | 7e64377 | 2021-07-09 14:59:44 +0100 | [diff] [blame] | 598 | EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 599 | } |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | TEST_P(VibratorAidl, GetPrimitiveDuration) { |
| 604 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 605 | std::vector<CompositePrimitive> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 606 | EXPECT_OK(vibrator->getSupportedPrimitives(&supported)); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 607 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 608 | for (CompositePrimitive primitive : kCompositePrimitives) { |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 609 | bool isPrimitiveSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 610 | std::find(supported.begin(), supported.end(), primitive) != supported.end(); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 611 | int32_t duration; |
| 612 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 613 | if (isPrimitiveSupported) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 614 | EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &duration)) |
| 615 | << "\n For primitive: " << toString(primitive) << " " << duration; |
Lais Andrade | f1b4dd3 | 2021-11-03 16:47:32 +0000 | [diff] [blame] | 616 | if (primitive != CompositePrimitive::NOOP) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 617 | ASSERT_GT(duration, 0) |
| 618 | << "\n For primitive: " << toString(primitive) << " " << duration; |
Lais Andrade | f1b4dd3 | 2021-11-03 16:47:32 +0000 | [diff] [blame] | 619 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 620 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 621 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->getPrimitiveDuration(primitive, &duration)) |
| 622 | << "\n For primitive: " << toString(primitive); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 623 | } |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 628 | TEST_P(VibratorAidl, ComposeValidPrimitives) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 629 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 630 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
| 631 | } |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 632 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 633 | std::vector<CompositePrimitive> supported; |
| 634 | int32_t maxDelay, maxSize; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 635 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 636 | EXPECT_OK(vibrator->getSupportedPrimitives(&supported)); |
| 637 | EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay)); |
| 638 | EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize)); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 639 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 640 | std::vector<CompositeEffect> composite; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 641 | |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 642 | for (int i = 0; i < supported.size(); i++) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 643 | CompositePrimitive primitive = supported[i]; |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 644 | float t = static_cast<float>(i + 1) / supported.size(); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 645 | CompositeEffect effect; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 646 | |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 647 | effect.delayMs = maxDelay * t; |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 648 | effect.primitive = primitive; |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 649 | effect.scale = t; |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 650 | |
| 651 | if (composite.size() == maxSize) { |
| 652 | break; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 653 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 654 | } |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 655 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 656 | if (composite.size() != 0) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 657 | EXPECT_OK(vibrator->compose(composite, nullptr)); |
| 658 | EXPECT_OK(vibrator->off()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 663 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 664 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
| 665 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 666 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 667 | std::vector<CompositePrimitive> unsupported(kInvalidPrimitives); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 668 | std::vector<CompositePrimitive> supported; |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 669 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 670 | EXPECT_OK(vibrator->getSupportedPrimitives(&supported)); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 671 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 672 | for (CompositePrimitive primitive : kCompositePrimitives) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 673 | bool isPrimitiveSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 674 | std::find(supported.begin(), supported.end(), primitive) != supported.end(); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 675 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 676 | if (!isPrimitiveSupported) { |
| 677 | unsupported.push_back(primitive); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 678 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 679 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 680 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 681 | for (CompositePrimitive primitive : unsupported) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 682 | std::vector<CompositeEffect> composite(1); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 683 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 684 | for (CompositeEffect& effect : composite) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 685 | effect.delayMs = 0; |
| 686 | effect.primitive = primitive; |
| 687 | effect.scale = 1.0f; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 688 | } |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 689 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->compose(composite, nullptr)); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 693 | TEST_P(VibratorAidl, ComposeScaleBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 694 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 695 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 696 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 697 | |
| 698 | std::vector<CompositeEffect> composite(1); |
| 699 | CompositeEffect& effect = composite[0]; |
| 700 | |
| 701 | effect.delayMs = 0; |
| 702 | effect.primitive = CompositePrimitive::CLICK; |
| 703 | |
| 704 | effect.scale = std::nextafter(0.0f, -1.0f); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 705 | EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr)); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 706 | |
| 707 | effect.scale = 0.0f; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 708 | EXPECT_OK(vibrator->compose(composite, nullptr)); |
| 709 | EXPECT_OK(vibrator->off()); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 710 | |
| 711 | effect.scale = 1.0f; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 712 | EXPECT_OK(vibrator->compose(composite, nullptr)); |
| 713 | EXPECT_OK(vibrator->off()); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 714 | |
| 715 | effect.scale = std::nextafter(1.0f, 2.0f); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 716 | EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr)); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | TEST_P(VibratorAidl, ComposeDelayBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 720 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 721 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 722 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 723 | |
| 724 | int32_t maxDelay; |
| 725 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 726 | EXPECT_OK(vibrator->getCompositionDelayMax(&maxDelay)); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 727 | |
| 728 | std::vector<CompositeEffect> composite(1); |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 729 | CompositeEffect& effect = composite[0]; |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 730 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 731 | effect.primitive = CompositePrimitive::CLICK; |
| 732 | effect.scale = 1.0f; |
| 733 | |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 734 | effect.delayMs = 0; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 735 | EXPECT_OK(vibrator->compose(composite, nullptr)); |
| 736 | EXPECT_OK(vibrator->off()); |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 737 | |
| 738 | effect.delayMs = 1; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 739 | EXPECT_OK(vibrator->compose(composite, nullptr)); |
| 740 | EXPECT_OK(vibrator->off()); |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 741 | |
| 742 | effect.delayMs = maxDelay; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 743 | EXPECT_OK(vibrator->compose(composite, nullptr)); |
| 744 | EXPECT_OK(vibrator->off()); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 745 | |
| 746 | effect.delayMs = maxDelay + 1; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 747 | EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr)); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 748 | } |
| 749 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 750 | TEST_P(VibratorAidl, ComposeSizeBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 751 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 752 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 753 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 754 | |
| 755 | int32_t maxSize; |
| 756 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 757 | EXPECT_OK(vibrator->getCompositionSizeMax(&maxSize)); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 758 | |
| 759 | std::vector<CompositeEffect> composite(maxSize); |
| 760 | CompositeEffect effect; |
| 761 | |
| 762 | effect.delayMs = 1; |
| 763 | effect.primitive = CompositePrimitive::CLICK; |
| 764 | effect.scale = 1.0f; |
| 765 | |
| 766 | std::fill(composite.begin(), composite.end(), effect); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 767 | EXPECT_OK(vibrator->compose(composite, nullptr)); |
| 768 | EXPECT_OK(vibrator->off()); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 769 | |
| 770 | composite.emplace_back(effect); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 771 | EXPECT_ILLEGAL_ARGUMENT(vibrator->compose(composite, nullptr)); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 772 | } |
| 773 | |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 774 | TEST_P(VibratorAidl, ComposeCallback) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 775 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 776 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
| 777 | } |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 778 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 779 | std::vector<CompositePrimitive> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 780 | EXPECT_OK(vibrator->getSupportedPrimitives(&supported)); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 781 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 782 | for (CompositePrimitive primitive : supported) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 783 | if (primitive == CompositePrimitive::NOOP) { |
| 784 | continue; |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 785 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 786 | |
| 787 | std::promise<void> completionPromise; |
| 788 | std::future<void> completionFuture{completionPromise.get_future()}; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 789 | auto callback = ndk::SharedRefBase::make<CompletionCallback>( |
| 790 | [&completionPromise] { completionPromise.set_value(); }); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 791 | CompositeEffect effect; |
| 792 | std::vector<CompositeEffect> composite; |
| 793 | int32_t durationMs; |
| 794 | std::chrono::milliseconds duration; |
| 795 | std::chrono::time_point<high_resolution_clock> start, end; |
| 796 | std::chrono::milliseconds elapsed; |
| 797 | |
| 798 | effect.delayMs = 0; |
| 799 | effect.primitive = primitive; |
| 800 | effect.scale = 1.0f; |
| 801 | composite.emplace_back(effect); |
| 802 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 803 | EXPECT_OK(vibrator->getPrimitiveDuration(primitive, &durationMs)) |
| 804 | << "\n For primitive: " << toString(primitive); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 805 | duration = std::chrono::milliseconds(durationMs); |
| 806 | |
| 807 | start = high_resolution_clock::now(); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 808 | EXPECT_OK(vibrator->compose(composite, callback)) |
| 809 | << "\n For primitive: " << toString(primitive); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 810 | |
Lais Andrade | c689ba5 | 2024-04-10 11:07:11 +0100 | [diff] [blame] | 811 | EXPECT_EQ(completionFuture.wait_for(duration + VIBRATION_CALLBACK_TIMEOUT), |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 812 | std::future_status::ready) |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 813 | << "\n For primitive: " << toString(primitive); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 814 | end = high_resolution_clock::now(); |
| 815 | |
| 816 | elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 817 | EXPECT_GE(elapsed.count(), duration.count()) |
| 818 | << "\n For primitive: " << toString(primitive); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 819 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 820 | EXPECT_OK(vibrator->off()) << "\n For primitive: " << toString(primitive); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 824 | TEST_P(VibratorAidl, AlwaysOn) { |
| 825 | if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) { |
| 826 | std::vector<Effect> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 827 | EXPECT_OK(vibrator->getSupportedAlwaysOnEffects(&supported)); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 828 | |
| 829 | for (Effect effect : kEffects) { |
| 830 | bool isEffectSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 831 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 832 | |
| 833 | for (EffectStrength strength : kEffectStrengths) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 834 | ndk::ScopedAStatus status = vibrator->alwaysOnEnable(0, effect, strength); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 835 | |
| 836 | if (isEffectSupported) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 837 | EXPECT_OK(std::move(status)) |
| 838 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 839 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 840 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)) |
| 841 | << "\n For effect: " << toString(effect) << " " << toString(strength); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 846 | EXPECT_OK(vibrator->alwaysOnDisable(0)); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 850 | TEST_P(VibratorAidl, GetResonantFrequency) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 851 | getResonantFrequencyHz(vibrator, capabilities); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | TEST_P(VibratorAidl, GetQFactor) { |
| 855 | float qFactor; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 856 | ndk::ScopedAStatus status = vibrator->getQFactor(&qFactor); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 857 | if (capabilities & IVibrator::CAP_GET_Q_FACTOR) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 858 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 859 | ASSERT_GT(qFactor, 0); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 860 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 861 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 865 | TEST_P(VibratorAidl, GetFrequencyResolution) { |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 866 | getFrequencyResolutionHz(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | TEST_P(VibratorAidl, GetFrequencyMinimum) { |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 870 | getFrequencyMinimumHz(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) { |
| 874 | std::vector<float> bandwidthAmplitudeMap; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 875 | ndk::ScopedAStatus status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap); |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 876 | |
| 877 | if (shouldValidateLegacyFrequencyControlResult(capabilities, version, status)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 878 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 879 | ASSERT_FALSE(bandwidthAmplitudeMap.empty()); |
| 880 | |
| 881 | int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) - |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 882 | getFrequencyMinimumHz(vibrator, capabilities, version)) / |
| 883 | getFrequencyResolutionHz(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 884 | ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize); |
| 885 | |
| 886 | for (float e : bandwidthAmplitudeMap) { |
| 887 | ASSERT_GE(e, 0.0); |
| 888 | ASSERT_LE(e, 1.0); |
| 889 | } |
| 890 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 891 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
| 895 | TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) { |
| 896 | int32_t durationMs; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 897 | ndk::ScopedAStatus status = vibrator->getPwlePrimitiveDurationMax(&durationMs); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 898 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 899 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 900 | ASSERT_NE(durationMs, 0); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 901 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 902 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
| 906 | TEST_P(VibratorAidl, GetPwleCompositionSizeMax) { |
| 907 | int32_t maxSize; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 908 | ndk::ScopedAStatus status = vibrator->getPwleCompositionSizeMax(&maxSize); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 909 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 910 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 911 | ASSERT_NE(maxSize, 0); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 912 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 913 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | |
| 917 | TEST_P(VibratorAidl, GetSupportedBraking) { |
| 918 | std::vector<Braking> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 919 | ndk::ScopedAStatus status = vibrator->getSupportedBraking(&supported); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 920 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 921 | bool isDefaultNoneSupported = |
| 922 | std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end(); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 923 | EXPECT_OK(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 924 | ASSERT_TRUE(isDefaultNoneSupported); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 925 | } else { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 926 | EXPECT_UNKNOWN_OR_UNSUPPORTED(std::move(status)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
| 930 | TEST_P(VibratorAidl, ComposeValidPwle) { |
| 931 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 932 | ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 933 | |
| 934 | std::vector<Braking> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 935 | EXPECT_OK(vibrator->getSupportedBraking(&supported)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 936 | bool isClabSupported = |
| 937 | std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end(); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 938 | BrakingPwle firstBraking; |
| 939 | firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE; |
| 940 | firstBraking.duration = 100; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 941 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 942 | ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities, version); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 943 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 944 | float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities, version); |
| 945 | float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities, version); |
| 946 | float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities, version); |
| 947 | // As of API 16 these APIs are deprecated and no longer required to be implemented |
| 948 | // with frequency control capability. |
| 949 | if (minFrequencyHz >= 0 && maxFrequencyHz >= 0 && freqResolutionHz >= 0) { |
| 950 | secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f); |
| 951 | secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f); |
| 952 | } |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 953 | } |
| 954 | BrakingPwle secondBraking; |
| 955 | secondBraking.braking = Braking::NONE; |
| 956 | secondBraking.duration = 10; |
| 957 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 958 | std::vector<PrimitivePwle> pwleQueue = {firstActive, firstBraking, secondActive, |
| 959 | secondBraking}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 960 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 961 | EXPECT_OK(vibrator->composePwle(pwleQueue, nullptr)); |
| 962 | EXPECT_OK(vibrator->off()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
| 966 | TEST_P(VibratorAidl, ComposeValidPwleWithCallback) { |
| 967 | if (!((capabilities & IVibrator::CAP_ON_CALLBACK) && |
| 968 | (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS))) |
| 969 | return; |
| 970 | |
| 971 | std::promise<void> completionPromise; |
| 972 | std::future<void> completionFuture{completionPromise.get_future()}; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 973 | auto callback = ndk::SharedRefBase::make<CompletionCallback>( |
| 974 | [&completionPromise] { completionPromise.set_value(); }); |
chasewu | 22cb901 | 2022-03-31 23:23:27 +0800 | [diff] [blame] | 975 | int32_t segmentDurationMaxMs; |
| 976 | vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs); |
| 977 | uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below |
Lais Andrade | c689ba5 | 2024-04-10 11:07:11 +0100 | [diff] [blame] | 978 | auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 979 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 980 | ActivePwle active = composeValidActivePwle(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 981 | |
| 982 | std::vector<Braking> supported; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 983 | EXPECT_OK(vibrator->getSupportedBraking(&supported)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 984 | bool isClabSupported = |
| 985 | std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end(); |
| 986 | BrakingPwle braking; |
| 987 | braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE; |
| 988 | braking.duration = 100; |
| 989 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 990 | std::vector<PrimitivePwle> pwleQueue = {active, braking, active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 991 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 992 | EXPECT_OK(vibrator->composePwle(pwleQueue, callback)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 993 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 994 | EXPECT_OK(vibrator->off()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | TEST_P(VibratorAidl, ComposePwleSegmentBoundary) { |
| 998 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 999 | std::vector<PrimitivePwle> pwleQueue; |
| 1000 | // test empty queue |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1001 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1002 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1003 | ActivePwle active = composeValidActivePwle(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1004 | |
| 1005 | PrimitivePwle pwle; |
| 1006 | pwle = active; |
| 1007 | int segmentCountMax; |
| 1008 | vibrator->getPwleCompositionSizeMax(&segmentCountMax); |
| 1009 | |
| 1010 | // Create PWLE queue with more segments than allowed |
| 1011 | for (int i = 0; i < segmentCountMax + 10; i++) { |
| 1012 | pwleQueue.emplace_back(std::move(pwle)); |
| 1013 | } |
| 1014 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1015 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) { |
| 1020 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1021 | ActivePwle active = composeValidActivePwle(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1022 | active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed |
| 1023 | active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed |
| 1024 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1025 | std::vector<PrimitivePwle> pwleQueueGreater = {active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1026 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1027 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1028 | |
| 1029 | active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed |
| 1030 | active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed |
| 1031 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1032 | std::vector<PrimitivePwle> pwleQueueLess = {active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1033 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1034 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) { |
| 1039 | if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) && |
| 1040 | (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) { |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1041 | float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities, version); |
| 1042 | float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities, version); |
| 1043 | float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1044 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1045 | // As of API 16 these APIs are deprecated and no longer required to be implemented with |
| 1046 | // frequency control capability. |
| 1047 | if (freqMinimumHz < 0 || freqMaximumHz < 0 || freqResolutionHz < 0) { |
| 1048 | GTEST_SKIP() << "PWLE V1 is not supported, skipping test"; |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | ActivePwle active = composeValidActivePwle(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1053 | active.startFrequency = |
| 1054 | freqMaximumHz + freqResolutionHz; // Frequency greater than allowed |
| 1055 | active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed |
| 1056 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1057 | std::vector<PrimitivePwle> pwleQueueGreater = {active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1058 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1059 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueGreater, nullptr)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1060 | |
| 1061 | active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed |
| 1062 | active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed |
| 1063 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1064 | std::vector<PrimitivePwle> pwleQueueLess = {active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1065 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1066 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueueLess, nullptr)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) { |
| 1071 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1072 | ActivePwle active = composeValidActivePwle(vibrator, capabilities, version); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1073 | |
chasewu | 22cb901 | 2022-03-31 23:23:27 +0800 | [diff] [blame] | 1074 | int32_t segmentDurationMaxMs; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1075 | vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs); |
| 1076 | active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed |
| 1077 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1078 | std::vector<PrimitivePwle> pwleQueue = {active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1079 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1080 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwle(pwleQueue, nullptr)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1084 | TEST_P(VibratorAidl, FrequencyToOutputAccelerationMapHasValidFrequencyRange) { |
| 1085 | if (version < PWLE_V2_MIN_VERSION || !(capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) { |
| 1086 | GTEST_SKIP() << "Frequency control is not supported, skipping test"; |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1087 | return; |
| 1088 | } |
| 1089 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1090 | std::vector<FrequencyAccelerationMapEntry> frequencyToOutputAccelerationMap; |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1091 | ndk::ScopedAStatus status = |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1092 | vibrator->getFrequencyToOutputAccelerationMap(&frequencyToOutputAccelerationMap); |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1093 | EXPECT_OK(std::move(status)); |
| 1094 | ASSERT_FALSE(frequencyToOutputAccelerationMap.empty()); |
| 1095 | auto sharpnessRange = |
| 1096 | pwle_v2_utils::getPwleV2SharpnessRange(vibrator, frequencyToOutputAccelerationMap); |
| 1097 | // Validate the curve provides a usable sharpness range, which is a range of frequencies |
| 1098 | // that are supported by the device. |
| 1099 | ASSERT_TRUE(sharpnessRange.first >= 0); |
| 1100 | // Validate that the sharpness range is a valid interval, not a single point. |
| 1101 | ASSERT_TRUE(sharpnessRange.first < sharpnessRange.second); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1104 | TEST_P(VibratorAidl, FrequencyToOutputAccelerationMapUnsupported) { |
| 1105 | if ((capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) return; |
| 1106 | |
| 1107 | std::vector<FrequencyAccelerationMapEntry> frequencyToOutputAccelerationMap; |
| 1108 | |
| 1109 | EXPECT_UNKNOWN_OR_UNSUPPORTED( |
| 1110 | vibrator->getFrequencyToOutputAccelerationMap(&frequencyToOutputAccelerationMap)); |
| 1111 | } |
| 1112 | |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1113 | TEST_P(VibratorAidl, GetPwleV2PrimitiveDurationMaxMillis) { |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1114 | if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) { |
| 1115 | GTEST_SKIP() << "PWLE V2 not supported, skipping test"; |
| 1116 | return; |
| 1117 | } |
| 1118 | |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1119 | int32_t durationMs; |
| 1120 | ndk::ScopedAStatus status = vibrator->getPwleV2PrimitiveDurationMaxMillis(&durationMs); |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1121 | EXPECT_OK(std::move(status)); |
| 1122 | ASSERT_GT(durationMs, 0); // Ensure greater than zero |
| 1123 | ASSERT_GE(durationMs, pwle_v2_utils::COMPOSE_PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | TEST_P(VibratorAidl, GetPwleV2CompositionSizeMax) { |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1127 | if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) { |
| 1128 | GTEST_SKIP() << "PWLE V2 not supported, skipping test"; |
| 1129 | return; |
| 1130 | } |
| 1131 | |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1132 | int32_t maxSize; |
| 1133 | ndk::ScopedAStatus status = vibrator->getPwleV2CompositionSizeMax(&maxSize); |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1134 | EXPECT_OK(std::move(status)); |
| 1135 | ASSERT_GT(maxSize, 0); // Ensure greater than zero |
| 1136 | ASSERT_GE(maxSize, pwle_v2_utils::COMPOSE_PWLE_V2_MIN_REQUIRED_SIZE); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | TEST_P(VibratorAidl, GetPwleV2PrimitiveDurationMinMillis) { |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1140 | if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) { |
| 1141 | GTEST_SKIP() << "PWLE V2 not supported, skipping test"; |
| 1142 | return; |
| 1143 | } |
| 1144 | |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1145 | int32_t durationMs; |
| 1146 | ndk::ScopedAStatus status = vibrator->getPwleV2PrimitiveDurationMinMillis(&durationMs); |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1147 | EXPECT_OK(std::move(status)); |
| 1148 | ASSERT_GT(durationMs, 0); // Ensure greater than zero |
| 1149 | ASSERT_LE(durationMs, pwle_v2_utils::COMPOSE_PWLE_V2_MAX_ALLOWED_PRIMITIVE_MIN_DURATION_MS); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1152 | TEST_P(VibratorAidl, ValidatePwleV2DependencyOnFrequencyControl) { |
| 1153 | if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) { |
| 1154 | GTEST_SKIP() << "PWLE V2 not supported, skipping test"; |
| 1155 | return; |
| 1156 | } |
| 1157 | |
| 1158 | // Check if frequency control is supported |
| 1159 | bool hasFrequencyControl = (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) != 0; |
| 1160 | ASSERT_TRUE(hasFrequencyControl) << "Frequency control MUST be supported when PWLE V2 is."; |
| 1161 | } |
| 1162 | |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1163 | TEST_P(VibratorAidl, ComposeValidPwleV2Effect) { |
| 1164 | if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) { |
| 1165 | GTEST_SKIP() << "PWLE V2 not supported, skipping test"; |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | EXPECT_OK(vibrator->composePwleV2(pwle_v2_utils::composeValidPwleV2Effect(vibrator), nullptr)); |
| 1170 | EXPECT_OK(vibrator->off()); |
| 1171 | } |
| 1172 | |
Ahmad Khalil | 7eacaad | 2024-08-22 12:25:36 +0000 | [diff] [blame] | 1173 | TEST_P(VibratorAidl, ComposePwleV2Unsupported) { |
| 1174 | if (version < PWLE_V2_MIN_VERSION) { |
| 1175 | EXPECT_EQ(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2, 0) |
| 1176 | << "Vibrator version " << version << " should not report PWLE V2 capability."; |
| 1177 | } |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1178 | if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) return; |
Ahmad Khalil | 7eacaad | 2024-08-22 12:25:36 +0000 | [diff] [blame] | 1179 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1180 | CompositePwleV2 composite; |
| 1181 | composite.pwlePrimitives.emplace_back(/*amplitude=*/1.0f, /*frequencyHz=*/100.0f, |
| 1182 | /*timeMillis=*/50); |
Ahmad Khalil | d73dbc6 | 2024-08-27 20:08:08 +0000 | [diff] [blame] | 1183 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1184 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->composePwleV2(composite, nullptr)); |
Ahmad Khalil | 7eacaad | 2024-08-22 12:25:36 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1187 | TEST_P(VibratorAidl, ComposeValidPwleV2EffectWithCallback) { |
| 1188 | if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) { |
| 1189 | GTEST_SKIP() << "PWLE V2 not supported, skipping test"; |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | std::promise<void> completionPromise; |
| 1194 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 1195 | auto callback = ndk::SharedRefBase::make<CompletionCallback>( |
| 1196 | [&completionPromise] { completionPromise.set_value(); }); |
| 1197 | |
| 1198 | int32_t minDuration; |
| 1199 | EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMinMillis(&minDuration)); |
| 1200 | auto timeout = std::chrono::milliseconds(minDuration) + VIBRATION_CALLBACK_TIMEOUT; |
| 1201 | float minFrequency = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator); |
| 1202 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1203 | CompositePwleV2 composite; |
| 1204 | composite.pwlePrimitives.emplace_back(/*amplitude=*/0.5, minFrequency, minDuration); |
| 1205 | |
| 1206 | EXPECT_OK(vibrator->composePwleV2(composite, callback)); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1207 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 1208 | EXPECT_OK(vibrator->off()); |
| 1209 | } |
| 1210 | |
| 1211 | TEST_P(VibratorAidl, composePwleV2EffectWithTooManyPoints) { |
| 1212 | if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) { |
| 1213 | GTEST_SKIP() << "PWLE V2 not supported, skipping test"; |
| 1214 | return; |
| 1215 | } |
| 1216 | |
| 1217 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2( |
| 1218 | pwle_v2_utils::composePwleV2EffectWithTooManyPoints(vibrator), nullptr)); |
| 1219 | } |
| 1220 | |
| 1221 | TEST_P(VibratorAidl, composeInvalidPwleV2Effect) { |
| 1222 | if (!(capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS_V2)) { |
| 1223 | GTEST_SKIP() << "PWLE V2 not supported, skipping test"; |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | // Retrieve min and max durations |
| 1228 | int32_t minDurationMs, maxDurationMs; |
| 1229 | EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMinMillis(&minDurationMs)); |
| 1230 | EXPECT_OK(vibrator->getPwleV2PrimitiveDurationMaxMillis(&maxDurationMs)); |
| 1231 | |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1232 | CompositePwleV2 composePwle; |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Negative amplitude |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1235 | composePwle.pwlePrimitives.push_back( |
| 1236 | PwleV2Primitive(/*amplitude=*/-0.8f, /*frequency=*/100, minDurationMs)); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1237 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr)) |
| 1238 | << "Composing PWLE V2 effect with negative amplitude should fail"; |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1239 | composePwle.pwlePrimitives.clear(); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Amplitude exceeding 1.0 |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1242 | composePwle.pwlePrimitives.push_back( |
| 1243 | PwleV2Primitive(/*amplitude=*/1.2f, /*frequency=*/100, minDurationMs)); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1244 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr)) |
| 1245 | << "Composing PWLE V2 effect with amplitude greater than 1.0 should fail"; |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1246 | composePwle.pwlePrimitives.clear(); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1247 | |
| 1248 | // Duration exceeding maximum |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1249 | composePwle.pwlePrimitives.push_back( |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1250 | PwleV2Primitive(/*amplitude=*/0.2f, /*frequency=*/100, maxDurationMs + 10)); |
| 1251 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr)) |
| 1252 | << "Composing PWLE V2 effect with duration exceeding maximum should fail"; |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1253 | composePwle.pwlePrimitives.clear(); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Negative duration |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1256 | composePwle.pwlePrimitives.push_back( |
| 1257 | PwleV2Primitive(/*amplitude=*/0.2f, /*frequency=*/100, /*time=*/-1)); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1258 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr)) |
| 1259 | << "Composing PWLE V2 effect with negative duration should fail"; |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1260 | composePwle.pwlePrimitives.clear(); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Frequency below minimum |
| 1263 | float minFrequency = pwle_v2_utils::getPwleV2FrequencyMinHz(vibrator); |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1264 | composePwle.pwlePrimitives.push_back( |
| 1265 | PwleV2Primitive(/*amplitude=*/0.2f, minFrequency - 1, minDurationMs)); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1266 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr)) |
| 1267 | << "Composing PWLE V2 effect with frequency below minimum should fail"; |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1268 | composePwle.pwlePrimitives.clear(); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1269 | |
| 1270 | // Frequency above maximum |
| 1271 | float maxFrequency = pwle_v2_utils::getPwleV2FrequencyMaxHz(vibrator); |
Ahmad Khalil | bb669e1 | 2024-09-28 20:08:14 +0000 | [diff] [blame] | 1272 | composePwle.pwlePrimitives.push_back( |
| 1273 | PwleV2Primitive(/*amplitude=*/0.2f, maxFrequency + 1, minDurationMs)); |
Ahmad Khalil | a788bed | 2024-08-05 14:43:38 +0000 | [diff] [blame] | 1274 | EXPECT_ILLEGAL_ARGUMENT(vibrator->composePwleV2(composePwle, nullptr)) |
| 1275 | << "Composing PWLE V2 effect with frequency above maximum should fail"; |
| 1276 | } |
| 1277 | |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 1278 | std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() { |
| 1279 | std::vector<std::tuple<int32_t, int32_t>> tuples; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 1280 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1281 | std::vector<std::string> managerNames = findVibratorManagerNames(); |
| 1282 | std::vector<int32_t> vibratorIds; |
| 1283 | for (int i = 0; i < managerNames.size(); i++) { |
| 1284 | auto vibratorManager = IVibratorManager::fromBinder( |
| 1285 | ndk::SpAIBinder(AServiceManager_waitForService(managerNames[i].c_str()))); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 1286 | if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1287 | for (int32_t vibratorId : vibratorIds) { |
| 1288 | tuples.emplace_back(i, vibratorId); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1293 | std::vector<std::string> vibratorNames = findUnmanagedVibratorNames(); |
| 1294 | for (int i = 0; i < vibratorNames.size(); i++) { |
| 1295 | tuples.emplace_back(-1, i); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | return tuples; |
| 1299 | } |
| 1300 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1301 | std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) { |
| 1302 | const auto &[managerIdx, vibratorId] = info.param; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 1303 | if (managerIdx < 0) { |
| 1304 | return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId); |
| 1305 | } |
| 1306 | return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" + |
| 1307 | std::to_string(vibratorId); |
| 1308 | } |
| 1309 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 1310 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 1311 | INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()), |
| 1312 | PrintGeneratedTest); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 1313 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 1314 | int main(int argc, char **argv) { |
Lais Andrade | a3c332f | 2024-06-20 10:46:06 +0100 | [diff] [blame] | 1315 | // Random values are used in the implementation. |
| 1316 | std::srand(std::time(nullptr)); |
| 1317 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 1318 | ::testing::InitGoogleTest(&argc, argv); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 1319 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 1320 | ABinderProcess_startThreadPool(); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 1321 | return RUN_ALL_TESTS(); |
| 1322 | } |