blob: 55be4532871c86fa0ea1d379eee4f64022809fe1 [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
Paul Ramirezcd7488c2024-09-13 23:01:12 +000033namespace {
34
35using std::chrono::nanoseconds;
36
37} // namespace
38
Paul Ramireze2bb1872024-08-12 20:21:13 +000039class InputConsumerTest : public testing::Test, public InputConsumerCallbacks {
40protected:
41 InputConsumerTest()
42 : mClientTestChannel{std::make_shared<TestInputChannel>("TestChannel")},
43 mTestLooper{std::make_shared<TestLooper>()} {
44 Looper::setForThread(mTestLooper->getLooper());
Paul Ramirezcd7488c2024-09-13 23:01:12 +000045 mConsumer =
46 std::make_unique<InputConsumerNoResampling>(mClientTestChannel, mTestLooper, *this,
47 std::make_unique<LegacyResampler>());
Paul Ramireze2bb1872024-08-12 20:21:13 +000048 }
49
50 void assertOnBatchedInputEventPendingWasCalled();
51
52 std::shared_ptr<TestInputChannel> mClientTestChannel;
53 std::shared_ptr<TestLooper> mTestLooper;
54 std::unique_ptr<InputConsumerNoResampling> mConsumer;
55
56 BlockingQueue<std::unique_ptr<KeyEvent>> mKeyEvents;
57 BlockingQueue<std::unique_ptr<MotionEvent>> mMotionEvents;
58 BlockingQueue<std::unique_ptr<FocusEvent>> mFocusEvents;
59 BlockingQueue<std::unique_ptr<CaptureEvent>> mCaptureEvents;
60 BlockingQueue<std::unique_ptr<DragEvent>> mDragEvents;
61 BlockingQueue<std::unique_ptr<TouchModeEvent>> mTouchModeEvents;
62
63private:
Paul Ramirezcd7488c2024-09-13 23:01:12 +000064 size_t mOnBatchedInputEventPendingInvocationCount{0};
Paul Ramireze2bb1872024-08-12 20:21:13 +000065
66 // InputConsumerCallbacks interface
67 void onKeyEvent(std::unique_ptr<KeyEvent> event, uint32_t seq) override {
68 mKeyEvents.push(std::move(event));
69 mConsumer->finishInputEvent(seq, true);
70 }
71 void onMotionEvent(std::unique_ptr<MotionEvent> event, uint32_t seq) override {
72 mMotionEvents.push(std::move(event));
73 mConsumer->finishInputEvent(seq, true);
74 }
75 void onBatchedInputEventPending(int32_t pendingBatchSource) override {
76 if (!mConsumer->probablyHasInput()) {
77 ADD_FAILURE() << "should deterministically have input because there is a batch";
78 }
Paul Ramirezcd7488c2024-09-13 23:01:12 +000079 ++mOnBatchedInputEventPendingInvocationCount;
Paul Ramireze2bb1872024-08-12 20:21:13 +000080 };
81 void onFocusEvent(std::unique_ptr<FocusEvent> event, uint32_t seq) override {
82 mFocusEvents.push(std::move(event));
83 mConsumer->finishInputEvent(seq, true);
84 };
85 void onCaptureEvent(std::unique_ptr<CaptureEvent> event, uint32_t seq) override {
86 mCaptureEvents.push(std::move(event));
87 mConsumer->finishInputEvent(seq, true);
88 };
89 void onDragEvent(std::unique_ptr<DragEvent> event, uint32_t seq) override {
90 mDragEvents.push(std::move(event));
91 mConsumer->finishInputEvent(seq, true);
92 }
93 void onTouchModeEvent(std::unique_ptr<TouchModeEvent> event, uint32_t seq) override {
94 mTouchModeEvents.push(std::move(event));
95 mConsumer->finishInputEvent(seq, true);
96 };
97};
98
99void InputConsumerTest::assertOnBatchedInputEventPendingWasCalled() {
Paul Ramirezcd7488c2024-09-13 23:01:12 +0000100 ASSERT_GT(mOnBatchedInputEventPendingInvocationCount, 0UL)
Paul Ramireze2bb1872024-08-12 20:21:13 +0000101 << "onBatchedInputEventPending has not been called.";
Paul Ramirezcd7488c2024-09-13 23:01:12 +0000102 --mOnBatchedInputEventPendingInvocationCount;
Paul Ramireze2bb1872024-08-12 20:21:13 +0000103}
104
105TEST_F(InputConsumerTest, MessageStreamBatchedInMotionEvent) {
Paul Ramirezcd7488c2024-09-13 23:01:12 +0000106 mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/0}
107 .eventTime(nanoseconds{0ms}.count())
108 .action(AMOTION_EVENT_ACTION_DOWN)
109 .build());
110 mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/1}
111 .eventTime(nanoseconds{5ms}.count())
112 .action(AMOTION_EVENT_ACTION_MOVE)
113 .build());
114 mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/2}
115 .eventTime(nanoseconds{10ms}.count())
116 .action(AMOTION_EVENT_ACTION_MOVE)
117 .build());
Paul Ramireze2bb1872024-08-12 20:21:13 +0000118
119 mClientTestChannel->assertNoSentMessages();
120
121 mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT);
122
123 assertOnBatchedInputEventPendingWasCalled();
124
125 mConsumer->consumeBatchedInputEvents(std::nullopt);
126
Paul Ramirezcd7488c2024-09-13 23:01:12 +0000127 std::unique_ptr<MotionEvent> downMotionEvent = mMotionEvents.pop();
128 ASSERT_NE(downMotionEvent, nullptr);
129
130 std::unique_ptr<MotionEvent> moveMotionEvent = mMotionEvents.pop();
131 ASSERT_NE(moveMotionEvent, nullptr);
132 EXPECT_EQ(moveMotionEvent->getHistorySize() + 1, 3UL);
Paul Ramireze2bb1872024-08-12 20:21:13 +0000133
134 mClientTestChannel->assertFinishMessage(/*seq=*/0, /*handled=*/true);
135 mClientTestChannel->assertFinishMessage(/*seq=*/1, /*handled=*/true);
136 mClientTestChannel->assertFinishMessage(/*seq=*/2, /*handled=*/true);
Paul Ramirezcd7488c2024-09-13 23:01:12 +0000137}
Paul Ramireze2bb1872024-08-12 20:21:13 +0000138
Paul Ramirezcd7488c2024-09-13 23:01:12 +0000139TEST_F(InputConsumerTest, LastBatchedSampleIsLessThanResampleTime) {
140 mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/0}
141 .eventTime(nanoseconds{0ms}.count())
142 .action(AMOTION_EVENT_ACTION_DOWN)
143 .build());
144 mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/1}
145 .eventTime(nanoseconds{5ms}.count())
146 .action(AMOTION_EVENT_ACTION_MOVE)
147 .build());
148 mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/2}
149 .eventTime(nanoseconds{10ms}.count())
150 .action(AMOTION_EVENT_ACTION_MOVE)
151 .build());
152 mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/3}
153 .eventTime(nanoseconds{15ms}.count())
154 .action(AMOTION_EVENT_ACTION_MOVE)
155 .build());
156
157 mClientTestChannel->assertNoSentMessages();
158
159 mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT);
160
161 assertOnBatchedInputEventPendingWasCalled();
162
163 mConsumer->consumeBatchedInputEvents(16'000'000 /*ns*/);
164
165 std::unique_ptr<MotionEvent> downMotionEvent = mMotionEvents.pop();
166 ASSERT_NE(downMotionEvent, nullptr);
167
168 std::unique_ptr<MotionEvent> moveMotionEvent = mMotionEvents.pop();
169 ASSERT_NE(moveMotionEvent, nullptr);
170 const size_t numSamples = moveMotionEvent->getHistorySize() + 1;
171 EXPECT_LT(moveMotionEvent->getHistoricalEventTime(numSamples - 2),
172 moveMotionEvent->getEventTime());
173
174 // Consume all remaining events before ending the test. Otherwise, the smart pointer that owns
175 // consumer is set to null before destroying consumer. This leads to a member function call on a
176 // null object.
177 // TODO(b/332613662): Remove this workaround.
178 mConsumer->consumeBatchedInputEvents(std::nullopt);
179
180 mClientTestChannel->assertFinishMessage(/*seq=*/0, true);
181 mClientTestChannel->assertFinishMessage(/*seq=*/1, true);
182 mClientTestChannel->assertFinishMessage(/*seq=*/2, true);
183 mClientTestChannel->assertFinishMessage(/*seq=*/3, true);
Paul Ramireze2bb1872024-08-12 20:21:13 +0000184}
185} // namespace android