blob: 363a3d19058ba4e0be2f63f49af2935573aabe94 [file] [log] [blame]
[email protected]c6d068ff2011-10-14 17:28:231// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]8b048322009-05-11 04:41:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]c6d068ff2011-10-14 17:28:235#include "content/test/mock_keyboard_driver_win.h"
[email protected]8b048322009-05-11 04:41:216
avi66a07722015-12-25 23:38:127#include <stddef.h>
8#include <string.h>
9
[email protected]8b048322009-05-11 04:41:2110#include "base/logging.h"
avi66a07722015-12-25 23:38:1211#include "base/macros.h"
[email protected]c6d068ff2011-10-14 17:28:2312#include "content/test/mock_keyboard.h"
[email protected]8b048322009-05-11 04:41:2113
[email protected]2dbcad1c2012-10-30 00:20:0914namespace content {
15
[email protected]8b048322009-05-11 04:41:2116MockKeyboardDriverWin::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]9db40b682009-05-29 00:39:5923 active_keyboard_layout_ = original_keyboard_layout_;
[email protected]8b048322009-05-11 04:41:2124 GetKeyboardState(&original_keyboard_states_[0]);
25
[email protected]9db40b682009-05-29 00:39:5926 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]8b048322009-05-11 04:41:2132 memset(&keyboard_states_[0], 0, sizeof(keyboard_states_));
33}
34
35MockKeyboardDriverWin::~MockKeyboardDriverWin() {
36 // Unload the keyboard-layout driver, restore the keyboard state, and reset
37 // the keyboard layout for succeeding tests.
[email protected]9db40b682009-05-29 00:39:5938 MaybeUnloadActiveLayout();
[email protected]8b048322009-05-11 04:41:2139 SetKeyboardState(&original_keyboard_states_[0]);
40 ActivateKeyboardLayout(original_keyboard_layout_, KLF_RESET);
41}
42
[email protected]9db40b682009-05-29 00:39:5943void 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]8b048322009-05-11 04:41:2160bool 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]9db40b682009-05-29 00:39:5963 MaybeUnloadActiveLayout();
[email protected]8b048322009-05-11 04:41:2164
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
viettrungluu2dfaba72014-10-16 05:30:25106 for (size_t i = 0; i < arraysize(kLanguageIDs); ++i) {
[email protected]8b048322009-05-11 04:41:21107 if (layout == kLanguageIDs[i].keyboard_layout) {
[email protected]9db40b682009-05-29 00:39:59108 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]8b048322009-05-11 04:41:21118 }
119 }
120
121 // Return false if there are not any matching drivers.
122 return false;
123}
124
125bool 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 };
viettrungluu2dfaba72014-10-16 05:30:25146 for (size_t i = 0; i < arraysize(kModifierMasks); ++i) {
[email protected]8b048322009-05-11 04:41:21147 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
156int 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]576269a702010-06-01 21:56:17163 &keyboard_states_[0], &code[0], arraysize(code), 0,
[email protected]9db40b682009-05-29 00:39:59164 active_keyboard_layout_);
[email protected]8b048322009-05-11 04:41:21165 if (length > 0)
166 output->assign(code);
167 return length;
168}
[email protected]2dbcad1c2012-10-30 00:20:09169
170} // namespace content