| 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/dom_code.h" |
| 12 #include "ui/events/keycodes/dom3/dom_key.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 = 155; |
| 20 #elif defined(OS_MACOSX) | 22 #elif defined(OS_MACOSX) |
| 21 const size_t kExpectedMappedKeyCount = 118; | 23 const size_t kExpectedMappedKeyCount = 118; |
| 22 #else | 24 #else |
| 23 const size_t kExpectedMappedKeyCount = 0; | 25 const size_t kExpectedMappedKeyCount = 0; |
| 24 #endif | 26 #endif |
| 25 | 27 |
| 26 const uint32_t kUsbNonExistentKeycode = 0xffffff; | 28 const uint32_t kUsbNonExistentKeycode = 0xffffff; |
| 27 const uint32_t kUsbUsBackslash = 0x070031; | 29 const uint32_t kUsbUsBackslash = 0x070031; |
| 28 const uint32_t kUsbNonUsHash = 0x070032; | 30 const uint32_t kUsbNonUsHash = 0x070032; |
| 29 | 31 |
| (...skipping 19 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 static_cast<int>(entry->code), |
| 64 ui::KeycodeConverter::NativeKeycodeToCode(entry->native_keycode)); | 66 static_cast<int>(ui::KeycodeConverter::NativeKeycodeToDomCode( |
| 65 } | 67 entry->native_keycode))); |
| 66 else { | 68 } else { |
| 67 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(), | 69 EXPECT_EQ(ui::KeycodeConverter::InvalidNativeKeycode(), |
| 68 ui::KeycodeConverter::CodeToNativeKeycode(entry->code)); | 70 ui::KeycodeConverter::DomCodeToNativeKeycode(entry->code)); |
| 69 } | 71 } |
| 70 | 72 |
| 71 // Verify that the USB or native codes aren't duplicated. | 73 // Verify that the USB or native codes aren't duplicated. |
| 72 EXPECT_EQ(0U, usb_to_native.count(entry->usb_keycode)) | 74 EXPECT_EQ(0U, usb_to_native.count(entry->usb_keycode)) |
| 73 << " duplicate of USB code 0x" << std::hex << std::setfill('0') | 75 << " duplicate of USB code 0x" << std::hex << std::setfill('0') |
| 74 << std::setw(6) << entry->usb_keycode | 76 << std::setw(6) << entry->usb_keycode << " to native 0x" << std::setw(4) |
| 75 << " to native 0x" | 77 << entry->native_keycode << " (previous was 0x" << std::setw(4) |
| 76 << std::setw(4) << entry->native_keycode | 78 << 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; | 79 usb_to_native[entry->usb_keycode] = entry->native_keycode; |
| 81 EXPECT_EQ(0U, native_to_usb.count(entry->native_keycode)) | 80 EXPECT_EQ(0U, native_to_usb.count(entry->native_keycode)) |
| 82 << " duplicate of native code 0x" << std::hex << std::setfill('0') | 81 << " duplicate of native code 0x" << std::hex << std::setfill('0') |
| 83 << std::setw(4) << entry->native_keycode | 82 << std::setw(4) << entry->native_keycode << " to USB 0x" << std::setw(6) |
| 84 << " to USB 0x" | 83 << entry->usb_keycode << " (previous was 0x" << std::setw(6) |
| 85 << std::setw(6) << entry->usb_keycode | 84 << 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; | 85 native_to_usb[entry->native_keycode] = entry->usb_keycode; |
| 90 } | 86 } |
| 91 ASSERT_EQ(usb_to_native.size(), native_to_usb.size()); | 87 ASSERT_EQ(usb_to_native.size(), native_to_usb.size()); |
| 92 | 88 |
| 93 // Verify that the number of mapped keys is what we expect, i.e. we haven't | 89 // 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. | 90 // lost any, and if we've added some then the expectation has been updated. |
| 95 EXPECT_EQ(kExpectedMappedKeyCount, usb_to_native.size()); | 91 EXPECT_EQ(kExpectedMappedKeyCount, usb_to_native.size()); |
| 96 } | 92 } |
| 97 | 93 |
| 98 TEST(UsbKeycodeMap, NonExistent) { | 94 TEST(UsbKeycodeMap, NonExistent) { |
| 99 // Verify that UsbKeycodeToNativeKeycode works for a non-existent USB keycode. | 95 // Verify that UsbKeycodeToNativeKeycode works for a non-existent USB keycode. |
| 100 EXPECT_EQ( | 96 EXPECT_EQ( |
| 101 ui::KeycodeConverter::InvalidNativeKeycode(), | 97 ui::KeycodeConverter::InvalidNativeKeycode(), |
| 102 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonExistentKeycode)); | 98 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonExistentKeycode)); |
| 103 } | 99 } |
| 104 | 100 |
| 105 TEST(UsbKeycodeMap, UsBackslashIsNonUsHash) { | 101 TEST(UsbKeycodeMap, UsBackslashIsNonUsHash) { |
| 106 // Verify that UsbKeycodeToNativeKeycode treats the non-US "hash" key | 102 // Verify that UsbKeycodeToNativeKeycode treats the non-US "hash" key |
| 107 // as equivalent to the US "backslash" key. | 103 // as equivalent to the US "backslash" key. |
| 108 EXPECT_EQ(ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbUsBackslash), | 104 EXPECT_EQ(ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbUsBackslash), |
| 109 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonUsHash)); | 105 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(kUsbNonUsHash)); |
| 110 } | 106 } |
| 111 | 107 |
| 108 TEST(KeycodeConverter, DomCode) { |
| 109 // Test invalid and unknown arguments to CodeStringToDomCode() |
| 110 EXPECT_EQ(ui::DomCode::NONE, |
| 111 ui::KeycodeConverter::CodeStringToDomCode(nullptr)); |
| 112 EXPECT_EQ(ui::DomCode::NONE, ui::KeycodeConverter::CodeStringToDomCode("-")); |
| 113 EXPECT_EQ(ui::DomCode::NONE, ui::KeycodeConverter::CodeStringToDomCode("")); |
| 114 // Round-trip test DOM Level 3 .code strings. |
| 115 const char* s = nullptr; |
| 116 for (size_t i = 0; |
| 117 (s = ui::KeycodeConverter::DomCodeStringForTest(i)) != nullptr; |
| 118 ++i) { |
| 119 SCOPED_TRACE(i); |
| 120 ui::DomCode code = ui::KeycodeConverter::CodeStringToDomCode(s); |
| 121 if (s) { |
| 122 EXPECT_STREQ(s, ui::KeycodeConverter::DomCodeToCodeString(code)); |
| 123 } else { |
| 124 EXPECT_EQ(ui::DomCode::NONE, code); |
| 125 } |
| 126 } |
| 127 } |
| 128 |
| 129 TEST(KeycodeConverter, DomKey) { |
| 130 // Test invalid and unknown arguments to KeyStringToDomKey() |
| 131 EXPECT_EQ(ui::DomKey::NONE, ui::KeycodeConverter::KeyStringToDomKey(nullptr)); |
| 132 EXPECT_EQ(ui::DomKey::NONE, ui::KeycodeConverter::KeyStringToDomKey("-")); |
| 133 // Round-trip test DOM Level 3 .key strings. |
| 134 const char* s = nullptr; |
| 135 for (size_t i = 0; |
| 136 (s = ui::KeycodeConverter::DomKeyStringForTest(i)) != nullptr; |
| 137 ++i) { |
| 138 SCOPED_TRACE(i); |
| 139 ui::DomKey key = ui::KeycodeConverter::KeyStringToDomKey(s); |
| 140 if (s) { |
| 141 EXPECT_STREQ(s, ui::KeycodeConverter::DomKeyToKeyString(key)); |
| 142 } else { |
| 143 EXPECT_EQ(ui::DomKey::NONE, key); |
| 144 } |
| 145 } |
| 146 } |
| 147 |
| 112 } // namespace | 148 } // namespace |
| OLD | NEW |