blob: 74ce359cac070f1c7cb8ef2793e089e77e53cdaf [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
19#include <EventHub.h>
20#include <gestures/GestureConverter.h>
21#include <gtest/gtest.h>
Josep del Riod0746382023-07-29 13:18:25 +000022#include <gui/constants.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000023
24#include "FakeEventHub.h"
25#include "FakeInputReaderPolicy.h"
26#include "FakePointerController.h"
27#include "InstrumentedInputReader.h"
28#include "NotifyArgs.h"
29#include "TestConstants.h"
30#include "TestInputListener.h"
31#include "TestInputListenerMatchers.h"
32#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
37using testing::AllOf;
38
39class GestureConverterTest : public testing::Test {
40protected:
41 static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
Harry Cuttsc5748d12022-12-02 17:30:18 +000042 static constexpr int32_t EVENTHUB_ID = 1;
Harry Cutts4fb941a2022-12-14 19:14:04 +000043 static constexpr stime_t ARBITRARY_GESTURE_TIME = 1.2;
Harry Cuttsb1e83552022-12-20 11:02:26 +000044 static constexpr float POINTER_X = 500;
Harry Cutts4fb941a2022-12-14 19:14:04 +000045 static constexpr float POINTER_Y = 200;
46
47 void SetUp() {
48 mFakeEventHub = std::make_unique<FakeEventHub>();
49 mFakePolicy = sp<FakeInputReaderPolicy>::make();
50 mFakeListener = std::make_unique<TestInputListener>();
51 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
52 *mFakeListener);
Harry Cuttsc5748d12022-12-02 17:30:18 +000053 mDevice = newDevice();
54 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, -500, 500, 0, 0, 20);
55 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, -500, 500, 0, 0, 20);
Harry Cutts4fb941a2022-12-14 19:14:04 +000056
57 mFakePointerController = std::make_shared<FakePointerController>();
58 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
59 mFakePointerController->setPosition(POINTER_X, POINTER_Y);
60 mFakePolicy->setPointerController(mFakePointerController);
61 }
62
Harry Cuttsc5748d12022-12-02 17:30:18 +000063 std::shared_ptr<InputDevice> newDevice() {
64 InputDeviceIdentifier identifier;
65 identifier.name = "device";
66 identifier.location = "USB1";
67 identifier.bus = 0;
68 std::shared_ptr<InputDevice> device =
69 std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, /* generation= */ 2,
70 identifier);
71 mReader->pushNextDevice(device);
72 mFakeEventHub->addDevice(EVENTHUB_ID, identifier.name, InputDeviceClass::TOUCHPAD,
73 identifier.bus);
74 mReader->loopOnce();
75 return device;
76 }
77
Harry Cutts4fb941a2022-12-14 19:14:04 +000078 std::shared_ptr<FakeEventHub> mFakeEventHub;
79 sp<FakeInputReaderPolicy> mFakePolicy;
80 std::unique_ptr<TestInputListener> mFakeListener;
81 std::unique_ptr<InstrumentedInputReader> mReader;
Harry Cuttsc5748d12022-12-02 17:30:18 +000082 std::shared_ptr<InputDevice> mDevice;
Harry Cutts4fb941a2022-12-14 19:14:04 +000083 std::shared_ptr<FakePointerController> mFakePointerController;
84};
85
86TEST_F(GestureConverterTest, Move) {
Harry Cuttsc5748d12022-12-02 17:30:18 +000087 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
88 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +000089 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cutts4fb941a2022-12-14 19:14:04 +000090
91 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
92 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
93 ASSERT_EQ(1u, args.size());
94
95 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
96 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
97 WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10),
Josep del Riod0746382023-07-29 13:18:25 +000098 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
99 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000100
Harry Cuttsb1e83552022-12-20 11:02:26 +0000101 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000102}
103
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000104TEST_F(GestureConverterTest, Move_Rotated) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000105 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
106 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000107 converter.setOrientation(ui::ROTATION_90);
Josep del Riod0746382023-07-29 13:18:25 +0000108 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000109
110 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
111 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
112 ASSERT_EQ(1u, args.size());
113
114 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
116 WithCoords(POINTER_X + 10, POINTER_Y + 5), WithRelativeMotion(10, 5),
Josep del Riod0746382023-07-29 13:18:25 +0000117 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
118 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000119
Harry Cuttsb1e83552022-12-20 11:02:26 +0000120 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X + 10, POINTER_Y + 5));
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000121}
122
Harry Cutts4fb941a2022-12-14 19:14:04 +0000123TEST_F(GestureConverterTest, ButtonsChange) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000124 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
125 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000126 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cutts4fb941a2022-12-14 19:14:04 +0000127
128 // Press left and right buttons at once
129 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
130 /* down= */ GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT,
131 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
132 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture);
133 ASSERT_EQ(3u, args.size());
134
135 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
136 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
137 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY |
138 AMOTION_EVENT_BUTTON_SECONDARY),
Josep del Riod0746382023-07-29 13:18:25 +0000139 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
140 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000141 args.pop_front();
142 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
143 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
144 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
145 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Josep del Riod0746382023-07-29 13:18:25 +0000146 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
147 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000148 args.pop_front();
149 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
150 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
151 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY),
152 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY |
153 AMOTION_EVENT_BUTTON_SECONDARY),
Josep del Riod0746382023-07-29 13:18:25 +0000154 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
155 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000156
157 // Then release the left button
158 Gesture leftUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
159 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT,
160 /* is_tap= */ false);
161 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, leftUpGesture);
162 ASSERT_EQ(1u, args.size());
163
164 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
166 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
167 WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY),
Josep del Riod0746382023-07-29 13:18:25 +0000168 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
169 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000170
171 // Finally release the right button
172 Gesture rightUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
173 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_RIGHT,
174 /* is_tap= */ false);
175 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, rightUpGesture);
Harry Cutts48e7a402023-07-07 15:22:40 +0000176 ASSERT_EQ(3u, args.size());
Harry Cutts4fb941a2022-12-14 19:14:04 +0000177
178 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
179 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
180 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY), WithButtonState(0),
Josep del Riod0746382023-07-29 13:18:25 +0000181 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
182 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000183 args.pop_front();
184 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0),
Josep del Riod0746382023-07-29 13:18:25 +0000186 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
187 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts48e7a402023-07-07 15:22:40 +0000188 args.pop_front();
189 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
190 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0),
Josep del Riod0746382023-07-29 13:18:25 +0000191 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
192 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000193}
194
195TEST_F(GestureConverterTest, DragWithButton) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000196 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
197 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000198 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cutts4fb941a2022-12-14 19:14:04 +0000199
200 // Press the button
201 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
202 /* down= */ GESTURES_BUTTON_LEFT, /* up= */ GESTURES_BUTTON_NONE,
203 /* is_tap= */ false);
204 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture);
205 ASSERT_EQ(2u, args.size());
206
207 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
208 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
209 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Josep del Riod0746382023-07-29 13:18:25 +0000210 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
211 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000212 args.pop_front();
213 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
214 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
215 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
216 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Josep del Riod0746382023-07-29 13:18:25 +0000217 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
218 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000219
220 // Move
221 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
222 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
223 ASSERT_EQ(1u, args.size());
224
225 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
226 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
227 WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10),
Josep del Riod0746382023-07-29 13:18:25 +0000228 WithToolType(ToolType::FINGER), WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
229 WithPressure(1.0f), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000230
Harry Cuttsb1e83552022-12-20 11:02:26 +0000231 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000232
233 // Release the button
234 Gesture upGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
235 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT,
236 /* is_tap= */ false);
237 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, upGesture);
Harry Cutts48e7a402023-07-07 15:22:40 +0000238 ASSERT_EQ(3u, args.size());
Harry Cutts4fb941a2022-12-14 19:14:04 +0000239
240 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
242 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0),
Josep del Riod0746382023-07-29 13:18:25 +0000243 WithCoords(POINTER_X - 5, POINTER_Y + 10), WithToolType(ToolType::FINGER),
244 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000245 args.pop_front();
246 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
247 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0),
Josep del Riod0746382023-07-29 13:18:25 +0000248 WithCoords(POINTER_X - 5, POINTER_Y + 10), WithToolType(ToolType::FINGER),
249 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts48e7a402023-07-07 15:22:40 +0000250 args.pop_front();
251 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
252 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0),
Josep del Riod0746382023-07-29 13:18:25 +0000253 WithCoords(POINTER_X - 5, POINTER_Y + 10), WithToolType(ToolType::FINGER),
254 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000255}
256
Harry Cuttsef400b22022-12-16 21:26:24 +0000257TEST_F(GestureConverterTest, Scroll) {
258 const nsecs_t downTime = 12345;
259 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
260 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000261 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsef400b22022-12-16 21:26:24 +0000262
Harry Cuttsa546ba82023-01-13 17:21:00 +0000263 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Harry Cuttsef400b22022-12-16 21:26:24 +0000264 std::list<NotifyArgs> args = converter.handleGesture(downTime, READ_TIME, startGesture);
265 ASSERT_EQ(2u, args.size());
266
267 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
268 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
269 WithGestureScrollDistance(0, 0, EPSILON),
270 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700271 WithToolType(ToolType::FINGER), WithDownTime(downTime),
Josep del Riod0746382023-07-29 13:18:25 +0000272 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
273 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000274 args.pop_front();
275 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
276 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
277 WithCoords(POINTER_X, POINTER_Y - 10),
278 WithGestureScrollDistance(0, 10, EPSILON),
279 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700280 WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +0000281 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
282 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000283
Harry Cuttsa546ba82023-01-13 17:21:00 +0000284 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Harry Cuttsef400b22022-12-16 21:26:24 +0000285 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
286 ASSERT_EQ(1u, args.size());
287 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
288 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
289 WithCoords(POINTER_X, POINTER_Y - 15),
290 WithGestureScrollDistance(0, 5, EPSILON),
291 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700292 WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +0000293 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
294 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000295
296 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
297 GESTURES_FLING_START);
298 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
299 ASSERT_EQ(1u, args.size());
300 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
301 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
302 WithCoords(POINTER_X, POINTER_Y - 15),
303 WithGestureScrollDistance(0, 0, EPSILON),
304 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700305 WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +0000306 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
307 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000308}
309
310TEST_F(GestureConverterTest, Scroll_Rotated) {
311 const nsecs_t downTime = 12345;
312 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
313 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
314 converter.setOrientation(ui::ROTATION_90);
Josep del Riod0746382023-07-29 13:18:25 +0000315 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsef400b22022-12-16 21:26:24 +0000316
Harry Cuttsa546ba82023-01-13 17:21:00 +0000317 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Harry Cuttsef400b22022-12-16 21:26:24 +0000318 std::list<NotifyArgs> args = converter.handleGesture(downTime, READ_TIME, startGesture);
319 ASSERT_EQ(2u, args.size());
320
321 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
322 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
323 WithGestureScrollDistance(0, 0, EPSILON),
324 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000325 WithToolType(ToolType::FINGER), WithDownTime(downTime),
326 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000327 args.pop_front();
328 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
329 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
330 WithCoords(POINTER_X - 10, POINTER_Y),
331 WithGestureScrollDistance(0, 10, EPSILON),
332 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000333 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000334
Harry Cuttsa546ba82023-01-13 17:21:00 +0000335 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Harry Cuttsef400b22022-12-16 21:26:24 +0000336 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
337 ASSERT_EQ(1u, args.size());
338 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
339 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
340 WithCoords(POINTER_X - 15, POINTER_Y),
341 WithGestureScrollDistance(0, 5, EPSILON),
342 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000343 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000344
345 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
346 GESTURES_FLING_START);
347 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
348 ASSERT_EQ(1u, args.size());
349 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
350 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
351 WithCoords(POINTER_X - 15, POINTER_Y),
352 WithGestureScrollDistance(0, 0, EPSILON),
353 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000354 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000355}
356
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000357TEST_F(GestureConverterTest, Scroll_ClearsClassificationAfterGesture) {
Harry Cuttsef400b22022-12-16 21:26:24 +0000358 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
359 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000360 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsef400b22022-12-16 21:26:24 +0000361
Harry Cuttsa546ba82023-01-13 17:21:00 +0000362 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Harry Cuttsef400b22022-12-16 21:26:24 +0000363 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
364
Harry Cuttsa546ba82023-01-13 17:21:00 +0000365 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Harry Cuttsef400b22022-12-16 21:26:24 +0000366 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
367
368 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
369 GESTURES_FLING_START);
370 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
371
372 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
373 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
374 ASSERT_EQ(1u, args.size());
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000375 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
Josep del Riod0746382023-07-29 13:18:25 +0000376 AllOf(WithMotionClassification(MotionClassification::NONE),
377 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000378}
379
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000380TEST_F(GestureConverterTest, Scroll_ClearsScrollDistanceAfterGesture) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000381 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
382 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000383 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000384
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000385 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
386 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
387
388 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
389 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
390
391 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
392 GESTURES_FLING_START);
393 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
394
395 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
396 // need to use another gesture type, like pinch.
397 Gesture pinchGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
398 GESTURES_ZOOM_START);
399 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, pinchGesture);
400 ASSERT_FALSE(args.empty());
401 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGestureScrollDistance(0, 0, EPSILON));
402}
403
404TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsClassificationAfterGesture) {
405 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
406 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000407 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000408
409 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
410 /*dy=*/0);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000411 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
412
413 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
414 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
415
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000416 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/-5,
417 /*dy=*/10);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000418 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
419 ASSERT_EQ(1u, args.size());
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000420 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
421 WithMotionClassification(MotionClassification::NONE));
422}
423
Harry Cutts8743f182023-05-17 12:03:49 +0000424TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsGestureAxesAfterGesture) {
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000425 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
426 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000427 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000428
429 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/5,
430 /*dy=*/5);
431 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
432
433 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
434 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
435
436 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
437 // need to use another gesture type, like pinch.
438 Gesture pinchGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
439 GESTURES_ZOOM_START);
440 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, pinchGesture);
441 ASSERT_FALSE(args.empty());
Harry Cutts8743f182023-05-17 12:03:49 +0000442 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
443 AllOf(WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(0)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000444}
445
446TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
447 // The gestures library will "lock" a swipe into the dimension it starts in. For example, if you
448 // start swiping up and then start moving left or right, it'll return gesture events with only Y
449 // deltas until you lift your fingers and start swiping again. That's why each of these tests
450 // only checks movement in one dimension.
451 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
452 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000453 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000454
455 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
456 /* dy= */ 10);
457 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
458 ASSERT_EQ(4u, args.size());
459
460 // Three fake fingers should be created. We don't actually care where they are, so long as they
461 // move appropriately.
462 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
463 ASSERT_THAT(arg,
464 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
Harry Cutts8743f182023-05-17 12:03:49 +0000465 WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000466 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000467 WithPointerCount(1u), WithToolType(ToolType::FINGER),
468 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000469 PointerCoords finger0Start = arg.pointerCoords[0];
470 args.pop_front();
471 arg = std::get<NotifyMotionArgs>(args.front());
472 ASSERT_THAT(arg,
473 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
474 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000475 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000476 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000477 WithPointerCount(2u), WithToolType(ToolType::FINGER),
478 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000479 PointerCoords finger1Start = arg.pointerCoords[1];
480 args.pop_front();
481 arg = std::get<NotifyMotionArgs>(args.front());
482 ASSERT_THAT(arg,
483 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
484 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000485 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000486 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000487 WithPointerCount(3u), WithToolType(ToolType::FINGER),
488 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000489 PointerCoords finger2Start = arg.pointerCoords[2];
490 args.pop_front();
491
492 arg = std::get<NotifyMotionArgs>(args.front());
493 ASSERT_THAT(arg,
494 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cutts8743f182023-05-17 12:03:49 +0000495 WithGestureOffset(0, -0.01, EPSILON), WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000496 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000497 WithPointerCount(3u), WithToolType(ToolType::FINGER),
498 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000499 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
500 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
501 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
502 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 10);
503 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 10);
504 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 10);
505
506 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
507 /* dx= */ 0, /* dy= */ 5);
508 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
509 ASSERT_EQ(1u, args.size());
510 arg = std::get<NotifyMotionArgs>(args.front());
511 ASSERT_THAT(arg,
512 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cutts8743f182023-05-17 12:03:49 +0000513 WithGestureOffset(0, -0.005, EPSILON), WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000514 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000515 WithPointerCount(3u), WithToolType(ToolType::FINGER),
516 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000517 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
518 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
519 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
520 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 15);
521 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 15);
522 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 15);
523
524 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
525 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
526 ASSERT_EQ(3u, args.size());
527 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
528 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
529 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000530 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000531 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000532 WithPointerCount(3u), WithToolType(ToolType::FINGER),
533 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000534 args.pop_front();
535 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
536 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
537 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000538 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000539 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000540 WithPointerCount(2u), WithToolType(ToolType::FINGER),
541 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000542 args.pop_front();
543 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
544 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
Harry Cutts8743f182023-05-17 12:03:49 +0000545 WithGestureSwipeFingerCount(3),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000546 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000547 WithPointerCount(1u), WithToolType(ToolType::FINGER),
548 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000549}
550
Harry Cutts94f5bd52023-01-06 18:02:18 +0000551TEST_F(GestureConverterTest, ThreeFingerSwipe_Rotated) {
552 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
553 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
554 converter.setOrientation(ui::ROTATION_90);
Josep del Riod0746382023-07-29 13:18:25 +0000555 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cutts94f5bd52023-01-06 18:02:18 +0000556
557 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
558 /* dy= */ 10);
559 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
560 ASSERT_EQ(4u, args.size());
561
562 // Three fake fingers should be created. We don't actually care where they are, so long as they
563 // move appropriately.
564 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
565 ASSERT_THAT(arg,
566 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
Josep del Riod0746382023-07-29 13:18:25 +0000567 WithPointerCount(1u), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000568 PointerCoords finger0Start = arg.pointerCoords[0];
569 args.pop_front();
570 arg = std::get<NotifyMotionArgs>(args.front());
571 ASSERT_THAT(arg,
572 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
573 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Josep del Riod0746382023-07-29 13:18:25 +0000574 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u),
575 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000576 PointerCoords finger1Start = arg.pointerCoords[1];
577 args.pop_front();
578 arg = std::get<NotifyMotionArgs>(args.front());
579 ASSERT_THAT(arg,
580 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
581 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Josep del Riod0746382023-07-29 13:18:25 +0000582 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u),
583 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000584 PointerCoords finger2Start = arg.pointerCoords[2];
585 args.pop_front();
586
587 arg = std::get<NotifyMotionArgs>(args.front());
588 ASSERT_THAT(arg,
589 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Josep del Riod0746382023-07-29 13:18:25 +0000590 WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u),
591 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000592 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 10);
593 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 10);
594 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 10);
595 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
596 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
597 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
598
599 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
600 /* dx= */ 0, /* dy= */ 5);
601 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
602 ASSERT_EQ(1u, args.size());
603 arg = std::get<NotifyMotionArgs>(args.front());
604 ASSERT_THAT(arg,
605 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Josep del Riod0746382023-07-29 13:18:25 +0000606 WithGestureOffset(0, -0.005, EPSILON), WithPointerCount(3u),
607 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000608 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 15);
609 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 15);
610 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 15);
611 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
612 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
613 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
614
615 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
616 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
617 ASSERT_EQ(3u, args.size());
618 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
619 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
620 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Josep del Riod0746382023-07-29 13:18:25 +0000621 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u),
622 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000623 args.pop_front();
624 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
625 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
626 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Josep del Riod0746382023-07-29 13:18:25 +0000627 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u),
628 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000629 args.pop_front();
630 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
631 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
Josep del Riod0746382023-07-29 13:18:25 +0000632 WithPointerCount(1u), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cutts94f5bd52023-01-06 18:02:18 +0000633}
634
Harry Cuttsc5748d12022-12-02 17:30:18 +0000635TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
636 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
637 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000638 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsc5748d12022-12-02 17:30:18 +0000639
640 Gesture startGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
641 /* dx= */ 10, /* dy= */ 0);
642 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
643 ASSERT_EQ(5u, args.size());
644
645 // Four fake fingers should be created. We don't actually care where they are, so long as they
646 // move appropriately.
647 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
648 ASSERT_THAT(arg,
649 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
Harry Cutts8743f182023-05-17 12:03:49 +0000650 WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000651 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000652 WithPointerCount(1u), WithToolType(ToolType::FINGER),
653 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000654 PointerCoords finger0Start = arg.pointerCoords[0];
655 args.pop_front();
656 arg = std::get<NotifyMotionArgs>(args.front());
657 ASSERT_THAT(arg,
658 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
659 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000660 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000661 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000662 WithPointerCount(2u), WithToolType(ToolType::FINGER),
663 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000664 PointerCoords finger1Start = arg.pointerCoords[1];
665 args.pop_front();
666 arg = std::get<NotifyMotionArgs>(args.front());
667 ASSERT_THAT(arg,
668 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
669 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000670 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000671 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000672 WithPointerCount(3u), WithToolType(ToolType::FINGER),
673 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000674 PointerCoords finger2Start = arg.pointerCoords[2];
675 args.pop_front();
676 arg = std::get<NotifyMotionArgs>(args.front());
677 ASSERT_THAT(arg,
678 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
679 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000680 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000681 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000682 WithPointerCount(4u), WithToolType(ToolType::FINGER),
683 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000684 PointerCoords finger3Start = arg.pointerCoords[3];
685 args.pop_front();
686
687 arg = std::get<NotifyMotionArgs>(args.front());
688 ASSERT_THAT(arg,
689 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cutts8743f182023-05-17 12:03:49 +0000690 WithGestureOffset(0.01, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000691 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000692 WithPointerCount(4u), WithToolType(ToolType::FINGER),
693 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000694 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10);
695 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 10);
696 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 10);
697 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 10);
698 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
699 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
700 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
701 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
702
703 Gesture continueGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
704 /* dx= */ 5, /* dy= */ 0);
705 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
706 ASSERT_EQ(1u, args.size());
707 arg = std::get<NotifyMotionArgs>(args.front());
708 ASSERT_THAT(arg,
709 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Harry Cutts8743f182023-05-17 12:03:49 +0000710 WithGestureOffset(0.005, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000711 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000712 WithPointerCount(4u), WithToolType(ToolType::FINGER),
713 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000714 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15);
715 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 15);
716 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 15);
717 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 15);
718 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
719 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
720 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
721 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
722
723 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
724 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
725 ASSERT_EQ(4u, args.size());
726 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
727 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
728 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000729 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000730 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000731 WithPointerCount(4u), WithToolType(ToolType::FINGER),
732 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000733 args.pop_front();
734 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
735 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
736 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000737 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000738 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000739 WithPointerCount(3u), WithToolType(ToolType::FINGER),
740 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000741 args.pop_front();
742 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
744 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Harry Cutts8743f182023-05-17 12:03:49 +0000745 WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000746 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000747 WithPointerCount(2u), WithToolType(ToolType::FINGER),
748 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000749 args.pop_front();
750 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
751 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
Harry Cutts8743f182023-05-17 12:03:49 +0000752 WithGestureSwipeFingerCount(4),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000753 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000754 WithPointerCount(1u), WithToolType(ToolType::FINGER),
755 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsc5748d12022-12-02 17:30:18 +0000756}
757
Harry Cuttsb1e83552022-12-20 11:02:26 +0000758TEST_F(GestureConverterTest, Pinch_Inwards) {
759 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
760 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000761 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000762
763 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
764 GESTURES_ZOOM_START);
765 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
766 ASSERT_EQ(2u, args.size());
767 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
768 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
769 WithMotionClassification(MotionClassification::PINCH),
770 WithGesturePinchScaleFactor(1.0f, EPSILON),
771 WithCoords(POINTER_X - 100, POINTER_Y), WithPointerCount(1u),
Josep del Riod0746382023-07-29 13:18:25 +0000772 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000773 args.pop_front();
774 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
775 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
776 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
777 WithMotionClassification(MotionClassification::PINCH),
778 WithGesturePinchScaleFactor(1.0f, EPSILON),
779 WithPointerCoords(1, POINTER_X + 100, POINTER_Y), WithPointerCount(2u),
Josep del Riod0746382023-07-29 13:18:25 +0000780 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000781
782 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
783 /* dz= */ 0.8, GESTURES_ZOOM_UPDATE);
784 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
785 ASSERT_EQ(1u, args.size());
786 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
787 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
788 WithMotionClassification(MotionClassification::PINCH),
789 WithGesturePinchScaleFactor(0.8f, EPSILON),
790 WithPointerCoords(0, POINTER_X - 80, POINTER_Y),
791 WithPointerCoords(1, POINTER_X + 80, POINTER_Y), WithPointerCount(2u),
Josep del Riod0746382023-07-29 13:18:25 +0000792 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000793
794 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
795 GESTURES_ZOOM_END);
796 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
797 ASSERT_EQ(2u, args.size());
798 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
800 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
801 WithMotionClassification(MotionClassification::PINCH),
802 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
Josep del Riod0746382023-07-29 13:18:25 +0000803 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000804 args.pop_front();
805 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
806 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
807 WithMotionClassification(MotionClassification::PINCH),
808 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
Josep del Riod0746382023-07-29 13:18:25 +0000809 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000810}
811
812TEST_F(GestureConverterTest, Pinch_Outwards) {
813 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
814 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000815 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000816
817 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
818 GESTURES_ZOOM_START);
819 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
820 ASSERT_EQ(2u, args.size());
821 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
822 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
823 WithMotionClassification(MotionClassification::PINCH),
824 WithGesturePinchScaleFactor(1.0f, EPSILON),
825 WithCoords(POINTER_X - 100, POINTER_Y), WithPointerCount(1u),
Josep del Riod0746382023-07-29 13:18:25 +0000826 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000827 args.pop_front();
828 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
829 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
830 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
831 WithMotionClassification(MotionClassification::PINCH),
832 WithGesturePinchScaleFactor(1.0f, EPSILON),
833 WithPointerCoords(1, POINTER_X + 100, POINTER_Y), WithPointerCount(2u),
Josep del Riod0746382023-07-29 13:18:25 +0000834 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000835
836 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
837 /* dz= */ 1.2, GESTURES_ZOOM_UPDATE);
838 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
839 ASSERT_EQ(1u, args.size());
840 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
841 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
842 WithMotionClassification(MotionClassification::PINCH),
843 WithGesturePinchScaleFactor(1.2f, EPSILON),
844 WithPointerCoords(0, POINTER_X - 120, POINTER_Y),
845 WithPointerCoords(1, POINTER_X + 120, POINTER_Y), WithPointerCount(2u),
Josep del Riod0746382023-07-29 13:18:25 +0000846 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000847
848 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
849 GESTURES_ZOOM_END);
850 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
851 ASSERT_EQ(2u, args.size());
852 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
853 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
854 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
855 WithMotionClassification(MotionClassification::PINCH),
856 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
Josep del Riod0746382023-07-29 13:18:25 +0000857 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000858 args.pop_front();
859 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
860 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
861 WithMotionClassification(MotionClassification::PINCH),
862 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
Josep del Riod0746382023-07-29 13:18:25 +0000863 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000864}
865
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000866TEST_F(GestureConverterTest, Pinch_ClearsClassificationAfterGesture) {
Harry Cuttsb1e83552022-12-20 11:02:26 +0000867 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
868 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000869 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000870
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000871 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
Harry Cuttsb1e83552022-12-20 11:02:26 +0000872 GESTURES_ZOOM_START);
873 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
874
875 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000876 /*dz=*/1.2, GESTURES_ZOOM_UPDATE);
Harry Cuttsb1e83552022-12-20 11:02:26 +0000877 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
878
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000879 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
Harry Cuttsb1e83552022-12-20 11:02:26 +0000880 GESTURES_ZOOM_END);
881 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
882
883 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
884 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
885 ASSERT_EQ(1u, args.size());
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000886 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
887 WithMotionClassification(MotionClassification::NONE));
888}
889
890TEST_F(GestureConverterTest, Pinch_ClearsScaleFactorAfterGesture) {
891 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
892 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000893 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttsa5f98c92023-05-17 15:05:44 +0000894
895 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
896 GESTURES_ZOOM_START);
897 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
898
899 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
900 /*dz=*/1.2, GESTURES_ZOOM_UPDATE);
901 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
902
903 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
904 GESTURES_ZOOM_END);
905 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
906
907 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
908 // need to use another gesture type, like scroll.
909 Gesture scrollGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/1,
910 /*dy=*/0);
911 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, scrollGesture);
912 ASSERT_FALSE(args.empty());
913 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGesturePinchScaleFactor(0, EPSILON));
Harry Cuttsb1e83552022-12-20 11:02:26 +0000914}
915
Harry Cuttse9b71422023-03-14 16:54:44 +0000916TEST_F(GestureConverterTest, ResetWithButtonPressed) {
917 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
918 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000919 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttse9b71422023-03-14 16:54:44 +0000920
921 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
922 /*down=*/GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT,
923 /*up=*/GESTURES_BUTTON_NONE, /*is_tap=*/false);
924 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture);
925
926 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
927 ASSERT_EQ(3u, args.size());
928
929 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
930 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
931 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
932 WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY),
Josep del Riod0746382023-07-29 13:18:25 +0000933 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
934 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +0000935 args.pop_front();
936 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
937 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
938 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY), WithButtonState(0),
Josep del Riod0746382023-07-29 13:18:25 +0000939 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
940 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +0000941 args.pop_front();
942 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0),
Josep del Riod0746382023-07-29 13:18:25 +0000944 WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER),
945 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +0000946}
947
948TEST_F(GestureConverterTest, ResetDuringScroll) {
949 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
950 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000951 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttse9b71422023-03-14 16:54:44 +0000952
953 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
954 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
955
956 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
957 ASSERT_EQ(1u, args.size());
958 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
959 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
960 WithCoords(POINTER_X, POINTER_Y - 10),
961 WithGestureScrollDistance(0, 0, EPSILON),
962 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700963 WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +0000964 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
965 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +0000966}
967
968TEST_F(GestureConverterTest, ResetDuringThreeFingerSwipe) {
969 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
970 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +0000971 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttse9b71422023-03-14 16:54:44 +0000972
973 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
974 /*dy=*/10);
975 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
976
977 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
978 ASSERT_EQ(3u, args.size());
979 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
980 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
981 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
982 WithGestureOffset(0, 0, EPSILON),
983 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000984 WithPointerCount(3u), WithToolType(ToolType::FINGER),
985 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +0000986 args.pop_front();
987 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
988 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
989 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
990 WithGestureOffset(0, 0, EPSILON),
991 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000992 WithPointerCount(2u), WithToolType(ToolType::FINGER),
993 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +0000994 args.pop_front();
995 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
996 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
997 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
Josep del Riod0746382023-07-29 13:18:25 +0000998 WithPointerCount(1u), WithToolType(ToolType::FINGER),
999 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +00001000}
1001
1002TEST_F(GestureConverterTest, ResetDuringPinch) {
1003 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1004 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +00001005 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Harry Cuttse9b71422023-03-14 16:54:44 +00001006
1007 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
1008 GESTURES_ZOOM_START);
1009 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
1010
1011 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
1012 ASSERT_EQ(2u, args.size());
1013 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
1014 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
1015 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1016 WithMotionClassification(MotionClassification::PINCH),
1017 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
Josep del Riod0746382023-07-29 13:18:25 +00001018 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +00001019 args.pop_front();
1020 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
1021 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1022 WithMotionClassification(MotionClassification::PINCH),
1023 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
Josep del Riod0746382023-07-29 13:18:25 +00001024 WithToolType(ToolType::FINGER), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Harry Cuttse9b71422023-03-14 16:54:44 +00001025}
1026
Prabir Pradhanf7c4b0e2023-05-10 21:25:16 +00001027TEST_F(GestureConverterTest, FlingTapDown) {
1028 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1029 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +00001030 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Prabir Pradhanf7c4b0e2023-05-10 21:25:16 +00001031
1032 Gesture tapDownGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1033 /*vx=*/0.f, /*vy=*/0.f, GESTURES_FLING_TAP_DOWN);
1034 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, tapDownGesture);
1035
1036 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1037 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1038 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
Josep del Riod0746382023-07-29 13:18:25 +00001039 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1040 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhanf7c4b0e2023-05-10 21:25:16 +00001041
1042 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X, POINTER_Y));
1043 ASSERT_TRUE(mFakePointerController->isPointerShown());
1044}
1045
Arpit Singha5ea7c12023-07-05 15:39:25 +00001046TEST_F(GestureConverterTest, Tap) {
1047 // Tap should produce button press/release events
1048 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1049 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +00001050 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001051
1052 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1053 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1054 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
1055
1056 ASSERT_EQ(1u, args.size());
1057 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1058 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1059 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
Josep del Riod0746382023-07-29 13:18:25 +00001060 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1061 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001062
1063 Gesture tapGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1064 /* down= */ GESTURES_BUTTON_LEFT,
1065 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1066 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, tapGesture);
1067
1068 ASSERT_EQ(5u, args.size());
1069 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1070 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
1071 WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +00001072 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f),
1073 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001074 args.pop_front();
1075 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1076 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1077 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1078 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1079 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1080 WithToolType(ToolType::FINGER), WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Josep del Riod0746382023-07-29 13:18:25 +00001081 WithPressure(1.0f), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001082 args.pop_front();
1083 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1084 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1085 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0),
1086 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
Josep del Riod0746382023-07-29 13:18:25 +00001087 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(1.0f),
1088 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001089 args.pop_front();
1090 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1091 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithCoords(POINTER_X, POINTER_Y),
1092 WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +00001093 WithButtonState(0), WithPressure(0.0f), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001094 args.pop_front();
1095 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1097 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
Josep del Riod0746382023-07-29 13:18:25 +00001098 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1099 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001100}
1101
1102TEST_F(GestureConverterTest, Click) {
1103 // Click should produce button press/release events
1104 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1105 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +00001106 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001107
1108 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1109 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1110 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
1111
1112 ASSERT_EQ(1u, args.size());
1113 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1114 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1115 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
Josep del Riod0746382023-07-29 13:18:25 +00001116 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1117 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001118
1119 Gesture buttonDownGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1120 /* down= */ GESTURES_BUTTON_LEFT,
1121 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
1122 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, buttonDownGesture);
1123
1124 ASSERT_EQ(2u, args.size());
1125 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
1127 WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +00001128 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f),
1129 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001130 args.pop_front();
1131 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1132 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1133 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1134 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1135 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1136 WithToolType(ToolType::FINGER), WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Josep del Riod0746382023-07-29 13:18:25 +00001137 WithPressure(1.0f), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001138
1139 Gesture buttonUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1140 /* down= */ GESTURES_BUTTON_NONE,
1141 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ false);
1142 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, buttonUpGesture);
1143
1144 ASSERT_EQ(3u, args.size());
1145 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1146 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1147 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0),
1148 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
Josep del Riod0746382023-07-29 13:18:25 +00001149 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(1.0f),
1150 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001151 args.pop_front();
1152 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1153 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithCoords(POINTER_X, POINTER_Y),
1154 WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +00001155 WithButtonState(0), WithPressure(0.0f), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001156 args.pop_front();
1157 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1158 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1159 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
Josep del Riod0746382023-07-29 13:18:25 +00001160 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1161 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001162}
1163
1164TEST_F(GestureConverterTest, TapWithTapToClickDisabled) {
1165 // Tap should be ignored when disabled
1166 mReader->getContext()->setPreventingTouchpadTaps(true);
1167
1168 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1169 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +00001170 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001171
1172 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1173 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1174 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
1175
1176 ASSERT_EQ(1u, args.size());
1177 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1178 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1179 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
Josep del Riod0746382023-07-29 13:18:25 +00001180 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1181 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001182 args.pop_front();
1183
1184 Gesture tapGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1185 /* down= */ GESTURES_BUTTON_LEFT,
1186 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1187 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, tapGesture);
1188
1189 // no events should be generated
1190 ASSERT_EQ(0u, args.size());
1191
1192 // Future taps should be re-enabled
1193 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1194}
1195
1196TEST_F(GestureConverterTest, ClickWithTapToClickDisabled) {
1197 // Click should still produce button press/release events
1198 mReader->getContext()->setPreventingTouchpadTaps(true);
1199
1200 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1201 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +00001202 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001203
1204 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1205 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1206 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
1207
1208 ASSERT_EQ(1u, args.size());
1209 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1211 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
Josep del Riod0746382023-07-29 13:18:25 +00001212 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1213 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001214
1215 Gesture buttonDownGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1216 /* down= */ GESTURES_BUTTON_LEFT,
1217 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
1218 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, buttonDownGesture);
1219 ASSERT_EQ(2u, args.size());
1220
1221 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1222 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
1223 WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +00001224 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f),
1225 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001226 args.pop_front();
1227 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1228 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1229 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1230 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1231 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1232 WithToolType(ToolType::FINGER), WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
Josep del Riod0746382023-07-29 13:18:25 +00001233 WithPressure(1.0f), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001234
1235 Gesture buttonUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1236 /* down= */ GESTURES_BUTTON_NONE,
1237 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ false);
1238 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, buttonUpGesture);
1239
1240 ASSERT_EQ(3u, args.size());
1241 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1242 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1243 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0),
1244 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
Josep del Riod0746382023-07-29 13:18:25 +00001245 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(1.0f),
1246 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001247 args.pop_front();
1248 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1249 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithCoords(POINTER_X, POINTER_Y),
1250 WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
Josep del Riod0746382023-07-29 13:18:25 +00001251 WithButtonState(0), WithPressure(0.0f), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001252 args.pop_front();
1253 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1254 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1255 WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
Josep del Riod0746382023-07-29 13:18:25 +00001256 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1257 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001258
1259 // Future taps should be re-enabled
1260 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1261}
1262
1263TEST_F(GestureConverterTest, MoveEnablesTapToClick) {
1264 // initially disable tap-to-click
1265 mReader->getContext()->setPreventingTouchpadTaps(true);
1266
1267 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1268 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Josep del Riod0746382023-07-29 13:18:25 +00001269 converter.setDisplayId(ADISPLAY_ID_DEFAULT);
Arpit Singha5ea7c12023-07-05 15:39:25 +00001270
1271 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
1272 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
1273 ASSERT_EQ(1u, args.size());
1274
1275 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1276 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1277 WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10),
Josep del Riod0746382023-07-29 13:18:25 +00001278 WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f),
1279 WithDisplayId(ADISPLAY_ID_DEFAULT)));
Arpit Singha5ea7c12023-07-05 15:39:25 +00001280
1281 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10));
1282
1283 // Future taps should be re-enabled
1284 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1285}
1286
Harry Cutts4fb941a2022-12-14 19:14:04 +00001287} // namespace android