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