diff options
-rw-r--r-- | content/test/render_view_test.cc | 94 | ||||
-rw-r--r-- | ui/base/x/x11_util.cc | 60 | ||||
-rw-r--r-- | ui/base/x/x11_util.h | 7 |
3 files changed, 85 insertions, 76 deletions
diff --git a/content/test/render_view_test.cc b/content/test/render_view_test.cc index 154dd4c..1cb8fda 100644 --- a/content/test/render_view_test.cc +++ b/content/test/render_view_test.cc @@ -30,10 +30,8 @@ #if defined(USE_AURA) && defined(USE_X11) #include <X11/Xlib.h> -#include <X11/keysym.h> #include "ui/base/events.h" #include "ui/base/keycodes/keyboard_code_conversion.h" -#include "ui/base/keycodes/keyboard_code_conversion_x.h" #include "ui/base/x/x11_util.h" #endif @@ -50,65 +48,6 @@ const int32 kOpenerId = 7; const int32 kRouteId = 5; #if defined(USE_AURA) && defined(USE_X11) -// Converts ui::EventType to XKeyEvent state. -unsigned int XKeyEventState(int flags) { - return - ((flags & ui::EF_SHIFT_DOWN) ? ShiftMask : 0) | - ((flags & ui::EF_CONTROL_DOWN) ? ControlMask : 0) | - ((flags & ui::EF_CAPS_LOCK_DOWN) ? LockMask : 0); -} - -// Converts ui::EventType to XKeyEvent type. -int XKeyEventType(ui::EventType type) { - switch (type) { - case ui::ET_KEY_PRESSED: - return KeyPress; - case ui::ET_KEY_RELEASED: - return KeyRelease; - default: - return 0; - } -} - -// Converts ui::KeyboardCode to XKeyEvent keycode. -unsigned int XKeyEventKeyCode(ui::KeyboardCode key_code, - int flags, - Display* display) { - const int keysym = ui::XKeysymForWindowsKeyCode(key_code, - flags & ui::EF_SHIFT_DOWN); - // The test assumes the keycode for XK_less is equal to the one of XK_comma, - // but XKeysymToKeycode returns 94 for XK_less while it returns 59 for - // XK_comma. Here we convert the value for XK_less to the value for XK_comma. - return (keysym == XK_less) ? 59 : XKeysymToKeycode(display, keysym); -} - -// Creates a fake XEvent for testing. -XEvent* CreateFakeXEvent(ui::EventType type, - ui::KeyboardCode key_code, - int flags) { - Display* display = ui::GetXDisplay(); - XKeyEvent key_event; - key_event.type = XKeyEventType(type); - key_event.serial = 0; - key_event.send_event = 0; - key_event.display = display; - key_event.time = 0; - key_event.window = 0; - key_event.root = 0; - key_event.subwindow = 0; - key_event.x = 0; - key_event.y = 0; - key_event.x_root = 0; - key_event.y_root = 0; - key_event.state = XKeyEventState(flags); - key_event.keycode = XKeyEventKeyCode(key_code, flags, display); - key_event.same_screen = 1; - XEvent* event = new XEvent; - event->type = key_event.type; - event->xkey = key_event; - return event; -} - // Converts MockKeyboard::Modifiers to ui::EventFlags. int ConvertMockKeyboardModifier(MockKeyboard::Modifiers modifiers) { static struct ModifierMap { @@ -306,27 +245,30 @@ int RenderViewTest::SendKeyEvent(MockKeyboard::Layout layout, CHECK(output); const int flags = ConvertMockKeyboardModifier(modifiers); - scoped_ptr<XEvent> xevent1(CreateFakeXEvent( - ui::ET_KEY_PRESSED, - static_cast<ui::KeyboardCode>(key_code), - flags)); - aura::KeyEvent event1(xevent1.get(), false); + XEvent xevent1; + InitXKeyEventForTesting(ui::ET_KEY_PRESSED, + static_cast<ui::KeyboardCode>(key_code), + flags, + &xevent1); + aura::KeyEvent event1(&xevent1, false); NativeWebKeyboardEvent keydown_event(&event1); SendNativeKeyEvent(keydown_event); - scoped_ptr<XEvent> xevent2(CreateFakeXEvent( - ui::ET_KEY_PRESSED, - static_cast<ui::KeyboardCode>(key_code), - flags)); - aura::KeyEvent event2(xevent2.get(), true); + XEvent xevent2; + InitXKeyEventForTesting(ui::ET_KEY_PRESSED, + static_cast<ui::KeyboardCode>(key_code), + flags, + &xevent2); + aura::KeyEvent event2(&xevent2, true); NativeWebKeyboardEvent char_event(&event2); SendNativeKeyEvent(char_event); - scoped_ptr<XEvent> xevent3(CreateFakeXEvent( - ui::ET_KEY_RELEASED, - static_cast<ui::KeyboardCode>(key_code), - flags)); - aura::KeyEvent event3(xevent3.get(), false); + XEvent xevent3; + InitXKeyEventForTesting(ui::ET_KEY_RELEASED, + static_cast<ui::KeyboardCode>(key_code), + flags, + &xevent3); + aura::KeyEvent event3(&xevent3, false); NativeWebKeyboardEvent keyup_event(&event3); SendNativeKeyEvent(keyup_event); diff --git a/ui/base/x/x11_util.cc b/ui/base/x/x11_util.cc index 690a9c7..e63c56a 100644 --- a/ui/base/x/x11_util.cc +++ b/ui/base/x/x11_util.cc @@ -24,6 +24,7 @@ #include "base/string_util.h" #include "base/stringprintf.h" #include "base/threading/thread.h" +#include "ui/base/keycodes/keyboard_code_conversion_x.h" #include "ui/base/x/x11_util_internal.h" #include "ui/gfx/rect.h" #include "ui/gfx/size.h" @@ -103,6 +104,38 @@ bool GetProperty(XID window, const std::string& property_name, long max_length, property); } +// Converts ui::EventType to XKeyEvent state. +unsigned int XKeyEventState(int flags) { + return + ((flags & ui::EF_SHIFT_DOWN) ? ShiftMask : 0) | + ((flags & ui::EF_CONTROL_DOWN) ? ControlMask : 0) | + ((flags & ui::EF_CAPS_LOCK_DOWN) ? LockMask : 0); +} + +// Converts EventType to XKeyEvent type. +int XKeyEventType(ui::EventType type) { + switch (type) { + case ui::ET_KEY_PRESSED: + return KeyPress; + case ui::ET_KEY_RELEASED: + return KeyRelease; + default: + return 0; + } +} + +// Converts KeyboardCode to XKeyEvent keycode. +unsigned int XKeyEventKeyCode(ui::KeyboardCode key_code, + int flags, + Display* display) { + const int keysym = XKeysymForWindowsKeyCode(key_code, + flags & ui::EF_SHIFT_DOWN); + // Tests assume the keycode for XK_less is equal to the one of XK_comma, + // but XKeysymToKeycode returns 94 for XK_less while it returns 59 for + // XK_comma. Here we convert the value for XK_less to the value for XK_comma. + return (keysym == XK_less) ? 59 : XKeysymToKeycode(display, keysym); +} + // A process wide singleton that manages the usage of X cursors. class XCursorCache { public: @@ -872,6 +905,33 @@ void UpdateButtonMap() { XButtonMap::GetInstance()->UpdateMapping(); } +void InitXKeyEventForTesting(EventType type, + KeyboardCode key_code, + int flags, + XEvent* event) { + CHECK(event); + Display* display = GetXDisplay(); + XKeyEvent key_event; + key_event.type = XKeyEventType(type); + CHECK_NE(0, key_event.type); + key_event.serial = 0; + key_event.send_event = 0; + key_event.display = display; + key_event.time = 0; + key_event.window = 0; + key_event.root = 0; + key_event.subwindow = 0; + key_event.x = 0; + key_event.y = 0; + key_event.x_root = 0; + key_event.y_root = 0; + key_event.state = XKeyEventState(flags); + key_event.keycode = XKeyEventKeyCode(key_code, flags, display); + key_event.same_screen = 1; + event->type = key_event.type; + event->xkey = key_event; +} + // ---------------------------------------------------------------------------- // These functions are declared in x11_util_internal.h because they require // XLib.h to be included, and it conflicts with many other headers. diff --git a/ui/base/x/x11_util.h b/ui/base/x/x11_util.h index 6c87e73..0147108 100644 --- a/ui/base/x/x11_util.h +++ b/ui/base/x/x11_util.h @@ -16,6 +16,7 @@ #include <vector> #include "base/basictypes.h" +#include "ui/base/events.h" #include "ui/base/ui_export.h" typedef unsigned long Atom; @@ -235,6 +236,12 @@ int GetMappedButton(int button); // received. UI_EXPORT void UpdateButtonMap(); +// Initializes a XEvent that holds XKeyEvent for testing. +UI_EXPORT void InitXKeyEventForTesting(EventType type, + KeyboardCode key_code, + int flags, + XEvent* event); + } // namespace ui #endif // UI_BASE_X_X11_UTIL_H_ |