OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/strings/string16.h" |
| 7 #include "base/strings/stringprintf.h" |
6 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
7 #include "chrome/test/chromedriver/chrome/ui_events.h" | 9 #include "chrome/test/chromedriver/chrome/ui_events.h" |
8 #include "chrome/test/chromedriver/keycode_text_conversion.h" | 10 #include "chrome/test/chromedriver/keycode_text_conversion.h" |
| 11 #include "ui/events/event_constants.h" |
| 12 #include "ui/events/keycodes/dom3/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" |
9 | 16 |
10 // TODO(arunprasadr) Implement these functions properly for ozone platforms. | 17 bool ConvertKeyCodeToText(ui::KeyboardCode key_code, |
11 bool ConvertKeyCodeToText( | 18 int modifiers, |
12 ui::KeyboardCode key_code, int modifiers, std::string* text, | 19 std::string* text, |
13 std::string* error_msg) { | 20 std::string* error_msg) { |
14 *text = std::string(); | 21 ui::KeyboardLayoutEngine* keyboard_layout_engine = |
15 *error_msg = std::string("Not Implemented"); | 22 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine(); |
16 NOTIMPLEMENTED(); | 23 ui::DomCode dom_code = ui::UsLayoutKeyboardCodeToDomCode(key_code); |
17 return false; | 24 int event_flags = ui::EF_NONE; |
| 25 |
| 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; |
| 34 |
| 35 ui::DomKey dom_key_ignored; |
| 36 base::char16 str[2] = {'\0'}; |
| 37 ui::KeyboardCode key_code_ignored; |
| 38 uint32 platform_keycode_ignored; |
| 39 |
| 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(); |
| 46 return true; |
| 47 } |
| 48 |
| 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", |
| 52 key_code, modifiers); |
| 53 return false; |
| 54 } |
| 55 |
| 56 return true; |
18 } | 57 } |
19 | 58 |
20 bool ConvertCharToKeyCode( | 59 bool ConvertCharToKeyCode(base::char16 key, |
21 base::char16 key, ui::KeyboardCode* key_code, int *necessary_modifiers, | 60 ui::KeyboardCode* key_code, |
22 std::string* error_msg) { | 61 int* necessary_modifiers, |
23 *error_msg = std::string("Not Implemented"); | 62 std::string* error_msg) { |
24 *necessary_modifiers = 0; | 63 base::string16 key_string; |
25 NOTIMPLEMENTED(); | 64 key_string.push_back(key); |
26 return false; | 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) |
| 75 continue; |
| 76 std::string key_string; |
| 77 if (!ConvertKeyCodeToText(code, 0, &key_string, error_msg)) |
| 78 return false; |
| 79 found_code = key_string_utf8 == key_string; |
| 80 std::string key_string_utf8_tmp; |
| 81 if (!ConvertKeyCodeToText(code, kShiftKeyModifierMask, &key_string_utf8_tmp, |
| 82 error_msg)) |
| 83 return false; |
| 84 if (!found_code && key_string_utf8 == key_string_utf8_tmp) { |
| 85 *necessary_modifiers = kShiftKeyModifierMask; |
| 86 found_code = true; |
| 87 } |
| 88 if (found_code) { |
| 89 *key_code = code; |
| 90 break; |
| 91 } |
| 92 } |
| 93 return found_code; |
27 } | 94 } |
OLD | NEW |