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(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::CodeToNativeKeycode(kCodeForSpace
);
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
= ui::KeycodeConverter::CodeToNativeKeycode("KeyA");
437 const uint16 kNativeCodeB
= ui::KeycodeConverter::CodeToNativeKeycode("KeyB");
439 ScopedXI2Event native_event_a_pressed
;
440 native_event_a_pressed
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
441 ScopedXI2Event native_event_a_pressed_1500
;
442 native_event_a_pressed_1500
.InitKeyEvent(
443 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
444 ScopedXI2Event native_event_a_pressed_3000
;
445 native_event_a_pressed_3000
.InitKeyEvent(
446 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
448 ScopedXI2Event native_event_a_released
;
449 native_event_a_released
.InitKeyEvent(ET_KEY_RELEASED
, VKEY_A
, kNativeCodeA
);
450 ScopedXI2Event native_event_b_pressed
;
451 native_event_b_pressed
.InitKeyEvent(ET_KEY_PRESSED
, VKEY_B
, kNativeCodeB
);
452 ScopedXI2Event native_event_a_pressed_nonstandard_state
;
453 native_event_a_pressed_nonstandard_state
.InitKeyEvent(
454 ET_KEY_PRESSED
, VKEY_A
, kNativeCodeA
);
455 // IBUS-GTK uses the mask (1 << 25) to detect reposted event.
456 static_cast<XEvent
*>(native_event_a_pressed_nonstandard_state
)->xkey
.state
|=
458 #elif defined(OS_WIN)
459 const LPARAM lParam_a
= GetLParamFromScanCode(kNativeCodeA
);
460 const LPARAM lParam_b
= GetLParamFromScanCode(kNativeCodeB
);
461 MSG native_event_a_pressed
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
462 MSG native_event_a_pressed_1500
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
463 MSG native_event_a_pressed_3000
= { NULL
, WM_KEYDOWN
, VKEY_A
, lParam_a
};
464 MSG native_event_a_released
= { NULL
, WM_KEYUP
, VKEY_A
, lParam_a
};
465 MSG native_event_b_pressed
= { NULL
, WM_KEYUP
, VKEY_B
, lParam_b
};
467 SetKeyEventTimestamp(native_event_a_pressed_1500
, 1500);
468 SetKeyEventTimestamp(native_event_a_pressed_3000
, 3000);
471 KeyEvent
key_a1(native_event_a_pressed
);
472 EXPECT_FALSE(key_a1
.IsRepeat());
474 KeyEvent
key_a1_with_same_event(native_event_a_pressed
);
475 EXPECT_FALSE(key_a1_with_same_event
.IsRepeat());
477 KeyEvent
key_a1_released(native_event_a_released
);
478 EXPECT_FALSE(key_a1_released
.IsRepeat());
480 KeyEvent
key_a2(native_event_a_pressed
);
481 EXPECT_FALSE(key_a2
.IsRepeat());
483 AdvanceKeyEventTimestamp(native_event_a_pressed
);
484 KeyEvent
key_a2_repeated(native_event_a_pressed
);
485 EXPECT_TRUE(key_a2_repeated
.IsRepeat());
487 KeyEvent
key_a2_released(native_event_a_released
);
488 EXPECT_FALSE(key_a2_released
.IsRepeat());
491 // Interleaved with different key press.
493 KeyEvent
key_a3(native_event_a_pressed
);
494 EXPECT_FALSE(key_a3
.IsRepeat());
496 KeyEvent
key_b(native_event_b_pressed
);
497 EXPECT_FALSE(key_b
.IsRepeat());
499 AdvanceKeyEventTimestamp(native_event_a_pressed
);
500 KeyEvent
key_a3_again(native_event_a_pressed
);
501 EXPECT_FALSE(key_a3_again
.IsRepeat());
503 AdvanceKeyEventTimestamp(native_event_a_pressed
);
504 KeyEvent
key_a3_repeated(native_event_a_pressed
);
505 EXPECT_TRUE(key_a3_repeated
.IsRepeat());
507 AdvanceKeyEventTimestamp(native_event_a_pressed
);
508 KeyEvent
key_a3_repeated2(native_event_a_pressed
);
509 EXPECT_TRUE(key_a3_repeated2
.IsRepeat());
511 KeyEvent
key_a3_released(native_event_a_released
);
512 EXPECT_FALSE(key_a3_released
.IsRepeat());
515 // Hold the key longer than max auto repeat timeout.
517 KeyEvent
key_a4_0(native_event_a_pressed
);
518 EXPECT_FALSE(key_a4_0
.IsRepeat());
520 KeyEvent
key_a4_1500(native_event_a_pressed_1500
);
521 EXPECT_TRUE(key_a4_1500
.IsRepeat());
523 KeyEvent
key_a4_3000(native_event_a_pressed_3000
);
524 EXPECT_TRUE(key_a4_3000
.IsRepeat());
526 KeyEvent
key_a4_released(native_event_a_released
);
527 EXPECT_FALSE(key_a4_released
.IsRepeat());
532 KeyEvent
key_a4_pressed(native_event_a_pressed
);
533 EXPECT_FALSE(key_a4_pressed
.IsRepeat());
535 KeyEvent
key_a4_pressed_nonstandard_state(
536 native_event_a_pressed_nonstandard_state
);
537 EXPECT_FALSE(key_a4_pressed_nonstandard_state
.IsRepeat());
541 #endif // USE_X11 || OS_WIN
543 TEST(EventTest
, TouchEventRadiusDefaultsToOtherAxis
) {
544 const base::TimeDelta time
= base::TimeDelta::FromMilliseconds(0);
545 const float non_zero_length1
= 30;
546 const float non_zero_length2
= 46;
548 TouchEvent
event1(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
549 non_zero_length1
, 0, 0, 0);
550 EXPECT_EQ(non_zero_length1
, event1
.radius_x());
551 EXPECT_EQ(non_zero_length1
, event1
.radius_y());
553 TouchEvent
event2(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
554 0, non_zero_length2
, 0, 0);
555 EXPECT_EQ(non_zero_length2
, event2
.radius_x());
556 EXPECT_EQ(non_zero_length2
, event2
.radius_y());
559 TEST(EventTest
, TouchEventRotationAngleFixing
) {
560 const base::TimeDelta time
= base::TimeDelta::FromMilliseconds(0);
561 const float radius_x
= 20;
562 const float radius_y
= 10;
565 const float angle_in_range
= 0;
566 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
567 radius_x
, radius_y
, angle_in_range
, 0);
568 EXPECT_FLOAT_EQ(angle_in_range
, event
.rotation_angle());
572 const float angle_in_range
= 179.9f
;
573 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
574 radius_x
, radius_y
, angle_in_range
, 0);
575 EXPECT_FLOAT_EQ(angle_in_range
, event
.rotation_angle());
579 const float angle_negative
= -0.1f
;
580 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
581 radius_x
, radius_y
, angle_negative
, 0);
582 EXPECT_FLOAT_EQ(180 - 0.1f
, event
.rotation_angle());
586 const float angle_negative
= -200;
587 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
588 radius_x
, radius_y
, angle_negative
, 0);
589 EXPECT_FLOAT_EQ(360 - 200, event
.rotation_angle());
593 const float angle_too_big
= 180;
594 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
595 radius_x
, radius_y
, angle_too_big
, 0);
596 EXPECT_FLOAT_EQ(0, event
.rotation_angle());
600 const float angle_too_big
= 400;
601 TouchEvent
event(ui::ET_TOUCH_PRESSED
, gfx::Point(0, 0), 0, 0, time
,
602 radius_x
, radius_y
, angle_too_big
, 0);
603 EXPECT_FLOAT_EQ(400 - 360, event
.rotation_angle());