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; |
| 31 | using android::hardware::vibrator::Effect; |
| 32 | using android::hardware::vibrator::EffectStrength; |
| 33 | using android::hardware::vibrator::IVibrator; |
| 34 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 35 | // TODO(b/143992652): autogenerate |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 36 | const std::vector<Effect> kEffects = { |
| 37 | Effect::CLICK, Effect::DOUBLE_CLICK, Effect::TICK, Effect::THUD, |
| 38 | Effect::POP, Effect::HEAVY_CLICK, Effect::RINGTONE_1, Effect::RINGTONE_2, |
| 39 | Effect::RINGTONE_3, Effect::RINGTONE_4, Effect::RINGTONE_5, Effect::RINGTONE_6, |
| 40 | Effect::RINGTONE_7, Effect::RINGTONE_8, Effect::RINGTONE_9, Effect::RINGTONE_10, |
| 41 | Effect::RINGTONE_11, Effect::RINGTONE_12, Effect::RINGTONE_13, Effect::RINGTONE_14, |
| 42 | Effect::RINGTONE_15, Effect::TEXTURE_TICK}; |
| 43 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 44 | // TODO(b/143992652): autogenerate |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 45 | const std::vector<EffectStrength> kEffectStrengths = {EffectStrength::LIGHT, EffectStrength::MEDIUM, |
| 46 | EffectStrength::STRONG}; |
| 47 | |
| 48 | const std::vector<Effect> kInvalidEffects = { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 49 | static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1), |
| 50 | static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1), |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | const std::vector<EffectStrength> kInvalidEffectStrengths = { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 54 | static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1), |
| 55 | static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1), |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | class CompletionCallback : public BnVibratorCallback { |
| 59 | public: |
| 60 | CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {} |
| 61 | Status onComplete() override { |
| 62 | mCallback(); |
| 63 | return Status::ok(); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | std::function<void()> mCallback; |
| 68 | }; |
| 69 | |
| 70 | class VibratorAidl : public testing::TestWithParam<std::string> { |
| 71 | public: |
| 72 | virtual void SetUp() override { |
| 73 | vibrator = android::waitForDeclaredService<IVibrator>(String16(GetParam().c_str())); |
| 74 | ASSERT_NE(vibrator, nullptr); |
| 75 | ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk()); |
| 76 | } |
| 77 | |
| 78 | sp<IVibrator> vibrator; |
| 79 | int32_t capabilities; |
| 80 | }; |
| 81 | |
| 82 | TEST_P(VibratorAidl, OnThenOffBeforeTimeout) { |
| 83 | EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk()); |
| 84 | sleep(1); |
| 85 | EXPECT_TRUE(vibrator->off().isOk()); |
| 86 | } |
| 87 | |
| 88 | TEST_P(VibratorAidl, OnWithCallback) { |
| 89 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return; |
| 90 | |
| 91 | std::promise<void> completionPromise; |
| 92 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 93 | sp<CompletionCallback> callback = |
| 94 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
| 95 | uint32_t durationMs = 250; |
| 96 | std::chrono::milliseconds timeout{durationMs * 2}; |
| 97 | EXPECT_TRUE(vibrator->on(durationMs, callback).isOk()); |
| 98 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 99 | EXPECT_TRUE(vibrator->off().isOk()); |
| 100 | } |
| 101 | |
| 102 | TEST_P(VibratorAidl, OnCallbackNotSupported) { |
| 103 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) { |
| 104 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
| 105 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->on(250, callback).exceptionCode()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | TEST_P(VibratorAidl, ValidateEffect) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 110 | std::vector<Effect> supported; |
| 111 | ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); |
| 112 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 113 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 114 | bool isEffectSupported = |
| 115 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
| 116 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 117 | for (EffectStrength strength : kEffectStrengths) { |
| 118 | int32_t lengthMs = 0; |
| 119 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 120 | |
| 121 | if (isEffectSupported) { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 122 | EXPECT_TRUE(status.isOk()) |
| 123 | << static_cast<int>(effect) << " " << static_cast<int>(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 124 | EXPECT_GT(lengthMs, 0); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 125 | usleep(lengthMs * 1000); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 126 | } else { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 127 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION) |
| 128 | << static_cast<int>(effect) << " " << static_cast<int>(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 129 | EXPECT_EQ(lengthMs, 0); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | TEST_P(VibratorAidl, ValidateEffectWithCallback) { |
| 136 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return; |
| 137 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 138 | std::vector<Effect> supported; |
| 139 | ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); |
| 140 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 141 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 142 | bool isEffectSupported = |
| 143 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
| 144 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 145 | for (EffectStrength strength : kEffectStrengths) { |
| 146 | std::promise<void> completionPromise; |
| 147 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 148 | sp<CompletionCallback> callback = |
| 149 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 150 | int lengthMs = 0; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 151 | Status status = vibrator->perform(effect, strength, callback, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 152 | |
| 153 | if (isEffectSupported) { |
| 154 | EXPECT_TRUE(status.isOk()); |
| 155 | EXPECT_GT(lengthMs, 0); |
| 156 | } else { |
| 157 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); |
| 158 | EXPECT_EQ(lengthMs, 0); |
| 159 | } |
| 160 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 161 | if (!status.isOk()) continue; |
| 162 | |
| 163 | std::chrono::milliseconds timeout{lengthMs * 2}; |
| 164 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) { |
| 170 | if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return; |
| 171 | |
| 172 | for (Effect effect : kEffects) { |
| 173 | for (EffectStrength strength : kEffectStrengths) { |
| 174 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
| 175 | int lengthMs; |
| 176 | Status status = vibrator->perform(effect, strength, callback, &lengthMs); |
| 177 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode()); |
| 178 | EXPECT_EQ(lengthMs, 0); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | TEST_P(VibratorAidl, InvalidEffectsUnsupported) { |
| 184 | for (Effect effect : kInvalidEffects) { |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 185 | for (EffectStrength strength : kEffectStrengths) { |
| 186 | int32_t lengthMs; |
| 187 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
| 188 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION) |
| 189 | << static_cast<int>(effect) << " " << static_cast<int>(strength); |
| 190 | } |
| 191 | } |
| 192 | for (Effect effect : kEffects) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 193 | for (EffectStrength strength : kInvalidEffectStrengths) { |
| 194 | int32_t lengthMs; |
| 195 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
Steven Moreland | f335388 | 2019-11-07 17:02:43 -0800 | [diff] [blame] | 196 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION) |
| 197 | << static_cast<int>(effect) << " " << static_cast<int>(strength); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | TEST_P(VibratorAidl, ChangeVibrationAmplitude) { |
| 203 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
| 204 | EXPECT_TRUE(vibrator->setAmplitude(1).isOk()); |
| 205 | EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk()); |
| 206 | EXPECT_TRUE(vibrator->setAmplitude(128).isOk()); |
| 207 | sleep(1); |
| 208 | EXPECT_TRUE(vibrator->setAmplitude(255).isOk()); |
| 209 | sleep(1); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) { |
| 214 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
| 215 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode()); |
| 216 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode()); |
| 217 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(256).exceptionCode()); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) { |
| 222 | if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) { |
| 223 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode()); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | TEST_P(VibratorAidl, ChangeVibrationExternalControl) { |
| 228 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
| 229 | EXPECT_TRUE(vibrator->setExternalControl(true).isOk()); |
| 230 | sleep(1); |
| 231 | EXPECT_TRUE(vibrator->setExternalControl(false).isOk()); |
| 232 | sleep(1); |
| 233 | } |
| 234 | } |
| 235 | |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 236 | TEST_P(VibratorAidl, ExternalAmplitudeControl) { |
| 237 | const bool supportsExternalAmplitudeControl = |
| 238 | (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0; |
| 239 | |
| 240 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
| 241 | EXPECT_TRUE(vibrator->setExternalControl(true).isOk()); |
| 242 | |
| 243 | Status amplitudeStatus = vibrator->setAmplitude(128); |
| 244 | if (supportsExternalAmplitudeControl) { |
| 245 | EXPECT_TRUE(amplitudeStatus.isOk()); |
| 246 | } else { |
| 247 | EXPECT_EQ(amplitudeStatus.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); |
| 248 | } |
| 249 | EXPECT_TRUE(vibrator->setExternalControl(false).isOk()); |
| 250 | } else { |
| 251 | EXPECT_FALSE(supportsExternalAmplitudeControl); |
| 252 | } |
| 253 | } |
| 254 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 255 | TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) { |
| 256 | if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) { |
| 257 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, |
| 258 | vibrator->setExternalControl(true).exceptionCode()); |
| 259 | } |
| 260 | } |
| 261 | |
Haibo Huang | 83b4c1e | 2019-11-08 11:59:40 -0800 | [diff] [blame] | 262 | INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 263 | testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)), |
| 264 | android::PrintInstanceNameToString); |
| 265 | |
| 266 | int main(int argc, char** argv) { |
| 267 | ::testing::InitGoogleTest(&argc, argv); |
| 268 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 269 | ProcessState::self()->startThreadPool(); |
| 270 | return RUN_ALL_TESTS(); |
| 271 | } |