aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/hid/tests/test_gamepad.py
blob: 26c74040b796aec82f0bce3ad515d0b36eb0d80c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/env python3
# SPDX-License-Identifier: GPL-2.0
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 Benjamin Tissoires <benjamin.tissoires@gmail.com>
# Copyright (c) 2019 Red Hat, Inc.
#

from . import base
import libevdev
import pytest

from hidtools.device.base_gamepad import AsusGamepad, SaitekGamepad

import logging

logger = logging.getLogger("hidtools.test.gamepad")


class BaseTest:
    class TestGamepad(base.BaseTestCase.TestUhid):
        @pytest.fixture(autouse=True)
        def send_initial_state(self):
            """send an empty report to initialize the axes"""
            uhdev = self.uhdev

            r = uhdev.event()
            events = uhdev.next_sync_events()
            self.debug_reports(r, uhdev, events)

        def assert_button(self, button):
            uhdev = self.uhdev
            evdev = uhdev.get_evdev()
            syn_event = self.syn_event

            buttons = {}
            key = libevdev.evbit(uhdev.buttons_map[button])

            buttons[button] = True
            r = uhdev.event(buttons=buttons)
            expected_event = libevdev.InputEvent(key, 1)
            events = uhdev.next_sync_events()
            self.debug_reports(r, uhdev, events)
            self.assertInputEventsIn((syn_event, expected_event), events)
            assert evdev.value[key] == 1

            buttons[button] = False
            r = uhdev.event(buttons=buttons)
            expected_event = libevdev.InputEvent(key, 0)
            events = uhdev.next_sync_events()
            self.debug_reports(r, uhdev, events)
            self.assertInputEventsIn((syn_event, expected_event), events)
            assert evdev.value[key] == 0

        def test_buttons(self):
            """check for button reliability."""
            uhdev = self.uhdev

            for b in uhdev.buttons:
                self.assert_button(b)

        def test_dual_buttons(self):
            """check for button reliability when pressing 2 buttons"""
            uhdev = self.uhdev
            evdev = uhdev.get_evdev()
            syn_event = self.syn_event

            # can change intended b1 b2 values
            b1 = uhdev.buttons[0]
            key1 = libevdev.evbit(uhdev.buttons_map[b1])
            b2 = uhdev.buttons[1]
            key2 = libevdev.evbit(uhdev.buttons_map[b2])

            buttons = {b1: True, b2: True}
            r = uhdev.event(buttons=buttons)
            expected_event0 = libevdev.InputEvent(key1, 1)
            expected_event1 = libevdev.InputEvent(key2, 1)
            events = uhdev.next_sync_events()
            self.debug_reports(r, uhdev, events)
            self.assertInputEventsIn(
                (syn_event, expected_event0, expected_event1), events
            )
            assert evdev.value[key1] == 1
            assert evdev.value[key2] == 1

            buttons = {b1: False, b2: None}
            r = uhdev.event(buttons=buttons)
            expected_event = libevdev.InputEvent(key1, 0)
            events = uhdev.next_sync_events()
            self.debug_reports(r, uhdev, events)
            self.assertInputEventsIn((syn_event, expected_event), events)
            assert evdev.value[key1] == 0
            assert evdev.value[key2] == 1

            buttons = {b1: None, b2: False}
            r = uhdev.event(buttons=buttons)
            expected_event = libevdev.InputEvent(key2, 0)
            events = uhdev.next_sync_events()
            self.debug_reports(r, uhdev, events)
            self.assertInputEventsIn((syn_event, expected_event), events)
            assert evdev.value[key1] == 0
            assert evdev.value[key2] == 0

        def _get_libevdev_abs_events(self, which):
            """Returns which ABS_* evdev axes are expected for the given stick"""
            abs_map = self.uhdev.axes_map[which]

            x = abs_map["x"].evdev
            y = abs_map["y"].evdev

            assert x
            assert y

            return x, y

        def _test_joystick_press(self, which, data):
            uhdev = self.uhdev

            libevdev_axes = self._get_libevdev_abs_events(which)

            r = None
            if which == "left_stick":
                r = uhdev.event(left=data)
            else:
                r = uhdev.event(right=data)
            events = uhdev.next_sync_events()
            self.debug_reports(r, uhdev, events)

            for i, d in enumerate(data):
                if d is not None and d != 127:
                    assert libevdev.InputEvent(libevdev_axes[i], d) in events
                else:
                    assert libevdev.InputEvent(libevdev_axes[i]) not in events

        def test_left_joystick_press_left(self):
            """check for the left joystick reliability"""
            self._test_joystick_press("left_stick", (63, None))
            self._test_joystick_press("left_stick", (0, 127))

        def test_left_joystick_press_right(self):
            """check for the left joystick reliability"""
            self._test_joystick_press("left_stick", (191, 127))
            self._test_joystick_press("left_stick", (255, None))

        def test_left_joystick_press_up(self):
            """check for the left joystick reliability"""
            self._test_joystick_press("left_stick", (None, 63))
            self._test_joystick_press("left_stick", (127, 0))

        def test_left_joystick_press_down(self):
            """check for the left joystick reliability"""
            self._test_joystick_press("left_stick", (127, 191))
            self._test_joystick_press("left_stick", (None, 255))

        def test_right_joystick_press_left(self):
            """check for the right joystick reliability"""
            self._test_joystick_press("right_stick", (63, None))
            self._test_joystick_press("right_stick", (0, 127))

        def test_right_joystick_press_right(self):
            """check for the right joystick reliability"""
            self._test_joystick_press("right_stick", (191, 127))
            self._test_joystick_press("right_stick", (255, None))

        def test_right_joystick_press_up(self):
            """check for the right joystick reliability"""
            self._test_joystick_press("right_stick", (None, 63))
            self._test_joystick_press("right_stick", (127, 0))

        def test_right_joystick_press_down(self):
            """check for the right joystick reliability"""
            self._test_joystick_press("right_stick", (127, 191))
            self._test_joystick_press("right_stick", (None, 255))

        @pytest.mark.skip_if_uhdev(
            lambda uhdev: "Hat switch" not in uhdev.fields,
            "Device not compatible, missing Hat switch usage",
        )
        @pytest.mark.parametrize(
            "hat_value,expected_evdev,evdev_value",
            [
                (0, "ABS_HAT0Y", -1),
                (2, "ABS_HAT0X", 1),
                (4, "ABS_HAT0Y", 1),
                (6, "ABS_HAT0X", -1),
            ],
        )
        def test_hat_switch(self, hat_value, expected_evdev, evdev_value):
            uhdev = self.uhdev

            r = uhdev.event(hat_switch=hat_value)
            events = uhdev.next_sync_events()
            self.debug_reports(r, uhdev, events)
            assert (
                libevdev.InputEvent(
                    libevdev.evbit("EV_ABS", expected_evdev), evdev_value
                )
                in events
            )


class TestSaitekGamepad(BaseTest.TestGamepad):
    def create_device(self):
        return SaitekGamepad()


class TestAsusGamepad(BaseTest.TestGamepad):
    def create_device(self):
        return AsusGamepad()