diff options
author | 2024-11-15 16:01:41 -0800 | |
---|---|---|
committer | 2024-11-15 16:10:40 -0800 | |
commit | 8047b3cd1aae93f48b87e47c8b6d44f114579d05 (patch) | |
tree | 53d2db3fda2f8faed56cf3915642fad066b26500 | |
parent | 3a362e5264d33ae4acb8fbf7212f2562c7b8f0dc (diff) |
Use matcher-based consumption in InputPublisherAndConsumerNoResampling_test
This makes it simpler to consume events in this test, and follows the
existing approach in other pieces of our testing code.
Bug: 305165753
Test: TEST=libinput_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST
Flag: EXEMPT refactor
Change-Id: I8481789c8ef22ddb995f1ccc800e536da7362125
-rw-r--r-- | libs/input/tests/InputPublisherAndConsumerNoResampling_test.cpp | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/libs/input/tests/InputPublisherAndConsumerNoResampling_test.cpp b/libs/input/tests/InputPublisherAndConsumerNoResampling_test.cpp index 1210f711de..39bb841c0a 100644 --- a/libs/input/tests/InputPublisherAndConsumerNoResampling_test.cpp +++ b/libs/input/tests/InputPublisherAndConsumerNoResampling_test.cpp @@ -14,15 +14,18 @@ * limitations under the License. */ +#include <TestEventMatchers.h> #include <android-base/logging.h> #include <attestation/HmacKeyManager.h> #include <ftl/enum.h> +#include <gmock/gmock.h> #include <gtest/gtest.h> #include <input/BlockingQueue.h> #include <input/InputConsumerNoResampling.h> #include <input/InputTransport.h> using android::base::Result; +using ::testing::Matcher; namespace android { @@ -278,7 +281,7 @@ protected: void SetUp() override { std::unique_ptr<InputChannel> serverChannel; status_t result = - InputChannel::openInputChannelPair("channel name", serverChannel, mClientChannel); + InputChannel::openInputChannelPair("test channel", serverChannel, mClientChannel); ASSERT_EQ(OK, result); mPublisher = std::make_unique<InputPublisher>(std::move(serverChannel)); @@ -336,6 +339,8 @@ protected: // accessed on the test thread. BlockingQueue<bool> mProbablyHasInputResponses; + std::unique_ptr<MotionEvent> assertReceivedMotionEvent(const Matcher<MotionEvent>& matcher); + private: sp<MessageHandler> mMessageHandler; void handleMessage(const Message& message); @@ -389,6 +394,20 @@ void InputPublisherAndConsumerNoResamplingTest::sendMessage(LooperMessage messag mLooper->sendMessage(mMessageHandler, msg); } +std::unique_ptr<MotionEvent> InputPublisherAndConsumerNoResamplingTest::assertReceivedMotionEvent( + const Matcher<MotionEvent>& matcher) { + std::optional<std::unique_ptr<MotionEvent>> event = mMotionEvents.popWithTimeout(TIMEOUT); + if (!event) { + ADD_FAILURE() << "No event was received, but expected motion " << matcher; + return nullptr; + } + if (*event == nullptr) { + LOG(FATAL) << "Event was received, but it was null"; + } + EXPECT_THAT(**event, matcher); + return std::move(*event); +} + void InputPublisherAndConsumerNoResamplingTest::handleMessage(const Message& message) { switch (static_cast<LooperMessage>(message.what)) { case LooperMessage::CALL_PROBABLY_HAS_INPUT: { @@ -572,8 +591,7 @@ void InputPublisherAndConsumerNoResamplingTest::publishAndConsumeSinglePointerMu const nsecs_t publishTimeOfDown = systemTime(SYSTEM_TIME_MONOTONIC); publishMotionEvent(*mPublisher, argsDown); - // Consume the DOWN event. - ASSERT_TRUE(mMotionEvents.popWithTimeout(TIMEOUT).has_value()); + assertReceivedMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); verifyFinishedSignal(*mPublisher, mSeq, publishTimeOfDown); @@ -622,10 +640,10 @@ void InputPublisherAndConsumerNoResamplingTest::publishAndConsumeSinglePointerMu // the motion as a batched event, or as a sequence of multiple single-sample MotionEvents (or a // mix of those) while (singleSampledMotionEvents.size() != nSamples) { - const std::optional<std::unique_ptr<MotionEvent>> batchedMotionEvent = - mMotionEvents.popWithTimeout(TIMEOUT); + const std::unique_ptr<MotionEvent> batchedMotionEvent = + assertReceivedMotionEvent(WithMotionAction(ACTION_MOVE)); // The events received by these calls are never null - std::vector<MotionEvent> splitMotionEvents = splitBatchedMotionEvent(**batchedMotionEvent); + std::vector<MotionEvent> splitMotionEvents = splitBatchedMotionEvent(*batchedMotionEvent); singleSampledMotionEvents.insert(singleSampledMotionEvents.end(), splitMotionEvents.begin(), splitMotionEvents.end()); } @@ -681,10 +699,7 @@ void InputPublisherAndConsumerNoResamplingTest::publishAndConsumeBatchedMotionMo } mNotifyLooperMayProceed.notify_all(); - std::optional<std::unique_ptr<MotionEvent>> optMotion = mMotionEvents.popWithTimeout(TIMEOUT); - ASSERT_TRUE(optMotion.has_value()); - std::unique_ptr<MotionEvent> motion = std::move(*optMotion); - ASSERT_EQ(ACTION_MOVE, motion->getAction()); + assertReceivedMotionEvent(WithMotionAction(ACTION_MOVE)); verifyFinishedSignal(*mPublisher, seq, publishTime); } @@ -696,9 +711,7 @@ void InputPublisherAndConsumerNoResamplingTest::publishAndConsumeMotionEvent( nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC); publishMotionEvent(*mPublisher, args); - std::optional<std::unique_ptr<MotionEvent>> optMotion = mMotionEvents.popWithTimeout(TIMEOUT); - ASSERT_TRUE(optMotion.has_value()); - std::unique_ptr<MotionEvent> event = std::move(*optMotion); + std::unique_ptr<MotionEvent> event = assertReceivedMotionEvent(WithMotionAction(action)); verifyArgsEqualToEvent(args, *event); |