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