1 // Copyright (c) 2012 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.
7 #include <X11/extensions/XInput2.h>
10 // Generically-named #defines from Xlib that conflict with symbols in GTest.
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_constants.h"
17 #include "ui/events/event_utils.h"
18 #include "ui/events/test/events_test_utils_x11.h"
19 #include "ui/gfx/point.h"
25 // Initializes the passed-in Xlib event.
26 void InitButtonEvent(XEvent* event,
28 const gfx::Point& location,
31 memset(event, 0, sizeof(*event));
33 // We don't bother setting fields that the event code doesn't use, such as
34 // x_root/y_root and window/root/subwindow.
35 XButtonEvent* button_event = &(event->xbutton);
36 button_event->type = is_press ? ButtonPress : ButtonRelease;
37 button_event->x = location.x();
38 button_event->y = location.y();
39 button_event->button = button;
40 button_event->state = state;
45 TEST(EventsXTest, ButtonEvents) {
47 gfx::Point location(5, 10);
50 InitButtonEvent(&event, true, location, 1, 0);
51 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event));
52 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event));
53 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
55 InitButtonEvent(&event, true, location, 2, Button1Mask | ShiftMask);
56 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event));
57 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON |
59 ui::EventFlagsFromNative(&event));
60 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
62 InitButtonEvent(&event, false, location, 3, 0);
63 EXPECT_EQ(ui::ET_MOUSE_RELEASED, ui::EventTypeFromNative(&event));
64 EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event));
65 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
68 InitButtonEvent(&event, true, location, 4, 0);
69 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event));
70 EXPECT_EQ(0, ui::EventFlagsFromNative(&event));
71 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
72 offset = ui::GetMouseWheelOffset(&event);
73 EXPECT_GT(offset.y(), 0);
74 EXPECT_EQ(0, offset.x());
77 InitButtonEvent(&event, true, location, 5, 0);
78 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event));
79 EXPECT_EQ(0, ui::EventFlagsFromNative(&event));
80 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
81 offset = ui::GetMouseWheelOffset(&event);
82 EXPECT_LT(offset.y(), 0);
83 EXPECT_EQ(0, offset.x());
86 InitButtonEvent(&event, true, location, 6, 0);
87 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event));
88 EXPECT_EQ(0, ui::EventFlagsFromNative(&event));
89 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
90 offset = ui::GetMouseWheelOffset(&event);
91 EXPECT_EQ(0, offset.y());
92 EXPECT_GT(offset.x(), 0);
95 InitButtonEvent(&event, true, location, 7, 0);
96 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event));
97 EXPECT_EQ(0, ui::EventFlagsFromNative(&event));
98 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
99 offset = ui::GetMouseWheelOffset(&event);
100 EXPECT_EQ(0, offset.y());
101 EXPECT_LT(offset.x(), 0);
103 // TODO(derat): Test XInput code.
106 TEST(EventsXTest, AvoidExtraEventsOnWheelRelease) {
108 gfx::Point location(5, 10);
110 InitButtonEvent(&event, true, location, 4, 0);
111 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event));
113 // We should return ET_UNKNOWN for the release event instead of returning
114 // ET_MOUSEWHEEL; otherwise we'll scroll twice for each scrollwheel step.
115 InitButtonEvent(&event, false, location, 4, 0);
116 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(&event));
118 // TODO(derat): Test XInput code.
121 TEST(EventsXTest, EnterLeaveEvent) {
123 event.xcrossing.type = EnterNotify;
124 event.xcrossing.x = 10;
125 event.xcrossing.y = 20;
126 event.xcrossing.x_root = 110;
127 event.xcrossing.y_root = 120;
129 // Mouse enter events are converted to mouse move events to be consistent with
130 // the way views handle mouse enter. See comments for EnterNotify case in
131 // ui::EventTypeFromNative for more details.
132 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(&event));
133 EXPECT_EQ("10,20", ui::EventLocationFromNative(&event).ToString());
134 EXPECT_EQ("110,120", ui::EventSystemLocationFromNative(&event).ToString());
136 event.xcrossing.type = LeaveNotify;
137 event.xcrossing.x = 30;
138 event.xcrossing.y = 40;
139 event.xcrossing.x_root = 230;
140 event.xcrossing.y_root = 240;
141 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(&event));
142 EXPECT_EQ("30,40", ui::EventLocationFromNative(&event).ToString());
143 EXPECT_EQ("230,240", ui::EventSystemLocationFromNative(&event).ToString());
146 TEST(EventsXTest, ClickCount) {
148 gfx::Point location(5, 10);
150 for (int i = 1; i <= 3; ++i) {
151 InitButtonEvent(&event, true, location, 1, 0);
153 MouseEvent mouseev(&event);
154 EXPECT_EQ(ui::ET_MOUSE_PRESSED, mouseev.type());
155 EXPECT_EQ(i, mouseev.GetClickCount());
158 InitButtonEvent(&event, false, location, 1, 0);
160 MouseEvent mouseev(&event);
161 EXPECT_EQ(ui::ET_MOUSE_RELEASED, mouseev.type());
162 EXPECT_EQ(i, mouseev.GetClickCount());
167 #if defined(USE_XI2_MT)
168 TEST(EventsXTest, TouchEventBasic) {
169 std::vector<unsigned int> devices;
170 devices.push_back(0);
171 ui::SetUpTouchDevicesForTest(devices);
172 std::vector<Valuator> valuators;
174 // Init touch begin with tracking id 5, touch id 0.
175 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR, 20));
176 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION, 0.3f));
177 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE, 100));
178 ui::ScopedXI2Event scoped_xevent;
179 scoped_xevent.InitTouchEvent(
180 0, XI_TouchBegin, 5, gfx::Point(10, 10), valuators);
181 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent));
182 EXPECT_EQ("10,10", ui::EventLocationFromNative(scoped_xevent).ToString());
183 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
184 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
185 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.15f);
186 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f);
188 // Touch update, with new orientation info.
190 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION, 0.5f));
191 scoped_xevent.InitTouchEvent(
192 0, XI_TouchUpdate, 5, gfx::Point(20, 20), valuators);
193 EXPECT_EQ(ui::ET_TOUCH_MOVED, ui::EventTypeFromNative(scoped_xevent));
194 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent).ToString());
195 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
196 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
197 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f);
198 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f);
200 // Another touch with tracking id 6, touch id 1.
202 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR, 100));
203 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION, 0.9f));
204 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE, 500));
205 scoped_xevent.InitTouchEvent(
206 0, XI_TouchBegin, 6, gfx::Point(200, 200), valuators);
207 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent));
208 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString());
209 EXPECT_EQ(GetTouchId(scoped_xevent), 1);
210 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 50);
211 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f);
212 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f);
214 // Touch with tracking id 5 should have old radius/angle value and new pressue
217 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE, 50));
218 scoped_xevent.InitTouchEvent(
219 0, XI_TouchEnd, 5, gfx::Point(30, 30), valuators);
220 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent));
221 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent).ToString());
222 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
223 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
224 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f);
225 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.05f);
227 // Touch with tracking id 6 should have old angle/pressure value and new
230 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR, 50));
231 scoped_xevent.InitTouchEvent(
232 0, XI_TouchEnd, 6, gfx::Point(200, 200), valuators);
233 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent));
234 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString());
235 EXPECT_EQ(GetTouchId(scoped_xevent), 1);
236 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 25);
237 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f);
238 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f);