From f350714a754aef6a2f83c590ff1e5cc87650ca68 Mon Sep 17 00:00:00 2001 From: sezanzeb Date: Wed, 3 Feb 2021 19:42:26 +0100 Subject: [PATCH] added tests for the previous commit --- keymapper/getdevices.py | 3 ++ tests/testcases/test_getdevices.py | 52 +++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/keymapper/getdevices.py b/keymapper/getdevices.py index 0e339eaa..25ef4bbe 100644 --- a/keymapper/getdevices.py +++ b/keymapper/getdevices.py @@ -71,6 +71,9 @@ def is_gamepad(device): # a joystick return True + if evdev.ecodes.ABS_Y in abs_capabilities: + return True + return False diff --git a/tests/testcases/test_getdevices.py b/tests/testcases/test_getdevices.py index 2f6e4507..7b52909c 100644 --- a/tests/testcases/test_getdevices.py +++ b/tests/testcases/test_getdevices.py @@ -23,7 +23,10 @@ import unittest import evdev -from keymapper.getdevices import _GetDevices, get_devices, is_gamepad +from keymapper.getdevices import _GetDevices, get_devices, is_gamepad, \ + refresh_devices + +from tests.test import cleanup, fixtures class FakePipe: @@ -34,6 +37,13 @@ class FakePipe: class TestGetDevices(unittest.TestCase): + def setUp(self): + self.original_list_devices = evdev.list_devices + + def tearDown(self): + evdev.list_devices = self.original_list_devices + cleanup() + def test_get_devices(self): pipe = FakePipe() _GetDevices(pipe).run() @@ -96,6 +106,46 @@ class TestGetDevices(unittest.TestCase): }, }) + def test_skip_camera(self): + def list_devices(): + return ['/foo/bar', '/dev/input/event30'] + + fixtures['/foo/bar'] = { + 'name': 'camera', 'phys': 'abcd1', + 'info': evdev.DeviceInfo(1, 2, 3, 4), + 'capabilities': { + evdev.ecodes.EV_KEY: [ + evdev.ecodes.KEY_CAMERA + ] + } + } + + evdev.list_devices = list_devices + + refresh_devices() + self.assertNotIn('camera', get_devices()) + self.assertIn('gamepad', get_devices()) + + def test_gamepad_without_ev_key(self): + def list_devices(): + return ['/foo/bar', '/dev/input/event30'] + + fixtures['/foo/bar'] = { + 'name': 'gamepad2', 'phys': 'abcd2', + 'info': evdev.DeviceInfo(1, 2, 3, 4), + 'capabilities': { + evdev.ecodes.EV_ABS: [ + evdev.ecodes.ABS_X + ] + } + } + + evdev.list_devices = list_devices + + refresh_devices() + self.assertIn('gamepad', get_devices()) + self.assertIn('gamepad2', get_devices()) + def test_is_gamepad(self): # properly detects if the device is a gamepad EV_ABS = evdev.ecodes.EV_ABS