diff --git a/Makefile b/Makefile index 364bd77c..63270a60 100644 --- a/Makefile +++ b/Makefile @@ -230,7 +230,8 @@ docs-check: || { python3 docs/checks/doc-links.py $$(git ls-files '*.md'); exit 1; } @python3 docs/quickstart/build.py --check >/dev/null \ || { python3 docs/quickstart/build.py --check; exit 1; } - @echo "docs: spelling, anchors and quick-start pages clean" + @sh docs/checks/lib-selftest.sh >/dev/null || { sh docs/checks/lib-selftest.sh; exit 1; } + @echo "docs: spelling, anchors, quick-start pages and the checks' prelude clean" ci: fmt-check vet lint test clients crosscompile java api-check flags-check license-check docs-check dotnet diff --git a/docs/checks/README.md b/docs/checks/README.md index 79d512aa..c6c78891 100644 --- a/docs/checks/README.md +++ b/docs/checks/README.md @@ -19,6 +19,38 @@ simulator UDID, `ios-webview.sh` defaults to the booted simulator, `test-runner.sh` and `test-ui.sh` run on `emulator-5554` and read `MOBIUM_IOS_DEVICE` for iOS, and `test-grid.sh` reads `MOBIUM_GRID`. +## Running several at once + +Every check sources [lib.sh](lib.sh), which keeps it from reaching into +another: + +- **A daemon of its own** (`MOBIUM_SESSION`), stopped when the check ends. + Checks once shared one: a check that ended on the `uiautomator` backend + handed it to every check after, and one run's `daemon stop` ended another + device's trace. A session the caller set, as a grid run's or + `mobium test`'s, is kept. +- **One check per device.** `check_lock` takes the device, and a second + check on it is refused, naming the first; a lock whose holder has gone is + taken over. A check that runs another shares its lock. +- **A folder of its own**, `CHECK_TMP`, removed at the end, and `at_exit` for + cleanup in place of `trap ... EXIT`, which would replace the prelude's. + +So checks on different devices can run side by side: + +```sh +./docs/checks/obstruction.sh emulator-5554 & +./docs/checks/obstruction.sh & +wait +``` + +What they share is the machine: an emulator under load stopped answering +`adb` once, mid-sweep, so start with as many devices as the Mac drives +comfortably. `make ci` runs [lib-selftest.sh](lib-selftest.sh), which tests +the prelude without a device and fails when a check stops using it. Four +checks do not: `clean-stop.sh`, which stops the caller's daemon, and +`test-runner.sh`, `test-grid.sh` and `test-ui.sh`, which test the sessions +`mobium test` and the grid give themselves. + | Script | What it proves | | --- | --- | | [calculator.sh](calculator.sh) | A full type-and-read loop: 1 + 1 = 2 in Google Calculator, every step asserted against a named element | diff --git a/docs/checks/accessibility.sh b/docs/checks/accessibility.sh index 3e74fcd6..a01837aa 100755 --- a/docs/checks/accessibility.sh +++ b/docs/checks/accessibility.sh @@ -18,6 +18,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-28s %-40s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp @@ -33,7 +35,7 @@ echo "--- $DEV ($PLATFORM)" if [ "$PLATFORM" = phone ]; then # Somebody's phone: every change is put back when the daemon stops, and # the trap stops it on failure too. - trap '$ROOT/bin/mobium daemon stop >/dev/null 2>&1 || true' EXIT + at_exit '$ROOT/bin/mobium daemon stop >/dev/null 2>&1 || true' SWITCHES="reduce_motion bold_text increase_contrast reduce_transparency button_shapes differentiate_without_color" before=$($M accessibility) || fail "reading the settings: $before" $M terminate $APP >/dev/null 2>&1 || true; $M launch $APP >/dev/null @@ -103,7 +105,7 @@ live() { $M daemon stop >/dev/null 2>&1 || true BEFORE=$(raw) LIVE_BEFORE=$(live) -trap '$M daemon stop >/dev/null 2>&1 || true' EXIT +at_exit '$M daemon stop >/dev/null 2>&1 || true' # What each platform has, and the value each is changed to. The last column # is the app's word for it in a11yState, or - where React Native tells the diff --git a/docs/checks/audit.sh b/docs/checks/audit.sh index 5fe585cd..ecef21b1 100755 --- a/docs/checks/audit.sh +++ b/docs/checks/audit.sh @@ -22,6 +22,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-10s %-58s ok\n' "$1" "$2"; } json() { python3 -c "import json,sys; d=json.load(sys.stdin); print($1)"; } diff --git a/docs/checks/autowait.sh b/docs/checks/autowait.sh index 14f70521..e0951883 100755 --- a/docs/checks/autowait.sh +++ b/docs/checks/autowait.sh @@ -23,6 +23,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-16s %-52s ok\n' "$1" "$2"; } ms() { python3 -c 'import time; print(int(time.time()*1000))'; } @@ -63,7 +65,7 @@ reduce() { # reduce on|off fi } cleanup() { reduce off >/dev/null 2>&1 || true; $M terminate "$APP" >/dev/null 2>&1 || true; } -trap cleanup EXIT +at_exit cleanup # A session first, so the setting's undo belongs to it. $M current >/dev/null diff --git a/docs/checks/back.sh b/docs/checks/back.sh index 84b4b330..cd2febde 100755 --- a/docs/checks/back.sh +++ b/docs/checks/back.sh @@ -26,6 +26,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" APP=dev.mobium.mobiumapp case "$DEV" in ????????-????????????????) PLATFORM=ios; PHONE=1 ;; diff --git a/docs/checks/battery.sh b/docs/checks/battery.sh index d194536a..139ad02b 100755 --- a/docs/checks/battery.sh +++ b/docs/checks/battery.sh @@ -15,8 +15,10 @@ # battery is put back — full, on AC — at the end. set -e ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi +check_lock "$DEV" case "$DEV" in *-*-*-*-*) PLATFORM=ios; M="$ROOT/bin/mobium --driver wda --device $DEV" ;; emulator-*) PLATFORM=android; M="$ROOT/bin/mobium --device $DEV" ;; @@ -56,7 +58,7 @@ fi ADB="adb -s $DEV" restore() { $ADB emu power ac on >/dev/null 2>&1; $ADB emu power status charging >/dev/null 2>&1 $ADB emu power capacity 100 >/dev/null 2>&1; } -trap restore EXIT +at_exit restore for want in 42 73; do $ADB emu power ac off >/dev/null diff --git a/docs/checks/biometric.sh b/docs/checks/biometric.sh index fdb7ad00..e2efff03 100755 --- a/docs/checks/biometric.sh +++ b/docs/checks/biometric.sh @@ -30,6 +30,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-14s %-60s ok\n' "$1" "$2"; } @@ -76,7 +78,7 @@ restore() { *) $M biometric enroll >/dev/null 2>&1 || true ;; esac } -trap restore EXIT +at_exit restore $M terminate $APP >/dev/null 2>&1 || true $M launch $APP >/dev/null @@ -178,7 +180,7 @@ else row "after" "the enrolled finger accepted once the lockout ended" fi -trap - EXIT +at_exit_clear restore case "$was" in "no "*) now=$($M biometric); case "$now" in "no "*) ;; *) fail "not put back: $now" ;; esac diff --git a/docs/checks/boot.sh b/docs/checks/boot.sh index 0dab4bc7..a3ac7544 100755 --- a/docs/checks/boot.sh +++ b/docs/checks/boot.sh @@ -20,6 +20,7 @@ set -e AVD="$1"; SIM="$2" if [ -z "$AVD" ]; then echo "usage: $0 [simulator-udid]" >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" M="$ROOT/bin/mobium" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-60s ok\n' "$1" "$2"; } diff --git a/docs/checks/chrome.sh b/docs/checks/chrome.sh index 9ffa8591..ef953f9a 100755 --- a/docs/checks/chrome.sh +++ b/docs/checks/chrome.sh @@ -30,6 +30,8 @@ case "$DEV" in *) PHONE=1 ;; esac ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --device $DEV" # closeTabs closes the pages in the phone's Chrome whose URL matches $1, a # Python regular expression, through Chrome's own DevTools endpoint: Mobium has diff --git a/docs/checks/clear-data.sh b/docs/checks/clear-data.sh index 59377472..3bea927a 100755 --- a/docs/checks/clear-data.sh +++ b/docs/checks/clear-data.sh @@ -22,6 +22,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } json() { python3 -c "import json,sys; d=json.load(sys.stdin); print($1)"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/clients.sh b/docs/checks/clients.sh index 7ee04150..3ed071b4 100755 --- a/docs/checks/clients.sh +++ b/docs/checks/clients.sh @@ -20,6 +20,8 @@ DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" cd "$ROOT" || exit 1 MOBIUM_E2E_DEVICE="$DEV"; MOBIUM_BIN_PATH="$ROOT/bin/mobium" export MOBIUM_E2E_DEVICE MOBIUM_BIN_PATH diff --git a/docs/checks/compose-app.sh b/docs/checks/compose-app.sh index 389e1e65..0c300030 100755 --- a/docs/checks/compose-app.sh +++ b/docs/checks/compose-app.sh @@ -26,7 +26,9 @@ # printed. set -e ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" DEV="${1:?usage: compose-app.sh [path-to-apk]}" +check_lock "$DEV" APK="$2" APP=com.junkfood.seal M="$ROOT/bin/mobium --device $DEV" @@ -45,7 +47,7 @@ cleanup() { $M uninstall $APP >/dev/null 2>&1 || true fi } -trap cleanup EXIT +at_exit cleanup "$ROOT/bin/mobium" daemon stop >/dev/null 2>&1 || true echo "--- $DEV" diff --git a/docs/checks/crashes.sh b/docs/checks/crashes.sh index d0958c13..0508d9b3 100755 --- a/docs/checks/crashes.sh +++ b/docs/checks/crashes.sh @@ -32,6 +32,8 @@ DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } json() { python3 -c "import json,sys; d=json.load(sys.stdin); print($1)"; } @@ -233,7 +235,7 @@ if [ "$PLATFORM" = android ] && [ "$APP" = dev.mobium.mobiumapp ]; then adb -s "$DEV" unroot >/dev/null 2>&1 as_user != || echo "WARNING: adb is still root on $DEV" >&2 set -e; } - trap thaw EXIT + at_exit thaw adb -s "$DEV" shell am force-stop "$APP" $M launch "$APP" >/dev/null # Frozen before its first screen is drawn, an app has no window to be @@ -254,7 +256,7 @@ if [ "$PLATFORM" = android ] && [ "$APP" = dev.mobium.mobiumapp ]; then $M crashes "$anr" | head -1 | grep -q "^ANR: Input dispatching timed out" || fail "ANR $anr does not lead with its reason" ref=$($M map | grep "Close app" | grep -oE '@e[0-9]+') $M tap "$ref" >/dev/null - thaw; trap - EXIT + thaw; at_exit_clear echo " anr a frozen app: its dialog, its log line, its report ok" else adb -s "$DEV" unroot >/dev/null 2>&1 || true diff --git a/docs/checks/device-paths.sh b/docs/checks/device-paths.sh index b47e8488..d4267cbd 100755 --- a/docs/checks/device-paths.sh +++ b/docs/checks/device-paths.sh @@ -23,6 +23,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" case "$DEV" in ????????-????????????????) PLATFORM=ios; PHONE=1 ;; *-*-*-*-*) PLATFORM=ios; PHONE="" ;; @@ -35,7 +37,7 @@ hash() { shasum -a 256 "$1" | cut -c1-16; } echo "--- $DEV ($PLATFORM)" W=$(mktemp -d) -trap 'rm -rf "$W"' EXIT +at_exit 'rm -rf "$W"' head -c 70000 /dev/urandom > "$W/bin.dat" mkdir -p "$W/tree/sub"; echo one > "$W/tree/a.txt"; echo two22 > "$W/tree/sub/b.txt" diff --git a/docs/checks/device-state.sh b/docs/checks/device-state.sh index 6931a24d..94945742 100755 --- a/docs/checks/device-state.sh +++ b/docs/checks/device-state.sh @@ -19,11 +19,13 @@ DEV="$1"; APP="${2:-org.wikipedia}" if [ -z "$DEV" ]; then echo "usage: $0 [package]" >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --device $DEV" fail() { echo "FAIL: $*" >&2; exit 1; } restore() { $M orientation auto >/dev/null 2>&1 || true $M locale "$APP" "" >/dev/null 2>&1 || true; } -trap restore EXIT +at_exit restore echo "--- $DEV" diff --git a/docs/checks/dialogs.sh b/docs/checks/dialogs.sh index 6536f8e0..4c616315 100755 --- a/docs/checks/dialogs.sh +++ b/docs/checks/dialogs.sh @@ -23,6 +23,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-18s %-50s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/external-driver.sh b/docs/checks/external-driver.sh index 18b8e54d..416217a7 100755 --- a/docs/checks/external-driver.sh +++ b/docs/checks/external-driver.sh @@ -15,6 +15,8 @@ DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 (see: mobium devices)" >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --device $DEV" MOBIUM_DRIVER_ADB="$ROOT/examples/drivers/mobium-driver-adb" export MOBIUM_DRIVER_ADB @@ -26,7 +28,7 @@ export MOBIUM_DRIVER_ADB "$ROOT/bin/mobium" daemon stop >/dev/null 2>&1 || true TMP="$(mktemp -d)" -trap 'rm -rf "$TMP"' EXIT +at_exit 'rm -rf "$TMP"' fail() { echo "FAIL: $*" >&2; exit 1; } echo "--- $DEV" diff --git a/docs/checks/feed.sh b/docs/checks/feed.sh index 4bd79d76..68dc5b99 100755 --- a/docs/checks/feed.sh +++ b/docs/checks/feed.sh @@ -28,6 +28,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-58s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/files.sh b/docs/checks/files.sh index a55be2f5..780e62cc 100755 --- a/docs/checks/files.sh +++ b/docs/checks/files.sh @@ -25,8 +25,10 @@ # mobium-report.txt is there already, and removes only the two files it made. set -e ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi +check_lock "$DEV" # A simulator is a UUID of five groups; a real iPhone's UDID is two, # 00008120-0001234567890ABC. Anything else is an Android serial. PHONE= @@ -74,7 +76,7 @@ cleanup() { rm -f "$docs/$UPLOAD" "$docs/mobium-report.txt" fi } -trap cleanup EXIT +at_exit cleanup "$ROOT/bin/mobium" daemon stop >/dev/null 2>&1 || true echo "--- $DEV ($PLATFORM)" @@ -91,7 +93,7 @@ echo "$apps" | grep -q "$APP" || fail "$APP is not installed" if [ "$PLATFORM" = android ]; then there=$(adb -s "$DEV" shell ls /sdcard/Download/ 2>/dev/null | tr -d '\r' | grep '^mobium-report' | tr '\n' ' ' | sed 's/ *$//') if [ -n "$there" ]; then - trap - EXIT + at_exit_clear fail "the Download folder holds $there already — the app would save under another number and this check would read the wrong one; remove them if they are an earlier run's" fi fi diff --git a/docs/checks/flutter.sh b/docs/checks/flutter.sh index 9b9411bc..9c763c83 100755 --- a/docs/checks/flutter.sh +++ b/docs/checks/flutter.sh @@ -25,6 +25,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" case "$DEV" in ????????-????????????????|*-*-*-*-*) PLATFORM=ios; APP=dev.mobium.mobiumFlutter M="$ROOT/bin/mobium --driver wda --device $DEV" ;; diff --git a/docs/checks/frames.sh b/docs/checks/frames.sh index c1ec3a0b..e6cd0f6c 100755 --- a/docs/checks/frames.sh +++ b/docs/checks/frames.sh @@ -21,6 +21,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-58s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/gestures.sh b/docs/checks/gestures.sh index c508e462..87b3e5eb 100755 --- a/docs/checks/gestures.sh +++ b/docs/checks/gestures.sh @@ -20,6 +20,8 @@ set -e DEV="${1:?usage: gestures.sh }" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" case "$DEV" in [0-9A-Fa-f]*-[0-9A-Fa-f]*-[0-9A-Fa-f]*-[0-9A-Fa-f]*-[0-9A-Fa-f]*) BACKEND="--driver wda"; PLATFORM=ios ;; diff --git a/docs/checks/graybox-edges.sh b/docs/checks/graybox-edges.sh index 89a7ba0f..3a3ff2ca 100755 --- a/docs/checks/graybox-edges.sh +++ b/docs/checks/graybox-edges.sh @@ -35,6 +35,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-8s %-62s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/graybox-hooks.sh b/docs/checks/graybox-hooks.sh index 082653fa..456dc50f 100755 --- a/docs/checks/graybox-hooks.sh +++ b/docs/checks/graybox-hooks.sh @@ -27,6 +27,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-11s %-58s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/graybox.sh b/docs/checks/graybox.sh index 98eeae4a..c4ab2e3b 100755 --- a/docs/checks/graybox.sh +++ b/docs/checks/graybox.sh @@ -25,6 +25,8 @@ DEV="$1" ROUNDS="${2:-10}" if [ -z "$DEV" ]; then echo "usage: $0 [rounds]" >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-58s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/hit-test.sh b/docs/checks/hit-test.sh index f6ea32ae..faeba0f9 100755 --- a/docs/checks/hit-test.sh +++ b/docs/checks/hit-test.sh @@ -26,6 +26,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-13s %-60s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp @@ -39,7 +41,7 @@ echo "--- $DEV ($KIND)" $M terminate "$APP" >/dev/null 2>&1 || true $M launch "$APP" >/dev/null -trap '$M terminate "$APP" >/dev/null 2>&1 || true' EXIT +at_exit '$M terminate "$APP" >/dev/null 2>&1 || true' $M scroll-to "testid=obstructionBtn" --direction down >/dev/null 2>&1 || true $M tap "testid=obstructionBtn" >/dev/null $M wait "testid=obstructionOutcome" >/dev/null diff --git a/docs/checks/icecubes-ios.sh b/docs/checks/icecubes-ios.sh index 84d479a0..5feb3328 100755 --- a/docs/checks/icecubes-ios.sh +++ b/docs/checks/icecubes-ios.sh @@ -44,6 +44,8 @@ set -e DEV="${1:?usage: icecubes-ios.sh }" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --driver wda --device $DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-58s ok\n' "$1" "$2"; } diff --git a/docs/checks/ios-device.sh b/docs/checks/ios-device.sh index 34f51b5b..89ae1977 100755 --- a/docs/checks/ios-device.sh +++ b/docs/checks/ios-device.sh @@ -26,6 +26,8 @@ DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 (see: mobium devices)" >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --driver wda --device $DEV" fail() { echo "FAIL: $*" >&2; exit 1; } now() { python3 -c 'import time; print(time.time())'; } diff --git a/docs/checks/ios-webview.sh b/docs/checks/ios-webview.sh index f76c9c79..d5d0ee70 100755 --- a/docs/checks/ios-webview.sh +++ b/docs/checks/ios-webview.sh @@ -16,10 +16,12 @@ set -e UDID="${1:-booted}" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" M="$ROOT/bin/mobium --driver wda" # Named, not left to selection: with a phone connected as well as the # simulator, "the iOS device" is two devices and every command is refused. [ "$UDID" = booted ] || M="$M --device $UDID" +[ "$UDID" = booted ] || check_lock "$UDID" fail() { echo "FAIL: $*" >&2; exit 1; } # appContexts lists the WebView contexts that belong to one app — WEBVIEW_, diff --git a/docs/checks/keyboard.sh b/docs/checks/keyboard.sh index 3761fad7..8aef0c04 100755 --- a/docs/checks/keyboard.sh +++ b/docs/checks/keyboard.sh @@ -25,6 +25,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } json() { python3 -c "import json,sys; d=json.load(sys.stdin); print($1)"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/kiwix-ios.sh b/docs/checks/kiwix-ios.sh index 6d95480d..e8da9bc6 100755 --- a/docs/checks/kiwix-ios.sh +++ b/docs/checks/kiwix-ios.sh @@ -46,6 +46,8 @@ set -e DEV="${1:?usage: kiwix-ios.sh }" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --driver wda --device $DEV" APP=self.Kiwix ZIM_NAME=wikipedia_en_ray-charles_mini_2026-08.zim diff --git a/docs/checks/lib-selftest.sh b/docs/checks/lib-selftest.sh new file mode 100755 index 00000000..6b3ef18e --- /dev/null +++ b/docs/checks/lib-selftest.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# The checks' prelude, lib.sh, tested without a device: handlers run newest +# first and before the daemon stops, a session of its own unless the caller +# gave one, its folder removed, the exit status kept, and one check per +# device — refused while the holder lives, taken from one that has gone, +# shared with a check it runs. Run by `make ci`. +# +# docs/checks/lib-selftest.sh +set -e +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +LIB="$ROOT/docs/checks/lib.sh" +H=$(mktemp -d); export MOBIUM_HOME=$H; unset MOBIUM_SESSION CHECK_LOCK_OWNER +pass() { echo "ok $1"; }; bad() { echo "FAIL $1"; exit 1; } +# 1. handlers run newest first, then the session is gone, tmp removed +out=$(sh -c '. "$1"; echo "$MOBIUM_SESSION" > "$2/sess"; echo "$CHECK_TMP" > "$2/tmp"; at_exit "echo first"; at_exit "echo second"' x "$LIB" "$H") +[ "$out" = "second +first" ] && pass "handlers newest first" || bad "handlers order: $out" +case "$(cat $H/sess)" in ck*) pass "own session ($(cat $H/sess))";; *) bad "no session";; esac +[ ! -d "$(cat $H/tmp)" ] && pass "CHECK_TMP removed" || bad "tmp left" +# 2. a caller's session is kept +s=$(MOBIUM_SESSION=given sh -c '. "$1"; echo $MOBIUM_SESSION' x "$LIB"); [ "$s" = given ] && pass "caller's session kept" || bad "session overridden: $s" +# 3. at_exit_clear drops the check's handlers +out=$(sh -c '. "$1"; at_exit "echo dropped"; at_exit_clear' x "$LIB"); [ -z "$out" ] && pass "at_exit_clear" || bad "clear: $out" +# 4. exit status kept +sh -c '. "$1"; at_exit "true"; exit 3' x "$LIB" || st=$?; [ "${st:-0}" = 3 ] && pass "exit status kept" || bad "status $st" +# 5. a second check on the same device is refused; a stale lock is taken; a nested check shares +sh -c '. "$1"; check_lock DEV1; sleep 3' x "$LIB" & holder=$!; sleep 1 +if sh -c '. "$1"; check_lock DEV1' x "$LIB" 2>"$H/err"; then bad "second check not refused"; else grep -q "in use by another check" "$H/err" && pass "second check refused"; fi +wait $holder +[ ! -d "$H/locks/DEV1" ] && pass "lock released at exit" || bad "lock left" +mkdir -p "$H/locks/DEV2"; echo 999999 > "$H/locks/DEV2/pid" +sh -c '. "$1"; check_lock DEV2' x "$LIB" && pass "stale lock taken" || bad "stale lock refused" +out=$(sh -c '. "$1"; check_lock DEV3; sh -c ". \"\$1\"; check_lock DEV3 && echo nested-ok" y "$1"' x "$LIB"); [ "$out" = nested-ok ] && pass "nested check shares the lock" || bad "nested: $out" +rm -rf "$H" +# 6. every check is on the prelude, but for these, and none sets its own EXIT +# trap, which would replace the prelude's. clean-stop.sh stops the +# caller's daemon, so it must not have one of its own; the test-runner, +# grid and test-ui checks test mobium test's own sessions, which it gives +# each project only when none is set; the rest run no device check. +exempt=" clean-stop.sh test-runner.sh test-grid.sh test-ui.sh lib.sh lib-selftest.sh calculator.sh clock-timer.sh ios-webview-probe.sh " +for f in "$ROOT"/docs/checks/*.sh; do + n=$(basename "$f") + case "$exempt" in *" $n "*) continue ;; esac + grep -q '^\. "\$ROOT/docs/checks/lib.sh"' "$f" || bad "$n does not source lib.sh" + if grep -qE '^[^#]*trap [^-].* EXIT' "$f"; then bad "$n sets its own EXIT trap — use at_exit"; fi +done +pass "every check is on the prelude" +echo "lib.sh: all ok" diff --git a/docs/checks/lib.sh b/docs/checks/lib.sh new file mode 100644 index 00000000..6b7fa5b9 --- /dev/null +++ b/docs/checks/lib.sh @@ -0,0 +1,106 @@ +# Sourced by every check, right after it sets ROOT: +# +# . "$ROOT/docs/checks/lib.sh" +# +# so that two checks cannot reach into each other. +# +# - A daemon of its own. Checks shared one daemon, and on one evening a +# check left it on the uiautomator backend, which every later check +# inherited, and a sweep's `daemon stop` killed a trace another device +# was recording. MOBIUM_SESSION names a daemon and its socket; a check +# gets one unless its caller chose one (a grid run, mobium test), and it +# is stopped when the check ends, since two daemons driving one device +# invalidate each other's session on it. +# - One check per device: check_lock "$DEV" once the device is known, and +# a second check on the same device is refused, naming the first. +# - CHECK_TMP, a folder of its own, removed at the end. +# - at_exit, in place of `trap ... EXIT`: a shell has one EXIT trap, and a +# check's own would replace this file's. Handlers run last-registered +# first, before the daemon stops, since they often still need it. +# at_exit_clear drops the check's handlers — where it once wrote +# `trap - EXIT` — and keeps this file's. +# +# POSIX sh, as every check is. + +if [ -z "${CHECK_LIB:-}" ]; then +CHECK_LIB=1 + +if [ -z "${MOBIUM_SESSION:-}" ]; then + # Short: the session names a socket path, which the OS caps at ~104 bytes. + MOBIUM_SESSION="ck$$" + CHECK_OWN_DAEMON=1 +fi +export MOBIUM_SESSION + +CHECK_TMP="$(mktemp -d "${TMPDIR:-/tmp}/mobium-check.XXXXXX")" +CHECK_LOCK="" +_check_handlers="" + +at_exit() { + # Newest first. + _check_handlers="$1 +$_check_handlers" +} + +at_exit_clear() { + _check_handlers="" +} + +_check_exit() { + _check_status=$? + trap - EXIT INT TERM + _check_rest="$_check_handlers" + _check_handlers="" + while [ -n "$_check_rest" ]; do + _check_cmd="${_check_rest%% +*}" + case "$_check_rest" in + *" +"*) _check_rest="${_check_rest#* +}" ;; + *) _check_rest="" ;; + esac + [ -n "$_check_cmd" ] && eval "$_check_cmd" || true + done + if [ -n "${CHECK_OWN_DAEMON:-}" ] && [ -n "${ROOT:-}" ] && [ -x "$ROOT/bin/mobium" ]; then + "$ROOT/bin/mobium" daemon stop >/dev/null 2>&1 || true + fi + [ -n "$CHECK_LOCK" ] && rm -rf "$CHECK_LOCK" + rm -rf "$CHECK_TMP" + exit "$_check_status" +} +trap _check_exit EXIT +trap 'exit 130' INT TERM + +# check_lock DEV takes the device for this check, or refuses: two checks on +# one device fight over its session. A lock whose holder has gone is taken. +# +# A check run by another — third-party-app.sh hands an iPhone to its iOS +# half — shares its caller's lock rather than being refused by it: the +# holder's pid is passed down as CHECK_LOCK_OWNER, and the lock stays the +# caller's to release. +check_lock() { + _check_dir="${MOBIUM_HOME:-$HOME/.mobium}/locks" + mkdir -p "$_check_dir" + _check_name=$(printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_') + _check_lockdir="$_check_dir/$_check_name" + if [ -n "${CHECK_LOCK_OWNER:-}" ] && [ "$(cat "$_check_lockdir/pid" 2>/dev/null)" = "$CHECK_LOCK_OWNER" ]; then + return 0 + fi + if ! mkdir "$_check_lockdir" 2>/dev/null; then + _check_pid=$(cat "$_check_lockdir/pid" 2>/dev/null || true) + if [ -n "$_check_pid" ] && kill -0 "$_check_pid" 2>/dev/null; then + echo "FAIL: $1 is in use by another check ($(cat "$_check_lockdir/check" 2>/dev/null), pid $_check_pid) — run one check per device at a time" >&2 + exit 2 + fi + rm -rf "$_check_lockdir" + mkdir "$_check_lockdir" || { echo "FAIL: could not lock $1" >&2; exit 2; } + fi + echo $$ > "$_check_lockdir/pid" + basename "$0" > "$_check_lockdir/check" + CHECK_LOCK="$_check_lockdir" + CHECK_LOCK_OWNER=$$ + export CHECK_LOCK_OWNER +} + +fi diff --git a/docs/checks/locale-ios.sh b/docs/checks/locale-ios.sh index 65411578..46929a48 100755 --- a/docs/checks/locale-ios.sh +++ b/docs/checks/locale-ios.sh @@ -16,12 +16,14 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-54s ok\n' "$1" "$2"; } M="$ROOT/bin/mobium --device $DEV" P=com.apple.Preferences general() { $M map 2>/dev/null | grep -o -E '\b(General|一般)\b' | head -1; } -trap '$M locale $P "" >/dev/null 2>&1 || true' EXIT +at_exit '$M locale $P "" >/dev/null 2>&1 || true' echo "--- $DEV" $M locale $P | grep -q 'follows the device' || fail "Settings started out pinned" diff --git a/docs/checks/login.sh b/docs/checks/login.sh index 7d46aa35..d9ac31b2 100755 --- a/docs/checks/login.sh +++ b/docs/checks/login.sh @@ -18,6 +18,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/map-diff.sh b/docs/checks/map-diff.sh index f9cc5796..3e6c8b7c 100755 --- a/docs/checks/map-diff.sh +++ b/docs/checks/map-diff.sh @@ -14,7 +14,9 @@ # Leaves the Form Demo's box as it found it. Emulators and simulators. set -e ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" DEV="${1:?usage: map-diff.sh }" +check_lock "$DEV" case "$DEV" in *-*-*-*-*) M="$ROOT/bin/mobium --driver wda --device $DEV" ;; *) M="$ROOT/bin/mobium --device $DEV" ;; diff --git a/docs/checks/mobium-app.sh b/docs/checks/mobium-app.sh index 34ec1296..5a229511 100755 --- a/docs/checks/mobium-app.sh +++ b/docs/checks/mobium-app.sh @@ -50,6 +50,8 @@ set -e DEV="${1:?usage: mobium-app.sh }" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" # An iOS simulator is named by a UUID; an Android device never is. iOS is not # inferred from the serial anywhere in mobium, so the backend is named here. # A real iPhone's UDID is two groups, 00008120-0001234567890ABC, so it needs a diff --git a/docs/checks/netnewswire-ios.sh b/docs/checks/netnewswire-ios.sh index 306f2286..718bdea1 100755 --- a/docs/checks/netnewswire-ios.sh +++ b/docs/checks/netnewswire-ios.sh @@ -39,6 +39,8 @@ set -e DEV="${1:?usage: netnewswire-ios.sh }" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --driver wda --device $DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-58s ok\n' "$1" "$2"; } diff --git a/docs/checks/network.sh b/docs/checks/network.sh index 04a2eb2f..cb875116 100755 --- a/docs/checks/network.sh +++ b/docs/checks/network.sh @@ -24,6 +24,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --device $DEV" A="adb -s $DEV" fail() { echo "FAIL: $*" >&2; exit 1; } @@ -40,7 +42,7 @@ cleanup() { for p in $PIDS; do kill "$p" 2>/dev/null || true; wait "$p" 2>/dev/null || true; done rm -rf "$TMP" } -trap cleanup EXIT +at_exit cleanup # connects: can the device open a connection to the internet at all. connects() { $A shell 'nc -z -w 5 connectivitycheck.gstatic.com 80 >/dev/null 2>&1 && echo yes || echo no' | tr -d '\r'; } diff --git a/docs/checks/notifications-ios.sh b/docs/checks/notifications-ios.sh index 554dce5c..f90d6e2c 100755 --- a/docs/checks/notifications-ios.sh +++ b/docs/checks/notifications-ios.sh @@ -23,6 +23,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-10s %-58s ok\n' "$1" "$2"; } M="$ROOT/bin/mobium --device $DEV" diff --git a/docs/checks/obstruction.sh b/docs/checks/obstruction.sh index 83852e84..ac31f84e 100755 --- a/docs/checks/obstruction.sh +++ b/docs/checks/obstruction.sh @@ -23,6 +23,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-14s %-54s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp @@ -36,7 +38,7 @@ echo "--- $DEV ($PLATFORM)" # answered "not installed" here while the real error was the device. APPS=$($M apps 2>&1) || fail "could not list the apps: $APPS" echo "$APPS" | grep -q "$APP" || fail "$APP is not installed — build it first (see mobium-app.sh's header)" -trap '$M terminate "$APP" >/dev/null 2>&1 || true' EXIT +at_exit '$M terminate "$APP" >/dev/null 2>&1 || true' $M terminate "$APP" >/dev/null 2>&1 || true $M launch "$APP" >/dev/null diff --git a/docs/checks/orientation.sh b/docs/checks/orientation.sh index 39f7129d..45f01b7d 100755 --- a/docs/checks/orientation.sh +++ b/docs/checks/orientation.sh @@ -16,6 +16,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-18s %-50s ok\n' "$1" "$2"; } M="$ROOT/bin/mobium --device $DEV" diff --git a/docs/checks/otp.sh b/docs/checks/otp.sh index 74b94ab5..44bbd6b4 100755 --- a/docs/checks/otp.sh +++ b/docs/checks/otp.sh @@ -33,6 +33,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-16s %-56s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/pocketcasts-ios.sh b/docs/checks/pocketcasts-ios.sh index 555684f0..c58995e3 100755 --- a/docs/checks/pocketcasts-ios.sh +++ b/docs/checks/pocketcasts-ios.sh @@ -41,6 +41,8 @@ set -e DEV="${1:?usage: pocketcasts-ios.sh }" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --driver wda --device $DEV" APP=au.com.shiftyjelly.podcasts fail() { echo "FAIL: $*" >&2; exit 1; } diff --git a/docs/checks/pwa.sh b/docs/checks/pwa.sh index 99e8c938..4876eae1 100755 --- a/docs/checks/pwa.sh +++ b/docs/checks/pwa.sh @@ -41,6 +41,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" case "$DEV" in emulator-*) PLATFORM=android; M="$ROOT/bin/mobium --device $DEV" ;; ????????-????-????-????-????????????) diff --git a/docs/checks/record.sh b/docs/checks/record.sh index bca72fcb..71bb0bf7 100755 --- a/docs/checks/record.sh +++ b/docs/checks/record.sh @@ -17,10 +17,12 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } json() { python3 -c "import json,sys; d=json.load(sys.stdin); print($1)"; } TMP="$(mktemp -d)" -trap 'rm -rf "$TMP"' EXIT +at_exit 'rm -rf "$TMP"' case "$DEV" in ????????-????????????????) PLATFORM=iphone; M="$ROOT/bin/mobium --driver wda --device $DEV" ;; diff --git a/docs/checks/screen-reader.sh b/docs/checks/screen-reader.sh index abc92fbc..25ff08e9 100755 --- a/docs/checks/screen-reader.sh +++ b/docs/checks/screen-reader.sh @@ -25,6 +25,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-14s %-58s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp @@ -61,7 +63,7 @@ restore() { if [ "$was_on" = null ]; then a settings delete secure accessibility_enabled >/dev/null else a "settings put secure accessibility_enabled '$was_on'"; fi } -trap restore EXIT INT TERM +at_exit restore $M daemon stop >/dev/null 2>&1 || true if [ "$was_svc" = null ] || [ -z "$was_svc" ]; then on="$TB"; else on="$was_svc:$TB"; fi @@ -102,7 +104,7 @@ row "after" "every service still bound, touch exploration on" $M terminate "$APP" >/dev/null 2>&1 || true restore -trap - EXIT INT TERM +at_exit_clear [ "$(a settings get secure enabled_accessibility_services)" = "$was_svc" ] || fail "enabled services not put back: $(a settings get secure enabled_accessibility_services)" [ "$(a settings get secure accessibility_enabled)" = "$was_on" ] || fail "accessibility_enabled not put back" row "restored" "the device's accessibility settings as found" diff --git a/docs/checks/shake.sh b/docs/checks/shake.sh index f7517fbd..cf2f25cf 100755 --- a/docs/checks/shake.sh +++ b/docs/checks/shake.sh @@ -24,6 +24,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-14s %-56s ok\n' "$1" "$2"; } diff --git a/docs/checks/slider.sh b/docs/checks/slider.sh index 532f1841..4b59bee1 100755 --- a/docs/checks/slider.sh +++ b/docs/checks/slider.sh @@ -27,6 +27,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-58s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/source.sh b/docs/checks/source.sh index b8f5d1a9..128e7a41 100755 --- a/docs/checks/source.sh +++ b/docs/checks/source.sh @@ -16,11 +16,13 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } APP=dev.mobium.mobiumapp SECRET='Hunter2x9' TMP="$(mktemp -d)" -trap 'rm -rf "$TMP"' EXIT +at_exit 'rm -rf "$TMP"' case "$DEV" in ????????-????????????????) PLATFORM=iphone; UNITS=pt; M="$ROOT/bin/mobium --driver wda --device $DEV" ;; diff --git a/docs/checks/third-party-app-ios.sh b/docs/checks/third-party-app-ios.sh index 864827ad..4ec6e178 100755 --- a/docs/checks/third-party-app-ios.sh +++ b/docs/checks/third-party-app-ios.sh @@ -30,6 +30,8 @@ set -e DEV="${1:?usage: third-party-app-ios.sh }" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --driver wda --device $DEV" APP=org.wikimedia.wikipedia # A simulator is refused by name: an App Store app does not run on one, and diff --git a/docs/checks/third-party-app.sh b/docs/checks/third-party-app.sh index 7bcfabf1..ee8ad358 100755 --- a/docs/checks/third-party-app.sh +++ b/docs/checks/third-party-app.sh @@ -32,6 +32,8 @@ DEV="$1"; APK="$2" if [ -z "$DEV" ]; then echo "usage: $0 [apk]" >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --device $DEV" APP=org.wikipedia fail() { echo "FAIL: $*" >&2; exit 1; } diff --git a/docs/checks/timezone-ios.sh b/docs/checks/timezone-ios.sh index 5434fe64..be22c914 100755 --- a/docs/checks/timezone-ios.sh +++ b/docs/checks/timezone-ios.sh @@ -20,6 +20,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-10s %-58s ok\n' "$1" "$2"; } M="$ROOT/bin/mobium --device $DEV" @@ -46,7 +48,7 @@ shows() { echo "--- $DEV" device=$($M timezone) -trap '$M timezone "$device" >/dev/null 2>&1 || true' EXIT +at_exit '$M timezone "$device" >/dev/null 2>&1 || true' $M terminate $C >/dev/null 2>&1 || true $M launch $C >/dev/null # Calendar opens in the view it was last left in. Only the day view has diff --git a/docs/checks/trace.sh b/docs/checks/trace.sh index 9429bb73..6b8a9a32 100755 --- a/docs/checks/trace.sh +++ b/docs/checks/trace.sh @@ -22,7 +22,9 @@ # Emulators and simulators. set -e ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" DEV="${1:?usage: trace.sh }" +check_lock "$DEV" case "$DEV" in *-*-*-*-*) M="$ROOT/bin/mobium --driver wda --device $DEV" ;; *) M="$ROOT/bin/mobium --device $DEV" ;; @@ -31,7 +33,7 @@ APP=dev.mobium.mobiumapp fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-14s %-58s ok\n' "$1" "$2"; } OUT=$(mktemp -d) -trap 'rm -rf "$OUT"; $M trace stop -o "$OUT/left.zip" >/dev/null 2>&1 || true' EXIT +at_exit 'rm -rf "$OUT"; $M trace stop -o "$OUT/left.zip" >/dev/null 2>&1 || true' SECRET="Tr4ce-$(od -An -N3 -tx1 /dev/urandom | tr -d ' ')" "$ROOT/bin/mobium" daemon stop >/dev/null 2>&1 || true diff --git a/docs/checks/twa.sh b/docs/checks/twa.sh index 45a5e356..8201c3f0 100755 --- a/docs/checks/twa.sh +++ b/docs/checks/twa.sh @@ -26,6 +26,8 @@ set -e DEV="$1"; PKG="${2:-com.oyo.consumerlite}" if [ -z "$DEV" ]; then echo "usage: $0 [package]" >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" M="$ROOT/bin/mobium --device $DEV" fail() { $M context NATIVE_APP >/dev/null 2>&1 || true; echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-12s %-60s ok\n' "$1" "$2"; } diff --git a/docs/checks/wait-states.sh b/docs/checks/wait-states.sh index 93a44544..d39d0f64 100755 --- a/docs/checks/wait-states.sh +++ b/docs/checks/wait-states.sh @@ -22,6 +22,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-16s %-52s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp diff --git a/docs/checks/web-actionability.sh b/docs/checks/web-actionability.sh index bc46ca83..6e427993 100755 --- a/docs/checks/web-actionability.sh +++ b/docs/checks/web-actionability.sh @@ -19,6 +19,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-24s %-50s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp @@ -32,7 +34,7 @@ echo "--- $DEV ($PLATFORM)" # answered "not installed" here while the real error was the device. APPS=$($M apps 2>&1) || fail "could not list the apps: $APPS" echo "$APPS" | grep -q "$APP" || fail "$APP is not installed — build it first (see mobium-app.sh's header)" -trap '$M context NATIVE_APP >/dev/null 2>&1; $M terminate "$APP" >/dev/null 2>&1 || true' EXIT +at_exit '$M context NATIVE_APP >/dev/null 2>&1; $M terminate "$APP" >/dev/null 2>&1 || true' $M terminate "$APP" >/dev/null 2>&1 || true $M launch "$APP" >/dev/null diff --git a/docs/checks/web-storage.sh b/docs/checks/web-storage.sh index d3354ace..30c6aa32 100755 --- a/docs/checks/web-storage.sh +++ b/docs/checks/web-storage.sh @@ -23,8 +23,10 @@ # simulators; it leaves the page's storage cleared. set -e ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi +check_lock "$DEV" case "$DEV" in *-*-*-*-*) PLATFORM=ios; M="$ROOT/bin/mobium --driver wda --device $DEV" ;; *) PLATFORM=android; M="$ROOT/bin/mobium --device $DEV" ;; @@ -33,7 +35,7 @@ APP=dev.mobium.mobiumapp fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-14s %-58s ok\n' "$1" "$2"; } OUT=$(mktemp -d) -trap 'rm -rf "$OUT"' EXIT +at_exit 'rm -rf "$OUT"' appContexts() { $M contexts | awk -v id="WEBVIEW_$1" \ '$1 == id || (index($1, id "_") == 1 && substr($1, length(id) + 2) ~ /^[0-9]+$/) { print $1 }'; } diff --git a/docs/checks/web-type.sh b/docs/checks/web-type.sh index 9f656b69..5e3b989a 100755 --- a/docs/checks/web-type.sh +++ b/docs/checks/web-type.sh @@ -17,6 +17,8 @@ set -e DEV="$1" if [ -z "$DEV" ]; then echo "usage: $0 " >&2; exit 2; fi ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" fail() { echo "FAIL: $*" >&2; exit 1; } row() { printf ' %-22s %-52s ok\n' "$1" "$2"; } APP=dev.mobium.mobiumapp @@ -28,7 +30,7 @@ esac echo "--- $DEV ($PLATFORM)" APPS=$($M apps 2>&1) || fail "could not list the apps: $APPS" echo "$APPS" | grep -q "$APP" || fail "$APP is not installed — build it first (see mobium-app.sh's header)" -trap '$M context NATIVE_APP >/dev/null 2>&1; $M terminate "$APP" >/dev/null 2>&1 || true' EXIT +at_exit '$M context NATIVE_APP >/dev/null 2>&1; $M terminate "$APP" >/dev/null 2>&1 || true' $M terminate "$APP" >/dev/null 2>&1 || true $M launch "$APP" >/dev/null diff --git a/docs/checks/zoom.sh b/docs/checks/zoom.sh index 3bd0419f..49b92fca 100755 --- a/docs/checks/zoom.sh +++ b/docs/checks/zoom.sh @@ -22,6 +22,8 @@ set -e DEV="${1:?usage: zoom.sh }" ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +. "$ROOT/docs/checks/lib.sh" +check_lock "$DEV" case "$DEV" in [0-9A-Fa-f]*-[0-9A-Fa-f]*-[0-9A-Fa-f]*-[0-9A-Fa-f]*-[0-9A-Fa-f]*) BACKEND="--driver wda"; PLATFORM=ios ;;