blob: b46055ed1c778fe671428a75bdd00b50dce32f4c [file] [log] [blame]
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001/*
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
22namespace android::inputdispatcher {
23
24namespace {
25
Prabir Pradhana2d3cf12024-02-05 23:02:01 +000026// 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.
30constexpr auto TRACE_TIMEOUT = std::chrono::seconds(5);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000031
32base::ResultError<> error(const std::ostringstream& ss) {
33 return base::ResultError(ss.str(), BAD_VALUE);
34}
35
Prabir Pradhan4497c862023-12-15 07:13:30 +000036inline auto getId(const trace::TracedEvent& v) {
37 return std::visit([](const auto& event) { return event.id; }, v);
38}
39
Prabir Pradhan4b408be2024-02-21 19:19:47 +000040MotionEvent toInputEvent(const trace::TracedMotionEvent& e,
41 const trace::WindowDispatchArgs& dispatchArgs,
42 const std::array<uint8_t, 32>& hmac) {
Prabir Pradhan65a071a2024-01-05 20:52:09 +000043 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 Pradhan4b408be2024-02-21 19:19:47 +000053KeyEvent toInputEvent(const trace::TracedKeyEvent& e, const trace::WindowDispatchArgs& dispatchArgs,
Prabir Pradhan65a071a2024-01-05 20:52:09 +000054 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 Pradhan560d0d12024-03-07 18:08:27 +000057 dispatchArgs.resolvedFlags, e.keyCode, e.scanCode, e.metaState,
58 dispatchArgs.resolvedKeyRepeatCount, e.downTime, e.eventTime);
Prabir Pradhan65a071a2024-01-05 20:52:09 +000059 return traced;
60}
61
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000062} // namespace
63
64// --- VerifyingTrace ---
65
Prabir Pradhan4497c862023-12-15 07:13:30 +000066void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId) {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000067 std::scoped_lock lock(mLock);
Prabir Pradhan4497c862023-12-15 07:13:30 +000068 mExpectedEvents.emplace_back(event, windowId);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000069}
70
Prabir Pradhan4497c862023-12-15 07:13:30 +000071void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId) {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000072 std::scoped_lock lock(mLock);
Prabir Pradhan4497c862023-12-15 07:13:30 +000073 mExpectedEvents.emplace_back(event, windowId);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000074}
75
76void VerifyingTrace::verifyExpectedEventsTraced() {
77 std::unique_lock lock(mLock);
78 base::ScopedLockAssertion assumeLocked(mLock);
79
Prabir Pradhan65a071a2024-01-05 20:52:09 +000080 // Poll for all expected events to be traced, and keep track of the latest poll result.
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000081 base::Result<void> result;
82 mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) {
Prabir Pradhan4497c862023-12-15 07:13:30 +000083 for (const auto& [expectedEvent, windowId] : mExpectedEvents) {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000084 std::visit([&](const auto& event)
Prabir Pradhan4497c862023-12-15 07:13:30 +000085 REQUIRES(mLock) { result = verifyEventTraced(event, windowId); },
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000086 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
99void VerifyingTrace::reset() {
100 std::scoped_lock lock(mLock);
101 mTracedEvents.clear();
Prabir Pradhan4497c862023-12-15 07:13:30 +0000102 mTracedWindowDispatches.clear();
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000103 mExpectedEvents.clear();
104}
105
106template <typename Event>
Prabir Pradhan4497c862023-12-15 07:13:30 +0000107base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent,
108 int32_t expectedWindowId) const {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000109 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 Pradhan4497c862023-12-15 07:13:30 +0000119 auto tracedDispatchesIt =
120 std::find_if(mTracedWindowDispatches.begin(), mTracedWindowDispatches.end(),
Prabir Pradhan4b408be2024-02-21 19:19:47 +0000121 [&](const trace::WindowDispatchArgs& args) {
Prabir Pradhan4497c862023-12-15 07:13:30 +0000122 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 Pradhan65a071a2024-01-05 20:52:09 +0000127 << " to window with ID 0x" << expectedWindowId << " to be traced, but it was not.\n"
128 << "Expected event: " << expectedEvent;
Prabir Pradhan4497c862023-12-15 07:13:30 +0000129 return error(msg);
130 }
131
Prabir Pradhan65a071a2024-01-05 20:52:09 +0000132 // 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 Pradhandc3a2ad2024-02-05 19:03:51 +0000160}
161
162// --- FakeInputTracingBackend ---
163
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000164void FakeInputTracingBackend::traceKeyEvent(const trace::TracedKeyEvent& event,
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +0000165 const trace::TracedEventMetadata&) {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000166 {
167 std::scoped_lock lock(mTrace->mLock);
Prabir Pradhan65a071a2024-01-05 20:52:09 +0000168 mTrace->mTracedEvents.emplace(event.id, event);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000169 }
170 mTrace->mEventTracedCondition.notify_all();
171}
172
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000173void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& event,
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +0000174 const trace::TracedEventMetadata&) {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000175 {
176 std::scoped_lock lock(mTrace->mLock);
Prabir Pradhan65a071a2024-01-05 20:52:09 +0000177 mTrace->mTracedEvents.emplace(event.id, event);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000178 }
179 mTrace->mEventTracedCondition.notify_all();
180}
181
Prabir Pradhan4b408be2024-02-21 19:19:47 +0000182void FakeInputTracingBackend::traceWindowDispatch(const trace::WindowDispatchArgs& args,
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +0000183 const trace::TracedEventMetadata&) {
Prabir Pradhan4497c862023-12-15 07:13:30 +0000184 {
185 std::scoped_lock lock(mTrace->mLock);
186 mTrace->mTracedWindowDispatches.push_back(args);
187 }
188 mTrace->mEventTracedCondition.notify_all();
189}
190
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000191} // namespace android::inputdispatcher