1#!/usr/bin/env bash 2 3# 4# Copyright (C) 2021 The Android Open Source Project 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19##### App specific parameters ##### 20 21PACKAGE_NAME='com.android.imsserviceentitlement' 22MODULE_NAME='ImsServiceEntitlement' 23MODULE_PATH='packages/apps/ImsServiceEntitlement' 24 25TEST_PACKAGE='com.android.imsserviceentitlement.tests' 26TEST_MODULE_NAME='ImsServiceEntitlementUnitTests' 27TEST_MODULE_PATH='packages/apps/ImsServiceEntitlement/tests/unittests' 28TEST_MODULE_INSTALL_PATH="testcases/$TEST_MODULE_NAME/arm64/$TEST_MODULE_NAME.apk" 29TEST_RUNNER="$TEST_PACKAGE/androidx.test.runner.AndroidJUnitRunner" 30 31##### End app specific parameters ##### 32 33if [[ $# != 0 && ! ($# == 1 && ($1 == "html" || $1 == "xml" || $1 == "csv")) ]]; then 34 echo "$0: usage: coverage.sh [REPORT_TYPE]" 35 echo "REPORT_TYPE [html | xml | csv] : the type of the report (default is html)" 36 exit 1 37fi 38 39REPORT_TYPE=${1:-html} 40 41if [ -z $ANDROID_BUILD_TOP ]; then 42 echo "You need to source and lunch before you can use this script" 43 exit 1 44fi 45 46REPORTER_JAR="$ANDROID_BUILD_TOP/out/soong/host/linux-x86/framework/jacoco-cli.jar" 47 48OUTPUT_DIR="$ANDROID_BUILD_TOP/out/coverage/$MODULE_NAME" 49 50echo "Running tests and generating coverage report" 51echo "Output dir: $OUTPUT_DIR" 52echo "Report type: $REPORT_TYPE" 53 54# location on the device to store coverage results, need to be accessible by the app 55REMOTE_COVERAGE_OUTPUT_FILE="/data/data/$PACKAGE_NAME/files/coverage.ec" 56 57COVERAGE_OUTPUT_FILE="$ANDROID_BUILD_TOP/out/$PACKAGE_NAME.ec" 58OUT_COMMON="$ANDROID_BUILD_TOP/out/target/common" 59COVERAGE_CLASS_FILE="$ANDROID_BUILD_TOP/out/soong/.intermediates/$MODULE_PATH/ImsServiceEntitlementLib/android_common/javac/ImsServiceEntitlementLib.jar" 60 61source $ANDROID_BUILD_TOP/build/envsetup.sh 62 63set -e # fail early 64 65echo "" 66echo "BUILDING TEST PACKAGE $TEST_PACKAGE_NAME" 67echo "============================================" 68(cd "$ANDROID_BUILD_TOP/$TEST_MODULE_PATH" && EMMA_INSTRUMENT=true EMMA_INSTRUMENT_STATIC=true mma -j32) 69echo "============================================" 70 71#set -x # print commands 72 73adb root 74adb wait-for-device 75 76adb shell rm -f "$REMOTE_COVERAGE_OUTPUT_FILE" 77 78adb install -r -g "$OUT/$TEST_MODULE_INSTALL_PATH" 79 80echo "" 81echo "RUNNING TESTS $TEST_RUNNER" 82echo "============================================" 83adb shell am instrument -e coverage true -e coverageFile "$REMOTE_COVERAGE_OUTPUT_FILE" -w "$TEST_RUNNER" 84echo "============================================" 85 86mkdir -p "$OUTPUT_DIR" 87 88adb pull "$REMOTE_COVERAGE_OUTPUT_FILE" "$COVERAGE_OUTPUT_FILE" 89 90java -jar "$REPORTER_JAR" \ 91 report "$COVERAGE_OUTPUT_FILE" \ 92 --$REPORT_TYPE "$OUTPUT_DIR" \ 93 --classfiles "$COVERAGE_CLASS_FILE" \ 94 --sourcefiles "$ANDROID_BUILD_TOP/$MODULE_PATH/src" 95 96#set +x 97 98# Echo the file as URI to quickly open the result using ctrl-click in terminal 99if [[ REPORT_TYPE == html ]] ; then 100 echo "COVERAGE RESULTS IN:" 101 echo "file://$OUTPUT_DIR/index.html" 102fi 103