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/keyboard_code_conversion.h"
14 #include "ui/events/ozone/layout/keyboard_layout_engine.h"
15 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
17 bool ConvertKeyCodeToText(ui::KeyboardCode key_code
,
20 std::string
* error_msg
) {
21 ui::KeyboardLayoutEngine
* keyboard_layout_engine
=
22 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine();
23 ui::DomCode dom_code
= ui::UsLayoutKeyboardCodeToDomCode(key_code
);
24 int event_flags
= ui::EF_NONE
;
26 // Chrome OS keyboards don't have meta or num lock keys, so these modifier
27 // masks are ignored. Only handle alt, ctrl and shift.
28 if (modifiers
& kAltKeyModifierMask
)
29 event_flags
|= ui::EF_ALT_DOWN
;
30 if (modifiers
& kControlKeyModifierMask
)
31 event_flags
|= ui::EF_CONTROL_DOWN
;
32 if (modifiers
& kShiftKeyModifierMask
)
33 event_flags
|= ui::EF_SHIFT_DOWN
;
35 ui::DomKey dom_key_ignored
;
36 base::char16 str
[2] = {'\0'};
37 ui::KeyboardCode key_code_ignored
;
38 uint32 platform_keycode_ignored
;
40 if (!keyboard_layout_engine
->Lookup(dom_code
, event_flags
, &dom_key_ignored
,
41 &str
[0], &key_code_ignored
,
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 (!base::UTF16ToUTF8(str
, base::c16len(str
), text
)) {
50 *error_msg
= base::StringPrintf(
51 "unicode conversion failed for keycode %d with modifiers 0x%x",
59 bool ConvertCharToKeyCode(base::char16 key
,
60 ui::KeyboardCode
* key_code
,
61 int* necessary_modifiers
,
62 std::string
* error_msg
) {
63 base::string16 key_string
;
64 key_string
.push_back(key
);
65 std::string key_string_utf8
= base::UTF16ToUTF8(key_string
);
66 bool found_code
= false;
67 *error_msg
= std::string();
68 // There doesn't seem to be a way to get a CrOS key code for a given unicode
69 // character. So here we check every key code to see if it produces the
70 // right character, as we do on Mac (see keycode_text_conversion_mac.mm).
71 for (int i
= 0; i
< 256; ++i
) {
72 ui::KeyboardCode code
= static_cast<ui::KeyboardCode
>(i
);
73 // Skip the numpad keys.
74 if (code
>= ui::VKEY_NUMPAD0
&& code
<= ui::VKEY_DIVIDE
)
76 std::string key_string
;
77 if (!ConvertKeyCodeToText(code
, 0, &key_string
, error_msg
))
79 found_code
= key_string_utf8
== key_string
;
80 std::string key_string_utf8_tmp
;
81 if (!ConvertKeyCodeToText(code
, kShiftKeyModifierMask
, &key_string_utf8_tmp
,
84 if (!found_code
&& key_string_utf8
== key_string_utf8_tmp
) {
85 *necessary_modifiers
= kShiftKeyModifierMask
;