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/dom3/dom_code.h"
10 #include "ui/events/keycodes/dom4/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(soon
);
87 EXPECT_TRUE(MouseEvent::IsRepeatedClickEvent(mouse_ev1
, mouse_ev2
));
90 test_ev1
.set_location(gfx::Point(0, 0));
91 test_ev2
.set_location(gfx::Point(10, 0));
92 test_ev1
.set_time_stamp(start
);
93 test_ev2
.set_time_stamp(soon
);
94 EXPECT_FALSE(MouseEvent::IsRepeatedClickEvent(mouse_ev1
, mouse_ev2
));
96 // Too long a time between clicks.
97 test_ev1
.set_location(gfx::Point(0, 0));
98 test_ev2
.set_location(gfx::Point(0, 0));
99 test_ev1
.set_time_stamp(start
);
100 test_ev2
.set_time_stamp(later
);
101 EXPECT_FALSE(MouseEvent::IsRepeatedClickEvent(mouse_ev1
, mouse_ev2
));
104 // Tests that an event only increases the click count and gets marked as a
105 // double click if a release event was seen for the previous click. This
106 // prevents the same PRESSED event from being processed twice:
107 // http://crbug.com/389162
108 TEST(EventTest
, DoubleClickRequiresRelease
) {
109 const gfx::Point
origin1(0, 0);
110 const gfx::Point
origin2(100, 0);
111 scoped_ptr
<MouseEvent
> ev
;
112 base::TimeDelta start
= base::TimeDelta::FromMilliseconds(0);
114 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin1
, origin1
, EventTimeForNow(),
116 ev
->set_time_stamp(start
);
117 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
118 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin1
, origin1
, EventTimeForNow(),
120 ev
->set_time_stamp(start
);
121 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
123 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin2
, origin2
, EventTimeForNow(),
125 ev
->set_time_stamp(start
);
126 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
127 ev
.reset(new MouseEvent(ET_MOUSE_RELEASED
, origin2
, origin2
,
128 EventTimeForNow(), 0, 0));
129 ev
->set_time_stamp(start
);
130 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
131 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin2
, origin2
, EventTimeForNow(),
133 ev
->set_time_stamp(start
);
134 EXPECT_EQ(2, MouseEvent::GetRepeatCount(*ev
));
135 ev
.reset(new MouseEvent(ET_MOUSE_RELEASED
, origin2
, origin2
,
136 EventTimeForNow(), 0, 0));
137 ev
->set_time_stamp(start
);
138 EXPECT_EQ(2, MouseEvent::GetRepeatCount(*ev
));
139 MouseEvent::ResetLastClickForTest();
142 // Tests that clicking right and then left clicking does not generate a double
144 TEST(EventTest
, SingleClickRightLeft
) {
145 const gfx::Point
origin(0, 0);
146 scoped_ptr
<MouseEvent
> ev
;
147 base::TimeDelta start
= base::TimeDelta::FromMilliseconds(0);
149 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(),
150 ui::EF_RIGHT_MOUSE_BUTTON
,
151 ui::EF_RIGHT_MOUSE_BUTTON
));
152 ev
->set_time_stamp(start
);
153 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
154 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(),
155 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
156 ev
->set_time_stamp(start
);
157 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
158 ev
.reset(new MouseEvent(ET_MOUSE_RELEASED
, origin
, origin
, EventTimeForNow(),
159 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
160 ev
->set_time_stamp(start
);
161 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev
));
162 ev
.reset(new MouseEvent(ET_MOUSE_PRESSED
, origin
, origin
, EventTimeForNow(),
163 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
164 ev
->set_time_stamp(start
);
165 EXPECT_EQ(2, MouseEvent::GetRepeatCount(*ev
));
166 MouseEvent::ResetLastClickForTest();
169 TEST(EventTest
, KeyEvent
) {
170 static const struct {
171 KeyboardCode key_code
;
176 { VKEY_A
, EF_SHIFT_DOWN
, 'A' },
177 { VKEY_A
, EF_CAPS_LOCK_DOWN
, 'A' },
178 { VKEY_A
, EF_SHIFT_DOWN
| EF_CAPS_LOCK_DOWN
, 'a' },
179 { VKEY_A
, EF_CONTROL_DOWN
, 0x01 },
180 { VKEY_A
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\x01' },
182 { VKEY_Z
, EF_SHIFT_DOWN
, 'Z' },
183 { VKEY_Z
, EF_CAPS_LOCK_DOWN
, 'Z' },
184 { VKEY_Z
, EF_SHIFT_DOWN
| EF_CAPS_LOCK_DOWN
, 'z' },
185 { VKEY_Z
, EF_CONTROL_DOWN
, '\x1A' },
186 { VKEY_Z
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\x1A' },
188 { VKEY_2
, EF_CONTROL_DOWN
, '\0' },
189 { VKEY_2
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
190 { VKEY_6
, EF_CONTROL_DOWN
, '\0' },
191 { VKEY_6
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\x1E' },
192 { VKEY_OEM_MINUS
, EF_CONTROL_DOWN
, '\0' },
193 { VKEY_OEM_MINUS
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\x1F' },
194 { VKEY_OEM_4
, EF_CONTROL_DOWN
, '\x1B' },
195 { VKEY_OEM_4
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
196 { VKEY_OEM_5
, EF_CONTROL_DOWN
, '\x1C' },
197 { VKEY_OEM_5
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
198 { VKEY_OEM_6
, EF_CONTROL_DOWN
, '\x1D' },
199 { VKEY_OEM_6
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
200 { VKEY_RETURN
, EF_CONTROL_DOWN
, '\x0A' },
203 { VKEY_0
, EF_SHIFT_DOWN
, ')' },
204 { VKEY_0
, EF_SHIFT_DOWN
| EF_CAPS_LOCK_DOWN
, ')' },
205 { VKEY_0
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
208 { VKEY_9
, EF_SHIFT_DOWN
, '(' },
209 { VKEY_9
, EF_SHIFT_DOWN
| EF_CAPS_LOCK_DOWN
, '(' },
210 { VKEY_9
, EF_SHIFT_DOWN
| EF_CONTROL_DOWN
, '\0' },
212 { VKEY_NUMPAD0
, EF_CONTROL_DOWN
, '\0' },
213 { VKEY_NUMPAD0
, EF_SHIFT_DOWN
, '0' },
215 { VKEY_NUMPAD9
, EF_CONTROL_DOWN
, '\0' },
216 { VKEY_NUMPAD9
, EF_SHIFT_DOWN
, '9' },
218 { VKEY_TAB
, EF_CONTROL_DOWN
, '\0' },
219 { VKEY_TAB
, EF_SHIFT_DOWN
, '\t' },
221 { VKEY_MULTIPLY
, EF_CONTROL_DOWN
, '\0' },
222 { VKEY_MULTIPLY
, EF_SHIFT_DOWN
, '*' },
223 { VKEY_ADD
, EF_CONTROL_DOWN
, '\0' },
224 { VKEY_ADD
, EF_SHIFT_DOWN
, '+' },
225 { VKEY_SUBTRACT
, EF_CONTROL_DOWN
, '\0' },
226 { VKEY_SUBTRACT
, EF_SHIFT_DOWN
, '-' },
227 { VKEY_DECIMAL
, EF_CONTROL_DOWN
, '\0' },
228 { VKEY_DECIMAL
, EF_SHIFT_DOWN
, '.' },
229 { VKEY_DIVIDE
, EF_CONTROL_DOWN
, '\0' },
230 { VKEY_DIVIDE
, EF_SHIFT_DOWN
, '/' },
232 { VKEY_OEM_1
, EF_CONTROL_DOWN
, '\0' },
233 { VKEY_OEM_1
, EF_SHIFT_DOWN
, ':' },
234 { VKEY_OEM_PLUS
, EF_CONTROL_DOWN
, '\0' },
235 { VKEY_OEM_PLUS
, EF_SHIFT_DOWN
, '+' },
236 { VKEY_OEM_COMMA
, EF_CONTROL_DOWN
, '\0' },
237 { VKEY_OEM_COMMA
, EF_SHIFT_DOWN
, '<' },
238 { VKEY_OEM_PERIOD
, EF_CONTROL_DOWN
, '\0' },
239 { VKEY_OEM_PERIOD
, EF_SHIFT_DOWN
, '>' },
240 { VKEY_OEM_3
, EF_CONTROL_DOWN
, '\0' },
241 { VKEY_OEM_3
, EF_SHIFT_DOWN
, '~' },
244 for (size_t i
= 0; i
< arraysize(kTestData
); ++i
) {
245 KeyEvent
key(ET_KEY_PRESSED
,
246 kTestData
[i
].key_code
,
248 EXPECT_EQ(kTestData
[i
].character
, key
.GetCharacter())
249 << " Index:" << i
<< " key_code:" << kTestData
[i
].key_code
;
253 TEST(EventTest
, KeyEventDirectUnicode
) {
254 KeyEvent
key(0x1234U
, ui::VKEY_UNKNOWN
, ui::EF_NONE
);
255 EXPECT_EQ(0x1234U
, key
.GetCharacter());
256 EXPECT_EQ(ET_KEY_PRESSED
, key
.type());
257 EXPECT_TRUE(key
.is_char());
260 TEST(EventTest
, NormalizeKeyEventFlags
) {
262 // Normalize flags when KeyEvent is created from XEvent.
263 ScopedXI2Event event
;
265 event
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_SHIFT
, EF_SHIFT_DOWN
);
266 KeyEvent
keyev(event
);
267 EXPECT_EQ(EF_SHIFT_DOWN
, keyev
.flags());
270 event
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_SHIFT
, EF_SHIFT_DOWN
);
271 KeyEvent
keyev(event
);
272 EXPECT_EQ(EF_NONE
, keyev
.flags());
275 event
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_CONTROL
, EF_CONTROL_DOWN
);
276 KeyEvent
keyev(event
);
277 EXPECT_EQ(EF_CONTROL_DOWN
, keyev
.flags());
280 event
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_CONTROL
, EF_CONTROL_DOWN
);
281 KeyEvent
keyev(event
);
282 EXPECT_EQ(EF_NONE
, keyev
.flags());
285 event
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_MENU
, EF_ALT_DOWN
);
286 KeyEvent
keyev(event
);
287 EXPECT_EQ(EF_ALT_DOWN
, keyev
.flags());
290 event
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_MENU
, EF_ALT_DOWN
);
291 KeyEvent
keyev(event
);
292 EXPECT_EQ(EF_NONE
, keyev
.flags());
296 // Do not normalize flags for synthesized events without
297 // KeyEvent::NormalizeFlags called explicitly.
299 KeyEvent
keyev(ET_KEY_PRESSED
, VKEY_SHIFT
, EF_SHIFT_DOWN
);
300 EXPECT_EQ(EF_SHIFT_DOWN
, keyev
.flags());
303 KeyEvent
keyev(ET_KEY_RELEASED
, VKEY_SHIFT
, EF_SHIFT_DOWN
);
304 EXPECT_EQ(EF_SHIFT_DOWN
, keyev
.flags());
305 keyev
.NormalizeFlags();
306 EXPECT_EQ(EF_NONE
, keyev
.flags());
309 KeyEvent
keyev(ET_KEY_PRESSED
, VKEY_CONTROL
, EF_CONTROL_DOWN
);
310 EXPECT_EQ(EF_CONTROL_DOWN
, keyev
.flags());
313 KeyEvent
keyev(ET_KEY_RELEASED
, VKEY_CONTROL
, EF_CONTROL_DOWN
);
314 EXPECT_EQ(EF_CONTROL_DOWN
, keyev
.flags());
315 keyev
.NormalizeFlags();
316 EXPECT_EQ(EF_NONE
, keyev
.flags());
319 KeyEvent
keyev(ET_KEY_PRESSED
, VKEY_MENU
, EF_ALT_DOWN
);
320 EXPECT_EQ(EF_ALT_DOWN
, keyev
.flags());
323 KeyEvent
keyev(ET_KEY_RELEASED
, VKEY_MENU
, EF_ALT_DOWN
);
324 EXPECT_EQ(EF_ALT_DOWN
, keyev
.flags());
325 keyev
.NormalizeFlags();
326 EXPECT_EQ(EF_NONE
, keyev
.flags());
330 TEST(EventTest
, KeyEventCopy
) {
331 KeyEvent
key(ET_KEY_PRESSED
, VKEY_A
, EF_NONE
);
332 scoped_ptr
<KeyEvent
> copied_key(new KeyEvent(key
));
333 EXPECT_EQ(copied_key
->type(), key
.type());
334 EXPECT_EQ(copied_key
->key_code(), key
.key_code());
337 TEST(EventTest
, KeyEventCode
) {
338 const DomCode kDomCodeForSpace
= DomCode::SPACE
;
339 const char kCodeForSpace
[] = "Space";
340 ASSERT_EQ(kDomCodeForSpace
,
341 ui::KeycodeConverter::CodeStringToDomCode(kCodeForSpace
));
342 const uint16 kNativeCodeSpace
=
343 ui::KeycodeConverter::CodeToNativeKeycode(kCodeForSpace
);
344 ASSERT_NE(ui::KeycodeConverter::InvalidNativeKeycode(), kNativeCodeSpace
);
345 ASSERT_EQ(kNativeCodeSpace
,
346 ui::KeycodeConverter::DomCodeToNativeKeycode(kDomCodeForSpace
));
349 KeyEvent
key(ET_KEY_PRESSED
, VKEY_SPACE
, kDomCodeForSpace
, EF_NONE
);
350 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
353 // Regardless the KeyEvent.key_code (VKEY_RETURN), code should be
354 // the specified value.
355 KeyEvent
key(ET_KEY_PRESSED
, VKEY_RETURN
, kDomCodeForSpace
, EF_NONE
);
356 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
359 // If the synthetic event is initialized without code, it returns
361 // TODO(komatsu): Fill a fallback value assuming the US keyboard layout.
362 KeyEvent
key(ET_KEY_PRESSED
, VKEY_SPACE
, EF_NONE
);
363 EXPECT_TRUE(key
.GetCodeString().empty());
367 // KeyEvent converts from the native keycode (XKB) to the code.
368 ScopedXI2Event xevent
;
369 xevent
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_SPACE
, kNativeCodeSpace
);
370 KeyEvent
key(xevent
);
371 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
376 // Test a non extended key.
377 ASSERT_EQ((kNativeCodeSpace
& 0xFF), kNativeCodeSpace
);
379 const LPARAM lParam
= GetLParamFromScanCode(kNativeCodeSpace
);
380 MSG native_event
= { NULL
, WM_KEYUP
, VKEY_SPACE
, lParam
};
381 KeyEvent
key(native_event
);
383 // KeyEvent converts from the native keycode (scan code) to the code.
384 EXPECT_EQ(kCodeForSpace
, key
.GetCodeString());
387 const char kCodeForHome
[] = "Home";
388 const uint16 kNativeCodeHome
= 0xe047;
390 // 'Home' is an extended key with 0xe000 bits.
391 ASSERT_NE((kNativeCodeHome
& 0xFF), kNativeCodeHome
);
392 const LPARAM lParam
= GetLParamFromScanCode(kNativeCodeHome
);
394 MSG native_event
= { NULL
, WM_KEYUP
, VKEY_HOME
, lParam
};
395 KeyEvent
key(native_event
);
397 // KeyEvent converts from the native keycode (scan code) to the code.
398 EXPECT_EQ(kCodeForHome
, key
.GetCodeString());
405 void SetKeyEventTimestamp(XEvent
* event
, long time
) {
406 event
->xkey
.time
= time
;
409 void AdvanceKeyEventTimestamp(XEvent
* event
) {
413 #elif defined(OS_WIN)
414 void SetKeyEventTimestamp(MSG
& msg
, long time
) {
418 void AdvanceKeyEventTimestamp(MSG
& msg
) {
424 #if defined(USE_X11) || defined(OS_WIN)
425 TEST(EventTest
, AutoRepeat
) {
426 const uint16 kNativeCodeA
= ui::KeycodeConverter::CodeToNativeKeycode("KeyA");
427 const uint16 kNativeCodeB
= ui::KeycodeConverter::CodeToNativeKeycode("KeyB");
429 ScopedXI2Event native_event_a_pressed
;
430 native_event_a_pressed
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
431 ScopedXI2Event native_event_a_pressed_1500
;
432 native_event_a_pressed_1500
.InitKeyEvent(
433 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
434 ScopedXI2Event native_event_a_pressed_3000
;
435 native_event_a_pressed_3000
.InitKeyEvent(
436 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
438 ScopedXI2Event native_event_a_released
;
439 native_event_a_released
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_A
, kNativeCodeA
);
440 ScopedXI2Event native_event_b_pressed
;
441 native_event_b_pressed
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_B
, kNativeCodeB
);
442 ScopedXI2Event native_event_a_pressed_nonstandard_state
;
443 native_event_a_pressed_nonstandard_state
.InitKeyEvent(
444 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
445 // IBUS-GTK uses the mask (1 << 25) to detect reposted event.
446 static_cast<XEvent
*>(native_event_a_pressed_nonstandard_state
)->xkey
.state
|=
448 #elif defined(OS_WIN)
449 const LPARAM lParam_a
= GetLParamFromScanCode(kNativeCodeA
);
450 const LPARAM lParam_b
= GetLParamFromScanCode(kNativeCodeB
);
451 MSG native_event_a_pressed
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
452 MSG native_event_a_pressed_1500
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
453 MSG native_event_a_pressed_3000
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
454 MSG native_event_a_released
= { NULL
, WM_KEYUP
, VKEY_A
, lParam_a
};
455 MSG native_event_b_pressed
= { NULL
, WM_KEYUP
, VKEY_B
, lParam_b
};
457 SetKeyEventTimestamp(native_event_a_pressed_1500
, 1500);
458 SetKeyEventTimestamp(native_event_a_pressed_3000
, 3000);
461 KeyEvent
key_a1(native_event_a_pressed
);
462 EXPECT_FALSE(key_a1
.IsRepeat());
464 KeyEvent
key_a1_with_same_event(native_event_a_pressed
);
465 EXPECT_FALSE(key_a1_with_same_event
.IsRepeat());
467 KeyEvent
key_a1_released(native_event_a_released
);
468 EXPECT_FALSE(key_a1_released
.IsRepeat());
470 KeyEvent
key_a2(native_event_a_pressed
);
471 EXPECT_FALSE(key_a2
.IsRepeat());
473 AdvanceKeyEventTimestamp(native_event_a_pressed
);
474 KeyEvent
key_a2_repeated(native_event_a_pressed
);
475 EXPECT_TRUE(key_a2_repeated
.IsRepeat());
477 KeyEvent
key_a2_released(native_event_a_released
);
478 EXPECT_FALSE(key_a2_released
.IsRepeat());
481 // Interleaved with different key press.
483 KeyEvent
key_a3(native_event_a_pressed
);
484 EXPECT_FALSE(key_a3
.IsRepeat());
486 KeyEvent
key_b(native_event_b_pressed
);
487 EXPECT_FALSE(key_b
.IsRepeat());
489 AdvanceKeyEventTimestamp(native_event_a_pressed
);
490 KeyEvent
key_a3_again(native_event_a_pressed
);
491 EXPECT_FALSE(key_a3_again
.IsRepeat());
493 AdvanceKeyEventTimestamp(native_event_a_pressed
);
494 KeyEvent
key_a3_repeated(native_event_a_pressed
);
495 EXPECT_TRUE(key_a3_repeated
.IsRepeat());
497 AdvanceKeyEventTimestamp(native_event_a_pressed
);
498 KeyEvent
key_a3_repeated2(native_event_a_pressed
);
499 EXPECT_TRUE(key_a3_repeated2
.IsRepeat());
501 KeyEvent
key_a3_released(native_event_a_released
);
502 EXPECT_FALSE(key_a3_released
.IsRepeat());
505 // Hold the key longer than max auto repeat timeout.
507 KeyEvent
key_a4_0(native_event_a_pressed
);
508 EXPECT_FALSE(key_a4_0
.IsRepeat());
510 KeyEvent
key_a4_1500(native_event_a_pressed_1500
);
511 EXPECT_TRUE(key_a4_1500
.IsRepeat());
513 KeyEvent
key_a4_3000(native_event_a_pressed_3000
);
514 EXPECT_TRUE(key_a4_3000
.IsRepeat());
516 KeyEvent
key_a4_released(native_event_a_released
);
517 EXPECT_FALSE(key_a4_released
.IsRepeat());
522 KeyEvent
key_a4_pressed(native_event_a_pressed
);
523 EXPECT_FALSE(key_a4_pressed
.IsRepeat());
525 KeyEvent
key_a4_pressed_nonstandard_state(
526 native_event_a_pressed_nonstandard_state
);
527 EXPECT_FALSE(key_a4_pressed_nonstandard_state
.IsRepeat());
531 #endif // USE_X11 || OS_WIN
533 TEST(EventTest
, TouchEventRadiusDefaultsToOtherAxis
) {
534 const base::TimeDelta time
= base::TimeDelta::FromMilliseconds(0);
535 const float non_zero_length1
= 30;
536 const float non_zero_length2
= 46;
538 TouchEvent
event1(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
539 non_zero_length1
, 0, 0, 0);
540 EXPECT_EQ(non_zero_length1
, event1
.radius_x());
541 EXPECT_EQ(non_zero_length1
, event1
.radius_y());
543 TouchEvent
event2(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
544 0, non_zero_length2
, 0, 0);
545 EXPECT_EQ(non_zero_length2
, event2
.radius_x());
546 EXPECT_EQ(non_zero_length2
, event2
.radius_y());
549 TEST(EventTest
, TouchEventRotationAngleFixing
) {
550 const base::TimeDelta time
= base::TimeDelta::FromMilliseconds(0);
551 const float radius_x
= 20;
552 const float radius_y
= 10;
555 const float angle_in_range
= 0;
556 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
557 radius_x
, radius_y
, angle_in_range
, 0);
558 EXPECT_FLOAT_EQ(angle_in_range
, event
.rotation_angle());
562 const float angle_in_range
= 179.9f
;
563 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
564 radius_x
, radius_y
, angle_in_range
, 0);
565 EXPECT_FLOAT_EQ(angle_in_range
, event
.rotation_angle());
569 const float angle_negative
= -0.1f
;
570 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
571 radius_x
, radius_y
, angle_negative
, 0);
572 EXPECT_FLOAT_EQ(180 - 0.1f
, event
.rotation_angle());
576 const float angle_negative
= -200;
577 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
578 radius_x
, radius_y
, angle_negative
, 0);
579 EXPECT_FLOAT_EQ(360 - 200, event
.rotation_angle());
583 const float angle_too_big
= 180;
584 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
585 radius_x
, radius_y
, angle_too_big
, 0);
586 EXPECT_FLOAT_EQ(0, event
.rotation_angle());
590 const float angle_too_big
= 400;
591 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
592 radius_x
, radius_y
, angle_too_big
, 0);
593 EXPECT_FLOAT_EQ(400 - 360, event
.rotation_angle());