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/dom/dom_code.h"
12 #include "ui/events/keycodes/dom/dom_key.h"
13 #include "ui/events/keycodes/dom/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
) {
29 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
,
30 ui::DomCode::CONTROL_LEFT
, ui::EF_CONTROL_DOWN
);
31 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
32 // However, modifier bit for Control in |webkit_event| should be set.
33 EXPECT_EQ(blink::WebInputEvent::ControlKey
| blink::WebInputEvent::IsLeft
,
34 webkit_event
.modifiers
);
35 EXPECT_EQ(static_cast<int>(ui::DomCode::CONTROL_LEFT
),
36 webkit_event
.domCode
);
40 ui::KeyEvent
event(ui::ET_KEY_RELEASED
, ui::VKEY_CONTROL
,
41 ui::DomCode::CONTROL_LEFT
, ui::EF_NONE
);
42 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
43 // However, modifier bit for Control in |webkit_event| shouldn't be set.
44 EXPECT_EQ(blink::WebInputEvent::IsLeft
, webkit_event
.modifiers
);
45 EXPECT_EQ(static_cast<int>(ui::DomCode::CONTROL_LEFT
),
46 webkit_event
.domCode
);
50 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
,
51 ui::DomCode::CONTROL_RIGHT
, ui::EF_CONTROL_DOWN
);
52 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
53 // However, modifier bit for Control in |webkit_event| should be set.
54 EXPECT_EQ(blink::WebInputEvent::ControlKey
| blink::WebInputEvent::IsRight
,
55 webkit_event
.modifiers
);
56 EXPECT_EQ(static_cast<int>(ui::DomCode::CONTROL_RIGHT
),
57 webkit_event
.domCode
);
60 // Release right Ctrl.
61 ui::KeyEvent
event(ui::ET_KEY_RELEASED
, ui::VKEY_CONTROL
,
62 ui::DomCode::CONTROL_RIGHT
, ui::EF_NONE
);
63 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
64 // However, modifier bit for Control in |webkit_event| shouldn't be set.
65 EXPECT_EQ(blink::WebInputEvent::IsRight
, webkit_event
.modifiers
);
66 EXPECT_EQ(static_cast<int>(ui::DomCode::CONTROL_RIGHT
),
67 webkit_event
.domCode
);
70 const int kLocationModifiers
=
71 blink::WebInputEvent::IsLeft
| blink::WebInputEvent::IsRight
;
72 ui::ScopedXI2Event xev
;
75 xev
.InitKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
, 0);
76 ui::KeyEvent
event(xev
);
77 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
78 // However, modifier bit for Control in |webkit_event| should be set.
79 EXPECT_EQ(blink::WebInputEvent::ControlKey
,
80 webkit_event
.modifiers
& ~kLocationModifiers
);
84 xev
.InitKeyEvent(ui::ET_KEY_RELEASED
, ui::VKEY_CONTROL
, ControlMask
);
85 ui::KeyEvent
event(xev
);
86 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
87 // However, modifier bit for Control in |webkit_event| shouldn't be set.
88 EXPECT_EQ(0, webkit_event
.modifiers
& ~kLocationModifiers
);
93 // Checks that MakeWebKeyboardEvent returns a correct windowsKeyCode.
94 #if defined(OS_CHROMEOS) || defined(THREAD_SANITIZER)
95 // Fails on Chrome OS and under ThreadSanitizer on Linux, see
96 // https://crbug.com/449103.
97 #define MAYBE_TestMakeWebKeyboardEventWindowsKeyCode \
98 DISABLED_TestMakeWebKeyboardEventWindowsKeyCode
100 #define MAYBE_TestMakeWebKeyboardEventWindowsKeyCode \
101 TestMakeWebKeyboardEventWindowsKeyCode
103 TEST(WebInputEventAuraTest
, MAYBE_TestMakeWebKeyboardEventWindowsKeyCode
) {
105 ui::ScopedXI2Event xev
;
108 xev
.InitKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
, 0);
109 XEvent
* xevent
= xev
;
110 xevent
->xkey
.keycode
=
111 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::CONTROL_LEFT
);
112 ui::KeyEvent
event(xev
);
113 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
114 // ui::VKEY_LCONTROL, instead of ui::VKEY_CONTROL, should be filled.
115 EXPECT_EQ(ui::VKEY_LCONTROL
, webkit_event
.windowsKeyCode
);
119 xev
.InitKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
, 0);
120 XEvent
* xevent
= xev
;
121 xevent
->xkey
.keycode
= ui::KeycodeConverter::DomCodeToNativeKeycode(
122 ui::DomCode::CONTROL_RIGHT
);
123 ui::KeyEvent
event(xev
);
124 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
125 // ui::VKEY_RCONTROL, instead of ui::VKEY_CONTROL, should be filled.
126 EXPECT_EQ(ui::VKEY_RCONTROL
, webkit_event
.windowsKeyCode
);
128 #elif defined(OS_WIN)
129 // TODO(yusukes): Add tests for win_aura once keyboardEvent() in
130 // third_party/WebKit/Source/web/win/WebInputEventFactory.cpp is modified
131 // to return VKEY_[LR]XXX instead of VKEY_XXX.
132 // https://bugs.webkit.org/show_bug.cgi?id=86694
136 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
,
137 ui::DomCode::CONTROL_LEFT
, ui::EF_CONTROL_DOWN
,
138 ui::DomKey::CONTROL
, ui::EventTimeForNow());
139 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
140 // ui::VKEY_LCONTROL, instead of ui::VKEY_CONTROL, should be filled.
141 EXPECT_EQ(ui::VKEY_LCONTROL
, webkit_event
.windowsKeyCode
);
145 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
,
146 ui::DomCode::CONTROL_RIGHT
, ui::EF_CONTROL_DOWN
,
147 ui::DomKey::CONTROL
, ui::EventTimeForNow());
148 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
149 // ui::VKEY_RCONTROL, instead of ui::VKEY_CONTROL, should be filled.
150 EXPECT_EQ(ui::VKEY_RCONTROL
, webkit_event
.windowsKeyCode
);
154 // Checks that MakeWebKeyboardEvent fills a correct keypad modifier.
155 TEST(WebInputEventAuraTest
, TestMakeWebKeyboardEventKeyPadKeyCode
) {
162 ui::DomCode dom_code
; // The physical key (location).
163 ui::KeyboardCode ui_keycode
; // The virtual key code.
164 uint32 x_keysym
; // The X11 keysym.
165 bool expected_result
; // true if the event has "isKeyPad" modifier.
167 {ui::DomCode::DIGIT0
, ui::VKEY_0
, XK(0), false},
168 {ui::DomCode::DIGIT1
, ui::VKEY_1
, XK(1), false},
169 {ui::DomCode::DIGIT2
, ui::VKEY_2
, XK(2), false},
170 {ui::DomCode::DIGIT3
, ui::VKEY_3
, XK(3), false},
171 {ui::DomCode::DIGIT4
, ui::VKEY_4
, XK(4), false},
172 {ui::DomCode::DIGIT5
, ui::VKEY_5
, XK(5), false},
173 {ui::DomCode::DIGIT6
, ui::VKEY_6
, XK(6), false},
174 {ui::DomCode::DIGIT7
, ui::VKEY_7
, XK(7), false},
175 {ui::DomCode::DIGIT8
, ui::VKEY_8
, XK(8), false},
176 {ui::DomCode::DIGIT9
, ui::VKEY_9
, XK(9), false},
178 {ui::DomCode::NUMPAD0
, ui::VKEY_NUMPAD0
, XK(KP_0
), true},
179 {ui::DomCode::NUMPAD1
, ui::VKEY_NUMPAD1
, XK(KP_1
), true},
180 {ui::DomCode::NUMPAD2
, ui::VKEY_NUMPAD2
, XK(KP_2
), true},
181 {ui::DomCode::NUMPAD3
, ui::VKEY_NUMPAD3
, XK(KP_3
), true},
182 {ui::DomCode::NUMPAD4
, ui::VKEY_NUMPAD4
, XK(KP_4
), true},
183 {ui::DomCode::NUMPAD5
, ui::VKEY_NUMPAD5
, XK(KP_5
), true},
184 {ui::DomCode::NUMPAD6
, ui::VKEY_NUMPAD6
, XK(KP_6
), true},
185 {ui::DomCode::NUMPAD7
, ui::VKEY_NUMPAD7
, XK(KP_7
), true},
186 {ui::DomCode::NUMPAD8
, ui::VKEY_NUMPAD8
, XK(KP_8
), true},
187 {ui::DomCode::NUMPAD9
, ui::VKEY_NUMPAD9
, XK(KP_9
), true},
189 {ui::DomCode::NUMPAD_MULTIPLY
, ui::VKEY_MULTIPLY
, XK(KP_Multiply
), true},
190 {ui::DomCode::NUMPAD_SUBTRACT
, ui::VKEY_SUBTRACT
, XK(KP_Subtract
), true},
191 {ui::DomCode::NUMPAD_ADD
, ui::VKEY_ADD
, XK(KP_Add
), true},
192 {ui::DomCode::NUMPAD_DIVIDE
, ui::VKEY_DIVIDE
, XK(KP_Divide
), true},
193 {ui::DomCode::NUMPAD_DECIMAL
, ui::VKEY_DECIMAL
, XK(KP_Decimal
), true},
194 {ui::DomCode::NUMPAD_DECIMAL
, ui::VKEY_DELETE
, XK(KP_Delete
), true},
195 {ui::DomCode::NUMPAD0
, ui::VKEY_INSERT
, XK(KP_Insert
), true},
196 {ui::DomCode::NUMPAD1
, ui::VKEY_END
, XK(KP_End
), true},
197 {ui::DomCode::NUMPAD2
, ui::VKEY_DOWN
, XK(KP_Down
), true},
198 {ui::DomCode::NUMPAD3
, ui::VKEY_NEXT
, XK(KP_Page_Down
), true},
199 {ui::DomCode::NUMPAD4
, ui::VKEY_LEFT
, XK(KP_Left
), true},
200 {ui::DomCode::NUMPAD5
, ui::VKEY_CLEAR
, XK(KP_Begin
), true},
201 {ui::DomCode::NUMPAD6
, ui::VKEY_RIGHT
, XK(KP_Right
), true},
202 {ui::DomCode::NUMPAD7
, ui::VKEY_HOME
, XK(KP_Home
), true},
203 {ui::DomCode::NUMPAD8
, ui::VKEY_UP
, XK(KP_Up
), true},
204 {ui::DomCode::NUMPAD9
, ui::VKEY_PRIOR
, XK(KP_Page_Up
), true},
206 for (const auto& test_case
: kTesCases
) {
207 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, test_case
.ui_keycode
,
208 test_case
.dom_code
, ui::EF_NONE
);
209 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
210 EXPECT_EQ(test_case
.expected_result
,
211 (webkit_event
.modifiers
& blink::WebInputEvent::IsKeyPad
) != 0)
214 << ui::KeycodeConverter::DomCodeToCodeString(test_case
.dom_code
)
215 << ", ui_keycode:" << test_case
.ui_keycode
216 << "}, expect: " << test_case
.expected_result
;
219 ui::ScopedXI2Event xev
;
220 for (size_t i
= 0; i
< arraysize(kTesCases
); ++i
) {
221 const TestCase
& test_case
= kTesCases
[i
];
223 // TODO: re-enable the two cases excluded here once all trybots
224 // are sufficiently up to date to round-trip the associated keys.
225 if ((test_case
.x_keysym
== XK_KP_Divide
) ||
226 (test_case
.x_keysym
== XK_KP_Decimal
))
229 xev
.InitKeyEvent(ui::ET_KEY_PRESSED
, test_case
.ui_keycode
, ui::EF_NONE
);
230 XEvent
* xevent
= xev
;
231 xevent
->xkey
.keycode
=
232 XKeysymToKeycode(gfx::GetXDisplay(), test_case
.x_keysym
);
233 if (!xevent
->xkey
.keycode
)
235 ui::KeyEvent
event(xev
);
236 blink::WebKeyboardEvent webkit_event
= MakeWebKeyboardEvent(event
);
237 EXPECT_EQ(test_case
.expected_result
,
238 (webkit_event
.modifiers
& blink::WebInputEvent::IsKeyPad
) != 0)
239 << "Failed in " << i
<< "th test case: "
241 << ui::KeycodeConverter::DomCodeToCodeString(test_case
.dom_code
)
242 << ", ui_keycode:" << test_case
.ui_keycode
243 << ", x_keysym:" << test_case
.x_keysym
244 << "}, expect: " << test_case
.expected_result
;
249 } // namespace content