• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
38ifneq ($(DISABLE_ROBO_RUN_TESTS),true)
39    # Where to find Robolectric.
40    my_robolectric_script_path := $(call my-dir)
41
42    my_collect_target := $(LOCAL_MODULE)-coverage
43    my_report_target := $(LOCAL_MODULE)-jacoco
44    .PHONY: $(my_collect_target) $(my_report_target)
45    # Whether or not to ignore the result of running the robotests.
46    # LOCAL_ROBOTEST_FAILURE_FATAL will take precedence over ROBOTEST_FAILURE_FATAL,
47    # if present.
48    my_failure_fatal := $(if $(LOCAL_ROBOTEST_FAILURE_FATAL)$(ROBOTEST_FAILURE_FATAL),true,false)
49    # The timeout for the command. A value of '0' means no timeout. The default is
50    # 10 minutes.
51    my_timeout := $(if $(LOCAL_ROBOTEST_TIMEOUT),$(LOCAL_ROBOTEST_TIMEOUT),600)
52    # Command to filter the list of test classes.
53    # If not specified, defaults to including all the tests.
54    my_test_filter_command := $(if $(ROBOTEST_FILTER),grep -E "$(ROBOTEST_FILTER)",cat)
55
56    # The directory containing the sources.
57    my_instrument_makefile_dir := $(dir $(ALL_MODULES.$(LOCAL_TEST_PACKAGE).MAKEFILE))
58    my_instrument_source_dirs := $(if $(LOCAL_INSTRUMENT_SOURCE_DIRS),\
59        $(LOCAL_INSTRUMENT_SOURCE_DIRS),\
60        $(my_instrument_makefile_dir)src $(my_instrument_makefile_dir)java)
61
62    ##########################
63    # Used by base_rules.mk. #
64    ##########################
65
66    LOCAL_MODULE_CLASS := ROBOLECTRIC
67    # This is actually a phony target that is never built.
68    LOCAL_BUILT_MODULE_STEM := test.fake
69    # Since it is not built, it cannot be installed. But we will define our own
70    # dist files, depending on which of the specific targets is invoked.
71    LOCAL_UNINSTALLABLE_MODULE := true
72    # Do not build it for checkbuild or mma
73    LOCAL_DONT_CHECK_MODULE := true
74
75    include $(BUILD_SYSTEM)/base_rules.mk
76
77
78    #############################
79    # Module specific settings. #
80    #############################
81
82    ### Used for running tests.
83
84    # The list of test classes. Robolectric requires an explicit list of tests to
85    # run, which is compiled from the Java files ending in "Test" within the
86    # directory from which this module is invoked.
87    ifeq ($(strip $(LOCAL_ROBOTEST_FILES)),)
88        LOCAL_ROBOTEST_FILES := $(call find-files-in-subdirs,$(LOCAL_PATH)/src,*Test.java,.)
89    endif
90    # Convert the paths into package names by removing .java extension and replacing "/" with "."
91    my_tests := $(subst /,.,$(basename $(LOCAL_ROBOTEST_FILES)))
92    my_tests := $(sort $(shell echo '$(my_tests)' | tr ' ' '\n' | $(my_test_filter_command)))
93    # The source jars containing the tests.
94    my_srcs_jars := \
95        $(foreach lib, \
96            $(LOCAL_JAVA_LIBRARIES) $(LOCAL_STATIC_JAVA_LIBRARIES), \
97            $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/classes-pre-proguard.jar) \
98        $(foreach lib, \
99            $(LOCAL_TEST_PACKAGE), \
100            $(call intermediates-dir-for,APPS,$(lib),,COMMON)/classes-pre-proguard.jar)
101    # The jars needed to run the tests.
102    my_jars := \
103        $(my_robolectric_jars) \
104        $(call java-lib-files,junitxml) \
105        $(my_srcs_jars)
106
107
108
109    # Run tests.
110    my_phony_target := $(LOCAL_MODULE)
111    my_target := $(LOCAL_BUILT_MODULE)
112    my_filename_stem := test
113
114    # Define rules that copy android-all jars to the intermediates folder.
115    local_android_all_source_jar := $(call intermediates-dir-for, JAVA_LIBRARIES, robolectric_android-all-stub,,COMMON)/classes-with-res.jar
116    android_all_lib_path := prebuilts/misc/common/robolectric/android-all
117    my_robolectric_path := $(intermediates.COMMON)/android-all
118    copy_android_all_jar_pairs := \
119      $(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 \
120      $(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 \
121      $(android_all_lib_path)/android-all-4.3_r2-robolectric-r1.jar:$(my_robolectric_path)/android-all-4.3_r2-robolectric-r1.jar \
122      $(android_all_lib_path)/android-all-4.4_r1-robolectric-r2.jar:$(my_robolectric_path)/android-all-4.4_r1-robolectric-r2.jar \
123      $(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 \
124      $(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 \
125      $(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 \
126      $(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 \
127      $(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 \
128      $(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 \
129      $(android_all_lib_path)/android-all-8.1.0-robolectric-4611349.jar:$(my_robolectric_path)/android-all-8.1.0-robolectric-4611349.jar \
130      $(android_all_lib_path)/android-all-9-robolectric-4913185-2.jar:$(my_robolectric_path)/android-all-9-robolectric-4913185-2.jar \
131      $(local_android_all_source_jar):$(my_robolectric_path)/android-all-Q-robolectric-r0.jar
132    copy_android_all_jars := $(call copy-many-files, $(copy_android_all_jar_pairs))
133
134    $(my_target): $(copy_android_all_jars)
135
136    include $(my_robolectric_script_path)/robotest-internal.mk
137    # clean local variables
138    my_java_args :=
139    my_phony_target :=
140    my_target :=
141
142    # Target for running robolectric tests using jacoco
143    my_phony_target := $(my_report_target)
144    my_target := $(LOCAL_BUILT_MODULE)-coverage
145    my_collect_file := $(my_target)
146    my_filename_stem := coverage
147    $(my_collect_target): $(my_target)
148    $(my_target): $(call java-lib-files,jvm-jacoco-agent,true) $(copy_android_all_jars)
149
150    my_coverage_dir := $(intermediates)/coverage
151    my_coverage_file := $(my_coverage_dir)/jacoco.exec
152
153    # List of packages to exclude jacoco from running
154    my_jacoco_excludes := \
155        org.robolectric.* \
156        org.mockito.* \
157        org.junit.* \
158        org.objectweb.* \
159        com.thoughtworks.xstream.*
160    # The Jacoco agent JAR.
161    my_jacoco_agent_jar := $(call java-lib-files,jvm-jacoco-agent,true)
162    # Using Jacoco with Robolectric is broken in 0.7.3 <= version < 0.7.6.
163    # In 0.7.6 or above, the parameter "inclnolocationclasses" is needed.
164    # See https://github.com/jacoco/jacoco/pull/288 for more
165    # In JDK9, if "inclnolocationclasses" is used, we also need to specify
166    # exclclassloader=jdk.internal.reflect.DelegatingClassLoader
167    # https://github.com/jacoco/jacoco/issues/16
168    my_jacoco_agent_args = \
169        destfile=$(my_coverage_file) \
170        excludes=$(call normalize-path-list, $(my_jacoco_excludes)) \
171        inclnolocationclasses=true \
172        exclclassloader=jdk.internal.reflect.DelegatingClassLoader \
173        append=false
174    my_java_args := \
175        -javaagent:$(my_jacoco_agent_jar)=$(call normalize-comma-list, $(my_jacoco_agent_args))
176    include $(my_robolectric_script_path)/robotest-internal.mk
177    # Clear temporary variables
178    my_failure_fatal :=
179    my_jacoco_agent_jar :=
180    my_jacoco_agent_args :=
181    my_jacoco_excludes :=
182    my_java_args :=
183    my_phony_target :=
184    my_robolectric_jars :=
185    my_target :=
186    my_tests :=
187    my_filename_stem :=
188
189    # Target for generating code coverage reports using jacoco.exec
190    my_target := $(LOCAL_BUILT_MODULE)-jacoco
191    $(my_report_target): $(my_target)
192
193    # The JAR file containing the report generation tool.
194    my_coverage_report_class := com.google.android.jacoco.reporter.ReportGenerator
195    my_coverage_report_jar := $(call java-lib-files,jvm-jacoco-reporter,true)
196    my_coverage_srcs_jars := $(my_srcs_jars)
197    my_coverage_report_dist_file := $(my_report_target)-html.zip
198
199    ## jacoco code coverage reports
200    include $(my_robolectric_script_path)/report-internal.mk
201    # Clear temporary variables
202    my_coverage_dir :=
203    my_coverage_file :=
204    my_coverage_report_class :=
205    my_coverage_report_dist_file :=
206    my_coverage_report_jar :=
207    my_coverage_srcs_jars :=
208    my_robolectric_script_path :=
209    my_robolectric_path :=
210    my_srcs_jars :=
211    my_target :=
212    my_collect_file :=
213endif
214