1#!/usr/bin/env bash
2
3set -e
4
5# Setup tmp folder
6COVERAGE_TMP_FOLDER=/tmp/coverage_robolectric
7rm -rf ${COVERAGE_TMP_FOLDER}
8mkdir -p ${COVERAGE_TMP_FOLDER}/com/android/server/
9
10# Run test + collect coverage
11script -q ${COVERAGE_TMP_FOLDER}/atest_log \
12  -c 'atest ServiceBluetoothRoboTests -- \
13  --jacocoagent-path gs://tradefed_test_resources/teams/code_coverage/jacocoagent.jar \
14  --coverage --coverage-toolchain JACOCO'
15
16COVERAGE_COLLECTED=$(rg 'Test Logs have been saved in ' ${COVERAGE_TMP_FOLDER}/atest_log | sed -e 's/^.* //' -e 's/log.*$/log/')
17
18# Link source into the tmp folder
19ln -s "${ANDROID_BUILD_TOP}"/packages/modules/Bluetooth/service/src ${COVERAGE_TMP_FOLDER}/com/android/server/bluetooth
20
21# Extract class coverage and delete unwanted class for the report
22unzip "${ANDROID_HOST_OUT}"/testcases/ServiceBluetoothRoboTests/ServiceBluetoothRoboTests.jar "com/android/server/bluetooth*" -d ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip
23# shellcheck disable=SC2046
24rm -rf $(find ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip -iname "*test*")
25# shellcheck disable=SC2016
26rm ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip/com/android/server/bluetooth/'BluetoothAdapterState$waitForState$3$invokeSuspend$$inlined$filter$1.class'
27rm ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip/com/android/server/bluetooth/R.class
28
29# Generate report:
30# You may want to run "m jacoco-cli" if above command failed
31java -jar "${ANDROID_BUILD_TOP}"/out/host/linux-x86/framework/jacoco-cli.jar report "${COVERAGE_COLLECTED}"/invocation_*/inv_*/coverage_*.ec --classfiles ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip --html ${COVERAGE_TMP_FOLDER}/coverage --name coverage.html --sourcefiles ${COVERAGE_TMP_FOLDER}
32
33# Start python server and expose URL
34printf "Url to connect to the coverage \033[32m http://%s:8000 \033[0m\n" "$(hostname)"
35python3 -m http.server --directory ${COVERAGE_TMP_FOLDER}/coverage
36