diff --git a/AGENTS.md b/AGENTS.md index e60b56d4f..7c8e98025 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -98,6 +98,7 @@ tools/phone-ssh CMD -> run anything; stream files with ssh 'cat > f' < f | `tools/kernel-pr-check [-C KERNEL] [--build OUT] [--merge] BASE [HEAD]` | The same gate for linux-omarchy-mobile pull requests: a human Signed-off-by from the author (a `Co-Authored-By` only warns; the kernel takes `Assisted-by`), subsystem prefixes, `checkpatch --strict`, identifiers, and with `--build` the `W=1` builds of touched C files, device trees (new warnings only, against the base) and bindings. `--merge` builds the pull request merged onto BASE, as CI does, and reports a conflict as "rebase needed". Build it in the project container with the Android clang on `PATH` | | `tools/phone-ssh [--fresh\|--stop] CMD` | Run a command on the phone. `OMAMOBILE_SSH_SHARE=1` shares one connection (kept 60 s), so a key that needs a touch per connection asks once rather than about 25 times in `push`. A shared connection survives what happens to new ones, so a check that SSH still works after a USB or firewall step uses `--fresh` (`tools/usbc-deadman` always does) | | `tools/phone-smoke` | Read-only smoke test of a running phone, with a report for the pull request: boot slot, storage guard, root, kernel health, USB, session, then display, GPU, touch, radios, battery, sound, sensors and cameras as evidence. `--here` on the phone, `--json` for a machine | +| `tools/phone-gesture-test [IDS] [--record] [--keep-tiles] [--loud]` (+ `phone-gesture-touch`) | On-phone gesture tests of the quick-settings pages ([GESTURE-TESTS](docs/GESTURE-TESTS.md)): a uinput touchscreen swipes at points taken from `hyprctl` (no device's numbers; so far run on a Pixel 9a and a VM with its screen), and the shell's IPC state plus arithmetic on the pixels give the verdict (PASS, FAIL, SKIP when the screen isn't the one a scenario needs, INCONCLUSIVE when a page that should stay open shows no movement). Runs as the session user, sudo for `/dev/uinput`. Takes over the screen for minutes (workspaces, a terminal, fictional notifications, do-not-disturb; all put back, also after a dropped session, and a run that couldn't clean up is put back by the next one or `--cleanup`): announce it | | `omamobile-update` (on the phone; `omamobile-omarchy`) | What `omarchy update` runs on the phone: Omarchy's own update with the phone's stand-ins (docs/RELEASES.md §5.4). `check --json` is read-only; an update changes the phone's packages and restarts the shell, so it is the human's call | | `omamobile-boot-update status\|preflight\|write\|rollback` (on the phone; `omamobile-boot-update`) | Turns the installed `linux-omamobile` into slot B's images and writes them, guarded and verified ([RELEASES](docs/RELEASES.md) §5.3, §5.5); its pacman hook does nothing unless `/etc/omamobile/boot-update.conf` says `AUTO_WRITE=yes`, which development phones don't. `status`, `preflight` and `--dry-run` are read-only; `write` and `rollback` write slot B, so they are the human's call, like `push` | | `omamobile-modem nv-overlay status\|diff\|capture\|reset [--all]` (on the phone; `omamobile-modem` 0.1.15+) | The modem's NV overlay (section 1): `status` and `diff` are read-only (`diff` lists what the modem changed since its start, names and sizes, and what would be kept); `capture` keeps the modem's allowed writes now (run it before a `reboot-to`, which skips the unit's stop); `reset` deletes the overlay, so the next boot runs from the backup alone. Turning it on (`/etc/omamobile/modem-nv-overlay-enable`) and resetting it are the human's call | diff --git a/docs/GESTURE-TESTS.md b/docs/GESTURE-TESTS.md new file mode 100644 index 000000000..c6846ab4f --- /dev/null +++ b/docs/GESTURE-TESTS.md @@ -0,0 +1,185 @@ +# Gesture tests on the phone + +`tools/phone-gesture-test` runs swipe scenarios on the quick-settings pages +(Wi-Fi, Bluetooth, the notification shade) on a real phone and checks each +one through the shell's own state. It came out of #553 (a swipe up closes the +shade card and the Wi-Fi and Bluetooth pages), where hand checks missed that +over an app a swipe from the very bottom edge reached the home strip instead +of the page. + +``` +tools/phone-gesture-test --list # the scenarios +tools/phone-gesture-test # all of them (asks for sudo once) +tools/phone-gesture-test 07 08 11 # some of them +tools/phone-gesture-test --record # plus a screen recording per test +tools/phone-gesture-test --cleanup # only put back what a cut-off run left +``` + +Run it on the phone, as the session user, from a checkout (it needs +`tools/phone-gesture-touch` next to it). To test a pull request's shell, +install its build first (`tools/phone-install-package packages/omamobile-shell` +or `pacman -U`); the suite tests whatever shell is running and records which +(package version and a hash of the quick-settings QML) in its results. + +**It takes over the screen for a few minutes:** workspaces switch, a terminal +opens, notifications appear and go. Tell whoever holds the phone first. + +It checks at its start that the commands it needs are there (`grim`, +`magick`, and `foot`, `notify-send`, `wf-recorder` or `sudo` when the chosen +tests need them) and names the package of any that is missing. + +## The scenarios + +| # | Screen | Gesture | Expected | +| --- | --- | --- | --- | +| 01, 02 | Wi-Fi / Bluetooth page, home screen | swipe up from the bottom edge | the page closes | +| 03 | Wi-Fi page, home screen | drag up from the last visible network | the list scrolls, the page stays | +| 04 | Wi-Fi page, home screen | drag up in the page body, above the strip | the page stays | +| 05 | quick settings, home screen | swipe up on the notification card's header | quick settings close | +| 06 | quick settings, home screen | drag up in the notification list | the list scrolls, quick settings stay | +| 07, 08 | Wi-Fi / Bluetooth page over an app | swipe up from the bottom edge | the page closes | +| 09, 10 | Wi-Fi / Bluetooth page over an app | swipe up from the page's own strip | the page closes | +| 11, 12 | Wi-Fi / Bluetooth page over an app | swipe up from 40 px above the edge, inside the home strip's 44 px zone | the page closes | +| 13 | Wi-Fi page, home screen | the same, 40 px above the edge | the page closes | + +## No number of one device's + +Nothing in a test depends on the device, its screen size or the user's setup +(so far it has run on a Pixel 9a, and in an aarch64 VM with the 9a's screen): + +- **Pages open through the shell's IPC**, never by a tap on a tile: + `shell call omamobile.quicksettings openWifi ''` ("the same as the tile's + chevron"), `openBluetooth ''`, and `shell summon omamobile.quicksettings + '{"expanded": false}'` for the sheet. The tile layout doesn't matter, and + for the run quick settings use the package's default layout anyway (the + user's `~/.config/omamobile/quicksettings.json` is moved aside and put + back; `--keep-tiles` leaves it). +- **A known screen:** "home" is an empty workspace (4-9), "app" one with a + terminal (`foot --app-id gesture-test`) that the suite opens and closes. +- **Only the gesture under test is a real touch,** through a virtual + touchscreen (`tools/phone-gesture-touch`, `/dev/uinput`), at points in + logical pixels taken from the output (`hyprctl monitors`) and the page's own + window (`hyprctl layers`): the edge swipe starts 13 px above the screen's + bottom, the page-strip swipe 16 px above the page window's bottom, and so on. + The finger moves at panel resolution, through libinput and Hyprland's touch + path, as a finger does. + +## Verdicts are computed, never judged + +- A test **passes** when the shell's state after the gesture is what the + scenario expects (`shell call omamobile.quicksettings state`: `wifiPanel`, + `bluetoothPanel`, `opened`) and Recents stayed shut, and **fails** + otherwise. No scenario opens Recents, so its window + (`omamobile-switcher`) after a gesture means the touch reached the home + strip, whatever became of the page. +- A page that should **stay open** (03, 04, 06) would also stay open if the + touch never arrived, so its pass also needs evidence from the pixels: the + row profile of the list (the mean grey of each panel row) before and after + the drag, and the upward shift that lines the two up best. The content must + have moved up by at least 20 px and line up much better at that shift than + unmoved; otherwise the result is **INCONCLUSIVE**, with the numbers in the + log. (How far a list can go depends on how much of it is left below, so + the rule doesn't ask for a share of the drag. A touch that never arrived + moves nothing, and rows a scan adds push the list down, not up.) Rows that + look alike (network names of one length, identical notifications) line up + almost as well a whole number of rows apart: the suite then takes the + shift nearest the drag, logs the others (`also_lines_up_at`), and passes + only if every one of them is a move of at least 20 px. The fictional + notifications differ in length, and one wraps onto a second line, so the + shade's list never looks like that. Before such a drag the suite + waits until the list stops changing, since a Wi-Fi scan adds rows for a few + seconds after the page opens. +- Before the gesture each test checks that the screen is the one it claims, + from the layers: on "home" no window and no home strip, with the page + reaching the screen's bottom; over an "app" the home strip shown and the + page ending where it begins; no on-screen keyboard and no Recents; the + screen not locked and no call's screen up (`omamobile-call`, + `omamobile-call-top`). These are checked before every gesture, since a run + takes minutes. 03 and 04 also need the + Wi-Fi list to reach the strip (they are about the end of the list); with + only a few networks in range it doesn't. A failed precondition is a + **SKIP**, neither a pass nor a fail. + +`tests/phone-gesture-test` checks this arithmetic in CI, without a phone. + +## What it changes, and puts back + +- **Fictional notifications** for the shade's tests (05, 06), never more than + the notification history has room for, less two slots for real ones that + arrive during the run: the service keeps only its newest `historyLimit` + (10) files and deletes the rest. Afterwards the suite takes its own out + again the way a swipe on the shade does (`dismissNotification`) and puts + the bar's unread mark back; `results.json` says whether every history file + from before the run is still there. +- **Do-not-disturb** during the run (`--loud` leaves it off): no sound or + popup for the fictional notifications, and no popup over a screen being + measured. The previous setting is put back. +- **A cool phone:** above 75 °C the phone throttles and delivers touches late + or in one burst, and a drag then looks like a jump (seen on the 9a at + 89-92 °C, with `--record`). Each test waits until the hottest thermal zone + is below 75 °C at its start (up to two minutes) and again before the + screen is measured (up to one), and is a SKIP if it is still above 75 °C + then. Measuring warms it up again, so the touch itself comes somewhat + hotter; both temperatures are in the log. + `--record` heats the phone (x264 at 30 fps): leave it off for verdicts. + +### A run that is cut off + +Ctrl-C, SIGTERM and SIGHUP (what a run gets when its SSH session or the USB +link drops) all end a run through the same cleanup, which then runs to its +end: a second signal doesn't cut it short, and each of its commands has a +timeout. `results.json` is written all the same, with `interrupted` naming the +signal. + +A run that never gets to its cleanup (SIGKILL, a crash, a power cut) leaves +its record, `~/.local/state/phone-gesture-test/run.json`. Each change is +written there before it is made (do-not-disturb as it was, the tile layout +moved aside, the terminal's process), and the file goes once everything is +back. The next run reads it first and puts back what is still changed: +do-not-disturb off again if it was off before, the tile layout, the terminal, +the fictional notifications, a page or the sheet left open, the workspace it +started on (only from one of the run's own, now empty), and the bar's unread +mark (only ever moved back, so a notification can show as unread that was +read, never the other way round). `--cleanup` does only that. A record whose run is still going stops +the new run. + +## Output + +`~/.cache/phone-gesture-test/run-TIME/` (or `--out DIR`): + +- `results.json`: the device (output, panel pixels, scale, logical size), the + shell that ran, do-not-disturb before, during and after, the notification + bookkeeping, and each test's result, expectation, the state it got, its + evidence and temperatures; +- per test: `log.json` (states and layers before and after, the touch in + logical and panel pixels with its wall-clock times), `before.png`, + `gesture.png` (the swipe drawn in red), `after.png`, and `raw.mp4` with + `--record`. + +The screenshots show what was on the screen, Wi-Fi network names and +notifications included: look before you share them. + +## Limits + +- 03 and 04 need more networks in range than fit on the page. A phone with + only a few in range SKIPs them. +- 03 finds the last network row as the lowest line of light text in the list, + so it assumes a dark theme. +- 05 starts at half the screen's height, where the shade card's header is + with the sheet collapsed; `gesture.png` shows where it landed. +- One output, portrait (it refuses a rotated display or a locked screen). +- `sudo` for `/dev/uinput`: a ticket first (`sudo -v`, which asks once), then + the helper under `sudo -n`, so its stdin only ever carries swipes. + `GESTURE_TEST_SUDO_PW` gives the password for an unattended run; it is + read once and taken out of the environment, so no child sees it (the + terminal keeps its environment for its lifetime), and nothing is stored. + +## Adding a scenario + +A scenario is an entry in `TESTS` (id, name, `ctx` home or app, `page` wifi, +bluetooth or sheet, `gesture`, title, expectation and the state it wants) and, +for a new gesture, a case in `gesture_points`, in logical pixels relative to +the output or the page's window. Keep the pass condition in the shell's state; +if a pass could also mean that nothing happened, add pixel evidence the way +03, 04 and 06 do. [REMOTE-TESTING](REMOTE-TESTING.md) covers driving the +phone by hand over ssh. diff --git a/tests/all b/tests/all index 2defa53b9..ce903330f 100755 --- a/tests/all +++ b/tests/all @@ -20,7 +20,8 @@ # upstream packages (rootfs/packages.pinned), # tools/fetch-toolchain against local fixtures, tools/release-upload against # a scratch server, tools/extract-camera-data's adrp decoding, -# tools/phone-smoke's dmesg error classification, the OWL conformance +# tools/phone-smoke's dmesg error classification, tools/phone-gesture-test's +# arithmetic and scenario list (no phone), the OWL conformance # report's differences on the host, bin/lineage-ref sync's branch handling # against stand-ins for docker and repo, tools/repo-publish --from-main's # copy of the prebuilt binaries into a fixture's publish tree, @@ -45,7 +46,7 @@ sh -n device/flame/initramfs/build bash -n tools/build-flame-probe echo "ok - scripts parse" python3 tests/bt-uart-gpio.py -for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs; do +for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/phone-gesture-test tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs; do echo "# $t" "$t" done diff --git a/tests/phone-gesture-test b/tests/phone-gesture-test new file mode 100755 index 000000000..48e345fe5 --- /dev/null +++ b/tests/phone-gesture-test @@ -0,0 +1,276 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# tools/phone-gesture-test without a phone: its scenario list and --help, +# and its arithmetic, imported as a module: the upward shift between two row +# profiles (a list that scrolled with the finger), the rule that turns that +# shift into evidence (with the numbers the 9a measured), the touch points +# taken from the output and the page's window, the preconditions that keep a +# test to the screen it claims (home, over an app, no on-screen keyboard), +# how many fictional notifications fit in the history, and the cleanup: a +# hang-up ends a run through it, the sudo password never reaches a child or +# the helper's stdin, and what a run that couldn't clean up left is put back +# at the next start. +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } +pass() { printf 'ok - %s\n' "$1"; } + +list=$(tools/phone-gesture-test --list) +[[ $(grep -c '' <<<"$list") == 13 ]] || fail "phone-gesture-test lists 13 scenarios" +for id in 01 07 11 13; do grep -cE "^$id " <<<"$list" >/dev/null || fail "scenario $id is listed"; done +pass "phone-gesture-test lists its 13 scenarios" +tools/phone-gesture-test --help | grep -c 'usage:' >/dev/null || fail "phone-gesture-test --help prints its usage" +pass "phone-gesture-test --help" + +GESTURE_TEST_SUDO_PW=not-a-password python3 -I - tools/phone-gesture-test <<'EOF' +import importlib.machinery, importlib.util, json, os, random, signal, subprocess, sys, tempfile + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE: no tools/__pycache__ +loader = importlib.machinery.SourceFileLoader("pgt", sys.argv[1]) +spec = importlib.util.spec_from_loader("pgt", loader) +m = importlib.util.module_from_spec(spec) +loader.exec_module(m) + + +def check(ok, what): + if not ok: + print("not ok - " + what, file=sys.stderr) + sys.exit(1) + print("ok - " + what) + + +# A list that moved up by 200 rows lines up with itself at a shift of 200 +# (grey values 0..65535, as magick's 16-bit profiles give them). +rng = random.Random(7) +a = [rng.uniform(0, 65535) for _ in range(2000)] +s, rb, r0, alts = m.shift_up(a, a[200:] + [0.0] * 200, 600) +check(s == 200 and rb == 0 and r0 > m.STABLE and alts == [], "a profile moved up 200 rows is found at a shift of 200") +s, rb, r0, alts = m.shift_up(a, list(a), 600) +check(s == 0 and r0 == 0, "an unmoved profile has no shift") + +# Look-alike rows (network names of one length) repeat every 170 panel rows +# (64 logical px at 8/3) and line up as well a whole number of rows apart: +# the shift nearest the drag is taken, the others are logged, and every one +# of them must be a real move. +row = [rng.uniform(0, 65535) for _ in range(170)] +rows = (row * 20)[:3000] +found = m.shift_up(rows[:2000], rows[755:2755], 1067, near=701) +check(found[0] == 755 and sorted(found[3]) == [75, 245, 415, 585, 925], + "look-alike rows: of the shifts that line up as well, the one nearest the drag") +ok, ev = m.scroll_evidence(found, 8 / 3, 263) +check(ok and ev["moved"] == 283 and ev["also_lines_up_at"] == [28, 92, 156, 219, 347], + "and it is evidence when every one of them is a move of 20 px or more") +ok, ev = m.scroll_evidence(m.shift_up(rows[:2000], rows[710:2710], 1067, near=701), 8 / 3, 263) +check(not ok and 11 in ev["also_lines_up_at"], "not when one of them is a move of 11 px") +ok, ev = m.scroll_evidence(m.shift_up(a, a[200:] + [0.0] * 200, 1067, near=701), 8 / 3, 263) +check(ok and ev["moved"] == 75 and "also_lines_up_at" not in ev, "rows that differ give one shift, whatever the drag") + +# The evidence rule, with the 9a's numbers (logical px moved, and the +# profiles' difference at that shift and unmoved). +check(m.scrolled_with(279, 270, 3173), "a list that followed the finger is evidence (06 on the 9a)") +check(m.scrolled_with(78, 286, 3172), "so is a short list that stopped at its end") +check(m.scrolled_with(111, 189, 2915), "and one whose touch arrived late, in a burst") +check(not m.scrolled_with(0, 227, 227), "rows a Wi-Fi scan added are not a scroll") +check(not m.scrolled_with(0, 0, 0), "an unchanged screen is no evidence") +check(not m.scrolled_with(12, 100, 3000), "nor a shift under 20 px") +check(m.scrolled_with(200, 30, 2464), "a screenshot shifted by 200 px is") + +# The fictional notifications differ in length, and one is long enough to +# wrap: identical rows make the row profile periodic, and a shift then reads +# a row short. +titles, bodies = zip(*m.NOTE_TEXTS) +check(len(m.NOTE_TEXTS) >= m.NOTIFICATIONS and len(set(titles)) == len(titles) + and len({len(b) for b in bodies}) == len(bodies) and max(len(b) for b in bodies) > 80, + "the fictional notifications all differ in length, and one wraps") + + +class Geo: # the Pixel 9a: 1080x2424 at 8/3, 405x909 logical + pw, ph, scale, w, h = 1080, 2424, 8 / 3, 405, 909 + px = m.Geometry.px + + +geo = Geo() +check(geo.px(202.5, 896) == (540, 2389), "logical points map to panel pixels") +over_app = {"x": 0, "y": 44, "w": 405, "h": 821} # the page ends above the home strip +t = {tid["id"]: tid for tid in m.TESTS} +check(m.gesture_points(t["01"], geo, over_app, None)[:4] == (202.5, 896, 202.5, 621), + "the edge swipe starts 13 px above the screen's bottom") +check(m.gesture_points(t["09"], geo, over_app, None)[:4] == (202.5, 849, 202.5, 586), + "the page-strip swipe starts 16 px above the page's bottom") +check(m.gesture_points(t["11"], geo, over_app, None)[1] == 869, + "the band-top swipe starts 40 px above the edge, inside a 44 px home strip") + +# Preconditions, with hyprctl's answers made up. +home_page = {"level": 2, "ns": "omamobile-wifi", "x": 0, "y": 44, "w": 405, "h": 865} +strip = {"level": 1, "ns": "omamobile-home-strip", "x": 0, "y": 865, "w": 405, "h": 44} +app_page = dict(home_page, h=821) +osk = {"level": 2, "ns": "osk", "x": 0, "y": 623, "w": 405, "h": 242} +open_wifi = {"wifiPanel": True} +m.clients_on = lambda ws: [] +m.screen_locked = lambda: False +check(m.precondition(t["01"], open_wifi, [home_page], "9", geo) == [], "home: no window, no strip, the page reaches the bottom") +check(m.precondition(t["01"], open_wifi, [home_page, strip], "9", geo) != [], "home: a home strip is refused") +check(m.precondition(t["01"], {"wifiPanel": False}, [home_page], "9", geo) != [], "a page that isn't open is refused") +check(m.precondition(t["01"], open_wifi, [home_page, osk], "9", geo) != [], "the on-screen keyboard is refused") +m.clients_on = lambda ws: [{"class": "gesture-test"}] +check(m.precondition(t["07"], open_wifi, [app_page, strip], "8", geo) == [], "over an app: the page ends where the home strip begins") +check(m.precondition(t["07"], open_wifi, [home_page, strip], "8", geo) != [], "over an app: a page over the strip is refused") +check(m.precondition(t["07"], open_wifi, [app_page], "8", geo) != [], "over an app: no home strip is refused") +m.clients_on = lambda ws: [] +sheet = {"opened": True, "media": None} +check(m.precondition(t["06"], dict(sheet, notifications=3), [], "9", geo) != [], "the list scroll needs 6 notifications") +check(m.precondition(t["06"], dict(sheet, notifications=8), [], "9", geo) == [], "and runs with 8") +recents = {"level": 3, "ns": "omamobile-switcher", "x": 0, "y": 0, "w": 405, "h": 909} +check(m.precondition(t["01"], open_wifi, [home_page, recents], "9", geo) != [], "a test doesn't start with Recents open") +m.screen_locked = lambda: True +check("the screen is locked" in m.precondition(t["01"], open_wifi, [home_page], "9", geo), + "the lock is checked before every gesture, not only at the start") +m.screen_locked = lambda: False +for ns in m.CALL_NS: + call = {"level": 3, "ns": ns, "x": 0, "y": 0, "w": 405, "h": 120} + check("a call's screen is up" in m.precondition(t["01"], open_wifi, [home_page, call], "9", geo), + "no gesture while a call's screen (%s) is up" % ns) + +# A page that closed passes only if Recents stayed shut: Recents means the +# touch reached the home strip. +check(m.gesture_result(t["07"], {"wifiPanel": False}, [strip]) == ("PASS", False), + "over an app: the page closed and Recents stayed shut") +check(m.gesture_result(t["07"], {"wifiPanel": False}, [strip, recents]) == ("FAIL", True), + "a page that closed while Recents opened fails") +check(m.gesture_result(t["07"], {"wifiPanel": True}, [app_page, strip, recents]) == ("FAIL", True), + "and so does Recents over a page that stayed open (the bug 07 is about)") + +# Fictional notifications never push the user's out of the history. +check(m.notes_to_post(8, 10, 2) == 6, "with 2 of 10 taken, 6 fit and 2 stay free") +check(m.notes_to_post(8, 10, 0) == 8, "with an empty history, all 8") +check(m.notes_to_post(8, 10, 9) == 0 and m.notes_to_post(8, 10, 12) == 0, "with a full history, none") + +# The commands a run needs are checked at its start, with their packages. +empty = tempfile.mkdtemp() +need = m.needed_tools([t["01"], t["07"], t["05"]], False, False) +check({"foot", "grim", "magick", "notify-send", "sudo"} <= need and "wf-recorder" not in need, + "a run needs foot, grim, magick, notify-send and sudo for these tests") +check("foot" not in m.needed_tools([t["01"]], True, True) and "wf-recorder" in m.needed_tools([t["01"]], True, True), + "foot only for a test over an app, wf-recorder only with --record") +check("magick (package imagemagick)" in m.missing_tools(need, empty), "a missing command is named with its package") + +# The sudo password: read once, out of the environment before any child +# gets one, and never on the helper's stdin (sudo -S -v, then sudo -n). +check(m.SUDO_PW == "not-a-password" and "GESTURE_TEST_SUDO_PW" not in m.ENV + and "GESTURE_TEST_SUDO_PW" not in os.environ, "the sudo password is taken out of the environment") +calls = [] +real_run = m.subprocess.run +m.subprocess.run = lambda cmd, **kw: calls.append((cmd, kw.get("input"))) or subprocess.CompletedProcess(cmd, 0) +check(m.sudo_prefix("pw") == ["sudo", "-n"] and calls == [(["sudo", "-S", "-p", "", "-v"], "pw\n")], + "with a password: a ticket from sudo -S -v, then sudo -n for the helper") +calls.clear() +check(m.sudo_prefix(None) == ["sudo", "-n"] and calls == [(["sudo", "-v"], None)], + "without one: sudo -v asks once, then sudo -n") +m.subprocess.run = lambda cmd, **kw: subprocess.CompletedProcess(cmd, 1) +try: + m.sudo_prefix("wrong") + check(False, "a refused password stops the run") +except RuntimeError: + check(True, "a refused password stops the run") +m.subprocess.run = real_run + + +# Finger.close never raises: the session user can't signal a helper under sudo. +class Stuck: + class stdin: + close = staticmethod(lambda: None) + + def wait(self, timeout=None): + raise subprocess.TimeoutExpired("sudo", timeout) + + def terminate(self): + raise PermissionError(1, "Operation not permitted") + + kill = terminate + + +f = object.__new__(m.Finger) +f.p, f.left_running = Stuck(), False +f.close() +check(f.left_running, "a helper that can't be stopped is noted, and close() doesn't raise") + +# SIGHUP (the SSH session or the USB link dropped) and SIGTERM end a run +# through its cleanup, as Ctrl-C does. +m.end_on_signals() +for sig in (signal.SIGHUP, signal.SIGTERM): + try: + os.kill(os.getpid(), sig) + check(False, "%s ends a run through its cleanup" % sig.name) + except SystemExit as e: + check(e.code == 128 + sig, "%s ends a run through its cleanup" % sig.name) +signal.signal(signal.SIGHUP, signal.SIG_DFL) +signal.signal(signal.SIGTERM, signal.SIG_DFL) + +# What a run that couldn't clean up left is put back at the next start, from +# its record; each step checks how things are now. +d = tempfile.mkdtemp() +m.STATE, m.CONF = d + "/state/run.json", d + "/quicksettings.json" +m.CONF_ASIDE, m.SEEN = m.CONF + ".gesture-test-aside", d + "/notifications-seen" +dnd = {"state": "on"} +dismissed = [] +rows = [{"app": m.NOTES_APP, "key": 3}, {"app": "Signal", "key": 4}, {"app": m.NOTES_APP, "key": 5}] + + +def fake_notifications(method, arg=None): + if method == "setDnd": + dnd["state"] = arg + return dnd["state"] + + +def fake_qs(method, arg=""): + if method == "notificationRows": + return json.dumps([r for r in rows if r["key"] not in dismissed]) + if method == "dismissNotification": + dismissed.append(int(arg)) + return "" + + +screen = {"ws": "1", "page": False, "reset": 0} +m.notifications, m.qs = fake_notifications, fake_qs +m.state = lambda: {"wifiPanel": screen["page"], "bluetoothPanel": False, "opened": False} +m.reset_ui = lambda: screen.update(page=False, reset=screen["reset"] + 1) +m.hypr = lambda what: {"name": screen["ws"]} +m.focus = lambda ws: screen.update(ws=ws) +m.clients_on = lambda ws: [] +check(m.read_state() is None, "no record, no earlier run") +m.write_state({"pid": 999999, "started": "then", "seen_before": "100\n", "dnd_before": "off", + "tiles_aside": True, "app_pid": 999999, "posted": 2}) +open(m.CONF_ASIDE, "w").write("{}") +open(m.SEEN, "w").write("200\n") +rec = m.read_state() +check(rec["dnd_before"] == "off" and not os.path.exists(m.STATE + ".tmp"), "the record is written in one rename") +done = m.put_back(rec) +check(dnd["state"] == "off" and os.path.exists(m.CONF) and not os.path.exists(m.CONF_ASIDE), + "do-not-disturb and the tile layout are put back") +check(sorted(dismissed) == [3, 5] and open(m.SEEN).read() == "100\n" and len(done) == 4, + "the fictional notifications go, the owner's stay, the unread mark moves back") +check(m.put_back(rec) == [] and dnd["state"] == "off", "a second time, nothing changes") +screen.update(ws="8", page=True) +done = m.put_back(dict(rec, start_ws="1", home_ws="9", app_ws="8")) +check(screen["ws"] == "1" and not screen["page"] and len(done) == 2, + "a page that run left open closes, and its empty workspace goes back to the one it started on") +screen.update(ws="3") +m.put_back(dict(rec, start_ws="1", home_ws="9", app_ws="8")) +check(screen["ws"] == "3", "a workspace that isn't the run's is left in front") +dnd["state"] = "on" +check(m.put_back(dict(rec, dnd_before="on")) == [] and dnd["state"] == "on", + "do-not-disturb that was on before the run stays on") +open(m.SEEN, "w").write("50\n") +m.put_back(rec) +check(open(m.SEEN).read() == "50\n", "the unread mark is never moved forward") +open(m.CONF_ASIDE, "w").write("{}") +m.put_back(rec) +check(os.path.exists(m.CONF_ASIDE), "a tile file aside is left alone when the owner has a new one") +check(not m.ours(os.getpid(), "no-such-program") and m.ours(os.getpid(), "python"), + "a recorded pid counts only while its command line is the suite's") +open(m.STATE, "w").write("garbage") +check(m.read_state() == {}, "an unreadable record still counts as a run that didn't clean up") +m.write_state({"pid": 999999, "started": "then"}) +m.recover(m.read_state()) +check(m.read_state() is None, "recover removes the record") +EOF diff --git a/tools/phone-gesture-test b/tools/phone-gesture-test new file mode 100755 index 000000000..2a76d827a --- /dev/null +++ b/tools/phone-gesture-test @@ -0,0 +1,1108 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""phone-gesture-test: the quick-settings swipe gestures, run on the phone and +checked through the shell's own state. Touch points come from the output and +the page's own window, so no number in it is one device's. + +usage: tools/phone-gesture-test [--record] [--keep-tiles] [--loud] [--out DIR] [--list] [--cleanup] [ID ...] + +Run it on the phone, in the user's session, from a checkout (or with +tools/phone-gesture-touch next to it). Each test sets up a known screen +itself, so the result doesn't depend on the device, its screen size or the +user's setup: + - pages and the sheet open through the shell's IPC (openWifi, openBluetooth, + summon), never by a tap on a tile, so the tile layout doesn't matter; + - for the run, quick settings use the package's default tile layout (the + user's ~/.config/omamobile/quicksettings.json is put back afterwards); + - "home" is an empty workspace, "app" one with a terminal the script opens + and closes again; + - the shade's tests post fictional notifications within the history's room + and dismiss them again; do-not-disturb is on during the run; + - only the gesture under test is a real touch, through a virtual + touchscreen (tools/phone-gesture-touch, root), at points taken from the + output's size and the page's own window (hyprctl layers), in logical + pixels. +Verdicts are computed, never judged: a test passes when the shell's state +after the gesture is the one the scenario expects (`omarchy-shell shell call +omamobile.quicksettings state`) and Recents stayed shut, and a page that +should stay open must also show in the pixels that its content moved up with +the drag (docs/GESTURE-TESTS.md). + +It takes over the screen for a few minutes (workspaces switch, a terminal +opens, notifications appear and go): tell whoever holds the phone first. + +root: phone-gesture-touch needs /dev/uinput. Without write access the script +first gets a sudo ticket (sudo -v, which asks once) and then starts it with +sudo -n, so the helper's stdin only ever carries swipes. GESTURE_TEST_SUDO_PW +supplies the password for an unattended run: it is read once and taken out of +the environment, so no child sees it (the terminal under the pages keeps its +environment for its lifetime), and nothing is stored. + +A run that ends early (Ctrl-C, SIGTERM, or SIGHUP when the SSH session or the +USB link drops) puts everything back before it exits. What it is about to +change is written to ~/.local/state/phone-gesture-test/run.json first, and +the file goes once everything is back, so what a run that couldn't clean up +left (SIGKILL, a crash, a power cut) is put back at the next start, or by +--cleanup alone. +""" +import argparse, glob, hashlib, json, os, re, shutil, signal, subprocess, sys, time + +HERE = os.path.dirname(os.path.abspath(__file__)) +VT = os.path.join(HERE, "phone-gesture-touch") +APP_ID = "gesture-test" +QS = "omamobile.quicksettings" +CONF = os.path.expanduser("~/.config/omamobile/quicksettings.json") +CONF_ASIDE = CONF + ".gesture-test-aside" +PAGE_NS = {"wifi": "omamobile-wifi", "bluetooth": "omamobile-bluetooth"} +RECENTS_NS = "omamobile-switcher" # mapped only while Recents is open +OPEN_KEY = {"wifi": "wifiPanel", "bluetooth": "bluetoothPanel", "sheet": "opened"} + +# Gestures, in logical pixels: +# edge from the true bottom edge of the screen +# page-strip from the page's own strip (just above its window's bottom) +# body from the page body, well above the strip +# last-row from the last visible row of the Wi-Fi list (found in a screenshot) +# card from the shade card's header (top half of the screen) +# list from the notification list near the bottom +# band-top from the top of the bottom band, 40 px above the edge: inside +# the home strip's 44 px zone over an app, where a page strip +# shorter than that zone left a band that opened Recents +TESTS = [ + dict(id="01", name="wifi-bottom-edge-home", ctx="home", page="wifi", gesture="edge", + title="Wi-Fi page on the home screen: swipe up from the bottom edge", + expect="the Wi-Fi page closes", want={"wifiPanel": False}), + dict(id="02", name="bluetooth-bottom-edge-home", ctx="home", page="bluetooth", gesture="edge", + title="Bluetooth page on the home screen: swipe up from the bottom edge", + expect="the Bluetooth page closes", want={"bluetoothPanel": False}), + dict(id="03", name="wifi-drag-from-last-network-home", ctx="home", page="wifi", gesture="last-row", + title="Wi-Fi page on the home screen: drag up starting on the last visible network", + expect="the page stays open (the list scrolls)", want={"wifiPanel": True}), + dict(id="04", name="wifi-drag-in-body-above-strip-home", ctx="home", page="wifi", gesture="body", + title="Wi-Fi page on the home screen: drag up in the page body, above the strip", + expect="the page stays open (only the strip closes it)", want={"wifiPanel": True}), + dict(id="05", name="notification-card-swipe-home", ctx="home", page="sheet", gesture="card", + title="Quick settings on the home screen: swipe up on the notification card's header", + expect="quick settings close", want={"opened": False}, notes=1), + dict(id="06", name="notification-list-scroll-home", ctx="home", page="sheet", gesture="list", + title="Quick settings on the home screen: drag up starting in the notification list", + expect="the list scrolls, quick settings stay open", want={"opened": True}, notes=6), + dict(id="07", name="wifi-bottom-edge-over-app", ctx="app", page="wifi", gesture="edge", + title="Wi-Fi page over an app: swipe up from the bottom edge", + expect="the Wi-Fi page closes", want={"wifiPanel": False}), + dict(id="08", name="bluetooth-bottom-edge-over-app", ctx="app", page="bluetooth", gesture="edge", + title="Bluetooth page over an app: swipe up from the bottom edge", + expect="the Bluetooth page closes", want={"bluetoothPanel": False}), + dict(id="09", name="wifi-page-strip-over-app", ctx="app", page="wifi", gesture="page-strip", + title="Wi-Fi page over an app: swipe up from the page's own strip", + expect="the Wi-Fi page closes", want={"wifiPanel": False}), + dict(id="10", name="bluetooth-page-strip-over-app", ctx="app", page="bluetooth", gesture="page-strip", + title="Bluetooth page over an app: swipe up from the page's own strip", + expect="the Bluetooth page closes", want={"bluetoothPanel": False}), + dict(id="11", name="wifi-band-top-over-app", ctx="app", page="wifi", gesture="band-top", + title="Wi-Fi page over an app: swipe up from the top of the bottom band, 40 px above the edge", + expect="the Wi-Fi page closes", want={"wifiPanel": False}), + dict(id="12", name="bluetooth-band-top-over-app", ctx="app", page="bluetooth", gesture="band-top", + title="Bluetooth page over an app: swipe up from the top of the bottom band, 40 px above the edge", + expect="the Bluetooth page closes", want={"bluetoothPanel": False}), + dict(id="13", name="wifi-band-top-home", ctx="home", page="wifi", gesture="band-top", + title="Wi-Fi page on the home screen: swipe up from the top of the bottom band, 40 px above the edge", + expect="the Wi-Fi page closes", want={"wifiPanel": False}), +] +SETTLE = {"wifi": 6.0, "bluetooth": 3.0, "sheet": 1.5} # the Wi-Fi page rescans on open +NOTIFICATIONS = 8 # posted for the shade's tests, never more than the history has room for +NOTES_APP = "phone-gesture-test" +# Fictional notifications of different lengths (the shade shows a body on one +# line, and the long one wraps onto a second): identical rows would make the +# list's row profile periodic, and its upward shift ambiguous by whole rows +# (shift_up). +NOTE_TEXTS = [ + ("Test notification 1", "Fictional, posted by phone-gesture-test."), + ("A fictional reminder", "Two lines this time,\nthe second one shorter."), + ("3", "Short."), + ("Gesture test: a longer fictional title", + "A longer fictional body that wraps onto another line in a phone's narrow shade, so rows differ in height."), + ("Fictional message", "Nothing to do here."), + ("Test notification six", "Three lines:\none\ntwo"), + ("Seven", "Fictional, posted to fill the shade."), + ("The eighth fictional notification", "Last one."), +] +HISTORY = os.path.expanduser("~/.local/state/omarchy/notifications/history") +OMARCHY_SHELL = "/usr/share/omarchy/shell" +SEEN = os.path.expanduser("~/.local/state/omamobile/notifications-seen") # the bar's unread mark +HEADROOM = 2 # history slots left free for real notifications that arrive during a run +# What a run is about to change, written before each change and removed once +# everything is back (Fixtures.save, put_back). +STATE = os.path.expanduser("~/.local/state/phone-gesture-test/run.json") +CALL_NS = ("omamobile-call", "omamobile-call-top") # a call's screen and its heads-up card +# The commands a run needs, with the package that has them. +TOOLS = {"hyprctl": "hyprland", "omarchy-shell": "omarchy-shell", "qs": "quickshell", "grim": "grim", + "magick": "imagemagick", "foot": "foot", "notify-send": "libnotify", "wf-recorder": "wf-recorder", + "sudo": "sudo"} +THERMAL_LIMIT = 75.0 # deg C: above it the phone throttles and touches arrive late; wait it out + + +def history_limit(): + """The notification service keeps only its newest historyLimit files and + deletes the rest, so posting more than there is room for would push the + owner's own notifications out for good.""" + try: + with open(OMARCHY_SHELL + "/plugins/notifications/Service.qml") as f: + return int(re.search(r"historyLimit:\s*(\d+)", f.read()).group(1)) + except (OSError, AttributeError): + return 10 + + +# ------------------------------------------------------------------ plumbing + +def session_env(): + env = dict(os.environ) + run = env.setdefault("XDG_RUNTIME_DIR", "/run/user/%d" % os.getuid()) + if not env.get("HYPRLAND_INSTANCE_SIGNATURE"): + sigs = sorted(glob.glob(run + "/hypr/*/"), key=os.path.getmtime) + if sigs: + env["HYPRLAND_INSTANCE_SIGNATURE"] = os.path.basename(sigs[-1].rstrip("/")) + if not env.get("WAYLAND_DISPLAY"): + socks = sorted(p for p in glob.glob(run + "/wayland-*") if not p.endswith(".lock")) + if socks: + env["WAYLAND_DISPLAY"] = os.path.basename(socks[0]) + return env + + +# The password for an unattended run, read once and taken out of the +# environment before ENV is copied from it: every child gets ENV, and the +# terminal under the pages keeps its environment for its lifetime. +SUDO_PW = os.environ.pop("GESTURE_TEST_SUDO_PW", None) +ENV = session_env() + + +def sh(*cmd, check=False, input=None, timeout=30): + """A command's stdout. A timeout, so a shell that stopped answering can't + hold up the cleanup, which runs with the signals that end a run ignored.""" + try: + r = subprocess.run(cmd, capture_output=True, text=True, input=input, env=ENV, timeout=timeout) + except subprocess.TimeoutExpired: + raise RuntimeError("%s gave no answer in %d s" % (" ".join(cmd), timeout)) + if check and r.returncode != 0: + raise RuntimeError("%s failed: %s" % (" ".join(cmd), r.stderr.strip())) + return r.stdout + + +def hypr(what): + return json.loads(sh("hyprctl", "-j", what)) + + +def dispatch(lua): + sh("hyprctl", "dispatch", lua) + + +def shell(*args): + return sh("omarchy-shell", "shell", *args) + + +def qs(method, arg=""): + return shell("call", QS, method, arg) + + +def state(): + return json.loads(qs("state")) + + +def notifications(method, arg=None): + """The Omarchy shell's notification service, through its own IPC target.""" + return sh("qs", "ipc", "-p", OMARCHY_SHELL, "call", "notifications", method, *([arg] if arg else [])).strip() + + +def shell_build(): + """Which shell ran: the package, and a hash of the quick-settings QML it + loaded (two builds can carry the same version).""" + d = os.path.realpath(os.path.expanduser("~/.config/omarchy/plugins/" + QS)) + h = hashlib.sha256() + for f in ("QuickSettings.qml", "WifiPanel.qml", "BluetoothPanel.qml"): + try: + with open(os.path.join(d, f), "rb") as fh: + h.update(fh.read()) + except OSError: + h.update(b"-") + return {"package": sh("pacman", "-Q", "omamobile-shell").strip(), "plugin_dir": d, + "qml_sha256": h.hexdigest()[:16]} + + +class Geometry: + """The focused output: its panel pixels, scale and logical size.""" + + def __init__(self): + m = next((m for m in hypr("monitors") if m.get("focused")), hypr("monitors")[0]) + self.name, self.transform = m["name"], m["transform"] + self.pw, self.ph, self.scale = m["width"], m["height"], float(m["scale"]) + self.w, self.h = round(self.pw / self.scale), round(self.ph / self.scale) + + def px(self, x, y): + return (min(self.pw - 1, max(0, round(x * self.scale))), + min(self.ph - 1, max(0, round(y * self.scale)))) + + def info(self): + return {"output": self.name, "panel": [self.pw, self.ph], "scale": self.scale, + "logical": [self.w, self.h]} + + +def layers(geo): + out = [] + for lvl, ls in hypr("layers").get(geo.name, {}).get("levels", {}).items(): + for l in ls: + out.append({"level": int(lvl), "ns": l["namespace"], "x": l["x"], "y": l["y"], + "w": l["w"], "h": l["h"]}) + return out + + +def clients_on(ws): + return [c for c in hypr("clients") if c["workspace"]["name"] == ws] + + +def focus(ws): + dispatch('hl.dsp.focus({ workspace = "%s" })' % ws) + + +def screen_locked(): + return sh("omarchy-shell", "lock", "isLocked").strip() == "true" + + +CLEANUP_TOOLS = {"hyprctl", "omarchy-shell", "qs"} # all that --cleanup uses + + +def needed_tools(tests, record, uinput_writable): + need = CLEANUP_TOOLS | {"grim", "magick"} + if any(t["ctx"] == "app" for t in tests): + need.add("foot") + if any(t["page"] == "sheet" for t in tests): + need.add("notify-send") + if record: + need.add("wf-recorder") + if not uinput_writable: + need.add("sudo") + return need + + +def missing_tools(need, path): + """The commands of `need` that aren't on `path`, as "cmd (package pkg)".""" + return ["%s (package %s)" % (c, TOOLS.get(c, c)) for c in sorted(need) if not shutil.which(c, path=path)] + + +# ------------------------------------------------------------------ the finger + +def sudo_prefix(pw): + """How to start the helper as root: with a ticket from sudo -v first, then + sudo -n, which never reads stdin. (Started with sudo -S, a ticket from a + moment ago means sudo doesn't read the password, and the password line goes + on to the helper as its first swipe.) pw comes from GESTURE_TEST_SUDO_PW; + None asks on the terminal, once.""" + if pw is None: + subprocess.run(["sudo", "-v"], check=True) + else: + r = subprocess.run(["sudo", "-S", "-p", "", "-v"], input=pw + "\n", text=True, capture_output=True) + if r.returncode != 0: + raise RuntimeError("sudo did not take GESTURE_TEST_SUDO_PW") + return ["sudo", "-n"] + + +class Finger: + """phone-gesture-touch, started once for the run (root for /dev/uinput).""" + + def __init__(self, geo): + args = [sys.executable, VT, str(geo.pw), str(geo.ph)] + if not os.access("/dev/uinput", os.W_OK): + args = sudo_prefix(SUDO_PW) + args + self.left_running = False + self.p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, bufsize=1) + ready = json.loads(self.p.stdout.readline() or "{}") + if not ready.get("ready"): + raise RuntimeError("the virtual touchscreen did not start") + + def swipe(self, geo, x0, y0, x1, y1, ms, label): + (a, b), (c, d) = geo.px(x0, y0), geo.px(x1, y1) + self.p.stdin.write(json.dumps({"x0": a, "y0": b, "x1": c, "y1": d, "ms": ms}) + "\n") + self.p.stdin.flush() + rec = json.loads(self.p.stdout.readline()) + if "error" in rec: + raise RuntimeError("touch refused: %s" % rec) + rec.update(label=label, logical=[x0, y0, x1, y1]) + return rec + + def close(self): + """End of input: the helper removes its device and exits. Never raises, + since it runs from the cleanup: the session user can't signal a helper + under sudo, and an exception here would cost results.json. A helper + that is still there is noted (left_running); it exits once it reads the + end of its input.""" + try: + self.p.stdin.close() + except OSError: + pass + for stop in (None, self.p.terminate): + try: + if stop: + stop() + self.p.wait(timeout=5) + return + except (subprocess.TimeoutExpired, OSError): + pass + self.left_running = True + + +# ------------------------------------------------------------------ fixtures + +def write_state(rec): + """STATE, replaced in one rename, so a run killed mid-write leaves the old + record or the new one, never half of one.""" + os.makedirs(os.path.dirname(STATE), exist_ok=True) + tmp = STATE + ".tmp" + with open(tmp, "w") as f: + json.dump(rec, f) + os.replace(tmp, STATE) + + +def read_state(): + """The record a run left: None when there is none; {} when it can't be + read, which still counts as a run that didn't clean up.""" + try: + with open(STATE) as f: + rec = json.load(f) + except FileNotFoundError: + return None + except (OSError, ValueError): + return {} + return rec if isinstance(rec, dict) else {} + + +def ours(pid, what): + """pid is still a process of the suite's (its command line names `what`), + not a later process that got the same number.""" + if not isinstance(pid, int) or pid <= 1: + return False + try: + with open("/proc/%d/cmdline" % pid, "rb") as f: + return what.encode() in f.read() + except OSError: + return False + + +def dismiss_fictional(): + """Take the suite's notifications out of the shade the way a swipe does + (dismissNotification). How many it dismissed.""" + try: + rows = json.loads(qs("notificationRows") or "[]") + except ValueError: + rows = [] + n = 0 + for r in rows: + if r.get("app") == NOTES_APP and r.get("key") is not None: + qs("dismissNotification", str(r["key"])) + n += 1 + return n + + +def put_back(rec): + """Put back what an earlier run recorded in STATE and didn't put back + itself. Each step checks the screen as it is now, so a step that run never + got to, or one already undone, changes nothing. Returns what it did, one + line each.""" + done = [] + if rec.get("dnd_before") == "off" and notifications("dndState") == "on": + notifications("setDnd", "off") + done.append("do-not-disturb off again (it was off before that run)") + if rec.get("tiles_aside") and os.path.exists(CONF_ASIDE): + if os.path.exists(CONF): + done.append("left %s alone: %s exists as well" % (CONF_ASIDE, CONF)) + else: + os.replace(CONF_ASIDE, CONF) + done.append("the tile layout back in place") + if ours(rec.get("app_pid"), APP_ID): + try: + os.killpg(rec["app_pid"], signal.SIGTERM) + done.append("its terminal closed") + except OSError: + pass + n = dismiss_fictional() + if n: + done.append("%d fictional notification%s dismissed" % (n, "" if n == 1 else "s")) + # The screen: a page or the sheet that run opened, and one of its + # workspaces in front (empty now that its terminal is gone). + try: + st = state() + except ValueError: + st = {} + if any(st.get(k) for k in OPEN_KEY.values()): + reset_ui() + done.append("quick settings closed") + ws = hypr("activeworkspace")["name"] + if rec.get("start_ws") and ws != rec["start_ws"] and ws in (rec.get("home_ws"), rec.get("app_ws")) \ + and not clients_on(ws): + focus(rec["start_ws"]) + done.append("back on workspace %s" % rec["start_ws"]) + # The bar's unread mark: only ever moved back. If it is newer than before + # that run, the run (opening the shade) or the owner since then moved it; + # moving it back can show a notification as unread that was read, never + # hide one that wasn't. + seen = rec.get("seen_before") + if isinstance(seen, str): + try: + with open(SEEN) as f: + now = f.read() + except OSError: + now = None + if now is not None and now != seen and now.strip().isdigit() and seen.strip().isdigit() \ + and int(now) > int(seen): + with open(SEEN, "w") as f: + f.write(seen) + done.append("the bar's unread mark back") + return done + + +def recover(rec): + """At the start of a run (or --cleanup): put back what an earlier run that + couldn't clean up left, and remove its record. Refuses while that run is + still going.""" + if ours(rec.get("pid"), "phone-gesture-test") and rec.get("pid") != os.getpid(): + sys.exit("phone-gesture-test: another run is going (pid %d)" % rec["pid"]) + lines = put_back(rec) + os.remove(STATE) + when = rec.get("started", "an unknown time") + if lines: + print("phone-gesture-test: a run from %s ended without its cleanup; put back: %s" + % (when, "; ".join(lines)), flush=True) + else: + print("phone-gesture-test: a run from %s ended without its cleanup; nothing was left changed" + % when, flush=True) + + +class Fixtures: + """Everything the run changes, and puts back in reverse. Each change is + recorded in STATE before it is made (save), so that a run that never gets + to restore() can be put back by the next one (recover).""" + + def __init__(self, keep_tiles, quiet): + self.keep_tiles = keep_tiles + self.tiles_aside = False + self.want_quiet = quiet + self.dnd_before = None + self.history_before = None + self.history_kept = None + self.posted = 0 + self.dnd_during = self.dnd_after = None + self.app = None + self.home_ws = self.app_ws = None + self.restore_errors = [] + self.start_ws = hypr("activeworkspace")["name"] + # The owner's notification history and the bar's unread mark, checked + # and put back at the end of every run. + self.history_before = sorted(os.listdir(HISTORY)) if os.path.isdir(HISTORY) else [] + try: + with open(SEEN) as f: + self.seen_before = f.read() + except OSError: + self.seen_before = None + self.rec = {"pid": os.getpid(), "started": time.strftime("%Y-%m-%d %H:%M:%S"), + "seen_before": self.seen_before, "start_ws": self.start_ws} + write_state(self.rec) + + def save(self, **changes): + self.rec.update(changes) + write_state(self.rec) + + def tiles(self): + if self.keep_tiles: + return + if os.path.exists(CONF_ASIDE): + raise RuntimeError("%s is left from an earlier run: put it back as %s first" % (CONF_ASIDE, CONF)) + if os.path.exists(CONF): + self.save(tiles_aside=True) + os.replace(CONF, CONF_ASIDE) # the shell watches the file: the default layout applies now + self.tiles_aside = True + + def workspaces(self): + used = {c["workspace"]["name"] for c in hypr("clients")} | {self.start_ws} + free = [w for w in ("9", "8", "7", "6", "5", "4") if w not in used] + if len(free) < 2: + raise RuntimeError("needs two empty workspaces among 4-9") + self.home_ws, self.app_ws = free[0], free[1] + self.save(home_ws=self.home_ws, app_ws=self.app_ws) + + def open_app(self): + focus(self.app_ws); time.sleep(0.5) + self.app = subprocess.Popen( + ["foot", "--app-id", APP_ID, "--title", "phone-gesture-test", + "sh", "-c", "echo 'phone-gesture-test: the app under the page'; exec sleep 3600"], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True, env=ENV) + self.save(app_pid=self.app.pid) + for _ in range(80): + if any(c["class"] == APP_ID for c in clients_on(self.app_ws)): + return + time.sleep(0.1) + raise RuntimeError("the test app did not appear on workspace %s" % self.app_ws) + + def quiet(self): + """Do-not-disturb for the run: no sound or popup for the fictional + notifications, and no popup over the screen being measured.""" + if not self.want_quiet: + return + state = notifications("dndState") + if state not in ("on", "off"): + raise RuntimeError("the notification service doesn't answer dndState (%r); use --loud" % state) + self.dnd_before = state + if state == "off": + self.save(dnd_before="off") + notifications("setDnd", "on") + self.dnd_during = notifications("dndState") + if self.dnd_during != "on": + raise RuntimeError("do-not-disturb didn't turn on (%r); use --loud" % self.dnd_during) + + def post_notifications(self, want): + """Fictional notifications for the shade's tests, never more than the + history has room for, less HEADROOM: the service deletes its oldest + files beyond historyLimit, and those would be the owner's.""" + n = notes_to_post(want, history_limit(), len(self.history_before)) + self.save(posted=n) + for title, body in NOTE_TEXTS[:n]: + sh("notify-send", "--app-name=" + NOTES_APP, title, body) + self.posted = n + time.sleep(1.0) + + def forget_notifications(self): + """Take the suite's notifications out of the shade again, then check + that every history file that was there before the run still is.""" + dismiss_fictional() + time.sleep(1.0) + now = set(os.listdir(HISTORY)) if os.path.isdir(HISTORY) else set() + self.history_kept = set(self.history_before) <= now + + def restore(self): + """Every step runs even when one before it fails; STATE goes only when + all of them worked, so the next run tries the rest again.""" + def step(what, fn): + try: + fn() + except Exception as e: + self.restore_errors.append("%s: %s" % (what, e)) + + def seen(): + try: + with open(SEEN) as f: + changed = f.read() != self.seen_before + except OSError: + changed = True + if changed: + with open(SEEN, "w") as f: + f.write(self.seen_before) + + def dnd(): + if self.dnd_before == "off": + notifications("setDnd", "off") + if self.dnd_before: + self.dnd_after = notifications("dndState") + if self.dnd_before == "off" and self.dnd_after != "off": + raise RuntimeError("do-not-disturb reads %r" % self.dnd_after) + + def app(): + try: + os.killpg(self.app.pid, signal.SIGTERM) + except ProcessLookupError: + pass + self.app = None + + if self.history_kept is None: + step("the fictional notifications", self.forget_notifications) + if self.seen_before is not None: + step("the unread mark", seen) + step("do-not-disturb", dnd) + if self.app: + step("the terminal", app) + if self.tiles_aside and os.path.exists(CONF_ASIDE): + step("the tile layout", lambda: os.replace(CONF_ASIDE, CONF)) + self.tiles_aside = os.path.exists(CONF_ASIDE) + step("the screen", reset_ui) + step("the workspace", lambda: focus(self.start_ws)) + if not self.restore_errors: + try: + os.remove(STATE) + except FileNotFoundError: + pass + + +def reset_ui(): + shell("summon", QS, "{}") # opening the sheet closes the Wi-Fi and Bluetooth pages + time.sleep(0.4) + for plugin in (QS, "omamobile.switcher", "omamobile.launcher"): + shell("hide", plugin) + shell("call", "omamobile.keyboard", "hideKeyboard", "") + time.sleep(1.0) + + +# ------------------------------------------------------------------ one test + +def last_row_y(png, page, geo): + """The lowest line of text in the Wi-Fi list, in logical pixels: inside the + page's window, below its header and switch, above its strip.""" + x0, y0 = geo.px(page["x"] + 60, page["y"] + 166) + x1, y1 = geo.px(page["x"] + page["w"] - 52, page["y"] + page["h"] - 48) + out = sh("magick", png, "-crop", "%dx%d+%d+%d" % (x1 - x0, y1 - y0, x0, y0), "+repage", + "-colorspace", "gray", "-threshold", "45%", "-scale", "1x!", "-threshold", "0.5%", "txt:-") + last = None + for line in out.splitlines()[1:]: + pos, rest = line.split(":", 1) + if "white" in rest or "FFFFFF" in rest.upper() or "gray(255)" in rest: + last = int(pos.split(",")[1]) + if last is None: + raise RuntimeError("no list text found (a light theme, or an empty list?)") + return (y0 + last) / geo.scale + + +def gesture_points(t, geo, page, png): + w, h = geo.w, geo.h + bottom = page["y"] + page["h"] if page else h + g = t["gesture"] + if g == "edge": + return (w / 2, h - 13, w / 2, h - 288, 300) + if g == "page-strip": + return (w / 2, bottom - 16, w / 2, bottom - 279, 300) + if g == "body": + return (w / 2, bottom - 73, w / 2, bottom - 336, 300) + if g == "last-row": + y = last_row_y(png, page, geo) - 10 + return (w / 2, y, w / 2, max(page["y"] + 60, y - 263), 350) + if g == "band-top": + return (w / 2, h - 40, w / 2, h - 315, 300) + if g == "card": + return (w * 0.28, h * 0.50, w * 0.28, h * 0.23, 350) + if g == "list": + return (w / 2, h - 67, w / 2, h - 367, 350) + raise ValueError(g) + + +def precondition(t, st, lay, ws, geo): + problems = [] + ns = {l["ns"]: l for l in lay} + strip = ns.get("omamobile-home-strip") + page = ns.get(PAGE_NS.get(t["page"], "")) + on_ws = clients_on(ws) + # Checked before every gesture, not once per run: a run takes minutes, + # and a swipe must never land on the lock screen or a call. + if screen_locked(): + problems.append("the screen is locked") + if any(n in ns for n in CALL_NS): + problems.append("a call's screen is up") + if "osk" in ns: + problems.append("the on-screen keyboard is up") + if RECENTS_NS in ns: + problems.append("Recents is open") + if t["ctx"] == "home": + if on_ws: + problems.append("the home workspace has windows") + if strip: + problems.append("the shell's bottom strip is shown on the home screen") + if page and abs(page["y"] + page["h"] - geo.h) > 1: + problems.append("the page ends at %d, not at the screen's bottom (%d)" % (page["y"] + page["h"], geo.h)) + else: + if not on_ws: + problems.append("no app on the app workspace") + if not strip: + problems.append("the shell's bottom strip is not shown over the app") + elif page and abs(page["y"] + page["h"] - strip["y"]) > 1: + problems.append("the page ends at %d, not above the strip (%d)" % (page["y"] + page["h"], strip["y"])) + if not st.get(OPEN_KEY[t["page"]]): + problems.append("%s is not open" % OPEN_KEY[t["page"]]) + if t["page"] == "sheet": + if st.get("media"): + problems.append("a media card is shown") + need = t.get("notes", 1) + if (st.get("notifications") or 0) < need: + problems.append("%s notifications in the shade, needs %d" % (st.get("notifications"), need)) + return problems + + +# Evidence is computed from the pixels, never judged: a region's row profile +# (the mean grey of each panel row) before and after a drag, and the upward +# shift that best lines the two up. A list that scrolled with the finger +# lines up again some way further up (scrolled_with); one that didn't move +# matches itself at a shift of 0. Grey values are 0..65535; STABLE is the +# mean per-row difference below which two frames count as the same picture. +STABLE = 130 + + +def profile(png, geo, rect): + """Mean grey of each panel row inside rect (logical pixels).""" + (x0, y0), (x1, y1) = geo.px(rect["x"], rect["y"]), geo.px(rect["x"] + rect["w"], rect["y"] + rect["h"]) + out = sh("magick", png, "-crop", "%dx%d+%d+%d" % (x1 - x0, y1 - y0, x0, y0), "+repage", + "-colorspace", "gray", "-depth", "16", "-scale", "1x!", "txt:-") + vals = [] + for line in out.splitlines()[1:]: + m = re.search(r"\(\s*([\d.]+)", line.split(":", 1)[-1]) + vals.append(float(m.group(1)) if m else 0.0) + return vals + + +def diff(a, b, s=0): + """Mean absolute difference of b[y] and a[y + s] over their overlap.""" + m = min(len(a) - s, len(b)) + if m < max(1, min(len(a), len(b)) // 3): + return float("inf") + return sum(abs(b[y] - a[y + s]) for y in range(m)) / m + + +TIE = 1.25 # shifts whose difference is within this factor of the best line up as well +# (look-alike rows on the 9a and in the VM: 1.05-1.21; any other second-best +# shift seen was more than 1.6 times the best) + + +def shift_up(a, b, max_shift, near=0): + """How far the content moved up from profile a to profile b, in panel + rows: (shift, its difference, the difference unmoved, other shifts that + line up as well). Rows that look alike (network names of one length, the + same notification) line up almost as well a whole number of rows apart; + of those, the shift nearest `near` (the drag) is taken.""" + curve = [diff(a, b, s) for s in range(max(1, max_shift))] + r0 = curve[0] + best = min(range(1, len(curve)), key=lambda s: curve[s], default=0) + if not best or curve[best] >= r0: + return 0, r0, r0, [] + found = [] + for s in range(1, len(curve)): + r = curve[s] + if r > TIE * curve[best] or r > curve[s - 1] or (s + 1 < len(curve) and r > curve[s + 1]): + continue + if found and s - found[-1] <= 3: # the same minimum, a flat bottom + if r < curve[found[-1]]: + found[-1] = s + else: + found.append(s) + pick = min(found, key=lambda s: (abs(s - near), s)) + return pick, curve[pick], r0, [s for s in found if s != pick] + + +def scrolled_with(moved, diff_at_shift, diff_unmoved): + """A drag that should scroll a list did: the picture changed, and its + content lines up much better moved up by at least 20 px than unmoved. How + far a list can go depends on how much of it is left below, so the rule + doesn't ask for a share of the drag; a touch that never arrived moves + nothing, and rows a Wi-Fi scan adds push the list down, not up.""" + return moved >= 20 and diff_unmoved > STABLE and diff_at_shift < 0.7 * diff_unmoved + + +def scroll_evidence(found, scale, drag): + """shift_up's answer (panel rows) as the log's evidence (logical px), and + whether it shows the content moving up with the drag. When look-alike + rows line up at several shifts, every one of them must pass: a list that + lines up as well after 7 px as after 263 px proves nothing.""" + s, rb, r0, alts = found + ev = {"dragged": round(drag), "moved": round(s / scale), "diff_at_shift": round(rb), + "diff_unmoved": round(r0)} + if alts: + ev["also_lines_up_at"] = sorted(round(x / scale) for x in alts) + return scrolled_with(min([s] + alts) / scale, rb, r0), ev + + +def gesture_result(t, got, lay): + """PASS when the shell's state is the one the scenario wants and Recents + stayed shut: no scenario here opens it, so Recents means the touch + reached the home strip, whatever became of the page.""" + recents = any(l["ns"] == RECENTS_NS for l in lay) + return ("PASS" if got == t["want"] and not recents else "FAIL"), recents + + +def notes_to_post(want, limit, existing): + """How many fictional notifications fit: the history keeps `limit` files, + and HEADROOM slots stay free for real ones arriving during the run.""" + return min(want, max(0, limit - HEADROOM - existing)) + + +def settle(geo, path, rect, timeout=12.0): + """Screenshot into path once rect stops changing (a Wi-Fi scan adds rows + for a few seconds after the page opens). False if it never did.""" + sh("grim", "-o", geo.name, path, check=True) + prev = profile(path, geo, rect) + t0 = time.time() + while time.time() - t0 < timeout: + time.sleep(1.0) + sh("grim", "-o", geo.name, path, check=True) + cur = profile(path, geo, rect) + if diff(prev, cur) < STABLE: + return True + prev = cur + return False + + +def evidence_rect(t, geo, page): + """Where a drag that keeps the page open must show movement: the Wi-Fi + list between the page's header and its strip, or the lower half of the + shade where the notification list is.""" + if t["page"] == "sheet": + return {"x": 0, "y": geo.h * 0.5, "w": geo.w, "h": geo.h * 0.5 - 10} + return {"x": page["x"] + 60, "y": page["y"] + 166, "w": page["w"] - 112, "h": page["h"] - 166 - 40} + + +def annotate(png, out, geo, pts): + (a, b), (c, d) = geo.px(*pts[0:2]), geo.px(*pts[2:4]) + r = max(12, round(8 * geo.scale)) + sh("magick", png, "-fill", "none", "-stroke", "#ff3b30", "-strokewidth", str(max(6, round(3 * geo.scale))), + "-draw", "line %d,%d %d,%d" % (a, b, c, d), "-fill", "#ff3b30", + "-draw", "circle %d,%d %d,%d" % (a, b, a + r, b), out) + + +def hottest(): + """The hottest thermal zone in deg C, batteries left out (None if none).""" + temps = [] + for z in glob.glob("/sys/class/thermal/thermal_zone*"): + try: + with open(z + "/type") as f: + if "batt" in f.read().lower(): + continue + with open(z + "/temp") as f: + temps.append(int(f.read()) / 1000) + except (OSError, ValueError): + pass + return max(temps) if temps else None + + +def cool_down(timeout=120.0): + """Wait until the SoC is below THERMAL_LIMIT: a throttled phone delivers + touches late or in one burst, and a drag then looks like a jump. Returns + the temperature it ended at.""" + t0, temp = time.time(), hottest() + while temp is not None and temp > THERMAL_LIMIT and time.time() - t0 < timeout: + time.sleep(3.0) + temp = hottest() + return temp + + +def run_test(t, fx, finger, geo, out_dir, record): + d = os.path.join(out_dir, t["id"] + "-" + t["name"]) + os.makedirs(d, exist_ok=True) + ws = fx.home_ws if t["ctx"] == "home" else fx.app_ws + log = {k: t[k] for k in ("id", "name", "title", "expect", "ctx", "page", "gesture", "want")} + log["workspace"] = ws + log["temp_start"] = cool_down() + reset_ui() + focus(ws); time.sleep(0.8) + rec = None + if record: + rec = subprocess.Popen(["wf-recorder", "-o", geo.name, "-f", os.path.join(d, "raw.mp4"), "-c", "libx264", + "-p", "preset=ultrafast", "-p", "crf=20", "-r", "30", "-y"], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env=ENV) + time.sleep(1.0) + if t["page"] == "wifi": + if qs("openWifi").strip() == "unavailable": + raise RuntimeError("no Wi-Fi device") + elif t["page"] == "bluetooth": + if qs("openBluetooth").strip() == "unavailable": + raise RuntimeError("no Bluetooth adapter") + else: + shell("summon", QS, json.dumps({"expanded": False})) + time.sleep(SETTLE[t["page"]]) + # The app under the page is a terminal, and focusing it can bring the + # on-screen keyboard up; the gesture is about the screen without it. + shell("call", "omamobile.keyboard", "hideKeyboard", "") + time.sleep(0.5) + # Opening the page warms the phone up again: cool down once more before + # the screen is measured and touched (and before the screenshots, so rows + # a scan adds while waiting aren't taken for a scroll). + log["temp_ready"] = cool_down(timeout=60.0) + st, lay = state(), layers(geo) + page = next((l for l in lay if l["ns"] == PAGE_NS.get(t["page"])), None) + keep_open = all(v is True for v in t["want"].values()) + before = os.path.join(d, "before.png") + rect = None + if keep_open and (page or t["page"] == "sheet"): + rect = evidence_rect(t, geo, page) + log["settled"] = settle(geo, before, rect) + st, lay = state(), layers(geo) + page = next((l for l in lay if l["ns"] == PAGE_NS.get(t["page"])), None) + else: + sh("grim", "-o", geo.name, before, check=True) + log["state_before"], log["layers_before"] = st, lay + problems = precondition(t, st, lay, ws, geo) + if log.get("settled") is False: + problems.append("the screen kept changing before the gesture") + if log["temp_ready"] is not None and log["temp_ready"] > THERMAL_LIMIT: + problems.append("the phone stayed at %.0f C, above %.0f C, where touches arrive late" + % (log["temp_ready"], THERMAL_LIMIT)) + # 03 and 04 are about the strip and the end of the list: they only mean + # something when the list reaches the strip. + if not problems and t["gesture"] in ("last-row", "body") and page: + try: + reach = page["y"] + page["h"] - last_row_y(before, page, geo) + log["list_reach"] = round(reach) + if reach > 110: + problems.append("the list ends %d px above the page's bottom, too short to reach the strip" + " (needs more networks in range)" % reach) + except RuntimeError as e: + problems.append(str(e)) + log["precondition"] = {"ok": not problems, "problems": problems} + if problems: + log["result"] = "SKIP" + else: + pts = gesture_points(t, geo, page, before) + annotate(before, os.path.join(d, "gesture.png"), geo, pts) + log["temp_gesture"] = hottest() + log["touch"] = finger.swipe(geo, *pts, label="gesture") + time.sleep(1.8) + log["state_after"] = state() + log["layers_after"] = layers(geo) + log["got"] = {k: log["state_after"].get(k) for k in t["want"]} + log["result"], log["recents"] = gesture_result(t, log["got"], log["layers_after"]) + if log["recents"]: + log["note"] = "Recents opened: the touch reached the home strip" + after = os.path.join(d, "after.png") + sh("grim", "-o", geo.name, after) + # A page that should stay open also stays open when the touch never + # arrived, so its pass needs evidence computed from the pixels: the + # content moved up with the drag (scrolled_with). Otherwise INCONCLUSIVE. + if log["result"] == "PASS" and keep_open: + drag = pts[1] - pts[3] + found = shift_up(profile(before, geo, rect), profile(after, geo, rect), round(400 * geo.scale), + near=round(drag * geo.scale)) + ok, log["evidence"] = scroll_evidence(found, geo.scale, drag) + if not ok: + ev = log["evidence"] + log["result"] = "INCONCLUSIVE" + log["note"] = ("the page stayed open, but its content did not move up with the drag" + " (dragged %d px, moved %d px%s)" % (ev["dragged"], ev["moved"], + "; rows that look alike line up as well at %s px" + % ", ".join(map(str, ev["also_lines_up_at"])) if "also_lines_up_at" in ev else "")) + if rec: + time.sleep(0.6) + rec.send_signal(signal.SIGINT) + try: + rec.wait(timeout=20) + except subprocess.TimeoutExpired: + rec.kill() + with open(os.path.join(d, "log.json"), "w") as f: + json.dump(log, f, indent=2) + return log + + +# ------------------------------------------------------------------ main + +def say(line): + """One line of progress. A terminal that went away (EIO, EPIPE) doesn't + stop the run: the results still go to results.json.""" + try: + print(line, flush=True) + except OSError: + sys.stdout = open(os.devnull, "w") # nor Python's own flush at exit + + +def end_on_signals(): + """SIGTERM and SIGHUP end a run the way Ctrl-C does, through the cleanup + in main's finally. SIGHUP is what a run gets when its SSH session or the + USB link drops; by default Python would exit on it without any cleanup.""" + def end(sig, frame): + raise SystemExit(128 + sig) + for sig in (signal.SIGTERM, signal.SIGHUP): + signal.signal(sig, end) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.split("\n")[0]) + ap.add_argument("ids", nargs="*", help="test ids to run (default: all)") + ap.add_argument("--record", action="store_true", help="a screen recording per test") + ap.add_argument("--keep-tiles", action="store_true", help="leave the owner's tile layout in place") + ap.add_argument("--loud", action="store_true", help="leave do-not-disturb off during the run") + ap.add_argument("--out", help="output directory (default ~/.cache/phone-gesture-test/run-TIME)") + ap.add_argument("--list", action="store_true", help="list the tests and exit") + ap.add_argument("--cleanup", action="store_true", + help="only put back what an earlier run that didn't clean up left, and exit") + a = ap.parse_args() + tests = [t for t in TESTS if not a.ids or t["id"] in a.ids] + if a.list: + for t in TESTS: + print("%s %-5s %s -> %s" % (t["id"], t["ctx"], t["title"], t["expect"])) + return 0 + need = CLEANUP_TOOLS if a.cleanup else needed_tools(tests, a.record, os.access("/dev/uinput", os.W_OK)) + missing = missing_tools(need, ENV.get("PATH")) + if missing: + sys.exit("phone-gesture-test: missing %s" % ", ".join(missing)) + rec = read_state() + if rec is not None: + recover(rec) + elif a.cleanup: + print("phone-gesture-test: no earlier run left anything to put back") + if a.cleanup: + return 0 + geo = Geometry() + if geo.transform != 0: + sys.exit("the display is rotated (transform %s): turn rotation back first" % geo.transform) + if screen_locked(): + sys.exit("the screen is locked") + out = a.out or os.path.expanduser(time.strftime("~/.cache/phone-gesture-test/run-%Y%m%d-%H%M%S")) + os.makedirs(out, exist_ok=True) + print("phone-gesture-test: %d tests on %s (%dx%d logical, scale %g). The screen is taken over until the end." + % (len(tests), geo.name, geo.w, geo.h, geo.scale), flush=True) + + end_on_signals() + build = shell_build() + fx = Fixtures(a.keep_tiles, not a.loud) + finger = None + results = [] + interrupted = None + try: + fx.tiles() + fx.workspaces() + fx.quiet() + finger = Finger(geo) + if any(t["page"] == "sheet" for t in tests): + fx.post_notifications(NOTIFICATIONS) + for t in tests: + if t["ctx"] == "app" and not fx.app: + fx.open_app() + try: + log = run_test(t, fx, finger, geo, out, a.record) + except Exception as e: # one broken test doesn't stop the others + log = {"id": t["id"], "name": t["name"], "result": "ERROR", "error": str(e)} + results.append(log) + why = {"SKIP": "; ".join(log.get("precondition", {}).get("problems", [])), + "ERROR": log.get("error", ""), "INCONCLUSIVE": log.get("note", ""), + "FAIL": log.get("note", "")}.get(log["result"], "") + say("%s %-38s %s%s" % (t["id"], t["name"], log["result"], " " + why if why else "")) + if fx.history_before is not None: + fx.forget_notifications() + except KeyboardInterrupt: + interrupted = "SIGINT" + except SystemExit as e: # end_on_signals + interrupted = signal.Signals(e.code - 128).name if isinstance(e.code, int) and e.code > 128 else str(e.code) + finally: + # The cleanup runs to its end: a second Ctrl-C, or the SIGHUP that + # follows a SIGTERM when the session goes, mustn't cut it short (sh() + # has a timeout, so it can't hang either). + for sig in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP): + signal.signal(sig, signal.SIG_IGN) + if finger: + finger.close() + fx.restore() + summary = { + "device": geo.info(), + "shell": build, + "time": time.strftime("%Y-%m-%dT%H:%M:%S%z"), + "tiles": "owner's" if a.keep_tiles else "package default", + "interrupted": interrupted, + "do_not_disturb": {"before": fx.dnd_before, "during": fx.dnd_during, "after": fx.dnd_after}, + "notifications": {"posted": fx.posted, "owner_history_kept": fx.history_kept}, + "cleanup": {"errors": fx.restore_errors, "touch_helper_left_running": bool(finger and finger.left_running)}, + "tests": [{k: r.get(k) for k in ("id", "name", "title", "expect", "result", "want", "got", "recents", + "error", "evidence", "note", "temp_start", "temp_ready", "temp_gesture")} + for r in results], + } + with open(os.path.join(out, "results.json"), "w") as f: + json.dump(summary, f, indent=2) + if fx.history_kept is False: + say("WARNING: a notification history file from before the run is gone") + for e in fx.restore_errors: + say("WARNING: not put back, %s (the next run tries again; or --cleanup)" % e) + passed = sum(r["result"] == "PASS" for r in results) + if interrupted: + say("stopped by %s after %d of %d tests, everything put back; details in %s" + % (interrupted, len(results), len(tests), out)) + return 130 + say("%d/%d passed; details in %s" % (passed, len(results), out)) + return 0 if passed == len(results) else 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/phone-gesture-touch b/tools/phone-gesture-touch new file mode 100755 index 000000000..d9c8fe770 --- /dev/null +++ b/tools/phone-gesture-touch @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +# A virtual multitouch screen over /dev/uinput, for gesture tests (needs root). +# +# sudo tools/phone-gesture-touch WIDTH HEIGHT +# +# WIDTH and HEIGHT are the panel's pixels (the touchscreen's range; Hyprland +# maps the range onto the output). One device serves the whole run, so the +# compositor adds it once: after "ready", it reads one swipe per line on +# stdin, +# {"x0": .., "y0": .., "x1": .., "y1": .., "ms": ..} +# and answers each with the same object plus the wall-clock "down" and "up" +# times, so a screen recording can be annotated with where and when the +# finger was. A tap is a swipe with x0 == x1 and y0 == y1 and a short ms. +import fcntl, json, os, struct, sys, time + +EV_SYN, EV_KEY, EV_ABS = 0, 1, 3 +SYN_REPORT = 0 +BTN_TOUCH = 0x14a +ABS_X, ABS_Y = 0, 1 +ABS_MT_SLOT, ABS_MT_POSITION_X, ABS_MT_POSITION_Y, ABS_MT_TRACKING_ID = 0x2f, 0x35, 0x36, 0x39 +INPUT_PROP_DIRECT = 1 +UI_SET_EVBIT, UI_SET_KEYBIT, UI_SET_ABSBIT, UI_SET_PROPBIT = 0x40045564, 0x40045565, 0x40045567, 0x4004556e +UI_DEV_CREATE, UI_DEV_DESTROY = 0x5501, 0x5502 + +W, H = int(sys.argv[1]), int(sys.argv[2]) + +fd = os.open("/dev/uinput", os.O_WRONLY | os.O_NONBLOCK) +for ev in (EV_KEY, EV_ABS): + fcntl.ioctl(fd, UI_SET_EVBIT, ev) +fcntl.ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH) +for a in (ABS_X, ABS_Y, ABS_MT_SLOT, ABS_MT_POSITION_X, ABS_MT_POSITION_Y, ABS_MT_TRACKING_ID): + fcntl.ioctl(fd, UI_SET_ABSBIT, a) +fcntl.ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT) +absmax = [0] * 64 +absmax[ABS_X] = absmax[ABS_MT_POSITION_X] = W - 1 +absmax[ABS_Y] = absmax[ABS_MT_POSITION_Y] = H - 1 +absmax[ABS_MT_SLOT] = 4 +absmax[ABS_MT_TRACKING_ID] = 65535 +name = b"gesture-test-touch".ljust(80, b"\0") +dev = (name + struct.pack("