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