| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/dom4/keycode_converter.h" | 5 #include "ui/events/keycodes/dom4/keycode_converter.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/events/keycodes/dom3/domcode.h" |
| 12 #include "ui/events/keycodes/dom3/domkey.h" |
| 11 | 13 |
| 12 using ui::KeycodeConverter; | 14 using ui::KeycodeConverter; |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
| 17 const size_t kExpectedMappedKeyCount = 138; | 19 const size_t kExpectedMappedKeyCount = 138; |
| 18 #elif defined(OS_LINUX) | 20 #elif defined(OS_LINUX) |
| 19 const size_t kExpectedMappedKeyCount = 145; | 21 const size_t kExpectedMappedKeyCount = 145; |
| 20 #elif defined(OS_MACOSX) | 22 #elif defined(OS_MACOSX) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 49 // Don't test keys with no native keycode mapping on this platform. | 51 // Don't test keys with no native keycode mapping on this platform. |
| 50 if (entry->native_keycode == ui::KeycodeConverter::InvalidNativeKeycode()) | 52 if (entry->native_keycode == ui::KeycodeConverter::InvalidNativeKeycode()) |
| 51 continue; | 53 continue; |
| 52 | 54 |
| 53 // Verify UsbKeycodeToNativeKeycode works for this key. | 55 // Verify UsbKeycodeToNativeKeycode works for this key. |
| 54 EXPECT_EQ( | 56 EXPECT_EQ( |
| 55 entry->native_keycode, | 57 entry->native_keycode, |
| 56 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(entry->usb_keycode)); | 58 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(entry->usb_keycode)); |
| 57 | 59 |
| 58 // Verify CodeToNativeKeycode and NativeKeycodeToCode work correctly. | 60 // Verify CodeToNativeKeycode and NativeKeycodeToCode work correctly. |
| 59 if (entry->code) { | 61 if (entry->code != ui::DomCode::NONE) { |
| 60 EXPECT_EQ(entry->native_keycode, | 62 EXPECT_EQ(entry->native_keycode, |
| 61 ui::KeycodeConverter::CodeToNativeKeycode(entry->code)); | 63 ui::KeycodeConverter::DomCodeToNativeKeycode(entry->code)); |
| 62 EXPECT_STREQ( | 64 EXPECT_EQ( |
| 63 entry->code, | 65 entry->code, |
| 64 ui::KeycodeConverter::NativeKeycodeToCode(entry->native_keycode)); | 66 ui::KeycodeConverter::NativeKeycodeToDomCode(entry->native_keycode)); |
| 65 } | 67 } else { |
| 66 else { | |
| 67 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(), | 68 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(), |
| 68 ui::KeycodeConverter::CodeToNativeKeycode(entry->code)); | 69 ui::KeycodeConverter::DomCodeToNativeKeycode(entry->code)); |
| 69 } | 70 } |
| 70 | 71 |
| 71 // Verify that the USB or native codes aren't duplicated. | 72 // Verify that the USB or native codes aren't duplicated. |
| 72 EXPECT_EQ(0U, usb_to_native.count(entry->usb_keycode)) | 73 EXPECT_EQ(0U, usb_to_native.count(entry->usb_keycode)) |
| 73 << " duplicate of USB code 0x" << std::hex << std::setfill('0') | 74 << " duplicate of USB code 0x" << std::hex << std::setfill('0') |
| 74 << std::setw(6) << entry->usb_keycode | 75 << std::setw(6) << entry->usb_keycode << " to native 0x" << std::setw(4) |
| 75 << " to native 0x" | 76 << entry->native_keycode << " (previous was 0x" << std::setw(4) |
| 76 << std::setw(4) << entry->native_keycode | 77 << usb_to_native[entry->usb_keycode] << ")"; |
| 77 << " (previous was 0x" | |
| 78 << std::setw(4) << usb_to_native[entry->usb_keycode] | |
| 79 << ")"; | |
| 80 usb_to_native[entry->usb_keycode] = entry->native_keycode; | 78 usb_to_native[entry->usb_keycode] = entry->native_keycode; |
| 81 EXPECT_EQ(0U, native_to_usb.count(entry->native_keycode)) | 79 EXPECT_EQ(0U, native_to_usb.count(entry->native_keycode)) |
| 82 << " duplicate of native code 0x" << std::hex << std::setfill('0') | 80 << " duplicate of native code 0x" << std::hex << std::setfill('0') |
| 83 << std::setw(4) << entry->native_keycode | 81 << std::setw(4) << entry->native_keycode << " to USB 0x" << std::setw(6) |
| 84 << " to USB 0x" | 82 << entry->usb_keycode << " (previous was 0x" << std::setw(6) |
| 85 << std::setw(6) << entry->usb_keycode | 83 << native_to_usb[entry->native_keycode] << ")"; |
| 86 << " (previous was 0x" | |
| 87 << std::setw(6) << native_to_usb[entry->native_keycode] | |
| 88 << ")"; | |
| 89 native_to_usb[entry->native_keycode] = entry->usb_keycode; | 84 native_to_usb[entry->native_keycode] = entry->usb_keycode; |
| 90 } | 85 } |
| 91 ASSERT_EQ(usb_to_native.size(), native_to_usb.size()); | 86 ASSERT_EQ(usb_to_native.size(), native_to_usb.size()); |
| 92 | 87 |
| 93 // Verify that the number of mapped keys is what we expect, i.e. we haven't | 88 // Verify that the number of mapped keys is what we expect, i.e. we haven't |
| 94 // lost any, and if we've added some then the expectation has been updated. | 89 // lost any, and if we've added some then the expectation has been updated. |
| 95 EXPECT_EQ(kExpectedMappedKeyCount, usb_to_native.size()); | 90 EXPECT_EQ(kExpectedMappedKeyCount, usb_to_native.size()); |
| 96 } | 91 } |
| 97 | 92 |
| 98 TEST(UsbKeycodeMap, NonExistent) { | 93 TEST(UsbKeycodeMap, NonExistent) { |
| 99 // Verify that UsbKeycodeToNativeKeycode works for a non-existent USB keycode. | 94 // Verify that UsbKeycodeToNativeKeycode works for a non-existent USB keycode. |
| 100 EXPECT_EQ( | 95 EXPECT_EQ( |
| 101 ui::KeycodeConverter::InvalidNativeKeycode(), | 96 ui::KeycodeConverter::InvalidNativeKeycode(), |
| 102 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonExistentKeycode)); | 97 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonExistentKeycode)); |
| 103 } | 98 } |
| 104 | 99 |
| 105 TEST(UsbKeycodeMap, UsBackslashIsNonUsHash) { | 100 TEST(UsbKeycodeMap, UsBackslashIsNonUsHash) { |
| 106 // Verify that UsbKeycodeToNativeKeycode treats the non-US "hash" key | 101 // Verify that UsbKeycodeToNativeKeycode treats the non-US "hash" key |
| 107 // as equivalent to the US "backslash" key. | 102 // as equivalent to the US "backslash" key. |
| 108 EXPECT_EQ(ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbUsBackslash), | 103 EXPECT_EQ(ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbUsBackslash), |
| 109 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonUsHash)); | 104 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonUsHash)); |
| 110 } | 105 } |
| 111 | 106 |
| 107 TEST(KeycodeConverter, DomCode) { |
| 108 size_t entries = ui::KeycodeConverter::NumDomCodeStringEntriesForTest(); |
| 109 EXPECT_NE(entries, static_cast<size_t>(0u)); |
| 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 char* const* string = ui::KeycodeConverter::DomCodeStringTableForTest(); |
| 117 for (size_t i = 0; i < entries; ++i) { |
| 118 const char* s = string[i]; |
| 119 ui::DomCode code = ui::KeycodeConverter::CodeStringToDomCode(s); |
| 120 if (s) { |
| 121 EXPECT_STREQ(s, ui::KeycodeConverter::DomCodeToCodeString(code)); |
| 122 } else { |
| 123 EXPECT_EQ(ui::DomCode::NONE, code); |
| 124 } |
| 125 } |
| 126 } |
| 127 |
| 128 TEST(KeycodeConverter, DomKey) { |
| 129 size_t entries = ui::KeycodeConverter::NumDomKeyStringEntriesForTest(); |
| 130 EXPECT_NE(entries, static_cast<size_t>(0u)); |
| 131 // Test invalid and unknown arguments to KeyStringToDomKey() |
| 132 EXPECT_EQ(ui::DomKey::NONE, ui::KeycodeConverter::KeyStringToDomKey(nullptr)); |
| 133 EXPECT_EQ(ui::DomKey::NONE, ui::KeycodeConverter::KeyStringToDomKey("-")); |
| 134 // Round-trip test DOM Level 3 .key strings. |
| 135 const char* const* string = ui::KeycodeConverter::DomKeyStringTableForTest(); |
| 136 for (size_t i = 0; i < entries; ++i) { |
| 137 const char* s = string[i]; |
| 138 ui::DomKey key = ui::KeycodeConverter::KeyStringToDomKey(s); |
| 139 if (s) { |
| 140 EXPECT_STREQ(s, ui::KeycodeConverter::DomKeyToKeyString(key)); |
| 141 } else { |
| 142 EXPECT_EQ(ui::DomKey::NONE, key); |
| 143 } |
| 144 } |
| 145 } |
| 146 |
| 112 } // namespace | 147 } // namespace |
| OLD | NEW |