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