Add ICU message format support
[chromium-blink-merge.git] / ui / events / keycodes / dom / keycode_converter_unittest.cc
blobf5da9a09226a447f0b7f82d77038ec84f134f507
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/keycodes/dom/keycode_converter.h"
7 #include <map>
9 #include "base/basictypes.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/events/keycodes/dom/dom_code.h"
12 #include "ui/events/keycodes/dom/dom_key.h"
14 using ui::KeycodeConverter;
16 namespace {
18 #if defined(OS_WIN)
19 const size_t kExpectedMappedKeyCount = 138;
20 #elif defined(OS_LINUX)
21 const size_t kExpectedMappedKeyCount = 168;
22 #elif defined(OS_MACOSX)
23 const size_t kExpectedMappedKeyCount = 118;
24 #else
25 const size_t kExpectedMappedKeyCount = 0;
26 #endif
28 const uint32_t kUsbNonExistentKeycode = 0xffffff;
29 const uint32_t kUsbUsBackslash = 0x070031;
30 const uint32_t kUsbNonUsHash = 0x070032;
32 TEST(UsbKeycodeMap, Basic) {
33 // Verify that the first element in the table is the "invalid" code.
34 const ui::KeycodeMapEntry* keycode_map =
35 ui::KeycodeConverter::GetKeycodeMapForTest();
36 EXPECT_EQ(ui::KeycodeConverter::InvalidUsbKeycode(),
37 keycode_map[0].usb_keycode);
38 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(),
39 keycode_map[0].native_keycode);
40 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(),
41 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::NONE));
43 // Verify that there are no duplicate entries in the mapping.
44 std::map<uint32_t, uint16_t> usb_to_native;
45 std::map<uint16_t, uint32_t> native_to_usb;
46 size_t numEntries = ui::KeycodeConverter::NumKeycodeMapEntriesForTest();
47 for (size_t i = 0; i < numEntries; ++i) {
48 const ui::KeycodeMapEntry* entry = &keycode_map[i];
49 // Don't test keys with no native keycode mapping on this platform.
50 if (entry->native_keycode == ui::KeycodeConverter::InvalidNativeKeycode())
51 continue;
53 // Verify UsbKeycodeToNativeKeycode works for this key.
54 EXPECT_EQ(
55 entry->native_keycode,
56 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(entry->usb_keycode));
58 // Verify DomCodeToNativeKeycode works correctly.
59 ui::DomCode dom_code =
60 ui::KeycodeConverter::CodeStringToDomCode(entry->code);
61 if (entry->code) {
62 EXPECT_EQ(entry->native_keycode,
63 ui::KeycodeConverter::DomCodeToNativeKeycode(dom_code));
64 } else {
65 EXPECT_EQ(ui::DomCode::NONE, dom_code);
68 // Verify that the USB or native codes aren't duplicated.
69 EXPECT_EQ(0U, usb_to_native.count(entry->usb_keycode))
70 << " duplicate of USB code 0x" << std::hex << std::setfill('0')
71 << std::setw(6) << entry->usb_keycode
72 << " to native 0x"
73 << std::setw(4) << entry->native_keycode
74 << " (previous was 0x"
75 << std::setw(4) << usb_to_native[entry->usb_keycode]
76 << ")";
77 usb_to_native[entry->usb_keycode] = entry->native_keycode;
78 EXPECT_EQ(0U, native_to_usb.count(entry->native_keycode))
79 << " duplicate of native code 0x" << std::hex << std::setfill('0')
80 << std::setw(4) << entry->native_keycode
81 << " to USB 0x"
82 << std::setw(6) << entry->usb_keycode
83 << " (previous was 0x"
84 << std::setw(6) << native_to_usb[entry->native_keycode]
85 << ")";
86 native_to_usb[entry->native_keycode] = entry->usb_keycode;
88 ASSERT_EQ(usb_to_native.size(), native_to_usb.size());
90 // Verify that the number of mapped keys is what we expect, i.e. we haven't
91 // lost any, and if we've added some then the expectation has been updated.
92 EXPECT_EQ(kExpectedMappedKeyCount, usb_to_native.size());
95 TEST(UsbKeycodeMap, NonExistent) {
96 // Verify that UsbKeycodeToNativeKeycode works for a non-existent USB keycode.
97 EXPECT_EQ(
98 ui::KeycodeConverter::InvalidNativeKeycode(),
99 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonExistentKeycode));
102 TEST(UsbKeycodeMap, UsBackslashIsNonUsHash) {
103 // Verify that UsbKeycodeToNativeKeycode treats the non-US "hash" key
104 // as equivalent to the US "backslash" key.
105 EXPECT_EQ(ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbUsBackslash),
106 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonUsHash));
109 TEST(KeycodeConverter, DomCode) {
110 // Test invalid and unknown arguments to CodeStringToDomCode()
111 EXPECT_EQ(ui::DomCode::NONE,
112 ui::KeycodeConverter::CodeStringToDomCode(nullptr));
113 EXPECT_EQ(ui::DomCode::NONE, ui::KeycodeConverter::CodeStringToDomCode("-"));
114 EXPECT_EQ(ui::DomCode::NONE, ui::KeycodeConverter::CodeStringToDomCode(""));
115 // Round-trip test DOM Level 3 .code strings.
116 const ui::KeycodeMapEntry* keycode_map =
117 ui::KeycodeConverter::GetKeycodeMapForTest();
118 size_t numEntries = ui::KeycodeConverter::NumKeycodeMapEntriesForTest();
119 for (size_t i = 0; i < numEntries; ++i) {
120 SCOPED_TRACE(i);
121 const ui::KeycodeMapEntry* entry = &keycode_map[i];
122 ui::DomCode code = ui::KeycodeConverter::CodeStringToDomCode(entry->code);
123 if (entry->code) {
124 EXPECT_STREQ(entry->code,
125 ui::KeycodeConverter::DomCodeToCodeString(code));
126 } else {
127 EXPECT_EQ(static_cast<int>(ui::DomCode::NONE),
128 static_cast<int>(code));
133 TEST(KeycodeConverter, DomKey) {
134 // Test invalid and unknown arguments to KeyStringToDomKey()
135 EXPECT_EQ(ui::DomKey::NONE, ui::KeycodeConverter::KeyStringToDomKey(nullptr));
136 EXPECT_EQ(ui::DomKey::NONE, ui::KeycodeConverter::KeyStringToDomKey("-"));
137 // Round-trip test DOM Level 3 .key strings.
138 const char* s = nullptr;
139 for (size_t i = 0;
140 (s = ui::KeycodeConverter::DomKeyStringForTest(i)) != nullptr;
141 ++i) {
142 SCOPED_TRACE(i);
143 ui::DomKey key = ui::KeycodeConverter::KeyStringToDomKey(s);
144 if (s) {
145 EXPECT_STREQ(s, ui::KeycodeConverter::DomKeyToKeyString(key));
146 } else {
147 EXPECT_EQ(ui::DomKey::NONE, key);
152 } // namespace