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 = { |
| 49 | static_cast<Effect>(static_cast<int32_t>(*kEffects.begin()) - 1), |
| 50 | static_cast<Effect>(static_cast<int32_t>(*kEffects.end()) + 1), |
| 51 | }; |
| 52 | |
| 53 | const std::vector<EffectStrength> kInvalidEffectStrengths = { |
| 54 | static_cast<EffectStrength>(static_cast<int8_t>(*kEffectStrengths.begin()) - 1), |
| 55 | static_cast<EffectStrength>(static_cast<int8_t>(*kEffectStrengths.end()) + 1), |
| 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) { |
| 122 | EXPECT_TRUE(status.isOk()); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 123 | EXPECT_GT(lengthMs, 0); |
| 124 | } else { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 125 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 126 | EXPECT_EQ(lengthMs, 0); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | TEST_P(VibratorAidl, ValidateEffectWithCallback) { |
| 133 | if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return; |
| 134 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 135 | std::vector<Effect> supported; |
| 136 | ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); |
| 137 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 138 | for (Effect effect : kEffects) { |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 139 | bool isEffectSupported = |
| 140 | std::find(supported.begin(), supported.end(), effect) != supported.end(); |
| 141 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 142 | for (EffectStrength strength : kEffectStrengths) { |
| 143 | std::promise<void> completionPromise; |
| 144 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 145 | sp<CompletionCallback> callback = |
| 146 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 147 | int lengthMs = 0; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 148 | Status status = vibrator->perform(effect, strength, callback, &lengthMs); |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 149 | |
| 150 | if (isEffectSupported) { |
| 151 | EXPECT_TRUE(status.isOk()); |
| 152 | EXPECT_GT(lengthMs, 0); |
| 153 | } else { |
| 154 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); |
| 155 | EXPECT_EQ(lengthMs, 0); |
| 156 | } |
| 157 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 158 | if (!status.isOk()) continue; |
| 159 | |
| 160 | std::chrono::milliseconds timeout{lengthMs * 2}; |
| 161 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) { |
| 167 | if (capabilities & IVibrator::CAP_PERFORM_CALLBACK) return; |
| 168 | |
| 169 | for (Effect effect : kEffects) { |
| 170 | for (EffectStrength strength : kEffectStrengths) { |
| 171 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
| 172 | int lengthMs; |
| 173 | Status status = vibrator->perform(effect, strength, callback, &lengthMs); |
| 174 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode()); |
| 175 | EXPECT_EQ(lengthMs, 0); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | TEST_P(VibratorAidl, InvalidEffectsUnsupported) { |
| 181 | for (Effect effect : kInvalidEffects) { |
| 182 | for (EffectStrength strength : kInvalidEffectStrengths) { |
| 183 | int32_t lengthMs; |
| 184 | Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); |
| 185 | EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | TEST_P(VibratorAidl, ChangeVibrationAmplitude) { |
| 191 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
| 192 | EXPECT_TRUE(vibrator->setAmplitude(1).isOk()); |
| 193 | EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk()); |
| 194 | EXPECT_TRUE(vibrator->setAmplitude(128).isOk()); |
| 195 | sleep(1); |
| 196 | EXPECT_TRUE(vibrator->setAmplitude(255).isOk()); |
| 197 | sleep(1); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) { |
| 202 | if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) { |
| 203 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode()); |
| 204 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode()); |
| 205 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(256).exceptionCode()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) { |
| 210 | if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) { |
| 211 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, vibrator->setAmplitude(1).exceptionCode()); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | TEST_P(VibratorAidl, ChangeVibrationExternalControl) { |
| 216 | if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) { |
| 217 | EXPECT_TRUE(vibrator->setExternalControl(true).isOk()); |
| 218 | sleep(1); |
| 219 | EXPECT_TRUE(vibrator->setExternalControl(false).isOk()); |
| 220 | sleep(1); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) { |
| 225 | if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) { |
| 226 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, |
| 227 | vibrator->setExternalControl(true).exceptionCode()); |
| 228 | } |
| 229 | } |
| 230 | |
Haibo Huang | 83b4c1e | 2019-11-08 11:59:40 -0800 | [diff] [blame^] | 231 | INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 232 | testing::ValuesIn(android::getAidlHalInstanceNames(IVibrator::descriptor)), |
| 233 | android::PrintInstanceNameToString); |
| 234 | |
| 235 | int main(int argc, char** argv) { |
| 236 | ::testing::InitGoogleTest(&argc, argv); |
| 237 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 238 | ProcessState::self()->startThreadPool(); |
| 239 | return RUN_ALL_TESTS(); |
| 240 | } |