Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 <android/hardware/vibrator/IVibratorManager.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <binder/ProcessState.h> |
| 24 | |
| 25 | #include <cmath> |
| 26 | #include <future> |
| 27 | |
| 28 | using android::ProcessState; |
| 29 | using android::sp; |
| 30 | using android::String16; |
| 31 | using android::binder::Status; |
| 32 | using android::hardware::vibrator::BnVibratorCallback; |
| 33 | using android::hardware::vibrator::CompositeEffect; |
| 34 | using android::hardware::vibrator::CompositePrimitive; |
| 35 | using android::hardware::vibrator::Effect; |
| 36 | using android::hardware::vibrator::EffectStrength; |
| 37 | using android::hardware::vibrator::IVibrator; |
| 38 | using android::hardware::vibrator::IVibratorManager; |
| 39 | using std::chrono::high_resolution_clock; |
| 40 | |
| 41 | const std::vector<Effect> kEffects{android::enum_range<Effect>().begin(), |
| 42 | android::enum_range<Effect>().end()}; |
| 43 | const std::vector<EffectStrength> kEffectStrengths{android::enum_range<EffectStrength>().begin(), |
| 44 | android::enum_range<EffectStrength>().end()}; |
| 45 | const std::vector<CompositePrimitive> kPrimitives{android::enum_range<CompositePrimitive>().begin(), |
| 46 | android::enum_range<CompositePrimitive>().end()}; |
| 47 | |
| 48 | class CompletionCallback : public BnVibratorCallback { |
| 49 | public: |
| 50 | CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {} |
| 51 | Status onComplete() override { |
| 52 | mCallback(); |
| 53 | return Status::ok(); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | std::function<void()> mCallback; |
| 58 | }; |
| 59 | |
| 60 | class VibratorAidl : public testing::TestWithParam<std::string> { |
| 61 | public: |
| 62 | virtual void SetUp() override { |
| 63 | manager = android::waitForDeclaredService<IVibratorManager>(String16(GetParam().c_str())); |
| 64 | ASSERT_NE(manager, nullptr); |
| 65 | ASSERT_TRUE(manager->getCapabilities(&capabilities).isOk()); |
| 66 | EXPECT_TRUE(manager->getVibratorIds(&vibratorIds).isOk()); |
| 67 | } |
| 68 | |
| 69 | sp<IVibratorManager> manager; |
| 70 | int32_t capabilities; |
| 71 | std::vector<int32_t> vibratorIds; |
| 72 | }; |
| 73 | |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 74 | inline bool isUnknownOrUnsupported(Status status) { |
| 75 | return status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 76 | status.transactionError() == android::UNKNOWN_TRANSACTION; |
| 77 | } |
| 78 | |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 79 | TEST_P(VibratorAidl, ValidateExistingVibrators) { |
| 80 | sp<IVibrator> vibrator; |
| 81 | for (auto& id : vibratorIds) { |
| 82 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 83 | ASSERT_NE(vibrator, nullptr); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | TEST_P(VibratorAidl, GetVibratorWithInvalidId) { |
| 88 | int32_t invalidId = *max_element(vibratorIds.begin(), vibratorIds.end()) + 1; |
| 89 | sp<IVibrator> vibrator; |
| 90 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 91 | manager->getVibrator(invalidId, &vibrator).exceptionCode()); |
| 92 | ASSERT_EQ(vibrator, nullptr); |
| 93 | } |
| 94 | |
| 95 | TEST_P(VibratorAidl, ValidatePrepareSyncedExistingVibrators) { |
| 96 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 97 | if (vibratorIds.empty()) return; |
| 98 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
Chase Wu | 8fb96d2 | 2022-09-20 13:38:57 +0800 | [diff] [blame] | 99 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_P(VibratorAidl, PrepareSyncedEmptySetIsInvalid) { |
| 103 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 104 | std::vector<int32_t> emptyIds; |
| 105 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, manager->prepareSynced(emptyIds).exceptionCode()); |
| 106 | } |
| 107 | |
| 108 | TEST_P(VibratorAidl, PrepareSyncedNotSupported) { |
| 109 | if (!(capabilities & IVibratorManager::CAP_SYNC)) { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 110 | Status status = manager->prepareSynced(vibratorIds); |
| 111 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | TEST_P(VibratorAidl, PrepareOnNotSupported) { |
| 116 | if (vibratorIds.empty()) return; |
| 117 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 118 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
| 119 | uint32_t durationMs = 250; |
| 120 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 121 | sp<IVibrator> vibrator; |
| 122 | for (auto& id : vibratorIds) { |
| 123 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 124 | ASSERT_NE(vibrator, nullptr); |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 125 | Status status = vibrator->on(durationMs, nullptr); |
| 126 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 127 | } |
| 128 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | TEST_P(VibratorAidl, PreparePerformNotSupported) { |
| 133 | if (vibratorIds.empty()) return; |
| 134 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 135 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
| 136 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 137 | sp<IVibrator> vibrator; |
| 138 | for (auto& id : vibratorIds) { |
| 139 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 140 | ASSERT_NE(vibrator, nullptr); |
| 141 | int32_t lengthMs = 0; |
| 142 | Status status = vibrator->perform(kEffects[0], kEffectStrengths[0], nullptr, &lengthMs); |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 143 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 144 | } |
| 145 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | TEST_P(VibratorAidl, PrepareComposeNotSupported) { |
| 150 | if (vibratorIds.empty()) return; |
| 151 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 152 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
| 153 | std::vector<CompositeEffect> composite; |
| 154 | CompositeEffect effect; |
| 155 | effect.delayMs = 10; |
| 156 | effect.primitive = kPrimitives[0]; |
| 157 | effect.scale = 1.0f; |
| 158 | composite.emplace_back(effect); |
| 159 | |
| 160 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 161 | sp<IVibrator> vibrator; |
| 162 | for (auto& id : vibratorIds) { |
| 163 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 164 | ASSERT_NE(vibrator, nullptr); |
| 165 | Status status = vibrator->compose(composite, nullptr); |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 166 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 167 | } |
| 168 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | TEST_P(VibratorAidl, TriggerWithCallback) { |
| 173 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 174 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) return; |
| 175 | if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) return; |
| 176 | if (vibratorIds.empty()) return; |
| 177 | |
| 178 | std::promise<void> completionPromise; |
| 179 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 180 | sp<CompletionCallback> callback = |
| 181 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
| 182 | uint32_t durationMs = 250; |
| 183 | std::chrono::milliseconds timeout{durationMs * 2}; |
| 184 | |
| 185 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 186 | sp<IVibrator> vibrator; |
| 187 | for (auto& id : vibratorIds) { |
| 188 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 189 | ASSERT_NE(vibrator, nullptr); |
| 190 | EXPECT_TRUE(vibrator->on(durationMs, nullptr).isOk()); |
| 191 | } |
| 192 | |
| 193 | EXPECT_TRUE(manager->triggerSynced(callback).isOk()); |
| 194 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 195 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
| 196 | } |
| 197 | |
| 198 | TEST_P(VibratorAidl, TriggerSyncNotSupported) { |
| 199 | if (!(capabilities & IVibratorManager::CAP_SYNC)) { |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 200 | Status status = manager->triggerSynced(nullptr); |
| 201 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | TEST_P(VibratorAidl, TriggerCallbackNotSupported) { |
| 206 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 207 | if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) { |
| 208 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
| 209 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
Lais Andrade | 4b54b1f | 2021-06-10 16:38:34 +0100 | [diff] [blame] | 210 | Status status = manager->triggerSynced(callback); |
| 211 | EXPECT_TRUE(isUnknownOrUnsupported(status)) << status; |
Chase Wu | 8fb96d2 | 2022-09-20 13:38:57 +0800 | [diff] [blame] | 212 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl); |
| 217 | INSTANTIATE_TEST_SUITE_P( |
| 218 | Vibrator, VibratorAidl, |
| 219 | testing::ValuesIn(android::getAidlHalInstanceNames(IVibratorManager::descriptor)), |
| 220 | android::PrintInstanceNameToString); |
| 221 | |
| 222 | int main(int argc, char** argv) { |
| 223 | ::testing::InitGoogleTest(&argc, argv); |
| 224 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 225 | ProcessState::self()->startThreadPool(); |
| 226 | return RUN_ALL_TESTS(); |
| 227 | } |