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
diff --git a/libs/input/tests/InputPublisherAndConsumerNoResampling_test.cpp b/libs/input/tests/InputPublisherAndConsumerNoResampling_test.cpp
index 1210f71..39bb841 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 @@
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 @@
// 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 @@
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 @@
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 @@
// 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 @@
}
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 @@
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);