1#!/usr/bin/env bash
2
3if [[ ! ($# == 1) ]]; then
4  echo "$0: usage: coverage.sh OUTPUT_DIR"
5  exit 1
6fi
7
8if [ -z $ANDROID_BUILD_TOP ]; then
9  echo "You need to source and lunch before you can use this script"
10  exit 1
11fi
12
13cd "$(dirname $0)" #cd to directory containing this script
14
15
16REPORTER_JAR=$ANDROID_HOST_OUT/framework/jacoco-cli.jar
17
18OUTPUT_DIR=$1
19
20echo "Running tests and generating coverage report"
21echo "Output dir: $OUTPUT_DIR"
22
23REMOTE_COVERAGE_OUTPUT_FILE=/data/data/com.android.server.wifi.test/files/coverage.ec
24COVERAGE_OUTPUT_FILE=$OUTPUT_DIR/wifi_coverage.ec
25
26set -e # fail early
27set -x # print commands
28
29# build this module so we can run its tests, and
30# build system/core so we can invoke `adb`, and
31# build jacoco-cli.jar so we can generate the report
32$ANDROID_BUILD_TOP/build/soong/soong_ui.bash --make-mode \
33  EMMA_INSTRUMENT=true \
34  EMMA_INSTRUMENT_FRAMEWORK=false \
35  EMMA_INSTRUMENT_STATIC=true \
36  ANDROID_COMPILE_WITH_JACK=false \
37  SKIP_BOOT_JARS_CHECK=true \
38  MODULES-IN-frameworks-opt-net-wifi-tests \
39  MODULES-IN-system-core \
40  MODULES-IN-external-jacoco \
41  FrameworksWifiTests
42
43APK_NAME="$(ls -t $(find $OUT -name FrameworksWifiTests.apk) | head -n 1)"
44
45adb root
46adb wait-for-device
47
48adb shell rm -f $REMOTE_COVERAGE_OUTPUT_FILE
49
50adb install -r -g "$APK_NAME"
51
52adb shell am instrument -e coverage true --no-hidden-api-checks -w 'com.android.server.wifi.test/com.android.server.wifi.CustomTestRunner'
53
54mkdir -p $OUTPUT_DIR
55
56adb pull $REMOTE_COVERAGE_OUTPUT_FILE $COVERAGE_OUTPUT_FILE
57
58java -jar $REPORTER_JAR \
59  report \
60  --classfiles $ANDROID_PRODUCT_OUT/../../common/obj/APPS/FrameworksWifiTests_intermediates/jacoco/report-resources/jacoco-report-classes.jar \
61  --html $OUTPUT_DIR \
62  --sourcefiles $ANDROID_BUILD_TOP/frameworks/opt/net/wifi/tests/wifitests/src \
63  --sourcefiles $ANDROID_BUILD_TOP/frameworks/opt/net/wifi/service/java \
64  --name wifi-coverage \
65  $COVERAGE_OUTPUT_FILE
66
67echo Created report at $OUTPUT_DIR/index.html
68
69