Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 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 | |
| 17 | #include "FakeInputTracingBackend.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <utils/Errors.h> |
| 21 | |
| 22 | namespace android::inputdispatcher { |
| 23 | |
| 24 | namespace { |
| 25 | |
Prabir Pradhan | a2d3cf1 | 2024-02-05 23:02:01 +0000 | [diff] [blame] | 26 | // Use a larger timeout while waiting for events to be traced, compared to the timeout used while |
| 27 | // waiting to receive events through the input channel. Events are traced from a separate thread, |
| 28 | // which does not have the same high thread priority as the InputDispatcher's thread, so the tracer |
| 29 | // is expected to lag behind the Dispatcher at times. |
| 30 | constexpr auto TRACE_TIMEOUT = std::chrono::seconds(5); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 31 | |
| 32 | base::ResultError<> error(const std::ostringstream& ss) { |
| 33 | return base::ResultError(ss.str(), BAD_VALUE); |
| 34 | } |
| 35 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 36 | inline auto getId(const trace::TracedEvent& v) { |
| 37 | return std::visit([](const auto& event) { return event.id; }, v); |
| 38 | } |
| 39 | |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 40 | MotionEvent toInputEvent( |
| 41 | const trace::TracedMotionEvent& e, |
| 42 | const trace::InputTracingBackendInterface::WindowDispatchArgs& dispatchArgs, |
| 43 | const std::array<uint8_t, 32>& hmac) { |
| 44 | MotionEvent traced; |
| 45 | traced.initialize(e.id, e.deviceId, e.source, e.displayId, hmac, e.action, e.actionButton, |
| 46 | dispatchArgs.resolvedFlags, e.edgeFlags, e.metaState, e.buttonState, |
| 47 | e.classification, dispatchArgs.transform, e.xPrecision, e.yPrecision, |
| 48 | e.xCursorPosition, e.yCursorPosition, dispatchArgs.rawTransform, e.downTime, |
| 49 | e.eventTime, e.pointerProperties.size(), e.pointerProperties.data(), |
| 50 | e.pointerCoords.data()); |
| 51 | return traced; |
| 52 | } |
| 53 | |
| 54 | KeyEvent toInputEvent(const trace::TracedKeyEvent& e, |
| 55 | const trace::InputTracingBackendInterface::WindowDispatchArgs& dispatchArgs, |
| 56 | const std::array<uint8_t, 32>& hmac) { |
| 57 | KeyEvent traced; |
| 58 | traced.initialize(e.id, e.deviceId, e.source, e.displayId, hmac, e.action, |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame^] | 59 | dispatchArgs.resolvedFlags, e.keyCode, e.scanCode, e.metaState, |
| 60 | dispatchArgs.resolvedKeyRepeatCount, e.downTime, e.eventTime); |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 61 | return traced; |
| 62 | } |
| 63 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 64 | } // namespace |
| 65 | |
| 66 | // --- VerifyingTrace --- |
| 67 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 68 | void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 69 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 70 | mExpectedEvents.emplace_back(event, windowId); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 73 | void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 74 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 75 | mExpectedEvents.emplace_back(event, windowId); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void VerifyingTrace::verifyExpectedEventsTraced() { |
| 79 | std::unique_lock lock(mLock); |
| 80 | base::ScopedLockAssertion assumeLocked(mLock); |
| 81 | |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 82 | // Poll for all expected events to be traced, and keep track of the latest poll result. |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 83 | base::Result<void> result; |
| 84 | mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 85 | for (const auto& [expectedEvent, windowId] : mExpectedEvents) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 86 | std::visit([&](const auto& event) |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 87 | REQUIRES(mLock) { result = verifyEventTraced(event, windowId); }, |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 88 | expectedEvent); |
| 89 | if (!result.ok()) { |
| 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | return true; |
| 94 | }); |
| 95 | |
| 96 | EXPECT_TRUE(result.ok()) |
| 97 | << "Timed out waiting for all expected events to be traced successfully: " |
| 98 | << result.error().message(); |
| 99 | } |
| 100 | |
| 101 | void VerifyingTrace::reset() { |
| 102 | std::scoped_lock lock(mLock); |
| 103 | mTracedEvents.clear(); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 104 | mTracedWindowDispatches.clear(); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 105 | mExpectedEvents.clear(); |
| 106 | } |
| 107 | |
| 108 | template <typename Event> |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 109 | base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent, |
| 110 | int32_t expectedWindowId) const { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 111 | std::ostringstream msg; |
| 112 | |
| 113 | auto tracedEventsIt = mTracedEvents.find(expectedEvent.getId()); |
| 114 | if (tracedEventsIt == mTracedEvents.end()) { |
| 115 | msg << "Expected event with ID 0x" << std::hex << expectedEvent.getId() |
| 116 | << " to be traced, but it was not.\n" |
| 117 | << "Expected event: " << expectedEvent; |
| 118 | return error(msg); |
| 119 | } |
| 120 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 121 | auto tracedDispatchesIt = |
| 122 | std::find_if(mTracedWindowDispatches.begin(), mTracedWindowDispatches.end(), |
| 123 | [&](const WindowDispatchArgs& args) { |
| 124 | return args.windowId == expectedWindowId && |
| 125 | getId(args.eventEntry) == expectedEvent.getId(); |
| 126 | }); |
| 127 | if (tracedDispatchesIt == mTracedWindowDispatches.end()) { |
| 128 | msg << "Expected dispatch of event with ID 0x" << std::hex << expectedEvent.getId() |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 129 | << " to window with ID 0x" << expectedWindowId << " to be traced, but it was not.\n" |
| 130 | << "Expected event: " << expectedEvent; |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 131 | return error(msg); |
| 132 | } |
| 133 | |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 134 | // Verify that the traced event matches the expected event exactly. |
| 135 | return std::visit( |
| 136 | [&](const auto& traced) -> base::Result<void> { |
| 137 | Event tracedEvent; |
| 138 | using T = std::decay_t<decltype(traced)>; |
| 139 | if constexpr (std::is_same_v<Event, MotionEvent> && |
| 140 | std::is_same_v<T, trace::TracedMotionEvent>) { |
| 141 | tracedEvent = |
| 142 | toInputEvent(traced, *tracedDispatchesIt, expectedEvent.getHmac()); |
| 143 | } else if constexpr (std::is_same_v<Event, KeyEvent> && |
| 144 | std::is_same_v<T, trace::TracedKeyEvent>) { |
| 145 | tracedEvent = |
| 146 | toInputEvent(traced, *tracedDispatchesIt, expectedEvent.getHmac()); |
| 147 | } else { |
| 148 | msg << "Received the wrong event type!\n" |
| 149 | << "Expected event: " << expectedEvent; |
| 150 | return error(msg); |
| 151 | } |
| 152 | |
| 153 | const auto result = testing::internal::CmpHelperEQ("expectedEvent", "tracedEvent", |
| 154 | expectedEvent, tracedEvent); |
| 155 | if (!result) { |
| 156 | msg << result.failure_message(); |
| 157 | return error(msg); |
| 158 | } |
| 159 | return {}; |
| 160 | }, |
| 161 | tracedEventsIt->second); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // --- FakeInputTracingBackend --- |
| 165 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 166 | void FakeInputTracingBackend::traceKeyEvent(const trace::TracedKeyEvent& event) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 167 | { |
| 168 | std::scoped_lock lock(mTrace->mLock); |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 169 | mTrace->mTracedEvents.emplace(event.id, event); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 170 | } |
| 171 | mTrace->mEventTracedCondition.notify_all(); |
| 172 | } |
| 173 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 174 | void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& event) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 175 | { |
| 176 | std::scoped_lock lock(mTrace->mLock); |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 177 | mTrace->mTracedEvents.emplace(event.id, event); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 178 | } |
| 179 | mTrace->mEventTracedCondition.notify_all(); |
| 180 | } |
| 181 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 182 | void FakeInputTracingBackend::traceWindowDispatch(const WindowDispatchArgs& args) { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 183 | { |
| 184 | std::scoped_lock lock(mTrace->mLock); |
| 185 | mTrace->mTracedWindowDispatches.push_back(args); |
| 186 | } |
| 187 | mTrace->mEventTracedCondition.notify_all(); |
| 188 | } |
| 189 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 190 | } // namespace android::inputdispatcher |