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