Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / events / event_unittest.cc
blob1297f3095b60f153b0fb08d9a5d8f01012e4c6f1
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"
13 #if defined(USE_X11)
14 #include <X11/Xlib.h>
15 #include "ui/events/test/events_test_utils_x11.h"
16 #include "ui/gfx/x/x11_types.h"
17 #endif
19 namespace ui {
21 TEST(EventTest, NoNativeEvent) {
22 KeyEvent keyev(ET_KEY_PRESSED, VKEY_SPACE, EF_NONE);
23 EXPECT_FALSE(keyev.HasNativeEvent());
26 TEST(EventTest, NativeEvent) {
27 #if defined(OS_WIN)
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)
32 ScopedXI2Event event;
33 event.InitKeyEvent(ET_KEY_RELEASED, VKEY_A, EF_NONE);
34 KeyEvent keyev(event);
35 EXPECT_TRUE(keyev.HasNativeEvent());
36 #endif
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());
47 #if defined(USE_X11)
48 // For X11, test the functions with native_event() as well. crbug.com/107837
49 ScopedXI2Event event;
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());
57 #endif
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,
72 0);
73 MouseEvent mouse_ev2(ET_MOUSE_PRESSED, origin, origin, EventTimeForNow(), 0,
74 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);
82 // Same event.
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));
91 // Close point.
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));
98 // Too far.
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(),
125 0, 0));
126 ev->set_time_stamp(start);
127 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev));
128 ev.reset(new MouseEvent(ET_MOUSE_PRESSED, origin1, origin1, EventTimeForNow(),
129 0, 0));
130 ev->set_time_stamp(start);
131 EXPECT_EQ(1, MouseEvent::GetRepeatCount(*ev));
133 ev.reset(new MouseEvent(ET_MOUSE_PRESSED, origin2, origin2, EventTimeForNow(),
134 0, 0));
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(),
142 0, 0));
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
153 // click.
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;
183 int flags;
184 uint16 character;
185 } kTestData[] = {
186 { VKEY_A, 0, 'a' },
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' },
192 { VKEY_Z, 0, 'z' },
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' },
213 { VKEY_0, 0, '0' },
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' },
218 { VKEY_9, 0, '9' },
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,
258 kTestData[i].flags);
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) {
272 #if defined(USE_X11)
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());
305 #endif
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, it returns
371 // an empty string.
372 // TODO(komatsu): Fill a fallback value assuming the US keyboard layout.
373 KeyEvent key(ET_KEY_PRESSED, VKEY_SPACE, EF_NONE);
374 EXPECT_TRUE(key.GetCodeString().empty());
376 #if defined(USE_X11)
378 // KeyEvent converts from the native keycode (XKB) to the code.
379 ScopedXI2Event xevent;
380 xevent.InitKeyEvent(ET_KEY_PRESSED, VKEY_SPACE, kNativeCodeSpace);
381 KeyEvent key(xevent);
382 EXPECT_EQ(kCodeForSpace, key.GetCodeString());
384 #endif // USE_X11
385 #if defined(OS_WIN)
387 // Test a non extended key.
388 ASSERT_EQ((kNativeCodeSpace & 0xFF), kNativeCodeSpace);
390 const LPARAM lParam = GetLParamFromScanCode(kNativeCodeSpace);
391 MSG native_event = { NULL, WM_KEYUP, VKEY_SPACE, lParam };
392 KeyEvent key(native_event);
394 // KeyEvent converts from the native keycode (scan code) to the code.
395 EXPECT_EQ(kCodeForSpace, key.GetCodeString());
398 const char kCodeForHome[] = "Home";
399 const uint16 kNativeCodeHome = 0xe047;
401 // 'Home' is an extended key with 0xe000 bits.
402 ASSERT_NE((kNativeCodeHome & 0xFF), kNativeCodeHome);
403 const LPARAM lParam = GetLParamFromScanCode(kNativeCodeHome);
405 MSG native_event = { NULL, WM_KEYUP, VKEY_HOME, lParam };
406 KeyEvent key(native_event);
408 // KeyEvent converts from the native keycode (scan code) to the code.
409 EXPECT_EQ(kCodeForHome, key.GetCodeString());
411 #endif // OS_WIN
414 namespace {
415 #if defined(USE_X11)
416 void SetKeyEventTimestamp(XEvent* event, long time) {
417 event->xkey.time = time;
420 void AdvanceKeyEventTimestamp(XEvent* event) {
421 event->xkey.time++;
424 #elif defined(OS_WIN)
425 void SetKeyEventTimestamp(MSG& msg, long time) {
426 msg.time = time;
429 void AdvanceKeyEventTimestamp(MSG& msg) {
430 msg.time++;
432 #endif
433 } // namespace
435 #if defined(USE_X11) || defined(OS_WIN)
436 TEST(EventTest, AutoRepeat) {
437 const uint16 kNativeCodeA = ui::KeycodeConverter::CodeToNativeKeycode("KeyA");
438 const uint16 kNativeCodeB = ui::KeycodeConverter::CodeToNativeKeycode("KeyB");
439 #if defined(USE_X11)
440 ScopedXI2Event native_event_a_pressed;
441 native_event_a_pressed.InitKeyEvent(ET_KEY_PRESSED, VKEY_A, kNativeCodeA);
442 ScopedXI2Event native_event_a_pressed_1500;
443 native_event_a_pressed_1500.InitKeyEvent(
444 ET_KEY_PRESSED, VKEY_A, kNativeCodeA);
445 ScopedXI2Event native_event_a_pressed_3000;
446 native_event_a_pressed_3000.InitKeyEvent(
447 ET_KEY_PRESSED, VKEY_A, kNativeCodeA);
449 ScopedXI2Event native_event_a_released;
450 native_event_a_released.InitKeyEvent(ET_KEY_RELEASED, VKEY_A, kNativeCodeA);
451 ScopedXI2Event native_event_b_pressed;
452 native_event_b_pressed.InitKeyEvent(ET_KEY_PRESSED, VKEY_B, kNativeCodeB);
453 ScopedXI2Event native_event_a_pressed_nonstandard_state;
454 native_event_a_pressed_nonstandard_state.InitKeyEvent(
455 ET_KEY_PRESSED, VKEY_A, kNativeCodeA);
456 // IBUS-GTK uses the mask (1 << 25) to detect reposted event.
457 static_cast<XEvent*>(native_event_a_pressed_nonstandard_state)->xkey.state |=
458 1 << 25;
459 #elif defined(OS_WIN)
460 const LPARAM lParam_a = GetLParamFromScanCode(kNativeCodeA);
461 const LPARAM lParam_b = GetLParamFromScanCode(kNativeCodeB);
462 MSG native_event_a_pressed = { NULL, WM_KEYDOWN, VKEY_A, lParam_a };
463 MSG native_event_a_pressed_1500 = { NULL, WM_KEYDOWN, VKEY_A, lParam_a };
464 MSG native_event_a_pressed_3000 = { NULL, WM_KEYDOWN, VKEY_A, lParam_a };
465 MSG native_event_a_released = { NULL, WM_KEYUP, VKEY_A, lParam_a };
466 MSG native_event_b_pressed = { NULL, WM_KEYUP, VKEY_B, lParam_b };
467 #endif
468 SetKeyEventTimestamp(native_event_a_pressed_1500, 1500);
469 SetKeyEventTimestamp(native_event_a_pressed_3000, 3000);
472 KeyEvent key_a1(native_event_a_pressed);
473 EXPECT_FALSE(key_a1.IsRepeat());
475 KeyEvent key_a1_with_same_event(native_event_a_pressed);
476 EXPECT_FALSE(key_a1_with_same_event.IsRepeat());
478 KeyEvent key_a1_released(native_event_a_released);
479 EXPECT_FALSE(key_a1_released.IsRepeat());
481 KeyEvent key_a2(native_event_a_pressed);
482 EXPECT_FALSE(key_a2.IsRepeat());
484 AdvanceKeyEventTimestamp(native_event_a_pressed);
485 KeyEvent key_a2_repeated(native_event_a_pressed);
486 EXPECT_TRUE(key_a2_repeated.IsRepeat());
488 KeyEvent key_a2_released(native_event_a_released);
489 EXPECT_FALSE(key_a2_released.IsRepeat());
492 // Interleaved with different key press.
494 KeyEvent key_a3(native_event_a_pressed);
495 EXPECT_FALSE(key_a3.IsRepeat());
497 KeyEvent key_b(native_event_b_pressed);
498 EXPECT_FALSE(key_b.IsRepeat());
500 AdvanceKeyEventTimestamp(native_event_a_pressed);
501 KeyEvent key_a3_again(native_event_a_pressed);
502 EXPECT_FALSE(key_a3_again.IsRepeat());
504 AdvanceKeyEventTimestamp(native_event_a_pressed);
505 KeyEvent key_a3_repeated(native_event_a_pressed);
506 EXPECT_TRUE(key_a3_repeated.IsRepeat());
508 AdvanceKeyEventTimestamp(native_event_a_pressed);
509 KeyEvent key_a3_repeated2(native_event_a_pressed);
510 EXPECT_TRUE(key_a3_repeated2.IsRepeat());
512 KeyEvent key_a3_released(native_event_a_released);
513 EXPECT_FALSE(key_a3_released.IsRepeat());
516 // Hold the key longer than max auto repeat timeout.
518 KeyEvent key_a4_0(native_event_a_pressed);
519 EXPECT_FALSE(key_a4_0.IsRepeat());
521 KeyEvent key_a4_1500(native_event_a_pressed_1500);
522 EXPECT_TRUE(key_a4_1500.IsRepeat());
524 KeyEvent key_a4_3000(native_event_a_pressed_3000);
525 EXPECT_TRUE(key_a4_3000.IsRepeat());
527 KeyEvent key_a4_released(native_event_a_released);
528 EXPECT_FALSE(key_a4_released.IsRepeat());
531 #if defined(USE_X11)
533 KeyEvent key_a4_pressed(native_event_a_pressed);
534 EXPECT_FALSE(key_a4_pressed.IsRepeat());
536 KeyEvent key_a4_pressed_nonstandard_state(
537 native_event_a_pressed_nonstandard_state);
538 EXPECT_FALSE(key_a4_pressed_nonstandard_state.IsRepeat());
540 #endif
542 #endif // USE_X11 || OS_WIN
544 TEST(EventTest, TouchEventRadiusDefaultsToOtherAxis) {
545 const base::TimeDelta time = base::TimeDelta::FromMilliseconds(0);
546 const float non_zero_length1 = 30;
547 const float non_zero_length2 = 46;
549 TouchEvent event1(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, 0, time,
550 non_zero_length1, 0, 0, 0);
551 EXPECT_EQ(non_zero_length1, event1.radius_x());
552 EXPECT_EQ(non_zero_length1, event1.radius_y());
554 TouchEvent event2(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, 0, time,
555 0, non_zero_length2, 0, 0);
556 EXPECT_EQ(non_zero_length2, event2.radius_x());
557 EXPECT_EQ(non_zero_length2, event2.radius_y());
560 TEST(EventTest, TouchEventRotationAngleFixing) {
561 const base::TimeDelta time = base::TimeDelta::FromMilliseconds(0);
562 const float radius_x = 20;
563 const float radius_y = 10;
566 const float angle_in_range = 0;
567 TouchEvent event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, 0, time,
568 radius_x, radius_y, angle_in_range, 0);
569 EXPECT_FLOAT_EQ(angle_in_range, event.rotation_angle());
573 const float angle_in_range = 179.9f;
574 TouchEvent event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, 0, time,
575 radius_x, radius_y, angle_in_range, 0);
576 EXPECT_FLOAT_EQ(angle_in_range, event.rotation_angle());
580 const float angle_negative = -0.1f;
581 TouchEvent event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, 0, time,
582 radius_x, radius_y, angle_negative, 0);
583 EXPECT_FLOAT_EQ(180 - 0.1f, event.rotation_angle());
587 const float angle_negative = -200;
588 TouchEvent event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, 0, time,
589 radius_x, radius_y, angle_negative, 0);
590 EXPECT_FLOAT_EQ(360 - 200, event.rotation_angle());
594 const float angle_too_big = 180;
595 TouchEvent event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, 0, time,
596 radius_x, radius_y, angle_too_big, 0);
597 EXPECT_FLOAT_EQ(0, event.rotation_angle());
601 const float angle_too_big = 400;
602 TouchEvent event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), 0, 0, time,
603 radius_x, radius_y, angle_too_big, 0);
604 EXPECT_FLOAT_EQ(400 - 360, event.rotation_angle());
608 } // namespace ui