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 | 4b408be | 2024-02-21 19:19:47 +0000 | [diff] [blame] | 40 | MotionEvent toInputEvent(const trace::TracedMotionEvent& e, |
| 41 | const trace::WindowDispatchArgs& dispatchArgs, |
| 42 | const std::array<uint8_t, 32>& hmac) { |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 43 | MotionEvent traced; |
| 44 | traced.initialize(e.id, e.deviceId, e.source, e.displayId, hmac, e.action, e.actionButton, |
| 45 | dispatchArgs.resolvedFlags, e.edgeFlags, e.metaState, e.buttonState, |
| 46 | e.classification, dispatchArgs.transform, e.xPrecision, e.yPrecision, |
| 47 | e.xCursorPosition, e.yCursorPosition, dispatchArgs.rawTransform, e.downTime, |
| 48 | e.eventTime, e.pointerProperties.size(), e.pointerProperties.data(), |
| 49 | e.pointerCoords.data()); |
| 50 | return traced; |
| 51 | } |
| 52 | |
Prabir Pradhan | 4b408be | 2024-02-21 19:19:47 +0000 | [diff] [blame] | 53 | KeyEvent toInputEvent(const trace::TracedKeyEvent& e, const trace::WindowDispatchArgs& dispatchArgs, |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 54 | const std::array<uint8_t, 32>& hmac) { |
| 55 | KeyEvent traced; |
| 56 | 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] | 57 | dispatchArgs.resolvedFlags, e.keyCode, e.scanCode, e.metaState, |
| 58 | dispatchArgs.resolvedKeyRepeatCount, e.downTime, e.eventTime); |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 59 | return traced; |
| 60 | } |
| 61 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 62 | } // namespace |
| 63 | |
| 64 | // --- VerifyingTrace --- |
| 65 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 66 | void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 67 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 68 | mExpectedEvents.emplace_back(event, windowId); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 71 | void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 72 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 73 | mExpectedEvents.emplace_back(event, windowId); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void VerifyingTrace::verifyExpectedEventsTraced() { |
| 77 | std::unique_lock lock(mLock); |
| 78 | base::ScopedLockAssertion assumeLocked(mLock); |
| 79 | |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 80 | // 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] | 81 | base::Result<void> result; |
| 82 | mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 83 | for (const auto& [expectedEvent, windowId] : mExpectedEvents) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 84 | std::visit([&](const auto& event) |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 85 | REQUIRES(mLock) { result = verifyEventTraced(event, windowId); }, |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 86 | expectedEvent); |
| 87 | if (!result.ok()) { |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | return true; |
| 92 | }); |
| 93 | |
| 94 | EXPECT_TRUE(result.ok()) |
| 95 | << "Timed out waiting for all expected events to be traced successfully: " |
| 96 | << result.error().message(); |
| 97 | } |
| 98 | |
| 99 | void VerifyingTrace::reset() { |
| 100 | std::scoped_lock lock(mLock); |
| 101 | mTracedEvents.clear(); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 102 | mTracedWindowDispatches.clear(); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 103 | mExpectedEvents.clear(); |
| 104 | } |
| 105 | |
| 106 | template <typename Event> |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 107 | base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent, |
| 108 | int32_t expectedWindowId) const { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 109 | std::ostringstream msg; |
| 110 | |
| 111 | auto tracedEventsIt = mTracedEvents.find(expectedEvent.getId()); |
| 112 | if (tracedEventsIt == mTracedEvents.end()) { |
| 113 | msg << "Expected event with ID 0x" << std::hex << expectedEvent.getId() |
| 114 | << " to be traced, but it was not.\n" |
| 115 | << "Expected event: " << expectedEvent; |
| 116 | return error(msg); |
| 117 | } |
| 118 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 119 | auto tracedDispatchesIt = |
| 120 | std::find_if(mTracedWindowDispatches.begin(), mTracedWindowDispatches.end(), |
Prabir Pradhan | 4b408be | 2024-02-21 19:19:47 +0000 | [diff] [blame] | 121 | [&](const trace::WindowDispatchArgs& args) { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 122 | return args.windowId == expectedWindowId && |
| 123 | getId(args.eventEntry) == expectedEvent.getId(); |
| 124 | }); |
| 125 | if (tracedDispatchesIt == mTracedWindowDispatches.end()) { |
| 126 | msg << "Expected dispatch of event with ID 0x" << std::hex << expectedEvent.getId() |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 127 | << " to window with ID 0x" << expectedWindowId << " to be traced, but it was not.\n" |
| 128 | << "Expected event: " << expectedEvent; |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 129 | return error(msg); |
| 130 | } |
| 131 | |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 132 | // Verify that the traced event matches the expected event exactly. |
| 133 | return std::visit( |
| 134 | [&](const auto& traced) -> base::Result<void> { |
| 135 | Event tracedEvent; |
| 136 | using T = std::decay_t<decltype(traced)>; |
| 137 | if constexpr (std::is_same_v<Event, MotionEvent> && |
| 138 | std::is_same_v<T, trace::TracedMotionEvent>) { |
| 139 | tracedEvent = |
| 140 | toInputEvent(traced, *tracedDispatchesIt, expectedEvent.getHmac()); |
| 141 | } else if constexpr (std::is_same_v<Event, KeyEvent> && |
| 142 | std::is_same_v<T, trace::TracedKeyEvent>) { |
| 143 | tracedEvent = |
| 144 | toInputEvent(traced, *tracedDispatchesIt, expectedEvent.getHmac()); |
| 145 | } else { |
| 146 | msg << "Received the wrong event type!\n" |
| 147 | << "Expected event: " << expectedEvent; |
| 148 | return error(msg); |
| 149 | } |
| 150 | |
| 151 | const auto result = testing::internal::CmpHelperEQ("expectedEvent", "tracedEvent", |
| 152 | expectedEvent, tracedEvent); |
| 153 | if (!result) { |
| 154 | msg << result.failure_message(); |
| 155 | return error(msg); |
| 156 | } |
| 157 | return {}; |
| 158 | }, |
| 159 | tracedEventsIt->second); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // --- FakeInputTracingBackend --- |
| 163 | |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 164 | void FakeInputTracingBackend::traceKeyEvent(const trace::TracedKeyEvent& event, |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 165 | const trace::TracedEventMetadata&) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 166 | { |
| 167 | std::scoped_lock lock(mTrace->mLock); |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 168 | mTrace->mTracedEvents.emplace(event.id, event); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 169 | } |
| 170 | mTrace->mEventTracedCondition.notify_all(); |
| 171 | } |
| 172 | |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 173 | void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& event, |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 174 | const trace::TracedEventMetadata&) { |
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 | 4b408be | 2024-02-21 19:19:47 +0000 | [diff] [blame] | 182 | void FakeInputTracingBackend::traceWindowDispatch(const trace::WindowDispatchArgs& args, |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 183 | const trace::TracedEventMetadata&) { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 184 | { |
| 185 | std::scoped_lock lock(mTrace->mLock); |
| 186 | mTrace->mTracedWindowDispatches.push_back(args); |
| 187 | } |
| 188 | mTrace->mEventTracedCondition.notify_all(); |
| 189 | } |
| 190 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 191 | } // namespace android::inputdispatcher |