| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Run with pytest |
| 4 |
|
| 5 |
from typing import Optional, List |
| 6 |
from pathlib import Path |
| 7 |
from dataclasses import dataclass |
| 8 |
import xml.etree |
| 9 |
|
| 10 |
import os |
| 11 |
import logging |
| 12 |
import string |
| 13 |
import pytest |
| 14 |
|
| 15 |
from . import WacomDatabase, WacomDevice |
| 16 |
from .conftest import load_test_db |
| 17 |
|
| 18 |
logger = logging.getLogger(__name__) |
| 19 |
|
| 20 |
|
| 21 |
def datadir(): |
| 22 |
return Path(os.getenv("MESON_SOURCE_ROOT") or ".") / "data" |
| 23 |
|
| 24 |
|
| 25 |
def layoutsdir(): |
| 26 |
return datadir() / "layouts" |
| 27 |
|
| 28 |
|
| 29 |
def load_svg(layoutfile: Path) -> xml.etree.ElementTree: |
| 30 |
try: |
| 31 |
f = layoutsdir() / layoutfile |
| 32 |
assert f.exists() |
| 33 |
tree = xml.etree.ElementTree.parse(f) |
| 34 |
assert tree is not None |
| 35 |
return tree |
| 36 |
except KeyError: |
| 37 |
pass |
| 38 |
return None |
| 39 |
|
| 40 |
|
| 41 |
@dataclass |
| 42 |
class SvgDevice: |
| 43 |
_db: WacomDatabase |
| 44 |
device: WacomDevice |
| 45 |
svg: xml.etree.ElementTree |
| 46 |
is_autogenerated: bool |
| 47 |
|
| 48 |
def has_item(self, id: str, classes: Optional[List[str]] = None): |
| 49 |
root = self.svg.getroot() |
| 50 |
nodes = root.findall(f".//*[@id='{id}']") |
| 51 |
assert nodes, f"Failed to find required element with id {id}" |
| 52 |
assert len(nodes) == 1, f"Expected one element with id {id}, have {len(nodes)}" |
| 53 |
node = nodes[0] |
| 54 |
for klass in classes or []: |
| 55 |
assert klass in node.get("class", "").split(" "), ( |
| 56 |
f"Missing class '{klass}' for {id}. Have: {node.get('class')}" |
| 57 |
) |
| 58 |
|
| 59 |
@property |
| 60 |
def name(self): |
| 61 |
return self.device.name |
| 62 |
|
| 63 |
|
| 64 |
def is_autogenerated(svg_filename: Path): |
| 65 |
tabletfile = datadir() / svg_filename.name.replace(".svg", ".tablet") |
| 66 |
return ( |
| 67 |
any("autogenerated" in t for t in open(tabletfile)) |
| 68 |
if tabletfile.exists() |
| 69 |
else False |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
def pytest_generate_tests(metafunc): |
| 74 |
# We need to keep the db inside each SvgDevice, otherwise python may clean it up |
| 75 |
# and thus invalidate all our devices from db.list_devices() |
| 76 |
db = load_test_db() |
| 77 |
devices = db.list_devices() |
| 78 |
devices = [ |
| 79 |
SvgDevice( |
| 80 |
db, |
| 81 |
d, |
| 82 |
load_svg(d.layout_filename), |
| 83 |
is_autogenerated(Path(d.layout_filename)), |
| 84 |
) |
| 85 |
for d in devices |
| 86 |
if d.layout_filename |
| 87 |
] |
| 88 |
devices = sorted(devices, key=lambda d: d.name) |
| 89 |
|
| 90 |
def filenames(devices: List[SvgDevice]) -> List[str]: |
| 91 |
return [Path(d.device.layout_filename).name for d in devices] |
| 92 |
|
| 93 |
if "svgdevice" in metafunc.fixturenames: |
| 94 |
metafunc.parametrize("svgdevice", devices, ids=filenames(devices)) |
| 95 |
|
| 96 |
if "ringdevice" in metafunc.fixturenames: |
| 97 |
devices = list(filter(lambda d: d.device.num_rings > 0, devices)) |
| 98 |
metafunc.parametrize("ringdevice", devices, ids=filenames(devices)) |
| 99 |
|
| 100 |
if "stripdevice" in metafunc.fixturenames: |
| 101 |
devices = list(filter(lambda d: d.device.num_strips > 0, devices)) |
| 102 |
metafunc.parametrize("stripdevice", devices, ids=filenames(devices)) |
| 103 |
|
| 104 |
if "dialdevice" in metafunc.fixturenames: |
| 105 |
devices = list(filter(lambda d: d.device.num_dials > 0, devices)) |
| 106 |
metafunc.parametrize("dialdevice", devices, ids=filenames(devices)) |
| 107 |
|
| 108 |
if "buttondevice" in metafunc.fixturenames: |
| 109 |
devices = list(filter(lambda d: d.device.num_buttons > 0, devices)) |
| 110 |
metafunc.parametrize("buttondevice", devices, ids=filenames(devices)) |
| 111 |
|
| 112 |
|
| 113 |
def test_svg(svgdevice): |
| 114 |
root = svgdevice.svg.getroot() |
| 115 |
assert root.tag in ["svg", "{http://www.w3.org/2000/svg}svg"] |
| 116 |
assert root.get("width") is not None |
| 117 |
assert root.get("height") is not None |
| 118 |
|
| 119 |
|
| 120 |
def test_svg_maybe_not_needed(svgdevice): |
| 121 |
device = svgdevice.device |
| 122 |
features = [ |
| 123 |
device.num_buttons, |
| 124 |
device.num_rings, |
| 125 |
device.num_strips, |
| 126 |
device.num_dials, |
| 127 |
] |
| 128 |
assert any(x > 0 for x in features), ( |
| 129 |
f"Device {device.name} has no buttons/rings/strips/dials and should not have an SVG" |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
def has_item(root, id: str, classes: Optional[List[str]] = None): |
| 134 |
nodes = root.findall(f".//*[@id='{id}']") |
| 135 |
assert nodes, f"Failed to find required element with id {id}" |
| 136 |
assert len(nodes) == 1, f"Expected on element with id {id}, have {len(nodes)}" |
| 137 |
node = nodes[0] |
| 138 |
for klass in classes or []: |
| 139 |
assert klass in node.get("class").split(" "), ( |
| 140 |
f"Missing class '{klass}' for {id}. Have: {node.get('class')}" |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
def test_svg_rings(ringdevice): |
| 145 |
if ringdevice.device.num_rings >= 1: |
| 146 |
ringdevice.has_item(id="Ring", classes=["Ring", "TouchRing"]) |
| 147 |
ringdevice.has_item(id="LabelRingCW", classes=["RingCW", "Ring", "Label"]) |
| 148 |
ringdevice.has_item(id="LabelRingCCW", classes=["RingCCW", "Ring", "Label"]) |
| 149 |
ringdevice.has_item(id="LeaderRingCW", classes=["RingCW", "Ring", "Leader"]) |
| 150 |
ringdevice.has_item(id="LeaderRingCCW", classes=["RingCCW", "Ring", "Leader"]) |
| 151 |
|
| 152 |
if ringdevice.device.num_rings >= 2: |
| 153 |
ringdevice.has_item(id="Ring2", classes=["Ring2", "TouchRing"]) |
| 154 |
ringdevice.has_item(id="LabelRing2CW", classes=["Ring2CW", "Ring2", "Label"]) |
| 155 |
ringdevice.has_item(id="LabelRing2CCW", classes=["Ring2CCW", "Ring2", "Label"]) |
| 156 |
ringdevice.has_item(id="LeaderRing2CW", classes=["Ring2CW", "Ring2", "Leader"]) |
| 157 |
ringdevice.has_item( |
| 158 |
id="LeaderRing2CCW", classes=["Ring2CCW", "Ring2", "Leader"] |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
def test_svg_strips(stripdevice): |
| 163 |
try: |
| 164 |
if stripdevice.device.num_strips >= 1: |
| 165 |
stripdevice.has_item(id="Strip", classes=["Strip", "TouchStrip"]) |
| 166 |
stripdevice.has_item( |
| 167 |
id="LabelStripUp", classes=["StripUp", "Strip", "Label"] |
| 168 |
) |
| 169 |
stripdevice.has_item( |
| 170 |
id="LabelStripDown", classes=["StripDown", "Strip", "Label"] |
| 171 |
) |
| 172 |
stripdevice.has_item( |
| 173 |
id="LeaderStripUp", classes=["StripUp", "Strip", "Leader"] |
| 174 |
) |
| 175 |
stripdevice.has_item( |
| 176 |
id="LeaderStripDown", classes=["StripDown", "Strip", "Leader"] |
| 177 |
) |
| 178 |
|
| 179 |
if stripdevice.device.num_strips >= 2: |
| 180 |
stripdevice.has_item(id="Strip2", classes=["Strip2", "TouchStrip"]) |
| 181 |
stripdevice.has_item( |
| 182 |
id="LabelStrip2Up", classes=["Strip2Up", "Strip2", "Label"] |
| 183 |
) |
| 184 |
stripdevice.has_item( |
| 185 |
id="LabelStrip2Down", classes=["Strip2Down", "Strip2", "Label"] |
| 186 |
) |
| 187 |
stripdevice.has_item( |
| 188 |
id="LeaderStrip2Up", classes=["Strip2Up", "Strip2", "Leader"] |
| 189 |
) |
| 190 |
stripdevice.has_item( |
| 191 |
id="LeaderStrip2Down", classes=["Strip2Down", "Strip2", "Leader"] |
| 192 |
) |
| 193 |
except AssertionError as e: |
| 194 |
if stripdevice.is_autogenerated: |
| 195 |
pytest.skip(f"Autogenerated device has errors in SVG: {e}") |
| 196 |
raise e |
| 197 |
|
| 198 |
|
| 199 |
def test_svg_dials(dialdevice): |
| 200 |
try: |
| 201 |
if dialdevice.device.num_dials >= 1: |
| 202 |
dialdevice.has_item(id="Dial", classes=["Dial", "TouchDial"]) |
| 203 |
dialdevice.has_item(id="LabelDialCW", classes=["DialCW", "Dial", "Label"]) |
| 204 |
dialdevice.has_item(id="LabelDialCCW", classes=["DialCCW", "Dial", "Label"]) |
| 205 |
dialdevice.has_item(id="LeaderDialCW", classes=["DialCW", "Dial", "Leader"]) |
| 206 |
dialdevice.has_item( |
| 207 |
id="LeaderDialCCW", classes=["DialCCW", "Dial", "Leader"] |
| 208 |
) |
| 209 |
|
| 210 |
if dialdevice.device.num_dials >= 2: |
| 211 |
dialdevice.has_item(id="Dial2", classes=["Dial2", "TouchDial"]) |
| 212 |
dialdevice.has_item( |
| 213 |
id="LabelDial2CW", classes=["Dial2CW", "Dial2", "Label"] |
| 214 |
) |
| 215 |
dialdevice.has_item( |
| 216 |
id="LabelDial2CCW", classes=["Dial2CCW", "Dial2", "Label"] |
| 217 |
) |
| 218 |
dialdevice.has_item( |
| 219 |
id="LeaderDial2CW", classes=["Dial2CW", "Dial2", "Leader"] |
| 220 |
) |
| 221 |
dialdevice.has_item( |
| 222 |
id="LeaderDial2CCW", classes=["Dial2CCW", "Dial2", "Leader"] |
| 223 |
) |
| 224 |
except AssertionError as e: |
| 225 |
if dialdevice.is_autogenerated: |
| 226 |
pytest.skip(f"Autogenerated device has errors in SVG: {e}") |
| 227 |
raise e |
| 228 |
|
| 229 |
|
| 230 |
def test_svg_button(buttondevice): |
| 231 |
for button in list(string.ascii_uppercase)[: buttondevice.device.num_buttons]: |
| 232 |
modeswitch_label = [] |
| 233 |
flags = buttondevice.device.button_flags(button) |
| 234 |
if any(f in flags for f in WacomDevice.ButtonFlags.modeswitch_flags()): |
| 235 |
modeswitch_label = ["ModeSwitch"] |
| 236 |
buttondevice.has_item( |
| 237 |
id=f"Button{button}", classes=["Button", button] + modeswitch_label |
| 238 |
) |
| 239 |
buttondevice.has_item( |
| 240 |
id=f"Label{button}", classes=["Label", button] + modeswitch_label |
| 241 |
) |
| 242 |
buttondevice.has_item( |
| 243 |
id=f"Leader{button}", classes=["Leader", button] + modeswitch_label |
| 244 |
) |