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
16# "Atest results and logs directory: /tmp/path<CR><LF>" -> "/tmp/path"
17COVERAGE_COLLECTED=$(rg 'Atest results and logs directory:' ${COVERAGE_TMP_FOLDER}/atest_log | cut -d ':' -f 2 | tr -d '\r' | xargs)
18
19# Link source into the tmp folder
20ln -s "${ANDROID_BUILD_TOP}"/packages/modules/Bluetooth/service/src ${COVERAGE_TMP_FOLDER}/com/android/server/bluetooth
21
22# Extract class coverage and delete unwanted class for the report
23unzip "${ANDROID_HOST_OUT}"/testcases/ServiceBluetoothRoboTests/ServiceBluetoothRoboTests.jar "com/android/server/bluetooth*" -d ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip
24# shellcheck disable=SC2046
25rm -rf $(find ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip -iname "*test*")
26# shellcheck disable=SC2016
27rm -rf ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip/com/android/server/bluetooth/'BluetoothAdapterState$waitForState$3$invokeSuspend$$inlined$filter$1.class'
28# shellcheck disable=SC2016
29rm -rf ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip/com/android/server/bluetooth/'BluetoothAdapterState$waitForState$3$invokeSuspend$$inlined$filter$1$2.class'
30rm -rf ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip/com/android/server/bluetooth/R.class
31
32# Generate report:
33# You may want to run "m jacoco-cli" if above command failed
34java -jar "${ANDROID_BUILD_TOP}"/out/host/linux-x86/framework/jacoco-cli.jar report "${COVERAGE_COLLECTED}"/log/invocation_*/inv_*/coverage_*.ec --classfiles ${COVERAGE_TMP_FOLDER}/ServiceBluetoothRobo_unzip --html ${COVERAGE_TMP_FOLDER}/coverage --name coverage.html --sourcefiles ${COVERAGE_TMP_FOLDER}
35
36# Start python server and expose URL
37printf "Url to connect to the coverage \033[32m http://%s:8000 \033[0m\n" "$(hostname)"
38python3 -m http.server --directory ${COVERAGE_TMP_FOLDER}/coverage
39