Debian Codegraph @

tests/test_uinput.py

raw ◀ ▶
1 Debian patch touch this file
1 # encoding: utf-8
2 import os
3 import stat
4 from select import select
5 from unittest.mock import patch
6
7 import pytest
•8 from pytest import raises, fixture, mark
9
10 from evdev import uinput, ecodes, device, UInputError
11
12 # -----------------------------------------------------------------------------
13 uinput_options = {
14 "name": "test-py-evdev-uinput",
15 "bustype": ecodes.BUS_USB,
16 "vendor": 0x1100,
17 "product": 0x2200,
18 "version": 0x3300,
19 }
20
21
22 @fixture
23 def c():
24 return uinput_options.copy()
25
26
27 def device_exists(bustype, vendor, product, version):
28 match = "I: Bus=%04hx Vendor=%04hx Product=%04hx Version=%04hx"
29 match = match % (bustype, vendor, product, version)
30
31 for line in open("/proc/bus/input/devices"):
32 if line.strip() == match:
33 return True
34
35 return False
36
37
38 # -----------------------------------------------------------------------------
•39 @mark.skip(reason="Requires root")
40 def test_open(c):
41 ui = uinput.UInput(**c)
42 args = (c["bustype"], c["vendor"], c["product"], c["version"])
43 assert device_exists(*args)
44 ui.close()
45 assert not device_exists(*args)
46
47
•48 @mark.skip(reason="Requires root")
49 def test_open_context(c):
50 args = (c["bustype"], c["vendor"], c["product"], c["version"])
51 with uinput.UInput(**c):
52 assert device_exists(*args)
53 assert not device_exists(*args)
54
55
56 def test_maxnamelen(c):
57 with raises(uinput.UInputError):
58 c["name"] = "a" * 150
59 uinput.UInput(**c)
60
61
•62 @mark.skip(reason="Requires root")
63 def test_enable_events(c):
64 e = ecodes
65 c["events"] = {e.EV_KEY: [e.KEY_A, e.KEY_B, e.KEY_C]}
66
67 with uinput.UInput(**c) as ui:
68 cap = ui.capabilities()
69 assert e.EV_KEY in cap
70 assert sorted(cap[e.EV_KEY]) == sorted(c["events"][e.EV_KEY])
71
72
•73 @mark.skip(reason="Requires root")
74 def test_enable_event_type_without_codes(c):
75 e = ecodes
76 c["events"] = {e.EV_KEY: [e.KEY_A], e.EV_REP: []}
77
78 with uinput.UInput(**c) as ui:
79 repeat = ui.device.repeat
80 assert repeat.delay > 0
81 assert repeat.repeat > 0
82
83
•84 @mark.skip(reason="Requires root")
85 def test_abs_values(c):
86 e = ecodes
87 c = {
88 e.EV_KEY: [e.KEY_A, e.KEY_B],
89 e.EV_ABS: [(e.ABS_X, (0, 0, 255, 0, 0)), (e.ABS_Y, device.AbsInfo(0, 0, 255, 5, 10, 0))],
90 }
91
92 with uinput.UInput(events=c) as ui:
93 c = ui.capabilities()
94 abs = device.AbsInfo(value=0, min=0, max=255, fuzz=0, flat=0, resolution=0)
95 assert c[e.EV_ABS][0] == (0, abs)
96
97 abs = device.AbsInfo(value=0, min=0, max=255, fuzz=5, flat=10, resolution=0)
98 assert c[e.EV_ABS][1] == (1, abs)
99
100 c = ui.capabilities(verbose=True)
101 abs = device.AbsInfo(value=0, min=0, max=255, fuzz=0, flat=0, resolution=0)
102 assert c[("EV_ABS", 3)][0] == (("ABS_X", 0), abs)
103
104 c = ui.capabilities(verbose=False, absinfo=False)
105 assert c[e.EV_ABS] == list((0, 1))
106
107
•108 @mark.skip(reason="Requires root")
109 def test_write(c):
110 with uinput.UInput(**c) as ui:
111 d = ui.device
112 wrote = False
113
114 while True:
115 r, w, x = select([d], [d], [])
116
117 if w and not wrote:
118 ui.write(ecodes.EV_KEY, ecodes.KEY_P, 1) # KEY_P down
119 ui.write(ecodes.EV_KEY, ecodes.KEY_P, 1) # KEY_P down
120 ui.write(ecodes.EV_KEY, ecodes.KEY_P, 0) # KEY_P up
121 ui.write(ecodes.EV_KEY, ecodes.KEY_A, 1) # KEY_A down
122 ui.write(ecodes.EV_KEY, ecodes.KEY_A, 2) # KEY_A hold
123 ui.write(ecodes.EV_KEY, ecodes.KEY_A, 0) # KEY_P up
124 ui.syn()
125 wrote = True
126
127 if r:
128 evs = list(d.read())
129
130 assert evs[0].code == ecodes.KEY_P and evs[0].value == 1
131 assert evs[1].code == ecodes.KEY_P and evs[1].value == 0
132 assert evs[2].code == ecodes.KEY_A and evs[2].value == 1
133 assert evs[3].code == ecodes.KEY_A and evs[3].value == 2
134 assert evs[4].code == ecodes.KEY_A and evs[4].value == 0
135 break
136
137
138 @patch.object(stat, 'S_ISCHR', return_value=False)
139 def test_not_a_character_device(ischr_mock, c):
140 with pytest.raises(UInputError, match='not a character device file'):
141 uinput.UInput(**c)
142
143 @patch.object(stat, 'S_ISCHR', return_value=True)
144 @patch.object(os, 'stat', side_effect=OSError())
145 def test_not_a_character_device_2(stat_mock, ischr_mock, c):
146 with pytest.raises(UInputError, match='not a character device file'):
147 uinput.UInput(**c)
148
149 @patch.object(stat, 'S_ISCHR', return_value=True)
150 @patch.object(os, 'stat', return_value=[])
151 def test_not_a_character_device_3(stat_mock, ischr_mock, c):
152 with pytest.raises(UInputError, match='not a character device file'):
153 uinput.UInput(**c)