blob: b7af356ebaac0abfed3818f9a17041a23af91822 [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 Pradhan65a071a2024-01-05 20:52:09 +000040MotionEvent 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
54KeyEvent 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,
59 dispatchArgs.resolvedFlags, e.keyCode, e.scanCode, e.metaState, e.repeatCount,
60 e.downTime, e.eventTime);
61 return traced;
62}
63
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000064} // namespace
65
66// --- VerifyingTrace ---
67
Prabir Pradhan4497c862023-12-15 07:13:30 +000068void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId) {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000069 std::scoped_lock lock(mLock);
Prabir Pradhan4497c862023-12-15 07:13:30 +000070 mExpectedEvents.emplace_back(event, windowId);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000071}
72
Prabir Pradhan4497c862023-12-15 07:13:30 +000073void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId) {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000074 std::scoped_lock lock(mLock);
Prabir Pradhan4497c862023-12-15 07:13:30 +000075 mExpectedEvents.emplace_back(event, windowId);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000076}
77
78void VerifyingTrace::verifyExpectedEventsTraced() {
79 std::unique_lock lock(mLock);
80 base::ScopedLockAssertion assumeLocked(mLock);
81
Prabir Pradhan65a071a2024-01-05 20:52:09 +000082 // Poll for all expected events to be traced, and keep track of the latest poll result.
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000083 base::Result<void> result;
84 mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) {
Prabir Pradhan4497c862023-12-15 07:13:30 +000085 for (const auto& [expectedEvent, windowId] : mExpectedEvents) {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000086 std::visit([&](const auto& event)
Prabir Pradhan4497c862023-12-15 07:13:30 +000087 REQUIRES(mLock) { result = verifyEventTraced(event, windowId); },
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000088 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
101void VerifyingTrace::reset() {
102 std::scoped_lock lock(mLock);
103 mTracedEvents.clear();
Prabir Pradhan4497c862023-12-15 07:13:30 +0000104 mTracedWindowDispatches.clear();
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000105 mExpectedEvents.clear();
106}
107
108template <typename Event>
Prabir Pradhan4497c862023-12-15 07:13:30 +0000109base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent,
110 int32_t expectedWindowId) const {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000111 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 Pradhan4497c862023-12-15 07:13:30 +0000121 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 Pradhan65a071a2024-01-05 20:52:09 +0000129 << " to window with ID 0x" << expectedWindowId << " to be traced, but it was not.\n"
130 << "Expected event: " << expectedEvent;
Prabir Pradhan4497c862023-12-15 07:13:30 +0000131 return error(msg);
132 }
133
Prabir Pradhan65a071a2024-01-05 20:52:09 +0000134 // 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 Pradhandc3a2ad2024-02-05 19:03:51 +0000162}
163
164// --- FakeInputTracingBackend ---
165
166void FakeInputTracingBackend::traceKeyEvent(const trace::TracedKeyEvent& event) const {
167 {
168 std::scoped_lock lock(mTrace->mLock);
Prabir Pradhan65a071a2024-01-05 20:52:09 +0000169 mTrace->mTracedEvents.emplace(event.id, event);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000170 }
171 mTrace->mEventTracedCondition.notify_all();
172}
173
174void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& event) const {
175 {
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 Pradhan4497c862023-12-15 07:13:30 +0000182void FakeInputTracingBackend::traceWindowDispatch(const WindowDispatchArgs& args) const {
183 {
184 std::scoped_lock lock(mTrace->mLock);
185 mTrace->mTracedWindowDispatches.push_back(args);
186 }
187 mTrace->mEventTracedCondition.notify_all();
188}
189
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000190} // namespace android::inputdispatcher