1# Rules for running robolectric tests. 2# 3# Uses the following variables: 4# 5# LOCAL_JAVA_LIBRARIES 6# LOCAL_STATIC_JAVA_LIBRARIES 7# LOCAL_ROBOTEST_FAILURE_FATAL 8# LOCAL_ROBOTEST_TIMEOUT 9# LOCAL_TEST_PACKAGE 10# LOCAL_ROBOTEST_FILES 11# ROBOTEST_FAILURE_FATAL 12# ROBOTEST_FILTER 13# ROBOTEST_RUN_INDIVIDUALLY 14# 15# 16# If ROBOTEST_FAILURE_FATAL is set to true then failing tests will cause a 17# build failure. Otherwise failures will be logged but ignored by make. 18# 19# If ROBOTEST_FILTER is set to a regex then only tests matching that pattern 20# will be run. This currently only works at the class level. 21# 22# TODO: Switch to a JUnit runner which can support method-level test 23# filtering and use that rather than grep to implement ROBOTEST_FILTER. 24# 25# If ROBOTEST_RUN_INDIVIDUALLY is set to true, each test class will be run by a 26# different JVM, preventing any interaction between different tests. This is 27# significantly slower than running all tests within the same JVM, but prevents 28# unwanted interactions. 29# 30# Tests classes are found by looking for *Test.java files in 31# LOCAL_PATH recursively. 32 33################################################ 34# General settings, independent of the module. # 35################################################ 36 37### Used for running tests. 38 39# Where to find Robolectric. 40my_robolectric_script_path := $(call my-dir) 41 42my_collect_target := $(LOCAL_MODULE)-coverage 43my_report_target := $(LOCAL_MODULE)-jacoco 44# Whether or not to ignore the result of running the robotests. 45# LOCAL_ROBOTEST_FAILURE_FATAL will take precedence over ROBOTEST_FAILURE_FATAL, 46# if present. 47my_failure_fatal := $(if $(LOCAL_ROBOTEST_FAILURE_FATAL)$(ROBOTEST_FAILURE_FATAL),true,false) 48# The timeout for the command. A value of '0' means no timeout. The default is 49# 10 minutes. 50my_timeout := $(if $(LOCAL_ROBOTEST_TIMEOUT),$(LOCAL_ROBOTEST_TIMEOUT),600) 51# Command to filter the list of test classes. 52# If not specified, defaults to including all the tests. 53my_test_filter_command := $(if $(ROBOTEST_FILTER),grep -E "$(ROBOTEST_FILTER)",cat) 54 55# The directory containing the sources. 56my_instrument_makefile_dir := $(dir $(ALL_MODULES.$(LOCAL_TEST_PACKAGE).MAKEFILE)) 57my_instrument_source_dirs := $(if $(LOCAL_INSTRUMENT_SOURCE_DIRS),\ 58 $(LOCAL_INSTRUMENT_SOURCE_DIRS),\ 59 $(my_instrument_makefile_dir)src $(my_instrument_makefile_dir)java) 60 61########################## 62# Used by base_rules.mk. # 63########################## 64 65LOCAL_MODULE_CLASS := ROBOLECTRIC 66# This is actually a phony target that is never built. 67LOCAL_BUILT_MODULE_STEM := test.fake 68# Since it is not built, it cannot be installed. But we will define our own 69# dist files, depending on which of the specific targets is invoked. 70LOCAL_UNINSTALLABLE_MODULE := true 71# Do not build it for checkbuild or mma 72LOCAL_DONT_CHECK_MODULE := true 73 74include $(BUILD_SYSTEM)/base_rules.mk 75 76 77############################# 78# Module specific settings. # 79############################# 80 81### Used for running tests. 82 83# The list of test classes. Robolectric requires an explicit list of tests to 84# run, which is compiled from the Java files ending in "Test" within the 85# directory from which this module is invoked. 86ifeq ($(strip $(LOCAL_ROBOTEST_FILES)),) 87 LOCAL_ROBOTEST_FILES := $(call find-files-in-subdirs,$(LOCAL_PATH)/src,*Test.java,.) 88endif 89# Convert the paths into package names by removing .java extension and replacing "/" with "." 90my_tests := $(subst /,.,$(basename $(LOCAL_ROBOTEST_FILES))) 91my_tests := $(sort $(shell echo '$(my_tests)' | tr ' ' '\n' | $(my_test_filter_command))) 92# The source jars containing the tests. 93my_srcs_jars := \ 94 $(foreach lib, \ 95 $(LOCAL_JAVA_LIBRARIES) $(LOCAL_STATIC_JAVA_LIBRARIES), \ 96 $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/classes-pre-proguard.jar) \ 97 $(foreach lib, \ 98 $(LOCAL_TEST_PACKAGE), \ 99 $(call intermediates-dir-for,APPS,$(lib),,COMMON)/classes-pre-proguard.jar) 100# The jars needed to run the tests. 101my_jars := \ 102 $(my_robolectric_jars) \ 103 $(call java-lib-files,junitxml) \ 104 $(my_srcs_jars) 105 106 107 108# Run tests. 109my_target := $(LOCAL_BUILT_MODULE) 110my_filename_stem := test 111 112# Define rules that copy android-all jars to the intermediates folder. 113p_android_all_source_jar := $(call intermediates-dir-for, JAVA_LIBRARIES, robolectric_android-all-stub, , COMMON)/classes-with-res.jar 114android_all_lib_path := prebuilts/misc/common/robolectric/android-all 115my_robolectric_path := $(intermediates.COMMON)/android-all 116copy_android_all_jar_pairs := \ 117 $(android_all_lib_path)/android-all-4.1.2_r1-robolectric-r1.jar:$(my_robolectric_path)/android-all-4.1.2_r1-robolectric-r1.jar \ 118 $(android_all_lib_path)/android-all-4.2.2_r1.2-robolectric-r1.jar:$(my_robolectric_path)/android-all-4.2.2_r1.2-robolectric-r1.jar \ 119 $(android_all_lib_path)/android-all-4.3_r2-robolectric-r1.jar:$(my_robolectric_path)/android-all-4.3_r2-robolectric-r1.jar \ 120 $(android_all_lib_path)/android-all-4.4_r1-robolectric-r2.jar:$(my_robolectric_path)/android-all-4.4_r1-robolectric-r2.jar \ 121 $(android_all_lib_path)/android-all-5.0.2_r3-robolectric-r0.jar:$(my_robolectric_path)/android-all-5.0.2_r3-robolectric-r0.jar \ 122 $(android_all_lib_path)/android-all-5.1.1_r9-robolectric-r2.jar:$(my_robolectric_path)/android-all-5.1.1_r9-robolectric-r2.jar \ 123 $(android_all_lib_path)/android-all-6.0.1_r3-robolectric-r1.jar:$(my_robolectric_path)/android-all-6.0.1_r3-robolectric-r1.jar \ 124 $(android_all_lib_path)/android-all-7.0.0_r1-robolectric-r1.jar:$(my_robolectric_path)/android-all-7.0.0_r1-robolectric-r1.jar \ 125 $(android_all_lib_path)/android-all-7.1.0_r7-robolectric-r1.jar:$(my_robolectric_path)/android-all-7.1.0_r7-robolectric-r1.jar \ 126 $(android_all_lib_path)/android-all-8.0.0_r4-robolectric-r1.jar:$(my_robolectric_path)/android-all-8.0.0_r4-robolectric-r1.jar \ 127 $(android_all_lib_path)/android-all-8.1.0-robolectric-r4458339.jar:$(my_robolectric_path)/android-all-8.1.0-robolectric-r4458339.jar \ 128 $(p_android_all_source_jar):$(my_robolectric_path)/android-all-P-robolectric-r0.jar 129copy_android_all_jars := $(call copy-many-files, $(copy_android_all_jar_pairs)) 130 131$(my_target): $(copy_android_all_jars) 132 133include $(my_robolectric_script_path)/robotest-internal.mk 134# clean local variables 135my_java_args := 136my_target := 137 138# Target for running robolectric tests using jacoco 139my_target := $(LOCAL_BUILT_MODULE)-coverage 140my_filename_stem := coverage 141$(my_collect_target): $(my_target) 142$(my_target): $(call java-lib-files,jvm-jacoco-agent,true) $(copy_android_all_jars) 143 144my_coverage_dir := $(intermediates)/coverage 145my_coverage_file := $(my_coverage_dir)/jacoco.exec 146 147# List of packages to exclude jacoco from running 148my_jacoco_excludes := \ 149 org.robolectric.* \ 150 org.mockito.* \ 151 org.junit.* \ 152 org.objectweb.* \ 153 com.thoughtworks.xstream.* 154# The Jacoco agent JAR. 155my_jacoco_agent_jar := $(call java-lib-files,jvm-jacoco-agent,true) 156# Using Jacoco with Robolectric is broken in 0.7.3 <= version < 0.7.6. 157# In 0.7.6 or above, the parameter "inclnolocationclasses" is needed. 158# See https://github.com/jacoco/jacoco/pull/288 for more 159# In JDK9, if "inclnolocationclasses" is used, we also need to specify 160# exclclassloader=jdk.internal.reflect.DelegatingClassLoader 161# https://github.com/jacoco/jacoco/issues/16 162my_jacoco_agent_args = \ 163 destfile=$(my_coverage_file) \ 164 excludes=$(call normalize-path-list, $(my_jacoco_excludes)) \ 165 inclnolocationclasses=true \ 166 exclclassloader=jdk.internal.reflect.DelegatingClassLoader \ 167 append=false 168my_java_args := \ 169 -javaagent:$(my_jacoco_agent_jar)=$(call normalize-comma-list, $(my_jacoco_agent_args)) 170include $(my_robolectric_script_path)/robotest-internal.mk 171# Clear temporary variables 172my_failure_fatal := 173my_jacoco_agent_jar := 174my_jacoco_agent_args := 175my_jacoco_excludes := 176my_java_args := 177my_robolectric_jars := 178my_target := 179my_tests := 180my_filename_stem := 181 182# Target for generating code coverage reports using jacoco.exec 183my_target := $(LOCAL_BUILT_MODULE)-jacoco 184$(my_report_target): $(my_target) 185 186# The JAR file containing the report generation tool. 187my_coverage_report_class := com.google.android.jacoco.reporter.ReportGenerator 188my_coverage_report_jar := $(call java-lib-files,jvm-jacoco-reporter,true) 189my_coverage_srcs_jars := $(my_srcs_jars) 190my_coverage_report_dist_file := $(my_report_target)-html.zip 191 192## jacoco code coverage reports 193include $(my_robolectric_script_path)/report-internal.mk 194# Clear temporary variables 195my_coverage_dir := 196my_coverage_file := 197my_coverage_report_class := 198my_coverage_report_dist_file := 199my_coverage_report_jar := 200my_coverage_srcs_jars := 201my_robolectric_script_path := 202my_robolectric_path := 203my_srcs_jars := 204my_target := 205