diff options
author | mazda@chromium.org <mazda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-08 19:46:03 +0000 |
---|---|---|
committer | mazda@chromium.org <mazda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-08 19:46:03 +0000 |
commit | 0c335fa2455e76c8fc5850446783a69d6552a0be (patch) | |
tree | 5aacf1fd00b1f240fd44d5e08467ff5121448250 /ui | |
parent | 7556ea2db8b28d15a37d5680febadc5cf1a5cc5b (diff) | |
download | chromium_src-0c335fa2455e76c8fc5850446783a69d6552a0be.zip chromium_src-0c335fa2455e76c8fc5850446783a69d6552a0be.tar.gz chromium_src-0c335fa2455e76c8fc5850446783a69d6552a0be.tar.bz2 |
Move CreateFakeXEvent to x11_util.{h,cc}.
I'd like to use this function from shell_accelerator_controller_unittest.
BUG=none
TEST=Ran browser_tests.
Review URL: http://codereview.chromium.org/8834014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113642 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/base/x/x11_util.cc | 60 | ||||
-rw-r--r-- | ui/base/x/x11_util.h | 7 |
2 files changed, 67 insertions, 0 deletions
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_ |