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 | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 450 | for (int i = 0; i < supported.size(); i++) { |
| 451 | auto primitive = supported[i]; |
| 452 | float t = static_cast<float>(i + 1) / supported.size(); |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 453 | CompositeEffect effect; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 454 | |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 455 | effect.delayMs = maxDelay * t; |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 456 | effect.primitive = primitive; |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 457 | effect.scale = t; |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 458 | |
| 459 | if (composite.size() == maxSize) { |
| 460 | break; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 461 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 462 | } |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 463 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 464 | if (composite.size() != 0) { |
| 465 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 466 | EXPECT_TRUE(vibrator->off().isOk()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 471 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 472 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
| 473 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 474 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 475 | auto unsupported = kInvalidPrimitives; |
| 476 | std::vector<CompositePrimitive> supported; |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 477 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 478 | ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk()); |
| 479 | |
| 480 | for (auto primitive : kCompositePrimitives) { |
| 481 | bool isPrimitiveSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 482 | std::find(supported.begin(), supported.end(), primitive) != supported.end(); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 483 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 484 | if (!isPrimitiveSupported) { |
| 485 | unsupported.push_back(primitive); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 486 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 487 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 488 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 489 | for (auto primitive : unsupported) { |
| 490 | std::vector<CompositeEffect> composite(1); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 491 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 492 | for (auto& effect : composite) { |
| 493 | effect.delayMs = 0; |
| 494 | effect.primitive = primitive; |
| 495 | effect.scale = 1.0f; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 496 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 497 | Status status = vibrator->compose(composite, nullptr); |
| 498 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 502 | TEST_P(VibratorAidl, ComposeScaleBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 503 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 504 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 505 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 506 | |
| 507 | std::vector<CompositeEffect> composite(1); |
| 508 | CompositeEffect& effect = composite[0]; |
| 509 | |
| 510 | effect.delayMs = 0; |
| 511 | effect.primitive = CompositePrimitive::CLICK; |
| 512 | |
| 513 | effect.scale = std::nextafter(0.0f, -1.0f); |
| 514 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode()); |
| 515 | |
| 516 | effect.scale = 0.0f; |
| 517 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 518 | EXPECT_TRUE(vibrator->off().isOk()); |
| 519 | |
| 520 | effect.scale = 1.0f; |
| 521 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 522 | EXPECT_TRUE(vibrator->off().isOk()); |
| 523 | |
| 524 | effect.scale = std::nextafter(1.0f, 2.0f); |
| 525 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode()); |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | TEST_P(VibratorAidl, ComposeDelayBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 529 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 530 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 531 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 532 | |
| 533 | int32_t maxDelay; |
| 534 | |
| 535 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode()); |
| 536 | |
| 537 | std::vector<CompositeEffect> composite(1); |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 538 | CompositeEffect& effect = composite[0]; |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 539 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 540 | effect.primitive = CompositePrimitive::CLICK; |
| 541 | effect.scale = 1.0f; |
| 542 | |
Lais Andrade | 661481e | 2024-02-12 10:33:09 +0000 | [diff] [blame] | 543 | effect.delayMs = 0; |
| 544 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 545 | EXPECT_TRUE(vibrator->off().isOk()); |
| 546 | |
| 547 | effect.delayMs = 1; |
| 548 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 549 | EXPECT_TRUE(vibrator->off().isOk()); |
| 550 | |
| 551 | effect.delayMs = maxDelay; |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 552 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 553 | EXPECT_TRUE(vibrator->off().isOk()); |
| 554 | |
| 555 | effect.delayMs = maxDelay + 1; |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 556 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 557 | } |
| 558 | |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 559 | TEST_P(VibratorAidl, ComposeSizeBoundary) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 560 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 561 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 562 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 563 | |
| 564 | int32_t maxSize; |
| 565 | |
| 566 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode()); |
| 567 | |
| 568 | std::vector<CompositeEffect> composite(maxSize); |
| 569 | CompositeEffect effect; |
| 570 | |
| 571 | effect.delayMs = 1; |
| 572 | effect.primitive = CompositePrimitive::CLICK; |
| 573 | effect.scale = 1.0f; |
| 574 | |
| 575 | std::fill(composite.begin(), composite.end(), effect); |
| 576 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 577 | EXPECT_TRUE(vibrator->off().isOk()); |
| 578 | |
| 579 | composite.emplace_back(effect); |
| 580 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->compose(composite, nullptr).exceptionCode()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 581 | } |
| 582 | |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 583 | TEST_P(VibratorAidl, ComposeCallback) { |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 584 | if (!(capabilities & IVibrator::CAP_COMPOSE_EFFECTS)) { |
| 585 | GTEST_SKIP() << "CAP_COMPOSE_EFFECTS not supported"; |
| 586 | } |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 587 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 588 | std::vector<CompositePrimitive> supported; |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 589 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 590 | ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk()); |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 591 | |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 592 | for (auto primitive : supported) { |
| 593 | if (primitive == CompositePrimitive::NOOP) { |
| 594 | continue; |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 595 | } |
Lais Andrade | 38d054e | 2024-02-09 12:42:09 +0000 | [diff] [blame] | 596 | |
| 597 | std::promise<void> completionPromise; |
| 598 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 599 | sp<CompletionCallback> callback = |
| 600 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
| 601 | CompositeEffect effect; |
| 602 | std::vector<CompositeEffect> composite; |
| 603 | int32_t durationMs; |
| 604 | std::chrono::milliseconds duration; |
| 605 | std::chrono::time_point<high_resolution_clock> start, end; |
| 606 | std::chrono::milliseconds elapsed; |
| 607 | |
| 608 | effect.delayMs = 0; |
| 609 | effect.primitive = primitive; |
| 610 | effect.scale = 1.0f; |
| 611 | composite.emplace_back(effect); |
| 612 | |
| 613 | EXPECT_EQ(Status::EX_NONE, |
| 614 | vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode()) |
| 615 | << toString(primitive); |
| 616 | duration = std::chrono::milliseconds(durationMs); |
| 617 | |
| 618 | start = high_resolution_clock::now(); |
| 619 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode()) |
| 620 | << toString(primitive); |
| 621 | |
| 622 | // TODO(b/261130361): Investigate why latency from driver and hardware will cause test |
| 623 | // to fail when wait duration is ~40ms or less. |
| 624 | EXPECT_EQ(completionFuture.wait_for(duration + std::chrono::milliseconds(50)), |
| 625 | std::future_status::ready) |
| 626 | << toString(primitive); |
| 627 | end = high_resolution_clock::now(); |
| 628 | |
| 629 | elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); |
| 630 | EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive); |
| 631 | |
| 632 | EXPECT_TRUE(vibrator->off().isOk()); |
Harpreet \"Eli\" Sangha | afa8636 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 636 | TEST_P(VibratorAidl, AlwaysOn) { |
| 637 | if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) { |
| 638 | std::vector<Effect> supported; |
| 639 | ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk()); |
| 640 | |
| 641 | for (Effect effect : kEffects) { |
| 642 | bool isEffectSupported = |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 643 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 644 | |
| 645 | for (EffectStrength strength : kEffectStrengths) { |
| 646 | Status status = vibrator->alwaysOnEnable(0, effect, strength); |
| 647 | |
| 648 | if (isEffectSupported) { |
| 649 | EXPECT_EQ(Status::EX_NONE, status.exceptionCode()) |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 650 | << toString(effect) << " " << toString(strength); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 651 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 652 | EXPECT_TRUE(isUnknownOrUnsupported(status)) |
| 653 | << status << " " << toString(effect) << " " << toString(strength); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode()); |
| 659 | } |
| 660 | } |
| 661 | |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 662 | TEST_P(VibratorAidl, GetResonantFrequency) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 663 | getResonantFrequencyHz(vibrator, capabilities); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | TEST_P(VibratorAidl, GetQFactor) { |
| 667 | float qFactor; |
| 668 | Status status = vibrator->getQFactor(&qFactor); |
| 669 | if (capabilities & IVibrator::CAP_GET_Q_FACTOR) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 670 | ASSERT_GT(qFactor, 0); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 671 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 672 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 673 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 677 | TEST_P(VibratorAidl, GetFrequencyResolution) { |
| 678 | getFrequencyResolutionHz(vibrator, capabilities); |
| 679 | } |
| 680 | |
| 681 | TEST_P(VibratorAidl, GetFrequencyMinimum) { |
| 682 | getFrequencyMinimumHz(vibrator, capabilities); |
| 683 | } |
| 684 | |
| 685 | TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) { |
| 686 | std::vector<float> bandwidthAmplitudeMap; |
| 687 | Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap); |
| 688 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 689 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 690 | ASSERT_FALSE(bandwidthAmplitudeMap.empty()); |
| 691 | |
| 692 | int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) - |
| 693 | getFrequencyMinimumHz(vibrator, capabilities)) / |
| 694 | getFrequencyResolutionHz(vibrator, capabilities); |
| 695 | ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize); |
| 696 | |
| 697 | for (float e : bandwidthAmplitudeMap) { |
| 698 | ASSERT_GE(e, 0.0); |
| 699 | ASSERT_LE(e, 1.0); |
| 700 | } |
| 701 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 702 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
| 706 | TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) { |
| 707 | int32_t durationMs; |
| 708 | Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs); |
| 709 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 710 | ASSERT_NE(durationMs, 0); |
| 711 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 712 | } else { |
Lais Andrade | 1493200 | 2021-06-16 13:37:37 +0100 | [diff] [blame] | 713 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | TEST_P(VibratorAidl, GetPwleCompositionSizeMax) { |
| 718 | int32_t maxSize; |
| 719 | Status status = vibrator->getPwleCompositionSizeMax(&maxSize); |
| 720 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 721 | ASSERT_NE(maxSize, 0); |
| 722 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 723 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 724 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
| 728 | TEST_P(VibratorAidl, GetSupportedBraking) { |
| 729 | std::vector<Braking> supported; |
| 730 | Status status = vibrator->getSupportedBraking(&supported); |
| 731 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 732 | bool isDefaultNoneSupported = |
| 733 | std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end(); |
| 734 | ASSERT_TRUE(isDefaultNoneSupported); |
| 735 | EXPECT_EQ(status.exceptionCode(), Status::EX_NONE); |
| 736 | } else { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 737 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | TEST_P(VibratorAidl, ComposeValidPwle) { |
| 742 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 743 | ActivePwle firstActive = composeValidActivePwle(vibrator, capabilities); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 744 | |
| 745 | std::vector<Braking> supported; |
| 746 | ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk()); |
| 747 | bool isClabSupported = |
| 748 | std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end(); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 749 | BrakingPwle firstBraking; |
| 750 | firstBraking.braking = isClabSupported ? Braking::CLAB : Braking::NONE; |
| 751 | firstBraking.duration = 100; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 752 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 753 | ActivePwle secondActive = composeValidActivePwle(vibrator, capabilities); |
| 754 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 755 | float minFrequencyHz = getFrequencyMinimumHz(vibrator, capabilities); |
| 756 | float maxFrequencyHz = getFrequencyMaximumHz(vibrator, capabilities); |
| 757 | float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities); |
| 758 | secondActive.startFrequency = minFrequencyHz + (freqResolutionHz / 2.0f); |
| 759 | secondActive.endFrequency = maxFrequencyHz - (freqResolutionHz / 3.0f); |
| 760 | } |
| 761 | BrakingPwle secondBraking; |
| 762 | secondBraking.braking = Braking::NONE; |
| 763 | secondBraking.duration = 10; |
| 764 | |
| 765 | auto pwleQueue = |
| 766 | std::vector<PrimitivePwle>{firstActive, firstBraking, secondActive, secondBraking}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 767 | |
| 768 | EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 769 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | TEST_P(VibratorAidl, ComposeValidPwleWithCallback) { |
| 774 | if (!((capabilities & IVibrator::CAP_ON_CALLBACK) && |
| 775 | (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS))) |
| 776 | return; |
| 777 | |
| 778 | std::promise<void> completionPromise; |
| 779 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 780 | sp<CompletionCallback> callback = |
| 781 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
chasewu | 22cb901 | 2022-03-31 23:23:27 +0800 | [diff] [blame] | 782 | int32_t segmentDurationMaxMs; |
| 783 | vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs); |
| 784 | 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] | 785 | //TODO(b/187207798): revert back to conservative timeout values once |
| 786 | //latencies have been fixed |
| 787 | std::chrono::milliseconds timeout{durationMs * 4}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 788 | |
| 789 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 790 | |
| 791 | std::vector<Braking> supported; |
| 792 | ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk()); |
| 793 | bool isClabSupported = |
| 794 | std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end(); |
| 795 | BrakingPwle braking; |
| 796 | braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE; |
| 797 | braking.duration = 100; |
| 798 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 799 | auto pwleQueue = std::vector<PrimitivePwle>{active, braking, active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 800 | |
| 801 | EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk()); |
| 802 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 803 | EXPECT_TRUE(vibrator->off().isOk()); |
| 804 | } |
| 805 | |
| 806 | TEST_P(VibratorAidl, ComposePwleSegmentBoundary) { |
| 807 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 808 | std::vector<PrimitivePwle> pwleQueue; |
| 809 | // test empty queue |
| 810 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 811 | vibrator->composePwle(pwleQueue, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 812 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 813 | |
| 814 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 815 | |
| 816 | PrimitivePwle pwle; |
| 817 | pwle = active; |
| 818 | int segmentCountMax; |
| 819 | vibrator->getPwleCompositionSizeMax(&segmentCountMax); |
| 820 | |
| 821 | // Create PWLE queue with more segments than allowed |
| 822 | for (int i = 0; i < segmentCountMax + 10; i++) { |
| 823 | pwleQueue.emplace_back(std::move(pwle)); |
| 824 | } |
| 825 | |
| 826 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 827 | vibrator->composePwle(pwleQueue, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 828 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) { |
| 833 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 834 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 835 | active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed |
| 836 | active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed |
| 837 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 838 | auto pwleQueueGreater = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 839 | |
| 840 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 841 | vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 842 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 843 | |
| 844 | active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed |
| 845 | active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed |
| 846 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 847 | auto pwleQueueLess = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 848 | |
| 849 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 850 | vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 851 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) { |
| 856 | if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) && |
| 857 | (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) { |
| 858 | float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities); |
| 859 | float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities); |
| 860 | float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities); |
| 861 | |
| 862 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 863 | active.startFrequency = |
| 864 | freqMaximumHz + freqResolutionHz; // Frequency greater than allowed |
| 865 | active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed |
| 866 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 867 | auto pwleQueueGreater = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 868 | |
| 869 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 870 | vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 871 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 872 | |
| 873 | active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed |
| 874 | active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed |
| 875 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 876 | auto pwleQueueLess = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 877 | |
| 878 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 879 | vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 880 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
| 884 | TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) { |
| 885 | if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) { |
| 886 | ActivePwle active = composeValidActivePwle(vibrator, capabilities); |
| 887 | |
chasewu | 22cb901 | 2022-03-31 23:23:27 +0800 | [diff] [blame] | 888 | int32_t segmentDurationMaxMs; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 889 | vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs); |
| 890 | active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed |
| 891 | |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 892 | auto pwleQueue = std::vector<PrimitivePwle>{active}; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 893 | |
| 894 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 895 | vibrator->composePwle(pwleQueue, nullptr).exceptionCode()); |
Lais Andrade | 22754c5 | 2021-09-14 12:21:59 +0000 | [diff] [blame] | 896 | EXPECT_TRUE(vibrator->off().isOk()); |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 900 | std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() { |
| 901 | std::vector<std::tuple<int32_t, int32_t>> tuples; |
| 902 | auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor); |
| 903 | std::vector<int32_t> vibratorIds; |
| 904 | |
| 905 | for (int i = 0; i < managerAidlNames.size(); i++) { |
| 906 | auto managerName = String16(managerAidlNames[i].c_str()); |
| 907 | auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName); |
| 908 | if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) { |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 909 | for (auto &vibratorId : vibratorIds) { |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 910 | tuples.push_back(std::make_tuple(i, vibratorId)); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor); |
| 916 | for (int i = 0; i < vibratorAidlNames.size(); i++) { |
| 917 | tuples.push_back(std::make_tuple(-1, i)); |
| 918 | } |
| 919 | |
| 920 | return tuples; |
| 921 | } |
| 922 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 923 | std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) { |
| 924 | const auto &[managerIdx, vibratorId] = info.param; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 925 | if (managerIdx < 0) { |
| 926 | return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId); |
| 927 | } |
| 928 | return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" + |
| 929 | std::to_string(vibratorId); |
| 930 | } |
| 931 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 932 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 933 | INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()), |
| 934 | PrintGeneratedTest); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 935 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 936 | int main(int argc, char **argv) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 937 | ::testing::InitGoogleTest(&argc, argv); |
| 938 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 939 | ProcessState::self()->startThreadPool(); |
| 940 | return RUN_ALL_TESTS(); |
| 941 | } |