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