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> |
| 18 | |
| 19 | #include <android/hardware/vibrator/BnVibratorCallback.h> |
| 20 | #include <android/hardware/vibrator/IVibrator.h> |
| 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/ProcessState.h> |
| 23 | |
| 24 | #include <future> |
| 25 | |
| 26 | using android::ProcessState; |
| 27 | using android::sp; |
| 28 | using android::String16; |
| 29 | using android::binder::Status; |
| 30 | using android::hardware::vibrator::BnVibratorCallback; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 31 | using android::hardware::vibrator::CompositeEffect; |
| 32 | using android::hardware::vibrator::CompositePrimitive; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 33 | using android::hardware::vibrator::Effect; |
| 34 | using android::hardware::vibrator::EffectStrength; |
| 35 | using android::hardware::vibrator::IVibrator; |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 36 | using std::chrono::high_resolution_clock; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 37 | |
Jooyung Han | 716648d | 2019-12-17 14:17:48 +0000 | [diff] [blame] | 38 | const std::vector<Effect> kEffects{android::enum_range<Effect>().begin(), |
| 39 | android::enum_range<Effect>().end()}; |
| 40 | const std::vector<EffectStrength> kEffectStrengths{android::enum_range<EffectStrength>().begin(), |
| 41 | android::enum_range<EffectStrength>().end()}; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 42 | |
| 43 | const std::vector<Effect> kInvalidEffects = { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 44 | static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1), |
| 45 | static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1), |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | const std::vector<EffectStrength> kInvalidEffectStrengths = { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 49 | static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1), |
| 50 | static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1), |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Steven Moreland | 1c26978 | 2020-01-09 11:16:05 -0800 | [diff] [blame] | 53 | const std::vector<CompositePrimitive> kCompositePrimitives{ |
| 54 | android::enum_range<CompositePrimitive>().begin(), |
| 55 | android::enum_range<CompositePrimitive>().end()}; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 56 | |
| 57 | const std::vector<CompositePrimitive> kInvalidPrimitives = { |
| 58 | static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1), |
| 59 | static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1), |
| 60 | }; |
| 61 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 62 | class CompletionCallback : public BnVibratorCallback { |
| 63 | public: |
| 64 | CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {} |
| 65 | Status onComplete() override { |
| 66 | mCallback(); |
| 67 | return Status::ok(); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | std::function<void()> mCallback; |
| 72 | }; |
| 73 | |
| 74 | class VibratorAidl : public testing::TestWithParam<std::string> { |
| 75 | public: |
| 76 | virtual void SetUp() override { |
| 77 | vibrator = android::waitForDeclaredService<IVibrator>(String16(GetParam().c_str())); |
| 78 | ASSERT_NE(vibrator, nullptr); |
| 79 | ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk()); |
| 80 | } |
| 81 | |
| 82 | sp<IVibrator> vibrator; |
| 83 | int32_t capabilities; |
| 84 | }; |
| 85 | |
| 86 | TEST_P(VibratorAidl, OnThenOffBeforeTimeout) { |
| 87 | EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk()); |
| 88 | sleep(1); |
| 89 | EXPECT_TRUE(vibrator->off().isOk()); |
| 90 | } |
| 91 | |
| 92 | TEST_P(VibratorAidl, OnWithCallback) { |
| 93 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return; |
| 94 | |
| 95 | std::promise<void> completionPromise; |
| 96 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 97 | sp<CompletionCallback> callback = |
| 98 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
| 99 | uint32_t durationMs = 250; |
| 100 | std::chrono::milliseconds timeout{durationMs * 2}; |
| 101 | EXPECT_TRUE(vibrator->on(durationMs, callback).isOk()); |
| 102 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 103 | EXPECT_TRUE(vibrator->off().isOk()); |
| 104 | } |
| 105 | |
| 106 | TEST_P(VibratorAidl, OnCallbackNotSupported) { |
| 107 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) { |
| 108 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
| 109 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->on(250, callback).exceptionCode()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | TEST_P(VibratorAidl, ValidateEffect) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 114 | std::vector<Effect> supported; |
| 115 | ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); |
| 116 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 117 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 118 | bool isEffectSupported = |
| 119 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
| 120 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 121 | for (EffectStrength strength : kEffectStrengths) { |
| 122 | int32_t lengthMs = 0; |
| 123 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 124 | |
| 125 | if (isEffectSupported) { |
Harpreet \"Eli\" Sangha | e1723a4 | 2019-12-06 14:09:33 +0900 | [diff] [blame] | 126 | EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 127 | EXPECT_GT(lengthMs, 0); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 128 | usleep(lengthMs * 1000); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 129 | } else { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 130 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION) |
Harpreet \"Eli\" Sangha | e1723a4 | 2019-12-06 14:09:33 +0900 | [diff] [blame] | 131 | << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 132 | EXPECT_EQ(lengthMs, 0); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | TEST_P(VibratorAidl, ValidateEffectWithCallback) { |
| 139 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return; |
| 140 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 141 | std::vector<Effect> supported; |
| 142 | ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); |
| 143 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 144 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 145 | bool isEffectSupported = |
| 146 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
| 147 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 148 | for (EffectStrength strength : kEffectStrengths) { |
| 149 | std::promise<void> completionPromise; |
| 150 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 151 | sp<CompletionCallback> callback = |
| 152 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 153 | int lengthMs = 0; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 154 | Status status = vibrator->perform(effect, strength, callback, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 155 | |
| 156 | if (isEffectSupported) { |
| 157 | EXPECT_TRUE(status.isOk()); |
| 158 | EXPECT_GT(lengthMs, 0); |
| 159 | } else { |
| 160 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); |
| 161 | EXPECT_EQ(lengthMs, 0); |
| 162 | } |
| 163 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 164 | if (!status.isOk()) continue; |
| 165 | |
| 166 | std::chrono::milliseconds timeout{lengthMs * 2}; |
| 167 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) { |
| 173 | if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return; |
| 174 | |
| 175 | for (Effect effect : kEffects) { |
| 176 | for (EffectStrength strength : kEffectStrengths) { |
| 177 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
| 178 | int lengthMs; |
| 179 | Status status = vibrator->perform(effect, strength, callback, &lengthMs); |
| 180 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode()); |
| 181 | EXPECT_EQ(lengthMs, 0); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | TEST_P(VibratorAidl, InvalidEffectsUnsupported) { |
| 187 | for (Effect effect : kInvalidEffects) { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 188 | for (EffectStrength strength : kEffectStrengths) { |
| 189 | int32_t lengthMs; |
| 190 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
| 191 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION) |
Harpreet \"Eli\" Sangha | e1723a4 | 2019-12-06 14:09:33 +0900 | [diff] [blame] | 192 | << toString(effect) << " " << toString(strength); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | for (Effect effect : kEffects) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 196 | for (EffectStrength strength : kInvalidEffectStrengths) { |
| 197 | int32_t lengthMs; |
| 198 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 199 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION) |
Harpreet \"Eli\" Sangha | e1723a4 | 2019-12-06 14:09:33 +0900 | [diff] [blame] | 200 | << toString(effect) << " " << toString(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | TEST_P(VibratorAidl, ChangeVibrationAmplitude) { |
| 206 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 207 | EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 208 | EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 209 | EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 210 | sleep(1); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 211 | EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 212 | sleep(1); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) { |
| 217 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
| 218 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode()); |
| 219 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode()); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 220 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) { |
| 225 | if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) { |
| 226 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode()); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | TEST_P(VibratorAidl, ChangeVibrationExternalControl) { |
| 231 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
| 232 | EXPECT_TRUE(vibrator->setExternalControl(true).isOk()); |
| 233 | sleep(1); |
| 234 | EXPECT_TRUE(vibrator->setExternalControl(false).isOk()); |
| 235 | sleep(1); |
| 236 | } |
| 237 | } |
| 238 | |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 239 | TEST_P(VibratorAidl, ExternalAmplitudeControl) { |
| 240 | const bool supportsExternalAmplitudeControl = |
| 241 | (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0; |
| 242 | |
| 243 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
| 244 | EXPECT_TRUE(vibrator->setExternalControl(true).isOk()); |
| 245 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 246 | Status amplitudeStatus = vibrator->setAmplitude(0.5); |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 247 | if (supportsExternalAmplitudeControl) { |
| 248 | EXPECT_TRUE(amplitudeStatus.isOk()); |
| 249 | } else { |
| 250 | EXPECT_EQ(amplitudeStatus.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); |
| 251 | } |
| 252 | EXPECT_TRUE(vibrator->setExternalControl(false).isOk()); |
| 253 | } else { |
| 254 | EXPECT_FALSE(supportsExternalAmplitudeControl); |
| 255 | } |
| 256 | } |
| 257 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 258 | TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) { |
| 259 | if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) { |
| 260 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, |
| 261 | vibrator->setExternalControl(true).exceptionCode()); |
| 262 | } |
| 263 | } |
| 264 | |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 265 | TEST_P(VibratorAidl, GetSupportedPrimitives) { |
| 266 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
| 267 | std::vector<CompositePrimitive> supported; |
| 268 | |
| 269 | EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode()); |
| 270 | |
| 271 | std::sort(supported.begin(), supported.end()); |
| 272 | |
| 273 | EXPECT_EQ(kCompositePrimitives, supported); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | TEST_P(VibratorAidl, GetPrimitiveDuration) { |
| 278 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
| 279 | int32_t duration; |
| 280 | |
| 281 | for (auto primitive : kCompositePrimitives) { |
| 282 | EXPECT_EQ(Status::EX_NONE, |
| 283 | vibrator->getPrimitiveDuration(primitive, &duration).exceptionCode()); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 288 | TEST_P(VibratorAidl, ComposeValidPrimitives) { |
| 289 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
| 290 | int32_t maxDelay, maxSize; |
| 291 | |
| 292 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode()); |
| 293 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode()); |
| 294 | |
| 295 | std::vector<CompositeEffect> composite; |
| 296 | |
| 297 | for (auto primitive : kCompositePrimitives) { |
| 298 | CompositeEffect effect; |
| 299 | |
| 300 | effect.delayMs = std::rand() % (maxDelay + 1); |
| 301 | effect.primitive = primitive; |
| 302 | effect.scale = static_cast<float>(std::rand()) / RAND_MAX ?: 1.0f; |
| 303 | composite.emplace_back(effect); |
| 304 | |
| 305 | if (composite.size() == maxSize) { |
| 306 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 307 | composite.clear(); |
| 308 | vibrator->off(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (composite.size() != 0) { |
| 313 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 314 | vibrator->off(); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) { |
| 320 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
| 321 | for (auto primitive : kInvalidPrimitives) { |
| 322 | std::vector<CompositeEffect> composite(1); |
| 323 | |
| 324 | for (auto& effect : composite) { |
| 325 | effect.delayMs = 0; |
| 326 | effect.primitive = primitive; |
| 327 | effect.scale = 1.0f; |
| 328 | } |
| 329 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, |
| 330 | vibrator->compose(composite, nullptr).exceptionCode()); |
| 331 | vibrator->off(); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | TEST_P(VibratorAidl, CompseDelayBoundary) { |
| 337 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
| 338 | int32_t maxDelay; |
| 339 | |
| 340 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode()); |
| 341 | |
| 342 | std::vector<CompositeEffect> composite(1); |
| 343 | CompositeEffect effect; |
| 344 | |
| 345 | effect.delayMs = 1; |
| 346 | effect.primitive = CompositePrimitive::CLICK; |
| 347 | effect.scale = 1.0f; |
| 348 | |
| 349 | std::fill(composite.begin(), composite.end(), effect); |
| 350 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 351 | |
| 352 | effect.delayMs = maxDelay + 1; |
| 353 | |
| 354 | std::fill(composite.begin(), composite.end(), effect); |
| 355 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 356 | vibrator->compose(composite, nullptr).exceptionCode()); |
| 357 | vibrator->off(); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | TEST_P(VibratorAidl, CompseSizeBoundary) { |
| 362 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
| 363 | int32_t maxSize; |
| 364 | |
| 365 | EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode()); |
| 366 | |
| 367 | std::vector<CompositeEffect> composite(maxSize); |
| 368 | CompositeEffect effect; |
| 369 | |
| 370 | effect.delayMs = 1; |
| 371 | effect.primitive = CompositePrimitive::CLICK; |
| 372 | effect.scale = 1.0f; |
| 373 | |
| 374 | std::fill(composite.begin(), composite.end(), effect); |
| 375 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode()); |
| 376 | |
| 377 | composite.emplace_back(effect); |
| 378 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 379 | vibrator->compose(composite, nullptr).exceptionCode()); |
| 380 | vibrator->off(); |
| 381 | } |
| 382 | } |
| 383 | |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 384 | TEST_P(VibratorAidl, ComposeCallback) { |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 385 | constexpr std::chrono::milliseconds allowedLatency{10}; |
| 386 | |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 387 | if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 388 | std::vector<CompositePrimitive> supported; |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 389 | |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 390 | ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk()); |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 391 | |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 392 | for (auto primitive : supported) { |
| 393 | if (primitive == CompositePrimitive::NOOP) { |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | std::promise<void> completionPromise; |
| 398 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 399 | sp<CompletionCallback> callback = |
| 400 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
| 401 | CompositeEffect effect; |
| 402 | std::vector<CompositeEffect> composite; |
| 403 | int32_t durationMs; |
| 404 | std::chrono::milliseconds duration; |
| 405 | std::chrono::time_point<high_resolution_clock> start, end; |
| 406 | std::chrono::milliseconds elapsed; |
| 407 | |
| 408 | effect.delayMs = 0; |
| 409 | effect.primitive = primitive; |
| 410 | effect.scale = 1.0f; |
| 411 | composite.emplace_back(effect); |
| 412 | |
| 413 | EXPECT_EQ(Status::EX_NONE, |
| 414 | vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode()) |
| 415 | << toString(primitive); |
| 416 | duration = std::chrono::milliseconds(durationMs); |
| 417 | |
| 418 | EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode()) |
| 419 | << toString(primitive); |
| 420 | start = high_resolution_clock::now(); |
| 421 | |
| 422 | EXPECT_EQ(completionFuture.wait_for(duration + allowedLatency), |
| 423 | std::future_status::ready) |
| 424 | << toString(primitive); |
| 425 | end = high_resolution_clock::now(); |
| 426 | |
| 427 | elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); |
| 428 | EXPECT_LE(elapsed.count(), (duration + allowedLatency).count()) << toString(primitive); |
| 429 | EXPECT_GE(elapsed.count(), (duration - allowedLatency).count()) << toString(primitive); |
| 430 | } |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 434 | TEST_P(VibratorAidl, AlwaysOn) { |
| 435 | if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) { |
| 436 | std::vector<Effect> supported; |
| 437 | ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk()); |
| 438 | |
| 439 | for (Effect effect : kEffects) { |
| 440 | bool isEffectSupported = |
| 441 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
| 442 | |
| 443 | for (EffectStrength strength : kEffectStrengths) { |
| 444 | Status status = vibrator->alwaysOnEnable(0, effect, strength); |
| 445 | |
| 446 | if (isEffectSupported) { |
| 447 | EXPECT_EQ(Status::EX_NONE, status.exceptionCode()) |
| 448 | << toString(effect) << " " << toString(strength); |
| 449 | } else { |
| 450 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode()) |
| 451 | << toString(effect) << " " << toString(strength); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode()); |
| 457 | } |
| 458 | } |
| 459 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame^] | 460 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl); |
Haibo Huang | 83b4c1e | 2019-11-08 11:59:40 -0800 | [diff] [blame] | 461 | INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 462 | testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)), |
| 463 | android::PrintInstanceNameToString); |
| 464 | |
| 465 | int main(int argc, char** argv) { |
| 466 | ::testing::InitGoogleTest(&argc, argv); |
| 467 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 468 | ProcessState::self()->startThreadPool(); |
| 469 | return RUN_ALL_TESTS(); |
| 470 | } |