Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / chrome / test / chromedriver / keycode_text_conversion_ozone.cc
blobbc3259ee27a1a40527a83e0090aefbdd28f42a70
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/strings/string16.h"
6 #include "base/strings/stringprintf.h"
7 #include "base/strings/utf_string_conversion_utils.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/test/chromedriver/chrome/ui_events.h"
10 #include "chrome/test/chromedriver/keycode_text_conversion.h"
11 #include "ui/events/event_constants.h"
12 #include "ui/events/keycodes/dom/dom_code.h"
13 #include "ui/events/keycodes/dom/keycode_converter.h"
14 #include "ui/events/keycodes/keyboard_code_conversion.h"
15 #include "ui/events/ozone/layout/keyboard_layout_engine.h"
16 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
18 bool ConvertKeyCodeToText(ui::KeyboardCode key_code,
19 int modifiers,
20 std::string* text,
21 std::string* error_msg) {
22 ui::KeyboardLayoutEngine* keyboard_layout_engine =
23 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine();
24 ui::DomCode dom_code = ui::UsLayoutKeyboardCodeToDomCode(key_code);
25 int event_flags = ui::EF_NONE;
27 // Chrome OS keyboards don't have meta or num lock keys, so these modifier
28 // masks are ignored. Only handle alt, ctrl and shift.
29 if (modifiers & kAltKeyModifierMask)
30 event_flags |= ui::EF_ALT_DOWN;
31 if (modifiers & kControlKeyModifierMask)
32 event_flags |= ui::EF_CONTROL_DOWN;
33 if (modifiers & kShiftKeyModifierMask)
34 event_flags |= ui::EF_SHIFT_DOWN;
36 ui::DomKey dom_key;
37 ui::KeyboardCode key_code_ignored;
39 if (!keyboard_layout_engine->Lookup(dom_code, event_flags, &dom_key,
40 &key_code_ignored) ||
41 !dom_key.IsCharacter()) {
42 // The keycode lookup failed, or mapped to a key that isn't a unicode
43 // character. Convert it to the empty string.
44 *text = std::string();
45 return true;
48 base::WriteUnicodeCharacter(dom_key.ToCharacter(), text);
49 return true;
52 bool ConvertCharToKeyCode(base::char16 key,
53 ui::KeyboardCode* key_code,
54 int* necessary_modifiers,
55 std::string* error_msg) {
56 base::string16 key_string;
57 key_string.push_back(key);
58 std::string key_string_utf8 = base::UTF16ToUTF8(key_string);
59 bool found_code = false;
60 *error_msg = std::string();
61 // There doesn't seem to be a way to get a CrOS key code for a given unicode
62 // character. So here we check every key code to see if it produces the
63 // right character, as we do on Mac (see keycode_text_conversion_mac.mm).
64 for (int i = 0; i < 256; ++i) {
65 ui::KeyboardCode code = static_cast<ui::KeyboardCode>(i);
66 // Skip the numpad keys.
67 if (code >= ui::VKEY_NUMPAD0 && code <= ui::VKEY_DIVIDE)
68 continue;
69 std::string key_string;
70 if (!ConvertKeyCodeToText(code, 0, &key_string, error_msg))
71 return false;
72 found_code = key_string_utf8 == key_string;
73 std::string key_string_utf8_tmp;
74 if (!ConvertKeyCodeToText(code, kShiftKeyModifierMask, &key_string_utf8_tmp,
75 error_msg))
76 return false;
77 if (!found_code && key_string_utf8 == key_string_utf8_tmp) {
78 *necessary_modifiers = kShiftKeyModifierMask;
79 found_code = true;
81 if (found_code) {
82 *key_code = code;
83 break;
86 return found_code;