OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ui/events/keycodes/dom4/keycode_converter.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 using ui::KeycodeConverter; | |
13 | |
14 namespace { | |
15 | |
16 #if defined(OS_LINUX) | |
17 const size_t kExpectedMappedKeyCount = 145; | |
18 #else | |
19 const size_t kExpectedMappedKeyCount = 0; | |
20 #endif | |
21 | |
22 const uint32_t kUsbNonExistentKeycode = 0xffffff; | |
23 const uint32_t kUsbUsBackslash = 0x070031; | |
24 const uint32_t kUsbNonUsHash = 0x070032; | |
25 | |
26 TEST(UsbKeycodeMap, Basic) { | |
27 // Verify that the first element in the table is the "invalid" code. | |
28 const ui::KeycodeMapEntry* keycode_map = | |
29 ui::KeycodeConverter::GetKeycodeMapForTest(); | |
30 EXPECT_EQ(ui::KeycodeConverter::InvalidUsbKeycode(), | |
31 keycode_map[0].usb_keycode); | |
32 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(), | |
33 keycode_map[0].native_keycode); | |
34 EXPECT_STREQ(ui::KeycodeConverter::InvalidKeyboardEventCode(), | |
35 "Unidentified"); | |
36 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(), | |
37 ui::KeycodeConverter::CodeToNativeKeycode("Unidentified")); | |
38 | |
39 // Verify that there are no duplicate entries in the mapping. | |
40 std::map<uint32_t, uint16_t> usb_to_native; | |
41 std::map<uint16_t, uint32_t> native_to_usb; | |
42 size_t numEntries = ui::KeycodeConverter::NumKeycodeMapEntriesForTest(); | |
43 for (size_t i = 0; i < numEntries; ++i) { | |
44 const ui::KeycodeMapEntry* entry = &keycode_map[i]; | |
45 // Don't test keys with no native keycode mapping on this platform. | |
46 if (entry->native_keycode == ui::KeycodeConverter::InvalidNativeKeycode()) | |
47 continue; | |
48 | |
49 // Verify UsbKeycodeToNativeKeycode works for this key. | |
50 EXPECT_EQ( | |
51 entry->native_keycode, | |
52 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(entry->usb_keycode)); | |
53 | |
54 // Verify CodeToNativeKeycode and NativeKeycodeToCode work correctly. | |
55 if (entry->code) { | |
56 EXPECT_EQ(entry->native_keycode, | |
57 ui::KeycodeConverter::CodeToNativeKeycode(entry->code)); | |
58 EXPECT_STREQ( | |
59 entry->code, | |
60 ui::KeycodeConverter::NativeKeycodeToCode(entry->native_keycode)); | |
61 } | |
62 else { | |
63 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(), | |
64 ui::KeycodeConverter::CodeToNativeKeycode(entry->code)); | |
65 } | |
66 | |
67 // Verify that the USB or native codes aren't duplicated. | |
68 EXPECT_EQ(0U, usb_to_native.count(entry->usb_keycode)) | |
69 << " duplicate of USB code 0x" << std::hex << std::setfill('0') | |
70 << std::setw(6) << entry->usb_keycode | |
71 << " to native 0x" | |
72 << std::setw(4) << entry->native_keycode | |
73 << " (previous was 0x" | |
74 << std::setw(4) << usb_to_native[entry->usb_keycode] | |
75 << ")"; | |
76 usb_to_native[entry->usb_keycode] = entry->native_keycode; | |
77 EXPECT_EQ(0U, native_to_usb.count(entry->native_keycode)) | |
78 << " duplicate of native code 0x" << std::hex << std::setfill('0') | |
79 << std::setw(4) << entry->native_keycode | |
80 << " to USB 0x" | |
81 << std::setw(6) << entry->usb_keycode | |
82 << " (previous was 0x" | |
83 << std::setw(6) << native_to_usb[entry->native_keycode] | |
84 << ")"; | |
85 native_to_usb[entry->native_keycode] = entry->usb_keycode; | |
86 } | |
87 ASSERT_EQ(usb_to_native.size(), native_to_usb.size()); | |
88 | |
89 // Verify that the number of mapped keys is what we expect, i.e. we haven't | |
90 // lost any, and if we've added some then the expectation has been updated. | |
91 EXPECT_EQ(kExpectedMappedKeyCount, usb_to_native.size()); | |
92 } | |
93 | |
94 TEST(UsbKeycodeMap, NonExistent) { | |
95 // Verify that UsbKeycodeToNativeKeycode works for a non-existent USB keycode. | |
96 EXPECT_EQ( | |
97 ui::KeycodeConverter::InvalidNativeKeycode(), | |
98 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonExistentKeycode)); | |
99 } | |
100 | |
101 TEST(UsbKeycodeMap, UsBackslashIsNonUsHash) { | |
102 // Verify that UsbKeycodeToNativeKeycode treats the non-US "hash" key | |
103 // as equivalent to the US "backslash" key. | |
104 EXPECT_EQ(ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbUsBackslash), | |
105 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonUsHash)); | |
106 } | |
107 | |
108 } // namespace | |
OLD | NEW |