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
12using ui::KeycodeConverter;
13
14namespace {
15
16#if defined(OS_WIN)
17const size_t kExpectedMappedKeyCount = 138;
18#elif defined(OS_LINUX)
19const size_t kExpectedMappedKeyCount = 145;
20#elif defined(OS_MACOSX)
21const size_t kExpectedMappedKeyCount = 118;
22#else
23const size_t kExpectedMappedKeyCount = 0;
24#endif
25
26const uint32_t kUsbNonExistentKeycode = 0xffffff;
27const uint32_t kUsbUsBackslash =        0x070031;
28const uint32_t kUsbNonUsHash =          0x070032;
29
30TEST(UsbKeycodeMap, Basic) {
31  ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
32  // Verify that the first element in the table is the "invalid" code.
33  const ui::KeycodeMapEntry* keycode_map =
34      key_converter->GetKeycodeMapForTest();
35  EXPECT_EQ(key_converter->InvalidUsbKeycode(), keycode_map[0].usb_keycode);
36  EXPECT_EQ(key_converter->InvalidNativeKeycode(),
37            keycode_map[0].native_keycode);
38  EXPECT_STREQ(key_converter->InvalidKeyboardEventCode(), "Unidentified");
39  EXPECT_EQ(key_converter->InvalidNativeKeycode(),
40            key_converter->CodeToNativeKeycode("Unidentified"));
41
42  // Verify that there are no duplicate entries in the mapping.
43  std::map<uint32_t, uint16_t> usb_to_native;
44  std::map<uint16_t, uint32_t> native_to_usb;
45  size_t numEntries = key_converter->NumKeycodeMapEntriesForTest();
46  for (size_t i = 0; i < numEntries; ++i) {
47    const ui::KeycodeMapEntry* entry = &keycode_map[i];
48    // Don't test keys with no native keycode mapping on this platform.
49    if (entry->native_keycode == key_converter->InvalidNativeKeycode())
50      continue;
51
52    // Verify UsbKeycodeToNativeKeycode works for this key.
53    EXPECT_EQ(entry->native_keycode,
54              key_converter->UsbKeycodeToNativeKeycode(entry->usb_keycode));
55
56    // Verify CodeToNativeKeycode and NativeKeycodeToCode work correctly.
57    if (entry->code) {
58      EXPECT_EQ(entry->native_keycode,
59                key_converter->CodeToNativeKeycode(entry->code));
60      EXPECT_STREQ(entry->code,
61                   key_converter->NativeKeycodeToCode(entry->native_keycode));
62    }
63    else {
64      EXPECT_EQ(key_converter->InvalidNativeKeycode(),
65                key_converter->CodeToNativeKeycode(entry->code));
66    }
67
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;
87  }
88  ASSERT_EQ(usb_to_native.size(), native_to_usb.size());
89
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());
93}
94
95TEST(UsbKeycodeMap, NonExistent) {
96  // Verify that UsbKeycodeToNativeKeycode works for a non-existent USB keycode.
97  ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
98  EXPECT_EQ(key_converter->InvalidNativeKeycode(),
99            key_converter->UsbKeycodeToNativeKeycode(kUsbNonExistentKeycode));
100}
101
102TEST(UsbKeycodeMap, UsBackslashIsNonUsHash) {
103  // Verify that UsbKeycodeToNativeKeycode treats the non-US "hash" key
104  // as equivalent to the US "backslash" key.
105  ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
106  EXPECT_EQ(key_converter->UsbKeycodeToNativeKeycode(kUsbUsBackslash),
107            key_converter->UsbKeycodeToNativeKeycode(kUsbNonUsHash));
108}
109
110}  // namespace
111