Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 1 | /* |
| 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> |
| 22 | |
| 23 | #include "FakeEventHub.h" |
| 24 | #include "FakeInputReaderPolicy.h" |
| 25 | #include "FakePointerController.h" |
| 26 | #include "InstrumentedInputReader.h" |
| 27 | #include "NotifyArgs.h" |
| 28 | #include "TestConstants.h" |
| 29 | #include "TestInputListener.h" |
| 30 | #include "TestInputListenerMatchers.h" |
| 31 | #include "include/gestures.h" |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame^] | 32 | #include "ui/Rotation.h" |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | using testing::AllOf; |
| 37 | |
| 38 | class GestureConverterTest : public testing::Test { |
| 39 | protected: |
| 40 | static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000; |
| 41 | static constexpr stime_t ARBITRARY_GESTURE_TIME = 1.2; |
| 42 | static constexpr float POINTER_X = 100; |
| 43 | static constexpr float POINTER_Y = 200; |
| 44 | |
| 45 | void SetUp() { |
| 46 | mFakeEventHub = std::make_unique<FakeEventHub>(); |
| 47 | mFakePolicy = sp<FakeInputReaderPolicy>::make(); |
| 48 | mFakeListener = std::make_unique<TestInputListener>(); |
| 49 | mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy, |
| 50 | *mFakeListener); |
| 51 | |
| 52 | mFakePointerController = std::make_shared<FakePointerController>(); |
| 53 | mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1); |
| 54 | mFakePointerController->setPosition(POINTER_X, POINTER_Y); |
| 55 | mFakePolicy->setPointerController(mFakePointerController); |
| 56 | } |
| 57 | |
| 58 | std::shared_ptr<FakeEventHub> mFakeEventHub; |
| 59 | sp<FakeInputReaderPolicy> mFakePolicy; |
| 60 | std::unique_ptr<TestInputListener> mFakeListener; |
| 61 | std::unique_ptr<InstrumentedInputReader> mReader; |
| 62 | std::shared_ptr<FakePointerController> mFakePointerController; |
| 63 | }; |
| 64 | |
| 65 | TEST_F(GestureConverterTest, Move) { |
| 66 | GestureConverter converter(*mReader->getContext(), DEVICE_ID); |
| 67 | |
| 68 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10); |
| 69 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 70 | ASSERT_EQ(1u, args.size()); |
| 71 | |
| 72 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 73 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 74 | WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10), |
| 75 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithButtonState(0), |
| 76 | WithPressure(0.0f))); |
| 77 | |
| 78 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(95, 210)); |
| 79 | } |
| 80 | |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame^] | 81 | TEST_F(GestureConverterTest, Move_Rotated) { |
| 82 | GestureConverter converter(*mReader->getContext(), DEVICE_ID); |
| 83 | converter.setOrientation(ui::ROTATION_90); |
| 84 | |
| 85 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10); |
| 86 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 87 | ASSERT_EQ(1u, args.size()); |
| 88 | |
| 89 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 90 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 91 | WithCoords(POINTER_X + 10, POINTER_Y + 5), WithRelativeMotion(10, 5), |
| 92 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithButtonState(0), |
| 93 | WithPressure(0.0f))); |
| 94 | |
| 95 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110, 205)); |
| 96 | } |
| 97 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 98 | TEST_F(GestureConverterTest, ButtonsChange) { |
| 99 | GestureConverter converter(*mReader->getContext(), DEVICE_ID); |
| 100 | |
| 101 | // Press left and right buttons at once |
| 102 | Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 103 | /* down= */ GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT, |
| 104 | /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false); |
| 105 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture); |
| 106 | ASSERT_EQ(3u, args.size()); |
| 107 | |
| 108 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 109 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 110 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY | |
| 111 | AMOTION_EVENT_BUTTON_SECONDARY), |
| 112 | WithCoords(POINTER_X, POINTER_Y), |
| 113 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 114 | args.pop_front(); |
| 115 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 116 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), |
| 117 | WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), |
| 118 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), |
| 119 | WithCoords(POINTER_X, POINTER_Y), |
| 120 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 121 | args.pop_front(); |
| 122 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 123 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), |
| 124 | WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY), |
| 125 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY | |
| 126 | AMOTION_EVENT_BUTTON_SECONDARY), |
| 127 | WithCoords(POINTER_X, POINTER_Y), |
| 128 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 129 | |
| 130 | // Then release the left button |
| 131 | Gesture leftUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 132 | /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT, |
| 133 | /* is_tap= */ false); |
| 134 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, leftUpGesture); |
| 135 | ASSERT_EQ(1u, args.size()); |
| 136 | |
| 137 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 138 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), |
| 139 | WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), |
| 140 | WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY), |
| 141 | WithCoords(POINTER_X, POINTER_Y), |
| 142 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 143 | |
| 144 | // Finally release the right button |
| 145 | Gesture rightUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 146 | /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_RIGHT, |
| 147 | /* is_tap= */ false); |
| 148 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, rightUpGesture); |
| 149 | ASSERT_EQ(2u, args.size()); |
| 150 | |
| 151 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 152 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), |
| 153 | WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY), WithButtonState(0), |
| 154 | WithCoords(POINTER_X, POINTER_Y), |
| 155 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 156 | args.pop_front(); |
| 157 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 158 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0), |
| 159 | WithCoords(POINTER_X, POINTER_Y), |
| 160 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 161 | } |
| 162 | |
| 163 | TEST_F(GestureConverterTest, DragWithButton) { |
| 164 | GestureConverter converter(*mReader->getContext(), DEVICE_ID); |
| 165 | |
| 166 | // Press the button |
| 167 | Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 168 | /* down= */ GESTURES_BUTTON_LEFT, /* up= */ GESTURES_BUTTON_NONE, |
| 169 | /* is_tap= */ false); |
| 170 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture); |
| 171 | ASSERT_EQ(2u, args.size()); |
| 172 | |
| 173 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 174 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 175 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), |
| 176 | WithCoords(POINTER_X, POINTER_Y), |
| 177 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 178 | args.pop_front(); |
| 179 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 180 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), |
| 181 | WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), |
| 182 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), |
| 183 | WithCoords(POINTER_X, POINTER_Y), |
| 184 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 185 | |
| 186 | // Move |
| 187 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10); |
| 188 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 189 | ASSERT_EQ(1u, args.size()); |
| 190 | |
| 191 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 192 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 193 | WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10), |
| 194 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), |
| 195 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f))); |
| 196 | |
| 197 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(95, 210)); |
| 198 | |
| 199 | // Release the button |
| 200 | Gesture upGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 201 | /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT, |
| 202 | /* is_tap= */ false); |
| 203 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, upGesture); |
| 204 | ASSERT_EQ(2u, args.size()); |
| 205 | |
| 206 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 207 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), |
| 208 | WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0), |
| 209 | WithCoords(POINTER_X - 5, POINTER_Y + 10), |
| 210 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 211 | args.pop_front(); |
| 212 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 213 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0), |
| 214 | WithCoords(POINTER_X - 5, POINTER_Y + 10), |
| 215 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 216 | } |
| 217 | |
| 218 | } // namespace android |