Chromium Code Reviews| Index: ui/events/ozone/gamepad/generic_gamepad_mapping_unittest.cc |
| diff --git a/ui/events/ozone/gamepad/generic_gamepad_mapping_unittest.cc b/ui/events/ozone/gamepad/generic_gamepad_mapping_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1e6c5a7444969e08b80b254d16b2da257dd5aff8 |
| --- /dev/null |
| +++ b/ui/events/ozone/gamepad/generic_gamepad_mapping_unittest.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/events/ozone/gamepad/generic_gamepad_mapping.h" |
| + |
| +#include <errno.h> |
| +#include <fcntl.h> |
| +#include <linux/input.h> |
| +#include <unistd.h> |
| + |
| +#include <memory> |
| +#include <queue> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_util.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/posix/eintr_wrapper.h" |
| +#include "base/run_loop.h" |
| +#include "base/time/time.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/events/event.h" |
| +#include "ui/events/ozone/device/device_manager.h" |
| +#include "ui/events/ozone/evdev/event_converter_test_util.h" |
| +#include "ui/events/ozone/evdev/event_device_info.h" |
| +#include "ui/events/ozone/evdev/event_device_test_util.h" |
| +#include "ui/events/ozone/evdev/event_device_util.h" |
| +#include "ui/events/ozone/evdev/event_factory_evdev.h" |
| +#include "ui/events/ozone/gamepad/gamepad_event.h" |
| +#include "ui/events/ozone/gamepad/gamepad_observer.h" |
| +#include "ui/events/ozone/gamepad/gamepad_provider_ozone.h" |
| +#include "ui/events/ozone/gamepad/static_gamepad_mapping.h" |
| +#include "ui/events/ozone/gamepad/webgamepad_constants.h" |
| +#include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" |
| +#include "ui/events/platform/platform_event_dispatcher.h" |
| +#include "ui/events/platform/platform_event_source.h" |
| + |
| +namespace { |
| + |
| +struct input_id { |
|
spang
2017/06/01 05:08:13
InputId
jkwang
2017/06/02 22:03:52
Acknowledged.
|
| + uint16_t vendor_id; |
| + uint16_t product_id; |
| +}; |
| + |
| +const input_id test_ids[] = { |
|
spang
2017/06/01 05:08:13
kTestIds
jkwang
2017/06/02 22:03:52
Acknowledged.
|
| + {0x045e, 0x028e}, // Xbox |
| + {0x0b05, 0x4500} // ADT 1 |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace ui { |
| + |
| +class GenericGamepadMappingTest : public testing::Test { |
| + public: |
| + GenericGamepadMappingTest() {} |
| + |
| + void FakeDevinfoFromMapper(GamepadMapper* static_mapper, |
| + EventDeviceInfo* dev_info) { |
| + unsigned long key_bits[EVDEV_BITS_TO_LONGS(KEY_CNT)]; |
| + unsigned long abs_bits[EVDEV_BITS_TO_LONGS(ABS_CNT)]; |
| + for (size_t i = 0; i < arraysize(key_bits); i++) { |
| + key_bits[i] = 0; |
| + } |
| + |
| + for (size_t i = 0; i < arraysize(abs_bits); i++) { |
| + abs_bits[i] = 0; |
| + } |
| + GamepadEventType type; |
| + uint16_t mapped_code; |
| + |
| + for (uint16_t code = BTN_MISC; code < KEY_MAX; code++) { |
| + if ((*static_mapper)(EV_KEY, code, &type, &mapped_code)) { |
| + EvdevSetBit(key_bits, code); |
| + } |
| + } |
| + for (uint16_t code = ABS_X; code < ABS_MAX; code++) { |
| + if ((*static_mapper)(EV_ABS, code, &type, &mapped_code)) { |
| + EvdevSetBit(abs_bits, code); |
| + } |
| + } |
| + dev_info->SetKeyEvents(key_bits, KEY_CNT); |
| + dev_info->SetAbsEvents(abs_bits, ABS_CNT); |
| + } |
| + |
| + void CompareGamepadMapper(GamepadMapper* l_mapper, GamepadMapper* r_mapper) { |
|
spang
2017/06/01 05:08:13
const& arguments
jkwang
2017/06/02 22:03:52
Acknowledged.
|
| + bool l_result, r_result; |
| + GamepadEventType l_mapped_type, r_mapped_type; |
| + uint16_t l_mapped_code, r_mapped_code; |
| + for (uint16_t code = BTN_MISC; code < KEY_MAX; code++) { |
| + l_result = (*l_mapper)(EV_KEY, code, &l_mapped_type, &l_mapped_code); |
| + r_result = (*r_mapper)(EV_KEY, code, &r_mapped_type, &r_mapped_code); |
| + EXPECT_EQ(l_result, r_result) << " Current Code: " << code; |
| + if (l_result) { |
| + EXPECT_EQ(l_mapped_type, r_mapped_type); |
| + EXPECT_EQ(r_mapped_code, r_mapped_code); |
| + } |
| + } |
| + for (uint16_t code = ABS_X; code < ABS_MAX; code++) { |
| + l_result = (*l_mapper)(EV_ABS, code, &l_mapped_type, &l_mapped_code); |
| + r_result = (*r_mapper)(EV_ABS, code, &r_mapped_type, &r_mapped_code); |
| + EXPECT_EQ(l_result, r_result); |
| + if (l_result) { |
| + EXPECT_EQ(l_mapped_type, r_mapped_type); |
| + EXPECT_EQ(r_mapped_code, r_mapped_code); |
| + } |
| + } |
| + } |
| +}; |
| + |
| +TEST_F(GenericGamepadMappingTest, GenericMappingCompatibility) { |
| + for (size_t i = 0; i < arraysize(test_ids); i++) { |
| + GamepadMapper* s_mapper = |
|
spang
2017/06/01 05:08:13
static_mapper
|
| + GetStaticGamepadMapper(test_ids[i].vendor_id, test_ids[i].product_id); |
| + EventDeviceInfo dev_info; |
| + FakeDevinfoFromMapper(s_mapper, &dev_info); |
|
spang
2017/06/01 05:08:13
I'd prefer testing with real data. Can you check i
jkwang
2017/06/02 22:03:52
Acknowledged.
|
| + GenericGamepadMapping g_mapper(dev_info); |
|
spang
2017/06/01 05:08:13
generic_mapper
jkwang
2017/06/02 22:03:52
Acknowledged.
|
| + CompareGamepadMapper(s_mapper, &g_mapper); |
| + delete s_mapper; |
| + } |
| +} |
| + |
| +} // namespace ui |