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> |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 18 | #include <android/hardware/vibrator/BnVibratorCallback.h> |
| 19 | #include <android/hardware/vibrator/IVibrator.h> |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 20 | #include <android/hardware/vibrator/IVibratorManager.h> |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/ProcessState.h> |
| 23 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 24 | #include <cmath> |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 25 | #include <future> |
| 26 | |
| 27 | using android::ProcessState; |
| 28 | using android::sp; |
| 29 | using android::String16; |
| 30 | using android::binder::Status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 31 | using android::hardware::vibrator::ActivePwle; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 32 | using android::hardware::vibrator::BnVibratorCallback; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 33 | using android::hardware::vibrator::Braking; |
| 34 | using android::hardware::vibrator::BrakingPwle; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 35 | using android::hardware::vibrator::CompositeEffect; |
| 36 | using android::hardware::vibrator::CompositePrimitive; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 37 | using android::hardware::vibrator::Effect; |
| 38 | using android::hardware::vibrator::EffectStrength; |
| 39 | using android::hardware::vibrator::IVibrator; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 40 | using android::hardware::vibrator::IVibratorManager; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 41 | using android::hardware::vibrator::PrimitivePwle; |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 42 | using std::chrono::high_resolution_clock; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 43 | |
Jooyung Han | 716648d | 2019-12-17 14:17:48 +0000 | [diff] [blame] | 44 | const std::vector<Effect> kEffects{android::enum_range<Effect>().begin(), |
| 45 | android::enum_range<Effect>().end()}; |
| 46 | const std::vector<EffectStrength> kEffectStrengths{android::enum_range<EffectStrength>().begin(), |
| 47 | android::enum_range<EffectStrength>().end()}; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 48 | |
| 49 | const std::vector<Effect> kInvalidEffects = { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 50 | static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1), |
| 51 | static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1), |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | const std::vector<EffectStrength> kInvalidEffectStrengths = { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 55 | static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1), |
| 56 | static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1), |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Steven Moreland | 1c26978 | 2020-01-09 11:16:05 -0800 | [diff] [blame] | 59 | const std::vector<CompositePrimitive> kCompositePrimitives{ |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 60 | android::enum_range<CompositePrimitive>().begin(), |
| 61 | android::enum_range<CompositePrimitive>().end()}; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 62 | |
Lais Andrade | 7e64377 | 2021-07-09 14:59:44 +0100 | [diff] [blame] | 63 | const std::vector<CompositePrimitive> kRequiredPrimitives = { |
| 64 | CompositePrimitive::CLICK, CompositePrimitive::LIGHT_TICK, |
| 65 | CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE, |
| 66 | CompositePrimitive::QUICK_FALL, |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 67 | }; |
| 68 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 69 | const std::vector<CompositePrimitive> kInvalidPrimitives = { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 70 | static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1), |
| 71 | static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1), |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 72 | }; |
| 73 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 74 | class CompletionCallback : public BnVibratorCallback { |
| 75 | public: |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 76 | CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {} |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 77 | Status onComplete() override { |
| 78 | mCallback(); |
| 79 | return Status::ok(); |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | std::function<void()> mCallback; |
| 84 | }; |
| 85 | |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 86 | class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 87 | public: |
| 88 | virtual void SetUp() override { |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 89 | int32_t managerIdx = std::get<0>(GetParam()); |
| 90 | int32_t vibratorId = std::get<1>(GetParam()); |
| 91 | auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor); |
| 92 | |
| 93 | if (managerIdx < 0) { |
| 94 | // Testing a unmanaged vibrator, using vibratorId as index from registered HALs |
| 95 | auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor); |
| 96 | ASSERT_LT(vibratorId, vibratorAidlNames.size()); |
| 97 | auto vibratorName = String16(vibratorAidlNames[vibratorId].c_str()); |
| 98 | vibrator = android::waitForDeclaredService<IVibrator>(vibratorName); |
| 99 | } else { |
| 100 | // Testing a managed vibrator, using vibratorId to retrieve it from the manager |
| 101 | ASSERT_LT(managerIdx, managerAidlNames.size()); |
| 102 | auto managerName = String16(managerAidlNames[managerIdx].c_str()); |
| 103 | auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName); |
| 104 | auto vibratorResult = vibratorManager->getVibrator(vibratorId, &vibrator); |
| 105 | ASSERT_TRUE(vibratorResult.isOk()); |
| 106 | } |
| 107 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 108 | ASSERT_NE(vibrator, nullptr); |
| 109 | ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk()); |
| 110 | } |
| 111 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 112 | virtual void TearDown() override { |
| 113 | // Reset vibrator state between tests. |
| 114 | EXPECT_TRUE(vibrator->off().isOk()); |
| 115 | } |
| 116 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 117 | sp<IVibrator> vibrator; |
| 118 | int32_t capabilities; |
| 119 | }; |
| 120 | |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 121 | inline bool isUnknownOrUnsupported(Status status) { |
| 122 | return status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 123 | status.transactionError() == android::UNKNOWN_TRANSACTION; |
| 124 | } |
| 125 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 126 | static float getResonantFrequencyHz(sp<IVibrator> vibrator, int32_t capabilities) { |
| 127 | float resonantFrequencyHz; |
| 128 | Status status = vibrator->getResonantFrequency(&resonantFrequencyHz); |
| 129 | if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) { |
| 130 | EXPECT_GT(resonantFrequencyHz, 0); |
| 131 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 132 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 133 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 134 | } |
| 135 | return resonantFrequencyHz; |
| 136 | } |
| 137 | |
| 138 | static float getFrequencyResolutionHz(sp<IVibrator> vibrator, int32_t capabilities) { |
| 139 | float freqResolutionHz; |
| 140 | Status status = vibrator->getFrequencyResolution(&freqResolutionHz); |
| 141 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 142 | EXPECT_GT(freqResolutionHz, 0); |
| 143 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 144 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 145 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 146 | } |
| 147 | return freqResolutionHz; |
| 148 | } |
| 149 | |
| 150 | static float getFrequencyMinimumHz(sp<IVibrator> vibrator, int32_t capabilities) { |
| 151 | float freqMinimumHz; |
| 152 | Status status = vibrator->getFrequencyMinimum(&freqMinimumHz); |
| 153 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 154 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 155 | |
| 156 | float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities); |
| 157 | |
| 158 | EXPECT_GT(freqMinimumHz, 0); |
| 159 | EXPECT_LE(freqMinimumHz, resonantFrequencyHz); |
| 160 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 161 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 162 | } |
| 163 | return freqMinimumHz; |
| 164 | } |
| 165 | |
| 166 | static float getFrequencyMaximumHz(sp<IVibrator> vibrator, int32_t capabilities) { |
| 167 | std::vector<float> bandwidthAmplitudeMap; |
| 168 | Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap); |
| 169 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 170 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 171 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 172 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 173 | } |
| 174 | |
chasewu | 70da3cc | 2022-03-15 15:16:04 +0800 | [diff] [blame] | 175 | float freqMaximumHz = ((bandwidthAmplitudeMap.size() - 1) * |
| 176 | getFrequencyResolutionHz(vibrator, capabilities)) + |
| 177 | getFrequencyMinimumHz(vibrator, capabilities); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 178 | return freqMaximumHz; |
| 179 | } |
| 180 | |
| 181 | static float getAmplitudeMin() { |
| 182 | return 0.0; |
| 183 | } |
| 184 | |
| 185 | static float getAmplitudeMax() { |
| 186 | return 1.0; |
| 187 | } |
| 188 | |
| 189 | static ActivePwle composeValidActivePwle(sp<IVibrator> vibrator, int32_t capabilities) { |
| 190 | float frequencyHz; |
| 191 | if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) { |
| 192 | frequencyHz = getResonantFrequencyHz(vibrator, capabilities); |
| 193 | } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 194 | frequencyHz = getFrequencyMinimumHz(vibrator, capabilities); |
| 195 | } else { |
| 196 | frequencyHz = 150.0; // default value commonly used |
| 197 | } |
| 198 | |
| 199 | ActivePwle active; |
| 200 | active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2; |
| 201 | active.startFrequency = frequencyHz; |
| 202 | active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2; |
| 203 | active.endFrequency = frequencyHz; |
chasewu | 22cb901 | 2022-03-31 23:23:27 +0800 | [diff] [blame] | 204 | vibrator->getPwlePrimitiveDurationMax(&(active.duration)); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 205 | |
| 206 | return active; |
| 207 | } |
| 208 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 209 | TEST_P(VibratorAidl, OnThenOffBeforeTimeout) { |
| 210 | EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk()); |
| 211 | sleep(1); |
| 212 | EXPECT_TRUE(vibrator->off().isOk()); |
| 213 | } |
| 214 | |
| 215 | TEST_P(VibratorAidl, OnWithCallback) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 216 | if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) |
| 217 | return; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 218 | |
| 219 | std::promise<void> completionPromise; |
| 220 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 221 | sp<CompletionCallback> callback = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 222 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 223 | uint32_t durationMs = 250; |
| 224 | std::chrono::milliseconds timeout{durationMs * 2}; |
| 225 | EXPECT_TRUE(vibrator->on(durationMs, callback).isOk()); |
| 226 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 227 | EXPECT_TRUE(vibrator->off().isOk()); |
| 228 | } |
| 229 | |
| 230 | TEST_P(VibratorAidl, OnCallbackNotSupported) { |
Fenglin Wu | 15b01dc | 2020-11-23 10:03:10 +0800 | [diff] [blame] | 231 | if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 232 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 233 | Status status = vibrator->on(250, callback); |
| 234 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | TEST_P(VibratorAidl, ValidateEffect) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 239 | std::vector<Effect> supported; |
| 240 | ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); |
| 241 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 242 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 243 | bool isEffectSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 244 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 245 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 246 | for (EffectStrength strength : kEffectStrengths) { |
| 247 | int32_t lengthMs = 0; |
| 248 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 249 | |
| 250 | if (isEffectSupported) { |
Harpreet \"Eli\" Sangha | e1723a4 | 2019-12-06 14:09:33 +0900 | [diff] [blame] | 251 | EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 252 | EXPECT_GT(lengthMs, 0); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 253 | usleep(lengthMs * 1000); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 254 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 255 | EXPECT_TRUE(isUnknownOrUnsupported(status)) |
| 256 | << status << " " << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | TEST_P(VibratorAidl, ValidateEffectWithCallback) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 263 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) |
| 264 | return; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 265 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 266 | std::vector<Effect> supported; |
| 267 | ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); |
| 268 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 269 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 270 | bool isEffectSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 271 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 272 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 273 | for (EffectStrength strength : kEffectStrengths) { |
| 274 | std::promise<void> completionPromise; |
| 275 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 276 | sp<CompletionCallback> callback = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 277 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 278 | int lengthMs = 0; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 279 | Status status = vibrator->perform(effect, strength, callback, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 280 | |
| 281 | if (isEffectSupported) { |
| 282 | EXPECT_TRUE(status.isOk()); |
| 283 | EXPECT_GT(lengthMs, 0); |
| 284 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 285 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 288 | if (!status.isOk()) |
| 289 | continue; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 290 | |
Vince Leung | 7b8606e | 2021-05-04 13:56:48 -0700 | [diff] [blame] | 291 | //TODO(b/187207798): revert back to conservative timeout values once |
| 292 | //latencies have been fixed |
| 293 | std::chrono::milliseconds timeout{lengthMs * 8}; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 294 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 300 | if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) |
| 301 | return; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 302 | |
| 303 | for (Effect effect : kEffects) { |
| 304 | for (EffectStrength strength : kEffectStrengths) { |
| 305 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
| 306 | int lengthMs; |
| 307 | Status status = vibrator->perform(effect, strength, callback, &lengthMs); |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 308 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | TEST_P(VibratorAidl, InvalidEffectsUnsupported) { |
| 314 | for (Effect effect : kInvalidEffects) { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 315 | for (EffectStrength strength : kEffectStrengths) { |
| 316 | int32_t lengthMs; |
| 317 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
Lais Andrade | 1493200 | 2021-06-16 13:37:37 +0100 | [diff] [blame] | 318 | EXPECT_TRUE(isUnknownOrUnsupported(status)) |
| 319 | << status << toString(effect) << " " << toString(strength); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | for (Effect effect : kEffects) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 323 | for (EffectStrength strength : kInvalidEffectStrengths) { |
| 324 | int32_t lengthMs; |
| 325 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 326 | EXPECT_TRUE(isUnknownOrUnsupported(status)) |
| 327 | << status << " " << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | TEST_P(VibratorAidl, ChangeVibrationAmplitude) { |
| 333 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 334 | EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 335 | EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 336 | EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 337 | sleep(1); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 338 | EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 339 | sleep(1); |
Fenglin Wu | a464fb4 | 2022-05-12 10:25:58 +0800 | [diff] [blame] | 340 | EXPECT_TRUE(vibrator->off().isOk()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) { |
| 345 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
| 346 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode()); |
| 347 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 348 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) { |
| 353 | if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 354 | Status status = vibrator->setAmplitude(1); |
| 355 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | TEST_P(VibratorAidl, ChangeVibrationExternalControl) { |
| 360 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
| 361 | EXPECT_TRUE(vibrator->setExternalControl(true).isOk()); |
| 362 | sleep(1); |
| 363 | EXPECT_TRUE(vibrator->setExternalControl(false).isOk()); |
| 364 | sleep(1); |
| 365 | } |
| 366 | } |
| 367 | |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 368 | TEST_P(VibratorAidl, ExternalAmplitudeControl) { |
| 369 | const bool supportsExternalAmplitudeControl = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 370 | (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0; |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 371 | |
| 372 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
| 373 | EXPECT_TRUE(vibrator->setExternalControl(true).isOk()); |
| 374 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 375 | Status amplitudeStatus = vibrator->setAmplitude(0.5); |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 376 | if (supportsExternalAmplitudeControl) { |
| 377 | EXPECT_TRUE(amplitudeStatus.isOk()); |
| 378 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 379 | EXPECT_TRUE(isUnknownOrUnsupported(amplitudeStatus)) << amplitudeStatus; |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 380 | } |
| 381 | EXPECT_TRUE(vibrator->setExternalControl(false).isOk()); |
| 382 | } else { |
| 383 | EXPECT_FALSE(supportsExternalAmplitudeControl); |
| 384 | } |
| 385 | } |
| 386 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 387 | TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) { |
| 388 | if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 389 | Status status = vibrator->setExternalControl(true); |
| 390 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 394 | TEST_P(VibratorAidl, GetSupportedPrimitives) { |
| 395 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
| 396 | std::vector<CompositePrimitive> supported; |
| 397 | |
| 398 | EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode()); |
| 399 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 400 | for (auto primitive : kCompositePrimitives) { |
| 401 | bool isPrimitiveSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 402 | std::find(supported.begin(), supported.end(), primitive) != supported.end(); |
Lais Andrade | 7e64377 | 2021-07-09 14:59:44 +0100 | [diff] [blame] | 403 | bool isPrimitiveRequired = |
| 404 | std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) != |
| 405 | kRequiredPrimitives.end(); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 406 | |
Lais Andrade | 7e64377 | 2021-07-09 14:59:44 +0100 | [diff] [blame] | 407 | EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 408 | } |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | TEST_P(VibratorAidl, GetPrimitiveDuration) { |
| 413 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 414 | std::vector<CompositePrimitive> supported; |
| 415 | ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk()); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 416 | |
| 417 | for (auto primitive : kCompositePrimitives) { |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 418 | bool isPrimitiveSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 419 | std::find(supported.begin(), supported.end(), primitive) != supported.end(); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 420 | int32_t duration; |
| 421 | |
| 422 | Status status = vibrator->getPrimitiveDuration(primitive, &duration); |
| 423 | |
| 424 | if (isPrimitiveSupported) { |
| 425 | EXPECT_EQ(Status::EX_NONE, status.exceptionCode()); |
Lais Andrade | f1b4dd3 | 2021-11-03 16:47:32 +0000 | [diff] [blame] | 426 | if (primitive != CompositePrimitive::NOOP) { |
| 427 | ASSERT_GT(duration, 0) << toString(primitive) << " " << duration; |
| 428 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 429 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 430 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 431 | } |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 436 | TEST_P(VibratorAidl, ComposeValidPrimitives) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 437 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 438 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
| 439 | } |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 440 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 441 | std::vector<CompositePrimitive> supported; |
| 442 | int32_t maxDelay, maxSize; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 443 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 444 | ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk()); |
| 445 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode()); |
| 446 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 447 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 448 | std::vector<CompositeEffect> composite; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 449 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 450 | for (auto primitive : supported) { |
| 451 | CompositeEffect effect; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 452 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 453 | effect.delayMs = std::rand() % (maxDelay + 1); |
| 454 | effect.primitive = primitive; |
| 455 | effect.scale = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX); |
| 456 | composite.emplace_back(effect); |
| 457 | |
| 458 | if (composite.size() == maxSize) { |
| 459 | break; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 460 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 461 | } |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 462 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 463 | if (composite.size() != 0) { |
| 464 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 465 | EXPECT_TRUE(vibrator->off().isOk()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 470 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 471 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
| 472 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 473 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 474 | auto unsupported = kInvalidPrimitives; |
| 475 | std::vector<CompositePrimitive> supported; |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 476 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 477 | ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk()); |
| 478 | |
| 479 | for (auto primitive : kCompositePrimitives) { |
| 480 | bool isPrimitiveSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 481 | std::find(supported.begin(), supported.end(), primitive) != supported.end(); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 482 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 483 | if (!isPrimitiveSupported) { |
| 484 | unsupported.push_back(primitive); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 485 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 486 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 487 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 488 | for (auto primitive : unsupported) { |
| 489 | std::vector<CompositeEffect> composite(1); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 490 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 491 | for (auto& effect : composite) { |
| 492 | effect.delayMs = 0; |
| 493 | effect.primitive = primitive; |
| 494 | effect.scale = 1.0f; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 495 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 496 | Status status = vibrator->compose(composite, nullptr); |
| 497 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 501 | TEST_P(VibratorAidl, ComposeScaleBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 502 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 503 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 504 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 505 | |
| 506 | std::vector<CompositeEffect> composite(1); |
| 507 | CompositeEffect& effect = composite[0]; |
| 508 | |
| 509 | effect.delayMs = 0; |
| 510 | effect.primitive = CompositePrimitive::CLICK; |
| 511 | |
| 512 | effect.scale = std::nextafter(0.0f, -1.0f); |
| 513 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode()); |
| 514 | |
| 515 | effect.scale = 0.0f; |
| 516 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 517 | EXPECT_TRUE(vibrator->off().isOk()); |
| 518 | |
| 519 | effect.scale = 1.0f; |
| 520 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 521 | EXPECT_TRUE(vibrator->off().isOk()); |
| 522 | |
| 523 | effect.scale = std::nextafter(1.0f, 2.0f); |
| 524 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode()); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | TEST_P(VibratorAidl, ComposeDelayBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 528 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 529 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 530 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 531 | |
| 532 | int32_t maxDelay; |
| 533 | |
| 534 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode()); |
| 535 | |
| 536 | std::vector<CompositeEffect> composite(1); |
| 537 | CompositeEffect effect; |
| 538 | |
| 539 | effect.delayMs = 1; |
| 540 | effect.primitive = CompositePrimitive::CLICK; |
| 541 | effect.scale = 1.0f; |
| 542 | |
| 543 | std::fill(composite.begin(), composite.end(), effect); |
| 544 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 545 | EXPECT_TRUE(vibrator->off().isOk()); |
| 546 | |
| 547 | effect.delayMs = maxDelay + 1; |
| 548 | |
| 549 | std::fill(composite.begin(), composite.end(), effect); |
| 550 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 551 | } |
| 552 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 553 | TEST_P(VibratorAidl, ComposeSizeBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 554 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 555 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 556 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 557 | |
| 558 | int32_t maxSize; |
| 559 | |
| 560 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode()); |
| 561 | |
| 562 | std::vector<CompositeEffect> composite(maxSize); |
| 563 | CompositeEffect effect; |
| 564 | |
| 565 | effect.delayMs = 1; |
| 566 | effect.primitive = CompositePrimitive::CLICK; |
| 567 | effect.scale = 1.0f; |
| 568 | |
| 569 | std::fill(composite.begin(), composite.end(), effect); |
| 570 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 571 | EXPECT_TRUE(vibrator->off().isOk()); |
| 572 | |
| 573 | composite.emplace_back(effect); |
| 574 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 575 | } |
| 576 | |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 577 | TEST_P(VibratorAidl, ComposeCallback) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 578 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 579 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
| 580 | } |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 581 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 582 | std::vector<CompositePrimitive> supported; |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 583 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 584 | ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk()); |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 585 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 586 | for (auto primitive : supported) { |
| 587 | if (primitive == CompositePrimitive::NOOP) { |
| 588 | continue; |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 589 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 590 | |
| 591 | std::promise<void> completionPromise; |
| 592 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 593 | sp<CompletionCallback> callback = |
| 594 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
| 595 | CompositeEffect effect; |
| 596 | std::vector<CompositeEffect> composite; |
| 597 | int32_t durationMs; |
| 598 | std::chrono::milliseconds duration; |
| 599 | std::chrono::time_point<high_resolution_clock> start, end; |
| 600 | std::chrono::milliseconds elapsed; |
| 601 | |
| 602 | effect.delayMs = 0; |
| 603 | effect.primitive = primitive; |
| 604 | effect.scale = 1.0f; |
| 605 | composite.emplace_back(effect); |
| 606 | |
| 607 | EXPECT_EQ(Status::EX_NONE, |
| 608 | vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode()) |
| 609 | << toString(primitive); |
| 610 | duration = std::chrono::milliseconds(durationMs); |
| 611 | |
| 612 | start = high_resolution_clock::now(); |
| 613 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode()) |
| 614 | << toString(primitive); |
| 615 | |
| 616 | // TODO(b/261130361): Investigate why latency from driver and hardware will cause test |
| 617 | // to fail when wait duration is ~40ms or less. |
| 618 | EXPECT_EQ(completionFuture.wait_for(duration + std::chrono::milliseconds(50)), |
| 619 | std::future_status::ready) |
| 620 | << toString(primitive); |
| 621 | end = high_resolution_clock::now(); |
| 622 | |
| 623 | elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); |
| 624 | EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive); |
| 625 | |
| 626 | EXPECT_TRUE(vibrator->off().isOk()); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 630 | TEST_P(VibratorAidl, AlwaysOn) { |
| 631 | if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) { |
| 632 | std::vector<Effect> supported; |
| 633 | ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk()); |
| 634 | |
| 635 | for (Effect effect : kEffects) { |
| 636 | bool isEffectSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 637 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 638 | |
| 639 | for (EffectStrength strength : kEffectStrengths) { |
| 640 | Status status = vibrator->alwaysOnEnable(0, effect, strength); |
| 641 | |
| 642 | if (isEffectSupported) { |
| 643 | EXPECT_EQ(Status::EX_NONE, status.exceptionCode()) |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 644 | << toString(effect) << " " << toString(strength); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 645 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 646 | EXPECT_TRUE(isUnknownOrUnsupported(status)) |
| 647 | << status << " " << toString(effect) << " " << toString(strength); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode()); |
| 653 | } |
| 654 | } |
| 655 | |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 656 | TEST_P(VibratorAidl, GetResonantFrequency) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 657 | getResonantFrequencyHz(vibrator, capabilities); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | TEST_P(VibratorAidl, GetQFactor) { |
| 661 | float qFactor; |
| 662 | Status status = vibrator->getQFactor(&qFactor); |
| 663 | if (capabilities & IVibrator::CAP_GET_Q_FACTOR) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 664 | ASSERT_GT(qFactor, 0); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 665 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 666 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 667 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 671 | TEST_P(VibratorAidl, GetFrequencyResolution) { |
| 672 | getFrequencyResolutionHz(vibrator, capabilities); |
| 673 | } |
| 674 | |
| 675 | TEST_P(VibratorAidl, GetFrequencyMinimum) { |
| 676 | getFrequencyMinimumHz(vibrator, capabilities); |
| 677 | } |
| 678 | |
| 679 | TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) { |
| 680 | std::vector<float> bandwidthAmplitudeMap; |
| 681 | Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap); |
| 682 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 683 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 684 | ASSERT_FALSE(bandwidthAmplitudeMap.empty()); |
| 685 | |
| 686 | int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) - |
| 687 | getFrequencyMinimumHz(vibrator, capabilities)) / |
| 688 | getFrequencyResolutionHz(vibrator, capabilities); |
| 689 | ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize); |
| 690 | |
| 691 | for (float e : bandwidthAmplitudeMap) { |
| 692 | ASSERT_GE(e, 0.0); |
| 693 | ASSERT_LE(e, 1.0); |
| 694 | } |
| 695 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 696 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) { |
| 701 | int32_t durationMs; |
| 702 | Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs); |
| 703 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 704 | ASSERT_NE(durationMs, 0); |
| 705 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 706 | } else { |
Lais Andrade | 1493200 | 2021-06-16 13:37:37 +0100 | [diff] [blame] | 707 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | TEST_P(VibratorAidl, GetPwleCompositionSizeMax) { |
| 712 | int32_t maxSize; |
| 713 | Status status = vibrator->getPwleCompositionSizeMax(&maxSize); |
| 714 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 715 | ASSERT_NE(maxSize, 0); |
| 716 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 717 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 718 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | TEST_P(VibratorAidl, GetSupportedBraking) { |
| 723 | std::vector<Braking> supported; |
| 724 | Status status = vibrator->getSupportedBraking(&supported); |
| 725 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 726 | bool isDefaultNoneSupported = |
| 727 | std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end(); |
| 728 | ASSERT_TRUE(isDefaultNoneSupported); |
| 729 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 730 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 731 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
| 735 | TEST_P(VibratorAidl, ComposeValidPwle) { |
| 736 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 737 | ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 738 | |
| 739 | std::vector<Braking> supported; |
| 740 | ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk()); |
| 741 | bool isClabSupported = |
| 742 | std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end(); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 743 | BrakingPwle firstBraking; |
| 744 | firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE; |
| 745 | firstBraking.duration = 100; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 746 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 747 | ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities); |
| 748 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 749 | float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities); |
| 750 | float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities); |
| 751 | float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities); |
| 752 | secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f); |
| 753 | secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f); |
| 754 | } |
| 755 | BrakingPwle secondBraking; |
| 756 | secondBraking.braking = Braking::NONE; |
| 757 | secondBraking.duration = 10; |
| 758 | |
| 759 | auto pwleQueue = |
| 760 | std::vector<PrimitivePwle>{firstActive, firstBraking, secondActive, secondBraking}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 761 | |
| 762 | EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 763 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
| 767 | TEST_P(VibratorAidl, ComposeValidPwleWithCallback) { |
| 768 | if (!((capabilities & IVibrator::CAP_ON_CALLBACK) && |
| 769 | (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS))) |
| 770 | return; |
| 771 | |
| 772 | std::promise<void> completionPromise; |
| 773 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 774 | sp<CompletionCallback> callback = |
| 775 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
chasewu | 22cb901 | 2022-03-31 23:23:27 +0800 | [diff] [blame] | 776 | int32_t segmentDurationMaxMs; |
| 777 | vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs); |
| 778 | uint32_t durationMs = segmentDurationMaxMs * 2 + 100; // Sum of 2 active and 1 braking below |
Vince Leung | 7b8606e | 2021-05-04 13:56:48 -0700 | [diff] [blame] | 779 | //TODO(b/187207798): revert back to conservative timeout values once |
| 780 | //latencies have been fixed |
| 781 | std::chrono::milliseconds timeout{durationMs * 4}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 782 | |
| 783 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 784 | |
| 785 | std::vector<Braking> supported; |
| 786 | ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk()); |
| 787 | bool isClabSupported = |
| 788 | std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end(); |
| 789 | BrakingPwle braking; |
| 790 | braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE; |
| 791 | braking.duration = 100; |
| 792 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 793 | auto pwleQueue = std::vector<PrimitivePwle>{active, braking, active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 794 | |
| 795 | EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk()); |
| 796 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 797 | EXPECT_TRUE(vibrator->off().isOk()); |
| 798 | } |
| 799 | |
| 800 | TEST_P(VibratorAidl, ComposePwleSegmentBoundary) { |
| 801 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 802 | std::vector<PrimitivePwle> pwleQueue; |
| 803 | // test empty queue |
| 804 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 805 | vibrator->composePwle(pwleQueue, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 806 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 807 | |
| 808 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 809 | |
| 810 | PrimitivePwle pwle; |
| 811 | pwle = active; |
| 812 | int segmentCountMax; |
| 813 | vibrator->getPwleCompositionSizeMax(&segmentCountMax); |
| 814 | |
| 815 | // Create PWLE queue with more segments than allowed |
| 816 | for (int i = 0; i < segmentCountMax + 10; i++) { |
| 817 | pwleQueue.emplace_back(std::move(pwle)); |
| 818 | } |
| 819 | |
| 820 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 821 | vibrator->composePwle(pwleQueue, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 822 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
| 826 | TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) { |
| 827 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 828 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 829 | active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed |
| 830 | active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed |
| 831 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 832 | auto pwleQueueGreater = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 833 | |
| 834 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 835 | vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 836 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 837 | |
| 838 | active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed |
| 839 | active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed |
| 840 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 841 | auto pwleQueueLess = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 842 | |
| 843 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 844 | vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 845 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) { |
| 850 | if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) && |
| 851 | (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) { |
| 852 | float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities); |
| 853 | float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities); |
| 854 | float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities); |
| 855 | |
| 856 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 857 | active.startFrequency = |
| 858 | freqMaximumHz + freqResolutionHz; // Frequency greater than allowed |
| 859 | active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed |
| 860 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 861 | auto pwleQueueGreater = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 862 | |
| 863 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 864 | vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 865 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 866 | |
| 867 | active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed |
| 868 | active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed |
| 869 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 870 | auto pwleQueueLess = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 871 | |
| 872 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 873 | vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 874 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) { |
| 879 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 880 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 881 | |
chasewu | 22cb901 | 2022-03-31 23:23:27 +0800 | [diff] [blame] | 882 | int32_t segmentDurationMaxMs; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 883 | vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs); |
| 884 | active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed |
| 885 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 886 | auto pwleQueue = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 887 | |
| 888 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 889 | vibrator->composePwle(pwleQueue, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 890 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 894 | std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() { |
| 895 | std::vector<std::tuple<int32_t, int32_t>> tuples; |
| 896 | auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor); |
| 897 | std::vector<int32_t> vibratorIds; |
| 898 | |
| 899 | for (int i = 0; i < managerAidlNames.size(); i++) { |
| 900 | auto managerName = String16(managerAidlNames[i].c_str()); |
| 901 | auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName); |
| 902 | if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 903 | for (auto &vibratorId : vibratorIds) { |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 904 | tuples.push_back(std::make_tuple(i, vibratorId)); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor); |
| 910 | for (int i = 0; i < vibratorAidlNames.size(); i++) { |
| 911 | tuples.push_back(std::make_tuple(-1, i)); |
| 912 | } |
| 913 | |
| 914 | return tuples; |
| 915 | } |
| 916 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 917 | std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) { |
| 918 | const auto &[managerIdx, vibratorId] = info.param; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 919 | if (managerIdx < 0) { |
| 920 | return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId); |
| 921 | } |
| 922 | return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" + |
| 923 | std::to_string(vibratorId); |
| 924 | } |
| 925 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 926 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 927 | INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()), |
| 928 | PrintGeneratedTest); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 929 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 930 | int main(int argc, char **argv) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 931 | ::testing::InitGoogleTest(&argc, argv); |
| 932 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 933 | ProcessState::self()->startThreadPool(); |
| 934 | return RUN_ALL_TESTS(); |
| 935 | } |