OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/ozone/gamepad/generic_gamepad_mapping.h" | |
6 | |
7 #include <errno.h> | |
8 #include <fcntl.h> | |
9 #include <linux/input.h> | |
10 #include <unistd.h> | |
11 | |
12 #include <memory> | |
13 #include <queue> | |
14 #include <utility> | |
15 #include <vector> | |
16 | |
17 #include "base/bind.h" | |
18 #include "base/files/file_util.h" | |
19 #include "base/macros.h" | |
20 #include "base/memory/ptr_util.h" | |
21 #include "base/posix/eintr_wrapper.h" | |
22 #include "base/run_loop.h" | |
23 #include "base/time/time.h" | |
24 #include "testing/gmock/include/gmock/gmock.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 #include "ui/events/event.h" | |
27 #include "ui/events/ozone/device/device_manager.h" | |
28 #include "ui/events/ozone/evdev/event_converter_test_util.h" | |
29 #include "ui/events/ozone/evdev/event_device_info.h" | |
30 #include "ui/events/ozone/evdev/event_device_test_util.h" | |
31 #include "ui/events/ozone/evdev/event_device_util.h" | |
32 #include "ui/events/ozone/evdev/event_factory_evdev.h" | |
33 #include "ui/events/ozone/gamepad/gamepad_event.h" | |
34 #include "ui/events/ozone/gamepad/gamepad_observer.h" | |
35 #include "ui/events/ozone/gamepad/gamepad_provider_ozone.h" | |
36 #include "ui/events/ozone/gamepad/static_gamepad_mapping.h" | |
37 #include "ui/events/ozone/gamepad/webgamepad_constants.h" | |
38 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" | |
39 #include "ui/events/platform/platform_event_dispatcher.h" | |
40 #include "ui/events/platform/platform_event_source.h" | |
41 | |
42 namespace { | |
43 | |
44 struct input_id { | |
spang
2017/06/01 05:08:13
InputId
jkwang
2017/06/02 22:03:52
Acknowledged.
| |
45 uint16_t vendor_id; | |
46 uint16_t product_id; | |
47 }; | |
48 | |
49 const input_id test_ids[] = { | |
spang
2017/06/01 05:08:13
kTestIds
jkwang
2017/06/02 22:03:52
Acknowledged.
| |
50 {0x045e, 0x028e}, // Xbox | |
51 {0x0b05, 0x4500} // ADT 1 | |
52 }; | |
53 | |
54 } // namespace | |
55 | |
56 namespace ui { | |
57 | |
58 class GenericGamepadMappingTest : public testing::Test { | |
59 public: | |
60 GenericGamepadMappingTest() {} | |
61 | |
62 void FakeDevinfoFromMapper(GamepadMapper* static_mapper, | |
63 EventDeviceInfo* dev_info) { | |
64 unsigned long key_bits[EVDEV_BITS_TO_LONGS(KEY_CNT)]; | |
65 unsigned long abs_bits[EVDEV_BITS_TO_LONGS(ABS_CNT)]; | |
66 for (size_t i = 0; i < arraysize(key_bits); i++) { | |
67 key_bits[i] = 0; | |
68 } | |
69 | |
70 for (size_t i = 0; i < arraysize(abs_bits); i++) { | |
71 abs_bits[i] = 0; | |
72 } | |
73 GamepadEventType type; | |
74 uint16_t mapped_code; | |
75 | |
76 for (uint16_t code = BTN_MISC; code < KEY_MAX; code++) { | |
77 if ((*static_mapper)(EV_KEY, code, &type, &mapped_code)) { | |
78 EvdevSetBit(key_bits, code); | |
79 } | |
80 } | |
81 for (uint16_t code = ABS_X; code < ABS_MAX; code++) { | |
82 if ((*static_mapper)(EV_ABS, code, &type, &mapped_code)) { | |
83 EvdevSetBit(abs_bits, code); | |
84 } | |
85 } | |
86 dev_info->SetKeyEvents(key_bits, KEY_CNT); | |
87 dev_info->SetAbsEvents(abs_bits, ABS_CNT); | |
88 } | |
89 | |
90 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.
| |
91 bool l_result, r_result; | |
92 GamepadEventType l_mapped_type, r_mapped_type; | |
93 uint16_t l_mapped_code, r_mapped_code; | |
94 for (uint16_t code = BTN_MISC; code < KEY_MAX; code++) { | |
95 l_result = (*l_mapper)(EV_KEY, code, &l_mapped_type, &l_mapped_code); | |
96 r_result = (*r_mapper)(EV_KEY, code, &r_mapped_type, &r_mapped_code); | |
97 EXPECT_EQ(l_result, r_result) << " Current Code: " << code; | |
98 if (l_result) { | |
99 EXPECT_EQ(l_mapped_type, r_mapped_type); | |
100 EXPECT_EQ(r_mapped_code, r_mapped_code); | |
101 } | |
102 } | |
103 for (uint16_t code = ABS_X; code < ABS_MAX; code++) { | |
104 l_result = (*l_mapper)(EV_ABS, code, &l_mapped_type, &l_mapped_code); | |
105 r_result = (*r_mapper)(EV_ABS, code, &r_mapped_type, &r_mapped_code); | |
106 EXPECT_EQ(l_result, r_result); | |
107 if (l_result) { | |
108 EXPECT_EQ(l_mapped_type, r_mapped_type); | |
109 EXPECT_EQ(r_mapped_code, r_mapped_code); | |
110 } | |
111 } | |
112 } | |
113 }; | |
114 | |
115 TEST_F(GenericGamepadMappingTest, GenericMappingCompatibility) { | |
116 for (size_t i = 0; i < arraysize(test_ids); i++) { | |
117 GamepadMapper* s_mapper = | |
spang
2017/06/01 05:08:13
static_mapper
| |
118 GetStaticGamepadMapper(test_ids[i].vendor_id, test_ids[i].product_id); | |
119 EventDeviceInfo dev_info; | |
120 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.
| |
121 GenericGamepadMapping g_mapper(dev_info); | |
spang
2017/06/01 05:08:13
generic_mapper
jkwang
2017/06/02 22:03:52
Acknowledged.
| |
122 CompareGamepadMapper(s_mapper, &g_mapper); | |
123 delete s_mapper; | |
124 } | |
125 } | |
126 | |
127 } // namespace ui | |
OLD | NEW |