OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 "ui/events/keycodes/platform_key_map_win.h" | 5 #include "ui/events/keycodes/platform_key_map_win.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "ui/events/event_constants.h" | 10 #include "ui/events/event_constants.h" |
11 #include "ui/events/keycodes/dom/dom_code.h" | 11 #include "ui/events/keycodes/dom/dom_code.h" |
12 #include "ui/events/keycodes/dom/dom_key.h" | 12 #include "ui/events/keycodes/dom/dom_key.h" |
13 #include "ui/events/keycodes/dom/keycode_converter.h" | 13 #include "ui/events/keycodes/dom/keycode_converter.h" |
14 #include "ui/events/keycodes/dom_us_layout_data.h" | 14 #include "ui/events/keycodes/dom_us_layout_data.h" |
15 | 15 |
16 namespace ui { | 16 namespace ui { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 enum Layout { | 20 enum Layout { |
21 LAYOUT_US, | 21 LAYOUT_US, |
22 LAYOUT_FR, | 22 LAYOUT_FR, |
23 LAYOUT_KR, | 23 LAYOUT_KR, |
| 24 LAYOUT_JP, |
24 }; | 25 }; |
25 | 26 |
26 // |LoadKeyboardLayout()| ensures the locale to be loaded into the system | 27 // |LoadKeyboardLayout()| ensures the locale to be loaded into the system |
27 // (Similar to temporarily adding a locale in Control Panel), otherwise | 28 // (Similar to temporarily adding a locale in Control Panel), otherwise |
28 // |ToUnicodeEx()| will fall-back to the default locale. | 29 // |ToUnicodeEx()| will fall-back to the default locale. |
29 // See MSDN LoadKeyboardLayout(): | 30 // See MSDN LoadKeyboardLayout(): |
30 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms646305(v=vs.85).as
px | 31 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms646305(v=vs.85).as
px |
31 // And language constants and strings: | 32 // And language constants and strings: |
32 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).as
px | 33 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).as
px |
33 HKL GetInputLocale(Layout layout) { | 34 HKL GetInputLocale(Layout layout) { |
34 switch (layout) { | 35 switch (layout) { |
35 case LAYOUT_US: | 36 case LAYOUT_US: |
36 return ::LoadKeyboardLayout(L"00000409", KLF_ACTIVATE); | 37 return ::LoadKeyboardLayout(L"00000409", KLF_ACTIVATE); |
37 case LAYOUT_FR: | 38 case LAYOUT_FR: |
38 return ::LoadKeyboardLayout(L"0000040c", KLF_ACTIVATE); | 39 return ::LoadKeyboardLayout(L"0000040c", KLF_ACTIVATE); |
39 case LAYOUT_KR: | 40 case LAYOUT_KR: |
40 // |LoadKeyboardLayout(L"00000412", KLF_ACTIVATE)| returns the correct | 41 // |LoadKeyboardLayout(L"00000412", KLF_ACTIVATE)| returns the correct |
41 // Korean locale, but it will fail on DrMemory tests. | 42 // Korean locale, but it will fail on DrMemory tests. |
42 // See https://crbug.com/612736#c6 | 43 // See https://crbug.com/612736#c6 |
43 // However we could bypass it since we are only testing non-printable keys | 44 // However we could bypass it since we are only testing non-printable keys |
44 // on Korean locale. | 45 // on Korean locale. |
45 // (This issue only happens on Korean and Japanese). | 46 // (This issue only happens on Korean and Japanese). |
46 return reinterpret_cast<HKL>(0x04120412); | 47 return reinterpret_cast<HKL>(0x04120412); |
| 48 case LAYOUT_JP: |
| 49 return reinterpret_cast<HKL>(0x04110411); |
47 default: | 50 default: |
48 return 0; | 51 return 0; |
49 } | 52 } |
50 } | 53 } |
51 | 54 |
52 struct TestKey { | 55 struct TestKey { |
53 KeyboardCode key_code; | 56 KeyboardCode key_code; |
54 const char* normal; | 57 const char* normal; |
55 const char* shift; | 58 const char* shift; |
56 const char* capslock; | 59 const char* capslock; |
57 const char* altgr; | 60 const char* altgr; |
58 const char* shift_capslock; | 61 const char* shift_capslock; |
59 const char* shift_altgr; | 62 const char* shift_altgr; |
60 const char* altgr_capslock; | 63 const char* altgr_capslock; |
61 }; | 64 }; |
62 | 65 |
63 } // anonymous namespace | 66 } // anonymous namespace |
64 | 67 |
65 class PlatformKeyMapTest : public testing::Test { | 68 class PlatformKeyMapTest : public testing::Test { |
66 public: | 69 public: |
67 PlatformKeyMapTest() {} | 70 PlatformKeyMapTest() {} |
68 ~PlatformKeyMapTest() override {} | 71 ~PlatformKeyMapTest() override {} |
69 | 72 |
70 void CheckKeyboardCodeToKeyString(const char* label, | 73 void CheckKeyboardCodeToKeyString(const char* label, |
71 const PlatformKeyMap& keymap, | 74 const PlatformKeyMap& keymap, |
72 const TestKey& test_case, | 75 const TestKey& test_case) { |
73 HKL layout) { | |
74 KeyboardCode key_code = test_case.key_code; | 76 KeyboardCode key_code = test_case.key_code; |
75 EXPECT_STREQ(test_case.normal, | 77 EXPECT_STREQ(test_case.normal, |
76 KeycodeConverter::DomKeyToKeyString( | 78 KeycodeConverter::DomKeyToKeyString( |
77 keymap.DomKeyFromKeyboardCodeImpl(key_code, EF_NONE)) | 79 keymap.DomKeyFromKeyboardCodeImpl(key_code, EF_NONE)) |
78 .c_str()) | 80 .c_str()) |
79 << label; | 81 << label; |
80 EXPECT_STREQ(test_case.shift, | 82 EXPECT_STREQ(test_case.shift, |
81 KeycodeConverter::DomKeyToKeyString( | 83 KeycodeConverter::DomKeyToKeyString( |
82 keymap.DomKeyFromKeyboardCodeImpl(key_code, EF_SHIFT_DOWN)) | 84 keymap.DomKeyFromKeyboardCodeImpl(key_code, EF_SHIFT_DOWN)) |
83 .c_str()) | 85 .c_str()) |
(...skipping 21 matching lines...) Expand all Loading... |
105 .c_str()) | 107 .c_str()) |
106 << label; | 108 << label; |
107 EXPECT_STREQ(test_case.altgr_capslock, | 109 EXPECT_STREQ(test_case.altgr_capslock, |
108 KeycodeConverter::DomKeyToKeyString( | 110 KeycodeConverter::DomKeyToKeyString( |
109 keymap.DomKeyFromKeyboardCodeImpl( | 111 keymap.DomKeyFromKeyboardCodeImpl( |
110 key_code, EF_ALTGR_DOWN | EF_CAPS_LOCK_ON)) | 112 key_code, EF_ALTGR_DOWN | EF_CAPS_LOCK_ON)) |
111 .c_str()) | 113 .c_str()) |
112 << label; | 114 << label; |
113 } | 115 } |
114 | 116 |
| 117 // Need this helper function to access private methods of |PlatformKeyMap|. |
115 DomKey DomKeyFromKeyboardCodeImpl(const PlatformKeyMap& keymap, | 118 DomKey DomKeyFromKeyboardCodeImpl(const PlatformKeyMap& keymap, |
116 KeyboardCode key_code, | 119 KeyboardCode key_code, |
117 int flags) { | 120 int flags) { |
118 return keymap.DomKeyFromKeyboardCodeImpl(key_code, flags); | 121 return keymap.DomKeyFromKeyboardCodeImpl(key_code, flags); |
119 } | 122 } |
120 | 123 |
121 private: | 124 private: |
122 DISALLOW_COPY_AND_ASSIGN(PlatformKeyMapTest); | 125 DISALLOW_COPY_AND_ASSIGN(PlatformKeyMapTest); |
123 }; | 126 }; |
124 | 127 |
125 TEST_F(PlatformKeyMapTest, USLayout) { | 128 TEST_F(PlatformKeyMapTest, USLayout) { |
126 HKL layout = GetInputLocale(LAYOUT_US); | 129 PlatformKeyMap keymap(GetInputLocale(LAYOUT_US)); |
127 PlatformKeyMap keymap(layout); | |
128 | 130 |
129 const TestKey kUSLayoutTestCases[] = { | 131 const TestKey kUSLayoutTestCases[] = { |
130 // n s c a sc sa ac | 132 // n s c a sc sa ac |
131 {VKEY_0, "0", ")", "0", "0", ")", ")", "0"}, | 133 {VKEY_0, "0", ")", "0", "0", ")", ")", "0"}, |
132 {VKEY_1, "1", "!", "1", "1", "!", "!", "1"}, | 134 {VKEY_1, "1", "!", "1", "1", "!", "!", "1"}, |
133 {VKEY_2, "2", "@", "2", "2", "@", "@", "2"}, | 135 {VKEY_2, "2", "@", "2", "2", "@", "@", "2"}, |
134 {VKEY_3, "3", "#", "3", "3", "#", "#", "3"}, | 136 {VKEY_3, "3", "#", "3", "3", "#", "#", "3"}, |
135 {VKEY_4, "4", "$", "4", "4", "$", "$", "4"}, | 137 {VKEY_4, "4", "$", "4", "4", "$", "$", "4"}, |
136 {VKEY_5, "5", "%", "5", "5", "%", "%", "5"}, | 138 {VKEY_5, "5", "%", "5", "5", "%", "%", "5"}, |
137 {VKEY_6, "6", "^", "6", "6", "^", "^", "6"}, | 139 {VKEY_6, "6", "^", "6", "6", "^", "^", "6"}, |
(...skipping 22 matching lines...) Expand all Loading... |
160 {VKEY_T, "t", "T", "T", "t", "t", "T", "T"}, | 162 {VKEY_T, "t", "T", "T", "t", "t", "T", "T"}, |
161 {VKEY_U, "u", "U", "U", "u", "u", "U", "U"}, | 163 {VKEY_U, "u", "U", "U", "u", "u", "U", "U"}, |
162 {VKEY_V, "v", "V", "V", "v", "v", "V", "V"}, | 164 {VKEY_V, "v", "V", "V", "v", "v", "V", "V"}, |
163 {VKEY_W, "w", "W", "W", "w", "w", "W", "W"}, | 165 {VKEY_W, "w", "W", "W", "w", "w", "W", "W"}, |
164 {VKEY_X, "x", "X", "X", "x", "x", "X", "X"}, | 166 {VKEY_X, "x", "X", "X", "x", "x", "X", "X"}, |
165 {VKEY_Y, "y", "Y", "Y", "y", "y", "Y", "Y"}, | 167 {VKEY_Y, "y", "Y", "Y", "y", "y", "Y", "Y"}, |
166 {VKEY_Z, "z", "Z", "Z", "z", "z", "Z", "Z"}, | 168 {VKEY_Z, "z", "Z", "Z", "z", "z", "Z", "Z"}, |
167 }; | 169 }; |
168 | 170 |
169 for (const auto& test_case : kUSLayoutTestCases) { | 171 for (const auto& test_case : kUSLayoutTestCases) { |
170 CheckKeyboardCodeToKeyString("USLayout", keymap, test_case, layout); | 172 CheckKeyboardCodeToKeyString("USLayout", keymap, test_case); |
171 } | 173 } |
172 } | 174 } |
173 | 175 |
174 TEST_F(PlatformKeyMapTest, FRLayout) { | 176 TEST_F(PlatformKeyMapTest, FRLayout) { |
175 HKL layout = GetInputLocale(LAYOUT_FR); | 177 PlatformKeyMap keymap(GetInputLocale(LAYOUT_FR)); |
176 PlatformKeyMap keymap(layout); | |
177 | 178 |
178 const TestKey kFRLayoutTestCases[] = { | 179 const TestKey kFRLayoutTestCases[] = { |
179 // n s c a sc sa ac | 180 // n s c a sc sa ac |
180 {VKEY_0, "à", "0", "0", "@", "à", "0", "@"}, | 181 {VKEY_0, "à", "0", "0", "@", "à", "0", "@"}, |
181 {VKEY_1, "&", "1", "1", "&", "&", "1", "1"}, | 182 {VKEY_1, "&", "1", "1", "&", "&", "1", "1"}, |
182 {VKEY_2, "é", "2", "2", "Dead", "é", "2", "Dead"}, | 183 {VKEY_2, "é", "2", "2", "Dead", "é", "2", "Dead"}, |
183 {VKEY_3, "\"", "3", "3", "#", "\"", "3", "#"}, | 184 {VKEY_3, "\"", "3", "3", "#", "\"", "3", "#"}, |
184 {VKEY_4, "\'", "4", "4", "{", "\'", "4", "{"}, | 185 {VKEY_4, "\'", "4", "4", "{", "\'", "4", "{"}, |
185 {VKEY_5, "(", "5", "5", "[", "(", "5", "["}, | 186 {VKEY_5, "(", "5", "5", "[", "(", "5", "["}, |
186 {VKEY_6, "-", "6", "6", "|", "-", "6", "|"}, | 187 {VKEY_6, "-", "6", "6", "|", "-", "6", "|"}, |
(...skipping 22 matching lines...) Expand all Loading... |
209 {VKEY_T, "t", "T", "T", "t", "t", "T", "T"}, | 210 {VKEY_T, "t", "T", "T", "t", "t", "T", "T"}, |
210 {VKEY_U, "u", "U", "U", "u", "u", "U", "U"}, | 211 {VKEY_U, "u", "U", "U", "u", "u", "U", "U"}, |
211 {VKEY_V, "v", "V", "V", "v", "v", "V", "V"}, | 212 {VKEY_V, "v", "V", "V", "v", "v", "V", "V"}, |
212 {VKEY_W, "w", "W", "W", "w", "w", "W", "W"}, | 213 {VKEY_W, "w", "W", "W", "w", "w", "W", "W"}, |
213 {VKEY_X, "x", "X", "X", "x", "x", "X", "X"}, | 214 {VKEY_X, "x", "X", "X", "x", "x", "X", "X"}, |
214 {VKEY_Y, "y", "Y", "Y", "y", "y", "Y", "Y"}, | 215 {VKEY_Y, "y", "Y", "Y", "y", "y", "Y", "Y"}, |
215 {VKEY_Z, "z", "Z", "Z", "z", "z", "Z", "Z"}, | 216 {VKEY_Z, "z", "Z", "Z", "z", "z", "Z", "Z"}, |
216 }; | 217 }; |
217 | 218 |
218 for (const auto& test_case : kFRLayoutTestCases) { | 219 for (const auto& test_case : kFRLayoutTestCases) { |
219 CheckKeyboardCodeToKeyString("FRLayout", keymap, test_case, layout); | 220 CheckKeyboardCodeToKeyString("FRLayout", keymap, test_case); |
220 } | 221 } |
221 } | 222 } |
222 | 223 |
223 TEST_F(PlatformKeyMapTest, NumPad) { | 224 TEST_F(PlatformKeyMapTest, NumPad) { |
224 HKL layout = GetInputLocale(LAYOUT_US); | 225 PlatformKeyMap keymap(GetInputLocale(LAYOUT_US)); |
225 PlatformKeyMap keymap(layout); | |
226 | 226 |
227 const struct TestCase { | 227 const struct TestCase { |
228 KeyboardCode key_code; | 228 KeyboardCode key_code; |
229 DomKey key; | 229 DomKey key; |
230 } kNumPadTestCases[] = { | 230 } kNumPadTestCases[] = { |
231 {VKEY_NUMPAD0, DomKey::FromCharacter('0')}, | 231 {VKEY_NUMPAD0, DomKey::FromCharacter('0')}, |
232 {VKEY_NUMPAD1, DomKey::FromCharacter('1')}, | 232 {VKEY_NUMPAD1, DomKey::FromCharacter('1')}, |
233 {VKEY_NUMPAD2, DomKey::FromCharacter('2')}, | 233 {VKEY_NUMPAD2, DomKey::FromCharacter('2')}, |
234 {VKEY_NUMPAD3, DomKey::FromCharacter('3')}, | 234 {VKEY_NUMPAD3, DomKey::FromCharacter('3')}, |
235 {VKEY_NUMPAD4, DomKey::FromCharacter('4')}, | 235 {VKEY_NUMPAD4, DomKey::FromCharacter('4')}, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 EXPECT_EQ(test_case.dom_key, | 303 EXPECT_EQ(test_case.dom_key, |
304 DomKeyFromKeyboardCodeImpl(keymap, key_code, | 304 DomKeyFromKeyboardCodeImpl(keymap, key_code, |
305 EF_ALTGR_DOWN | EF_CONTROL_DOWN)) | 305 EF_ALTGR_DOWN | EF_CONTROL_DOWN)) |
306 << key_code << ", " << scan_code; | 306 << key_code << ", " << scan_code; |
307 } | 307 } |
308 } | 308 } |
309 | 309 |
310 TEST_F(PlatformKeyMapTest, KoreanSpecificKeys) { | 310 TEST_F(PlatformKeyMapTest, KoreanSpecificKeys) { |
311 const struct TestCase { | 311 const struct TestCase { |
312 KeyboardCode key_code; | 312 KeyboardCode key_code; |
313 DomKey key; | 313 DomKey kr_key; |
| 314 DomKey us_key; |
314 } kKoreanTestCases[] = { | 315 } kKoreanTestCases[] = { |
315 {VKEY_HANGUL, DomKey::HANGUL_MODE}, {VKEY_HANJA, DomKey::HANJA_MODE}, | 316 {VKEY_HANGUL, DomKey::HANGUL_MODE, DomKey::UNIDENTIFIED}, |
| 317 {VKEY_HANJA, DomKey::HANJA_MODE, DomKey::UNIDENTIFIED}, |
316 }; | 318 }; |
317 | 319 |
318 // US English should not return values for these keys. | 320 PlatformKeyMap us_keymap(GetInputLocale(LAYOUT_US)); |
319 HKL us_layout = GetInputLocale(LAYOUT_US); | 321 PlatformKeyMap kr_keymap(GetInputLocale(LAYOUT_KR)); |
320 PlatformKeyMap us_keymap(us_layout); | |
321 for (const auto& test_case : kKoreanTestCases) { | 322 for (const auto& test_case : kKoreanTestCases) { |
322 EXPECT_EQ(DomKey::NONE, DomKeyFromKeyboardCodeImpl( | 323 EXPECT_EQ(test_case.us_key, DomKeyFromKeyboardCodeImpl( |
323 us_keymap, test_case.key_code, EF_NONE)) | 324 us_keymap, test_case.key_code, EF_NONE)) |
324 << test_case.key_code; | 325 << test_case.key_code; |
325 } | 326 EXPECT_EQ(test_case.kr_key, DomKeyFromKeyboardCodeImpl( |
326 | 327 kr_keymap, test_case.key_code, EF_NONE)) |
327 // Korean layout should return specific DomKey. | |
328 HKL ko_layout = GetInputLocale(LAYOUT_KR); | |
329 PlatformKeyMap ko_keymap(ko_layout); | |
330 for (const auto& test_case : kKoreanTestCases) { | |
331 EXPECT_EQ(test_case.key, DomKeyFromKeyboardCodeImpl( | |
332 ko_keymap, test_case.key_code, EF_NONE)) | |
333 << test_case.key_code; | 328 << test_case.key_code; |
334 } | 329 } |
335 } | 330 } |
| 331 |
| 332 TEST_F(PlatformKeyMapTest, JapaneseSpecificKeys) { |
| 333 const struct TestCase { |
| 334 KeyboardCode key_code; |
| 335 DomKey jp_key; |
| 336 DomKey us_key; |
| 337 } kJapaneseTestCases[] = { |
| 338 {VKEY_KANA, DomKey::KANA_MODE, DomKey::UNIDENTIFIED}, |
| 339 {VKEY_KANJI, DomKey::KANJI_MODE, DomKey::UNIDENTIFIED}, |
| 340 {VKEY_OEM_ATTN, DomKey::ALPHANUMERIC, DomKey::UNIDENTIFIED}, |
| 341 {VKEY_OEM_FINISH, DomKey::KATAKANA, DomKey::UNIDENTIFIED}, |
| 342 {VKEY_OEM_COPY, DomKey::HIRAGANA, DomKey::UNIDENTIFIED}, |
| 343 {VKEY_DBE_SBCSCHAR, DomKey::HANKAKU, DomKey::UNIDENTIFIED}, |
| 344 {VKEY_DBE_DBCSCHAR, DomKey::ZENKAKU, DomKey::UNIDENTIFIED}, |
| 345 {VKEY_OEM_BACKTAB, DomKey::ROMAJI, DomKey::UNIDENTIFIED}, |
| 346 {VKEY_ATTN, DomKey::KANA_MODE, DomKey::ATTN}, |
| 347 }; |
| 348 |
| 349 PlatformKeyMap us_keymap(GetInputLocale(LAYOUT_US)); |
| 350 PlatformKeyMap jp_keymap(GetInputLocale(LAYOUT_JP)); |
| 351 for (const auto& test_case : kJapaneseTestCases) { |
| 352 EXPECT_EQ(test_case.us_key, DomKeyFromKeyboardCodeImpl( |
| 353 us_keymap, test_case.key_code, EF_NONE)) |
| 354 << test_case.key_code; |
| 355 EXPECT_EQ(test_case.jp_key, DomKeyFromKeyboardCodeImpl( |
| 356 jp_keymap, test_case.key_code, EF_NONE)) |
| 357 << test_case.key_code; |
| 358 } |
| 359 } |
336 | 360 |
337 } // namespace ui | 361 } // namespace ui |
OLD | NEW |