| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/events/ozone/evdev/input_injector_evdev.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h" |
| 12 #include "ui/events/ozone/evdev/event_modifiers_evdev.h" |
| 13 #include "ui/events/ozone/evdev/keyboard_evdev.h" |
| 14 #include "ui/events/ozone/events_ozone.h" |
| 15 |
| 16 namespace ui { |
| 17 |
| 18 using testing::AllOf; |
| 19 using testing::InSequence; |
| 20 using testing::Property; |
| 21 |
| 22 class EventObserver { |
| 23 public: |
| 24 void EventDispatchCallback(scoped_ptr<Event> event) { |
| 25 DispatchEventFromNativeUiEvent( |
| 26 event.get(), |
| 27 base::Bind(&EventObserver::OnEvent, base::Unretained(this))); |
| 28 } |
| 29 |
| 30 void OnEvent(Event* event) { |
| 31 if (event->IsMouseEvent()) { |
| 32 if (event->type() == ET_MOUSEWHEEL) { |
| 33 OnMouseWheelEvent(static_cast<MouseWheelEvent*>(event)); |
| 34 } else { |
| 35 OnMouseEvent(static_cast<MouseEvent*>(event)); |
| 36 } |
| 37 } |
| 38 } |
| 39 |
| 40 // Mock functions for intercepting mouse events. |
| 41 MOCK_METHOD1(OnMouseEvent, void(MouseEvent* event)); |
| 42 MOCK_METHOD1(OnMouseWheelEvent, void(MouseWheelEvent* event)); |
| 43 }; |
| 44 |
| 45 class MockCursorEvdev : public CursorDelegateEvdev { |
| 46 public: |
| 47 MockCursorEvdev() {} |
| 48 ~MockCursorEvdev() override {} |
| 49 |
| 50 // CursorDelegateEvdev: |
| 51 void MoveCursorTo(gfx::AcceleratedWidget widget, |
| 52 const gfx::PointF& location) override { |
| 53 cursor_location_ = location; |
| 54 } |
| 55 void MoveCursorTo(const gfx::PointF& location) override { |
| 56 cursor_location_ = location; |
| 57 } |
| 58 void MoveCursor(const gfx::Vector2dF& delta) override { |
| 59 cursor_location_ = gfx::PointF(delta.x(), delta.y()); |
| 60 } |
| 61 bool IsCursorVisible() override { return 1; } |
| 62 gfx::Rect GetCursorDisplayBounds() override { |
| 63 NOTIMPLEMENTED(); |
| 64 return gfx::Rect(); |
| 65 } |
| 66 gfx::PointF location() override { return cursor_location_; } |
| 67 |
| 68 private: |
| 69 // The location of the mock cursor. |
| 70 gfx::PointF cursor_location_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(MockCursorEvdev); |
| 73 }; |
| 74 |
| 75 MATCHER_P4(MatchesMouseEvent, type, button, x, y, "") { |
| 76 if (arg->type() != type) { |
| 77 *result_listener << "Expected type: " << type << " actual: " << arg->type() |
| 78 << " (" << arg->name() << ")"; |
| 79 return false; |
| 80 } |
| 81 if (button == EF_LEFT_MOUSE_BUTTON && !arg->IsLeftMouseButton()) { |
| 82 *result_listener << "Expected the left button flag is set."; |
| 83 return false; |
| 84 } |
| 85 if (button == EF_RIGHT_MOUSE_BUTTON && !arg->IsRightMouseButton()) { |
| 86 *result_listener << "Expected the right button flag is set."; |
| 87 return false; |
| 88 } |
| 89 if (button == EF_MIDDLE_MOUSE_BUTTON && !arg->IsMiddleMouseButton()) { |
| 90 *result_listener << "Expected the middle button flag is set."; |
| 91 return false; |
| 92 } |
| 93 if (arg->x() != x || arg->y() != y) { |
| 94 *result_listener << "Expected location: (" << x << ", " << y |
| 95 << ") actual: (" << arg->x() << ", " << arg->y() << ")"; |
| 96 return false; |
| 97 } |
| 98 return true; |
| 99 } |
| 100 |
| 101 class InputInjectorEvdevTest : public testing::Test { |
| 102 public: |
| 103 InputInjectorEvdevTest(); |
| 104 |
| 105 protected: |
| 106 void SimulateMouseClick(int x, int y, EventFlags button, int count); |
| 107 void ExpectClick(int x, int y, int button, int count); |
| 108 |
| 109 EventObserver event_observer_; |
| 110 EventModifiersEvdev modifiers_; |
| 111 EventDispatchCallback dispatch_callback_; |
| 112 MockCursorEvdev cursor_; |
| 113 KeyboardEvdev keyboard_; |
| 114 |
| 115 InputInjectorEvdev injector_; |
| 116 |
| 117 base::MessageLoop message_loop_; |
| 118 base::RunLoop run_loop_; |
| 119 |
| 120 private: |
| 121 DISALLOW_COPY_AND_ASSIGN(InputInjectorEvdevTest); |
| 122 }; |
| 123 |
| 124 InputInjectorEvdevTest::InputInjectorEvdevTest() |
| 125 : dispatch_callback_(base::Bind(&EventObserver::EventDispatchCallback, |
| 126 base::Unretained(&event_observer_))), |
| 127 keyboard_(&modifiers_, dispatch_callback_), |
| 128 injector_(&modifiers_, &cursor_, &keyboard_, dispatch_callback_) { |
| 129 } |
| 130 |
| 131 void InputInjectorEvdevTest::SimulateMouseClick(int x, |
| 132 int y, |
| 133 EventFlags button, |
| 134 int count) { |
| 135 injector_.MoveCursorTo(gfx::PointF(x, y)); |
| 136 for (int i = 0; i < count; i++) { |
| 137 injector_.InjectMouseButton(button, true); |
| 138 injector_.InjectMouseButton(button, false); |
| 139 } |
| 140 } |
| 141 |
| 142 void InputInjectorEvdevTest::ExpectClick(int x, int y, int button, int count) { |
| 143 InSequence dummy; |
| 144 EXPECT_CALL(event_observer_, |
| 145 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_MOVED, EF_NONE, x, y))); |
| 146 |
| 147 for (int i = 0; i < count; i++) { |
| 148 EXPECT_CALL(event_observer_, OnMouseEvent(MatchesMouseEvent( |
| 149 ET_MOUSE_PRESSED, button, x, y))); |
| 150 EXPECT_CALL(event_observer_, OnMouseEvent(MatchesMouseEvent( |
| 151 ET_MOUSE_RELEASED, button, x, y))); |
| 152 } |
| 153 } |
| 154 |
| 155 TEST_F(InputInjectorEvdevTest, LeftClick) { |
| 156 ExpectClick(12, 13, EF_LEFT_MOUSE_BUTTON, 1); |
| 157 SimulateMouseClick(12, 13, EF_LEFT_MOUSE_BUTTON, 1); |
| 158 run_loop_.RunUntilIdle(); |
| 159 } |
| 160 |
| 161 TEST_F(InputInjectorEvdevTest, RightClick) { |
| 162 ExpectClick(12, 13, EF_RIGHT_MOUSE_BUTTON, 1); |
| 163 SimulateMouseClick(12, 13, EF_RIGHT_MOUSE_BUTTON, 1); |
| 164 run_loop_.RunUntilIdle(); |
| 165 } |
| 166 |
| 167 TEST_F(InputInjectorEvdevTest, DoubleClick) { |
| 168 ExpectClick(12, 13, EF_LEFT_MOUSE_BUTTON, 2); |
| 169 SimulateMouseClick(12, 13, EF_LEFT_MOUSE_BUTTON, 2); |
| 170 run_loop_.RunUntilIdle(); |
| 171 } |
| 172 |
| 173 TEST_F(InputInjectorEvdevTest, MouseMoved) { |
| 174 injector_.MoveCursorTo(gfx::PointF(1, 1)); |
| 175 run_loop_.RunUntilIdle(); |
| 176 EXPECT_EQ(cursor_.location(), gfx::PointF(1, 1)); |
| 177 } |
| 178 |
| 179 TEST_F(InputInjectorEvdevTest, MouseDragged) { |
| 180 InSequence dummy; |
| 181 EXPECT_CALL(event_observer_, |
| 182 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_PRESSED, |
| 183 EF_LEFT_MOUSE_BUTTON, 0, 0))); |
| 184 EXPECT_CALL(event_observer_, |
| 185 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_DRAGGED, |
| 186 EF_LEFT_MOUSE_BUTTON, 1, 1))); |
| 187 EXPECT_CALL(event_observer_, |
| 188 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_DRAGGED, |
| 189 EF_LEFT_MOUSE_BUTTON, 2, 3))); |
| 190 EXPECT_CALL(event_observer_, |
| 191 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_RELEASED, |
| 192 EF_LEFT_MOUSE_BUTTON, 2, 3))); |
| 193 injector_.InjectMouseButton(EF_LEFT_MOUSE_BUTTON, true); |
| 194 injector_.MoveCursorTo(gfx::PointF(1, 1)); |
| 195 injector_.MoveCursorTo(gfx::PointF(2, 3)); |
| 196 injector_.InjectMouseButton(EF_LEFT_MOUSE_BUTTON, false); |
| 197 run_loop_.RunUntilIdle(); |
| 198 } |
| 199 |
| 200 TEST_F(InputInjectorEvdevTest, MouseWheel) { |
| 201 InSequence dummy; |
| 202 EXPECT_CALL(event_observer_, OnMouseWheelEvent(AllOf( |
| 203 MatchesMouseEvent(ET_MOUSEWHEEL, 0, 10, 20), |
| 204 Property(&MouseWheelEvent::x_offset, 0), |
| 205 Property(&MouseWheelEvent::y_offset, 100)))); |
| 206 EXPECT_CALL(event_observer_, OnMouseWheelEvent(AllOf( |
| 207 MatchesMouseEvent(ET_MOUSEWHEEL, 0, 10, 20), |
| 208 Property(&MouseWheelEvent::x_offset, 100), |
| 209 Property(&MouseWheelEvent::y_offset, 0)))); |
| 210 injector_.MoveCursorTo(gfx::PointF(10, 20)); |
| 211 injector_.InjectMouseWheel(0, 100); |
| 212 injector_.InjectMouseWheel(100, 0); |
| 213 run_loop_.RunUntilIdle(); |
| 214 } |
| 215 |
| 216 } // namespace ui |
| OLD | NEW |