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