Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: content/test/render_view_test.cc

Issue 8801008: Enable RenderViewImplTest.{OnHandleKeyboardEvent,InsertCharacters} for Aura on X11. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/test/render_view_test.h ('k') | ui/base/keycodes/keyboard_code_conversion_x.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/test/render_view_test.h" 5 #include "content/test/render_view_test.h"
6 6
7 #include "content/common/dom_storage_common.h" 7 #include "content/common/dom_storage_common.h"
8 #include "content/common/view_messages.h" 8 #include "content/common/view_messages.h"
9 #include "content/public/browser/native_web_keyboard_event.h" 9 #include "content/public/browser/native_web_keyboard_event.h"
10 #include "content/public/common/renderer_preferences.h" 10 #include "content/public/common/renderer_preferences.h"
11 #include "content/renderer/render_view_impl.h" 11 #include "content/renderer/render_view_impl.h"
12 #include "content/renderer/renderer_main_platform_delegate.h" 12 #include "content/renderer/renderer_main_platform_delegate.h"
13 #include "content/test/mock_render_process.h" 13 #include "content/test/mock_render_process.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h " 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h "
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
21 #include "webkit/glue/webkit_glue.h" 21 #include "webkit/glue/webkit_glue.h"
22 22
23 #if defined(OS_LINUX) && !defined(USE_AURA) 23 #if defined(OS_LINUX) && !defined(USE_AURA)
24 #include "ui/base/gtk/event_synthesis_gtk.h" 24 #include "ui/base/gtk/event_synthesis_gtk.h"
25 #endif 25 #endif
26 26
27 #if defined(USE_AURA)
28 #include "ui/aura/event.h"
29 #endif
30
31 #if defined(USE_AURA) && defined(USE_X11)
32 #include <X11/Xlib.h>
33 #include <X11/keysym.h>
34 #include "ui/base/events.h"
35 #include "ui/base/keycodes/keyboard_code_conversion.h"
36 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
37 #include "ui/base/x/x11_util.h"
38 #endif
39
27 using WebKit::WebFrame; 40 using WebKit::WebFrame;
28 using WebKit::WebInputEvent; 41 using WebKit::WebInputEvent;
29 using WebKit::WebMouseEvent; 42 using WebKit::WebMouseEvent;
30 using WebKit::WebScriptController; 43 using WebKit::WebScriptController;
31 using WebKit::WebScriptSource; 44 using WebKit::WebScriptSource;
32 using WebKit::WebString; 45 using WebKit::WebString;
33 using WebKit::WebURLRequest; 46 using WebKit::WebURLRequest;
34 47
35 namespace { 48 namespace {
36 const int32 kOpenerId = 7; 49 const int32 kOpenerId = 7;
37 const int32 kRouteId = 5; 50 const int32 kRouteId = 5;
51
52 #if defined(USE_AURA) && defined(USE_X11)
53 // Converts ui::EventType to XKeyEvent state.
54 unsigned int XKeyEventState(int flags) {
55 return
56 ((flags & ui::EF_SHIFT_DOWN) ? ShiftMask : 0) |
57 ((flags & ui::EF_CONTROL_DOWN) ? ControlMask : 0) |
58 ((flags & ui::EF_CAPS_LOCK_DOWN) ? LockMask : 0);
59 }
60
61 // Converts ui::EventType to XKeyEvent type.
62 int XKeyEventType(ui::EventType type) {
63 switch (type) {
64 case ui::ET_KEY_PRESSED:
65 return KeyPress;
66 case ui::ET_KEY_RELEASED:
67 return KeyRelease;
68 default:
69 return 0;
70 }
71 }
72
73 // Converts ui::KeyboardCode to XKeyEvent keycode.
74 unsigned int XKeyEventKeyCode(ui::KeyboardCode key_code,
75 int flags,
76 Display* display) {
77 const int keysym = ui::XKeysymForWindowsKeyCode(key_code,
78 flags & ui::EF_SHIFT_DOWN);
79 // The test assumes the keycode for XK_less is equal to the one of XK_comma,
80 // but XKeysymToKeycode returns 94 for XK_less while it returns 59 for
81 // XK_comma. Here we convert the value for XK_less to the value for XK_comma.
82 return (keysym == XK_less) ? 59 : XKeysymToKeycode(display, keysym);
83 }
84
85 // Creates a fake XEvent for testing.
86 XEvent* CreateFakeXEvent(ui::EventType type,
87 ui::KeyboardCode key_code,
88 int flags) {
89 Display* display = ui::GetXDisplay();
90 XKeyEvent key_event;
91 key_event.type = XKeyEventType(type);
92 key_event.serial = 0;
93 key_event.send_event = 0;
94 key_event.display = display;
95 key_event.time = 0;
96 key_event.window = 0;
97 key_event.root = 0;
98 key_event.subwindow = 0;
99 key_event.x = 0;
100 key_event.y = 0;
101 key_event.x_root = 0;
102 key_event.y_root = 0;
103 key_event.state = XKeyEventState(flags);
104 key_event.keycode = XKeyEventKeyCode(key_code, flags, display);
105 key_event.same_screen = 1;
106 XEvent* event = new XEvent;
107 event->type = key_event.type;
108 event->xkey = key_event;
109 return event;
110 }
111
112 // Converts MockKeyboard::Modifiers to ui::EventFlags.
113 int ConvertMockKeyboardModifier(MockKeyboard::Modifiers modifiers) {
114 static struct ModifierMap {
115 MockKeyboard::Modifiers src;
116 int dst;
117 } kModifierMap[] = {
118 { MockKeyboard::LEFT_SHIFT, ui::EF_SHIFT_DOWN },
119 { MockKeyboard::RIGHT_SHIFT, ui::EF_SHIFT_DOWN },
120 { MockKeyboard::LEFT_CONTROL, ui::EF_CONTROL_DOWN },
121 { MockKeyboard::RIGHT_CONTROL, ui::EF_CONTROL_DOWN },
122 { MockKeyboard::LEFT_ALT, ui::EF_ALT_DOWN },
123 { MockKeyboard::RIGHT_ALT, ui::EF_ALT_DOWN },
124 };
125 int flags = 0;
126 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kModifierMap); ++i) {
127 if (kModifierMap[i].src & modifiers) {
128 flags |= kModifierMap[i].dst;
129 }
130 }
131 return flags;
132 }
133 #endif
38 } // namespace 134 } // namespace
39 135
40 namespace content { 136 namespace content {
41 137
42 RenderViewTest::RenderViewTest() : view_(NULL) { 138 RenderViewTest::RenderViewTest() : view_(NULL) {
43 } 139 }
44 140
45 RenderViewTest::~RenderViewTest() { 141 RenderViewTest::~RenderViewTest() {
46 } 142 }
47 143
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 246
151 platform_->PlatformUninitialize(); 247 platform_->PlatformUninitialize();
152 platform_.reset(); 248 platform_.reset();
153 params_.reset(); 249 params_.reset();
154 command_line_.reset(); 250 command_line_.reset();
155 } 251 }
156 252
157 int RenderViewTest::SendKeyEvent(MockKeyboard::Layout layout, 253 int RenderViewTest::SendKeyEvent(MockKeyboard::Layout layout,
158 int key_code, 254 int key_code,
159 MockKeyboard::Modifiers modifiers, 255 MockKeyboard::Modifiers modifiers,
160 std::wstring* output) { 256 string16* output) {
161 #if defined(USE_AURA) 257 #if defined(OS_WIN)
162 NOTIMPLEMENTED();
163 return L'\0';
164 #elif defined(OS_WIN)
165 // Retrieve the Unicode character for the given tuple (keyboard-layout, 258 // Retrieve the Unicode character for the given tuple (keyboard-layout,
166 // key-code, and modifiers). 259 // key-code, and modifiers).
167 // Exit when a keyboard-layout driver cannot assign a Unicode character to 260 // Exit when a keyboard-layout driver cannot assign a Unicode character to
168 // the tuple to prevent sending an invalid key code to the RenderView object. 261 // the tuple to prevent sending an invalid key code to the RenderView object.
169 CHECK(mock_keyboard_.get()); 262 CHECK(mock_keyboard_.get());
170 CHECK(output); 263 CHECK(output);
171 int length = mock_keyboard_->GetCharacters(layout, key_code, modifiers, 264 int length = mock_keyboard_->GetCharacters(layout, key_code, modifiers,
172 output); 265 output);
173 if (length != 1) 266 if (length != 1)
174 return -1; 267 return -1;
175 268
176 // Create IPC messages from Windows messages and send them to our 269 // Create IPC messages from Windows messages and send them to our
177 // back-end. 270 // back-end.
178 // A keyboard event of Windows consists of three Windows messages: 271 // A keyboard event of Windows consists of three Windows messages:
179 // WM_KEYDOWN, WM_CHAR, and WM_KEYUP. 272 // WM_KEYDOWN, WM_CHAR, and WM_KEYUP.
180 // WM_KEYDOWN and WM_KEYUP sends virtual-key codes. On the other hand, 273 // WM_KEYDOWN and WM_KEYUP sends virtual-key codes. On the other hand,
181 // WM_CHAR sends a composed Unicode character. 274 // WM_CHAR sends a composed Unicode character.
182 MSG msg1 = { NULL, WM_KEYDOWN, key_code, 0 }; 275 MSG msg1 = { NULL, WM_KEYDOWN, key_code, 0 };
276 #if defined(USE_AURA)
277 aura::KeyEvent evt1(msg1, false);
278 NativeWebKeyboardEvent keydown_event(&evt1);
279 #else
183 NativeWebKeyboardEvent keydown_event(msg1); 280 NativeWebKeyboardEvent keydown_event(msg1);
281 #endif
184 SendNativeKeyEvent(keydown_event); 282 SendNativeKeyEvent(keydown_event);
185 283
186 MSG msg2 = { NULL, WM_CHAR, (*output)[0], 0 }; 284 MSG msg2 = { NULL, WM_CHAR, (*output)[0], 0 };
285 #if defined(USE_AURA)
286 aura::KeyEvent evt2(msg2, true);
287 NativeWebKeyboardEvent char_event(&evt2);
288 #else
187 NativeWebKeyboardEvent char_event(msg2); 289 NativeWebKeyboardEvent char_event(msg2);
290 #endif
188 SendNativeKeyEvent(char_event); 291 SendNativeKeyEvent(char_event);
189 292
190 MSG msg3 = { NULL, WM_KEYUP, key_code, 0 }; 293 MSG msg3 = { NULL, WM_KEYUP, key_code, 0 };
294 #if defined(USE_AURA)
295 aura::KeyEvent evt3(msg3, false);
296 NativeWebKeyboardEvent keyup_event(&evt3);
297 #else
191 NativeWebKeyboardEvent keyup_event(msg3); 298 NativeWebKeyboardEvent keyup_event(msg3);
299 #endif
192 SendNativeKeyEvent(keyup_event); 300 SendNativeKeyEvent(keyup_event);
193 301
194 return length; 302 return length;
303 #elif defined(USE_AURA) && defined(USE_X11)
304 // We ignore |layout|, which means we are only testing the layout of the
305 // current locale. TODO(mazda): fix this to respect |layout|.
306 CHECK(output);
307 const int flags = ConvertMockKeyboardModifier(modifiers);
308
309 scoped_ptr<XEvent> xevent1(CreateFakeXEvent(
310 ui::ET_KEY_PRESSED,
311 static_cast<ui::KeyboardCode>(key_code),
312 flags));
313 aura::KeyEvent event1(xevent1.get(), false);
314 NativeWebKeyboardEvent keydown_event(&event1);
315 SendNativeKeyEvent(keydown_event);
316
317 scoped_ptr<XEvent> xevent2(CreateFakeXEvent(
318 ui::ET_KEY_PRESSED,
319 static_cast<ui::KeyboardCode>(key_code),
320 flags));
321 aura::KeyEvent event2(xevent2.get(), true);
322 NativeWebKeyboardEvent char_event(&event2);
323 SendNativeKeyEvent(char_event);
324
325 scoped_ptr<XEvent> xevent3(CreateFakeXEvent(
326 ui::ET_KEY_RELEASED,
327 static_cast<ui::KeyboardCode>(key_code),
328 flags));
329 aura::KeyEvent event3(xevent3.get(), false);
330 NativeWebKeyboardEvent keyup_event(&event3);
331 SendNativeKeyEvent(keyup_event);
332
333 long c = GetCharacterFromKeyCode(static_cast<ui::KeyboardCode>(key_code),
334 flags);
335 output->assign(1, static_cast<char16>(c));
336 return 1;
195 #elif defined(OS_LINUX) 337 #elif defined(OS_LINUX)
196 // We ignore |layout|, which means we are only testing the layout of the 338 // We ignore |layout|, which means we are only testing the layout of the
197 // current locale. TODO(estade): fix this to respect |layout|. 339 // current locale. TODO(estade): fix this to respect |layout|.
198 std::vector<GdkEvent*> events; 340 std::vector<GdkEvent*> events;
199 ui::SynthesizeKeyPressEvents( 341 ui::SynthesizeKeyPressEvents(
200 NULL, static_cast<ui::KeyboardCode>(key_code), 342 NULL, static_cast<ui::KeyboardCode>(key_code),
201 modifiers & (MockKeyboard::LEFT_CONTROL | MockKeyboard::RIGHT_CONTROL), 343 modifiers & (MockKeyboard::LEFT_CONTROL | MockKeyboard::RIGHT_CONTROL),
202 modifiers & (MockKeyboard::LEFT_SHIFT | MockKeyboard::RIGHT_SHIFT), 344 modifiers & (MockKeyboard::LEFT_SHIFT | MockKeyboard::RIGHT_SHIFT),
203 modifiers & (MockKeyboard::LEFT_ALT | MockKeyboard::RIGHT_ALT), 345 modifiers & (MockKeyboard::LEFT_ALT | MockKeyboard::RIGHT_ALT),
204 &events); 346 &events);
(...skipping 11 matching lines...) Expand all
216 if (webkit_event.type == WebKit::WebInputEvent::RawKeyDown) { 358 if (webkit_event.type == WebKit::WebInputEvent::RawKeyDown) {
217 NativeWebKeyboardEvent char_event = webkit_event; 359 NativeWebKeyboardEvent char_event = webkit_event;
218 char_event.type = WebKit::WebInputEvent::Char; 360 char_event.type = WebKit::WebInputEvent::Char;
219 char_event.skip_in_browser = true; 361 char_event.skip_in_browser = true;
220 SendNativeKeyEvent(char_event); 362 SendNativeKeyEvent(char_event);
221 } 363 }
222 } 364 }
223 gdk_event_free(events[i]); 365 gdk_event_free(events[i]);
224 } 366 }
225 367
226 *output = std::wstring(1, unicode_key); 368 output->assign(1, static_cast<char16>(unicode_key));
227 return 1; 369 return 1;
228 #else 370 #else
229 NOTIMPLEMENTED(); 371 NOTIMPLEMENTED();
230 return L'\0'; 372 return L'\0';
231 #endif 373 #endif
232 } 374 }
233 375
234 void RenderViewTest::SendNativeKeyEvent( 376 void RenderViewTest::SendNativeKeyEvent(
235 const NativeWebKeyboardEvent& key_event) { 377 const NativeWebKeyboardEvent& key_event) {
236 SendWebKeyboardEvent(key_event); 378 SendWebKeyboardEvent(key_event);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 486 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
345 impl->set_send_content_state_immediately(true); 487 impl->set_send_content_state_immediately(true);
346 } 488 }
347 489
348 WebKit::WebWidget* RenderViewTest::GetWebWidget() { 490 WebKit::WebWidget* RenderViewTest::GetWebWidget() {
349 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_); 491 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
350 return impl->webwidget(); 492 return impl->webwidget();
351 } 493 }
352 494
353 } // namespace content 495 } // namespace content
OLDNEW
« no previous file with comments | « content/test/render_view_test.h ('k') | ui/base/keycodes/keyboard_code_conversion_x.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698