Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / events / ozone / evdev / input_injector_evdev_unittest.cc
blob88040c22b9e9d32c9ee3ad7eafc40b3048fef5c9
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.
5 #include "ui/events/ozone/evdev/input_injector_evdev.h"
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/device/device_manager.h"
12 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h"
13 #include "ui/events/ozone/evdev/event_converter_test_util.h"
14 #include "ui/events/ozone/evdev/event_factory_evdev.h"
15 #include "ui/events/ozone/events_ozone.h"
16 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
18 namespace ui {
20 using testing::AllOf;
21 using testing::InSequence;
22 using testing::Property;
24 class EventObserver {
25 public:
26 void EventDispatchCallback(Event* event) {
27 DispatchEventFromNativeUiEvent(
28 event, base::Bind(&EventObserver::OnEvent, base::Unretained(this)));
31 void OnEvent(Event* event) {
32 if (event->IsMouseEvent()) {
33 if (event->type() == ET_MOUSEWHEEL) {
34 OnMouseWheelEvent(static_cast<MouseWheelEvent*>(event));
35 } else {
36 OnMouseEvent(static_cast<MouseEvent*>(event));
41 // Mock functions for intercepting mouse events.
42 MOCK_METHOD1(OnMouseEvent, void(MouseEvent* event));
43 MOCK_METHOD1(OnMouseWheelEvent, void(MouseWheelEvent* event));
46 class MockCursorEvdev : public CursorDelegateEvdev {
47 public:
48 MockCursorEvdev() {}
49 ~MockCursorEvdev() override {}
51 // CursorDelegateEvdev:
52 void MoveCursorTo(gfx::AcceleratedWidget widget,
53 const gfx::PointF& location) override {
54 cursor_location_ = location;
56 void MoveCursorTo(const gfx::PointF& location) override {
57 cursor_location_ = location;
59 void MoveCursor(const gfx::Vector2dF& delta) override {
60 cursor_location_ = gfx::PointF(delta.x(), delta.y());
62 bool IsCursorVisible() override { return 1; }
63 gfx::Rect GetCursorConfinedBounds() override {
64 NOTIMPLEMENTED();
65 return gfx::Rect();
67 gfx::PointF GetLocation() override { return cursor_location_; }
69 private:
70 // The location of the mock cursor.
71 gfx::PointF cursor_location_;
73 DISALLOW_COPY_AND_ASSIGN(MockCursorEvdev);
76 MATCHER_P4(MatchesMouseEvent, type, button, x, y, "") {
77 if (arg->type() != type) {
78 *result_listener << "Expected type: " << type << " actual: " << arg->type()
79 << " (" << arg->name() << ")";
80 return false;
82 if (button == EF_LEFT_MOUSE_BUTTON && !arg->IsLeftMouseButton()) {
83 *result_listener << "Expected the left button flag is set.";
84 return false;
86 if (button == EF_RIGHT_MOUSE_BUTTON && !arg->IsRightMouseButton()) {
87 *result_listener << "Expected the right button flag is set.";
88 return false;
90 if (button == EF_MIDDLE_MOUSE_BUTTON && !arg->IsMiddleMouseButton()) {
91 *result_listener << "Expected the middle button flag is set.";
92 return false;
94 if (arg->x() != x || arg->y() != y) {
95 *result_listener << "Expected location: (" << x << ", " << y
96 << ") actual: (" << arg->x() << ", " << arg->y() << ")";
97 return false;
99 return true;
102 class InputInjectorEvdevTest : public testing::Test {
103 public:
104 InputInjectorEvdevTest();
106 protected:
107 void SimulateMouseClick(int x, int y, EventFlags button, int count);
108 void ExpectClick(int x, int y, int button, int count);
110 EventObserver event_observer_;
111 EventDispatchCallback dispatch_callback_;
112 MockCursorEvdev cursor_;
114 scoped_ptr<DeviceManager> device_manager_;
115 scoped_ptr<EventFactoryEvdev> event_factory_;
117 InputInjectorEvdev injector_;
119 base::MessageLoop message_loop_;
120 base::RunLoop run_loop_;
122 private:
123 DISALLOW_COPY_AND_ASSIGN(InputInjectorEvdevTest);
126 InputInjectorEvdevTest::InputInjectorEvdevTest()
127 : dispatch_callback_(base::Bind(&EventObserver::EventDispatchCallback,
128 base::Unretained(&event_observer_))),
129 device_manager_(CreateDeviceManagerForTest()),
130 event_factory_(CreateEventFactoryEvdevForTest(
131 &cursor_,
132 device_manager_.get(),
133 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine(),
134 dispatch_callback_)),
135 injector_(CreateDeviceEventDispatcherEvdevForTest(event_factory_.get()),
136 &cursor_) {
139 void InputInjectorEvdevTest::SimulateMouseClick(int x,
140 int y,
141 EventFlags button,
142 int count) {
143 injector_.MoveCursorTo(gfx::PointF(x, y));
144 for (int i = 0; i < count; i++) {
145 injector_.InjectMouseButton(button, true);
146 injector_.InjectMouseButton(button, false);
150 void InputInjectorEvdevTest::ExpectClick(int x, int y, int button, int count) {
151 InSequence dummy;
152 EXPECT_CALL(event_observer_,
153 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_MOVED, EF_NONE, x, y)));
155 for (int i = 0; i < count; i++) {
156 EXPECT_CALL(event_observer_, OnMouseEvent(MatchesMouseEvent(
157 ET_MOUSE_PRESSED, button, x, y)));
158 EXPECT_CALL(event_observer_, OnMouseEvent(MatchesMouseEvent(
159 ET_MOUSE_RELEASED, button, x, y)));
163 TEST_F(InputInjectorEvdevTest, LeftClick) {
164 ExpectClick(12, 13, EF_LEFT_MOUSE_BUTTON, 1);
165 SimulateMouseClick(12, 13, EF_LEFT_MOUSE_BUTTON, 1);
166 run_loop_.RunUntilIdle();
169 TEST_F(InputInjectorEvdevTest, RightClick) {
170 ExpectClick(12, 13, EF_RIGHT_MOUSE_BUTTON, 1);
171 SimulateMouseClick(12, 13, EF_RIGHT_MOUSE_BUTTON, 1);
172 run_loop_.RunUntilIdle();
175 TEST_F(InputInjectorEvdevTest, DoubleClick) {
176 ExpectClick(12, 13, EF_LEFT_MOUSE_BUTTON, 2);
177 SimulateMouseClick(12, 13, EF_LEFT_MOUSE_BUTTON, 2);
178 run_loop_.RunUntilIdle();
181 TEST_F(InputInjectorEvdevTest, MouseMoved) {
182 injector_.MoveCursorTo(gfx::PointF(1, 1));
183 run_loop_.RunUntilIdle();
184 EXPECT_EQ(cursor_.GetLocation(), gfx::PointF(1, 1));
187 TEST_F(InputInjectorEvdevTest, MouseDragged) {
188 InSequence dummy;
189 EXPECT_CALL(event_observer_,
190 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_PRESSED,
191 EF_LEFT_MOUSE_BUTTON, 0, 0)));
192 EXPECT_CALL(event_observer_,
193 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_DRAGGED,
194 EF_LEFT_MOUSE_BUTTON, 1, 1)));
195 EXPECT_CALL(event_observer_,
196 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_DRAGGED,
197 EF_LEFT_MOUSE_BUTTON, 2, 3)));
198 EXPECT_CALL(event_observer_,
199 OnMouseEvent(MatchesMouseEvent(ET_MOUSE_RELEASED,
200 EF_LEFT_MOUSE_BUTTON, 2, 3)));
201 injector_.InjectMouseButton(EF_LEFT_MOUSE_BUTTON, true);
202 injector_.MoveCursorTo(gfx::PointF(1, 1));
203 injector_.MoveCursorTo(gfx::PointF(2, 3));
204 injector_.InjectMouseButton(EF_LEFT_MOUSE_BUTTON, false);
205 run_loop_.RunUntilIdle();
208 TEST_F(InputInjectorEvdevTest, MouseWheel) {
209 InSequence dummy;
210 EXPECT_CALL(event_observer_, OnMouseWheelEvent(AllOf(
211 MatchesMouseEvent(ET_MOUSEWHEEL, 0, 10, 20),
212 Property(&MouseWheelEvent::x_offset, 0),
213 Property(&MouseWheelEvent::y_offset, 100))));
214 EXPECT_CALL(event_observer_, OnMouseWheelEvent(AllOf(
215 MatchesMouseEvent(ET_MOUSEWHEEL, 0, 10, 20),
216 Property(&MouseWheelEvent::x_offset, 100),
217 Property(&MouseWheelEvent::y_offset, 0))));
218 injector_.MoveCursorTo(gfx::PointF(10, 20));
219 injector_.InjectMouseWheel(0, 100);
220 injector_.InjectMouseWheel(100, 0);
221 run_loop_.RunUntilIdle();
224 } // namespace ui