summaryrefslogtreecommitdiff
path: root/test/test_data_files.py
blob: c2db899759cafec9850ecc945c91ac9e73795537 (plain)
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
#!/usr/bin/env python3
#
# Run with pytest

import configparser
import os
import re
from pathlib import Path


WACOM_RECEIVER_USBIDS = [
    (0x56A, 0x84),
]


def datadir():
    return Path(os.getenv("MESON_SOURCE_ROOT") or ".") / "data"


def layoutsdir():
    return datadir() / "layouts"


def pytest_generate_tests(metafunc):
    # for any function that takes a "tabletfile" argument return the path to
    # a tablet file
    if "tabletfile" in metafunc.fixturenames:
        metafunc.parametrize("tabletfile", [f for f in datadir().glob("*.tablet")])


def test_device_match(tabletfile):
    config = configparser.ConfigParser()
    config.read(tabletfile)

    # Match format must be bus:vid:pid:name
    # where bus is 'usb' or 'bluetooth'
    # where vid/pid is a lowercase hex
    # where name is optional
    for match in config["Device"]["DeviceMatch"].split(";"):
        if not match or match == "generic":
            continue

        bus, vid, pid = match.split(":")[:3]  # skip the name part of the match
        assert bus in [
            "usb",
            "bluetooth",
            "i2c",
            "serial",
        ], f"{tabletfile}: unknown bus type"
        assert re.match(
            "[0-9a-f]{4}", vid
        ), f"{tabletfile}: {vid} must be lowercase hex"
        assert re.match(
            "[0-9a-f]{4}", pid
        ), f"{tabletfile}: {pid} must be lowercase hex"


def test_no_receiver_id(tabletfile):
    config = configparser.ConfigParser(strict=True)
    # Don't convert to lowercase
    config.optionxform = lambda option: option
    config.read(tabletfile)

    receivers = ["usb:{:04x}:{:04x}".format(*r) for r in WACOM_RECEIVER_USBIDS]
    for match in config["Device"]["DeviceMatch"].split(";"):
        assert match not in receivers


def test_svg_exists(tabletfile):
    config = configparser.ConfigParser(strict=True)
    # Don't convert to lowercase
    config.optionxform = lambda option: option
    config.read(tabletfile)

    try:
        svg = config["Device"]["Layout"]
        assert (layoutsdir() / svg).exists()

    except KeyError:
        pass


def test_button_evcodes(tabletfile):
    config = configparser.ConfigParser(strict=True)
    # Don't convert to lowercase
    config.optionxform = lambda option: option
    config.read(tabletfile)

    try:
        nbuttons = int(config["Features"]["Buttons"])
        str = config["Buttons"]["EvdevCodes"]
        codes = [
            c for c in str.split(";") if c
        ]  # drop empty strings from trailing semicolons
        assert len(codes) == nbuttons, "Number of buttons mismatches the EvdevCodes"
    except KeyError:
        pass