InputDispatcher_test: Verify all events dispatched to windows are traced

Bug: 210460522
Test: atest inputflinger_tests
Change-Id: I8ce95c44645f8c3bee7e2eb7cfefab2dae59f6e6
diff --git a/services/inputflinger/tests/FakeInputTracingBackend.cpp b/services/inputflinger/tests/FakeInputTracingBackend.cpp
index 1d27107..f4a06f7 100644
--- a/services/inputflinger/tests/FakeInputTracingBackend.cpp
+++ b/services/inputflinger/tests/FakeInputTracingBackend.cpp
@@ -33,18 +33,22 @@
     return base::ResultError(ss.str(), BAD_VALUE);
 }
 
+inline auto getId(const trace::TracedEvent& v) {
+    return std::visit([](const auto& event) { return event.id; }, v);
+}
+
 } // namespace
 
 // --- VerifyingTrace ---
 
-void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event) {
+void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId) {
     std::scoped_lock lock(mLock);
-    mExpectedEvents.emplace_back(event);
+    mExpectedEvents.emplace_back(event, windowId);
 }
 
-void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event) {
+void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId) {
     std::scoped_lock lock(mLock);
-    mExpectedEvents.emplace_back(event);
+    mExpectedEvents.emplace_back(event, windowId);
 }
 
 void VerifyingTrace::verifyExpectedEventsTraced() {
@@ -53,9 +57,9 @@
 
     base::Result<void> result;
     mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) {
-        for (const auto& expectedEvent : mExpectedEvents) {
+        for (const auto& [expectedEvent, windowId] : mExpectedEvents) {
             std::visit([&](const auto& event)
-                               REQUIRES(mLock) { result = verifyEventTraced(event); },
+                               REQUIRES(mLock) { result = verifyEventTraced(event, windowId); },
                        expectedEvent);
             if (!result.ok()) {
                 return false;
@@ -72,11 +76,13 @@
 void VerifyingTrace::reset() {
     std::scoped_lock lock(mLock);
     mTracedEvents.clear();
+    mTracedWindowDispatches.clear();
     mExpectedEvents.clear();
 }
 
 template <typename Event>
-base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent) const {
+base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent,
+                                                     int32_t expectedWindowId) const {
     std::ostringstream msg;
 
     auto tracedEventsIt = mTracedEvents.find(expectedEvent.getId());
@@ -87,6 +93,19 @@
         return error(msg);
     }
 
+    auto tracedDispatchesIt =
+            std::find_if(mTracedWindowDispatches.begin(), mTracedWindowDispatches.end(),
+                         [&](const WindowDispatchArgs& args) {
+                             return args.windowId == expectedWindowId &&
+                                     getId(args.eventEntry) == expectedEvent.getId();
+                         });
+    if (tracedDispatchesIt == mTracedWindowDispatches.end()) {
+        msg << "Expected dispatch of event with ID 0x" << std::hex << expectedEvent.getId()
+            << " to window with ID 0x" << expectedWindowId << " to be traced, but it was not."
+            << "\nExpected event: " << expectedEvent;
+        return error(msg);
+    }
+
     return {};
 }
 
@@ -108,4 +127,12 @@
     mTrace->mEventTracedCondition.notify_all();
 }
 
+void FakeInputTracingBackend::traceWindowDispatch(const WindowDispatchArgs& args) const {
+    {
+        std::scoped_lock lock(mTrace->mLock);
+        mTrace->mTracedWindowDispatches.push_back(args);
+    }
+    mTrace->mEventTracedCondition.notify_all();
+}
+
 } // namespace android::inputdispatcher