1#!/usr/bin/env bash
2
3# This script was copied from vendor/google/apps/SetupWizard/tools/coverage.sh
4
5##### App specific parameters #####
6
7PACKAGE_NAME='com.android.managedprovisioning'
8MODULE_NAME='ManagedProvisioning'
9MODULE_PATH='packages/apps/ManagedProvisioning'
10MODULE_INSTALL_PATH='system/priv-app/ManagedProvisioning/ManagedProvisioning.apk'
11
12TEST_MODULE_PATH='packages/apps/ManagedProvisioning/tests/instrumentation'
13TEST_MODULE_INSTALL_PATH='data/app/ManagedProvisioningTests/ManagedProvisioningTests.apk'
14TEST_RUNNER='com.android.managedprovisioning.tests/com.android.managedprovisioning.TestInstrumentationRunner'
15
16##### End app specific parameters #####
17
18if [[ $# != 0 && ! ($# == 1 && ($1 == "HTML" || $1 == "XML" || $1 == "CSV")) ]]; then
19  echo "$0: usage: coverage.sh [REPORT_TYPE]"
20  echo "REPORT_TYPE [HTML | XML | CSV] : the type of the report (default is HTML)"
21  exit 1
22fi
23
24REPORT_TYPE=${1:-HTML}
25
26if [ -z $ANDROID_BUILD_TOP ]; then
27  echo "You need to source and lunch before you can use this script"
28  exit 1
29fi
30
31REPORTER_JAR="$ANDROID_BUILD_TOP/prebuilts/sdk/tools/jack-jacoco-reporter.jar"
32
33OUTPUT_DIR="$ANDROID_BUILD_TOP/out/coverage/$MODULE_NAME"
34
35echo "Running tests and generating coverage report"
36echo "Output dir: $OUTPUT_DIR"
37echo "Report type: $REPORT_TYPE"
38
39REMOTE_COVERAGE_OUTPUT_FILE="/data/data/$PACKAGE_NAME/files/coverage.ec"
40COVERAGE_OUTPUT_FILE="$ANDROID_BUILD_TOP/out/$PACKAGE_NAME.ec"
41OUT_COMMON="$ANDROID_BUILD_TOP/out/target/common"
42COVERAGE_METADATA_FILE="$OUT_COMMON/obj/APPS/${MODULE_NAME}_intermediates/coverage.em"
43
44source $ANDROID_BUILD_TOP/build/envsetup.sh
45
46set -e # fail early
47
48(cd "$ANDROID_BUILD_TOP/$MODULE_PATH" && EMMA_INSTRUMENT_STATIC=true mma -j32)
49(cd "$ANDROID_BUILD_TOP/$TEST_MODULE_PATH" && EMMA_INSTRUMENT_STATIC=true mma -j32)
50
51set -x # print commands
52
53adb root
54adb wait-for-device
55
56adb shell rm -f "$REMOTE_COVERAGE_OUTPUT_FILE"
57
58adb install -r -g "$OUT/$TEST_MODULE_INSTALL_PATH"
59adb install -r -g "$OUT/$MODULE_INSTALL_PATH"
60
61adb shell am instrument -e coverage true -e size small -w "$TEST_RUNNER"
62
63mkdir -p "$OUTPUT_DIR"
64
65adb pull "$REMOTE_COVERAGE_OUTPUT_FILE" "$COVERAGE_OUTPUT_FILE"
66
67java -jar "$REPORTER_JAR" \
68  --report-dir "$OUTPUT_DIR" \
69  --metadata-file "$COVERAGE_METADATA_FILE" \
70  --coverage-file "$COVERAGE_OUTPUT_FILE" \
71  --report-type "$REPORT_TYPE" \
72  --source-dir "$ANDROID_BUILD_TOP/$MODULE_PATH/src"
73
74set +x
75
76# Echo the file as URI to quickly open the result using ctrl-click in terminal
77echo "file://$OUTPUT_DIR/index.html"
78