blob: c30f243398262a5bf22c68b91e000ef3e680af7b [file] [log] [blame]
Paul Ramireze2bb1872024-08-12 20:21:13 +00001/**
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <input/InputConsumerNoResampling.h>
18
19#include <memory>
20#include <optional>
21#include <utility>
22
23#include <TestInputChannel.h>
24#include <TestLooper.h>
25#include <android-base/logging.h>
26#include <gtest/gtest.h>
27#include <input/BlockingQueue.h>
28#include <input/InputEventBuilders.h>
29#include <utils/StrongPointer.h>
30
31namespace android {
32
33class InputConsumerTest : public testing::Test, public InputConsumerCallbacks {
34protected:
35 InputConsumerTest()
36 : mClientTestChannel{std::make_shared<TestInputChannel>("TestChannel")},
37 mTestLooper{std::make_shared<TestLooper>()} {
38 Looper::setForThread(mTestLooper->getLooper());
39 mConsumer = std::make_unique<InputConsumerNoResampling>(mClientTestChannel, mTestLooper,
40 *this, /*resampler=*/nullptr);
41 }
42
43 void assertOnBatchedInputEventPendingWasCalled();
44
45 std::shared_ptr<TestInputChannel> mClientTestChannel;
46 std::shared_ptr<TestLooper> mTestLooper;
47 std::unique_ptr<InputConsumerNoResampling> mConsumer;
48
49 BlockingQueue<std::unique_ptr<KeyEvent>> mKeyEvents;
50 BlockingQueue<std::unique_ptr<MotionEvent>> mMotionEvents;
51 BlockingQueue<std::unique_ptr<FocusEvent>> mFocusEvents;
52 BlockingQueue<std::unique_ptr<CaptureEvent>> mCaptureEvents;
53 BlockingQueue<std::unique_ptr<DragEvent>> mDragEvents;
54 BlockingQueue<std::unique_ptr<TouchModeEvent>> mTouchModeEvents;
55
56private:
57 size_t onBatchedInputEventPendingInvocationCount{0};
58
59 // InputConsumerCallbacks interface
60 void onKeyEvent(std::unique_ptr<KeyEvent> event, uint32_t seq) override {
61 mKeyEvents.push(std::move(event));
62 mConsumer->finishInputEvent(seq, true);
63 }
64 void onMotionEvent(std::unique_ptr<MotionEvent> event, uint32_t seq) override {
65 mMotionEvents.push(std::move(event));
66 mConsumer->finishInputEvent(seq, true);
67 }
68 void onBatchedInputEventPending(int32_t pendingBatchSource) override {
69 if (!mConsumer->probablyHasInput()) {
70 ADD_FAILURE() << "should deterministically have input because there is a batch";
71 }
72 ++onBatchedInputEventPendingInvocationCount;
73 };
74 void onFocusEvent(std::unique_ptr<FocusEvent> event, uint32_t seq) override {
75 mFocusEvents.push(std::move(event));
76 mConsumer->finishInputEvent(seq, true);
77 };
78 void onCaptureEvent(std::unique_ptr<CaptureEvent> event, uint32_t seq) override {
79 mCaptureEvents.push(std::move(event));
80 mConsumer->finishInputEvent(seq, true);
81 };
82 void onDragEvent(std::unique_ptr<DragEvent> event, uint32_t seq) override {
83 mDragEvents.push(std::move(event));
84 mConsumer->finishInputEvent(seq, true);
85 }
86 void onTouchModeEvent(std::unique_ptr<TouchModeEvent> event, uint32_t seq) override {
87 mTouchModeEvents.push(std::move(event));
88 mConsumer->finishInputEvent(seq, true);
89 };
90};
91
92void InputConsumerTest::assertOnBatchedInputEventPendingWasCalled() {
93 ASSERT_GT(onBatchedInputEventPendingInvocationCount, 0UL)
94 << "onBatchedInputEventPending has not been called.";
95 --onBatchedInputEventPendingInvocationCount;
96}
97
98TEST_F(InputConsumerTest, MessageStreamBatchedInMotionEvent) {
99 mClientTestChannel->enqueueMessage(
100 InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/0}.build());
101 mClientTestChannel->enqueueMessage(
102 InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/1}.build());
103 mClientTestChannel->enqueueMessage(
104 InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/2}.build());
105
106 mClientTestChannel->assertNoSentMessages();
107
108 mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT);
109
110 assertOnBatchedInputEventPendingWasCalled();
111
112 mConsumer->consumeBatchedInputEvents(std::nullopt);
113
114 std::unique_ptr<MotionEvent> batchedMotionEvent = mMotionEvents.pop();
115 ASSERT_NE(batchedMotionEvent, nullptr);
116
117 mClientTestChannel->assertFinishMessage(/*seq=*/0, /*handled=*/true);
118 mClientTestChannel->assertFinishMessage(/*seq=*/1, /*handled=*/true);
119 mClientTestChannel->assertFinishMessage(/*seq=*/2, /*handled=*/true);
120
121 EXPECT_EQ(batchedMotionEvent->getHistorySize() + 1, 3UL);
122}
123} // namespace android