From a54db13528fa8b586d58b42aff46df40466ea950 Mon Sep 17 00:00:00 2001 From: Mitchell Wills Date: Tue, 2 Feb 2016 10:53:39 -0800 Subject: Add coverage support for Wifi unit tests This adds a script to automatically run the tests and generate a coverage report. Bug: 26958680 Change-Id: I139be7065ddd3c20f6e813191d4993bd1b9377e6 --- tests/wifitests/Android.mk | 29 +++++++++++++++++++++++ tests/wifitests/coverage.sh | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100755 tests/wifitests/coverage.sh (limited to 'tests') diff --git a/tests/wifitests/Android.mk b/tests/wifitests/Android.mk index fa4155a5d..eeefe6d8b 100644 --- a/tests/wifitests/Android.mk +++ b/tests/wifitests/Android.mk @@ -70,6 +70,35 @@ LOCAL_SRC_FILES := $(filter-out $(call all-java-files-under, \ src/com/android/server/wifi/nan),$(LOCAL_SRC_FILES)) endif +# Provide jack a list of classes to exclude form code coverage +# This list is generated from the java source files in this module +# The list is a comma separated list of class names with * matching zero or more characters. +# Example: +# Input files: src/com/android/server/wifi/Test.java src/com/android/server/wifi/AnotherTest.java +# Generated exclude list: com.android.server.wifi.Test*,com.android.server.wifi.AnotherTest* + +# Filter all src files to just java files +local_java_files := $(filter %.java,$(LOCAL_SRC_FILES)) +# Transform java file names into full class names. +# This only works if the class name matches the file name and the directory structure +# matches the package. +local_classes := $(subst /,.,$(patsubst src/%.java,%,$(local_java_files))) +# Utility variables to allow replacing a space with a comma +comma:= , +empty:= +space:= $(empty) $(empty) +# Convert class name list to jacoco exclude list +# This appends a * to all classes and replace the space separators with commas. +# These patterns will match all classes in this module and their inner classes. +jacoco_exclude := $(subst $(space),$(comma),$(patsubst %,%*,$(local_classes))) + +jacoco_include := com.android.server.wifi.*,android.net.wifi.* + + +LOCAL_JACK_FLAGS := \ + -D jack.coverage.jacoco.include=$(jacoco_include) \ + -D jack.coverage.jacoco.exclude=$(jacoco_exclude) + LOCAL_STATIC_JAVA_LIBRARIES := \ mockito-target \ android-support-test \ diff --git a/tests/wifitests/coverage.sh b/tests/wifitests/coverage.sh new file mode 100755 index 000000000..e291a1f74 --- /dev/null +++ b/tests/wifitests/coverage.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +if [[ ! ( ($# == 1) || ($# == 2 && ($2 == "HTML" || $2 == "XML" || $2 == "CSV"))) ]]; then + echo "$0: usage: coverage.sh OUTPUT_DIR [REPORT_TYPE]" + echo "REPORT_TYPE [HTML | XML | CSV] : the type of the report (default is HTML)" + exit 1 +fi + +if [ -z $ANDROID_BUILD_TOP ]; then + echo "You need to source and lunch before you can use this script" + exit 1 +fi + +REPORTER_JAR=$ANDROID_BUILD_TOP/prebuilts/sdk/tools/jack-jacoco-reporter.jar + +OUTPUT_DIR=$1 +if [[ $# == 2 ]]; then + REPORT_TYPE=$2 +else + REPORT_TYPE="HTML" +fi + +echo "Running tests and generating coverage report" +echo "Output dir: $OUTPUT_DIR" +echo "Report type: $REPORT_TYPE" + +REMOTE_COVERAGE_OUTPUT_FILE=/data/data/com.android.server.wifi.test/files/coverage.ec +COVERAGE_OUTPUT_FILE=$OUTPUT_DIR/wifi_coverage.ec +COVERAGE_METADATA_FILE=$ANDROID_BUILD_TOP/out/target/common/obj/APPS/FrameworksWifiTests_intermediates/coverage.em + +set -e # fail early + +echo "+ EMMA_INSTRUMENT_STATIC=true mmma -j32 $ANDROID_BUILD_TOP/frameworks/opt/net/wifi/tests" +# NOTE Don't actually run the command above since this shell doesn't inherit functions from the +# caller. +EMMA_INSTRUMENT_STATIC=true make -j32 -C $ANDROID_BUILD_TOP -f build/core/main.mk MODULES-IN-frameworks-opt-net-wifi-tests + +set -x # print commands + +adb shell rm -f $REMOTE_COVERAGE_OUTPUT_FILE + +adb install -r -g "$OUT/data/app/FrameworksWifiTests/FrameworksWifiTests.apk" + +adb shell am instrument -e coverage true -w 'com.android.server.wifi.test/android.support.test.runner.AndroidJUnitRunner' + +mkdir -p $OUTPUT_DIR + +adb pull $REMOTE_COVERAGE_OUTPUT_FILE $COVERAGE_OUTPUT_FILE + +java -jar $REPORTER_JAR \ + --report-dir $OUTPUT_DIR \ + --metadata-file $COVERAGE_METADATA_FILE \ + --coverage-file $COVERAGE_OUTPUT_FILE \ + --report-type $REPORT_TYPE \ + --source-dir $ANDROID_BUILD_TOP/frameworks/opt/net/wifi/tests/wifitests/src \ + --source-dir $ANDROID_BUILD_TOP/frameworks/opt/net/wifi/service/java \ + --source-dir $ANDROID_BUILD_TOP/frameworks/base/wifi/java -- cgit v1.2.3