blob: 64223afbba74eb148e8bdb743a4b654a7af32dc8 [file] [log] [blame]
Harry Cutts4fb941a2022-12-14 19:14:04 +00001/*
2 * Copyright 2022 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 <memory>
18
Arpit Singh3d84add2023-10-10 19:08:29 +000019#include <com_android_input_flags.h>
20#include <flag_macros.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000021#include <gestures/GestureConverter.h>
22#include <gtest/gtest.h>
23
24#include "FakeEventHub.h"
25#include "FakeInputReaderPolicy.h"
26#include "FakePointerController.h"
27#include "InstrumentedInputReader.h"
28#include "NotifyArgs.h"
29#include "TestConstants.h"
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000030#include "TestEventMatchers.h"
Harry Cutts4fb941a2022-12-14 19:14:04 +000031#include "TestInputListener.h"
Harry Cutts4fb941a2022-12-14 19:14:04 +000032#include "include/gestures.h"
Harry Cuttsedf6ce72023-01-04 12:15:53 +000033#include "ui/Rotation.h"
Harry Cutts4fb941a2022-12-14 19:14:04 +000034
35namespace android {
36
Byoungho Jungee6268f2023-10-30 17:27:26 +090037namespace input_flags = com::android::input::flags;
38
Arpit Singh3d84add2023-10-10 19:08:29 +000039namespace {
40
41const auto TOUCHPAD_PALM_REJECTION =
Byoungho Jungee6268f2023-10-30 17:27:26 +090042 ACONFIG_FLAG(input_flags, enable_touchpad_typing_palm_rejection);
Arpit Singh33a10a62023-10-12 13:06:54 +000043const auto TOUCHPAD_PALM_REJECTION_V2 =
44 ACONFIG_FLAG(input_flags, enable_v2_touchpad_typing_palm_rejection);
Arpit Singh3d84add2023-10-10 19:08:29 +000045
46} // namespace
47
Linnan Li13bf76a2024-05-05 19:18:02 +080048using android::ui::ADISPLAY_ID_DEFAULT;
Harry Cutts4fb941a2022-12-14 19:14:04 +000049using testing::AllOf;
Harry Cuttsef95e712024-02-16 18:56:39 +000050using testing::Each;
Harry Cutts5f26e952023-11-30 18:20:27 +000051using testing::ElementsAre;
52using testing::VariantWith;
Harry Cutts4fb941a2022-12-14 19:14:04 +000053
Prabir Pradhan8b053512024-05-03 23:15:39 +000054class GestureConverterTest : public testing::Test {
Harry Cutts4fb941a2022-12-14 19:14:04 +000055protected:
56 static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
Harry Cuttsc5748d12022-12-02 17:30:18 +000057 static constexpr int32_t EVENTHUB_ID = 1;
Harry Cutts4fb941a2022-12-14 19:14:04 +000058 static constexpr stime_t ARBITRARY_GESTURE_TIME = 1.2;
Harry Cutts4fb941a2022-12-14 19:14:04 +000059
60 void SetUp() {
61 mFakeEventHub = std::make_unique<FakeEventHub>();
62 mFakePolicy = sp<FakeInputReaderPolicy>::make();
63 mFakeListener = std::make_unique<TestInputListener>();
64 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
65 *mFakeListener);
Harry Cuttsc5748d12022-12-02 17:30:18 +000066 mDevice = newDevice();
67 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, -500, 500, 0, 0, 20);
68 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, -500, 500, 0, 0, 20);
Harry Cutts4fb941a2022-12-14 19:14:04 +000069 }
70
Harry Cuttsc5748d12022-12-02 17:30:18 +000071 std::shared_ptr<InputDevice> newDevice() {
72 InputDeviceIdentifier identifier;
73 identifier.name = "device";
74 identifier.location = "USB1";
75 identifier.bus = 0;
76 std::shared_ptr<InputDevice> device =
77 std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, /* generation= */ 2,
78 identifier);
79 mReader->pushNextDevice(device);
80 mFakeEventHub->addDevice(EVENTHUB_ID, identifier.name, InputDeviceClass::TOUCHPAD,
81 identifier.bus);
82 mReader->loopOnce();
83 return device;
84 }
85
Harry Cutts4fb941a2022-12-14 19:14:04 +000086 std::shared_ptr<FakeEventHub> mFakeEventHub;
87 sp<FakeInputReaderPolicy> mFakePolicy;
88 std::unique_ptr<TestInputListener> mFakeListener;
89 std::unique_ptr<InstrumentedInputReader> mReader;
Harry Cuttsc5748d12022-12-02 17:30:18 +000090 std::shared_ptr<InputDevice> mDevice;
Byoungho Jungee6268f2023-10-30 17:27:26 +090091};
92
Harry Cutts4fb941a2022-12-14 19:14:04 +000093TEST_F(GestureConverterTest, Move) {
Harry Cuttsc5748d12022-12-02 17:30:18 +000094 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
95 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +080096 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cutts4fb941a2022-12-14 19:14:04 +000097
98 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
Arpit Singh33a10a62023-10-12 13:06:54 +000099 std::list<NotifyArgs> args =
100 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000101 ASSERT_THAT(args,
102 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +0000103 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Harry Cuttsef95e712024-02-16 18:56:39 +0000104 WithRelativeMotion(0, 0))),
Harry Cutts379ea422023-12-21 15:31:47 +0000105 VariantWith<NotifyMotionArgs>(
106 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000107 WithRelativeMotion(-5, 10), WithButtonState(0),
108 WithPressure(0.0f)))));
109 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +0000110 Each(VariantWith<NotifyMotionArgs>(AllOf(WithCoords(0, 0),
111 WithToolType(ToolType::FINGER),
Harry Cuttsef95e712024-02-16 18:56:39 +0000112 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000113
Prabir Pradhan8b053512024-05-03 23:15:39 +0000114 // The same gesture again should only repeat the HOVER_MOVE, not the HOVER_ENTER.
Harry Cutts379ea422023-12-21 15:31:47 +0000115 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
116 ASSERT_THAT(args,
117 ElementsAre(VariantWith<NotifyMotionArgs>(
Prabir Pradhan8b053512024-05-03 23:15:39 +0000118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithCoords(0, 0),
Harry Cutts379ea422023-12-21 15:31:47 +0000119 WithRelativeMotion(-5, 10), WithToolType(ToolType::FINGER),
120 WithButtonState(0), WithPressure(0.0f),
121 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000122}
123
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000124TEST_F(GestureConverterTest, Move_Rotated) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000125 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
126 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000127 converter.setOrientation(ui::ROTATION_90);
Linnan Li13bf76a2024-05-05 19:18:02 +0800128 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000129
130 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000131 std::list<NotifyArgs> args =
132 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000133 ASSERT_THAT(args,
134 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +0000135 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Harry Cuttsef95e712024-02-16 18:56:39 +0000136 WithRelativeMotion(0, 0))),
Harry Cutts379ea422023-12-21 15:31:47 +0000137 VariantWith<NotifyMotionArgs>(
138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000139 WithRelativeMotion(10, 5), WithButtonState(0),
140 WithPressure(0.0f)))));
141 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +0000142 Each(VariantWith<NotifyMotionArgs>(AllOf(WithCoords(0, 0),
143 WithToolType(ToolType::FINGER),
Harry Cuttsef95e712024-02-16 18:56:39 +0000144 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000145}
146
Harry Cutts4fb941a2022-12-14 19:14:04 +0000147TEST_F(GestureConverterTest, ButtonsChange) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000148 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
149 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800150 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cutts4fb941a2022-12-14 19:14:04 +0000151
152 // Press left and right buttons at once
153 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
154 /* down= */ GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT,
155 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +0000156 std::list<NotifyArgs> args =
157 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, downGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000158 ASSERT_THAT(args,
159 ElementsAre(VariantWith<NotifyMotionArgs>(
160 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
161 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY |
Harry Cuttsef95e712024-02-16 18:56:39 +0000162 AMOTION_EVENT_BUTTON_SECONDARY))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000163 VariantWith<NotifyMotionArgs>(
164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
165 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +0000166 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000167 VariantWith<NotifyMotionArgs>(
168 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
169 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY),
170 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY |
Harry Cuttsef95e712024-02-16 18:56:39 +0000171 AMOTION_EVENT_BUTTON_SECONDARY)))));
172 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +0000173 Each(VariantWith<NotifyMotionArgs>(AllOf(WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000174 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800175 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000176
177 // Then release the left button
178 Gesture leftUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
179 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT,
180 /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +0000181 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, leftUpGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000182 ASSERT_THAT(args,
183 ElementsAre(VariantWith<NotifyMotionArgs>(
184 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
185 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000186 WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY), WithCoords(0, 0),
187 WithToolType(ToolType::FINGER),
Harry Cutts5f26e952023-11-30 18:20:27 +0000188 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000189
190 // Finally release the right button
191 Gesture rightUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
192 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_RIGHT,
193 /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +0000194 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, rightUpGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000195 ASSERT_THAT(args,
196 ElementsAre(VariantWith<NotifyMotionArgs>(
197 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000198 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000199 VariantWith<NotifyMotionArgs>(
Harry Cuttsef95e712024-02-16 18:56:39 +0000200 WithMotionAction(AMOTION_EVENT_ACTION_UP)),
Harry Cutts5f26e952023-11-30 18:20:27 +0000201 VariantWith<NotifyMotionArgs>(
Harry Cuttsef95e712024-02-16 18:56:39 +0000202 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER))));
203 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +0000204 Each(VariantWith<NotifyMotionArgs>(AllOf(WithButtonState(0), WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000205 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800206 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000207}
208
Harry Cutts379ea422023-12-21 15:31:47 +0000209TEST_F(GestureConverterTest, ButtonDownAfterMoveExitsHover) {
210 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
211 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800212 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cutts379ea422023-12-21 15:31:47 +0000213
214 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
215 std::list<NotifyArgs> args =
216 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
217
218 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
219 /*down=*/GESTURES_BUTTON_LEFT, /*up=*/GESTURES_BUTTON_NONE,
220 /*is_tap=*/false);
221 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, downGesture);
222 ASSERT_THAT(args.front(),
223 VariantWith<NotifyMotionArgs>(
224 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), WithButtonState(0),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000225 WithCoords(0, 0), WithToolType(ToolType::FINGER),
226 WithDisplayId(ADISPLAY_ID_DEFAULT))));
Harry Cutts379ea422023-12-21 15:31:47 +0000227}
228
Harry Cutts4fb941a2022-12-14 19:14:04 +0000229TEST_F(GestureConverterTest, DragWithButton) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000230 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
231 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800232 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cutts4fb941a2022-12-14 19:14:04 +0000233
234 // Press the button
235 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
236 /* down= */ GESTURES_BUTTON_LEFT, /* up= */ GESTURES_BUTTON_NONE,
237 /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +0000238 std::list<NotifyArgs> args =
239 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, downGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000240 ASSERT_THAT(args,
241 ElementsAre(VariantWith<NotifyMotionArgs>(
242 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Harry Cuttsef95e712024-02-16 18:56:39 +0000243 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000244 VariantWith<NotifyMotionArgs>(
245 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
246 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +0000247 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY)))));
248 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +0000249 Each(VariantWith<NotifyMotionArgs>(AllOf(WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000250 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800251 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000252
253 // Move
254 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000255 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000256 ASSERT_THAT(args,
257 ElementsAre(VariantWith<NotifyMotionArgs>(
Prabir Pradhan8b053512024-05-03 23:15:39 +0000258 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithCoords(0, 0),
259 WithRelativeMotion(-5, 10), WithToolType(ToolType::FINGER),
Harry Cutts5f26e952023-11-30 18:20:27 +0000260 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f),
Linnan Li13bf76a2024-05-05 19:18:02 +0800261 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000262
Harry Cutts4fb941a2022-12-14 19:14:04 +0000263 // Release the button
264 Gesture upGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
265 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT,
266 /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +0000267 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, upGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000268 ASSERT_THAT(args,
269 ElementsAre(VariantWith<NotifyMotionArgs>(
270 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000271 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000272 VariantWith<NotifyMotionArgs>(
Harry Cuttsef95e712024-02-16 18:56:39 +0000273 WithMotionAction(AMOTION_EVENT_ACTION_UP)),
Harry Cutts5f26e952023-11-30 18:20:27 +0000274 VariantWith<NotifyMotionArgs>(
Harry Cuttsef95e712024-02-16 18:56:39 +0000275 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER))));
276 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +0000277 Each(VariantWith<NotifyMotionArgs>(AllOf(WithButtonState(0), WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000278 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800279 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000280}
281
Harry Cuttsef400b22022-12-16 21:26:24 +0000282TEST_F(GestureConverterTest, Scroll) {
283 const nsecs_t downTime = 12345;
284 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
285 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800286 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsef400b22022-12-16 21:26:24 +0000287
Harry Cuttsa546ba82023-01-13 17:21:00 +0000288 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000289 std::list<NotifyArgs> args =
290 converter.handleGesture(downTime, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000291 ASSERT_THAT(args,
292 ElementsAre(VariantWith<NotifyMotionArgs>(
293 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000294 WithCoords(0, 0),
Harry Cutts5f26e952023-11-30 18:20:27 +0000295 WithGestureScrollDistance(0, 0, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +0000296 WithDownTime(downTime))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000297 VariantWith<NotifyMotionArgs>(
298 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000299 WithCoords(0, -10),
Harry Cuttsef95e712024-02-16 18:56:39 +0000300 WithGestureScrollDistance(0, 10, EPSILON)))));
301 ASSERT_THAT(args,
302 Each(VariantWith<NotifyMotionArgs>(
303 AllOf(WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
304 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
305 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800306 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsef400b22022-12-16 21:26:24 +0000307
Harry Cuttsa546ba82023-01-13 17:21:00 +0000308 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Arpit Singh33a10a62023-10-12 13:06:54 +0000309 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000310 ASSERT_THAT(args,
311 ElementsAre(VariantWith<NotifyMotionArgs>(
Prabir Pradhan8b053512024-05-03 23:15:39 +0000312 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithCoords(0, -15),
Harry Cutts5f26e952023-11-30 18:20:27 +0000313 WithGestureScrollDistance(0, 5, EPSILON),
314 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
315 WithToolType(ToolType::FINGER),
316 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
Linnan Li13bf76a2024-05-05 19:18:02 +0800317 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsef400b22022-12-16 21:26:24 +0000318
319 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
320 GESTURES_FLING_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000321 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000322 ASSERT_THAT(args,
323 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +0000324 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000325 WithCoords(0, -15),
Harry Cutts379ea422023-12-21 15:31:47 +0000326 WithGestureScrollDistance(0, 0, EPSILON),
327 WithMotionClassification(
328 MotionClassification::TWO_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000329 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE))),
Harry Cutts379ea422023-12-21 15:31:47 +0000330 VariantWith<NotifyMotionArgs>(
331 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000332 WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000333 WithMotionClassification(MotionClassification::NONE)))));
334 ASSERT_THAT(args,
335 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800336 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsef400b22022-12-16 21:26:24 +0000337}
338
339TEST_F(GestureConverterTest, Scroll_Rotated) {
340 const nsecs_t downTime = 12345;
341 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
342 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
343 converter.setOrientation(ui::ROTATION_90);
Linnan Li13bf76a2024-05-05 19:18:02 +0800344 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsef400b22022-12-16 21:26:24 +0000345
Harry Cuttsa546ba82023-01-13 17:21:00 +0000346 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000347 std::list<NotifyArgs> args =
348 converter.handleGesture(downTime, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000349 ASSERT_THAT(args,
350 ElementsAre(VariantWith<NotifyMotionArgs>(
351 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000352 WithCoords(0, 0),
Harry Cutts5f26e952023-11-30 18:20:27 +0000353 WithGestureScrollDistance(0, 0, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +0000354 WithDownTime(downTime))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000355 VariantWith<NotifyMotionArgs>(
356 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000357 WithCoords(-10, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000358 WithGestureScrollDistance(0, 10, EPSILON)))));
359 ASSERT_THAT(args,
360 Each(VariantWith<NotifyMotionArgs>(
361 AllOf(WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
362 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800363 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsef400b22022-12-16 21:26:24 +0000364
Harry Cuttsa546ba82023-01-13 17:21:00 +0000365 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Arpit Singh33a10a62023-10-12 13:06:54 +0000366 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000367 ASSERT_THAT(args,
368 ElementsAre(VariantWith<NotifyMotionArgs>(
Prabir Pradhan8b053512024-05-03 23:15:39 +0000369 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithCoords(-15, 0),
Harry Cutts5f26e952023-11-30 18:20:27 +0000370 WithGestureScrollDistance(0, 5, EPSILON),
371 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
372 WithToolType(ToolType::FINGER),
373 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
Prabir Pradhan8b053512024-05-03 23:15:39 +0000374
Harry Cuttsef400b22022-12-16 21:26:24 +0000375 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
376 GESTURES_FLING_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000377 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000378 ASSERT_THAT(args,
379 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +0000380 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000381 WithCoords(-15, 0),
Harry Cutts379ea422023-12-21 15:31:47 +0000382 WithGestureScrollDistance(0, 0, EPSILON),
383 WithMotionClassification(
Harry Cuttsef95e712024-02-16 18:56:39 +0000384 MotionClassification::TWO_FINGER_SWIPE))),
Harry Cutts379ea422023-12-21 15:31:47 +0000385 VariantWith<NotifyMotionArgs>(
386 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000387 WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000388 WithMotionClassification(MotionClassification::NONE)))));
389 ASSERT_THAT(args,
390 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800391 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsef400b22022-12-16 21:26:24 +0000392}
393
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000394TEST_F(GestureConverterTest, Scroll_ClearsClassificationAfterGesture) {
Harry Cuttsef400b22022-12-16 21:26:24 +0000395 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
396 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800397 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsef400b22022-12-16 21:26:24 +0000398
Harry Cuttsa546ba82023-01-13 17:21:00 +0000399 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000400 std::list<NotifyArgs> args =
401 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttsef400b22022-12-16 21:26:24 +0000402
Harry Cuttsa546ba82023-01-13 17:21:00 +0000403 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Arpit Singh33a10a62023-10-12 13:06:54 +0000404 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
Harry Cuttsef400b22022-12-16 21:26:24 +0000405
406 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
407 GESTURES_FLING_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000408 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
Harry Cuttsef400b22022-12-16 21:26:24 +0000409
410 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000411 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000412 ASSERT_THAT(args,
413 ElementsAre(VariantWith<NotifyMotionArgs>(
414 AllOf(WithMotionClassification(MotionClassification::NONE),
Linnan Li13bf76a2024-05-05 19:18:02 +0800415 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsef400b22022-12-16 21:26:24 +0000416}
417
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000418TEST_F(GestureConverterTest, Scroll_ClearsScrollDistanceAfterGesture) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000419 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
420 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800421 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000422
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000423 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000424 std::list<NotifyArgs> args =
425 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000426
427 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Arpit Singh33a10a62023-10-12 13:06:54 +0000428 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000429
430 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
431 GESTURES_FLING_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000432 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000433
434 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
435 // need to use another gesture type, like pinch.
436 Gesture pinchGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
437 GESTURES_ZOOM_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000438 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, pinchGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000439 ASSERT_FALSE(args.empty());
440 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGestureScrollDistance(0, 0, EPSILON));
441}
442
Prabir Pradhan13acbd22024-05-14 22:36:40 +0000443TEST_F(GestureConverterTest, Scroll_ClearsFakeFingerPositionOnSubsequentScrollGestures) {
444 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
445 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
446 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
447
448 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 15, -10);
449 std::list<NotifyArgs> args =
450 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
451
452 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -2, -5);
453 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
454
455 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
456 GESTURES_FLING_START);
457 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
458 Gesture flingGestureEnd(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, 0,
459 GESTURES_FLING_TAP_DOWN);
460 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGestureEnd);
461
462 // Start a second scoll gesture, and ensure the fake finger is reset to (0, 0), instead of
463 // continuing from the position where the last scroll gesture's fake finger ended.
464 Gesture secondScrollStart(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 2,
465 14);
466 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, secondScrollStart);
467 ASSERT_THAT(args,
468 ElementsAre(VariantWith<NotifyMotionArgs>(
469 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)),
470 VariantWith<NotifyMotionArgs>(
471 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
472 WithCoords(0, 0),
473 WithGestureScrollDistance(0, 0, EPSILON))),
474 VariantWith<NotifyMotionArgs>(
475 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
476 WithCoords(2, 14),
477 WithGestureScrollDistance(-2, -14, EPSILON)))));
478}
479
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000480TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsClassificationAfterGesture) {
481 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
482 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800483 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000484
485 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
486 /*dy=*/0);
Arpit Singh33a10a62023-10-12 13:06:54 +0000487 std::list<NotifyArgs> args =
488 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000489
490 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
Arpit Singh33a10a62023-10-12 13:06:54 +0000491 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000492
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000493 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/-5,
494 /*dy=*/10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000495 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000496 ASSERT_THAT(args,
497 ElementsAre(VariantWith<NotifyMotionArgs>(
498 WithMotionClassification(MotionClassification::NONE))));
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000499}
500
Harry Cutts8743f182023-05-17 12:03:49 +0000501TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsGestureAxesAfterGesture) {
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000502 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
503 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800504 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000505
506 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/5,
507 /*dy=*/5);
Arpit Singh33a10a62023-10-12 13:06:54 +0000508 std::list<NotifyArgs> args =
509 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000510
511 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
Arpit Singh33a10a62023-10-12 13:06:54 +0000512 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000513
514 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
515 // need to use another gesture type, like pinch.
516 Gesture pinchGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
517 GESTURES_ZOOM_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000518 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, pinchGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000519 ASSERT_FALSE(args.empty());
Harry Cutts8743f182023-05-17 12:03:49 +0000520 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
521 AllOf(WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(0)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000522}
523
524TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
525 // The gestures library will "lock" a swipe into the dimension it starts in. For example, if you
526 // start swiping up and then start moving left or right, it'll return gesture events with only Y
527 // deltas until you lift your fingers and start swiping again. That's why each of these tests
528 // only checks movement in one dimension.
529 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
530 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800531 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000532
533 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
534 /* dy= */ 10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000535 std::list<NotifyArgs> args =
536 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000537 ASSERT_EQ(4u, args.size());
Harry Cuttsef95e712024-02-16 18:56:39 +0000538 ASSERT_THAT(args,
539 Each(VariantWith<NotifyMotionArgs>(
540 AllOf(WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
541 WithGestureSwipeFingerCount(3), WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800542 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000543
544 // Three fake fingers should be created. We don't actually care where they are, so long as they
545 // move appropriately.
546 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
547 ASSERT_THAT(arg,
548 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +0000549 WithPointerCount(1u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000550 PointerCoords finger0Start = arg.pointerCoords[0];
551 args.pop_front();
552 arg = std::get<NotifyMotionArgs>(args.front());
553 ASSERT_THAT(arg,
554 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
555 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000556 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000557 PointerCoords finger1Start = arg.pointerCoords[1];
558 args.pop_front();
559 arg = std::get<NotifyMotionArgs>(args.front());
560 ASSERT_THAT(arg,
561 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
562 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000563 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000564 PointerCoords finger2Start = arg.pointerCoords[2];
565 args.pop_front();
566
567 arg = std::get<NotifyMotionArgs>(args.front());
568 ASSERT_THAT(arg,
569 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000570 WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000571 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
572 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
573 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
574 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 10);
575 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 10);
576 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 10);
577
578 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
579 /* dx= */ 0, /* dy= */ 5);
Arpit Singh33a10a62023-10-12 13:06:54 +0000580 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000581 ASSERT_EQ(1u, args.size());
582 arg = std::get<NotifyMotionArgs>(args.front());
583 ASSERT_THAT(arg,
584 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cutts8743f182023-05-17 12:03:49 +0000585 WithGestureOffset(0, -0.005, EPSILON), WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000586 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000587 WithPointerCount(3u), WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800588 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000589 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
590 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
591 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
592 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 15);
593 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 15);
594 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 15);
595
596 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
Arpit Singh33a10a62023-10-12 13:06:54 +0000597 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000598 ASSERT_THAT(args,
599 ElementsAre(VariantWith<NotifyMotionArgs>(
600 AllOf(WithMotionAction(
601 AMOTION_EVENT_ACTION_POINTER_UP |
602 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
603 WithGestureOffset(0, 0, EPSILON),
604 WithGestureSwipeFingerCount(3),
605 WithMotionClassification(
606 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000607 WithPointerCount(3u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000608 VariantWith<NotifyMotionArgs>(
609 AllOf(WithMotionAction(
610 AMOTION_EVENT_ACTION_POINTER_UP |
611 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
612 WithGestureOffset(0, 0, EPSILON),
613 WithGestureSwipeFingerCount(3),
614 WithMotionClassification(
615 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000616 WithPointerCount(2u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000617 VariantWith<NotifyMotionArgs>(
618 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
619 WithGestureOffset(0, 0, EPSILON),
620 WithGestureSwipeFingerCount(3),
621 WithMotionClassification(
622 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000623 WithPointerCount(1u))),
Harry Cutts379ea422023-12-21 15:31:47 +0000624 VariantWith<NotifyMotionArgs>(
625 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000626 WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000627 WithMotionClassification(MotionClassification::NONE)))));
628 ASSERT_THAT(args,
629 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800630 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000631}
632
Harry Cutts94f5bd52023-01-06 18:02:18 +0000633TEST_F(GestureConverterTest, ThreeFingerSwipe_Rotated) {
634 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
635 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
636 converter.setOrientation(ui::ROTATION_90);
Linnan Li13bf76a2024-05-05 19:18:02 +0800637 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cutts94f5bd52023-01-06 18:02:18 +0000638
639 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
640 /* dy= */ 10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000641 std::list<NotifyArgs> args =
642 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cutts94f5bd52023-01-06 18:02:18 +0000643 ASSERT_EQ(4u, args.size());
Linnan Li13bf76a2024-05-05 19:18:02 +0800644 ASSERT_THAT(args, Each(VariantWith<NotifyMotionArgs>(WithDisplayId(ui::ADISPLAY_ID_DEFAULT))));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000645
646 // Three fake fingers should be created. We don't actually care where they are, so long as they
647 // move appropriately.
648 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
649 ASSERT_THAT(arg,
650 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +0000651 WithPointerCount(1u)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000652 PointerCoords finger0Start = arg.pointerCoords[0];
653 args.pop_front();
654 arg = std::get<NotifyMotionArgs>(args.front());
655 ASSERT_THAT(arg,
656 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
657 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000658 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000659 PointerCoords finger1Start = arg.pointerCoords[1];
660 args.pop_front();
661 arg = std::get<NotifyMotionArgs>(args.front());
662 ASSERT_THAT(arg,
663 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
664 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000665 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000666 PointerCoords finger2Start = arg.pointerCoords[2];
667 args.pop_front();
668
669 arg = std::get<NotifyMotionArgs>(args.front());
670 ASSERT_THAT(arg,
671 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000672 WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000673 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 10);
674 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 10);
675 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 10);
676 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
677 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
678 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
679
680 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
681 /* dx= */ 0, /* dy= */ 5);
Arpit Singh33a10a62023-10-12 13:06:54 +0000682 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
Harry Cutts94f5bd52023-01-06 18:02:18 +0000683 ASSERT_EQ(1u, args.size());
684 arg = std::get<NotifyMotionArgs>(args.front());
685 ASSERT_THAT(arg,
686 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Josep del Riod0746382023-07-29 13:18:25 +0000687 WithGestureOffset(0, -0.005, EPSILON), WithPointerCount(3u),
Linnan Li13bf76a2024-05-05 19:18:02 +0800688 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000689 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 15);
690 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 15);
691 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 15);
692 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
693 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
694 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
695
696 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
Arpit Singh33a10a62023-10-12 13:06:54 +0000697 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000698 ASSERT_THAT(args,
699 ElementsAre(VariantWith<NotifyMotionArgs>(
700 AllOf(WithMotionAction(
701 AMOTION_EVENT_ACTION_POINTER_UP |
702 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000703 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000704 VariantWith<NotifyMotionArgs>(
705 AllOf(WithMotionAction(
706 AMOTION_EVENT_ACTION_POINTER_UP |
707 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000708 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000709 VariantWith<NotifyMotionArgs>(
710 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Harry Cuttsef95e712024-02-16 18:56:39 +0000711 WithGestureOffset(0, 0, EPSILON), WithPointerCount(1u))),
Harry Cutts379ea422023-12-21 15:31:47 +0000712 VariantWith<NotifyMotionArgs>(
Prabir Pradhan8b053512024-05-03 23:15:39 +0000713 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)))));
Harry Cuttsef95e712024-02-16 18:56:39 +0000714 ASSERT_THAT(args, Each(VariantWith<NotifyMotionArgs>(WithDisplayId(ADISPLAY_ID_DEFAULT))));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000715}
716
Harry Cuttsc5748d12022-12-02 17:30:18 +0000717TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
718 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
719 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800720 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000721
722 Gesture startGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
723 /* dx= */ 10, /* dy= */ 0);
Arpit Singh33a10a62023-10-12 13:06:54 +0000724 std::list<NotifyArgs> args =
725 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000726 ASSERT_EQ(5u, args.size());
Harry Cuttsef95e712024-02-16 18:56:39 +0000727 ASSERT_THAT(args,
728 Each(VariantWith<NotifyMotionArgs>(
729 AllOf(WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
730 WithGestureSwipeFingerCount(4), WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800731 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000732
733 // Four fake fingers should be created. We don't actually care where they are, so long as they
734 // move appropriately.
735 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
736 ASSERT_THAT(arg,
737 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +0000738 WithPointerCount(1u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000739 PointerCoords finger0Start = arg.pointerCoords[0];
740 args.pop_front();
741 arg = std::get<NotifyMotionArgs>(args.front());
742 ASSERT_THAT(arg,
743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
744 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000745 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000746 PointerCoords finger1Start = arg.pointerCoords[1];
747 args.pop_front();
748 arg = std::get<NotifyMotionArgs>(args.front());
749 ASSERT_THAT(arg,
750 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
751 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000752 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000753 PointerCoords finger2Start = arg.pointerCoords[2];
754 args.pop_front();
755 arg = std::get<NotifyMotionArgs>(args.front());
756 ASSERT_THAT(arg,
757 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
758 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cuttsef95e712024-02-16 18:56:39 +0000759 WithGestureOffset(0, 0, EPSILON), WithPointerCount(4u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000760 PointerCoords finger3Start = arg.pointerCoords[3];
761 args.pop_front();
762
763 arg = std::get<NotifyMotionArgs>(args.front());
764 ASSERT_THAT(arg,
765 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000766 WithGestureOffset(0.01, 0, EPSILON), WithPointerCount(4u)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000767 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10);
768 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 10);
769 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 10);
770 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 10);
771 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
772 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
773 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
774 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
775
776 Gesture continueGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
777 /* dx= */ 5, /* dy= */ 0);
Arpit Singh33a10a62023-10-12 13:06:54 +0000778 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000779 ASSERT_EQ(1u, args.size());
780 arg = std::get<NotifyMotionArgs>(args.front());
781 ASSERT_THAT(arg,
782 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cutts8743f182023-05-17 12:03:49 +0000783 WithGestureOffset(0.005, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000784 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000785 WithPointerCount(4u), WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800786 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000787 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15);
788 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 15);
789 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 15);
790 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 15);
791 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
792 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
793 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
794 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
795
796 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
Arpit Singh33a10a62023-10-12 13:06:54 +0000797 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000798 ASSERT_THAT(args,
799 ElementsAre(VariantWith<NotifyMotionArgs>(
800 AllOf(WithMotionAction(
801 AMOTION_EVENT_ACTION_POINTER_UP |
802 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
803 WithGestureOffset(0, 0, EPSILON),
804 WithGestureSwipeFingerCount(4),
805 WithMotionClassification(
806 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000807 WithPointerCount(4u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000808 VariantWith<NotifyMotionArgs>(
809 AllOf(WithMotionAction(
810 AMOTION_EVENT_ACTION_POINTER_UP |
811 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
812 WithGestureOffset(0, 0, EPSILON),
813 WithGestureSwipeFingerCount(4),
814 WithMotionClassification(
815 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000816 WithPointerCount(3u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000817 VariantWith<NotifyMotionArgs>(
818 AllOf(WithMotionAction(
819 AMOTION_EVENT_ACTION_POINTER_UP |
820 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
821 WithGestureOffset(0, 0, EPSILON),
822 WithGestureSwipeFingerCount(4),
823 WithMotionClassification(
824 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000825 WithPointerCount(2u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000826 VariantWith<NotifyMotionArgs>(
827 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
828 WithGestureOffset(0, 0, EPSILON),
829 WithGestureSwipeFingerCount(4),
830 WithMotionClassification(
831 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +0000832 WithPointerCount(1u))),
Harry Cutts379ea422023-12-21 15:31:47 +0000833 VariantWith<NotifyMotionArgs>(
834 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000835 WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000836 WithMotionClassification(MotionClassification::NONE)))));
837 ASSERT_THAT(args,
838 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800839 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000840}
841
Harry Cuttsb1e83552022-12-20 11:02:26 +0000842TEST_F(GestureConverterTest, Pinch_Inwards) {
843 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
844 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800845 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000846
847 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
848 GESTURES_ZOOM_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000849 std::list<NotifyArgs> args =
850 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000851 ASSERT_THAT(args,
852 ElementsAre(VariantWith<NotifyMotionArgs>(
853 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000854 WithCoords(-100, 0), WithPointerCount(1u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000855 VariantWith<NotifyMotionArgs>(
856 AllOf(WithMotionAction(
857 AMOTION_EVENT_ACTION_POINTER_DOWN |
858 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000859 WithPointerCoords(1, 100, 0), WithPointerCount(2u)))));
Harry Cuttsef95e712024-02-16 18:56:39 +0000860 ASSERT_THAT(args,
861 Each(VariantWith<NotifyMotionArgs>(
862 AllOf(WithMotionClassification(MotionClassification::PINCH),
863 WithGesturePinchScaleFactor(1.0f, EPSILON),
864 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800865 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000866
867 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
868 /* dz= */ 0.8, GESTURES_ZOOM_UPDATE);
Arpit Singh33a10a62023-10-12 13:06:54 +0000869 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, updateGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000870 ASSERT_THAT(args,
871 ElementsAre(VariantWith<NotifyMotionArgs>(
872 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
873 WithMotionClassification(MotionClassification::PINCH),
874 WithGesturePinchScaleFactor(0.8f, EPSILON),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000875 WithPointerCoords(0, -80, 0), WithPointerCoords(1, 80, 0),
Harry Cutts5f26e952023-11-30 18:20:27 +0000876 WithPointerCount(2u), WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800877 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000878
879 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
880 GESTURES_ZOOM_END);
Arpit Singh33a10a62023-10-12 13:06:54 +0000881 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, endGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000882 ASSERT_THAT(args,
883 ElementsAre(VariantWith<NotifyMotionArgs>(
884 AllOf(WithMotionAction(
885 AMOTION_EVENT_ACTION_POINTER_UP |
886 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
887 WithMotionClassification(MotionClassification::PINCH),
888 WithGesturePinchScaleFactor(1.0f, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +0000889 WithPointerCount(2u))),
Harry Cutts5f26e952023-11-30 18:20:27 +0000890 VariantWith<NotifyMotionArgs>(
891 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
892 WithMotionClassification(MotionClassification::PINCH),
893 WithGesturePinchScaleFactor(1.0f, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +0000894 WithPointerCount(1u))),
Harry Cutts379ea422023-12-21 15:31:47 +0000895 VariantWith<NotifyMotionArgs>(
896 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000897 WithCoords(0, 0),
898 WithMotionClassification(MotionClassification::NONE)))));
899 ASSERT_THAT(args,
900 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
901 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
902}
903
904TEST_F(GestureConverterTest, Pinch_Outwards) {
905 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
906 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
907 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
908
909 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
910 GESTURES_ZOOM_START);
911 std::list<NotifyArgs> args =
912 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
913 ASSERT_THAT(args,
914 ElementsAre(VariantWith<NotifyMotionArgs>(
915 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
916 WithCoords(-100, 0), WithPointerCount(1u))),
917 VariantWith<NotifyMotionArgs>(
918 AllOf(WithMotionAction(
919 AMOTION_EVENT_ACTION_POINTER_DOWN |
920 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
921 WithPointerCoords(1, 100, 0), WithPointerCount(2u)))));
922 ASSERT_THAT(args,
923 Each(VariantWith<NotifyMotionArgs>(
924 AllOf(WithMotionClassification(MotionClassification::PINCH),
925 WithGesturePinchScaleFactor(1.0f, EPSILON),
926 WithToolType(ToolType::FINGER),
927 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
928
929 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
930 /* dz= */ 1.1, GESTURES_ZOOM_UPDATE);
931 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, updateGesture);
932 ASSERT_THAT(args,
933 ElementsAre(VariantWith<NotifyMotionArgs>(
934 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
935 WithMotionClassification(MotionClassification::PINCH),
936 WithGesturePinchScaleFactor(1.1f, EPSILON),
937 WithPointerCoords(0, -110, 0), WithPointerCoords(1, 110, 0),
938 WithPointerCount(2u), WithToolType(ToolType::FINGER),
939 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
940
941 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
942 GESTURES_ZOOM_END);
943 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, endGesture);
944 ASSERT_THAT(args,
945 ElementsAre(VariantWith<NotifyMotionArgs>(
946 AllOf(WithMotionAction(
947 AMOTION_EVENT_ACTION_POINTER_UP |
948 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
949 WithMotionClassification(MotionClassification::PINCH),
950 WithGesturePinchScaleFactor(1.0f, EPSILON),
951 WithPointerCount(2u))),
952 VariantWith<NotifyMotionArgs>(
953 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
954 WithMotionClassification(MotionClassification::PINCH),
955 WithGesturePinchScaleFactor(1.0f, EPSILON),
956 WithPointerCount(1u))),
957 VariantWith<NotifyMotionArgs>(
958 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
959 WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +0000960 WithMotionClassification(MotionClassification::NONE)))));
961 ASSERT_THAT(args,
962 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +0800963 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000964}
965
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000966TEST_F(GestureConverterTest, Pinch_ClearsClassificationAfterGesture) {
Harry Cuttsb1e83552022-12-20 11:02:26 +0000967 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
968 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800969 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000970
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000971 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
Harry Cuttsb1e83552022-12-20 11:02:26 +0000972 GESTURES_ZOOM_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000973 std::list<NotifyArgs> args =
974 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000975
976 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000977 /*dz=*/1.2, GESTURES_ZOOM_UPDATE);
Arpit Singh33a10a62023-10-12 13:06:54 +0000978 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, updateGesture);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000979
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000980 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
Harry Cuttsb1e83552022-12-20 11:02:26 +0000981 GESTURES_ZOOM_END);
Arpit Singh33a10a62023-10-12 13:06:54 +0000982 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, endGesture);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000983
984 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
Arpit Singh33a10a62023-10-12 13:06:54 +0000985 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +0000986 ASSERT_THAT(args,
987 ElementsAre(VariantWith<NotifyMotionArgs>(
988 WithMotionClassification(MotionClassification::NONE))));
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000989}
990
991TEST_F(GestureConverterTest, Pinch_ClearsScaleFactorAfterGesture) {
992 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
993 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +0800994 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000995
996 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
997 GESTURES_ZOOM_START);
Arpit Singh33a10a62023-10-12 13:06:54 +0000998 std::list<NotifyArgs> args =
999 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +00001000
1001 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1002 /*dz=*/1.2, GESTURES_ZOOM_UPDATE);
Arpit Singh33a10a62023-10-12 13:06:54 +00001003 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, updateGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +00001004
1005 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
1006 GESTURES_ZOOM_END);
Arpit Singh33a10a62023-10-12 13:06:54 +00001007 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, endGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +00001008
1009 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
1010 // need to use another gesture type, like scroll.
1011 Gesture scrollGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/1,
1012 /*dy=*/0);
Arpit Singh33a10a62023-10-12 13:06:54 +00001013 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, scrollGesture);
Harry Cuttsa5f98c92023-05-17 15:05:44 +00001014 ASSERT_FALSE(args.empty());
1015 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGesturePinchScaleFactor(0, EPSILON));
Harry Cuttsb1e83552022-12-20 11:02:26 +00001016}
1017
Harry Cuttse9b71422023-03-14 16:54:44 +00001018TEST_F(GestureConverterTest, ResetWithButtonPressed) {
1019 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1020 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001021 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttse9b71422023-03-14 16:54:44 +00001022
1023 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1024 /*down=*/GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT,
1025 /*up=*/GESTURES_BUTTON_NONE, /*is_tap=*/false);
Arpit Singh33a10a62023-10-12 13:06:54 +00001026 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, downGesture);
Harry Cuttse9b71422023-03-14 16:54:44 +00001027
1028 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
Harry Cutts5f26e952023-11-30 18:20:27 +00001029 ASSERT_THAT(args,
1030 ElementsAre(VariantWith<NotifyMotionArgs>(
1031 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1032 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001033 WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001034 VariantWith<NotifyMotionArgs>(
1035 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1036 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001037 WithButtonState(0))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001038 VariantWith<NotifyMotionArgs>(
1039 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Harry Cuttsef95e712024-02-16 18:56:39 +00001040 WithButtonState(0))),
Harry Cutts379ea422023-12-21 15:31:47 +00001041 VariantWith<NotifyMotionArgs>(
1042 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Harry Cuttsef95e712024-02-16 18:56:39 +00001043 WithButtonState(0)))));
1044 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +00001045 Each(VariantWith<NotifyMotionArgs>(AllOf(WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +00001046 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +08001047 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttse9b71422023-03-14 16:54:44 +00001048}
1049
1050TEST_F(GestureConverterTest, ResetDuringScroll) {
1051 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1052 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001053 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttse9b71422023-03-14 16:54:44 +00001054
1055 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Arpit Singh33a10a62023-10-12 13:06:54 +00001056 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttse9b71422023-03-14 16:54:44 +00001057
1058 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
Harry Cutts5f26e952023-11-30 18:20:27 +00001059 ASSERT_THAT(args,
1060 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan8b053512024-05-03 23:15:39 +00001062 WithCoords(0, -10),
Harry Cutts379ea422023-12-21 15:31:47 +00001063 WithGestureScrollDistance(0, 0, EPSILON),
1064 WithMotionClassification(
1065 MotionClassification::TWO_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +00001066 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE))),
Harry Cutts379ea422023-12-21 15:31:47 +00001067 VariantWith<NotifyMotionArgs>(
1068 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Prabir Pradhan8b053512024-05-03 23:15:39 +00001069 WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +00001070 WithMotionClassification(MotionClassification::NONE)))));
1071 ASSERT_THAT(args,
1072 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +08001073 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttse9b71422023-03-14 16:54:44 +00001074}
1075
1076TEST_F(GestureConverterTest, ResetDuringThreeFingerSwipe) {
1077 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1078 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001079 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttse9b71422023-03-14 16:54:44 +00001080
1081 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
1082 /*dy=*/10);
Arpit Singh33a10a62023-10-12 13:06:54 +00001083 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttse9b71422023-03-14 16:54:44 +00001084
1085 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
Harry Cutts5f26e952023-11-30 18:20:27 +00001086 ASSERT_THAT(args,
1087 ElementsAre(VariantWith<NotifyMotionArgs>(
1088 AllOf(WithMotionAction(
1089 AMOTION_EVENT_ACTION_POINTER_UP |
1090 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1091 WithGestureOffset(0, 0, EPSILON),
1092 WithMotionClassification(
1093 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +00001094 WithPointerCount(3u))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001095 VariantWith<NotifyMotionArgs>(
1096 AllOf(WithMotionAction(
1097 AMOTION_EVENT_ACTION_POINTER_UP |
1098 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1099 WithGestureOffset(0, 0, EPSILON),
1100 WithMotionClassification(
1101 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +00001102 WithPointerCount(2u))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001103 VariantWith<NotifyMotionArgs>(
1104 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1105 WithGestureOffset(0, 0, EPSILON),
1106 WithMotionClassification(
1107 MotionClassification::MULTI_FINGER_SWIPE),
Harry Cuttsef95e712024-02-16 18:56:39 +00001108 WithPointerCount(1u))),
Harry Cutts379ea422023-12-21 15:31:47 +00001109 VariantWith<NotifyMotionArgs>(
1110 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Harry Cuttsef95e712024-02-16 18:56:39 +00001111 WithMotionClassification(MotionClassification::NONE)))));
1112 ASSERT_THAT(args,
1113 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +08001114 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttse9b71422023-03-14 16:54:44 +00001115}
1116
1117TEST_F(GestureConverterTest, ResetDuringPinch) {
1118 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1119 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001120 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cuttse9b71422023-03-14 16:54:44 +00001121
1122 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
1123 GESTURES_ZOOM_START);
Arpit Singh33a10a62023-10-12 13:06:54 +00001124 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
Harry Cuttse9b71422023-03-14 16:54:44 +00001125
1126 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
Harry Cutts5f26e952023-11-30 18:20:27 +00001127 ASSERT_THAT(args,
1128 ElementsAre(VariantWith<NotifyMotionArgs>(
1129 AllOf(WithMotionAction(
1130 AMOTION_EVENT_ACTION_POINTER_UP |
1131 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1132 WithMotionClassification(MotionClassification::PINCH),
1133 WithGesturePinchScaleFactor(1.0f, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +00001134 WithPointerCount(2u))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001135 VariantWith<NotifyMotionArgs>(
1136 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1137 WithMotionClassification(MotionClassification::PINCH),
1138 WithGesturePinchScaleFactor(1.0f, EPSILON),
Harry Cuttsef95e712024-02-16 18:56:39 +00001139 WithPointerCount(1u))),
Harry Cutts379ea422023-12-21 15:31:47 +00001140 VariantWith<NotifyMotionArgs>(
1141 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Prabir Pradhan8b053512024-05-03 23:15:39 +00001142 WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +00001143 WithMotionClassification(MotionClassification::NONE)))));
1144 ASSERT_THAT(args,
1145 Each(VariantWith<NotifyMotionArgs>(AllOf(WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +08001146 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Harry Cuttse9b71422023-03-14 16:54:44 +00001147}
1148
Prabir Pradhanf7c4b0e2023-05-10 21:25:16 +00001149TEST_F(GestureConverterTest, FlingTapDown) {
1150 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1151 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001152 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Prabir Pradhanf7c4b0e2023-05-10 21:25:16 +00001153
1154 Gesture tapDownGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1155 /*vx=*/0.f, /*vy=*/0.f, GESTURES_FLING_TAP_DOWN);
Arpit Singh33a10a62023-10-12 13:06:54 +00001156 std::list<NotifyArgs> args =
1157 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, tapDownGesture);
Prabir Pradhanf7c4b0e2023-05-10 21:25:16 +00001158
1159 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
Prabir Pradhan8b053512024-05-03 23:15:39 +00001160 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), WithCoords(0, 0),
1161 WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
1162 WithButtonState(0), WithPressure(0.0f), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhanf7c4b0e2023-05-10 21:25:16 +00001163}
1164
Harry Cutts39648ab2024-02-15 14:23:50 +00001165TEST_F(GestureConverterTest, FlingTapDownAfterScrollStopsFling) {
1166 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1167 input_flags::enable_touchpad_fling_stop(true);
1168 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001169 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Harry Cutts39648ab2024-02-15 14:23:50 +00001170
1171 Gesture scrollGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
1172 std::list<NotifyArgs> args =
1173 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, scrollGesture);
1174 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
1175 GESTURES_FLING_START);
1176 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
1177
1178 Gesture tapDownGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1179 /*vx=*/0.f, /*vy=*/0.f, GESTURES_FLING_TAP_DOWN);
1180 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, tapDownGesture);
1181 ASSERT_THAT(args,
1182 ElementsAre(VariantWith<NotifyMotionArgs>(
1183 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)),
1184 VariantWith<NotifyMotionArgs>(
1185 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)),
1186 VariantWith<NotifyMotionArgs>(
1187 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)),
1188 VariantWith<NotifyMotionArgs>(
Harry Cutts574781a2024-04-23 15:30:45 +00001189 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER))));
Harry Cutts39648ab2024-02-15 14:23:50 +00001190 ASSERT_THAT(args,
Harry Cutts574781a2024-04-23 15:30:45 +00001191 Each(VariantWith<NotifyMotionArgs>(
Prabir Pradhan8b053512024-05-03 23:15:39 +00001192 AllOf(WithCoords(0, 0), WithToolType(ToolType::FINGER),
Harry Cutts574781a2024-04-23 15:30:45 +00001193 WithDisplayId(ADISPLAY_ID_DEFAULT),
1194 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE)))));
Harry Cutts39648ab2024-02-15 14:23:50 +00001195}
1196
Arpit Singha5ea7c12023-07-05 15:39:25 +00001197TEST_F(GestureConverterTest, Tap) {
1198 // Tap should produce button press/release events
1199 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1200 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001201 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001202
1203 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1204 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
Arpit Singh33a10a62023-10-12 13:06:54 +00001205 std::list<NotifyArgs> args =
1206 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
Harry Cutts379ea422023-12-21 15:31:47 +00001207 // We don't need to check args here, since it's covered by the FlingTapDown test.
Arpit Singha5ea7c12023-07-05 15:39:25 +00001208
1209 Gesture tapGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1210 /* down= */ GESTURES_BUTTON_LEFT,
1211 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
Arpit Singh33a10a62023-10-12 13:06:54 +00001212 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, tapGesture);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001213
Harry Cutts5f26e952023-11-30 18:20:27 +00001214 ASSERT_THAT(args,
1215 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001216 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
Harry Cuttsef95e712024-02-16 18:56:39 +00001217 WithButtonState(0), WithPressure(0.0f))),
Harry Cutts379ea422023-12-21 15:31:47 +00001218 VariantWith<NotifyMotionArgs>(
Harry Cutts5f26e952023-11-30 18:20:27 +00001219 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Harry Cutts5f26e952023-11-30 18:20:27 +00001220 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001221 WithPressure(1.0f))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001222 VariantWith<NotifyMotionArgs>(
1223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1224 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1225 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001226 WithPressure(1.0f))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001227 VariantWith<NotifyMotionArgs>(
1228 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1229 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001230 WithButtonState(0), WithPressure(1.0f))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001231 VariantWith<NotifyMotionArgs>(
1232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Harry Cuttsef95e712024-02-16 18:56:39 +00001233 WithButtonState(0), WithPressure(0.0f))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001234 VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001235 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Harry Cuttsef95e712024-02-16 18:56:39 +00001236 WithButtonState(0), WithPressure(0.0f)))));
1237 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +00001238 Each(VariantWith<NotifyMotionArgs>(AllOf(WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +00001239 WithRelativeMotion(0.f, 0.f),
1240 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +08001241 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001242}
1243
1244TEST_F(GestureConverterTest, Click) {
1245 // Click should produce button press/release events
1246 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1247 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001248 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001249
1250 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1251 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
Arpit Singh33a10a62023-10-12 13:06:54 +00001252 std::list<NotifyArgs> args =
1253 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
Harry Cutts379ea422023-12-21 15:31:47 +00001254 // We don't need to check args here, since it's covered by the FlingTapDown test.
Arpit Singha5ea7c12023-07-05 15:39:25 +00001255
1256 Gesture buttonDownGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1257 /* down= */ GESTURES_BUTTON_LEFT,
1258 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +00001259 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, buttonDownGesture);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001260
Harry Cutts5f26e952023-11-30 18:20:27 +00001261 ASSERT_THAT(args,
1262 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001263 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
Harry Cuttsef95e712024-02-16 18:56:39 +00001264 WithButtonState(0), WithPressure(0.0f))),
Harry Cutts379ea422023-12-21 15:31:47 +00001265 VariantWith<NotifyMotionArgs>(
Harry Cutts5f26e952023-11-30 18:20:27 +00001266 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Harry Cutts5f26e952023-11-30 18:20:27 +00001267 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001268 WithPressure(1.0f))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001269 VariantWith<NotifyMotionArgs>(
1270 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1271 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1272 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001273 WithPressure(1.0f)))));
1274 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +00001275 Each(VariantWith<NotifyMotionArgs>(AllOf(WithCoords(0, 0),
Harry Cuttsef95e712024-02-16 18:56:39 +00001276 WithRelativeMotion(0.f, 0.f),
1277 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +08001278 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001279
1280 Gesture buttonUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1281 /* down= */ GESTURES_BUTTON_NONE,
1282 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +00001283 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, buttonUpGesture);
Prabir Pradhan8b053512024-05-03 23:15:39 +00001284
Harry Cutts5f26e952023-11-30 18:20:27 +00001285 ASSERT_THAT(args,
1286 ElementsAre(VariantWith<NotifyMotionArgs>(
1287 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1288 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001289 WithPressure(1.0f))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001290 VariantWith<NotifyMotionArgs>(
1291 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Harry Cuttsef95e712024-02-16 18:56:39 +00001292 WithPressure(0.0f))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001293 VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001294 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Harry Cuttsef95e712024-02-16 18:56:39 +00001295 WithPressure(0.0f)))));
1296 ASSERT_THAT(args,
Prabir Pradhan8b053512024-05-03 23:15:39 +00001297 Each(VariantWith<NotifyMotionArgs>(AllOf(WithButtonState(0), WithCoords(0, 0),
1298 WithRelativeMotion(0.f, 0.f),
1299 WithToolType(ToolType::FINGER),
1300 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001301}
1302
Arpit Singh3d84add2023-10-10 19:08:29 +00001303TEST_F_WITH_FLAGS(GestureConverterTest, TapWithTapToClickDisabled,
Arpit Singh82b27a02023-10-16 11:02:19 +00001304 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION),
1305 REQUIRES_FLAGS_DISABLED(TOUCHPAD_PALM_REJECTION_V2)) {
1306 nsecs_t currentTime = ARBITRARY_GESTURE_TIME;
1307
Arpit Singha5ea7c12023-07-05 15:39:25 +00001308 // Tap should be ignored when disabled
1309 mReader->getContext()->setPreventingTouchpadTaps(true);
1310
1311 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1312 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001313 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001314
Arpit Singh82b27a02023-10-16 11:02:19 +00001315 Gesture flingGesture(kGestureFling, currentTime, currentTime, /* vx= */ 0,
Arpit Singha5ea7c12023-07-05 15:39:25 +00001316 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
Arpit Singh33a10a62023-10-12 13:06:54 +00001317 std::list<NotifyArgs> args =
Arpit Singh82b27a02023-10-16 11:02:19 +00001318 converter.handleGesture(currentTime, currentTime, currentTime, flingGesture);
Harry Cutts379ea422023-12-21 15:31:47 +00001319 // We don't need to check args here, since it's covered by the FlingTapDown test.
Arpit Singha5ea7c12023-07-05 15:39:25 +00001320
Arpit Singh82b27a02023-10-16 11:02:19 +00001321 Gesture tapGesture(kGestureButtonsChange, currentTime, currentTime,
Arpit Singha5ea7c12023-07-05 15:39:25 +00001322 /* down= */ GESTURES_BUTTON_LEFT,
1323 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
Arpit Singh82b27a02023-10-16 11:02:19 +00001324 args = converter.handleGesture(currentTime, currentTime, currentTime, tapGesture);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001325
1326 // no events should be generated
1327 ASSERT_EQ(0u, args.size());
1328
1329 // Future taps should be re-enabled
1330 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1331}
1332
Arpit Singh82b27a02023-10-16 11:02:19 +00001333TEST_F_WITH_FLAGS(GestureConverterTest, TapWithTapToClickDisabledWithDelay,
1334 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION_V2)) {
1335 nsecs_t currentTime = ARBITRARY_GESTURE_TIME;
1336
1337 // Tap should be ignored when disabled
1338 mReader->getContext()->setPreventingTouchpadTaps(true);
1339
1340 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1341 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001342 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Arpit Singh82b27a02023-10-16 11:02:19 +00001343
1344 Gesture flingGesture(kGestureFling, currentTime, currentTime, /* vx= */ 0,
1345 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1346 std::list<NotifyArgs> args =
1347 converter.handleGesture(currentTime, currentTime, currentTime, flingGesture);
Harry Cutts379ea422023-12-21 15:31:47 +00001348 // We don't need to check args here, since it's covered by the FlingTapDown test.
Arpit Singh82b27a02023-10-16 11:02:19 +00001349
1350 Gesture tapGesture(kGestureButtonsChange, currentTime, currentTime,
1351 /* down= */ GESTURES_BUTTON_LEFT,
1352 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1353 args = converter.handleGesture(currentTime, currentTime, currentTime, tapGesture);
1354
1355 // no events should be generated
1356 ASSERT_EQ(0u, args.size());
1357
1358 // Future taps should be re-enabled
1359 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1360
1361 // taps before the threshold should still be ignored
1362 currentTime += TAP_ENABLE_DELAY_NANOS.count();
1363 flingGesture = Gesture(kGestureFling, currentTime, currentTime, /* vx= */ 0,
1364 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1365 args = converter.handleGesture(currentTime, currentTime, currentTime, flingGesture);
1366
1367 ASSERT_EQ(1u, args.size());
1368 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1369 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithRelativeMotion(0, 0)));
1370
1371 tapGesture = Gesture(kGestureButtonsChange, currentTime, currentTime,
1372 /* down= */ GESTURES_BUTTON_LEFT,
1373 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1374 args = converter.handleGesture(currentTime, currentTime, currentTime, tapGesture);
1375
1376 // no events should be generated
1377 ASSERT_EQ(0u, args.size());
1378
1379 // taps after the threshold should be recognised
1380 currentTime += 1;
1381 flingGesture = Gesture(kGestureFling, currentTime, currentTime, /* vx= */ 0,
1382 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1383 args = converter.handleGesture(currentTime, currentTime, currentTime, flingGesture);
1384
1385 ASSERT_EQ(1u, args.size());
1386 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1387 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithRelativeMotion(0, 0)));
1388
1389 tapGesture = Gesture(kGestureButtonsChange, currentTime, currentTime,
1390 /* down= */ GESTURES_BUTTON_LEFT,
1391 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1392 args = converter.handleGesture(currentTime, currentTime, currentTime, tapGesture);
Harry Cuttsef95e712024-02-16 18:56:39 +00001393 ASSERT_THAT(args,
1394 ElementsAre(VariantWith<NotifyMotionArgs>(
1395 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1396 WithButtonState(0))),
1397 VariantWith<NotifyMotionArgs>(
1398 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1399 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))),
1400 VariantWith<NotifyMotionArgs>(
1401 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1402 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1403 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))),
1404 VariantWith<NotifyMotionArgs>(
1405 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1406 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1407 WithButtonState(0))),
1408 VariantWith<NotifyMotionArgs>(
1409 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1410 WithButtonState(0))),
1411 VariantWith<NotifyMotionArgs>(
1412 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1413 WithButtonState(0)))));
1414 ASSERT_THAT(args, Each(VariantWith<NotifyMotionArgs>(WithRelativeMotion(0.f, 0.f))));
Arpit Singh82b27a02023-10-16 11:02:19 +00001415}
1416
Arpit Singh3d84add2023-10-10 19:08:29 +00001417TEST_F_WITH_FLAGS(GestureConverterTest, ClickWithTapToClickDisabled,
1418 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION)) {
Arpit Singha5ea7c12023-07-05 15:39:25 +00001419 // Click should still produce button press/release events
1420 mReader->getContext()->setPreventingTouchpadTaps(true);
1421
1422 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1423 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001424 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001425
1426 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1427 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
Arpit Singh33a10a62023-10-12 13:06:54 +00001428 std::list<NotifyArgs> args =
1429 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
Harry Cutts379ea422023-12-21 15:31:47 +00001430 // We don't need to check args here, since it's covered by the FlingTapDown test.
Arpit Singha5ea7c12023-07-05 15:39:25 +00001431
1432 Gesture buttonDownGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1433 /* down= */ GESTURES_BUTTON_LEFT,
1434 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +00001435 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, buttonDownGesture);
Byoungho Jungee6268f2023-10-30 17:27:26 +09001436
Harry Cutts5f26e952023-11-30 18:20:27 +00001437 ASSERT_THAT(args,
1438 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001439 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
Harry Cuttsef95e712024-02-16 18:56:39 +00001440 WithButtonState(0), WithPressure(0.0f))),
Harry Cutts379ea422023-12-21 15:31:47 +00001441 VariantWith<NotifyMotionArgs>(
Harry Cutts5f26e952023-11-30 18:20:27 +00001442 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Harry Cutts5f26e952023-11-30 18:20:27 +00001443 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001444 WithPressure(1.0f))),
Harry Cutts5f26e952023-11-30 18:20:27 +00001445 VariantWith<NotifyMotionArgs>(
1446 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1447 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1448 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Harry Cuttsef95e712024-02-16 18:56:39 +00001449 WithPressure(1.0f)))));
1450 ASSERT_THAT(args,
1451 Each(VariantWith<NotifyMotionArgs>(AllOf(WithCoords(0, 0),
1452 WithRelativeMotion(0.f, 0.f),
1453 WithToolType(ToolType::FINGER),
Linnan Li13bf76a2024-05-05 19:18:02 +08001454 WithDisplayId(ui::ADISPLAY_ID_DEFAULT)))));
Byoungho Jungee6268f2023-10-30 17:27:26 +09001455
1456 Gesture buttonUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1457 /* down= */ GESTURES_BUTTON_NONE,
1458 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ false);
Arpit Singh33a10a62023-10-12 13:06:54 +00001459 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, buttonUpGesture);
Byoungho Jungee6268f2023-10-30 17:27:26 +09001460
Harry Cutts5f26e952023-11-30 18:20:27 +00001461 ASSERT_THAT(args,
1462 ElementsAre(VariantWith<NotifyMotionArgs>(
1463 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1464 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1465 WithButtonState(0), WithCoords(0, 0),
1466 WithRelativeMotion(0.f, 0.f),
1467 WithToolType(ToolType::FINGER), WithButtonState(0),
1468 WithPressure(1.0f), WithDisplayId(ADISPLAY_ID_DEFAULT))),
1469 VariantWith<NotifyMotionArgs>(
1470 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1471 WithCoords(0, 0), WithRelativeMotion(0.f, 0.f),
1472 WithToolType(ToolType::FINGER), WithButtonState(0),
1473 WithPressure(0.0f), WithDisplayId(ADISPLAY_ID_DEFAULT))),
1474 VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001475 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Harry Cutts5f26e952023-11-30 18:20:27 +00001476 WithCoords(0, 0), WithRelativeMotion(0, 0),
1477 WithToolType(ToolType::FINGER), WithButtonState(0),
1478 WithPressure(0.0f),
1479 WithDisplayId(ADISPLAY_ID_DEFAULT)))));
Byoungho Jungee6268f2023-10-30 17:27:26 +09001480
1481 // Future taps should be re-enabled
1482 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1483}
1484
Prabir Pradhan8b053512024-05-03 23:15:39 +00001485TEST_F_WITH_FLAGS(GestureConverterTest, MoveEnablesTapToClick,
Byoungho Jungee6268f2023-10-30 17:27:26 +09001486 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION)) {
1487 // initially disable tap-to-click
1488 mReader->getContext()->setPreventingTouchpadTaps(true);
1489
1490 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1491 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001492 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Byoungho Jungee6268f2023-10-30 17:27:26 +09001493
1494 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
Arpit Singh33a10a62023-10-12 13:06:54 +00001495 std::list<NotifyArgs> args =
1496 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
Harry Cutts379ea422023-12-21 15:31:47 +00001497 // We don't need to check args here, since it's covered by the Move test.
Byoungho Jungee6268f2023-10-30 17:27:26 +09001498
1499 // Future taps should be re-enabled
1500 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1501}
1502
Prabir Pradhan8b053512024-05-03 23:15:39 +00001503TEST_F_WITH_FLAGS(GestureConverterTest, KeypressCancelsHoverMove,
Arpit Singh33a10a62023-10-12 13:06:54 +00001504 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION_V2)) {
1505 const nsecs_t gestureStartTime = 1000;
1506 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1507 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Linnan Li13bf76a2024-05-05 19:18:02 +08001508 converter.setDisplayId(ui::ADISPLAY_ID_DEFAULT);
Arpit Singh33a10a62023-10-12 13:06:54 +00001509
1510 // Start a move gesture at gestureStartTime
1511 Gesture moveGesture(kGestureMove, gestureStartTime, gestureStartTime, -5, 10);
1512 std::list<NotifyArgs> args =
1513 converter.handleGesture(gestureStartTime, READ_TIME, gestureStartTime, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +00001514 ASSERT_THAT(args,
1515 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001516 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)),
1517 VariantWith<NotifyMotionArgs>(
1518 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE))));
Arpit Singh33a10a62023-10-12 13:06:54 +00001519
1520 // Key presses with IME connection should cancel ongoing move gesture
1521 nsecs_t currentTime = gestureStartTime + 100;
1522 mFakePolicy->setIsInputMethodConnectionActive(true);
1523 mReader->getContext()->setLastKeyDownTimestamp(currentTime);
1524 moveGesture = Gesture(kGestureMove, currentTime, currentTime, -5, 10);
1525 args = converter.handleGesture(currentTime, READ_TIME, gestureStartTime, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +00001526 ASSERT_THAT(args,
1527 ElementsAre(VariantWith<NotifyMotionArgs>(
1528 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT))));
Arpit Singh33a10a62023-10-12 13:06:54 +00001529
1530 // any updates in existing move gesture should be ignored
1531 moveGesture = Gesture(kGestureMove, currentTime, currentTime, -5, 10);
1532 args = converter.handleGesture(currentTime, READ_TIME, gestureStartTime, moveGesture);
1533 ASSERT_EQ(0u, args.size());
1534
1535 // New gesture should not be affected
1536 currentTime += 100;
1537 moveGesture = Gesture(kGestureMove, currentTime, currentTime, -5, 10);
1538 args = converter.handleGesture(currentTime, READ_TIME, currentTime, moveGesture);
Harry Cutts5f26e952023-11-30 18:20:27 +00001539 ASSERT_THAT(args,
1540 ElementsAre(VariantWith<NotifyMotionArgs>(
Harry Cutts379ea422023-12-21 15:31:47 +00001541 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)),
1542 VariantWith<NotifyMotionArgs>(
1543 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE))));
Arpit Singh33a10a62023-10-12 13:06:54 +00001544}
1545
Harry Cutts4fb941a2022-12-14 19:14:04 +00001546} // namespace android