Skip to content

Commit 68a7cae

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "InputDispatcher_test: Verify all events dispatched to windows are traced" into main
2 parents 0b63b2a + 4497c86 commit 68a7cae

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

services/inputflinger/tests/FakeInputTracingBackend.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,22 @@ base::ResultError<> error(const std::ostringstream& ss) {
3333
return base::ResultError(ss.str(), BAD_VALUE);
3434
}
3535

36+
inline auto getId(const trace::TracedEvent& v) {
37+
return std::visit([](const auto& event) { return event.id; }, v);
38+
}
39+
3640
} // namespace
3741

3842
// --- VerifyingTrace ---
3943

40-
void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event) {
44+
void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId) {
4145
std::scoped_lock lock(mLock);
42-
mExpectedEvents.emplace_back(event);
46+
mExpectedEvents.emplace_back(event, windowId);
4347
}
4448

45-
void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event) {
49+
void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId) {
4650
std::scoped_lock lock(mLock);
47-
mExpectedEvents.emplace_back(event);
51+
mExpectedEvents.emplace_back(event, windowId);
4852
}
4953

5054
void VerifyingTrace::verifyExpectedEventsTraced() {
@@ -53,9 +57,9 @@ void VerifyingTrace::verifyExpectedEventsTraced() {
5357

5458
base::Result<void> result;
5559
mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) {
56-
for (const auto& expectedEvent : mExpectedEvents) {
60+
for (const auto& [expectedEvent, windowId] : mExpectedEvents) {
5761
std::visit([&](const auto& event)
58-
REQUIRES(mLock) { result = verifyEventTraced(event); },
62+
REQUIRES(mLock) { result = verifyEventTraced(event, windowId); },
5963
expectedEvent);
6064
if (!result.ok()) {
6165
return false;
@@ -72,11 +76,13 @@ void VerifyingTrace::verifyExpectedEventsTraced() {
7276
void VerifyingTrace::reset() {
7377
std::scoped_lock lock(mLock);
7478
mTracedEvents.clear();
79+
mTracedWindowDispatches.clear();
7580
mExpectedEvents.clear();
7681
}
7782

7883
template <typename Event>
79-
base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent) const {
84+
base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent,
85+
int32_t expectedWindowId) const {
8086
std::ostringstream msg;
8187

8288
auto tracedEventsIt = mTracedEvents.find(expectedEvent.getId());
@@ -87,6 +93,19 @@ base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent)
8793
return error(msg);
8894
}
8995

96+
auto tracedDispatchesIt =
97+
std::find_if(mTracedWindowDispatches.begin(), mTracedWindowDispatches.end(),
98+
[&](const WindowDispatchArgs& args) {
99+
return args.windowId == expectedWindowId &&
100+
getId(args.eventEntry) == expectedEvent.getId();
101+
});
102+
if (tracedDispatchesIt == mTracedWindowDispatches.end()) {
103+
msg << "Expected dispatch of event with ID 0x" << std::hex << expectedEvent.getId()
104+
<< " to window with ID 0x" << expectedWindowId << " to be traced, but it was not."
105+
<< "\nExpected event: " << expectedEvent;
106+
return error(msg);
107+
}
108+
90109
return {};
91110
}
92111

@@ -108,4 +127,12 @@ void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& e
108127
mTrace->mEventTracedCondition.notify_all();
109128
}
110129

130+
void FakeInputTracingBackend::traceWindowDispatch(const WindowDispatchArgs& args) const {
131+
{
132+
std::scoped_lock lock(mTrace->mLock);
133+
mTrace->mTracedWindowDispatches.push_back(args);
134+
}
135+
mTrace->mEventTracedCondition.notify_all();
136+
}
137+
111138
} // namespace android::inputdispatcher

services/inputflinger/tests/FakeInputTracingBackend.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class VerifyingTrace {
3939
VerifyingTrace() = default;
4040

4141
/** Add an expectation for a key event to be traced. */
42-
void expectKeyDispatchTraced(const KeyEvent& event);
42+
void expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId);
4343

4444
/** Add an expectation for a motion event to be traced. */
45-
void expectMotionDispatchTraced(const MotionEvent& event);
45+
void expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId);
4646

4747
/**
4848
* Wait and verify that all expected events are traced.
@@ -59,14 +59,17 @@ class VerifyingTrace {
5959
std::mutex mLock;
6060
std::condition_variable mEventTracedCondition;
6161
std::unordered_set<uint32_t /*eventId*/> mTracedEvents GUARDED_BY(mLock);
62-
std::vector<std::variant<KeyEvent, MotionEvent>> mExpectedEvents GUARDED_BY(mLock);
62+
using WindowDispatchArgs = trace::InputTracingBackendInterface::WindowDispatchArgs;
63+
std::vector<WindowDispatchArgs> mTracedWindowDispatches GUARDED_BY(mLock);
64+
std::vector<std::pair<std::variant<KeyEvent, MotionEvent>, int32_t /*windowId*/>>
65+
mExpectedEvents GUARDED_BY(mLock);
6366

6467
friend class FakeInputTracingBackend;
6568

6669
// Helper to verify that the given event appears as expected in the trace. If the verification
6770
// fails, the error message describes why.
6871
template <typename Event>
69-
base::Result<void> verifyEventTraced(const Event&) const REQUIRES(mLock);
72+
base::Result<void> verifyEventTraced(const Event&, int32_t windowId) const REQUIRES(mLock);
7073
};
7174

7275
/**
@@ -82,7 +85,7 @@ class FakeInputTracingBackend : public trace::InputTracingBackendInterface {
8285

8386
void traceKeyEvent(const trace::TracedKeyEvent& entry) const override;
8487
void traceMotionEvent(const trace::TracedMotionEvent& entry) const override;
85-
void traceWindowDispatch(const WindowDispatchArgs& entry) const override {}
88+
void traceWindowDispatch(const WindowDispatchArgs& entry) const override;
8689
};
8790

8891
} // namespace android::inputdispatcher

services/inputflinger/tests/InputDispatcher_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,11 +1482,12 @@ class FakeWindowHandle : public WindowInfoHandle {
14821482

14831483
switch (event->getType()) {
14841484
case InputEventType::KEY: {
1485-
gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event));
1485+
gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), mInfo.id);
14861486
break;
14871487
}
14881488
case InputEventType::MOTION: {
1489-
gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event));
1489+
gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event),
1490+
mInfo.id);
14901491
break;
14911492
}
14921493
default:

0 commit comments

Comments
 (0)