diff options
author | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-22 02:59:20 +0000 |
---|---|---|
committer | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-22 02:59:20 +0000 |
commit | e20aa5b622e390c720d1a07d55136b411b8808c5 (patch) | |
tree | c4b77238dca423c905f3b8a0f4df6218907d8d70 /chrome/test/webdriver/keycode_text_conversion_linux.cc | |
parent | 5cb7e177ac5622d5923a092acb1758194b787543 (diff) | |
download | chromium_src-e20aa5b622e390c720d1a07d55136b411b8808c5.zip chromium_src-e20aa5b622e390c720d1a07d55136b411b8808c5.tar.gz chromium_src-e20aa5b622e390c720d1a07d55136b411b8808c5.tar.bz2 |
Implement keycode/text conversion on mac for chromedriver.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/6537024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75569 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/webdriver/keycode_text_conversion_linux.cc')
-rw-r--r-- | chrome/test/webdriver/keycode_text_conversion_linux.cc | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/chrome/test/webdriver/keycode_text_conversion_linux.cc b/chrome/test/webdriver/keycode_text_conversion_linux.cc new file mode 100644 index 0000000..ebbb626 --- /dev/null +++ b/chrome/test/webdriver/keycode_text_conversion_linux.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2011 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 "chrome/test/webdriver/keycode_text_conversion.h" + +#include <gdk/gdk.h> + +#include "base/string_util.h" +#include "base/utf_string_conversions.h" +#include "chrome/common/automation_constants.h" +#include "ui/base/keycodes/keyboard_code_conversion_gtk.h" + +namespace webdriver { + +std::string ConvertKeyCodeToText(ui::KeyboardCode key_code, int modifiers) { + // |gdk_keyval_to_upper| does not convert some keys like '1' to '!', so + // provide |ui::GdkKeyCodeForWindowsKeyCode| with our shift state as well, + // which will do basic conversions like it for us. + guint gdk_key_code = ui::GdkKeyCodeForWindowsKeyCode( + key_code, modifiers & automation::kShiftKeyMask); + if (modifiers & automation::kShiftKeyMask) + gdk_key_code = gdk_keyval_to_upper(gdk_key_code); + guint32 unicode_char = gdk_keyval_to_unicode(gdk_key_code); + if (!unicode_char) + return ""; + gchar buffer[6]; + gint length = g_unichar_to_utf8(unicode_char, buffer); + return std::string(buffer, length); +} + +// Converts a character to the key code and modifier set that would +// produce the character using the given keyboard layout. +bool ConvertCharToKeyCode( + char16 key, ui::KeyboardCode* key_code, int *necessary_modifiers) { + guint gdk_key_code = gdk_unicode_to_keyval(key); + if (!gdk_key_code) + return false; + + string16 key_string; + key_string.push_back(key); + const std::string kNeedsShiftSymbols= "!@#$%^&*()_+~{}|\":<>?"; + bool is_special_symbol = IsStringASCII(key_string) && + kNeedsShiftSymbols.find(static_cast<char>(key)) != std::string::npos; + + glong char_count = 0; + gunichar* key_string_utf32 = g_utf16_to_ucs4( + &key, 1, NULL, &char_count, NULL); + if (!key_string_utf32) + return false; + if (char_count != 1) { + g_free(key_string_utf32); + return false; + } + gunichar key_utf32 = key_string_utf32[0]; + g_free(key_string_utf32); + + if (is_special_symbol || key_utf32 != g_unichar_tolower(key_utf32)) + *necessary_modifiers = automation::kShiftKeyMask; + ui::KeyboardCode code = ui::WindowsKeyCodeForGdkKeyCode(gdk_key_code); + if (code != ui::VKEY_UNKNOWN) + *key_code = code; + return code != ui::VKEY_UNKNOWN; +} + +} // namespace webdriver |