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 | |
| 74 | TEST_P(VibratorAidl, ValidateExistingVibrators) { |
| 75 | sp<IVibrator> vibrator; |
| 76 | for (auto& id : vibratorIds) { |
| 77 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 78 | ASSERT_NE(vibrator, nullptr); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | TEST_P(VibratorAidl, GetVibratorWithInvalidId) { |
| 83 | int32_t invalidId = *max_element(vibratorIds.begin(), vibratorIds.end()) + 1; |
| 84 | sp<IVibrator> vibrator; |
| 85 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, |
| 86 | manager->getVibrator(invalidId, &vibrator).exceptionCode()); |
| 87 | ASSERT_EQ(vibrator, nullptr); |
| 88 | } |
| 89 | |
| 90 | TEST_P(VibratorAidl, ValidatePrepareSyncedExistingVibrators) { |
| 91 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 92 | if (vibratorIds.empty()) return; |
| 93 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 94 | } |
| 95 | |
| 96 | TEST_P(VibratorAidl, PrepareSyncedEmptySetIsInvalid) { |
| 97 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 98 | std::vector<int32_t> emptyIds; |
| 99 | EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, manager->prepareSynced(emptyIds).exceptionCode()); |
| 100 | } |
| 101 | |
| 102 | TEST_P(VibratorAidl, PrepareSyncedNotSupported) { |
| 103 | if (!(capabilities & IVibratorManager::CAP_SYNC)) { |
| 104 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, |
| 105 | manager->prepareSynced(vibratorIds).exceptionCode()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | TEST_P(VibratorAidl, PrepareOnNotSupported) { |
| 110 | if (vibratorIds.empty()) return; |
| 111 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 112 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
| 113 | uint32_t durationMs = 250; |
| 114 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 115 | sp<IVibrator> vibrator; |
| 116 | for (auto& id : vibratorIds) { |
| 117 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 118 | ASSERT_NE(vibrator, nullptr); |
| 119 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, |
| 120 | vibrator->on(durationMs, nullptr).exceptionCode()); |
| 121 | } |
| 122 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | TEST_P(VibratorAidl, PreparePerformNotSupported) { |
| 127 | if (vibratorIds.empty()) return; |
| 128 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 129 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
| 130 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 131 | sp<IVibrator> vibrator; |
| 132 | for (auto& id : vibratorIds) { |
| 133 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 134 | ASSERT_NE(vibrator, nullptr); |
| 135 | int32_t lengthMs = 0; |
| 136 | Status status = vibrator->perform(kEffects[0], kEffectStrengths[0], nullptr, &lengthMs); |
| 137 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode()); |
| 138 | } |
| 139 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | TEST_P(VibratorAidl, PrepareComposeNotSupported) { |
| 144 | if (vibratorIds.empty()) return; |
| 145 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 146 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
| 147 | std::vector<CompositeEffect> composite; |
| 148 | CompositeEffect effect; |
| 149 | effect.delayMs = 10; |
| 150 | effect.primitive = kPrimitives[0]; |
| 151 | effect.scale = 1.0f; |
| 152 | composite.emplace_back(effect); |
| 153 | |
| 154 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 155 | sp<IVibrator> vibrator; |
| 156 | for (auto& id : vibratorIds) { |
| 157 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 158 | ASSERT_NE(vibrator, nullptr); |
| 159 | Status status = vibrator->compose(composite, nullptr); |
| 160 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, status.exceptionCode()); |
| 161 | } |
| 162 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | TEST_P(VibratorAidl, TriggerWithCallback) { |
| 167 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 168 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) return; |
| 169 | if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) return; |
| 170 | if (vibratorIds.empty()) return; |
| 171 | |
| 172 | std::promise<void> completionPromise; |
| 173 | std::future<void> completionFuture{completionPromise.get_future()}; |
| 174 | sp<CompletionCallback> callback = |
| 175 | new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); |
| 176 | uint32_t durationMs = 250; |
| 177 | std::chrono::milliseconds timeout{durationMs * 2}; |
| 178 | |
| 179 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 180 | sp<IVibrator> vibrator; |
| 181 | for (auto& id : vibratorIds) { |
| 182 | EXPECT_TRUE(manager->getVibrator(id, &vibrator).isOk()); |
| 183 | ASSERT_NE(vibrator, nullptr); |
| 184 | EXPECT_TRUE(vibrator->on(durationMs, nullptr).isOk()); |
| 185 | } |
| 186 | |
| 187 | EXPECT_TRUE(manager->triggerSynced(callback).isOk()); |
| 188 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
| 189 | EXPECT_TRUE(manager->cancelSynced().isOk()); |
| 190 | } |
| 191 | |
| 192 | TEST_P(VibratorAidl, TriggerSyncNotSupported) { |
| 193 | if (!(capabilities & IVibratorManager::CAP_SYNC)) { |
| 194 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, |
| 195 | manager->triggerSynced(nullptr).exceptionCode()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | TEST_P(VibratorAidl, TriggerCallbackNotSupported) { |
| 200 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 201 | if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) { |
| 202 | sp<CompletionCallback> callback = new CompletionCallback([] {}); |
| 203 | EXPECT_TRUE(manager->prepareSynced(vibratorIds).isOk()); |
| 204 | EXPECT_EQ(Status::EX_UNSUPPORTED_OPERATION, |
| 205 | manager->triggerSynced(callback).exceptionCode()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl); |
| 210 | INSTANTIATE_TEST_SUITE_P( |
| 211 | Vibrator, VibratorAidl, |
| 212 | testing::ValuesIn(android::getAidlHalInstanceNames(IVibratorManager::descriptor)), |
| 213 | android::PrintInstanceNameToString); |
| 214 | |
| 215 | int main(int argc, char** argv) { |
| 216 | ::testing::InitGoogleTest(&argc, argv); |
| 217 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 218 | ProcessState::self()->startThreadPool(); |
| 219 | return RUN_ALL_TESTS(); |
| 220 | } |