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 "content/browser/renderer_host/web_input_event_aura.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/events/event.h"
10 #include "ui/events/event_utils.h"
11 #include "ui/events/keycodes/dom3/dom_code.h"
12 #include "ui/events/keycodes/dom3/dom_key.h"
13 #include "ui/events/keycodes/dom4/keycode_converter.h"
16 #include <X11/keysym.h>
18 #include "ui/events/test/events_test_utils_x11.h"
19 #include "ui/gfx/x/x11_types.h"
24 // Checks that MakeWebKeyboardEvent makes a DOM3 spec compliant key event.
26 TEST(WebInputEventAuraTest
, TestMakeWebKeyboardEvent
) {
28 ui::ScopedXI2Event xev
;
31 xev
.InitKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
, 0);
32 ui::KeyEvent
event(xev
);
33 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
34 // However, modifier bit for Control in |webkit_event| should be set.
35 EXPECT_EQ(webkit_event
.modifiers
, blink::WebInputEvent::ControlKey
);
39 xev
.InitKeyEvent(ui::ET_KEY_RELEASED
, ui::VKEY_CONTROL
, ControlMask
);
40 ui::KeyEvent
event(xev
);
41 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
42 // However, modifier bit for Control in |webkit_event| shouldn't be set.
43 EXPECT_EQ(webkit_event
.modifiers
, 0);
48 // Checks that MakeWebKeyboardEvent returns a correct windowsKeyCode.
49 #if defined(OS_CHROMEOS) || defined(THREAD_SANITIZER)
50 // Fails on Chrome OS and under ThreadSanitizer on Linux, see
51 // https://crbug.com/449103.
52 #define MAYBE_TestMakeWebKeyboardEventWindowsKeyCode \
53 DISABLED_TestMakeWebKeyboardEventWindowsKeyCode
55 #define MAYBE_TestMakeWebKeyboardEventWindowsKeyCode \
56 TestMakeWebKeyboardEventWindowsKeyCode
58 TEST(WebInputEventAuraTest
, MAYBE_TestMakeWebKeyboardEventWindowsKeyCode
) {
60 ui::ScopedXI2Event xev
;
63 xev
.InitKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
, 0);
65 xevent
->xkey
.keycode
= ui::KeycodeConverter::DomCodeToNativeKeycode(
66 ui::DomCode::CONTROL_LEFT
);
67 ui::KeyEvent
event(xev
);
68 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
69 // ui::VKEY_LCONTROL, instead of ui::VKEY_CONTROL, should be filled.
70 EXPECT_EQ(ui::VKEY_LCONTROL
, webkit_event
.windowsKeyCode
);
74 xev
.InitKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
, 0);
76 xevent
->xkey
.keycode
= ui::KeycodeConverter::DomCodeToNativeKeycode(
77 ui::DomCode::CONTROL_RIGHT
);
78 ui::KeyEvent
event(xev
);
79 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
80 // ui::VKEY_RCONTROL, instead of ui::VKEY_CONTROL, should be filled.
81 EXPECT_EQ(ui::VKEY_RCONTROL
, webkit_event
.windowsKeyCode
);
84 // TODO(yusukes): Add tests for win_aura once keyboardEvent() in
85 // third_party/WebKit/Source/web/win/WebInputEventFactory.cpp is modified
86 // to return VKEY_[LR]XXX instead of VKEY_XXX.
87 // https://bugs.webkit.org/show_bug.cgi?id=86694
91 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
,
92 ui::DomCode::CONTROL_LEFT
, ui::EF_CONTROL_DOWN
,
93 ui::DomKey::CONTROL
, 0, ui::EventTimeForNow());
94 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
95 // ui::VKEY_LCONTROL, instead of ui::VKEY_CONTROL, should be filled.
96 EXPECT_EQ(ui::VKEY_LCONTROL
, webkit_event
.windowsKeyCode
);
100 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
,
101 ui::DomCode::CONTROL_RIGHT
, ui::EF_CONTROL_DOWN
,
102 ui::DomKey::CONTROL
, 0, ui::EventTimeForNow());
103 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
104 // ui::VKEY_RCONTROL, instead of ui::VKEY_CONTROL, should be filled.
105 EXPECT_EQ(ui::VKEY_RCONTROL
, webkit_event
.windowsKeyCode
);
109 // Checks that MakeWebKeyboardEvent fills a correct keypard modifier.
110 TEST(WebInputEventAuraTest
, TestMakeWebKeyboardEventKeyPadKeyCode
) {
113 ui::KeyboardCode ui_keycode
; // The virtual key code.
114 uint32 x_keycode
; // The platform key code.
115 bool expected_result
; // true if the event has "isKeyPad" modifier.
117 {ui::VKEY_0
, XK_0
, false},
118 {ui::VKEY_1
, XK_1
, false},
119 {ui::VKEY_2
, XK_2
, false},
120 {ui::VKEY_3
, XK_3
, false},
121 {ui::VKEY_4
, XK_4
, false},
122 {ui::VKEY_5
, XK_5
, false},
123 {ui::VKEY_6
, XK_6
, false},
124 {ui::VKEY_7
, XK_7
, false},
125 {ui::VKEY_8
, XK_8
, false},
126 {ui::VKEY_9
, XK_9
, false},
128 {ui::VKEY_NUMPAD0
, XK_KP_0
, true},
129 {ui::VKEY_NUMPAD1
, XK_KP_1
, true},
130 {ui::VKEY_NUMPAD2
, XK_KP_2
, true},
131 {ui::VKEY_NUMPAD3
, XK_KP_3
, true},
132 {ui::VKEY_NUMPAD4
, XK_KP_4
, true},
133 {ui::VKEY_NUMPAD5
, XK_KP_5
, true},
134 {ui::VKEY_NUMPAD6
, XK_KP_6
, true},
135 {ui::VKEY_NUMPAD7
, XK_KP_7
, true},
136 {ui::VKEY_NUMPAD8
, XK_KP_8
, true},
137 {ui::VKEY_NUMPAD9
, XK_KP_9
, true},
139 {ui::VKEY_MULTIPLY
, XK_KP_Multiply
, true},
140 {ui::VKEY_SUBTRACT
, XK_KP_Subtract
, true},
141 {ui::VKEY_ADD
, XK_KP_Add
, true},
142 {ui::VKEY_DIVIDE
, XK_KP_Divide
, true},
143 {ui::VKEY_DECIMAL
, XK_KP_Decimal
, true},
144 {ui::VKEY_DELETE
, XK_KP_Delete
, true},
145 {ui::VKEY_INSERT
, XK_KP_Insert
, true},
146 {ui::VKEY_END
, XK_KP_End
, true},
147 {ui::VKEY_DOWN
, XK_KP_Down
, true},
148 {ui::VKEY_NEXT
, XK_KP_Page_Down
, true},
149 {ui::VKEY_LEFT
, XK_KP_Left
, true},
150 {ui::VKEY_CLEAR
, XK_KP_Begin
, true},
151 {ui::VKEY_RIGHT
, XK_KP_Right
, true},
152 {ui::VKEY_HOME
, XK_KP_Home
, true},
153 {ui::VKEY_UP
, XK_KP_Up
, true},
154 {ui::VKEY_PRIOR
, XK_KP_Page_Up
, true},
156 ui::ScopedXI2Event xev
;
157 for (size_t i
= 0; i
< arraysize(kTesCases
); ++i
) {
158 const TestCase
& test_case
= kTesCases
[i
];
160 xev
.InitKeyEvent(ui::ET_KEY_PRESSED
, test_case
.ui_keycode
, 0);
161 XEvent
* xevent
= xev
;
162 xevent
->xkey
.keycode
= XKeysymToKeycode(gfx::GetXDisplay(),
163 test_case
.x_keycode
);
164 ui::KeyEvent
event(xev
);
165 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
166 EXPECT_EQ(test_case
.expected_result
,
167 (webkit_event
.modifiers
& blink::WebInputEvent::IsKeyPad
) != 0)
168 << "Failed in " << i
<< "th test case: "
169 << "{ui_keycode:" << test_case
.ui_keycode
170 << ", x_keycode:" << test_case
.x_keycode
171 << "}, expect: " << test_case
.expected_result
;
176 } // namespace content