aboutsummaryrefslogtreecommitdiff
path: root/test-rig/screenshots.sh
blob: 5da63499c27fa8a45d781be1d72b7ced202692c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
# Regenerate the F-Droid listing screenshots.
#
# Boots a pixel_6 AVD on the same AOSP API-35 image the e2e tier uses -
# natively 1080x2400, the geometry the tracked PNGs already have - and
# screencaps Main and Settings into
# fastlane/metadata/android/en-US/images/phoneScreenshots/.
#
# The AVD is its own (scrcpy-shots, not scrcpy-test) so a screenshot run
# never perturbs the device state ./test e2e asserts against. Panel size
# comes from the device profile rather than `wm size`: overriding
# geometry on a booted software-GPU emulator ANRs SystemUI, and the ANR
# dialog lands in the capture.
#
# Runs inside the test image; orchestrated by ../test.

set -eu

ROOT=/work
# Debug applicationId (note the .debug suffix). Activity classes live in
# the invalid.lena.scrcpy namespace, which carries no suffix.
PKG=invalid.lena.scrcpy.debug
AVD=scrcpy-shots
SERIAL=emulator-5554
APK=$ROOT/app/build/outputs/apk/debug/app-debug.apk
SHOTS=$ROOT/fastlane/metadata/android/en-US/images/phoneScreenshots
TMPDIR=$ROOT/.tools/shots
WIDTH=1080
HEIGHT=2400
mkdir -p "$TMPDIR" "$SHOTS"

log() { printf 'screenshots: %s\n' "$*" >&2; }

# ---- 0. server jar ----
[ -f "$ROOT/app/src/main/assets/scrcpy-server.jar" ] || "$ROOT/scripts/update-server"

# ---- 1. build APK ----
log 'gradle :app:assembleDebug'
"$ROOT/gradlew" --no-daemon -q :app:assembleDebug

# ---- 2. AVD + boot ----
if ! avdmanager list avd | grep -q "Name: $AVD$"; then
    log "creating avd $AVD (pixel_6, ${WIDTH}x${HEIGHT})"
    echo no | avdmanager create avd -n "$AVD" -k 'system-images;android-35;default;x86_64' -d pixel_6 >/dev/null
fi

# Clean stale lock files from a previously aborted run; snapshot.lock.lock
# and read-snapshot.txt in particular make the next boot die with
# "a snapshot operation is pending and timeout has expired".
AVD_DIR="$ANDROID_AVD_HOME/$AVD.avd"
rm -f "$AVD_DIR"/multiinstance.lock "$AVD_DIR"/hardware-qemu.ini.lock \
      "$AVD_DIR"/snapshot.lock.lock "$AVD_DIR"/read-snapshot.txt 2>/dev/null || true
rm -rf "$HOME/.android/avd/running" 2>/dev/null || true

if ! pgrep -f "emulator.*-avd $AVD" >/dev/null; then
    log 'booting emulator (no window, no snapshot)'
    emulator -avd "$AVD" -no-window -no-audio -no-snapshot -gpu swiftshader_indirect \
             -no-boot-anim -accel on >"$TMPDIR/emulator.log" 2>&1 &
fi
adb -s "$SERIAL" wait-for-device
until [ "$(adb -s "$SERIAL" shell getprop sys.boot_completed | tr -d '\r')" = 1 ]; do
    sleep 2
done

trap 'adb -s "$SERIAL" emu kill >/dev/null 2>&1 || true' EXIT INT TERM

# ---- 3. install ----
log "install $APK"
adb -s "$SERIAL" install -r "$APK" >/dev/null
# Screenshot 1 is the empty-list state; a re-run must not inherit the
# devices.json a previous run left behind.
adb -s "$SERIAL" shell pm clear "$PKG" >/dev/null

# ---- 4. capture ----
# One activity per shot: start it, let the window settle, screencap.
# SettingsActivity is not exported, so this needs adbd running as root.
log 'adb root'
adb -s "$SERIAL" root
adb -s "$SERIAL" wait-for-device

shot() {
    activity=$1
    out=$2
    log "capture $activity -> $out"
    adb -s "$SERIAL" shell am force-stop "$PKG"
    adb -s "$SERIAL" shell am start -W -n "$PKG/invalid.lena.scrcpy.$activity" >/dev/null
    sleep 4
    # Anything else on top - a system dialog, an ANR, a failed launch -
    # means the shot is not the screen we asked for. Fail, do not ship it.
    focus=$(adb -s "$SERIAL" shell dumpsys window \
            | sed -n 's/.*mCurrentFocus=[^ ]* [^ ]* \([^ }]*\).*/\1/p' | head -1)
    case "$focus" in
        "$PKG/"*) ;;
        *) log "focused window is '$focus', want $PKG/*"; exit 1 ;;
    esac
    adb -s "$SERIAL" exec-out screencap -p > "$SHOTS/$out"
    [ -s "$SHOTS/$out" ] || { log "$out is empty"; exit 1; }
    # Reject anything that is not the panel we expect: a wrong-sized PNG
    # means the device profile changed and the shot is unusable.
    got=$(ffprobe -v error -select_streams v:0 \
                  -show_entries stream=width,height -of csv=p=0:s=x "$SHOTS/$out")
    [ "$got" = "${WIDTH}x${HEIGHT}" ] || { log "$out is $got, want ${WIDTH}x${HEIGHT}"; exit 1; }
}

shot Main             1.png
shot SettingsActivity 2.png

log "wrote $SHOTS/1.png $SHOTS/2.png"
log 'done'