1717#include "../dispatcher/InputDispatcher.h"
1818#include "../BlockingQueue.h"
1919#include "FakeApplicationHandle.h"
20+ #include "FakeInputTracingBackend.h"
2021#include "TestEventMatchers.h"
2122
2223#include <NotifyArgsBuilders.h>
@@ -660,21 +661,30 @@ class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
660661
661662// --- InputDispatcherTest ---
662663
664+ // The trace is a global variable for now, to avoid having to pass it into all of the
665+ // FakeWindowHandles created throughout the tests.
666+ // TODO(b/210460522): Update the tests to avoid the need to have the trace be a global variable.
667+ static std::shared_ptr<VerifyingTrace> gVerifyingTrace = std::make_shared<VerifyingTrace>();
668+
663669class InputDispatcherTest : public testing::Test {
664670protected:
665671 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
666672 std::unique_ptr<InputDispatcher> mDispatcher;
667673
668674 void SetUp() override {
675+ gVerifyingTrace->reset();
669676 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
670- mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, nullptr);
677+ mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy,
678+ std::make_unique<FakeInputTracingBackend>(
679+ gVerifyingTrace));
671680
672681 mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
673682 // Start InputDispatcher thread
674683 ASSERT_EQ(OK, mDispatcher->start());
675684 }
676685
677686 void TearDown() override {
687+ ASSERT_NO_FATAL_FAILURE(gVerifyingTrace->verifyExpectedEventsTraced());
678688 ASSERT_EQ(OK, mDispatcher->stop());
679689 mFakePolicy.reset();
680690 mDispatcher.reset();
@@ -1397,11 +1407,7 @@ class FakeWindowHandle : public WindowInfoHandle {
13971407 }
13981408
13991409 std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() {
1400- if (mInputReceiver == nullptr) {
1401- ADD_FAILURE() << "Invalid receive event on window with no receiver";
1402- return std::make_pair(std::nullopt, nullptr);
1403- }
1404- return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
1410+ return receive();
14051411 }
14061412
14071413 void finishEvent(uint32_t sequenceNum) {
@@ -1439,6 +1445,7 @@ class FakeWindowHandle : public WindowInfoHandle {
14391445
14401446 int getChannelFd() { return mInputReceiver->getChannelFd(); }
14411447
1448+ // FakeWindowHandle uses this consume method to ensure received events are added to the trace.
14421449 std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) {
14431450 if (mInputReceiver == nullptr) {
14441451 LOG(FATAL) << "Cannot consume event from a window with no input event receiver";
@@ -1447,6 +1454,7 @@ class FakeWindowHandle : public WindowInfoHandle {
14471454 if (event == nullptr) {
14481455 ADD_FAILURE() << "Consume failed: no event";
14491456 }
1457+ expectReceivedEventTraced(event);
14501458 return event;
14511459 }
14521460
@@ -1456,6 +1464,37 @@ class FakeWindowHandle : public WindowInfoHandle {
14561464 std::shared_ptr<FakeInputReceiver> mInputReceiver;
14571465 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
14581466 friend class sp<FakeWindowHandle>;
1467+
1468+ // FakeWindowHandle uses this receive method to ensure received events are added to the trace.
1469+ std::pair<std::optional<uint32_t /*seq*/>, std::unique_ptr<InputEvent>> receive() {
1470+ if (mInputReceiver == nullptr) {
1471+ ADD_FAILURE() << "Invalid receive event on window with no receiver";
1472+ return std::make_pair(std::nullopt, nullptr);
1473+ }
1474+ auto out = mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
1475+ const auto& [_, event] = out;
1476+ expectReceivedEventTraced(event);
1477+ return std::move(out);
1478+ }
1479+
1480+ void expectReceivedEventTraced(const std::unique_ptr<InputEvent>& event) {
1481+ if (!event) {
1482+ return;
1483+ }
1484+
1485+ switch (event->getType()) {
1486+ case InputEventType::KEY: {
1487+ gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event));
1488+ break;
1489+ }
1490+ case InputEventType::MOTION: {
1491+ gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event));
1492+ break;
1493+ }
1494+ default:
1495+ break;
1496+ }
1497+ }
14591498};
14601499
14611500std::atomic<int32_t> FakeWindowHandle::sId{1};
0 commit comments