1#!/usr/bin/env bash
2
3if [[ ! ( ($# == 1) || ($# == 2 && ($2 == "HTML" || $2 == "XML" || $2 == "CSV"))) ]]; then
4  echo "$0: usage: coverage.sh OUTPUT_DIR [REPORT_TYPE]"
5  echo "REPORT_TYPE [HTML | XML | CSV] : the type of the report (default is HTML)"
6  exit 1
7fi
8
9if [ -z $ANDROID_BUILD_TOP ]; then
10  echo "You need to source and lunch before you can use this script"
11  exit 1
12fi
13
14REPORTER_JAR=$ANDROID_BUILD_TOP/prebuilts/sdk/tools/jack-jacoco-reporter.jar
15
16OUTPUT_DIR=$1
17if [[ $# == 2 ]]; then
18  REPORT_TYPE=$2
19else
20  REPORT_TYPE="HTML"
21fi
22
23echo "Running tests and generating coverage report"
24echo "Output dir: $OUTPUT_DIR"
25echo "Report type: $REPORT_TYPE"
26
27REMOTE_COVERAGE_OUTPUT_FILE=/data/data/com.android.server.wifi.test/files/coverage.ec
28COVERAGE_OUTPUT_FILE=$OUTPUT_DIR/wifi_coverage.ec
29COVERAGE_METADATA_FILE=$ANDROID_BUILD_TOP/out/target/common/obj/APPS/FrameworksWifiTests_intermediates/coverage.em
30
31set -e # fail early
32
33echo "+ EMMA_INSTRUMENT_STATIC=true mmma -j32 $ANDROID_BUILD_TOP/frameworks/opt/net/wifi/tests"
34# NOTE Don't actually run the command above since this shell doesn't inherit functions from the
35#      caller.
36EMMA_INSTRUMENT_STATIC=true make -j32 -C $ANDROID_BUILD_TOP -f build/core/main.mk MODULES-IN-frameworks-opt-net-wifi-tests
37
38set -x # print commands
39
40adb root
41adb wait-for-device
42
43adb shell rm -f $REMOTE_COVERAGE_OUTPUT_FILE
44
45adb install -r -g "$OUT/data/app/FrameworksWifiTests/FrameworksWifiTests.apk"
46
47adb shell am instrument -e coverage true -w 'com.android.server.wifi.test/android.support.test.runner.AndroidJUnitRunner'
48
49mkdir -p $OUTPUT_DIR
50
51adb pull $REMOTE_COVERAGE_OUTPUT_FILE $COVERAGE_OUTPUT_FILE
52
53java -jar $REPORTER_JAR \
54  --report-dir $OUTPUT_DIR \
55  --metadata-file $COVERAGE_METADATA_FILE \
56  --coverage-file $COVERAGE_OUTPUT_FILE \
57  --report-type $REPORT_TYPE \
58  --source-dir $ANDROID_BUILD_TOP/frameworks/opt/net/wifi/tests/wifitests/src \
59  --source-dir $ANDROID_BUILD_TOP/frameworks/opt/net/wifi/service/java \
60  --source-dir $ANDROID_BUILD_TOP/frameworks/base/wifi/java
61