Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: ui/events/x/events_x_unittest.cc

Issue 289283015: Extract touchscreen device management into a generic manager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <cstring> 5 #include <cstring>
6 6
7 #include <X11/extensions/XInput2.h> 7 #include <X11/extensions/XInput2.h>
8 #include <X11/Xlib.h> 8 #include <X11/Xlib.h>
9 #include <X11/Xutil.h> 9 #include <X11/Xutil.h>
10 #include <X11/XKBlib.h> 10 #include <X11/XKBlib.h>
11 11
12 // Generically-named #defines from Xlib that conflict with symbols in GTest. 12 // Generically-named #defines from Xlib that conflict with symbols in GTest.
13 #undef Bool 13 #undef Bool
14 #undef None 14 #undef None
15 15
16 #include "base/at_exit.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/events/device_data_manager.h"
17 #include "ui/events/event.h" 19 #include "ui/events/event.h"
18 #include "ui/events/event_constants.h" 20 #include "ui/events/event_constants.h"
19 #include "ui/events/event_utils.h" 21 #include "ui/events/event_utils.h"
20 #include "ui/events/test/events_test_utils_x11.h" 22 #include "ui/events/test/events_test_utils_x11.h"
21 #include "ui/gfx/point.h" 23 #include "ui/gfx/point.h"
22 24
23 namespace ui { 25 namespace ui {
24 26
25 namespace { 27 namespace {
26 28
(...skipping 27 matching lines...) Expand all
54 // x_root/y_root and window/root/subwindow. 56 // x_root/y_root and window/root/subwindow.
55 XKeyEvent* key_event = &(event->xkey); 57 XKeyEvent* key_event = &(event->xkey);
56 key_event->display = display; 58 key_event->display = display;
57 key_event->type = is_press ? KeyPress : KeyRelease; 59 key_event->type = is_press ? KeyPress : KeyRelease;
58 key_event->keycode = keycode; 60 key_event->keycode = keycode;
59 key_event->state = state; 61 key_event->state = state;
60 } 62 }
61 63
62 } // namespace 64 } // namespace
63 65
64 TEST(EventsXTest, ButtonEvents) { 66 class EventsXTest : public testing::Test {
67 public:
68 EventsXTest() {}
69 virtual ~EventsXTest() {}
70
71 virtual void SetUp() OVERRIDE {
72 DeviceDataManager::Initialize();
73 }
74 virtual void TearDown() OVERRIDE {
75 base::AtExitManager::ProcessCallbacksNow();
76 }
77 private:
78 DISALLOW_COPY_AND_ASSIGN(EventsXTest);
79 };
80
81 TEST_F(EventsXTest, ButtonEvents) {
65 XEvent event; 82 XEvent event;
66 gfx::Point location(5, 10); 83 gfx::Point location(5, 10);
67 gfx::Vector2d offset; 84 gfx::Vector2d offset;
68 85
69 InitButtonEvent(&event, true, location, 1, 0); 86 InitButtonEvent(&event, true, location, 1, 0);
70 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event)); 87 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event));
71 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event)); 88 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event));
72 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 89 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
73 90
74 InitButtonEvent(&event, true, location, 2, Button1Mask | ShiftMask); 91 InitButtonEvent(&event, true, location, 2, Button1Mask | ShiftMask);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); 132 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event));
116 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); 133 EXPECT_EQ(0, ui::EventFlagsFromNative(&event));
117 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 134 EXPECT_EQ(location, ui::EventLocationFromNative(&event));
118 offset = ui::GetMouseWheelOffset(&event); 135 offset = ui::GetMouseWheelOffset(&event);
119 EXPECT_EQ(0, offset.y()); 136 EXPECT_EQ(0, offset.y());
120 EXPECT_LT(offset.x(), 0); 137 EXPECT_LT(offset.x(), 0);
121 138
122 // TODO(derat): Test XInput code. 139 // TODO(derat): Test XInput code.
123 } 140 }
124 141
125 TEST(EventsXTest, AvoidExtraEventsOnWheelRelease) { 142 TEST_F(EventsXTest, AvoidExtraEventsOnWheelRelease) {
126 XEvent event; 143 XEvent event;
127 gfx::Point location(5, 10); 144 gfx::Point location(5, 10);
128 145
129 InitButtonEvent(&event, true, location, 4, 0); 146 InitButtonEvent(&event, true, location, 4, 0);
130 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); 147 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event));
131 148
132 // We should return ET_UNKNOWN for the release event instead of returning 149 // We should return ET_UNKNOWN for the release event instead of returning
133 // ET_MOUSEWHEEL; otherwise we'll scroll twice for each scrollwheel step. 150 // ET_MOUSEWHEEL; otherwise we'll scroll twice for each scrollwheel step.
134 InitButtonEvent(&event, false, location, 4, 0); 151 InitButtonEvent(&event, false, location, 4, 0);
135 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(&event)); 152 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(&event));
136 153
137 // TODO(derat): Test XInput code. 154 // TODO(derat): Test XInput code.
138 } 155 }
139 156
140 TEST(EventsXTest, EnterLeaveEvent) { 157 TEST_F(EventsXTest, EnterLeaveEvent) {
141 XEvent event; 158 XEvent event;
142 event.xcrossing.type = EnterNotify; 159 event.xcrossing.type = EnterNotify;
143 event.xcrossing.x = 10; 160 event.xcrossing.x = 10;
144 event.xcrossing.y = 20; 161 event.xcrossing.y = 20;
145 event.xcrossing.x_root = 110; 162 event.xcrossing.x_root = 110;
146 event.xcrossing.y_root = 120; 163 event.xcrossing.y_root = 120;
147 164
148 // Mouse enter events are converted to mouse move events to be consistent with 165 // Mouse enter events are converted to mouse move events to be consistent with
149 // the way views handle mouse enter. See comments for EnterNotify case in 166 // the way views handle mouse enter. See comments for EnterNotify case in
150 // ui::EventTypeFromNative for more details. 167 // ui::EventTypeFromNative for more details.
151 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(&event)); 168 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(&event));
152 EXPECT_EQ("10,20", ui::EventLocationFromNative(&event).ToString()); 169 EXPECT_EQ("10,20", ui::EventLocationFromNative(&event).ToString());
153 EXPECT_EQ("110,120", ui::EventSystemLocationFromNative(&event).ToString()); 170 EXPECT_EQ("110,120", ui::EventSystemLocationFromNative(&event).ToString());
154 171
155 event.xcrossing.type = LeaveNotify; 172 event.xcrossing.type = LeaveNotify;
156 event.xcrossing.x = 30; 173 event.xcrossing.x = 30;
157 event.xcrossing.y = 40; 174 event.xcrossing.y = 40;
158 event.xcrossing.x_root = 230; 175 event.xcrossing.x_root = 230;
159 event.xcrossing.y_root = 240; 176 event.xcrossing.y_root = 240;
160 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(&event)); 177 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(&event));
161 EXPECT_EQ("30,40", ui::EventLocationFromNative(&event).ToString()); 178 EXPECT_EQ("30,40", ui::EventLocationFromNative(&event).ToString());
162 EXPECT_EQ("230,240", ui::EventSystemLocationFromNative(&event).ToString()); 179 EXPECT_EQ("230,240", ui::EventSystemLocationFromNative(&event).ToString());
163 } 180 }
164 181
165 TEST(EventsXTest, ClickCount) { 182 TEST_F(EventsXTest, ClickCount) {
166 XEvent event; 183 XEvent event;
167 gfx::Point location(5, 10); 184 gfx::Point location(5, 10);
168 185
169 for (int i = 1; i <= 3; ++i) { 186 for (int i = 1; i <= 3; ++i) {
170 InitButtonEvent(&event, true, location, 1, 0); 187 InitButtonEvent(&event, true, location, 1, 0);
171 { 188 {
172 MouseEvent mouseev(&event); 189 MouseEvent mouseev(&event);
173 EXPECT_EQ(ui::ET_MOUSE_PRESSED, mouseev.type()); 190 EXPECT_EQ(ui::ET_MOUSE_PRESSED, mouseev.type());
174 EXPECT_EQ(i, mouseev.GetClickCount()); 191 EXPECT_EQ(i, mouseev.GetClickCount());
175 } 192 }
176 193
177 InitButtonEvent(&event, false, location, 1, 0); 194 InitButtonEvent(&event, false, location, 1, 0);
178 { 195 {
179 MouseEvent mouseev(&event); 196 MouseEvent mouseev(&event);
180 EXPECT_EQ(ui::ET_MOUSE_RELEASED, mouseev.type()); 197 EXPECT_EQ(ui::ET_MOUSE_RELEASED, mouseev.type());
181 EXPECT_EQ(i, mouseev.GetClickCount()); 198 EXPECT_EQ(i, mouseev.GetClickCount());
182 } 199 }
183 } 200 }
184 } 201 }
185 202
186 #if defined(USE_XI2_MT) 203 #if defined(USE_XI2_MT)
187 TEST(EventsXTest, TouchEventBasic) { 204 TEST_F(EventsXTest, TouchEventBasic) {
188 std::vector<unsigned int> devices; 205 std::vector<unsigned int> devices;
189 devices.push_back(0); 206 devices.push_back(0);
190 ui::SetUpTouchDevicesForTest(devices); 207 ui::SetUpTouchDevicesForTest(devices);
191 std::vector<Valuator> valuators; 208 std::vector<Valuator> valuators;
192 209
193 // Init touch begin with tracking id 5, touch id 0. 210 // Init touch begin with tracking id 5, touch id 0.
194 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR, 20)); 211 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 20));
195 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION, 0.3f)); 212 valuators.push_back(
196 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE, 100)); 213 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.3f));
214 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 100));
197 ui::ScopedXI2Event scoped_xevent; 215 ui::ScopedXI2Event scoped_xevent;
198 scoped_xevent.InitTouchEvent( 216 scoped_xevent.InitTouchEvent(
199 0, XI_TouchBegin, 5, gfx::Point(10, 10), valuators); 217 0, XI_TouchBegin, 5, gfx::Point(10, 10), valuators);
200 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent)); 218 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent));
201 EXPECT_EQ("10,10", ui::EventLocationFromNative(scoped_xevent).ToString()); 219 EXPECT_EQ("10,10", ui::EventLocationFromNative(scoped_xevent).ToString());
202 EXPECT_EQ(GetTouchId(scoped_xevent), 0); 220 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
203 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); 221 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
204 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.15f); 222 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.15f);
205 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f); 223 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f);
206 224
207 // Touch update, with new orientation info. 225 // Touch update, with new orientation info.
208 valuators.clear(); 226 valuators.clear();
209 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION, 0.5f)); 227 valuators.push_back(
228 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.5f));
210 scoped_xevent.InitTouchEvent( 229 scoped_xevent.InitTouchEvent(
211 0, XI_TouchUpdate, 5, gfx::Point(20, 20), valuators); 230 0, XI_TouchUpdate, 5, gfx::Point(20, 20), valuators);
212 EXPECT_EQ(ui::ET_TOUCH_MOVED, ui::EventTypeFromNative(scoped_xevent)); 231 EXPECT_EQ(ui::ET_TOUCH_MOVED, ui::EventTypeFromNative(scoped_xevent));
213 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent).ToString()); 232 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent).ToString());
214 EXPECT_EQ(GetTouchId(scoped_xevent), 0); 233 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
215 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); 234 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
216 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f); 235 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f);
217 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f); 236 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f);
218 237
219 // Another touch with tracking id 6, touch id 1. 238 // Another touch with tracking id 6, touch id 1.
220 valuators.clear(); 239 valuators.clear();
221 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR, 100)); 240 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 100));
222 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION, 0.9f)); 241 valuators.push_back(Valuator(
223 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE, 500)); 242 DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.9f));
243 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 500));
224 scoped_xevent.InitTouchEvent( 244 scoped_xevent.InitTouchEvent(
225 0, XI_TouchBegin, 6, gfx::Point(200, 200), valuators); 245 0, XI_TouchBegin, 6, gfx::Point(200, 200), valuators);
226 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent)); 246 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent));
227 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString()); 247 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString());
228 EXPECT_EQ(GetTouchId(scoped_xevent), 1); 248 EXPECT_EQ(GetTouchId(scoped_xevent), 1);
229 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 50); 249 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 50);
230 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f); 250 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f);
231 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f); 251 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f);
232 252
233 // Touch with tracking id 5 should have old radius/angle value and new pressue 253 // Touch with tracking id 5 should have old radius/angle value and new pressue
234 // value. 254 // value.
235 valuators.clear(); 255 valuators.clear();
236 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE, 50)); 256 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 50));
237 scoped_xevent.InitTouchEvent( 257 scoped_xevent.InitTouchEvent(
238 0, XI_TouchEnd, 5, gfx::Point(30, 30), valuators); 258 0, XI_TouchEnd, 5, gfx::Point(30, 30), valuators);
239 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent)); 259 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent));
240 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent).ToString()); 260 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent).ToString());
241 EXPECT_EQ(GetTouchId(scoped_xevent), 0); 261 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
242 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); 262 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
243 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f); 263 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f);
244 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.05f); 264 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.05f);
245 265
246 // Touch with tracking id 6 should have old angle/pressure value and new 266 // Touch with tracking id 6 should have old angle/pressure value and new
247 // radius value. 267 // radius value.
248 valuators.clear(); 268 valuators.clear();
249 valuators.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR, 50)); 269 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 50));
250 scoped_xevent.InitTouchEvent( 270 scoped_xevent.InitTouchEvent(
251 0, XI_TouchEnd, 6, gfx::Point(200, 200), valuators); 271 0, XI_TouchEnd, 6, gfx::Point(200, 200), valuators);
252 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent)); 272 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent));
253 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString()); 273 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString());
254 EXPECT_EQ(GetTouchId(scoped_xevent), 1); 274 EXPECT_EQ(GetTouchId(scoped_xevent), 1);
255 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 25); 275 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 25);
256 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f); 276 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f);
257 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f); 277 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f);
258 } 278 }
259 #endif 279 #endif
260 280
261 TEST(EventsXTest, NumpadKeyEvents) { 281 TEST_F(EventsXTest, NumpadKeyEvents) {
262 XEvent event; 282 XEvent event;
263 Display* display = gfx::GetXDisplay(); 283 Display* display = gfx::GetXDisplay();
264 284
265 struct { 285 struct {
266 bool is_numpad_key; 286 bool is_numpad_key;
267 int x_keysym; 287 int x_keysym;
268 ui::KeyboardCode ui_keycode; 288 ui::KeyboardCode ui_keycode;
269 } keys[] = { 289 } keys[] = {
270 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional 290 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional
271 // keysymdef.h numbering. 291 // keysymdef.h numbering.
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 // int keysym = XLookupKeysym(&event.xkey, 0); 384 // int keysym = XLookupKeysym(&event.xkey, 0);
365 // if (keysym) { 385 // if (keysym) {
366 ui::KeyEvent ui_key_event(&event, false); 386 ui::KeyEvent ui_key_event(&event, false);
367 EXPECT_EQ(keys[k].is_numpad_key ? ui::EF_NUMPAD_KEY : 0, 387 EXPECT_EQ(keys[k].is_numpad_key ? ui::EF_NUMPAD_KEY : 0,
368 ui_key_event.flags() & ui::EF_NUMPAD_KEY); 388 ui_key_event.flags() & ui::EF_NUMPAD_KEY);
369 } 389 }
370 } 390 }
371 } 391 }
372 392
373 } // namespace ui 393 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698