Skip to content

Commit

Permalink
test: add support for testing new_from_path() through uinput
Browse files Browse the repository at this point in the history
  • Loading branch information
whot committed Jun 19, 2024
1 parent 76ab3e2 commit fa92a55
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
6 changes: 6 additions & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,12 @@ def new_from_name(self, name: str) -> Optional[WacomDevice]:
device = self.libwacom_new_from_name(name.encode("utf-8"), 0)
return WacomDevice(device) if device else None

def new_from_path(
self, path: str, fallback: Fallback = Fallback.NONE
) -> Optional[WacomDevice]:
device = self.libwacom_new_from_path(path.encode("utf-8"), fallback, 0)
return WacomDevice(device) if device else None

def new_from_usbid(self, vid: int, pid: int) -> Optional[WacomDevice]:
device = self.libwacom_new_from_usbid(vid, pid, 0)
return WacomDevice(device) if device else None
Expand Down
69 changes: 67 additions & 2 deletions test/test_libwacom.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,11 @@ def test_dont_ignore_exact_matches(custom_datadir):
assert device is None


# Emulates the behavior of new_from_path for an unknown device
@pytest.mark.parametrize("fallback", (WacomDatabase.Fallback.NONE, WacomDatabase.Fallback.GENERIC))
# Emulates the behavior of new_from_path for an unknown device but without
# uinput devices
@pytest.mark.parametrize(
"fallback", (WacomDatabase.Fallback.NONE, WacomDatabase.Fallback.GENERIC)
)
@pytest.mark.parametrize("bustype", (WacomBustype.USB, WacomBustype.BLUETOOTH))
def test_new_unknown_device_with_fallback(custom_datadir, fallback, bustype):
USBID = (0x1234, 0x5678)
Expand All @@ -402,3 +405,65 @@ def test_new_unknown_device_with_fallback(custom_datadir, fallback, bustype):
assert device.name == NAME
else:
assert device is None


def create_uinput(name, vid, pid):
libevdev = pytest.importorskip("libevdev")
dev = libevdev.Device()
dev.name = name
dev.id = {"bustype": 0x3, "vendor": vid, "product": pid}
dev.enable(
libevdev.EV_ABS.ABS_X,
libevdev.InputAbsInfo(minimum=0, maximum=10000, resolution=200),
)
dev.enable(
libevdev.EV_ABS.ABS_Y,
libevdev.InputAbsInfo(minimum=0, maximum=10000, resolution=200),
)
dev.enable(libevdev.EV_KEY.BTN_STYLUS)
dev.enable(libevdev.EV_KEY.BTN_TOOL_PEN)
try:
return dev.create_uinput_device()
except OSError as e:
pytest.skip(f"Failed to create uinput device: {e}")


@pytest.mark.parametrize(
"fallback", (WacomDatabase.Fallback.NONE, WacomDatabase.Fallback.GENERIC)
)
def test_new_from_path_known_device(fallback):
name = "Wacom Intuos4 WL"
vid = 0x056A
pid = 0x00BC
uinput = create_uinput(name, vid, pid)

db = WacomDatabase()
dev = db.new_from_path(
uinput.devnode, fallback=fallback
) # fallback has no effect here
assert dev is not None
assert dev.name == name
assert dev.vendor_id == vid
assert dev.product_id == pid


@pytest.mark.parametrize(
"fallback", (WacomDatabase.Fallback.NONE, WacomDatabase.Fallback.GENERIC)
)
def test_new_from_path_unknown_device(fallback):
name = "Unknown device"
vid = 0x1234
pid = 0xABAC
uinput = create_uinput(name, vid, pid)

db = WacomDatabase()
dev = db.new_from_path(
uinput.devnode, fallback=fallback
) # fallback has no effect here
if fallback == WacomDatabase.Fallback.NONE:
assert dev is None
else:
assert dev is not None
assert dev.name == name
assert dev.vendor_id == 0
assert dev.product_id == 0

0 comments on commit fa92a55

Please sign in to comment.