servo-embedder-tests 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. servo_embedder_tests/__init__.py +12 -0
  2. servo_embedder_tests/__main__.py +5 -0
  3. servo_embedder_tests/cli.py +175 -0
  4. servo_embedder_tests/errors.py +10 -0
  5. servo_embedder_tests/expectations.py +182 -0
  6. servo_embedder_tests/oracles.py +23 -0
  7. servo_embedder_tests/pages/basic.html +13 -0
  8. servo_embedder_tests/pages/history-a.html +9 -0
  9. servo_embedder_tests/pages/history-b.html +7 -0
  10. servo_embedder_tests/pages/ime.html +37 -0
  11. servo_embedder_tests/pages/report.js +6 -0
  12. servo_embedder_tests/pages.py +147 -0
  13. servo_embedder_tests/platform/__init__.py +6 -0
  14. servo_embedder_tests/platform/desktop.py +39 -0
  15. servo_embedder_tests/platform/ohos.py +297 -0
  16. servo_embedder_tests/plugin.py +493 -0
  17. servo_embedder_tests/proxy.py +113 -0
  18. servo_embedder_tests/scenarios/__init__.py +0 -0
  19. servo_embedder_tests/scenarios/ime/__init__.py +0 -0
  20. servo_embedder_tests/scenarios/ime/test_text_input.py +128 -0
  21. servo_embedder_tests/scenarios/navigation/__init__.py +0 -0
  22. servo_embedder_tests/scenarios/navigation/test_local_pages.py +23 -0
  23. servo_embedder_tests/scenarios/sites/__init__.py +0 -0
  24. servo_embedder_tests/scenarios/sites/mossel.py +73 -0
  25. servo_embedder_tests/scenarios/sites/test_mossel.py +57 -0
  26. servo_embedder_tests/scenarios/sites/test_servo_org.py +23 -0
  27. servo_embedder_tests/servoshell.py +134 -0
  28. servo_embedder_tests/touch.py +68 -0
  29. servo_embedder_tests/ui.py +97 -0
  30. servo_embedder_tests/webdriver.py +61 -0
  31. servo_embedder_tests-0.1.0.dist-info/METADATA +105 -0
  32. servo_embedder_tests-0.1.0.dist-info/RECORD +36 -0
  33. servo_embedder_tests-0.1.0.dist-info/WHEEL +4 -0
  34. servo_embedder_tests-0.1.0.dist-info/entry_points.txt +6 -0
  35. servo_embedder_tests-0.1.0.dist-info/licenses/LICENSE-APACHE +176 -0
  36. servo_embedder_tests-0.1.0.dist-info/licenses/LICENSE-MIT +23 -0
