Loading libs/ui/tests/Android.bp +11 −0 Original line number Diff line number Diff line Loading @@ -53,6 +53,17 @@ cc_test { ], } cc_test { name: "DisplayIdentification_test", shared_libs: ["libui"], static_libs: ["libgmock"], srcs: ["DisplayIdentification_test.cpp"], cflags: [ "-Wall", "-Werror", ], } cc_test { name: "FlattenableHelpers_test", shared_libs: ["libui"], Loading services/inputflinger/tests/FakeInputTracingBackend.cpp +34 −7 Original line number Diff line number Diff line Loading @@ -33,18 +33,22 @@ base::ResultError<> error(const std::ostringstream& ss) { 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() { Loading @@ -53,9 +57,9 @@ void VerifyingTrace::verifyExpectedEventsTraced() { 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; Loading @@ -72,11 +76,13 @@ void VerifyingTrace::verifyExpectedEventsTraced() { 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()); Loading @@ -87,6 +93,19 @@ base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent) 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 {}; } Loading @@ -108,4 +127,12 @@ void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& e 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 services/inputflinger/tests/FakeInputTracingBackend.h +8 −5 Original line number Diff line number Diff line Loading @@ -39,10 +39,10 @@ public: VerifyingTrace() = default; /** Add an expectation for a key event to be traced. */ void expectKeyDispatchTraced(const KeyEvent& event); void expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId); /** Add an expectation for a motion event to be traced. */ void expectMotionDispatchTraced(const MotionEvent& event); void expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId); /** * Wait and verify that all expected events are traced. Loading @@ -59,14 +59,17 @@ private: std::mutex mLock; std::condition_variable mEventTracedCondition; std::unordered_set<uint32_t /*eventId*/> mTracedEvents GUARDED_BY(mLock); std::vector<std::variant<KeyEvent, MotionEvent>> mExpectedEvents GUARDED_BY(mLock); using WindowDispatchArgs = trace::InputTracingBackendInterface::WindowDispatchArgs; std::vector<WindowDispatchArgs> mTracedWindowDispatches GUARDED_BY(mLock); std::vector<std::pair<std::variant<KeyEvent, MotionEvent>, int32_t /*windowId*/>> mExpectedEvents GUARDED_BY(mLock); friend class FakeInputTracingBackend; // Helper to verify that the given event appears as expected in the trace. If the verification // fails, the error message describes why. template <typename Event> base::Result<void> verifyEventTraced(const Event&) const REQUIRES(mLock); base::Result<void> verifyEventTraced(const Event&, int32_t windowId) const REQUIRES(mLock); }; /** Loading @@ -82,7 +85,7 @@ private: void traceKeyEvent(const trace::TracedKeyEvent& entry) const override; void traceMotionEvent(const trace::TracedMotionEvent& entry) const override; void traceWindowDispatch(const WindowDispatchArgs& entry) const override {} void traceWindowDispatch(const WindowDispatchArgs& entry) const override; }; } // namespace android::inputdispatcher services/inputflinger/tests/InputDispatcher_test.cpp +3 −2 Original line number Diff line number Diff line Loading @@ -1482,11 +1482,12 @@ private: switch (event->getType()) { case InputEventType::KEY: { gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event)); gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), mInfo.id); break; } case InputEventType::MOTION: { gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event)); gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event), mInfo.id); break; } default: Loading Loading
libs/ui/tests/Android.bp +11 −0 Original line number Diff line number Diff line Loading @@ -53,6 +53,17 @@ cc_test { ], } cc_test { name: "DisplayIdentification_test", shared_libs: ["libui"], static_libs: ["libgmock"], srcs: ["DisplayIdentification_test.cpp"], cflags: [ "-Wall", "-Werror", ], } cc_test { name: "FlattenableHelpers_test", shared_libs: ["libui"], Loading
services/inputflinger/tests/FakeInputTracingBackend.cpp +34 −7 Original line number Diff line number Diff line Loading @@ -33,18 +33,22 @@ base::ResultError<> error(const std::ostringstream& ss) { 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() { Loading @@ -53,9 +57,9 @@ void VerifyingTrace::verifyExpectedEventsTraced() { 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; Loading @@ -72,11 +76,13 @@ void VerifyingTrace::verifyExpectedEventsTraced() { 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()); Loading @@ -87,6 +93,19 @@ base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent) 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 {}; } Loading @@ -108,4 +127,12 @@ void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& e 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
services/inputflinger/tests/FakeInputTracingBackend.h +8 −5 Original line number Diff line number Diff line Loading @@ -39,10 +39,10 @@ public: VerifyingTrace() = default; /** Add an expectation for a key event to be traced. */ void expectKeyDispatchTraced(const KeyEvent& event); void expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId); /** Add an expectation for a motion event to be traced. */ void expectMotionDispatchTraced(const MotionEvent& event); void expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId); /** * Wait and verify that all expected events are traced. Loading @@ -59,14 +59,17 @@ private: std::mutex mLock; std::condition_variable mEventTracedCondition; std::unordered_set<uint32_t /*eventId*/> mTracedEvents GUARDED_BY(mLock); std::vector<std::variant<KeyEvent, MotionEvent>> mExpectedEvents GUARDED_BY(mLock); using WindowDispatchArgs = trace::InputTracingBackendInterface::WindowDispatchArgs; std::vector<WindowDispatchArgs> mTracedWindowDispatches GUARDED_BY(mLock); std::vector<std::pair<std::variant<KeyEvent, MotionEvent>, int32_t /*windowId*/>> mExpectedEvents GUARDED_BY(mLock); friend class FakeInputTracingBackend; // Helper to verify that the given event appears as expected in the trace. If the verification // fails, the error message describes why. template <typename Event> base::Result<void> verifyEventTraced(const Event&) const REQUIRES(mLock); base::Result<void> verifyEventTraced(const Event&, int32_t windowId) const REQUIRES(mLock); }; /** Loading @@ -82,7 +85,7 @@ private: void traceKeyEvent(const trace::TracedKeyEvent& entry) const override; void traceMotionEvent(const trace::TracedMotionEvent& entry) const override; void traceWindowDispatch(const WindowDispatchArgs& entry) const override {} void traceWindowDispatch(const WindowDispatchArgs& entry) const override; }; } // namespace android::inputdispatcher
services/inputflinger/tests/InputDispatcher_test.cpp +3 −2 Original line number Diff line number Diff line Loading @@ -1482,11 +1482,12 @@ private: switch (event->getType()) { case InputEventType::KEY: { gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event)); gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), mInfo.id); break; } case InputEventType::MOTION: { gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event)); gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event), mInfo.id); break; } default: Loading