| 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 |
| 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 |
def test_open(c): |
| 40 |
ui = uinput.UInput(**c) |
| 41 |
args = (c["bustype"], c["vendor"], c["product"], c["version"]) |
| 42 |
assert device_exists(*args) |
| 43 |
ui.close() |
| 44 |
assert not device_exists(*args) |
| 45 |
|
| 46 |
|
| 47 |
def test_open_context(c): |
| 48 |
args = (c["bustype"], c["vendor"], c["product"], c["version"]) |
| 49 |
with uinput.UInput(**c): |
| 50 |
assert device_exists(*args) |
| 51 |
assert not device_exists(*args) |
| 52 |
|
| 53 |
|
| 54 |
def test_maxnamelen(c): |
| 55 |
with raises(uinput.UInputError): |
| 56 |
c["name"] = "a" * 150 |
| 57 |
uinput.UInput(**c) |
| 58 |
|
| 59 |
|
| 60 |
def test_enable_events(c): |
| 61 |
e = ecodes |
| 62 |
c["events"] = {e.EV_KEY: [e.KEY_A, e.KEY_B, e.KEY_C]} |
| 63 |
|
| 64 |
with uinput.UInput(**c) as ui: |
| 65 |
cap = ui.capabilities() |
| 66 |
assert e.EV_KEY in cap |
| 67 |
assert sorted(cap[e.EV_KEY]) == sorted(c["events"][e.EV_KEY]) |
| 68 |
|
| 69 |
|
| 70 |
def test_enable_event_type_without_codes(c): |
| 71 |
e = ecodes |
| 72 |
c["events"] = {e.EV_KEY: [e.KEY_A], e.EV_REP: []} |
| 73 |
|
| 74 |
with uinput.UInput(**c) as ui: |
| 75 |
repeat = ui.device.repeat |
| 76 |
assert repeat.delay > 0 |
| 77 |
assert repeat.repeat > 0 |
| 78 |
|
| 79 |
|
| 80 |
def test_abs_values(c): |
| 81 |
e = ecodes |
| 82 |
c = { |
| 83 |
e.EV_KEY: [e.KEY_A, e.KEY_B], |
| 84 |
e.EV_ABS: [(e.ABS_X, (0, 0, 255, 0, 0)), (e.ABS_Y, device.AbsInfo(0, 0, 255, 5, 10, 0))], |
| 85 |
} |
| 86 |
|
| 87 |
with uinput.UInput(events=c) as ui: |
| 88 |
c = ui.capabilities() |
| 89 |
abs = device.AbsInfo(value=0, min=0, max=255, fuzz=0, flat=0, resolution=0) |
| 90 |
assert c[e.EV_ABS][0] == (0, abs) |
| 91 |
|
| 92 |
abs = device.AbsInfo(value=0, min=0, max=255, fuzz=5, flat=10, resolution=0) |
| 93 |
assert c[e.EV_ABS][1] == (1, abs) |
| 94 |
|
| 95 |
c = ui.capabilities(verbose=True) |
| 96 |
abs = device.AbsInfo(value=0, min=0, max=255, fuzz=0, flat=0, resolution=0) |
| 97 |
assert c[("EV_ABS", 3)][0] == (("ABS_X", 0), abs) |
| 98 |
|
| 99 |
c = ui.capabilities(verbose=False, absinfo=False) |
| 100 |
assert c[e.EV_ABS] == list((0, 1)) |
| 101 |
|
| 102 |
|
| 103 |
def test_write(c): |
| 104 |
with uinput.UInput(**c) as ui: |
| 105 |
d = ui.device |
| 106 |
wrote = False |
| 107 |
|
| 108 |
while True: |
| 109 |
r, w, x = select([d], [d], []) |
| 110 |
|
| 111 |
if w and not wrote: |
| 112 |
ui.write(ecodes.EV_KEY, ecodes.KEY_P, 1) # KEY_P down |
| 113 |
ui.write(ecodes.EV_KEY, ecodes.KEY_P, 1) # KEY_P down |
| 114 |
ui.write(ecodes.EV_KEY, ecodes.KEY_P, 0) # KEY_P up |
| 115 |
ui.write(ecodes.EV_KEY, ecodes.KEY_A, 1) # KEY_A down |
| 116 |
ui.write(ecodes.EV_KEY, ecodes.KEY_A, 2) # KEY_A hold |
| 117 |
ui.write(ecodes.EV_KEY, ecodes.KEY_A, 0) # KEY_P up |
| 118 |
ui.syn() |
| 119 |
wrote = True |
| 120 |
|
| 121 |
if r: |
| 122 |
evs = list(d.read()) |
| 123 |
|
| 124 |
assert evs[0].code == ecodes.KEY_P and evs[0].value == 1 |
| 125 |
assert evs[1].code == ecodes.KEY_P and evs[1].value == 0 |
| 126 |
assert evs[2].code == ecodes.KEY_A and evs[2].value == 1 |
| 127 |
assert evs[3].code == ecodes.KEY_A and evs[3].value == 2 |
| 128 |
assert evs[4].code == ecodes.KEY_A and evs[4].value == 0 |
| 129 |
break |
| 130 |
|
| 131 |
|
| 132 |
@patch.object(stat, 'S_ISCHR', return_value=False) |
| 133 |
def test_not_a_character_device(ischr_mock, c): |
| 134 |
with pytest.raises(UInputError, match='not a character device file'): |
| 135 |
uinput.UInput(**c) |
| 136 |
|
| 137 |
@patch.object(stat, 'S_ISCHR', return_value=True) |
| 138 |
@patch.object(os, 'stat', side_effect=OSError()) |
| 139 |
def test_not_a_character_device_2(stat_mock, 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', return_value=[]) |
| 145 |
def test_not_a_character_device_3(stat_mock, ischr_mock, c): |
| 146 |
with pytest.raises(UInputError, match='not a character device file'): |
| 147 |
uinput.UInput(**c) |