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> |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 18 | #include <aidl/android/hardware/vibrator/BnVibratorCallback.h> |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 19 | #include <aidl/android/hardware/vibrator/IVibrationSession.h> |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 20 | #include <aidl/android/hardware/vibrator/IVibrator.h> |
| 21 | #include <aidl/android/hardware/vibrator/IVibratorManager.h> |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 22 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 23 | #include <android/binder_manager.h> |
| 24 | #include <android/binder_process.h> |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 25 | |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 26 | #include <algorithm> |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 27 | #include <cmath> |
| 28 | #include <future> |
| 29 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 30 | #include "test_utils.h" |
| 31 | |
| 32 | using aidl::android::hardware::vibrator::BnVibratorCallback; |
| 33 | using aidl::android::hardware::vibrator::CompositeEffect; |
| 34 | using aidl::android::hardware::vibrator::CompositePrimitive; |
| 35 | using aidl::android::hardware::vibrator::Effect; |
| 36 | using aidl::android::hardware::vibrator::EffectStrength; |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 37 | using aidl::android::hardware::vibrator::IVibrationSession; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 38 | using aidl::android::hardware::vibrator::IVibrator; |
| 39 | using aidl::android::hardware::vibrator::IVibratorManager; |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 40 | using aidl::android::hardware::vibrator::VibrationSessionConfig; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 41 | using std::chrono::high_resolution_clock; |
| 42 | |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 43 | using namespace ::std::chrono_literals; |
| 44 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 45 | const std::vector<Effect> kEffects{ndk::enum_range<Effect>().begin(), |
| 46 | ndk::enum_range<Effect>().end()}; |
| 47 | const std::vector<EffectStrength> kEffectStrengths{ndk::enum_range<EffectStrength>().begin(), |
| 48 | ndk::enum_range<EffectStrength>().end()}; |
| 49 | const std::vector<CompositePrimitive> kPrimitives{ndk::enum_range<CompositePrimitive>().begin(), |
| 50 | ndk::enum_range<CompositePrimitive>().end()}; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 51 | |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 52 | // Timeout to wait for vibration callback completion. |
| 53 | static constexpr std::chrono::milliseconds VIBRATION_CALLBACK_TIMEOUT = 100ms; |
| 54 | |
| 55 | static constexpr int32_t VIBRATION_SESSIONS_MIN_VERSION = 3; |
| 56 | |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 57 | class CompletionCallback : public BnVibratorCallback { |
| 58 | public: |
| 59 | CompletionCallback(const std::function<void()>& callback) : mCallback(callback) {} |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 60 | ndk::ScopedAStatus onComplete() override { |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 61 | mCallback(); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 62 | return ndk::ScopedAStatus::ok(); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private: |
| 66 | std::function<void()> mCallback; |
| 67 | }; |
| 68 | |
| 69 | class VibratorAidl : public testing::TestWithParam<std::string> { |
| 70 | public: |
| 71 | virtual void SetUp() override { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 72 | auto serviceName = GetParam().c_str(); |
| 73 | manager = IVibratorManager::fromBinder( |
| 74 | ndk::SpAIBinder(AServiceManager_waitForService(serviceName))); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 75 | ASSERT_NE(manager, nullptr); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 76 | EXPECT_OK(manager->getCapabilities(&capabilities)); |
| 77 | EXPECT_OK(manager->getVibratorIds(&vibratorIds)); |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 78 | EXPECT_OK(manager->getInterfaceVersion(&version)); |
| 79 | } |
| 80 | |
| 81 | virtual void TearDown() override { |
| 82 | // Reset manager state between tests. |
| 83 | if (capabilities & IVibratorManager::CAP_SYNC) { |
| 84 | manager->cancelSynced(); |
| 85 | } |
| 86 | if (capabilities & IVibratorManager::CAP_START_SESSIONS) { |
| 87 | manager->clearSessions(); |
| 88 | } |
| 89 | // Reset all managed vibrators. |
| 90 | for (int32_t id : vibratorIds) { |
| 91 | std::shared_ptr<IVibrator> vibrator; |
| 92 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 93 | ASSERT_NE(vibrator, nullptr); |
| 94 | EXPECT_OK(vibrator->off()); |
| 95 | } |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 98 | std::shared_ptr<IVibratorManager> manager; |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 99 | std::shared_ptr<IVibrationSession> session; |
| 100 | int32_t version; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 101 | int32_t capabilities; |
| 102 | std::vector<int32_t> vibratorIds; |
| 103 | }; |
| 104 | |
| 105 | TEST_P(VibratorAidl, ValidateExistingVibrators) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 106 | std::shared_ptr<IVibrator> vibrator; |
| 107 | for (int32_t id : vibratorIds) { |
| 108 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 109 | ASSERT_NE(vibrator, nullptr); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | TEST_P(VibratorAidl, GetVibratorWithInvalidId) { |
| 114 | int32_t invalidId = *max_element(vibratorIds.begin(), vibratorIds.end()) + 1; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 115 | std::shared_ptr<IVibrator> vibrator; |
| 116 | EXPECT_ILLEGAL_ARGUMENT(manager->getVibrator(invalidId, &vibrator)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 117 | ASSERT_EQ(vibrator, nullptr); |
| 118 | } |
| 119 | |
| 120 | TEST_P(VibratorAidl, ValidatePrepareSyncedExistingVibrators) { |
| 121 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 122 | if (vibratorIds.empty()) return; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 123 | EXPECT_OK(manager->prepareSynced(vibratorIds)); |
| 124 | EXPECT_OK(manager->cancelSynced()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST_P(VibratorAidl, PrepareSyncedEmptySetIsInvalid) { |
| 128 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 129 | std::vector<int32_t> emptyIds; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 130 | EXPECT_ILLEGAL_ARGUMENT(manager->prepareSynced(emptyIds)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST_P(VibratorAidl, PrepareSyncedNotSupported) { |
| 134 | if (!(capabilities & IVibratorManager::CAP_SYNC)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 135 | EXPECT_UNKNOWN_OR_UNSUPPORTED(manager->prepareSynced(vibratorIds)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | TEST_P(VibratorAidl, PrepareOnNotSupported) { |
| 140 | if (vibratorIds.empty()) return; |
| 141 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 142 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 143 | int32_t durationMs = 250; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 144 | EXPECT_OK(manager->prepareSynced(vibratorIds)); |
| 145 | std::shared_ptr<IVibrator> vibrator; |
| 146 | for (int32_t id : vibratorIds) { |
| 147 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 148 | ASSERT_NE(vibrator, nullptr); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 149 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->on(durationMs, nullptr)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 150 | } |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 151 | EXPECT_OK(manager->cancelSynced()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | TEST_P(VibratorAidl, PreparePerformNotSupported) { |
| 156 | if (vibratorIds.empty()) return; |
| 157 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 158 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 159 | EXPECT_OK(manager->prepareSynced(vibratorIds)); |
| 160 | std::shared_ptr<IVibrator> vibrator; |
| 161 | for (int32_t id : vibratorIds) { |
| 162 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 163 | ASSERT_NE(vibrator, nullptr); |
| 164 | int32_t lengthMs = 0; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 165 | EXPECT_UNKNOWN_OR_UNSUPPORTED( |
| 166 | vibrator->perform(kEffects[0], kEffectStrengths[0], nullptr, &lengthMs)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 167 | } |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 168 | EXPECT_OK(manager->cancelSynced()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | TEST_P(VibratorAidl, PrepareComposeNotSupported) { |
| 173 | if (vibratorIds.empty()) return; |
| 174 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 175 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) { |
| 176 | std::vector<CompositeEffect> composite; |
| 177 | CompositeEffect effect; |
| 178 | effect.delayMs = 10; |
| 179 | effect.primitive = kPrimitives[0]; |
| 180 | effect.scale = 1.0f; |
| 181 | composite.emplace_back(effect); |
| 182 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 183 | EXPECT_OK(manager->prepareSynced(vibratorIds)); |
| 184 | std::shared_ptr<IVibrator> vibrator; |
| 185 | for (int32_t id : vibratorIds) { |
| 186 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 187 | ASSERT_NE(vibrator, nullptr); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 188 | EXPECT_UNKNOWN_OR_UNSUPPORTED(vibrator->compose(composite, nullptr)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 189 | } |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 190 | EXPECT_OK(manager->cancelSynced()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | TEST_P(VibratorAidl, TriggerWithCallback) { |
| 195 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 196 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) return; |
| 197 | if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) return; |
| 198 | if (vibratorIds.empty()) return; |
| 199 | |
| 200 | std::promise<void> completionPromise; |
| 201 | std::future<void> completionFuture{completionPromise.get_future()}; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 202 | auto callback = ndk::SharedRefBase::make<CompletionCallback>( |
| 203 | [&completionPromise] { completionPromise.set_value(); }); |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 204 | int32_t durationMs = 250; |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 205 | std::chrono::milliseconds timeout{durationMs * 2}; |
| 206 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 207 | EXPECT_OK(manager->prepareSynced(vibratorIds)); |
| 208 | std::shared_ptr<IVibrator> vibrator; |
| 209 | for (int32_t id : vibratorIds) { |
| 210 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 211 | ASSERT_NE(vibrator, nullptr); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 212 | EXPECT_OK(vibrator->on(durationMs, nullptr)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 215 | EXPECT_OK(manager->triggerSynced(callback)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 216 | EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 217 | EXPECT_OK(manager->cancelSynced()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | TEST_P(VibratorAidl, TriggerSyncNotSupported) { |
| 221 | if (!(capabilities & IVibratorManager::CAP_SYNC)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 222 | EXPECT_UNKNOWN_OR_UNSUPPORTED(manager->triggerSynced(nullptr)); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | TEST_P(VibratorAidl, TriggerCallbackNotSupported) { |
| 227 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 228 | if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 229 | auto callback = ndk::SharedRefBase::make<CompletionCallback>([] {}); |
| 230 | EXPECT_OK(manager->prepareSynced(vibratorIds)); |
| 231 | EXPECT_UNKNOWN_OR_UNSUPPORTED(manager->triggerSynced(callback)); |
| 232 | EXPECT_OK(manager->cancelSynced()); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 236 | TEST_P(VibratorAidl, VibrationSessionsSupported) { |
| 237 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 238 | if (vibratorIds.empty()) return; |
| 239 | |
| 240 | std::promise<void> sessionPromise; |
| 241 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 242 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 243 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 244 | |
| 245 | VibrationSessionConfig sessionConfig; |
| 246 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 247 | ASSERT_NE(session, nullptr); |
| 248 | |
| 249 | int32_t durationMs = 250; |
| 250 | std::vector<std::promise<void>> vibrationPromises; |
| 251 | std::vector<std::future<void>> vibrationFutures; |
| 252 | for (int32_t id : vibratorIds) { |
| 253 | std::shared_ptr<IVibrator> vibrator; |
| 254 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 255 | ASSERT_NE(vibrator, nullptr); |
| 256 | |
| 257 | std::promise<void>& vibrationPromise = vibrationPromises.emplace_back(); |
| 258 | vibrationFutures.push_back(vibrationPromise.get_future()); |
| 259 | auto vibrationCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 260 | [&vibrationPromise] { vibrationPromise.set_value(); }); |
| 261 | EXPECT_OK(vibrator->on(durationMs, vibrationCallback)); |
| 262 | } |
| 263 | |
| 264 | auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT; |
| 265 | for (std::future<void>& future : vibrationFutures) { |
| 266 | EXPECT_EQ(future.wait_for(timeout), std::future_status::ready); |
| 267 | } |
| 268 | |
| 269 | // Session callback not triggered. |
| 270 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 271 | |
| 272 | // Ending a session should not take long since the vibration was already completed |
| 273 | EXPECT_OK(session->close()); |
| 274 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::ready); |
| 275 | } |
| 276 | |
| 277 | TEST_P(VibratorAidl, VibrationSessionInterrupted) { |
| 278 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 279 | if (vibratorIds.empty()) return; |
| 280 | |
| 281 | std::promise<void> sessionPromise; |
| 282 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 283 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 284 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 285 | |
| 286 | VibrationSessionConfig sessionConfig; |
| 287 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 288 | ASSERT_NE(session, nullptr); |
| 289 | |
| 290 | std::vector<std::promise<void>> vibrationPromises; |
| 291 | std::vector<std::future<void>> vibrationFutures; |
| 292 | for (int32_t id : vibratorIds) { |
| 293 | std::shared_ptr<IVibrator> vibrator; |
| 294 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 295 | ASSERT_NE(vibrator, nullptr); |
| 296 | |
| 297 | std::promise<void>& vibrationPromise = vibrationPromises.emplace_back(); |
| 298 | vibrationFutures.push_back(vibrationPromise.get_future()); |
| 299 | auto vibrationCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 300 | [&vibrationPromise] { vibrationPromise.set_value(); }); |
| 301 | |
| 302 | // Vibration longer than test timeout. |
| 303 | EXPECT_OK(vibrator->on(2000, vibrationCallback)); |
| 304 | } |
| 305 | |
| 306 | // Session callback not triggered. |
| 307 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 308 | |
| 309 | // Interrupt vibrations and session. |
| 310 | EXPECT_OK(session->abort()); |
| 311 | |
| 312 | // Both callbacks triggered. |
| 313 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::ready); |
| 314 | for (std::future<void>& future : vibrationFutures) { |
| 315 | EXPECT_EQ(future.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::ready); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | TEST_P(VibratorAidl, VibrationSessionEndingInterrupted) { |
| 320 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 321 | if (vibratorIds.empty()) return; |
| 322 | |
| 323 | std::promise<void> sessionPromise; |
| 324 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 325 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 326 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 327 | |
| 328 | VibrationSessionConfig sessionConfig; |
| 329 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 330 | ASSERT_NE(session, nullptr); |
| 331 | |
| 332 | std::vector<std::promise<void>> vibrationPromises; |
| 333 | std::vector<std::future<void>> vibrationFutures; |
| 334 | for (int32_t id : vibratorIds) { |
| 335 | std::shared_ptr<IVibrator> vibrator; |
| 336 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 337 | ASSERT_NE(vibrator, nullptr); |
| 338 | |
| 339 | std::promise<void>& vibrationPromise = vibrationPromises.emplace_back(); |
| 340 | vibrationFutures.push_back(vibrationPromise.get_future()); |
| 341 | auto vibrationCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 342 | [&vibrationPromise] { vibrationPromise.set_value(); }); |
| 343 | |
| 344 | // Vibration longer than test timeout. |
| 345 | EXPECT_OK(vibrator->on(2000, vibrationCallback)); |
| 346 | } |
| 347 | |
| 348 | // Session callback not triggered. |
| 349 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 350 | |
| 351 | // End session, this might take a while |
| 352 | EXPECT_OK(session->close()); |
| 353 | |
| 354 | // Interrupt ending session. |
| 355 | EXPECT_OK(session->abort()); |
| 356 | |
| 357 | // Both callbacks triggered. |
| 358 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::ready); |
| 359 | for (std::future<void>& future : vibrationFutures) { |
| 360 | EXPECT_EQ(future.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::ready); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | TEST_P(VibratorAidl, VibrationSessionCleared) { |
| 365 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 366 | if (vibratorIds.empty()) return; |
| 367 | |
| 368 | std::promise<void> sessionPromise; |
| 369 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 370 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 371 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 372 | |
| 373 | VibrationSessionConfig sessionConfig; |
| 374 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 375 | ASSERT_NE(session, nullptr); |
| 376 | |
| 377 | int32_t durationMs = 250; |
| 378 | std::vector<std::promise<void>> vibrationPromises; |
| 379 | std::vector<std::future<void>> vibrationFutures; |
| 380 | for (int32_t id : vibratorIds) { |
| 381 | std::shared_ptr<IVibrator> vibrator; |
| 382 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 383 | ASSERT_NE(vibrator, nullptr); |
| 384 | |
| 385 | std::promise<void>& vibrationPromise = vibrationPromises.emplace_back(); |
| 386 | vibrationFutures.push_back(vibrationPromise.get_future()); |
| 387 | auto vibrationCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 388 | [&vibrationPromise] { vibrationPromise.set_value(); }); |
| 389 | EXPECT_OK(vibrator->on(durationMs, vibrationCallback)); |
| 390 | } |
| 391 | |
| 392 | // Session callback not triggered. |
| 393 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 394 | |
| 395 | // Clearing sessions should abort ongoing session |
| 396 | EXPECT_OK(manager->clearSessions()); |
| 397 | |
| 398 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::ready); |
| 399 | for (std::future<void>& future : vibrationFutures) { |
| 400 | EXPECT_EQ(future.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::ready); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | TEST_P(VibratorAidl, VibrationSessionsClearedWithoutSession) { |
| 405 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 406 | |
| 407 | EXPECT_OK(manager->clearSessions()); |
| 408 | } |
| 409 | |
| 410 | TEST_P(VibratorAidl, VibrationSessionsWithSyncedVibrations) { |
| 411 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 412 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 413 | if (!(capabilities & IVibratorManager::CAP_PREPARE_ON)) return; |
| 414 | if (!(capabilities & IVibratorManager::CAP_TRIGGER_CALLBACK)) return; |
| 415 | if (vibratorIds.empty()) return; |
| 416 | |
| 417 | std::promise<void> sessionPromise; |
| 418 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 419 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 420 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 421 | |
| 422 | VibrationSessionConfig sessionConfig; |
| 423 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 424 | ASSERT_NE(session, nullptr); |
| 425 | |
| 426 | EXPECT_OK(manager->prepareSynced(vibratorIds)); |
| 427 | |
| 428 | int32_t durationMs = 250; |
| 429 | std::vector<std::promise<void>> vibrationPromises; |
| 430 | std::vector<std::future<void>> vibrationFutures; |
| 431 | for (int32_t id : vibratorIds) { |
| 432 | std::shared_ptr<IVibrator> vibrator; |
| 433 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 434 | ASSERT_NE(vibrator, nullptr); |
| 435 | |
| 436 | std::promise<void>& vibrationPromise = vibrationPromises.emplace_back(); |
| 437 | vibrationFutures.push_back(vibrationPromise.get_future()); |
| 438 | auto vibrationCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 439 | [&vibrationPromise] { vibrationPromise.set_value(); }); |
| 440 | EXPECT_OK(vibrator->on(durationMs, vibrationCallback)); |
| 441 | } |
| 442 | |
| 443 | std::promise<void> triggerPromise; |
| 444 | std::future<void> triggerFuture{triggerPromise.get_future()}; |
| 445 | auto triggerCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 446 | [&triggerPromise] { triggerPromise.set_value(); }); |
| 447 | |
| 448 | EXPECT_OK(manager->triggerSynced(triggerCallback)); |
| 449 | |
| 450 | auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT; |
| 451 | EXPECT_EQ(triggerFuture.wait_for(timeout), std::future_status::ready); |
| 452 | for (std::future<void>& future : vibrationFutures) { |
| 453 | EXPECT_EQ(future.wait_for(timeout), std::future_status::ready); |
| 454 | } |
| 455 | |
| 456 | // Session callback not triggered. |
| 457 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 458 | |
| 459 | // Ending a session should not take long since the vibration was already completed |
| 460 | EXPECT_OK(session->close()); |
| 461 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::ready); |
| 462 | } |
| 463 | |
| 464 | TEST_P(VibratorAidl, VibrationSessionWithMultipleIndependentVibrations) { |
| 465 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 466 | if (vibratorIds.empty()) return; |
| 467 | |
| 468 | std::promise<void> sessionPromise; |
| 469 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 470 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 471 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 472 | |
| 473 | VibrationSessionConfig sessionConfig; |
| 474 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 475 | ASSERT_NE(session, nullptr); |
| 476 | |
| 477 | for (int32_t id : vibratorIds) { |
| 478 | std::shared_ptr<IVibrator> vibrator; |
| 479 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 480 | ASSERT_NE(vibrator, nullptr); |
| 481 | |
| 482 | EXPECT_OK(vibrator->on(100, nullptr)); |
| 483 | EXPECT_OK(vibrator->on(200, nullptr)); |
| 484 | EXPECT_OK(vibrator->on(300, nullptr)); |
| 485 | } |
| 486 | |
| 487 | // Session callback not triggered. |
| 488 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 489 | |
| 490 | EXPECT_OK(session->close()); |
| 491 | |
| 492 | int32_t maxDurationMs = 100 + 200 + 300; |
| 493 | auto timeout = std::chrono::milliseconds(maxDurationMs) + VIBRATION_CALLBACK_TIMEOUT; |
| 494 | EXPECT_EQ(sessionFuture.wait_for(timeout), std::future_status::ready); |
| 495 | } |
| 496 | |
| 497 | TEST_P(VibratorAidl, VibrationSessionsIgnoresSecondSessionWhenFirstIsOngoing) { |
| 498 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 499 | if (vibratorIds.empty()) return; |
| 500 | |
| 501 | std::promise<void> sessionPromise; |
| 502 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 503 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 504 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 505 | |
| 506 | VibrationSessionConfig sessionConfig; |
| 507 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 508 | ASSERT_NE(session, nullptr); |
| 509 | |
| 510 | std::shared_ptr<IVibrationSession> secondSession; |
| 511 | EXPECT_ILLEGAL_STATE( |
| 512 | manager->startSession(vibratorIds, sessionConfig, nullptr, &secondSession)); |
| 513 | EXPECT_EQ(secondSession, nullptr); |
| 514 | |
| 515 | // First session was not cancelled. |
| 516 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 517 | |
| 518 | // First session still ongoing, we can still vibrate. |
| 519 | int32_t durationMs = 100; |
| 520 | for (int32_t id : vibratorIds) { |
| 521 | std::shared_ptr<IVibrator> vibrator; |
| 522 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 523 | ASSERT_NE(vibrator, nullptr); |
| 524 | EXPECT_OK(vibrator->on(durationMs, nullptr)); |
| 525 | } |
| 526 | |
| 527 | EXPECT_OK(session->close()); |
| 528 | |
| 529 | auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT; |
| 530 | EXPECT_EQ(sessionFuture.wait_for(timeout), std::future_status::ready); |
| 531 | } |
| 532 | |
| 533 | TEST_P(VibratorAidl, VibrationSessionEndMultipleTimes) { |
| 534 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 535 | if (vibratorIds.empty()) return; |
| 536 | |
| 537 | std::promise<void> sessionPromise; |
| 538 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 539 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 540 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 541 | |
| 542 | VibrationSessionConfig sessionConfig; |
| 543 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 544 | ASSERT_NE(session, nullptr); |
| 545 | |
| 546 | int32_t durationMs = 250; |
| 547 | std::vector<std::promise<void>> vibrationPromises; |
| 548 | std::vector<std::future<void>> vibrationFutures; |
| 549 | for (int32_t id : vibratorIds) { |
| 550 | std::shared_ptr<IVibrator> vibrator; |
| 551 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 552 | ASSERT_NE(vibrator, nullptr); |
| 553 | |
| 554 | std::promise<void>& vibrationPromise = vibrationPromises.emplace_back(); |
| 555 | vibrationFutures.push_back(vibrationPromise.get_future()); |
| 556 | auto vibrationCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 557 | [&vibrationPromise] { vibrationPromise.set_value(); }); |
| 558 | EXPECT_OK(vibrator->on(durationMs, vibrationCallback)); |
| 559 | } |
| 560 | |
| 561 | // Session callback not triggered. |
| 562 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 563 | |
| 564 | // End session, this might take a while |
| 565 | EXPECT_OK(session->close()); |
| 566 | |
| 567 | // End session again |
| 568 | EXPECT_OK(session->close()); |
| 569 | |
| 570 | // Both callbacks triggered within timeout. |
| 571 | auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT; |
| 572 | EXPECT_EQ(sessionFuture.wait_for(timeout), std::future_status::ready); |
| 573 | for (std::future<void>& future : vibrationFutures) { |
| 574 | EXPECT_EQ(future.wait_for(timeout), std::future_status::ready); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | TEST_P(VibratorAidl, VibrationSessionDeletedAfterEnded) { |
| 579 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 580 | if (vibratorIds.empty()) return; |
| 581 | |
| 582 | std::promise<void> sessionPromise; |
| 583 | std::future<void> sessionFuture{sessionPromise.get_future()}; |
| 584 | auto sessionCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 585 | [&sessionPromise] { sessionPromise.set_value(); }); |
| 586 | |
| 587 | VibrationSessionConfig sessionConfig; |
| 588 | EXPECT_OK(manager->startSession(vibratorIds, sessionConfig, sessionCallback, &session)); |
| 589 | ASSERT_NE(session, nullptr); |
| 590 | |
| 591 | int32_t durationMs = 250; |
| 592 | std::vector<std::promise<void>> vibrationPromises; |
| 593 | std::vector<std::future<void>> vibrationFutures; |
| 594 | for (int32_t id : vibratorIds) { |
| 595 | std::shared_ptr<IVibrator> vibrator; |
| 596 | EXPECT_OK(manager->getVibrator(id, &vibrator)); |
| 597 | ASSERT_NE(vibrator, nullptr); |
| 598 | |
| 599 | std::promise<void>& vibrationPromise = vibrationPromises.emplace_back(); |
| 600 | vibrationFutures.push_back(vibrationPromise.get_future()); |
| 601 | auto vibrationCallback = ndk::SharedRefBase::make<CompletionCallback>( |
| 602 | [&vibrationPromise] { vibrationPromise.set_value(); }); |
| 603 | EXPECT_OK(vibrator->on(durationMs, vibrationCallback)); |
| 604 | } |
| 605 | |
| 606 | // Session callback not triggered. |
| 607 | EXPECT_EQ(sessionFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT), std::future_status::timeout); |
| 608 | |
| 609 | // End session, this might take a while |
| 610 | EXPECT_OK(session->close()); |
| 611 | |
| 612 | session.reset(); |
| 613 | |
| 614 | // Both callbacks triggered within timeout, even after session was deleted. |
| 615 | auto timeout = std::chrono::milliseconds(durationMs) + VIBRATION_CALLBACK_TIMEOUT; |
| 616 | EXPECT_EQ(sessionFuture.wait_for(timeout), std::future_status::ready); |
| 617 | for (std::future<void>& future : vibrationFutures) { |
| 618 | EXPECT_EQ(future.wait_for(timeout), std::future_status::ready); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | TEST_P(VibratorAidl, VibrationSessionWrongVibratorIdsFail) { |
| 623 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 624 | |
| 625 | auto maxIdIt = std::max_element(vibratorIds.begin(), vibratorIds.end()); |
| 626 | int32_t wrongId = maxIdIt == vibratorIds.end() ? 0 : *maxIdIt + 1; |
| 627 | |
| 628 | std::vector<int32_t> emptyIds; |
| 629 | std::vector<int32_t> wrongIds{wrongId}; |
| 630 | VibrationSessionConfig sessionConfig; |
| 631 | EXPECT_ILLEGAL_ARGUMENT(manager->startSession(emptyIds, sessionConfig, nullptr, &session)); |
| 632 | EXPECT_ILLEGAL_ARGUMENT(manager->startSession(wrongIds, sessionConfig, nullptr, &session)); |
| 633 | EXPECT_EQ(session, nullptr); |
| 634 | } |
| 635 | |
| 636 | TEST_P(VibratorAidl, VibrationSessionDuringPrepareSyncedFails) { |
| 637 | if (!(capabilities & IVibratorManager::CAP_SYNC)) return; |
| 638 | if (!(capabilities & IVibratorManager::CAP_START_SESSIONS)) return; |
| 639 | if (vibratorIds.empty()) return; |
| 640 | |
| 641 | EXPECT_OK(manager->prepareSynced(vibratorIds)); |
| 642 | |
| 643 | VibrationSessionConfig sessionConfig; |
| 644 | EXPECT_ILLEGAL_STATE(manager->startSession(vibratorIds, sessionConfig, nullptr, &session)); |
| 645 | EXPECT_EQ(session, nullptr); |
| 646 | |
| 647 | EXPECT_OK(manager->cancelSynced()); |
| 648 | } |
| 649 | |
| 650 | TEST_P(VibratorAidl, VibrationSessionsUnsupported) { |
| 651 | if (version < VIBRATION_SESSIONS_MIN_VERSION) { |
| 652 | EXPECT_EQ(capabilities & IVibratorManager::CAP_START_SESSIONS, 0) |
| 653 | << "Vibrator manager version " << version |
| 654 | << " should not report start session capability"; |
| 655 | } |
| 656 | if (capabilities & IVibratorManager::CAP_START_SESSIONS) return; |
| 657 | |
| 658 | VibrationSessionConfig sessionConfig; |
| 659 | EXPECT_UNKNOWN_OR_UNSUPPORTED( |
| 660 | manager->startSession(vibratorIds, sessionConfig, nullptr, &session)); |
| 661 | EXPECT_EQ(session, nullptr); |
| 662 | EXPECT_UNKNOWN_OR_UNSUPPORTED(manager->clearSessions()); |
| 663 | } |
| 664 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 665 | std::vector<std::string> FindVibratorManagerNames() { |
| 666 | std::vector<std::string> names; |
| 667 | constexpr auto callback = [](const char* instance, void* context) { |
| 668 | std::string fullName = std::string(IVibratorManager::descriptor) + "/" + instance; |
| 669 | static_cast<std::vector<std::string>*>(context)->emplace_back(fullName); |
| 670 | }; |
| 671 | AServiceManager_forEachDeclaredInstance(IVibratorManager::descriptor, |
| 672 | static_cast<void*>(&names), callback); |
| 673 | return names; |
| 674 | } |
| 675 | |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 676 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 677 | INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(FindVibratorManagerNames()), |
| 678 | android::PrintInstanceNameToString); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 679 | |
| 680 | int main(int argc, char** argv) { |
| 681 | ::testing::InitGoogleTest(&argc, argv); |
Lais Andrade | f1120b1 | 2024-08-23 15:12:12 +0100 | [diff] [blame] | 682 | ABinderProcess_setThreadPoolMaxThreadCount(2); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 683 | ABinderProcess_startThreadPool(); |
Lais Andrade | 80b1861 | 2020-10-12 18:44:40 +0000 | [diff] [blame] | 684 | return RUN_ALL_TESTS(); |
| 685 | } |