| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. | 
 | 2 | // | 
 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 4 | // you may not use this file except in compliance with the License. | 
 | 5 | // You may obtain a copy of the License at | 
 | 6 | // | 
 | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 8 | // | 
 | 9 | // Unless required by applicable law or agreed to in writing, software | 
 | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 12 | // See the License for the specific language governing permissions and | 
 | 13 | // limitations under the License. | 
 | 14 |  | 
 | 15 | package java | 
 | 16 |  | 
 | 17 | import ( | 
 | 18 | 	"fmt" | 
 | 19 | 	"io" | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 20 | 	"strconv" | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 21 | 	"strings" | 
 | 22 |  | 
 | 23 | 	"android/soong/android" | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 24 | 	"android/soong/java/config" | 
 | 25 | 	"android/soong/tradefed" | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 26 | ) | 
 | 27 |  | 
 | 28 | func init() { | 
 | 29 | 	android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 30 | 	android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 31 | } | 
 | 32 |  | 
 | 33 | var robolectricDefaultLibs = []string{ | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 34 | 	"mockito-robolectric-prebuilt", | 
 | 35 | 	"truth-prebuilt", | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 36 | 	// TODO(ccross): this is not needed at link time | 
 | 37 | 	"junitxml", | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 38 | } | 
 | 39 |  | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 40 | const robolectricCurrentLib = "Robolectric_all-target" | 
 | 41 | const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt" | 
 | 42 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 43 | var ( | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 44 | 	roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"} | 
 | 45 | 	roboRuntimesTag     = dependencyTag{name: "roboRuntimes"} | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 46 | ) | 
 | 47 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 48 | type robolectricProperties struct { | 
 | 49 | 	// The name of the android_app module that the tests will run against. | 
 | 50 | 	Instrumentation_for *string | 
 | 51 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 52 | 	// Additional libraries for which coverage data should be generated | 
 | 53 | 	Coverage_libs []string | 
 | 54 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 55 | 	Test_options struct { | 
 | 56 | 		// Timeout in seconds when running the tests. | 
| Colin Cross | 2f9a7c8 | 2019-05-30 11:16:26 -0700 | [diff] [blame] | 57 | 		Timeout *int64 | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 58 |  | 
 | 59 | 		// Number of shards to use when running the tests. | 
 | 60 | 		Shards *int64 | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 61 | 	} | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 62 |  | 
 | 63 | 	// The version number of a robolectric prebuilt to use from prebuilts/misc/common/robolectric | 
 | 64 | 	// instead of the one built from source in external/robolectric-shadows. | 
 | 65 | 	Robolectric_prebuilt_version *string | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 66 | } | 
 | 67 |  | 
 | 68 | type robolectricTest struct { | 
 | 69 | 	Library | 
 | 70 |  | 
 | 71 | 	robolectricProperties robolectricProperties | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 72 | 	testProperties        testProperties | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 73 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 74 | 	libs  []string | 
 | 75 | 	tests []string | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 76 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 77 | 	manifest    android.Path | 
 | 78 | 	resourceApk android.Path | 
 | 79 |  | 
 | 80 | 	combinedJar android.WritablePath | 
 | 81 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 82 | 	roboSrcJar android.Path | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 83 |  | 
 | 84 | 	testConfig android.Path | 
 | 85 | 	data       android.Paths | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 86 | } | 
 | 87 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 88 | func (r *robolectricTest) TestSuites() []string { | 
 | 89 | 	return r.testProperties.Test_suites | 
 | 90 | } | 
 | 91 |  | 
 | 92 | var _ android.TestSuiteModule = (*robolectricTest)(nil) | 
 | 93 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 94 | func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) { | 
 | 95 | 	r.Library.DepsMutator(ctx) | 
 | 96 |  | 
 | 97 | 	if r.robolectricProperties.Instrumentation_for != nil { | 
 | 98 | 		ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for)) | 
 | 99 | 	} else { | 
 | 100 | 		ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module") | 
 | 101 | 	} | 
 | 102 |  | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 103 | 	if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" { | 
 | 104 | 		ctx.AddVariationDependencies(nil, libTag, fmt.Sprintf(robolectricPrebuiltLibPattern, v)) | 
 | 105 | 	} else { | 
 | 106 | 		ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib) | 
 | 107 | 	} | 
 | 108 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 109 | 	ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...) | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 110 |  | 
 | 111 | 	ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 112 |  | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 113 | 	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), | 
 | 114 | 		roboRuntimesTag, "robolectric-android-all-prebuilts") | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 115 | } | 
 | 116 |  | 
 | 117 | func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 118 | 	r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config, | 
 | 119 | 		r.testProperties.Test_config_template, r.testProperties.Test_suites, | 
 | 120 | 		r.testProperties.Auto_gen_config) | 
 | 121 | 	r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data) | 
 | 122 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 123 | 	roboTestConfig := android.PathForModuleGen(ctx, "robolectric"). | 
 | 124 | 		Join(ctx, "com/android/tools/test_config.properties") | 
 | 125 |  | 
 | 126 | 	// TODO: this inserts paths to built files into the test, it should really be inserting the contents. | 
 | 127 | 	instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag) | 
 | 128 |  | 
 | 129 | 	if len(instrumented) != 1 { | 
 | 130 | 		panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented))) | 
 | 131 | 	} | 
 | 132 |  | 
 | 133 | 	instrumentedApp, ok := instrumented[0].(*AndroidApp) | 
 | 134 | 	if !ok { | 
 | 135 | 		ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app") | 
 | 136 | 	} | 
 | 137 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 138 | 	r.manifest = instrumentedApp.mergedManifestFile | 
 | 139 | 	r.resourceApk = instrumentedApp.outputFile | 
 | 140 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 141 | 	generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp) | 
 | 142 | 	r.extraResources = android.Paths{roboTestConfig} | 
 | 143 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 144 | 	r.Library.GenerateAndroidBuildActions(ctx) | 
 | 145 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 146 | 	roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar") | 
 | 147 | 	r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp) | 
 | 148 | 	r.roboSrcJar = roboSrcJar | 
 | 149 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 150 | 	roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar") | 
 | 151 | 	generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar) | 
 | 152 |  | 
 | 153 | 	combinedJarJars := android.Paths{ | 
 | 154 | 		// roboTestConfigJar comes first so that its com/android/tools/test_config.properties | 
 | 155 | 		// overrides the one from r.extraResources.  The r.extraResources one can be removed | 
 | 156 | 		// once the Make test runner is removed. | 
 | 157 | 		roboTestConfigJar, | 
 | 158 | 		r.outputFile, | 
 | 159 | 		instrumentedApp.implementationAndResourcesJar, | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 160 | 	} | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 161 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 162 | 	for _, dep := range ctx.GetDirectDepsWithTag(libTag) { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 163 | 		m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) | 
 | 164 | 		r.libs = append(r.libs, ctx.OtherModuleName(dep)) | 
 | 165 | 		if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) { | 
 | 166 | 			combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 167 | 		} | 
 | 168 | 	} | 
 | 169 |  | 
 | 170 | 	r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base()) | 
 | 171 | 	TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{}, | 
 | 172 | 		false, nil, nil) | 
 | 173 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 174 | 	// TODO: this could all be removed if tradefed was used as the test runner, it will find everything | 
 | 175 | 	// annotated as a test and run it. | 
 | 176 | 	for _, src := range r.compiledJavaSrcs { | 
 | 177 | 		s := src.Rel() | 
 | 178 | 		if !strings.HasSuffix(s, "Test.java") { | 
 | 179 | 			continue | 
 | 180 | 		} else if strings.HasSuffix(s, "/BaseRobolectricTest.java") { | 
 | 181 | 			continue | 
 | 182 | 		} else if strings.HasPrefix(s, "src/") { | 
 | 183 | 			s = strings.TrimPrefix(s, "src/") | 
 | 184 | 		} | 
 | 185 | 		r.tests = append(r.tests, s) | 
 | 186 | 	} | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 187 |  | 
 | 188 | 	r.data = append(r.data, r.manifest, r.resourceApk) | 
 | 189 |  | 
 | 190 | 	runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag) | 
 | 191 |  | 
 | 192 | 	installPath := android.PathForModuleInstall(ctx, r.BaseModuleName()) | 
 | 193 |  | 
 | 194 | 	installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk) | 
 | 195 | 	installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest) | 
 | 196 | 	installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig) | 
 | 197 |  | 
 | 198 | 	var installDeps android.Paths | 
 | 199 | 	for _, runtime := range runtimes.(*robolectricRuntimes).runtimes { | 
 | 200 | 		installDeps = append(installDeps, runtime) | 
 | 201 | 	} | 
 | 202 | 	installDeps = append(installDeps, installedResourceApk, installedManifest, installedConfig) | 
 | 203 |  | 
 | 204 | 	for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) { | 
 | 205 | 		installedData := ctx.InstallFile(installPath, data.Rel(), data) | 
 | 206 | 		installDeps = append(installDeps, installedData) | 
 | 207 | 	} | 
 | 208 |  | 
 | 209 | 	ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...) | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 210 | } | 
 | 211 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 212 | func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath, | 
 | 213 | 	instrumentedApp *AndroidApp) { | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 214 | 	rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 215 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 216 | 	manifest := instrumentedApp.mergedManifestFile | 
 | 217 | 	resourceApk := instrumentedApp.outputFile | 
 | 218 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 219 | 	rule.Command().Text("rm -f").Output(outputFile) | 
 | 220 | 	rule.Command(). | 
 | 221 | 		Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&"). | 
 | 222 | 		Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile). | 
 | 223 | 		// Make it depend on the files to which it points so the test file's timestamp is updated whenever the | 
 | 224 | 		// contents change | 
 | 225 | 		Implicit(manifest). | 
 | 226 | 		Implicit(resourceApk) | 
 | 227 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 228 | 	rule.Build("generate_test_config", "generate test_config.properties") | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 229 | } | 
 | 230 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 231 | func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) { | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 232 | 	rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 233 |  | 
 | 234 | 	outputDir := outputFile.InSameDir(ctx) | 
 | 235 | 	configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties") | 
 | 236 | 	rule.Temporary(configFile) | 
 | 237 | 	rule.Command().Text("rm -f").Output(outputFile).Output(configFile) | 
 | 238 | 	rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String()) | 
 | 239 | 	rule.Command(). | 
 | 240 | 		Text("("). | 
 | 241 | 		Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()). | 
 | 242 | 		Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()). | 
 | 243 | 		Text(") >>").Output(configFile) | 
 | 244 | 	rule.Command(). | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 245 | 		BuiltTool("soong_zip"). | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 246 | 		FlagWithArg("-C ", outputDir.String()). | 
 | 247 | 		FlagWithInput("-f ", configFile). | 
 | 248 | 		FlagWithOutput("-o ", outputFile) | 
 | 249 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 250 | 	rule.Build("generate_test_config_samedir", "generate test_config.properties") | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 251 | } | 
 | 252 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 253 | func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath, | 
 | 254 | 	instrumentedApp *AndroidApp) { | 
 | 255 |  | 
 | 256 | 	srcJarArgs := copyOf(instrumentedApp.srcJarArgs) | 
 | 257 | 	srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...) | 
 | 258 |  | 
 | 259 | 	for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 260 | 		if ctx.OtherModuleHasProvider(m, JavaInfoProvider) { | 
 | 261 | 			dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo) | 
 | 262 | 			srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...) | 
 | 263 | 			srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...) | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 264 | 		} | 
 | 265 | 	} | 
 | 266 |  | 
 | 267 | 	TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps) | 
 | 268 | } | 
 | 269 |  | 
| Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 270 | func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries { | 
 | 271 | 	entriesList := r.Library.AndroidMkEntries() | 
 | 272 | 	entries := &entriesList[0] | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 273 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 274 | 	entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{ | 
| Jaewoong Jung | 02b11a6 | 2020-12-07 10:23:54 -0800 | [diff] [blame] | 275 | 		func(w io.Writer, name, prefix, moduleDir string) { | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 276 | 			if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 { | 
| Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 277 | 				numShards := int(*s) | 
 | 278 | 				shardSize := (len(r.tests) + numShards - 1) / numShards | 
 | 279 | 				shards := android.ShardStrings(r.tests, shardSize) | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 280 | 				for i, shard := range shards { | 
 | 281 | 					r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard) | 
 | 282 | 				} | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 283 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 284 | 				// TODO: add rules to dist the outputs of the individual tests, or combine them together? | 
 | 285 | 				fmt.Fprintln(w, "") | 
 | 286 | 				fmt.Fprintln(w, ".PHONY:", "Run"+name) | 
 | 287 | 				fmt.Fprintln(w, "Run"+name, ": \\") | 
 | 288 | 				for i := range shards { | 
 | 289 | 					fmt.Fprintln(w, "   ", "Run"+name+strconv.Itoa(i), "\\") | 
 | 290 | 				} | 
 | 291 | 				fmt.Fprintln(w, "") | 
 | 292 | 			} else { | 
 | 293 | 				r.writeTestRunner(w, name, "Run"+name, r.tests) | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 294 | 			} | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 295 | 		}, | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 296 | 	} | 
 | 297 |  | 
| Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 298 | 	return entriesList | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 299 | } | 
 | 300 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 301 | func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) { | 
 | 302 | 	fmt.Fprintln(w, "") | 
 | 303 | 	fmt.Fprintln(w, "include $(CLEAR_VARS)") | 
 | 304 | 	fmt.Fprintln(w, "LOCAL_MODULE :=", name) | 
 | 305 | 	fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", module) | 
 | 306 | 	fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " ")) | 
 | 307 | 	fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for)) | 
 | 308 | 	fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String()) | 
 | 309 | 	fmt.Fprintln(w, "LOCAL_ROBOTEST_FILES :=", strings.Join(tests, " ")) | 
 | 310 | 	if t := r.robolectricProperties.Test_options.Timeout; t != nil { | 
 | 311 | 		fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t) | 
 | 312 | 	} | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 313 | 	if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" { | 
 | 314 | 		fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v) | 
 | 315 | 	} else { | 
 | 316 | 		fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk") | 
 | 317 | 	} | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 318 | } | 
 | 319 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 320 | // An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host | 
 | 321 | // instead of on a device.  It also generates a rule with the name of the module prefixed with "Run" that can be | 
 | 322 | // used to run the tests.  Running the tests with build rule will eventually be deprecated and replaced with atest. | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 323 | // | 
 | 324 | // The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless | 
 | 325 | // it is named BaseRobolectricTest.java.  The path to the each source file must exactly match the package | 
 | 326 | // name, or match the package name when the prefix "src/" is removed. | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 327 | func RobolectricTestFactory() android.Module { | 
 | 328 | 	module := &robolectricTest{} | 
 | 329 |  | 
| Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 330 | 	module.addHostProperties() | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 331 | 	module.AddProperties( | 
| Colin Cross | e323f3c | 2019-09-17 15:34:09 -0700 | [diff] [blame] | 332 | 		&module.Module.deviceProperties, | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 333 | 		&module.robolectricProperties, | 
 | 334 | 		&module.testProperties) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 335 |  | 
 | 336 | 	module.Module.dexpreopter.isTest = true | 
| Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 337 | 	module.Module.linter.test = true | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 338 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 339 | 	module.testProperties.Test_suites = []string{"robolectric-tests"} | 
 | 340 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 341 | 	InitJavaModule(module, android.DeviceSupported) | 
 | 342 | 	return module | 
 | 343 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 344 |  | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 345 | func (r *robolectricTest) InstallBypassMake() bool  { return true } | 
 | 346 | func (r *robolectricTest) InstallInTestcases() bool { return true } | 
 | 347 | func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) { | 
 | 348 | 	return &android.BuildOs, &android.BuildArch | 
 | 349 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 350 |  | 
 | 351 | func robolectricRuntimesFactory() android.Module { | 
 | 352 | 	module := &robolectricRuntimes{} | 
 | 353 | 	module.AddProperties(&module.props) | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 354 | 	android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 355 | 	return module | 
 | 356 | } | 
 | 357 |  | 
 | 358 | type robolectricRuntimesProperties struct { | 
 | 359 | 	Jars []string `android:"path"` | 
 | 360 | 	Lib  *string | 
 | 361 | } | 
 | 362 |  | 
 | 363 | type robolectricRuntimes struct { | 
 | 364 | 	android.ModuleBase | 
 | 365 |  | 
 | 366 | 	props robolectricRuntimesProperties | 
 | 367 |  | 
 | 368 | 	runtimes []android.InstallPath | 
 | 369 | } | 
 | 370 |  | 
 | 371 | func (r *robolectricRuntimes) TestSuites() []string { | 
 | 372 | 	return []string{"robolectric-tests"} | 
 | 373 | } | 
 | 374 |  | 
 | 375 | var _ android.TestSuiteModule = (*robolectricRuntimes)(nil) | 
 | 376 |  | 
 | 377 | func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 378 | 	if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 379 | 		ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib)) | 
 | 380 | 	} | 
 | 381 | } | 
 | 382 |  | 
 | 383 | func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 384 | 	if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os { | 
 | 385 | 		return | 
 | 386 | 	} | 
 | 387 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 388 | 	files := android.PathsForModuleSrc(ctx, r.props.Jars) | 
 | 389 |  | 
 | 390 | 	androidAllDir := android.PathForModuleInstall(ctx, "android-all") | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 391 | 	for _, from := range files { | 
 | 392 | 		installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from) | 
 | 393 | 		r.runtimes = append(r.runtimes, installedRuntime) | 
 | 394 | 	} | 
 | 395 |  | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 396 | 	if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 397 | 		runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag) | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 398 | 		if runtimeFromSourceModule == nil { | 
 | 399 | 			if ctx.Config().AllowMissingDependencies() { | 
 | 400 | 				ctx.AddMissingDependencies([]string{String(r.props.Lib)}) | 
 | 401 | 			} else { | 
 | 402 | 				ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib)) | 
 | 403 | 			} | 
 | 404 | 			return | 
 | 405 | 		} | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 406 | 		runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "") | 
 | 407 |  | 
 | 408 | 		runtimeName := fmt.Sprintf("android-all-%s-robolectric-r0.jar", | 
| Joseph Murphy | fdb9f7f | 2020-11-19 00:59:38 +0000 | [diff] [blame] | 409 | 			    ctx.Config().PlatformSdkCodename()) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 410 | 		installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar) | 
 | 411 | 		r.runtimes = append(r.runtimes, installedRuntime) | 
 | 412 | 	} | 
 | 413 | } | 
 | 414 |  | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 415 | func (r *robolectricRuntimes) InstallBypassMake() bool  { return true } | 
 | 416 | func (r *robolectricRuntimes) InstallInTestcases() bool { return true } | 
 | 417 | func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) { | 
 | 418 | 	return &android.BuildOs, &android.BuildArch | 
 | 419 | } |