diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-19 03:39:27 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-19 03:39:27 +0000 |
commit | 687e25dfe5c6b8c3c9f93114a7fe6446b01ff2f1 (patch) | |
tree | e502bf911ae8ff5472a0b6c0a29a0fb4f6b2177b /base | |
parent | 0002677c9021d3d00266e63f5eb8af51e5d82bc5 (diff) | |
download | chromium_src-687e25dfe5c6b8c3c9f93114a7fe6446b01ff2f1.zip chromium_src-687e25dfe5c6b8c3c9f93114a7fe6446b01ff2f1.tar.gz chromium_src-687e25dfe5c6b8c3c9f93114a7fe6446b01ff2f1.tar.bz2 |
enable RenderViewTest.OnHandleKeyboardEvent on Linux.
Rearrange some existing automation code and write some new code.
BUG=none
TEST=runs
Review URL: http://codereview.chromium.org/2083012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47624 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gypi | 2 | ||||
-rw-r--r-- | base/event_synthesis_gtk.cc | 90 | ||||
-rw-r--r-- | base/event_synthesis_gtk.h | 36 |
3 files changed, 128 insertions, 0 deletions
diff --git a/base/base.gypi b/base/base.gypi index d529dab..ec5cb4d 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -71,6 +71,8 @@ 'event_trace_controller_win.h', 'event_trace_provider_win.cc', 'event_trace_provider_win.h', + 'event_synthesis_gtk.cc', + 'event_synthesis_gtk.h', 'file_path.cc', 'file_path.h', 'file_util.cc', diff --git a/base/event_synthesis_gtk.cc b/base/event_synthesis_gtk.cc new file mode 100644 index 0000000..2055909 --- /dev/null +++ b/base/event_synthesis_gtk.cc @@ -0,0 +1,90 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/event_synthesis_gtk.h" + +#include "base/keyboard_code_conversion_gtk.h" + +namespace base { + +GdkEvent* SynthesizeKeyEvent(GdkWindow* window, + bool press, guint gdk_key, guint state) { + GdkEvent* event = gdk_event_new(press ? GDK_KEY_PRESS : GDK_KEY_RELEASE); + + event->key.type = press ? GDK_KEY_PRESS : GDK_KEY_RELEASE; + event->key.window = window; + if (window) + g_object_ref(window); + event->key.send_event = false; + + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + event->key.time = ts.tv_sec * 1000 + ts.tv_nsec / 1000000; + + event->key.state = state; + event->key.keyval = gdk_key; + + GdkKeymapKey* keys; + gint n_keys; + if (event->key.keyval != 0 && + gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), + event->key.keyval, &keys, &n_keys)) { + event->key.hardware_keycode = keys[0].keycode; + event->key.group = keys[0].group; + g_free(keys); + } + + return event; +} + +void SynthesizeKeyPressEvents(GdkWindow* window, + base::KeyboardCode key, + bool control, bool shift, bool alt, + std::vector<GdkEvent*>* events) { + if (control) + events->push_back( + SynthesizeKeyEvent(window, true, GDK_Control_L, 0)); + + if (shift) { + events->push_back(SynthesizeKeyEvent(window, true, GDK_Shift_L, + control ? GDK_CONTROL_MASK : 0)); + } + + if (alt) { + guint state = (control ? GDK_CONTROL_MASK : 0) | + (shift ? GDK_SHIFT_MASK : 0); + events->push_back( + SynthesizeKeyEvent(window, true, GDK_Alt_L, state)); + } + + // TODO(estade): handle other state flags besides control, shift, alt? + // For example caps lock. + guint state = (control ? GDK_CONTROL_MASK : 0) | + (shift ? GDK_SHIFT_MASK : 0) | + (alt ? GDK_MOD1_MASK : 0); + + guint gdk_key = base::GdkKeyCodeForWindowsKeyCode(key, shift); + events->push_back(SynthesizeKeyEvent(window, true, gdk_key, state)); + events->push_back(SynthesizeKeyEvent(window, false, gdk_key, state)); + + if (alt) { + guint state = (control ? GDK_CONTROL_MASK : 0) | + (shift ? GDK_SHIFT_MASK : 0) | GDK_MOD1_MASK; + events->push_back( + SynthesizeKeyEvent(window, false, GDK_Alt_L, state)); + } + + if (shift) { + events->push_back( + SynthesizeKeyEvent(window, false, GDK_Shift_L, + (control ? GDK_CONTROL_MASK : 0) | GDK_SHIFT_MASK)); + } + + if (control) { + events->push_back( + SynthesizeKeyEvent(window, false, GDK_Control_L, GDK_CONTROL_MASK)); + } +} + +} // namespace base diff --git a/base/event_synthesis_gtk.h b/base/event_synthesis_gtk.h new file mode 100644 index 0000000..11a540c --- /dev/null +++ b/base/event_synthesis_gtk.h @@ -0,0 +1,36 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// This file declares routines for creating fake GDK events (at the moment, +// only keyboard events). This is useful for a variety of testing purposes. +// NOTE: This should not be used outside of testing. + +#ifndef BASE_EVENT_SYNTHESIS_GTK_ +#define BASE_EVENT_SYNTHESIS_GTK_ + +#include <gdk/gdk.h> +#include <gdk/gdkkeysyms.h> +#include <vector> + +#include "base/keyboard_codes.h" + +namespace base { + +// Creates and returns a key event. Passes ownership to the caller. +GdkEvent* SynthesizeKeyEvent(GdkWindow* event_window, + bool press, + guint gdk_key, + guint state); + +// Creates the proper sequence of key events for a key press + release. +// Ownership of the events in the vector is passed to the caller. +void SynthesizeKeyPressEvents( + GdkWindow* window, + base::KeyboardCode key, + bool control, bool shift, bool alt, + std::vector<GdkEvent*>* events); + +} // namespace base + +#endif // BASE_EVENT_SYNTHESIS_GTK_ |