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/logging.h"
6 #include "base/strings/string16.h"
7 #include "base/strings/stringprintf.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
,
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
;
37 ui::KeyboardCode key_code_ignored
;
38 uint32 platform_keycode_ignored
;
40 if (!keyboard_layout_engine
->Lookup(dom_code
, event_flags
, &dom_key
,
42 &platform_keycode_ignored
)) {
43 // Key codes like ui::VKEY_UNKNOWN need to be mapped to the empty string, so
44 // even if the lookup fails we still need to return true here.
45 *text
= std::string();
49 if (!dom_key
.IsCharacter()) {
50 *error_msg
= base::StringPrintf(
51 "unicode conversion failed for keycode %d with modifiers 0x%x",
56 *text
= ui::KeycodeConverter::DomKeyToKeyString(dom_key
);
60 bool ConvertCharToKeyCode(base::char16 key
,
61 ui::KeyboardCode
* key_code
,
62 int* necessary_modifiers
,
63 std::string
* error_msg
) {
64 base::string16 key_string
;
65 key_string
.push_back(key
);
66 std::string key_string_utf8
= base::UTF16ToUTF8(key_string
);
67 bool found_code
= false;
68 *error_msg
= std::string();
69 // There doesn't seem to be a way to get a CrOS key code for a given unicode
70 // character. So here we check every key code to see if it produces the
71 // right character, as we do on Mac (see keycode_text_conversion_mac.mm).
72 for (int i
= 0; i
< 256; ++i
) {
73 ui::KeyboardCode code
= static_cast<ui::KeyboardCode
>(i
);
74 // Skip the numpad keys.
75 if (code
>= ui::VKEY_NUMPAD0
&& code
<= ui::VKEY_DIVIDE
)
77 std::string key_string
;
78 if (!ConvertKeyCodeToText(code
, 0, &key_string
, error_msg
))
80 found_code
= key_string_utf8
== key_string
;
81 std::string key_string_utf8_tmp
;
82 if (!ConvertKeyCodeToText(code
, kShiftKeyModifierMask
, &key_string_utf8_tmp
,
85 if (!found_code
&& key_string_utf8
== key_string_utf8_tmp
) {
86 *necessary_modifiers
= kShiftKeyModifierMask
;