[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 5 | #include "content/test/mock_keyboard_driver_win.h" |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 6 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <string.h> |
| 9 | |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 10 | #include "base/logging.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 12 | #include "content/test/mock_keyboard.h" |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 13 | |
[email protected] | 2dbcad1c | 2012-10-30 00:20:09 | [diff] [blame] | 14 | namespace content { |
| 15 | |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 16 | MockKeyboardDriverWin::MockKeyboardDriverWin() { |
| 17 | // Save the keyboard layout and status of the application. |
| 18 | // This class changes the keyboard layout and status of this application. |
| 19 | // This change may break succeeding tests. To prevent this possible break, we |
| 20 | // should save the layout and status here to restore when this instance is |
| 21 | // destroyed. |
| 22 | original_keyboard_layout_ = GetKeyboardLayout(0); |
[email protected] | 9db40b68 | 2009-05-29 00:39:59 | [diff] [blame] | 23 | active_keyboard_layout_ = original_keyboard_layout_; |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 24 | GetKeyboardState(&original_keyboard_states_[0]); |
| 25 | |
[email protected] | 9db40b68 | 2009-05-29 00:39:59 | [diff] [blame] | 26 | const UINT num_keyboard_layouts = GetKeyboardLayoutList(0, NULL); |
| 27 | DCHECK(num_keyboard_layouts > 0); |
| 28 | |
| 29 | orig_keyboard_layouts_list_.resize(num_keyboard_layouts); |
| 30 | GetKeyboardLayoutList(num_keyboard_layouts, &orig_keyboard_layouts_list_[0]); |
| 31 | |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 32 | memset(&keyboard_states_[0], 0, sizeof(keyboard_states_)); |
| 33 | } |
| 34 | |
| 35 | MockKeyboardDriverWin::~MockKeyboardDriverWin() { |
| 36 | // Unload the keyboard-layout driver, restore the keyboard state, and reset |
| 37 | // the keyboard layout for succeeding tests. |
[email protected] | 9db40b68 | 2009-05-29 00:39:59 | [diff] [blame] | 38 | MaybeUnloadActiveLayout(); |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 39 | SetKeyboardState(&original_keyboard_states_[0]); |
| 40 | ActivateKeyboardLayout(original_keyboard_layout_, KLF_RESET); |
| 41 | } |
| 42 | |
[email protected] | 9db40b68 | 2009-05-29 00:39:59 | [diff] [blame] | 43 | void MockKeyboardDriverWin::MaybeUnloadActiveLayout() { |
| 44 | // Workaround for http://crbug.com/12093 |
| 45 | // Only unload a keyboard layout if it was loaded by this mock driver. |
| 46 | // Contrary to the documentation on MSDN unloading a keyboard layout |
| 47 | // previously loaded by the system causes that layout to stop working. |
| 48 | // We have confirmation of this behavior on XP & Vista. |
| 49 | for (size_t i = 0; i < orig_keyboard_layouts_list_.size(); ++i) { |
| 50 | if (orig_keyboard_layouts_list_[i] == active_keyboard_layout_) |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // If we got here, this keyboard layout wasn't loaded by the system so it's |
| 55 | // safe to unload it ourselve's. |
| 56 | UnloadKeyboardLayout(active_keyboard_layout_); |
| 57 | active_keyboard_layout_ = original_keyboard_layout_; |
| 58 | } |
| 59 | |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 60 | bool MockKeyboardDriverWin::SetLayout(int layout) { |
| 61 | // Unload the current keyboard-layout driver and load a new keyboard-layout |
| 62 | // driver for mapping a virtual key-code to a Unicode character. |
[email protected] | 9db40b68 | 2009-05-29 00:39:59 | [diff] [blame] | 63 | MaybeUnloadActiveLayout(); |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 64 | |
| 65 | // Scan the mapping table and retrieve a Language ID for the input layout. |
| 66 | // Load the keyboard-layout driver when we find a Language ID. |
| 67 | // This Language IDs are copied from the registry |
| 68 | // "HKLM\SYSTEM\CurrentControlSet\Control\Keyboard layouts". |
| 69 | // TODO(hbono): Add more keyboard-layout drivers. |
| 70 | static const struct { |
| 71 | const wchar_t* language; |
| 72 | MockKeyboard::Layout keyboard_layout; |
| 73 | } kLanguageIDs[] = { |
| 74 | {L"00000401", MockKeyboard::LAYOUT_ARABIC}, |
| 75 | {L"00000402", MockKeyboard::LAYOUT_BULGARIAN}, |
| 76 | {L"00000404", MockKeyboard::LAYOUT_CHINESE_TRADITIONAL}, |
| 77 | {L"00000405", MockKeyboard::LAYOUT_CZECH}, |
| 78 | {L"00000406", MockKeyboard::LAYOUT_DANISH}, |
| 79 | {L"00000407", MockKeyboard::LAYOUT_GERMAN}, |
| 80 | {L"00000408", MockKeyboard::LAYOUT_GREEK}, |
| 81 | {L"00000409", MockKeyboard::LAYOUT_UNITED_STATES}, |
| 82 | {L"0000040a", MockKeyboard::LAYOUT_SPANISH}, |
| 83 | {L"0000040b", MockKeyboard::LAYOUT_FINNISH}, |
| 84 | {L"0000040c", MockKeyboard::LAYOUT_FRENCH}, |
| 85 | {L"0000040d", MockKeyboard::LAYOUT_HEBREW}, |
| 86 | {L"0000040e", MockKeyboard::LAYOUT_HUNGARIAN}, |
| 87 | {L"00000410", MockKeyboard::LAYOUT_ITALIAN}, |
| 88 | {L"00000411", MockKeyboard::LAYOUT_JAPANESE}, |
| 89 | {L"00000412", MockKeyboard::LAYOUT_KOREAN}, |
| 90 | {L"00000415", MockKeyboard::LAYOUT_POLISH}, |
| 91 | {L"00000416", MockKeyboard::LAYOUT_PORTUGUESE_BRAZILIAN}, |
| 92 | {L"00000418", MockKeyboard::LAYOUT_ROMANIAN}, |
| 93 | {L"00000419", MockKeyboard::LAYOUT_RUSSIAN}, |
| 94 | {L"0000041a", MockKeyboard::LAYOUT_CROATIAN}, |
| 95 | {L"0000041b", MockKeyboard::LAYOUT_SLOVAK}, |
| 96 | {L"0000041e", MockKeyboard::LAYOUT_THAI}, |
| 97 | {L"0000041d", MockKeyboard::LAYOUT_SWEDISH}, |
| 98 | {L"0000041f", MockKeyboard::LAYOUT_TURKISH_Q}, |
| 99 | {L"0000042a", MockKeyboard::LAYOUT_VIETNAMESE}, |
| 100 | {L"00000439", MockKeyboard::LAYOUT_DEVANAGARI_INSCRIPT}, |
| 101 | {L"00000816", MockKeyboard::LAYOUT_PORTUGUESE}, |
| 102 | {L"00001409", MockKeyboard::LAYOUT_UNITED_STATES_DVORAK}, |
| 103 | {L"00001009", MockKeyboard::LAYOUT_CANADIAN_FRENCH}, |
| 104 | }; |
| 105 | |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 106 | for (size_t i = 0; i < arraysize(kLanguageIDs); ++i) { |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 107 | if (layout == kLanguageIDs[i].keyboard_layout) { |
[email protected] | 9db40b68 | 2009-05-29 00:39:59 | [diff] [blame] | 108 | HKL new_keyboard_layout = LoadKeyboardLayout(kLanguageIDs[i].language, |
| 109 | KLF_ACTIVATE); |
| 110 | // loaded_keyboard_layout_ must always have a valid keyboard handle |
| 111 | // so we only assign upon success. |
| 112 | if (new_keyboard_layout) { |
| 113 | active_keyboard_layout_ = new_keyboard_layout; |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | return false; |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | // Return false if there are not any matching drivers. |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | bool MockKeyboardDriverWin::SetModifiers(int modifiers) { |
| 126 | // Over-write the keyboard status with our modifier-key status. |
| 127 | // WebInputEventFactory::keyboardEvent() uses GetKeyState() to retrive |
| 128 | // modifier-key status. So, we update the modifier-key status with this |
| 129 | // SetKeyboardState() call before creating NativeWebKeyboardEvent |
| 130 | // instances. |
| 131 | memset(&keyboard_states_[0], 0, sizeof(keyboard_states_)); |
| 132 | static const struct { |
| 133 | int key_code; |
| 134 | int mask; |
| 135 | } kModifierMasks[] = { |
| 136 | {VK_SHIFT, MockKeyboard::LEFT_SHIFT | MockKeyboard::RIGHT_SHIFT}, |
| 137 | {VK_CONTROL, MockKeyboard::LEFT_CONTROL | MockKeyboard::RIGHT_CONTROL}, |
| 138 | {VK_MENU, MockKeyboard::LEFT_ALT | MockKeyboard::RIGHT_ALT}, |
| 139 | {VK_LSHIFT, MockKeyboard::LEFT_SHIFT}, |
| 140 | {VK_LCONTROL, MockKeyboard::LEFT_CONTROL}, |
| 141 | {VK_LMENU, MockKeyboard::LEFT_ALT}, |
| 142 | {VK_RSHIFT, MockKeyboard::RIGHT_SHIFT}, |
| 143 | {VK_RCONTROL, MockKeyboard::RIGHT_CONTROL}, |
| 144 | {VK_RMENU, MockKeyboard::RIGHT_ALT}, |
| 145 | }; |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 146 | for (size_t i = 0; i < arraysize(kModifierMasks); ++i) { |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 147 | const int kKeyDownMask = 0x80; |
| 148 | if (modifiers & kModifierMasks[i].mask) |
| 149 | keyboard_states_[kModifierMasks[i].key_code] = kKeyDownMask; |
| 150 | } |
| 151 | SetKeyboardState(&keyboard_states_[0]); |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | int MockKeyboardDriverWin::GetCharacters(int key_code, |
| 157 | std::wstring* output) { |
| 158 | // Retrieve Unicode characters composed from the input key-code and |
| 159 | // the mofifiers. |
| 160 | CHECK(output); |
| 161 | wchar_t code[16]; |
| 162 | int length = ToUnicodeEx(key_code, MapVirtualKey(key_code, 0), |
[email protected] | 576269a70 | 2010-06-01 21:56:17 | [diff] [blame] | 163 | &keyboard_states_[0], &code[0], arraysize(code), 0, |
[email protected] | 9db40b68 | 2009-05-29 00:39:59 | [diff] [blame] | 164 | active_keyboard_layout_); |
[email protected] | 8b04832 | 2009-05-11 04:41:21 | [diff] [blame] | 165 | if (length > 0) |
| 166 | output->assign(code); |
| 167 | return length; |
| 168 | } |
[email protected] | 2dbcad1c | 2012-10-30 00:20:09 | [diff] [blame] | 169 | |
| 170 | } // namespace content |