1#! /bin/sh
2# Copyright 2018 Google LLC.
3# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4
5# If you have more than one device attached, run `adb devices -l` and then set
6# the ANDROID_SERIAL environment variable to the correct serial number.
7
8APK="$1"
9shift
10
11if ! [ -f "$APK" ]; then
12    cat >&2 <<- EOM
13
14	Usage:
15	  $0 SKQP_APK_FILE_PATH [OPTIONAL_TESTS_TO_RUN...]
16
17	e.g.:
18	  $0 skqp-universal-debug.apk
19	or:
20	  $0 skqp-universal-debug.apk vk_hairmodes gles_gammatext gles_aarectmodes
21
22	EOM
23    exit 1
24fi
25
26ARGS=''
27if [ "$#" -gt 0 ]; then
28    ARGS="-e class org.skia.skqp.SkQPRunner#${1}"
29    shift
30    for arg; do
31        ARGS="${ARGS},org.skia.skqp.SkQPRunner#${arg}"
32    done
33fi
34
35TDIR="$(mktemp -d "${TMPDIR:-/tmp}/skqp_report.XXXXXXXXXX")"
36
37adb install -r "$APK" || exit 2
38adb logcat -c
39
40adb logcat TestRunner org.skia.skqp skia DEBUG "*:S" | tee "${TDIR}/logcat.txt" &
41LOGCAT_PID=$!
42
43ADBSHELL_PID=''
44trap 'kill $LOGCAT_PID; kill $ADBSHELL_PID' INT
45
46printf '\n%s\n\n' "adb shell am instrument $ARGS -w org.skia.skqp"
47adb shell am instrument $ARGS -w org.skia.skqp \
48    >  "${TDIR}/stdout.txt" \
49    2> "${TDIR}/stderr.txt" &
50ADBSHELL_PID=$!
51
52wait $ADBSHELL_PID
53trap - INT
54kill $LOGCAT_PID
55
56printf '\nTEST OUTPUT IS IN: "%s"\n\n' "$TDIR"
57
58SED_CMD='s/^.* org.skia.skqp: output written to "\([^"]*\)".*$/\1/p'
59ODIR="$(sed -n "$SED_CMD" "${TDIR}/logcat.txt" | head -1)"
60
61if ! adb shell "test -d '$ODIR'" ; then
62    echo 'missing output :('
63    exit 3
64fi
65
66odir_basename="$(basename "$ODIR")"
67
68adb pull "${ODIR}" "${TDIR}/${odir_basename}"
69
70REPORT="${TDIR}/${odir_basename}/report.html"
71
72if [ -f "$REPORT" ]; then
73    grep 'f(.*;' "$REPORT"
74    echo "$REPORT"
75    "$(dirname "$0")"/../../bin/sysopen "$REPORT"
76else
77    echo "$TDIR"
78fi
79