@@ -0,0 +1,12 @@
1
+ """Embedder tests for Servo's servoshell."""
2
+
3
+ from importlib.metadata import PackageNotFoundError, version
4
+
5
+ try:
6
+ __version__ = version("servo-embedder-tests")
7
+ except PackageNotFoundError:
8
+ __version__ = "0.0.0"
9
+
10
+ from .cli import run_tests # noqa: E402
11
+
12
+ __all__ = ["__version__", "run_tests"]
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from .cli import main
4
+
5
+ sys.exit(main())
@@ -0,0 +1,175 @@
1
+ """`servo-embedder-tests run | list | doctor | update-expectations | record-dump`."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import shutil
7
+ import subprocess
8
+ import sys
9
+ from collections.abc import Sequence
10
+ from pathlib import Path
11
+
12
+ import pytest
13
+
14
+ from . import __version__
15
+ from .expectations import Expectations, RunReport, update
16
+ from .plugin import BUNDLED_SCENARIOS
17
+
18
+
19
+ def _bundled(arg: str) -> str | None:
20
+ """`arg` as a path into the bundled scenarios, if it names a folder (`ime/`) or file (`ime/test_x.py::name`) there
21
+ and no such path exists locally."""
22
+ if arg.startswith("-") or not ("/" in arg or ".py" in arg):
23
+ return None
24
+ path = arg.split("::", 1)[0]
25
+ if Path(path).exists() or not (BUNDLED_SCENARIOS / path).exists():
26
+ return None
27
+ return str(BUNDLED_SCENARIOS / arg)
28
+
29
+
30
+ class _Selection:
31
+ """Runs the bundled suite unless the arguments name tests, finds named tests in it, and adds the `--extra`
32
+ directories. Works on the positional arguments as pytest parsed them, so option values are never tests."""
33
+
34
+ def __init__(self, extra: list[str]) -> None:
35
+ self.extra = extra
36
+
37
+ def pytest_configure(self, config: pytest.Config) -> None:
38
+ if config.args_source == pytest.Config.ArgsSource.ARGS:
39
+ args = [_bundled(arg) or arg for arg in config.args]
40
+ else:
41
+ args = [str(BUNDLED_SCENARIOS)]
42
+ config.args = args + self.extra
43
+
44
+
45
+ def _pytest_args(argv: Sequence[str]) -> tuple[list[str], list[str]]:
46
+ """The pytest arguments for `argv`, and the directories passed with `--extra DIR`."""
47
+ args: list[str] = []
48
+ extra: list[str] = []
49
+ iterator = iter(argv)
50
+ for arg in iterator:
51
+ if arg == "--extra":
52
+ extra.append(next(iterator))
53
+ elif arg.startswith("--extra="):
54
+ extra.append(arg.split("=", 1)[1])
55
+ else:
56
+ args.append(arg)
57
+ platform = [] if any(a == "--platform" or a.startswith("--platform=") for a in args) else ["--platform", "ohos"]
58
+ quiet = ["-W", "ignore::pytest.PytestAssertRewriteWarning"]
59
+ # Without it, pytest derives the rootdir from existing paths among plugin option values.
60
+ rootdir = [] if any(a.startswith("--rootdir") for a in args) else [f"--rootdir={Path.cwd()}"]
61
+ return ["-p", "no:cacheprovider", *quiet, *rootdir, *platform, *args], extra
62
+
63
+
64
+ def run_tests(argv: Sequence[str] | None = None) -> int:
65
+ """Runs the scenarios with pytest. `argv` takes pytest and servo-embedder-tests options (see `--help`)."""
66
+ args, extra = _pytest_args(sys.argv[1:] if argv is None else argv)
67
+ return int(pytest.main(args, plugins=[_Selection(extra)]))
68
+
69
+
70
+ def _doctor(argv: Sequence[str]) -> int:
71
+ parser = argparse.ArgumentParser(prog="servo-embedder-tests doctor")
72
+ parser.add_argument("--platform", choices=("ohos", "desktop"), default="ohos")
73
+ parser.add_argument("--hdc-target")
74
+ parser.add_argument("--bundle", default="org.servo.servo")
75
+ parser.add_argument("--servo-bin", type=Path, default=Path("target/release/servoshell"))
76
+ options = parser.parse_args(argv)
77
+ problems = 0
78
+
79
+ def check(ok: bool, what: str, hint: str = "") -> None:
80
+ nonlocal problems
81
+ print(f"{'ok ' if ok else 'FAIL'} {what}" + (f" -- {hint}" if hint and not ok else ""))
82
+ problems += not ok
83
+
84
+ print(f"servo-embedder-tests {__version__}")
85
+ if options.platform == "ohos":
86
+ hdc = shutil.which("hdc")
87
+ check(hdc is not None, "hdc on PATH", "install the OpenHarmony SDK toolchains")
88
+ if hdc is None:
89
+ return 1
90
+ targets = [
91
+ t
92
+ for t in subprocess.run([hdc, "list", "targets"], capture_output=True, text=True).stdout.split()
93
+ if t != "[Empty]"
94
+ ]
95
+ check(bool(targets), f"hdc targets: {', '.join(targets) or 'none'}", "connect a device")
96
+ if len(targets) > 1 and not options.hdc_target:
97
+ check(False, "one target selected", "pass --hdc-target")
98
+ return 1
99
+ if targets:
100
+ from .platform import OhosDevice
101
+
102
+ device = OhosDevice(options.hdc_target)
103
+ print(f" {device.target}: {device.describe()} class={device.device_class()}")
104
+ check(device.is_installed(options.bundle), f"{options.bundle} installed", "pass --hap to `run`")
105
+ check(not device.is_screen_locked(), "screen unlocked (or no lock screen)", "the harness swipes it away")
106
+ else:
107
+ check(options.servo_bin.is_file(), f"servoshell at {options.servo_bin}", "pass --servo-bin")
108
+ from .proxy import mitmdump_command
109
+
110
+ try:
111
+ check(True, f"mitmdump: {' '.join(mitmdump_command())}")
112
+ except Exception as error:
113
+ check(False, "mitmdump (only needed for --proxy)", str(error))
114
+ return 1 if problems else 0
115
+
116
+
117
+ def _update_expectations(argv: Sequence[str]) -> int:
118
+ parser = argparse.ArgumentParser(
119
+ prog="servo-embedder-tests update-expectations",
120
+ description="Rewrite an expectations file so that the given runs have no unexpected results.",
121
+ )
122
+ parser.add_argument("results", type=Path, nargs="+", help="results.json files written by `run`")
123
+ parser.add_argument("--expectations", type=Path, required=True)
124
+ parser.add_argument("--intermittent", action="store_true", help="add new statuses instead of replacing")
125
+ parser.add_argument("--prune", action="store_true", help="drop entries for tests the runs didn't collect")
126
+ options = parser.parse_args(argv)
127
+ expectations = Expectations.load(options.expectations)
128
+ changes = update(
129
+ expectations, [RunReport.from_file(p) for p in options.results], options.intermittent, options.prune
130
+ )
131
+ expectations.save(options.expectations)
132
+ print("\n".join(changes) if changes else "expectations unchanged")
133
+ return 0
134
+
135
+
136
+ def main(argv: Sequence[str] | None = None) -> int:
137
+ argv = list(sys.argv[1:] if argv is None else argv)
138
+ commands = {
139
+ "run": "run the scenarios (takes pytest options; `run -- --help` lists them)",
140
+ "list": "list the scenarios that `run` would run",
141
+ "doctor": "check hdc, the device and the app",
142
+ "update-expectations": "rewrite expectations from results.json files",
143
+ "record-dump": "record a mitmproxy dump of the network scenarios: record-dump PATH [run options]",
144
+ }
145
+ if not argv or argv[0] in ("-h", "--help") or argv[0] not in commands:
146
+ print("usage: servo-embedder-tests <command> [options]\n")
147
+ for name, help in commands.items():
148
+ print(f" {name:20} {help}")
149
+ print(f"\nservo-embedder-tests {__version__}")
150
+ return 0 if argv and argv[0] in ("-h", "--help") else 2
151
+ command, rest = argv[0], argv[1:]
152
+ if rest[:1] == ["--"]:
153
+ rest = rest[1:]
154
+ if command == "run":
155
+ return run_tests(rest)
156
+ if command == "list":
157
+ return run_tests(["--collect-only", "-q", *rest])
158
+ if command == "doctor":
159
+ return _doctor(rest)
160
+ if command == "update-expectations":
161
+ return _update_expectations(rest)
162
+ if not rest:
163
+ print("usage: servo-embedder-tests record-dump PATH [run options]")
164
+ return 2
165
+ dump = Path(rest[0])
166
+ code = run_tests(["-m", "network", "--proxy", "record", "--proxy-dump", str(dump), *rest[1:]])
167
+ if not dump.is_file() or dump.stat().st_size == 0:
168
+ print(f"error: {dump} is empty; servoshell did not use the proxy")
169
+ return code or 1
170
+ print(f"recorded {dump.stat().st_size} bytes to {dump}")
171
+ return code
172
+
173
+
174
+ if __name__ == "__main__":
175
+ sys.exit(main())
@@ -0,0 +1,10 @@
1
+ class ScenarioError(RuntimeError):
2
+ """The harness or the device failed, as opposed to the app under test."""
3
+
4
+
5
+ class ScenarioTimeout(AssertionError):
6
+ """A wait in a scenario ran out of time. Reported as TIMEOUT."""
7
+
8
+
9
+ class AppCrashed(AssertionError):
10
+ """The app under test exited during the test. Reported as CRASH."""
@@ -0,0 +1,182 @@
1
+ """WPT-style expectations: per test and device class, the statuses a run may produce.
2
+
3
+ The file is TOML. Each table is a test key (see `test_key`); each entry maps a device class, or `default`, to a status
4
+ or a list of statuses. Lookup tries the device class, then its prefixes up to each `-` (so `openharmony-emulator`
5
+ falls back to `openharmony`), then `default`. A test without an entry is expected to PASS.
6
+
7
+ ["navigation/test_history.py::test_back[webdriver]"]
8
+ default = "PASS"
9
+ harmonyos = ["PASS", "FAIL"]
10
+ issue = "https://github.com/servo/servo/issues/12345"
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import json
16
+ from collections.abc import Iterable
17
+ from dataclasses import dataclass, field
18
+ from pathlib import Path
19
+
20
+ import tomlkit
21
+ from tomlkit.items import Table
22
+
23
+ STATUSES = ("PASS", "FAIL", "TIMEOUT", "CRASH", "SKIP")
24
+ NON_STATUS_KEYS = ("issue", "comment")
25
+
26
+
27
+ def _as_list(value: object) -> list[str]:
28
+ values = [value] if isinstance(value, str) else list(value) # type: ignore[call-overload]
29
+ for status in values:
30
+ if status not in STATUSES:
31
+ raise ValueError(f"unknown status {status!r}; expected one of {', '.join(STATUSES)}")
32
+ return [str(v) for v in values]
33
+
34
+
35
+ def lookup_chain(device_class: str) -> list[str]:
36
+ parts = device_class.split("-")
37
+ return ["-".join(parts[:i]) for i in range(len(parts), 0, -1)] + ["default"]
38
+
39
+
40
+ @dataclass
41
+ class Expectation:
42
+ statuses: list[str]
43
+ issue: str | None = None
44
+
45
+ @property
46
+ def is_default(self) -> bool:
47
+ return self.statuses == ["PASS"]
48
+
49
+
50
+ class Expectations:
51
+ def __init__(self, document: tomlkit.TOMLDocument | None = None, path: Path | None = None) -> None:
52
+ self.document = document if document is not None else tomlkit.document()
53
+ self.path = path
54
+ for key, table in self.document.items():
55
+ if not isinstance(table, dict):
56
+ raise ValueError(f"{path}: entry {key!r} must be a table")
57
+ for name, value in table.items():
58
+ if name not in NON_STATUS_KEYS:
59
+ _as_list(value)
60
+
61
+ @classmethod
62
+ def load(cls, path: Path | None) -> Expectations:
63
+ if path is None or not path.exists():
64
+ return cls(path=path)
65
+ return cls(tomlkit.parse(path.read_text()), path)
66
+
67
+ def save(self, path: Path | None = None) -> None:
68
+ target = path or self.path
69
+ if target is None:
70
+ raise ValueError("no path to save expectations to")
71
+ target.write_text(tomlkit.dumps(self.document))
72
+
73
+ def get(self, key: str, device_class: str) -> Expectation:
74
+ table = self.document.get(key)
75
+ if table is None:
76
+ return Expectation(["PASS"])
77
+ issue = table.get("issue")
78
+ for name in lookup_chain(device_class):
79
+ if name in table:
80
+ return Expectation(_as_list(table[name]), issue)
81
+ return Expectation(["PASS"], issue)
82
+
83
+ def keys(self) -> list[str]:
84
+ return list(self.document.keys())
85
+
86
+ def set(self, key: str, device_class: str, statuses: list[str]) -> None:
87
+ """Records `statuses` for `device_class`, then drops entries that the fallback chain already implies."""
88
+ table = self.document.get(key)
89
+ if table is None:
90
+ table = tomlkit.table()
91
+ self.document[key] = table
92
+ assert isinstance(table, Table)
93
+ table[device_class] = statuses[0] if len(statuses) == 1 else sorted(statuses, key=STATUSES.index)
94
+ self._simplify(key)
95
+
96
+ def _simplify(self, key: str) -> None:
97
+ table = self.document[key]
98
+ assert isinstance(table, Table)
99
+ for name in [n for n in table.keys() if n not in NON_STATUS_KEYS and n != "default"]:
100
+ fallback = next((_as_list(table[n]) for n in lookup_chain(name)[1:] if n in table), ["PASS"])
101
+ if sorted(_as_list(table[name])) == sorted(fallback):
102
+ del table[name]
103
+ if "default" in table and _as_list(table["default"]) == ["PASS"]:
104
+ only_default = all(n in NON_STATUS_KEYS or n == "default" for n in table.keys())
105
+ if only_default:
106
+ table.pop("default")
107
+ if all(n in NON_STATUS_KEYS for n in table.keys()):
108
+ del self.document[key]
109
+
110
+ def remove(self, key: str) -> None:
111
+ if key in self.document:
112
+ del self.document[key]
113
+
114
+
115
+ @dataclass
116
+ class Result:
117
+ key: str
118
+ status: str
119
+ expected: list[str]
120
+ message: str = ""
121
+ duration: float = 0.0
122
+
123
+ @property
124
+ def unexpected(self) -> bool:
125
+ return self.status != "SKIP" and self.status not in self.expected
126
+
127
+
128
+ @dataclass
129
+ class RunReport:
130
+ device_class: str
131
+ results: dict[str, Result] = field(default_factory=dict)
132
+ environment: dict[str, object] = field(default_factory=dict)
133
+
134
+ def to_json(self) -> str:
135
+ return json.dumps(
136
+ {
137
+ "device_class": self.device_class,
138
+ "environment": self.environment,
139
+ "results": {k: vars(r) for k, r in sorted(self.results.items())},
140
+ },
141
+ indent=2,
142
+ )
143
+
144
+ @classmethod
145
+ def from_file(cls, path: Path) -> RunReport:
146
+ data = json.loads(path.read_text())
147
+ report = cls(data["device_class"], environment=data.get("environment", {}))
148
+ for key, raw in data["results"].items():
149
+ report.results[key] = Result(**raw)
150
+ return report
151
+
152
+
153
+ def update(
154
+ expectations: Expectations,
155
+ reports: Iterable[RunReport],
156
+ intermittent: bool = False,
157
+ prune: bool = False,
158
+ ) -> list[str]:
159
+ """Rewrites expectations so the given runs would have no unexpected results. Returns a change log."""
160
+ changes = []
161
+ observed: dict[tuple[str, str], set[str]] = {}
162
+ seen: set[str] = set()
163
+ for report in reports:
164
+ for key, result in report.results.items():
165
+ seen.add(key)
166
+ observed.setdefault((key, report.device_class), set()).add(result.status)
167
+ for (key, device_class), statuses in sorted(observed.items()):
168
+ if "SKIP" in statuses and len(statuses) == 1:
169
+ continue
170
+ statuses.discard("SKIP")
171
+ current = expectations.get(key, device_class).statuses
172
+ new = set(statuses) | (set(current) if intermittent else set())
173
+ if new == set(current):
174
+ continue
175
+ expectations.set(key, device_class, sorted(new, key=STATUSES.index))
176
+ changes.append(f"{key} [{device_class}]: {'/'.join(current)} -> {'/'.join(sorted(new, key=STATUSES.index))}")
177
+ if prune:
178
+ for key in expectations.keys():
179
+ if key not in seen:
180
+ expectations.remove(key)
181
+ changes.append(f"{key}: removed (no longer collected)")
182
+ return changes
@@ -0,0 +1,23 @@
1
+ """Waiting for observable state instead of sleeping."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import time
6
+ from collections.abc import Callable
7
+ from typing import TypeVar
8
+
9
+ from .errors import ScenarioTimeout
10
+
11
+ T = TypeVar("T")
12
+
13
+
14
+ def wait_until(condition: Callable[[], T], timeout: float = 15, interval: float = 0.2, message: str = "condition") -> T:
15
+ """Polls `condition` until it returns a truthy value, which is returned. Raises ScenarioTimeout."""
16
+ deadline = time.monotonic() + timeout
17
+ while True:
18
+ value = condition()
19
+ if value:
20
+ return value
21
+ if time.monotonic() > deadline:
22
+ raise ScenarioTimeout(f"timed out after {timeout}s waiting for {message}")
23
+ time.sleep(interval)
@@ -0,0 +1,13 @@
1
+ <!doctype html>
2
+ <meta charset="utf-8">
3
+ <title>servo-embedder-tests basic</title>
4
+ <style>
5
+ body { margin: 0; font: 24px sans-serif; background: #fff; }
6
+ #status { padding: 16px; }
7
+ </style>
8
+ <p id="status">loading</p>
9
+ <script>
10
+ addEventListener("load", () => {
11
+ document.getElementById("status").textContent = "loaded";
12
+ });
13
+ </script>
@@ -0,0 +1,9 @@
1
+ <!doctype html>
2
+ <meta charset="utf-8">
3
+ <title>history A</title>
4
+ <style>
5
+ body { margin: 0; font: 24px sans-serif; }
6
+ a { display: block; padding: 40px 16px; }
7
+ </style>
8
+ <h1 id="page">A</h1>
9
+ <a id="next" href="history-b.html">to B</a>
@@ -0,0 +1,7 @@
1
+ <!doctype html>
2
+ <meta charset="utf-8">
3
+ <title>history B</title>
4
+ <style>
5
+ body { margin: 0; font: 24px sans-serif; }
6
+ </style>
7
+ <h1 id="page">B</h1>
@@ -0,0 +1,37 @@
1
+ <!doctype html>
2
+ <meta charset="utf-8">
3
+ <meta name="viewport" content="width=device-width">
4
+ <title>servo-embedder-tests IME</title>
5
+ <style>
6
+ body { margin: 0; font: 24px sans-serif; }
7
+ input, textarea { display: block; box-sizing: border-box; width: 90%; margin: 24px 5%; font: 28px sans-serif; }
8
+ input { height: 72px; }
9
+ textarea { height: 160px; }
10
+ </style>
11
+ <input id="field" autocomplete="off">
12
+ <textarea id="area"></textarea>
13
+ <script src="report.js"></script>
14
+ <script>
15
+ for (const type of ["keydown", "beforeinput", "input", "compositionstart", "compositionupdate", "compositionend",
16
+ "focus", "blur"]) {
17
+ document.addEventListener(type, (event) => {
18
+ const target = event.target;
19
+ report({
20
+ type,
21
+ target: target.id ?? null,
22
+ value: target.value ?? null,
23
+ data: event.data ?? null,
24
+ inputType: event.inputType ?? null,
25
+ key: event.key ?? null,
26
+ isComposing: event.isComposing ?? null,
27
+ });
28
+ }, true);
29
+ }
30
+ addEventListener("load", () => {
31
+ const rect = (id) => {
32
+ const r = document.getElementById(id).getBoundingClientRect();
33
+ return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
34
+ };
35
+ report({ type: "geometry", dpr: devicePixelRatio, field: rect("field"), area: rect("area") });
36
+ });
37
+ </script>
@@ -0,0 +1,6 @@
1
+ // Sends `data` to the fixture server, where tests read it with `pages.wait_for_report`. Reports arrive in order.
2
+ let reportChain = Promise.resolve();
3
+ function report(data) {
4
+ const body = JSON.stringify(data);
5
+ reportChain = reportChain.then(() => fetch("/__report", { method: "POST", body, keepalive: true })).catch(() => {});
6
+ }
@@ -0,0 +1,147 @@
1
+ """A local HTTP server for fixture pages, reachable from the device through a reverse port forward."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import http.server
6
+ import json
7
+ import mimetypes
8
+ import socket
9
+ import threading
10
+ import time
11
+ import urllib.parse
12
+ from collections.abc import Callable, Sequence
13
+ from importlib import resources
14
+ from pathlib import Path
15
+ from typing import cast
16
+
17
+ # Pages POST JSON here (see pages/report.js); tests read it with `PageServer.wait_for_report`.
18
+ REPORT_PATH = "/__report"
19
+
20
+
21
+ class _Handler(http.server.BaseHTTPRequestHandler):
22
+ protocol_version = "HTTP/1.1"
23
+
24
+ @property
25
+ def scenario_server(self) -> _Server:
26
+ return cast(_Server, self.server)
27
+
28
+ def setup(self) -> None:
29
+ super().setup()
30
+ # Servo opens many parallel requests to the same host; Nagle plus delayed ACKs would stall them.
31
+ self.connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
32
+
33
+ def do_GET(self) -> None: # noqa: N802
34
+ path = urllib.parse.urlsplit(self.path).path
35
+ self.scenario_server.requests.append(path)
36
+ if path in self.scenario_server.dynamic:
37
+ body, content_type = self.scenario_server.dynamic[path]
38
+ self._send(200, body, content_type)
39
+ return
40
+ file = self.scenario_server.find(path)
41
+ if file is None:
42
+ self._send(404, b"not found", "text/plain")
43
+ return
44
+ content_type = mimetypes.guess_type(file.name)[0] or "application/octet-stream"
45
+ if content_type.startswith("text/") or content_type in ("application/javascript", "application/json"):
46
+ content_type += "; charset=utf-8"
47
+ self._send(200, file.read_bytes(), content_type)
48
+
49
+ def do_POST(self) -> None: # noqa: N802
50
+ path = urllib.parse.urlsplit(self.path).path
51
+ body = self.rfile.read(int(self.headers.get("Content-Length", 0)))
52
+ if path != REPORT_PATH:
53
+ self._send(404, b"not found", "text/plain")
54
+ return
55
+ try:
56
+ report = json.loads(body)
57
+ except ValueError:
58
+ self._send(400, b"bad json", "text/plain")
59
+ return
60
+ with self.scenario_server.condition:
61
+ self.scenario_server.reports.append(report)
62
+ self.scenario_server.condition.notify_all()
63
+ self._send(204, b"", "text/plain")
64
+
65
+ def _send(self, status: int, body: bytes, content_type: str) -> None:
66
+ self.send_response(status)
67
+ self.send_header("Content-Type", content_type)
68
+ self.send_header("Content-Length", str(len(body)))
69
+ self.send_header("Cache-Control", "no-store")
70
+ self.end_headers()
71
+ self.wfile.write(body)
72
+
73
+ def log_message(self, format: str, *args: object) -> None:
74
+ pass
75
+
76
+
77
+ class _Server(http.server.ThreadingHTTPServer):
78
+ daemon_threads = True
79
+
80
+ def __init__(self, roots: list[Path]) -> None:
81
+ super().__init__(("127.0.0.1", 0), _Handler)
82
+ self.roots = roots
83
+ self.dynamic: dict[str, tuple[bytes, str]] = {}
84
+ self.requests: list[str] = []
85
+ self.reports: list[dict] = []
86
+ self.condition = threading.Condition()
87
+
88
+ def find(self, path: str) -> Path | None:
89
+ relative = urllib.parse.unquote(path).lstrip("/") or "index.html"
90
+ for root in self.roots:
91
+ candidate = (root / relative).resolve()
92
+ if candidate.is_relative_to(root.resolve()) and candidate.is_file():
93
+ return candidate
94
+ return None
95
+
96
+
97
+ class PageServer:
98
+ """Serves the bundled `pages/` directory, extra directories and pages added at runtime."""
99
+
100
+ def __init__(self, extra_roots: Sequence[Path] = ()) -> None:
101
+ bundled = Path(str(resources.files("servo_embedder_tests") / "pages"))
102
+ self._server = _Server([*extra_roots, bundled])
103
+ self.port = self._server.server_address[1]
104
+ self._thread = threading.Thread(target=self._server.serve_forever, daemon=True)
105
+
106
+ def start(self) -> PageServer:
107
+ self._thread.start()
108
+ return self
109
+
110
+ def stop(self) -> None:
111
+ self._server.shutdown()
112
+ self._server.server_close()
113
+
114
+ def url(self, path: str) -> str:
115
+ return f"http://127.0.0.1:{self.port}/{path.lstrip('/')}"
116
+
117
+ def add(self, path: str, html: str, content_type: str = "text/html; charset=utf-8") -> str:
118
+ """Serves `html` at `path` and returns its URL."""
119
+ path = "/" + path.lstrip("/")
120
+ self._server.dynamic[path] = (html.encode(), content_type)
121
+ return self.url(path)
122
+
123
+ @property
124
+ def requests(self) -> list[str]:
125
+ return self._server.requests
126
+
127
+ @property
128
+ def reports(self) -> list[dict]:
129
+ """JSON objects the pages reported, in arrival order."""
130
+ return self._server.reports
131
+
132
+ def clear_reports(self) -> None:
133
+ with self._server.condition:
134
+ self._server.reports.clear()
135
+
136
+ def wait_for_report(self, predicate: Callable[[dict], bool], timeout: float = 10, since: int = 0) -> dict | None:
137
+ """First report at index >= `since` that matches `predicate`, waiting up to `timeout` seconds for it."""
138
+ deadline = time.monotonic() + timeout
139
+ with self._server.condition:
140
+ while True:
141
+ for report in self._server.reports[since:]:
142
+ if predicate(report):
143
+ return report
144
+ remaining = deadline - time.monotonic()
145
+ if remaining <= 0:
146
+ return None
147
+ self._server.condition.wait(remaining)
@@ -0,0 +1,6 @@
1
+ from .desktop import DesktopHost
2
+ from .ohos import HilogStream, OhosDevice
3
+
4
+ Platform = OhosDevice | DesktopHost
5
+
6
+ __all__ = ["DesktopHost", "HilogStream", "OhosDevice", "Platform"]