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.
5 #include "base/memory/scoped_ptr.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/events/event.h"
8 #include "ui/events/event_utils.h"
9 #include "ui/events/keycodes/dom/dom_code.h"
10 #include "ui/events/keycodes/dom/keycode_converter.h"
11 #include "ui/events/test/events_test_utils.h"
15 #include "ui/events/test/events_test_utils_x11.h"
16 #include "ui/gfx/x/x11_types.h"
21 TEST(EventTest
, NoNativeEvent
) {
22 KeyEvent
keyev(ET_KEY_PRESSED
, VKEY_SPACE
, EF_NONE
);
23 EXPECT_FALSE(keyev
.HasNativeEvent());
26 TEST(EventTest
, NativeEvent
) {
28 MSG native_event
= { NULL
, WM_KEYUP
, VKEY_A
, 0 };
29 KeyEvent
keyev(native_event
);
30 EXPECT_TRUE(keyev
.HasNativeEvent());
31 #elif defined(USE_X11)
33 event
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_A
, EF_NONE
);
34 KeyEvent
keyev(event
);
35 EXPECT_TRUE(keyev
.HasNativeEvent());
39 TEST(EventTest
, GetCharacter
) {
40 // Check if Control+Enter returns 10.
41 KeyEvent
keyev1(ET_KEY_PRESSED
, VKEY_RETURN
, EF_CONTROL_DOWN
);
42 EXPECT_EQ(10, keyev1
.GetCharacter());
43 // Check if Enter returns 13.
44 KeyEvent
keyev2(ET_KEY_PRESSED
, VKEY_RETURN
, EF_NONE
);
45 EXPECT_EQ(13, keyev2
.GetCharacter());
48 // For X11, test the functions with native_event() as well. crbug.com/107837
50 event
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_RETURN
, EF_CONTROL_DOWN
);
51 KeyEvent
keyev3(event
);
52 EXPECT_EQ(10, keyev3
.GetCharacter());
54 event
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_RETURN
, EF_NONE
);
55 KeyEvent
keyev4(event
);
56 EXPECT_EQ(13, keyev4
.GetCharacter());
60 TEST(EventTest
, ClickCount
) {
61 const gfx::Point
origin(0, 0);
62 MouseEvent
mouseev(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(), 0, 0);
63 for (int i
= 1; i
<=3 ; ++i
) {
64 mouseev
.SetClickCount(i
);
65 EXPECT_EQ(i
, mouseev
.GetClickCount());
69 TEST(EventTest
, RepeatedClick
) {
70 const gfx::Point
origin(0, 0);
71 MouseEvent
mouse_ev1(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(), 0,
73 MouseEvent
mouse_ev2(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(), 0,
75 LocatedEventTestApi
test_ev1(&mouse_ev1
);
76 LocatedEventTestApi
test_ev2(&mouse_ev2
);
78 base::TimeDelta start
= base::TimeDelta::FromMilliseconds(0);
79 base::TimeDelta soon
= start
+ base::TimeDelta::FromMilliseconds(1);
80 base::TimeDelta later
= start
+ base::TimeDelta::FromMilliseconds(1000);
83 test_ev1
.set_location(gfx::Point(0, 0));
84 test_ev2
.set_location(gfx::Point(1, 0));
85 test_ev1
.set_time_stamp(start
);
86 test_ev2
.set_time_stamp(start
);
87 EXPECT_FALSE(MouseEvent::IsRepeatedClickEvent(mouse_ev1
, mouse_ev2
));
88 MouseEvent
mouse_ev3(mouse_ev1
);
89 EXPECT_FALSE(MouseEvent::IsRepeatedClickEvent(mouse_ev1
, mouse_ev3
));
92 test_ev1
.set_location(gfx::Point(0, 0));
93 test_ev2
.set_location(gfx::Point(1, 0));
94 test_ev1
.set_time_stamp(start
);
95 test_ev2
.set_time_stamp(soon
);
96 EXPECT_TRUE(MouseEvent::IsRepeatedClickEvent(mouse_ev1
, mouse_ev2
));
99 test_ev1
.set_location(gfx::Point(0, 0));
100 test_ev2
.set_location(gfx::Point(10, 0));
101 test_ev1
.set_time_stamp(start
);
102 test_ev2
.set_time_stamp(soon
);
103 EXPECT_FALSE(MouseEvent::IsRepeatedClickEvent(mouse_ev1
, mouse_ev2
));
105 // Too long a time between clicks.
106 test_ev1
.set_location(gfx::Point(0, 0));
107 test_ev2
.set_location(gfx::Point(0, 0));
108 test_ev1
.set_time_stamp(start
);
109 test_ev2
.set_time_stamp(later
);
110 EXPECT_FALSE(MouseEvent::IsRepeatedClickEvent(mouse_ev1
, mouse_ev2
));
113 // Tests that an event only increases the click count and gets marked as a
114 // double click if a release event was seen for the previous click. This
115 // prevents the same PRESSED event from being processed twice:
116 // http://crbug.com/389162
117 TEST(EventTest
, DoubleClickRequiresRelease
) {
118 const gfx::Point
origin1(0, 0);
119 const gfx::Point
origin2(100, 0);
120 scoped_ptr
<MouseEvent
> ev
;
121 base::TimeDelta start
= base::TimeDelta::FromMilliseconds(0);
122 base::TimeDelta soon
= start
+ base::TimeDelta::FromMilliseconds(1);
124 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin1
, origin1
, EventTimeForNow(),
126 ev
->set_time_stamp(start
);
127 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
128 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin1
, origin1
, EventTimeForNow(),
130 ev
->set_time_stamp(start
);
131 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
133 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin2
, origin2
, EventTimeForNow(),
135 ev
->set_time_stamp(start
);
136 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
137 ev
.reset(new MouseEvent(ET_MOUSE_RELEASED
, origin2
, origin2
,
138 EventTimeForNow(), 0, 0));
139 ev
->set_time_stamp(start
);
140 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
141 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin2
, origin2
, EventTimeForNow(),
143 ev
->set_time_stamp(soon
);
144 EXPECT_EQ(2, MouseEvent::GetRepeatCount(*ev
));
145 ev
.reset(new MouseEvent(ET_MOUSE_RELEASED
, origin2
, origin2
,
146 EventTimeForNow(), 0, 0));
147 ev
->set_time_stamp(soon
);
148 EXPECT_EQ(2, MouseEvent::GetRepeatCount(*ev
));
149 MouseEvent::ResetLastClickForTest();
152 // Tests that clicking right and then left clicking does not generate a double
154 TEST(EventTest
, SingleClickRightLeft
) {
155 const gfx::Point
origin(0, 0);
156 scoped_ptr
<MouseEvent
> ev
;
157 base::TimeDelta start
= base::TimeDelta::FromMilliseconds(0);
158 base::TimeDelta soon
= start
+ base::TimeDelta::FromMilliseconds(1);
160 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(),
161 ui::EF_RIGHT_MOUSE_BUTTON
,
162 ui::EF_RIGHT_MOUSE_BUTTON
));
163 ev
->set_time_stamp(start
);
164 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
165 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(),
166 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
167 ev
->set_time_stamp(start
);
168 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
169 ev
.reset(new MouseEvent(ET_MOUSE_RELEASED
, origin
, origin
, EventTimeForNow(),
170 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
171 ev
->set_time_stamp(start
);
172 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
173 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(),
174 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
175 ev
->set_time_stamp(soon
);
176 EXPECT_EQ(2, MouseEvent::GetRepeatCount(*ev
));
177 MouseEvent::ResetLastClickForTest();
180 TEST(EventTest
, KeyEvent
) {
181 static const struct {
182 KeyboardCode key_code
;
187 { VKEY_A
, EF_SHIFT_DOWN
, 'A' },
188 { VKEY_A
, EF_CAPS_LOCK_DOWN
, 'A' },
189 { VKEY_A
, EF_SHIFT_DOWN
| EF_CAPS_LOCK_DOWN
, 'a' },
190 { VKEY_A
, EF_CONTROL_DOWN
, 0x01 },
191 { VKEY_A
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\x01' },
193 { VKEY_Z
, EF_SHIFT_DOWN
, 'Z' },
194 { VKEY_Z
, EF_CAPS_LOCK_DOWN
, 'Z' },
195 { VKEY_Z
, EF_SHIFT_DOWN
| EF_CAPS_LOCK_DOWN
, 'z' },
196 { VKEY_Z
, EF_CONTROL_DOWN
, '\x1A' },
197 { VKEY_Z
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\x1A' },
199 { VKEY_2
, EF_CONTROL_DOWN
, '\0' },
200 { VKEY_2
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
201 { VKEY_6
, EF_CONTROL_DOWN
, '\0' },
202 { VKEY_6
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\x1E' },
203 { VKEY_OEM_MINUS
, EF_CONTROL_DOWN
, '\0' },
204 { VKEY_OEM_MINUS
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\x1F' },
205 { VKEY_OEM_4
, EF_CONTROL_DOWN
, '\x1B' },
206 { VKEY_OEM_4
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
207 { VKEY_OEM_5
, EF_CONTROL_DOWN
, '\x1C' },
208 { VKEY_OEM_5
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
209 { VKEY_OEM_6
, EF_CONTROL_DOWN
, '\x1D' },
210 { VKEY_OEM_6
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
211 { VKEY_RETURN
, EF_CONTROL_DOWN
, '\x0A' },
214 { VKEY_0
, EF_SHIFT_DOWN
, ')' },
215 { VKEY_0
, EF_SHIFT_DOWN
| EF_CAPS_LOCK_DOWN
, ')' },
216 { VKEY_0
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
219 { VKEY_9
, EF_SHIFT_DOWN
, '(' },
220 { VKEY_9
, EF_SHIFT_DOWN
| EF_CAPS_LOCK_DOWN
, '(' },
221 { VKEY_9
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
223 { VKEY_NUMPAD0
, EF_CONTROL_DOWN
, '\0' },
224 { VKEY_NUMPAD0
, EF_SHIFT_DOWN
, '0' },
226 { VKEY_NUMPAD9
, EF_CONTROL_DOWN
, '\0' },
227 { VKEY_NUMPAD9
, EF_SHIFT_DOWN
, '9' },
229 { VKEY_TAB
, EF_CONTROL_DOWN
, '\0' },
230 { VKEY_TAB
, EF_SHIFT_DOWN
, '\t' },
232 { VKEY_MULTIPLY
, EF_CONTROL_DOWN
, '\0' },
233 { VKEY_MULTIPLY
, EF_SHIFT_DOWN
, '*' },
234 { VKEY_ADD
, EF_CONTROL_DOWN
, '\0' },
235 { VKEY_ADD
, EF_SHIFT_DOWN
, '+' },
236 { VKEY_SUBTRACT
, EF_CONTROL_DOWN
, '\0' },
237 { VKEY_SUBTRACT
, EF_SHIFT_DOWN
, '-' },
238 { VKEY_DECIMAL
, EF_CONTROL_DOWN
, '\0' },
239 { VKEY_DECIMAL
, EF_SHIFT_DOWN
, '.' },
240 { VKEY_DIVIDE
, EF_CONTROL_DOWN
, '\0' },
241 { VKEY_DIVIDE
, EF_SHIFT_DOWN
, '/' },
243 { VKEY_OEM_1
, EF_CONTROL_DOWN
, '\0' },
244 { VKEY_OEM_1
, EF_SHIFT_DOWN
, ':' },
245 { VKEY_OEM_PLUS
, EF_CONTROL_DOWN
, '\0' },
246 { VKEY_OEM_PLUS
, EF_SHIFT_DOWN
, '+' },
247 { VKEY_OEM_COMMA
, EF_CONTROL_DOWN
, '\0' },
248 { VKEY_OEM_COMMA
, EF_SHIFT_DOWN
, '<' },
249 { VKEY_OEM_PERIOD
, EF_CONTROL_DOWN
, '\0' },
250 { VKEY_OEM_PERIOD
, EF_SHIFT_DOWN
, '>' },
251 { VKEY_OEM_3
, EF_CONTROL_DOWN
, '\0' },
252 { VKEY_OEM_3
, EF_SHIFT_DOWN
, '~' },
255 for (size_t i
= 0; i
< arraysize(kTestData
); ++i
) {
256 KeyEvent
key(ET_KEY_PRESSED
,
257 kTestData
[i
].key_code
,
259 EXPECT_EQ(kTestData
[i
].character
, key
.GetCharacter())
260 << " Index:" << i
<< " key_code:" << kTestData
[i
].key_code
;
264 TEST(EventTest
, KeyEventDirectUnicode
) {
265 KeyEvent
key(0x1234U
, ui::VKEY_UNKNOWN
, ui::EF_NONE
);
266 EXPECT_EQ(0x1234U
, key
.GetCharacter());
267 EXPECT_EQ(ET_KEY_PRESSED
, key
.type());
268 EXPECT_TRUE(key
.is_char());
271 TEST(EventTest
, NormalizeKeyEventFlags
) {
273 // Normalize flags when KeyEvent is created from XEvent.
274 ScopedXI2Event event
;
276 event
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_SHIFT
, EF_SHIFT_DOWN
);
277 KeyEvent
keyev(event
);
278 EXPECT_EQ(EF_SHIFT_DOWN
, keyev
.flags());
281 event
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_SHIFT
, EF_SHIFT_DOWN
);
282 KeyEvent
keyev(event
);
283 EXPECT_EQ(EF_NONE
, keyev
.flags());
286 event
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_CONTROL
, EF_CONTROL_DOWN
);
287 KeyEvent
keyev(event
);
288 EXPECT_EQ(EF_CONTROL_DOWN
, keyev
.flags());
291 event
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_CONTROL
, EF_CONTROL_DOWN
);
292 KeyEvent
keyev(event
);
293 EXPECT_EQ(EF_NONE
, keyev
.flags());
296 event
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_MENU
, EF_ALT_DOWN
);
297 KeyEvent
keyev(event
);
298 EXPECT_EQ(EF_ALT_DOWN
, keyev
.flags());
301 event
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_MENU
, EF_ALT_DOWN
);
302 KeyEvent
keyev(event
);
303 EXPECT_EQ(EF_NONE
, keyev
.flags());
307 // Do not normalize flags for synthesized events without
308 // KeyEvent::NormalizeFlags called explicitly.
310 KeyEvent
keyev(ET_KEY_PRESSED
, VKEY_SHIFT
, EF_SHIFT_DOWN
);
311 EXPECT_EQ(EF_SHIFT_DOWN
, keyev
.flags());
314 KeyEvent
keyev(ET_KEY_RELEASED
, VKEY_SHIFT
, EF_SHIFT_DOWN
);
315 EXPECT_EQ(EF_SHIFT_DOWN
, keyev
.flags());
316 keyev
.NormalizeFlags();
317 EXPECT_EQ(EF_NONE
, keyev
.flags());
320 KeyEvent
keyev(ET_KEY_PRESSED
, VKEY_CONTROL
, EF_CONTROL_DOWN
);
321 EXPECT_EQ(EF_CONTROL_DOWN
, keyev
.flags());
324 KeyEvent
keyev(ET_KEY_RELEASED
, VKEY_CONTROL
, EF_CONTROL_DOWN
);
325 EXPECT_EQ(EF_CONTROL_DOWN
, keyev
.flags());
326 keyev
.NormalizeFlags();
327 EXPECT_EQ(EF_NONE
, keyev
.flags());
330 KeyEvent
keyev(ET_KEY_PRESSED
, VKEY_MENU
, EF_ALT_DOWN
);
331 EXPECT_EQ(EF_ALT_DOWN
, keyev
.flags());
334 KeyEvent
keyev(ET_KEY_RELEASED
, VKEY_MENU
, EF_ALT_DOWN
);
335 EXPECT_EQ(EF_ALT_DOWN
, keyev
.flags());
336 keyev
.NormalizeFlags();
337 EXPECT_EQ(EF_NONE
, keyev
.flags());
341 TEST(EventTest
, KeyEventCopy
) {
342 KeyEvent
key(ET_KEY_PRESSED
, VKEY_A
, EF_NONE
);
343 scoped_ptr
<KeyEvent
> copied_key(new KeyEvent(key
));
344 EXPECT_EQ(copied_key
->type(), key
.type());
345 EXPECT_EQ(copied_key
->key_code(), key
.key_code());
348 TEST(EventTest
, KeyEventCode
) {
349 const DomCode kDomCodeForSpace
= DomCode::SPACE
;
350 const char kCodeForSpace
[] = "Space";
351 ASSERT_EQ(kDomCodeForSpace
,
352 ui::KeycodeConverter::CodeStringToDomCode(kCodeForSpace
));
353 const uint16 kNativeCodeSpace
=
354 ui::KeycodeConverter::DomCodeToNativeKeycode(kDomCodeForSpace
);
355 ASSERT_NE(ui::KeycodeConverter::InvalidNativeKeycode(), kNativeCodeSpace
);
356 ASSERT_EQ(kNativeCodeSpace
,
357 ui::KeycodeConverter::DomCodeToNativeKeycode(kDomCodeForSpace
));
360 KeyEvent
key(ET_KEY_PRESSED
, VKEY_SPACE
, kDomCodeForSpace
, EF_NONE
);
361 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
364 // Regardless the KeyEvent.key_code (VKEY_RETURN), code should be
365 // the specified value.
366 KeyEvent
key(ET_KEY_PRESSED
, VKEY_RETURN
, kDomCodeForSpace
, EF_NONE
);
367 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
370 // If the synthetic event is initialized without code, the code is
371 // determined from the KeyboardCode assuming a US keyboard layout.
372 KeyEvent
key(ET_KEY_PRESSED
, VKEY_SPACE
, EF_NONE
);
373 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
377 // KeyEvent converts from the native keycode (XKB) to the code.
378 ScopedXI2Event xevent
;
379 xevent
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_SPACE
, kNativeCodeSpace
);
380 KeyEvent
key(xevent
);
381 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
386 // Test a non extended key.
387 ASSERT_EQ((kNativeCodeSpace
& 0xFF), kNativeCodeSpace
);
389 const LPARAM lParam
= GetLParamFromScanCode(kNativeCodeSpace
);
390 MSG native_event
= { NULL
, WM_KEYUP
, VKEY_SPACE
, lParam
};
391 KeyEvent
key(native_event
);
393 // KeyEvent converts from the native keycode (scan code) to the code.
394 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
397 const char kCodeForHome
[] = "Home";
398 const uint16 kNativeCodeHome
= 0xe047;
400 // 'Home' is an extended key with 0xe000 bits.
401 ASSERT_NE((kNativeCodeHome
& 0xFF), kNativeCodeHome
);
402 const LPARAM lParam
= GetLParamFromScanCode(kNativeCodeHome
);
404 MSG native_event
= { NULL
, WM_KEYUP
, VKEY_HOME
, lParam
};
405 KeyEvent
key(native_event
);
407 // KeyEvent converts from the native keycode (scan code) to the code.
408 EXPECT_EQ(kCodeForHome
, key
.GetCodeString());
415 void SetKeyEventTimestamp(XEvent
* event
, long time
) {
416 event
->xkey
.time
= time
;
419 void AdvanceKeyEventTimestamp(XEvent
* event
) {
423 #elif defined(OS_WIN)
424 void SetKeyEventTimestamp(MSG
& msg
, long time
) {
428 void AdvanceKeyEventTimestamp(MSG
& msg
) {
434 #if defined(USE_X11) || defined(OS_WIN)
435 TEST(EventTest
, AutoRepeat
) {
436 const uint16 kNativeCodeA
=
437 ui::KeycodeConverter::DomCodeToNativeKeycode(DomCode::KEY_A
);
438 const uint16 kNativeCodeB
=
439 ui::KeycodeConverter::DomCodeToNativeKeycode(DomCode::KEY_B
);
441 ScopedXI2Event native_event_a_pressed
;
442 native_event_a_pressed
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
443 ScopedXI2Event native_event_a_pressed_1500
;
444 native_event_a_pressed_1500
.InitKeyEvent(
445 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
446 ScopedXI2Event native_event_a_pressed_3000
;
447 native_event_a_pressed_3000
.InitKeyEvent(
448 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
450 ScopedXI2Event native_event_a_released
;
451 native_event_a_released
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_A
, kNativeCodeA
);
452 ScopedXI2Event native_event_b_pressed
;
453 native_event_b_pressed
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_B
, kNativeCodeB
);
454 ScopedXI2Event native_event_a_pressed_nonstandard_state
;
455 native_event_a_pressed_nonstandard_state
.InitKeyEvent(
456 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
457 // IBUS-GTK uses the mask (1 << 25) to detect reposted event.
458 static_cast<XEvent
*>(native_event_a_pressed_nonstandard_state
)->xkey
.state
|=
460 #elif defined(OS_WIN)
461 const LPARAM lParam_a
= GetLParamFromScanCode(kNativeCodeA
);
462 const LPARAM lParam_b
= GetLParamFromScanCode(kNativeCodeB
);
463 MSG native_event_a_pressed
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
464 MSG native_event_a_pressed_1500
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
465 MSG native_event_a_pressed_3000
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
466 MSG native_event_a_released
= { NULL
, WM_KEYUP
, VKEY_A
, lParam_a
};
467 MSG native_event_b_pressed
= { NULL
, WM_KEYUP
, VKEY_B
, lParam_b
};
469 SetKeyEventTimestamp(native_event_a_pressed_1500
, 1500);
470 SetKeyEventTimestamp(native_event_a_pressed_3000
, 3000);
473 KeyEvent
key_a1(native_event_a_pressed
);
474 EXPECT_FALSE(key_a1
.IsRepeat());
476 KeyEvent
key_a1_with_same_event(native_event_a_pressed
);
477 EXPECT_FALSE(key_a1_with_same_event
.IsRepeat());
479 KeyEvent
key_a1_released(native_event_a_released
);
480 EXPECT_FALSE(key_a1_released
.IsRepeat());
482 KeyEvent
key_a2(native_event_a_pressed
);
483 EXPECT_FALSE(key_a2
.IsRepeat());
485 AdvanceKeyEventTimestamp(native_event_a_pressed
);
486 KeyEvent
key_a2_repeated(native_event_a_pressed
);
487 EXPECT_TRUE(key_a2_repeated
.IsRepeat());
489 KeyEvent
key_a2_released(native_event_a_released
);
490 EXPECT_FALSE(key_a2_released
.IsRepeat());
493 // Interleaved with different key press.
495 KeyEvent
key_a3(native_event_a_pressed
);
496 EXPECT_FALSE(key_a3
.IsRepeat());
498 KeyEvent
key_b(native_event_b_pressed
);
499 EXPECT_FALSE(key_b
.IsRepeat());
501 AdvanceKeyEventTimestamp(native_event_a_pressed
);
502 KeyEvent
key_a3_again(native_event_a_pressed
);
503 EXPECT_FALSE(key_a3_again
.IsRepeat());
505 AdvanceKeyEventTimestamp(native_event_a_pressed
);
506 KeyEvent
key_a3_repeated(native_event_a_pressed
);
507 EXPECT_TRUE(key_a3_repeated
.IsRepeat());
509 AdvanceKeyEventTimestamp(native_event_a_pressed
);
510 KeyEvent
key_a3_repeated2(native_event_a_pressed
);
511 EXPECT_TRUE(key_a3_repeated2
.IsRepeat());
513 KeyEvent
key_a3_released(native_event_a_released
);
514 EXPECT_FALSE(key_a3_released
.IsRepeat());
517 // Hold the key longer than max auto repeat timeout.
519 KeyEvent
key_a4_0(native_event_a_pressed
);
520 EXPECT_FALSE(key_a4_0
.IsRepeat());
522 KeyEvent
key_a4_1500(native_event_a_pressed_1500
);
523 EXPECT_TRUE(key_a4_1500
.IsRepeat());
525 KeyEvent
key_a4_3000(native_event_a_pressed_3000
);
526 EXPECT_TRUE(key_a4_3000
.IsRepeat());
528 KeyEvent
key_a4_released(native_event_a_released
);
529 EXPECT_FALSE(key_a4_released
.IsRepeat());
534 KeyEvent
key_a4_pressed(native_event_a_pressed
);
535 EXPECT_FALSE(key_a4_pressed
.IsRepeat());
537 KeyEvent
key_a4_pressed_nonstandard_state(
538 native_event_a_pressed_nonstandard_state
);
539 EXPECT_FALSE(key_a4_pressed_nonstandard_state
.IsRepeat());
543 #endif // USE_X11 || OS_WIN
545 TEST(EventTest
, TouchEventRadiusDefaultsToOtherAxis
) {
546 const base::TimeDelta time
= base::TimeDelta::FromMilliseconds(0);
547 const float non_zero_length1
= 30;
548 const float non_zero_length2
= 46;
550 TouchEvent
event1(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
551 non_zero_length1
, 0, 0, 0);
552 EXPECT_EQ(non_zero_length1
, event1
.pointer_details().radius_x());
553 EXPECT_EQ(non_zero_length1
, event1
.pointer_details().radius_y());
555 TouchEvent
event2(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
556 0, non_zero_length2
, 0, 0);
557 EXPECT_EQ(non_zero_length2
, event2
.pointer_details().radius_x());
558 EXPECT_EQ(non_zero_length2
, event2
.pointer_details().radius_y());
561 TEST(EventTest
, TouchEventRotationAngleFixing
) {
562 const base::TimeDelta time
= base::TimeDelta::FromMilliseconds(0);
563 const float radius_x
= 20;
564 const float radius_y
= 10;
567 const float angle_in_range
= 0;
568 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
569 radius_x
, radius_y
, angle_in_range
, 0);
570 EXPECT_FLOAT_EQ(angle_in_range
, event
.rotation_angle());
574 const float angle_in_range
= 179.9f
;
575 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
576 radius_x
, radius_y
, angle_in_range
, 0);
577 EXPECT_FLOAT_EQ(angle_in_range
, event
.rotation_angle());
581 const float angle_negative
= -0.1f
;
582 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
583 radius_x
, radius_y
, angle_negative
, 0);
584 EXPECT_FLOAT_EQ(180 - 0.1f
, event
.rotation_angle());
588 const float angle_negative
= -200;
589 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
590 radius_x
, radius_y
, angle_negative
, 0);
591 EXPECT_FLOAT_EQ(360 - 200, event
.rotation_angle());
595 const float angle_too_big
= 180;
596 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
597 radius_x
, radius_y
, angle_too_big
, 0);
598 EXPECT_FLOAT_EQ(0, event
.rotation_angle());
602 const float angle_too_big
= 400;
603 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
604 radius_x
, radius_y
, angle_too_big
, 0);
605 EXPECT_FLOAT_EQ(400 - 360, event
.rotation_angle());
609 TEST(EventTest
, PointerEventDetailsTouch
) {
610 ui::TouchEvent
touch_event_plain(ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0,
611 ui::EventTimeForNow());
613 EXPECT_EQ(EventPointerType::POINTER_TYPE_TOUCH
,
614 touch_event_plain
.pointer_details().pointer_type());
615 EXPECT_EQ(0.0f
, touch_event_plain
.pointer_details().radius_x());
616 EXPECT_EQ(0.0f
, touch_event_plain
.pointer_details().radius_y());
617 EXPECT_EQ(0.0f
, touch_event_plain
.pointer_details().force());
618 EXPECT_EQ(0.0f
, touch_event_plain
.pointer_details().tilt_x());
619 EXPECT_EQ(0.0f
, touch_event_plain
.pointer_details().tilt_y());
621 ui::TouchEvent
touch_event_with_details(ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0,
622 0, ui::EventTimeForNow(), 10.0f
, 5.0f
,
625 EXPECT_EQ(EventPointerType::POINTER_TYPE_TOUCH
,
626 touch_event_with_details
.pointer_details().pointer_type());
627 EXPECT_EQ(10.0f
, touch_event_with_details
.pointer_details().radius_x());
628 EXPECT_EQ(5.0f
, touch_event_with_details
.pointer_details().radius_y());
629 EXPECT_EQ(15.0f
, touch_event_with_details
.pointer_details().force());
630 EXPECT_EQ(0.0f
, touch_event_with_details
.pointer_details().tilt_x());
631 EXPECT_EQ(0.0f
, touch_event_with_details
.pointer_details().tilt_y());
633 ui::TouchEvent
touch_event_copy(touch_event_with_details
);
634 EXPECT_EQ(EventPointerType::POINTER_TYPE_TOUCH
,
635 touch_event_copy
.pointer_details().pointer_type());
636 EXPECT_EQ(10.0f
, touch_event_copy
.pointer_details().radius_x());
637 EXPECT_EQ(5.0f
, touch_event_copy
.pointer_details().radius_y());
638 EXPECT_EQ(15.0f
, touch_event_copy
.pointer_details().force());
639 EXPECT_EQ(0.0f
, touch_event_copy
.pointer_details().tilt_x());
640 EXPECT_EQ(0.0f
, touch_event_copy
.pointer_details().tilt_y());
643 TEST(EventTest
, PointerEventDetailsMouse
) {
644 ui::MouseEvent
mouse_event(ET_MOUSE_PRESSED
, gfx::PointF(0, 0),
645 gfx::PointF(0, 0), ui::EventTimeForNow(), 0, 0);
647 EXPECT_EQ(EventPointerType::POINTER_TYPE_MOUSE
,
648 mouse_event
.pointer_details().pointer_type());
649 EXPECT_EQ(0.0f
, mouse_event
.pointer_details().radius_x());
650 EXPECT_EQ(0.0f
, mouse_event
.pointer_details().radius_y());
651 EXPECT_EQ(0.0f
, mouse_event
.pointer_details().force());
652 EXPECT_EQ(0.0f
, mouse_event
.pointer_details().tilt_x());
653 EXPECT_EQ(0.0f
, mouse_event
.pointer_details().tilt_y());
655 ui::MouseEvent
mouse_event_copy(mouse_event
);
656 EXPECT_EQ(EventPointerType::POINTER_TYPE_MOUSE
,
657 mouse_event_copy
.pointer_details().pointer_type());
658 EXPECT_EQ(0.0f
, mouse_event_copy
.pointer_details().radius_x());
659 EXPECT_EQ(0.0f
, mouse_event_copy
.pointer_details().radius_y());
660 EXPECT_EQ(0.0f
, mouse_event_copy
.pointer_details().force());
661 EXPECT_EQ(0.0f
, mouse_event_copy
.pointer_details().tilt_x());
662 EXPECT_EQ(0.0f
, mouse_event_copy
.pointer_details().tilt_y());
665 TEST(EventTest
, PointerEventDetailsStylus
) {
666 ui::MouseEvent
stylus_event(ET_MOUSE_PRESSED
, gfx::PointF(0, 0),
667 gfx::PointF(0, 0), ui::EventTimeForNow(), 0, 0);
668 ui::PointerDetails
pointer_details(EventPointerType::POINTER_TYPE_PEN
,
673 /* tilt_y */ -45.0f
);
675 stylus_event
.set_pointer_details(pointer_details
);
676 EXPECT_EQ(EventPointerType::POINTER_TYPE_PEN
,
677 stylus_event
.pointer_details().pointer_type());
678 EXPECT_EQ(21.0f
, stylus_event
.pointer_details().force());
679 EXPECT_EQ(45.0f
, stylus_event
.pointer_details().tilt_x());
680 EXPECT_EQ(-45.0f
, stylus_event
.pointer_details().tilt_y());
681 EXPECT_EQ(0.0f
, stylus_event
.pointer_details().radius_x());
682 EXPECT_EQ(0.0f
, stylus_event
.pointer_details().radius_y());
684 ui::MouseEvent
stylus_event_copy(stylus_event
);
685 EXPECT_EQ(EventPointerType::POINTER_TYPE_PEN
,
686 stylus_event_copy
.pointer_details().pointer_type());
687 EXPECT_EQ(21.0f
, stylus_event_copy
.pointer_details().force());
688 EXPECT_EQ(45.0f
, stylus_event_copy
.pointer_details().tilt_x());
689 EXPECT_EQ(-45.0f
, stylus_event_copy
.pointer_details().tilt_y());
690 EXPECT_EQ(0.0f
, stylus_event_copy
.pointer_details().radius_x());
691 EXPECT_EQ(0.0f
, stylus_event_copy
.pointer_details().radius_y());