This source file includes following definitions.
- TEST
- TEST
- TEST
#include "ui/events/keycodes/dom4/keycode_converter.h"
#include <map>
#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest.h"
using ui::KeycodeConverter;
namespace {
#if defined(OS_WIN)
const size_t kExpectedMappedKeyCount = 138;
#elif defined(OS_LINUX)
const size_t kExpectedMappedKeyCount = 145;
#elif defined(OS_MACOSX)
const size_t kExpectedMappedKeyCount = 118;
#else
const size_t kExpectedMappedKeyCount = 0;
#endif
const uint32_t kUsbNonExistentKeycode = 0xffffff;
const uint32_t kUsbUsBackslash = 0x070031;
const uint32_t kUsbNonUsHash = 0x070032;
TEST(UsbKeycodeMap, Basic) {
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
const ui::KeycodeMapEntry* keycode_map =
key_converter->GetKeycodeMapForTest();
EXPECT_EQ(key_converter->InvalidUsbKeycode(), keycode_map[0].usb_keycode);
EXPECT_EQ(key_converter->InvalidNativeKeycode(),
keycode_map[0].native_keycode);
EXPECT_STREQ(key_converter->InvalidKeyboardEventCode(), "Unidentified");
EXPECT_EQ(key_converter->InvalidNativeKeycode(),
key_converter->CodeToNativeKeycode("Unidentified"));
std::map<uint32_t, uint16_t> usb_to_native;
std::map<uint16_t, uint32_t> native_to_usb;
size_t numEntries = key_converter->NumKeycodeMapEntriesForTest();
for (size_t i = 0; i < numEntries; ++i) {
const ui::KeycodeMapEntry* entry = &keycode_map[i];
if (entry->native_keycode == key_converter->InvalidNativeKeycode())
continue;
EXPECT_EQ(entry->native_keycode,
key_converter->UsbKeycodeToNativeKeycode(entry->usb_keycode));
if (entry->code) {
EXPECT_EQ(entry->native_keycode,
key_converter->CodeToNativeKeycode(entry->code));
EXPECT_STREQ(entry->code,
key_converter->NativeKeycodeToCode(entry->native_keycode));
}
else {
EXPECT_EQ(key_converter->InvalidNativeKeycode(),
key_converter->CodeToNativeKeycode(entry->code));
}
EXPECT_EQ(0U, usb_to_native.count(entry->usb_keycode))
<< " duplicate of USB code 0x" << std::hex << std::setfill('0')
<< std::setw(6) << entry->usb_keycode
<< " to native 0x"
<< std::setw(4) << entry->native_keycode
<< " (previous was 0x"
<< std::setw(4) << usb_to_native[entry->usb_keycode]
<< ")";
usb_to_native[entry->usb_keycode] = entry->native_keycode;
EXPECT_EQ(0U, native_to_usb.count(entry->native_keycode))
<< " duplicate of native code 0x" << std::hex << std::setfill('0')
<< std::setw(4) << entry->native_keycode
<< " to USB 0x"
<< std::setw(6) << entry->usb_keycode
<< " (previous was 0x"
<< std::setw(6) << native_to_usb[entry->native_keycode]
<< ")";
native_to_usb[entry->native_keycode] = entry->usb_keycode;
}
ASSERT_EQ(usb_to_native.size(), native_to_usb.size());
EXPECT_EQ(kExpectedMappedKeyCount, usb_to_native.size());
}
TEST(UsbKeycodeMap, NonExistent) {
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
EXPECT_EQ(key_converter->InvalidNativeKeycode(),
key_converter->UsbKeycodeToNativeKeycode(kUsbNonExistentKeycode));
}
TEST(UsbKeycodeMap, UsBackslashIsNonUsHash) {
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
EXPECT_EQ(key_converter->UsbKeycodeToNativeKeycode(kUsbUsBackslash),
key_converter->UsbKeycodeToNativeKeycode(kUsbNonUsHash));
}
}