Skip to content

Commit 8047b3c

Browse files
committed
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
1 parent 3a362e5 commit 8047b3c

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

libs/input/tests/InputPublisherAndConsumerNoResampling_test.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include <TestEventMatchers.h>
1718
#include <android-base/logging.h>
1819
#include <attestation/HmacKeyManager.h>
1920
#include <ftl/enum.h>
21+
#include <gmock/gmock.h>
2022
#include <gtest/gtest.h>
2123
#include <input/BlockingQueue.h>
2224
#include <input/InputConsumerNoResampling.h>
2325
#include <input/InputTransport.h>
2426

2527
using android::base::Result;
28+
using ::testing::Matcher;
2629

2730
namespace android {
2831

@@ -278,7 +281,7 @@ class InputPublisherAndConsumerNoResamplingTest : public testing::Test,
278281
void SetUp() override {
279282
std::unique_ptr<InputChannel> serverChannel;
280283
status_t result =
281-
InputChannel::openInputChannelPair("channel name", serverChannel, mClientChannel);
284+
InputChannel::openInputChannelPair("test channel", serverChannel, mClientChannel);
282285
ASSERT_EQ(OK, result);
283286

284287
mPublisher = std::make_unique<InputPublisher>(std::move(serverChannel));
@@ -336,6 +339,8 @@ class InputPublisherAndConsumerNoResamplingTest : public testing::Test,
336339
// accessed on the test thread.
337340
BlockingQueue<bool> mProbablyHasInputResponses;
338341

342+
std::unique_ptr<MotionEvent> assertReceivedMotionEvent(const Matcher<MotionEvent>& matcher);
343+
339344
private:
340345
sp<MessageHandler> mMessageHandler;
341346
void handleMessage(const Message& message);
@@ -389,6 +394,20 @@ void InputPublisherAndConsumerNoResamplingTest::sendMessage(LooperMessage messag
389394
mLooper->sendMessage(mMessageHandler, msg);
390395
}
391396

397+
std::unique_ptr<MotionEvent> InputPublisherAndConsumerNoResamplingTest::assertReceivedMotionEvent(
398+
const Matcher<MotionEvent>& matcher) {
399+
std::optional<std::unique_ptr<MotionEvent>> event = mMotionEvents.popWithTimeout(TIMEOUT);
400+
if (!event) {
401+
ADD_FAILURE() << "No event was received, but expected motion " << matcher;
402+
return nullptr;
403+
}
404+
if (*event == nullptr) {
405+
LOG(FATAL) << "Event was received, but it was null";
406+
}
407+
EXPECT_THAT(**event, matcher);
408+
return std::move(*event);
409+
}
410+
392411
void InputPublisherAndConsumerNoResamplingTest::handleMessage(const Message& message) {
393412
switch (static_cast<LooperMessage>(message.what)) {
394413
case LooperMessage::CALL_PROBABLY_HAS_INPUT: {
@@ -572,8 +591,7 @@ void InputPublisherAndConsumerNoResamplingTest::publishAndConsumeSinglePointerMu
572591
const nsecs_t publishTimeOfDown = systemTime(SYSTEM_TIME_MONOTONIC);
573592
publishMotionEvent(*mPublisher, argsDown);
574593

575-
// Consume the DOWN event.
576-
ASSERT_TRUE(mMotionEvents.popWithTimeout(TIMEOUT).has_value());
594+
assertReceivedMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
577595

578596
verifyFinishedSignal(*mPublisher, mSeq, publishTimeOfDown);
579597

@@ -622,10 +640,10 @@ void InputPublisherAndConsumerNoResamplingTest::publishAndConsumeSinglePointerMu
622640
// the motion as a batched event, or as a sequence of multiple single-sample MotionEvents (or a
623641
// mix of those)
624642
while (singleSampledMotionEvents.size() != nSamples) {
625-
const std::optional<std::unique_ptr<MotionEvent>> batchedMotionEvent =
626-
mMotionEvents.popWithTimeout(TIMEOUT);
643+
const std::unique_ptr<MotionEvent> batchedMotionEvent =
644+
assertReceivedMotionEvent(WithMotionAction(ACTION_MOVE));
627645
// The events received by these calls are never null
628-
std::vector<MotionEvent> splitMotionEvents = splitBatchedMotionEvent(**batchedMotionEvent);
646+
std::vector<MotionEvent> splitMotionEvents = splitBatchedMotionEvent(*batchedMotionEvent);
629647
singleSampledMotionEvents.insert(singleSampledMotionEvents.end(), splitMotionEvents.begin(),
630648
splitMotionEvents.end());
631649
}
@@ -681,10 +699,7 @@ void InputPublisherAndConsumerNoResamplingTest::publishAndConsumeBatchedMotionMo
681699
}
682700
mNotifyLooperMayProceed.notify_all();
683701

684-
std::optional<std::unique_ptr<MotionEvent>> optMotion = mMotionEvents.popWithTimeout(TIMEOUT);
685-
ASSERT_TRUE(optMotion.has_value());
686-
std::unique_ptr<MotionEvent> motion = std::move(*optMotion);
687-
ASSERT_EQ(ACTION_MOVE, motion->getAction());
702+
assertReceivedMotionEvent(WithMotionAction(ACTION_MOVE));
688703

689704
verifyFinishedSignal(*mPublisher, seq, publishTime);
690705
}
@@ -696,9 +711,7 @@ void InputPublisherAndConsumerNoResamplingTest::publishAndConsumeMotionEvent(
696711
nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
697712
publishMotionEvent(*mPublisher, args);
698713

699-
std::optional<std::unique_ptr<MotionEvent>> optMotion = mMotionEvents.popWithTimeout(TIMEOUT);
700-
ASSERT_TRUE(optMotion.has_value());
701-
std::unique_ptr<MotionEvent> event = std::move(*optMotion);
714+
std::unique_ptr<MotionEvent> event = assertReceivedMotionEvent(WithMotionAction(action));
702715

703716
verifyArgsEqualToEvent(args, *event);
704717

0 commit comments

Comments
 (0)