| 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 #include "ui/events/test/keyboard_layout.h" |
| 15 | 16 |
| 16 namespace ui { | 17 namespace ui { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 enum Layout { | |
| 21 LAYOUT_US, | |
| 22 LAYOUT_FR, | |
| 23 LAYOUT_KR, | |
| 24 LAYOUT_JP, | |
| 25 }; | |
| 26 | |
| 27 // |LoadKeyboardLayout()| ensures the locale to be loaded into the system | |
| 28 // (Similar to temporarily adding a locale in Control Panel), otherwise | |
| 29 // |ToUnicodeEx()| will fall-back to the default locale. | |
| 30 // See MSDN LoadKeyboardLayout(): | |
| 31 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms646305(v=vs.85).as
px | |
| 32 // And language constants and strings: | |
| 33 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).as
px | |
| 34 HKL GetInputLocale(Layout layout) { | |
| 35 switch (layout) { | |
| 36 case LAYOUT_US: | |
| 37 return ::LoadKeyboardLayout(L"00000409", KLF_ACTIVATE); | |
| 38 case LAYOUT_FR: | |
| 39 return ::LoadKeyboardLayout(L"0000040c", KLF_ACTIVATE); | |
| 40 case LAYOUT_KR: | |
| 41 // |LoadKeyboardLayout(L"00000412", KLF_ACTIVATE)| returns the correct | |
| 42 // Korean locale, but it will fail on DrMemory tests. | |
| 43 // See https://crbug.com/612736#c6 | |
| 44 // However we could bypass it since we are only testing non-printable keys | |
| 45 // on Korean locale. | |
| 46 // (This issue only happens on Korean and Japanese). | |
| 47 return reinterpret_cast<HKL>(0x04120412); | |
| 48 case LAYOUT_JP: | |
| 49 return reinterpret_cast<HKL>(0x04110411); | |
| 50 default: | |
| 51 return 0; | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 struct TestKey { | 21 struct TestKey { |
| 56 KeyboardCode key_code; | 22 KeyboardCode key_code; |
| 57 const char* normal; | 23 const char* normal; |
| 58 const char* shift; | 24 const char* shift; |
| 59 const char* capslock; | 25 const char* capslock; |
| 60 const char* altgr; | 26 const char* altgr; |
| 61 const char* shift_capslock; | 27 const char* shift_capslock; |
| 62 const char* shift_altgr; | 28 const char* shift_altgr; |
| 63 const char* altgr_capslock; | 29 const char* altgr_capslock; |
| 64 }; | 30 }; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 KeyboardCode key_code, | 85 KeyboardCode key_code, |
| 120 int flags) { | 86 int flags) { |
| 121 return keymap.DomKeyFromKeyboardCodeImpl(key_code, flags); | 87 return keymap.DomKeyFromKeyboardCodeImpl(key_code, flags); |
| 122 } | 88 } |
| 123 | 89 |
| 124 private: | 90 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(PlatformKeyMapTest); | 91 DISALLOW_COPY_AND_ASSIGN(PlatformKeyMapTest); |
| 126 }; | 92 }; |
| 127 | 93 |
| 128 TEST_F(PlatformKeyMapTest, USLayout) { | 94 TEST_F(PlatformKeyMapTest, USLayout) { |
| 129 PlatformKeyMap keymap(GetInputLocale(LAYOUT_US)); | 95 PlatformKeyMap keymap(GetPlatformKeyboardLayout(KEYBOARD_LAYOUT_ENGLISH_US)); |
| 130 | 96 |
| 131 const TestKey kUSLayoutTestCases[] = { | 97 const TestKey kUSLayoutTestCases[] = { |
| 132 // n s c a sc sa ac | 98 // n s c a sc sa ac |
| 133 {VKEY_0, "0", ")", "0", "0", ")", ")", "0"}, | 99 {VKEY_0, "0", ")", "0", "0", ")", ")", "0"}, |
| 134 {VKEY_1, "1", "!", "1", "1", "!", "!", "1"}, | 100 {VKEY_1, "1", "!", "1", "1", "!", "!", "1"}, |
| 135 {VKEY_2, "2", "@", "2", "2", "@", "@", "2"}, | 101 {VKEY_2, "2", "@", "2", "2", "@", "@", "2"}, |
| 136 {VKEY_3, "3", "#", "3", "3", "#", "#", "3"}, | 102 {VKEY_3, "3", "#", "3", "3", "#", "#", "3"}, |
| 137 {VKEY_4, "4", "$", "4", "4", "$", "$", "4"}, | 103 {VKEY_4, "4", "$", "4", "4", "$", "$", "4"}, |
| 138 {VKEY_5, "5", "%", "5", "5", "%", "%", "5"}, | 104 {VKEY_5, "5", "%", "5", "5", "%", "%", "5"}, |
| 139 {VKEY_6, "6", "^", "6", "6", "^", "^", "6"}, | 105 {VKEY_6, "6", "^", "6", "6", "^", "^", "6"}, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 167 {VKEY_Y, "y", "Y", "Y", "y", "y", "Y", "Y"}, | 133 {VKEY_Y, "y", "Y", "Y", "y", "y", "Y", "Y"}, |
| 168 {VKEY_Z, "z", "Z", "Z", "z", "z", "Z", "Z"}, | 134 {VKEY_Z, "z", "Z", "Z", "z", "z", "Z", "Z"}, |
| 169 }; | 135 }; |
| 170 | 136 |
| 171 for (const auto& test_case : kUSLayoutTestCases) { | 137 for (const auto& test_case : kUSLayoutTestCases) { |
| 172 CheckKeyboardCodeToKeyString("USLayout", keymap, test_case); | 138 CheckKeyboardCodeToKeyString("USLayout", keymap, test_case); |
| 173 } | 139 } |
| 174 } | 140 } |
| 175 | 141 |
| 176 TEST_F(PlatformKeyMapTest, FRLayout) { | 142 TEST_F(PlatformKeyMapTest, FRLayout) { |
| 177 PlatformKeyMap keymap(GetInputLocale(LAYOUT_FR)); | 143 PlatformKeyMap keymap(GetPlatformKeyboardLayout(KEYBOARD_LAYOUT_FRENCH)); |
| 178 | 144 |
| 179 const TestKey kFRLayoutTestCases[] = { | 145 const TestKey kFRLayoutTestCases[] = { |
| 180 // n s c a sc sa ac | 146 // n s c a sc sa ac |
| 181 {VKEY_0, "à", "0", "0", "@", "à", "0", "@"}, | 147 {VKEY_0, "à", "0", "0", "@", "à", "0", "@"}, |
| 182 {VKEY_1, "&", "1", "1", "&", "&", "1", "1"}, | 148 {VKEY_1, "&", "1", "1", "&", "&", "1", "1"}, |
| 183 {VKEY_2, "é", "2", "2", "Dead", "é", "2", "Dead"}, | 149 {VKEY_2, "é", "2", "2", "Dead", "é", "2", "Dead"}, |
| 184 {VKEY_3, "\"", "3", "3", "#", "\"", "3", "#"}, | 150 {VKEY_3, "\"", "3", "3", "#", "\"", "3", "#"}, |
| 185 {VKEY_4, "\'", "4", "4", "{", "\'", "4", "{"}, | 151 {VKEY_4, "\'", "4", "4", "{", "\'", "4", "{"}, |
| 186 {VKEY_5, "(", "5", "5", "[", "(", "5", "["}, | 152 {VKEY_5, "(", "5", "5", "[", "(", "5", "["}, |
| 187 {VKEY_6, "-", "6", "6", "|", "-", "6", "|"}, | 153 {VKEY_6, "-", "6", "6", "|", "-", "6", "|"}, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 215 {VKEY_Y, "y", "Y", "Y", "y", "y", "Y", "Y"}, | 181 {VKEY_Y, "y", "Y", "Y", "y", "y", "Y", "Y"}, |
| 216 {VKEY_Z, "z", "Z", "Z", "z", "z", "Z", "Z"}, | 182 {VKEY_Z, "z", "Z", "Z", "z", "z", "Z", "Z"}, |
| 217 }; | 183 }; |
| 218 | 184 |
| 219 for (const auto& test_case : kFRLayoutTestCases) { | 185 for (const auto& test_case : kFRLayoutTestCases) { |
| 220 CheckKeyboardCodeToKeyString("FRLayout", keymap, test_case); | 186 CheckKeyboardCodeToKeyString("FRLayout", keymap, test_case); |
| 221 } | 187 } |
| 222 } | 188 } |
| 223 | 189 |
| 224 TEST_F(PlatformKeyMapTest, NumPad) { | 190 TEST_F(PlatformKeyMapTest, NumPad) { |
| 225 PlatformKeyMap keymap(GetInputLocale(LAYOUT_US)); | 191 PlatformKeyMap keymap(GetPlatformKeyboardLayout(KEYBOARD_LAYOUT_ENGLISH_US)); |
| 226 | 192 |
| 227 const struct TestCase { | 193 const struct TestCase { |
| 228 KeyboardCode key_code; | 194 KeyboardCode key_code; |
| 229 DomKey key; | 195 DomKey key; |
| 230 } kNumPadTestCases[] = { | 196 } kNumPadTestCases[] = { |
| 231 {VKEY_NUMPAD0, DomKey::FromCharacter('0')}, | 197 {VKEY_NUMPAD0, DomKey::FromCharacter('0')}, |
| 232 {VKEY_NUMPAD1, DomKey::FromCharacter('1')}, | 198 {VKEY_NUMPAD1, DomKey::FromCharacter('1')}, |
| 233 {VKEY_NUMPAD2, DomKey::FromCharacter('2')}, | 199 {VKEY_NUMPAD2, DomKey::FromCharacter('2')}, |
| 234 {VKEY_NUMPAD3, DomKey::FromCharacter('3')}, | 200 {VKEY_NUMPAD3, DomKey::FromCharacter('3')}, |
| 235 {VKEY_NUMPAD4, DomKey::FromCharacter('4')}, | 201 {VKEY_NUMPAD4, DomKey::FromCharacter('4')}, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 264 DomKeyFromKeyboardCodeImpl(keymap, key_code, EF_CONTROL_DOWN)) | 230 DomKeyFromKeyboardCodeImpl(keymap, key_code, EF_CONTROL_DOWN)) |
| 265 << key_code; | 231 << key_code; |
| 266 EXPECT_EQ(test_case.key, | 232 EXPECT_EQ(test_case.key, |
| 267 DomKeyFromKeyboardCodeImpl(keymap, key_code, | 233 DomKeyFromKeyboardCodeImpl(keymap, key_code, |
| 268 EF_ALTGR_DOWN | EF_CONTROL_DOWN)) | 234 EF_ALTGR_DOWN | EF_CONTROL_DOWN)) |
| 269 << key_code; | 235 << key_code; |
| 270 } | 236 } |
| 271 } | 237 } |
| 272 | 238 |
| 273 TEST_F(PlatformKeyMapTest, NonPrintableKey) { | 239 TEST_F(PlatformKeyMapTest, NonPrintableKey) { |
| 274 HKL layout = GetInputLocale(LAYOUT_US); | 240 HKL layout = GetPlatformKeyboardLayout(KEYBOARD_LAYOUT_ENGLISH_US); |
| 275 PlatformKeyMap keymap(layout); | 241 PlatformKeyMap keymap(layout); |
| 276 | 242 |
| 277 for (const auto& test_case : kNonPrintableCodeMap) { | 243 for (const auto& test_case : kNonPrintableCodeMap) { |
| 278 // Not available on |LAYOUT_US|. | 244 // Not available on |LAYOUT_US|. |
| 279 if (test_case.dom_code == DomCode::PAUSE || | 245 if (test_case.dom_code == DomCode::PAUSE || |
| 280 test_case.dom_code == DomCode::LANG2 || | 246 test_case.dom_code == DomCode::LANG2 || |
| 281 test_case.dom_code == DomCode::NON_CONVERT) | 247 test_case.dom_code == DomCode::NON_CONVERT) |
| 282 continue; | 248 continue; |
| 283 | 249 |
| 284 int scan_code = | 250 int scan_code = |
| (...skipping 25 matching lines...) Expand all Loading... |
| 310 TEST_F(PlatformKeyMapTest, KoreanSpecificKeys) { | 276 TEST_F(PlatformKeyMapTest, KoreanSpecificKeys) { |
| 311 const struct TestCase { | 277 const struct TestCase { |
| 312 KeyboardCode key_code; | 278 KeyboardCode key_code; |
| 313 DomKey kr_key; | 279 DomKey kr_key; |
| 314 DomKey us_key; | 280 DomKey us_key; |
| 315 } kKoreanTestCases[] = { | 281 } kKoreanTestCases[] = { |
| 316 {VKEY_HANGUL, DomKey::HANGUL_MODE, DomKey::UNIDENTIFIED}, | 282 {VKEY_HANGUL, DomKey::HANGUL_MODE, DomKey::UNIDENTIFIED}, |
| 317 {VKEY_HANJA, DomKey::HANJA_MODE, DomKey::UNIDENTIFIED}, | 283 {VKEY_HANJA, DomKey::HANJA_MODE, DomKey::UNIDENTIFIED}, |
| 318 }; | 284 }; |
| 319 | 285 |
| 320 PlatformKeyMap us_keymap(GetInputLocale(LAYOUT_US)); | 286 PlatformKeyMap us_keymap( |
| 321 PlatformKeyMap kr_keymap(GetInputLocale(LAYOUT_KR)); | 287 GetPlatformKeyboardLayout(KEYBOARD_LAYOUT_ENGLISH_US)); |
| 288 PlatformKeyMap kr_keymap(GetPlatformKeyboardLayout(KEYBOARD_LAYOUT_KOREAN)); |
| 322 for (const auto& test_case : kKoreanTestCases) { | 289 for (const auto& test_case : kKoreanTestCases) { |
| 323 EXPECT_EQ(test_case.us_key, DomKeyFromKeyboardCodeImpl( | 290 EXPECT_EQ(test_case.us_key, DomKeyFromKeyboardCodeImpl( |
| 324 us_keymap, test_case.key_code, EF_NONE)) | 291 us_keymap, test_case.key_code, EF_NONE)) |
| 325 << test_case.key_code; | 292 << test_case.key_code; |
| 326 EXPECT_EQ(test_case.kr_key, DomKeyFromKeyboardCodeImpl( | 293 EXPECT_EQ(test_case.kr_key, DomKeyFromKeyboardCodeImpl( |
| 327 kr_keymap, test_case.key_code, EF_NONE)) | 294 kr_keymap, test_case.key_code, EF_NONE)) |
| 328 << test_case.key_code; | 295 << test_case.key_code; |
| 329 } | 296 } |
| 330 } | 297 } |
| 331 | 298 |
| 332 TEST_F(PlatformKeyMapTest, JapaneseSpecificKeys) { | 299 TEST_F(PlatformKeyMapTest, JapaneseSpecificKeys) { |
| 333 const struct TestCase { | 300 const struct TestCase { |
| 334 KeyboardCode key_code; | 301 KeyboardCode key_code; |
| 335 DomKey jp_key; | 302 DomKey jp_key; |
| 336 DomKey us_key; | 303 DomKey us_key; |
| 337 } kJapaneseTestCases[] = { | 304 } kJapaneseTestCases[] = { |
| 338 {VKEY_KANA, DomKey::KANA_MODE, DomKey::UNIDENTIFIED}, | 305 {VKEY_KANA, DomKey::KANA_MODE, DomKey::UNIDENTIFIED}, |
| 339 {VKEY_KANJI, DomKey::KANJI_MODE, DomKey::UNIDENTIFIED}, | 306 {VKEY_KANJI, DomKey::KANJI_MODE, DomKey::UNIDENTIFIED}, |
| 340 {VKEY_OEM_ATTN, DomKey::ALPHANUMERIC, DomKey::UNIDENTIFIED}, | 307 {VKEY_OEM_ATTN, DomKey::ALPHANUMERIC, DomKey::UNIDENTIFIED}, |
| 341 {VKEY_OEM_FINISH, DomKey::KATAKANA, DomKey::UNIDENTIFIED}, | 308 {VKEY_OEM_FINISH, DomKey::KATAKANA, DomKey::UNIDENTIFIED}, |
| 342 {VKEY_OEM_COPY, DomKey::HIRAGANA, DomKey::UNIDENTIFIED}, | 309 {VKEY_OEM_COPY, DomKey::HIRAGANA, DomKey::UNIDENTIFIED}, |
| 343 {VKEY_DBE_SBCSCHAR, DomKey::HANKAKU, DomKey::UNIDENTIFIED}, | 310 {VKEY_DBE_SBCSCHAR, DomKey::HANKAKU, DomKey::UNIDENTIFIED}, |
| 344 {VKEY_DBE_DBCSCHAR, DomKey::ZENKAKU, DomKey::UNIDENTIFIED}, | 311 {VKEY_DBE_DBCSCHAR, DomKey::ZENKAKU, DomKey::UNIDENTIFIED}, |
| 345 {VKEY_OEM_BACKTAB, DomKey::ROMAJI, DomKey::UNIDENTIFIED}, | 312 {VKEY_OEM_BACKTAB, DomKey::ROMAJI, DomKey::UNIDENTIFIED}, |
| 346 {VKEY_ATTN, DomKey::KANA_MODE, DomKey::ATTN}, | 313 {VKEY_ATTN, DomKey::KANA_MODE, DomKey::ATTN}, |
| 347 }; | 314 }; |
| 348 | 315 |
| 349 PlatformKeyMap us_keymap(GetInputLocale(LAYOUT_US)); | 316 PlatformKeyMap us_keymap( |
| 350 PlatformKeyMap jp_keymap(GetInputLocale(LAYOUT_JP)); | 317 GetPlatformKeyboardLayout(KEYBOARD_LAYOUT_ENGLISH_US)); |
| 318 PlatformKeyMap jp_keymap(GetPlatformKeyboardLayout(KEYBOARD_LAYOUT_JAPANESE)); |
| 351 for (const auto& test_case : kJapaneseTestCases) { | 319 for (const auto& test_case : kJapaneseTestCases) { |
| 352 EXPECT_EQ(test_case.us_key, DomKeyFromKeyboardCodeImpl( | 320 EXPECT_EQ(test_case.us_key, DomKeyFromKeyboardCodeImpl( |
| 353 us_keymap, test_case.key_code, EF_NONE)) | 321 us_keymap, test_case.key_code, EF_NONE)) |
| 354 << test_case.key_code; | 322 << test_case.key_code; |
| 355 EXPECT_EQ(test_case.jp_key, DomKeyFromKeyboardCodeImpl( | 323 EXPECT_EQ(test_case.jp_key, DomKeyFromKeyboardCodeImpl( |
| 356 jp_keymap, test_case.key_code, EF_NONE)) | 324 jp_keymap, test_case.key_code, EF_NONE)) |
| 357 << test_case.key_code; | 325 << test_case.key_code; |
| 358 } | 326 } |
| 359 } | 327 } |
| 360 | 328 |
| 361 } // namespace ui | 329 } // namespace ui |
| OLD | NEW |