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