| 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" | 
| Aditya Choudhary | 9b59352 | 2023-10-06 19:54:58 +0000 | [diff] [blame] | 25 | 	"android/soong/testing" | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 26 | 	"android/soong/tradefed" | 
| Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 27 |  | 
| Cole Faust | d57e8b2 | 2022-08-11 11:59:04 -0700 | [diff] [blame] | 28 | 	"github.com/google/blueprint/proptools" | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 29 | ) | 
 | 30 |  | 
 | 31 | func init() { | 
 | 32 | 	android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 33 | 	android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 34 | } | 
 | 35 |  | 
 | 36 | var robolectricDefaultLibs = []string{ | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 37 | 	"mockito-robolectric-prebuilt", | 
| Krzysztof KosiĆski | 5a55439 | 2023-10-07 19:59:58 +0000 | [diff] [blame] | 38 | 	"truth", | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 39 | 	// TODO(ccross): this is not needed at link time | 
 | 40 | 	"junitxml", | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 41 | } | 
 | 42 |  | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 43 | const robolectricCurrentLib = "Robolectric_all-target" | 
 | 44 | const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt" | 
 | 45 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 46 | var ( | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 47 | 	roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"} | 
 | 48 | 	roboRuntimesTag     = dependencyTag{name: "roboRuntimes"} | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 49 | ) | 
 | 50 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 51 | type robolectricProperties struct { | 
 | 52 | 	// The name of the android_app module that the tests will run against. | 
 | 53 | 	Instrumentation_for *string | 
 | 54 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 55 | 	// Additional libraries for which coverage data should be generated | 
 | 56 | 	Coverage_libs []string | 
 | 57 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 58 | 	Test_options struct { | 
 | 59 | 		// Timeout in seconds when running the tests. | 
| Colin Cross | 2f9a7c8 | 2019-05-30 11:16:26 -0700 | [diff] [blame] | 60 | 		Timeout *int64 | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 61 |  | 
 | 62 | 		// Number of shards to use when running the tests. | 
 | 63 | 		Shards *int64 | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 64 | 	} | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 65 |  | 
 | 66 | 	// The version number of a robolectric prebuilt to use from prebuilts/misc/common/robolectric | 
 | 67 | 	// instead of the one built from source in external/robolectric-shadows. | 
 | 68 | 	Robolectric_prebuilt_version *string | 
| Rex Hoffman | 54641d2 | 2022-08-25 17:29:50 +0000 | [diff] [blame] | 69 |  | 
 | 70 | 	// Use /external/robolectric rather than /external/robolectric-shadows as the version of robolectri | 
 | 71 | 	// to use.  /external/robolectric closely tracks github's master, and will fully replace /external/robolectric-shadows | 
 | 72 | 	Upstream *bool | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 73 | } | 
 | 74 |  | 
 | 75 | type robolectricTest struct { | 
 | 76 | 	Library | 
 | 77 |  | 
 | 78 | 	robolectricProperties robolectricProperties | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 79 | 	testProperties        testProperties | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 80 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 81 | 	libs  []string | 
 | 82 | 	tests []string | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 83 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 84 | 	manifest    android.Path | 
 | 85 | 	resourceApk android.Path | 
 | 86 |  | 
 | 87 | 	combinedJar android.WritablePath | 
 | 88 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 89 | 	roboSrcJar android.Path | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 90 |  | 
 | 91 | 	testConfig android.Path | 
 | 92 | 	data       android.Paths | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 93 |  | 
 | 94 | 	forceOSType   android.OsType | 
 | 95 | 	forceArchType android.ArchType | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 96 | } | 
 | 97 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 98 | func (r *robolectricTest) TestSuites() []string { | 
 | 99 | 	return r.testProperties.Test_suites | 
 | 100 | } | 
 | 101 |  | 
 | 102 | var _ android.TestSuiteModule = (*robolectricTest)(nil) | 
 | 103 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 104 | func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) { | 
 | 105 | 	r.Library.DepsMutator(ctx) | 
 | 106 |  | 
 | 107 | 	if r.robolectricProperties.Instrumentation_for != nil { | 
 | 108 | 		ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for)) | 
 | 109 | 	} else { | 
 | 110 | 		ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module") | 
 | 111 | 	} | 
 | 112 |  | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 113 | 	if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" { | 
 | 114 | 		ctx.AddVariationDependencies(nil, libTag, fmt.Sprintf(robolectricPrebuiltLibPattern, v)) | 
 | 115 | 	} else { | 
| Rex Hoffman | 54641d2 | 2022-08-25 17:29:50 +0000 | [diff] [blame] | 116 | 		if proptools.Bool(r.robolectricProperties.Upstream) { | 
 | 117 | 			ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib+"_upstream") | 
 | 118 | 		} else { | 
 | 119 | 			ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib) | 
 | 120 | 		} | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 121 | 	} | 
 | 122 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 123 | 	ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...) | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 124 |  | 
 | 125 | 	ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 126 |  | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 127 | 	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), | 
 | 128 | 		roboRuntimesTag, "robolectric-android-all-prebuilts") | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 129 | } | 
 | 130 |  | 
 | 131 | func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 132 | 	r.forceOSType = ctx.Config().BuildOS | 
 | 133 | 	r.forceArchType = ctx.Config().BuildArch | 
 | 134 |  | 
| Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 135 | 	r.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ | 
 | 136 | 		TestConfigProp:         r.testProperties.Test_config, | 
 | 137 | 		TestConfigTemplateProp: r.testProperties.Test_config_template, | 
 | 138 | 		TestSuites:             r.testProperties.Test_suites, | 
 | 139 | 		AutoGenConfig:          r.testProperties.Auto_gen_config, | 
 | 140 | 		DeviceTemplate:         "${RobolectricTestConfigTemplate}", | 
 | 141 | 		HostTemplate:           "${RobolectricTestConfigTemplate}", | 
 | 142 | 	}) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 143 | 	r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data) | 
 | 144 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 145 | 	roboTestConfig := android.PathForModuleGen(ctx, "robolectric"). | 
 | 146 | 		Join(ctx, "com/android/tools/test_config.properties") | 
 | 147 |  | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 148 | 	var ok bool | 
 | 149 | 	var instrumentedApp *AndroidApp | 
 | 150 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 151 | 	// TODO: this inserts paths to built files into the test, it should really be inserting the contents. | 
 | 152 | 	instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag) | 
 | 153 |  | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 154 | 	if len(instrumented) == 1 { | 
 | 155 | 		instrumentedApp, ok = instrumented[0].(*AndroidApp) | 
 | 156 | 		if !ok { | 
 | 157 | 			ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app") | 
 | 158 | 		} | 
 | 159 | 	} else if !ctx.Config().AllowMissingDependencies() { | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 160 | 		panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented))) | 
 | 161 | 	} | 
 | 162 |  | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 163 | 	if instrumentedApp != nil { | 
 | 164 | 		r.manifest = instrumentedApp.mergedManifestFile | 
 | 165 | 		r.resourceApk = instrumentedApp.outputFile | 
 | 166 |  | 
 | 167 | 		generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp) | 
 | 168 | 		r.extraResources = android.Paths{roboTestConfig} | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 169 | 	} | 
 | 170 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 171 | 	r.Library.GenerateAndroidBuildActions(ctx) | 
 | 172 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 173 | 	roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar") | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 174 |  | 
 | 175 | 	if instrumentedApp != nil { | 
 | 176 | 		r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp) | 
 | 177 | 		r.roboSrcJar = roboSrcJar | 
 | 178 | 	} | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 179 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 180 | 	roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar") | 
 | 181 | 	generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar) | 
 | 182 |  | 
 | 183 | 	combinedJarJars := android.Paths{ | 
 | 184 | 		// roboTestConfigJar comes first so that its com/android/tools/test_config.properties | 
 | 185 | 		// overrides the one from r.extraResources.  The r.extraResources one can be removed | 
 | 186 | 		// once the Make test runner is removed. | 
 | 187 | 		roboTestConfigJar, | 
 | 188 | 		r.outputFile, | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 189 | 	} | 
 | 190 |  | 
 | 191 | 	if instrumentedApp != nil { | 
 | 192 | 		combinedJarJars = append(combinedJarJars, instrumentedApp.implementationAndResourcesJar) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 193 | 	} | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 194 |  | 
| Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 195 | 	handleLibDeps := func(dep android.Module) { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 196 | 		m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) | 
 | 197 | 		r.libs = append(r.libs, ctx.OtherModuleName(dep)) | 
 | 198 | 		if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) { | 
 | 199 | 			combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 200 | 		} | 
 | 201 | 	} | 
 | 202 |  | 
| Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 203 | 	for _, dep := range ctx.GetDirectDepsWithTag(libTag) { | 
 | 204 | 		handleLibDeps(dep) | 
 | 205 | 	} | 
 | 206 | 	for _, dep := range ctx.GetDirectDepsWithTag(sdkLibTag) { | 
 | 207 | 		handleLibDeps(dep) | 
 | 208 | 	} | 
 | 209 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 210 | 	r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base()) | 
 | 211 | 	TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{}, | 
 | 212 | 		false, nil, nil) | 
 | 213 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 214 | 	// TODO: this could all be removed if tradefed was used as the test runner, it will find everything | 
 | 215 | 	// annotated as a test and run it. | 
| Chaohui Wang | dcbe33c | 2022-10-11 11:13:30 +0800 | [diff] [blame] | 216 | 	for _, src := range r.uniqueSrcFiles { | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 217 | 		s := src.Rel() | 
| Chaohui Wang | dcbe33c | 2022-10-11 11:13:30 +0800 | [diff] [blame] | 218 | 		if !strings.HasSuffix(s, "Test.java") && !strings.HasSuffix(s, "Test.kt") { | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 219 | 			continue | 
 | 220 | 		} else if strings.HasSuffix(s, "/BaseRobolectricTest.java") { | 
 | 221 | 			continue | 
| Ulya Trafimovich | 497a093 | 2021-07-14 16:35:33 +0100 | [diff] [blame] | 222 | 		} else { | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 223 | 			s = strings.TrimPrefix(s, "src/") | 
 | 224 | 		} | 
 | 225 | 		r.tests = append(r.tests, s) | 
 | 226 | 	} | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 227 |  | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 228 | 	installPath := android.PathForModuleInstall(ctx, r.BaseModuleName()) | 
| Colin Cross | 09ad3a6 | 2023-11-15 12:29:33 -0800 | [diff] [blame] | 229 | 	var installDeps android.InstallPaths | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 230 |  | 
 | 231 | 	if r.manifest != nil { | 
 | 232 | 		r.data = append(r.data, r.manifest) | 
 | 233 | 		installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest) | 
 | 234 | 		installDeps = append(installDeps, installedManifest) | 
 | 235 | 	} | 
 | 236 |  | 
 | 237 | 	if r.resourceApk != nil { | 
 | 238 | 		r.data = append(r.data, r.resourceApk) | 
 | 239 | 		installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk) | 
 | 240 | 		installDeps = append(installDeps, installedResourceApk) | 
 | 241 | 	} | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 242 |  | 
 | 243 | 	runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 244 | 	for _, runtime := range runtimes.(*robolectricRuntimes).runtimes { | 
 | 245 | 		installDeps = append(installDeps, runtime) | 
 | 246 | 	} | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 247 |  | 
 | 248 | 	installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig) | 
 | 249 | 	installDeps = append(installDeps, installedConfig) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 250 |  | 
 | 251 | 	for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) { | 
 | 252 | 		installedData := ctx.InstallFile(installPath, data.Rel(), data) | 
 | 253 | 		installDeps = append(installDeps, installedData) | 
 | 254 | 	} | 
 | 255 |  | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 256 | 	r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...) | 
| Aditya Choudhary | 9b59352 | 2023-10-06 19:54:58 +0000 | [diff] [blame] | 257 | 	ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{}) | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 258 | } | 
 | 259 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 260 | func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath, | 
 | 261 | 	instrumentedApp *AndroidApp) { | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 262 | 	rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 263 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 264 | 	manifest := instrumentedApp.mergedManifestFile | 
 | 265 | 	resourceApk := instrumentedApp.outputFile | 
 | 266 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 267 | 	rule.Command().Text("rm -f").Output(outputFile) | 
 | 268 | 	rule.Command(). | 
 | 269 | 		Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&"). | 
 | 270 | 		Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile). | 
 | 271 | 		// Make it depend on the files to which it points so the test file's timestamp is updated whenever the | 
 | 272 | 		// contents change | 
 | 273 | 		Implicit(manifest). | 
 | 274 | 		Implicit(resourceApk) | 
 | 275 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 276 | 	rule.Build("generate_test_config", "generate test_config.properties") | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 277 | } | 
 | 278 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 279 | func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) { | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 280 | 	rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 281 |  | 
 | 282 | 	outputDir := outputFile.InSameDir(ctx) | 
 | 283 | 	configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties") | 
 | 284 | 	rule.Temporary(configFile) | 
 | 285 | 	rule.Command().Text("rm -f").Output(outputFile).Output(configFile) | 
 | 286 | 	rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String()) | 
 | 287 | 	rule.Command(). | 
 | 288 | 		Text("("). | 
 | 289 | 		Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()). | 
 | 290 | 		Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()). | 
 | 291 | 		Text(") >>").Output(configFile) | 
 | 292 | 	rule.Command(). | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 293 | 		BuiltTool("soong_zip"). | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 294 | 		FlagWithArg("-C ", outputDir.String()). | 
 | 295 | 		FlagWithInput("-f ", configFile). | 
 | 296 | 		FlagWithOutput("-o ", outputFile) | 
 | 297 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 298 | 	rule.Build("generate_test_config_samedir", "generate test_config.properties") | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 299 | } | 
 | 300 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 301 | func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath, | 
 | 302 | 	instrumentedApp *AndroidApp) { | 
 | 303 |  | 
| Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 304 | 	srcJarArgs := android.CopyOf(instrumentedApp.srcJarArgs) | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 305 | 	srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...) | 
 | 306 |  | 
 | 307 | 	for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 308 | 		if ctx.OtherModuleHasProvider(m, JavaInfoProvider) { | 
 | 309 | 			dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo) | 
 | 310 | 			srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...) | 
 | 311 | 			srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...) | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 312 | 		} | 
 | 313 | 	} | 
 | 314 |  | 
 | 315 | 	TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps) | 
 | 316 | } | 
 | 317 |  | 
| Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 318 | func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries { | 
 | 319 | 	entriesList := r.Library.AndroidMkEntries() | 
 | 320 | 	entries := &entriesList[0] | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 321 | 	entries.ExtraEntries = append(entries.ExtraEntries, | 
 | 322 | 		func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { | 
 | 323 | 			entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true) | 
| nelsonli | 3f97ea2 | 2022-11-21 21:37:56 +0800 | [diff] [blame] | 324 | 			entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", "robolectric-tests") | 
| Yike | 0fcf90a | 2023-02-14 12:17:32 +0800 | [diff] [blame] | 325 | 			if r.testConfig != nil { | 
 | 326 | 				entries.SetPath("LOCAL_FULL_TEST_CONFIG", r.testConfig) | 
 | 327 | 			} | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 328 | 		}) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 329 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 330 | 	entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{ | 
| Jaewoong Jung | 02b11a6 | 2020-12-07 10:23:54 -0800 | [diff] [blame] | 331 | 		func(w io.Writer, name, prefix, moduleDir string) { | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 332 | 			if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 { | 
| Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 333 | 				numShards := int(*s) | 
 | 334 | 				shardSize := (len(r.tests) + numShards - 1) / numShards | 
 | 335 | 				shards := android.ShardStrings(r.tests, shardSize) | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 336 | 				for i, shard := range shards { | 
 | 337 | 					r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard) | 
 | 338 | 				} | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 339 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 340 | 				// TODO: add rules to dist the outputs of the individual tests, or combine them together? | 
 | 341 | 				fmt.Fprintln(w, "") | 
 | 342 | 				fmt.Fprintln(w, ".PHONY:", "Run"+name) | 
 | 343 | 				fmt.Fprintln(w, "Run"+name, ": \\") | 
 | 344 | 				for i := range shards { | 
 | 345 | 					fmt.Fprintln(w, "   ", "Run"+name+strconv.Itoa(i), "\\") | 
 | 346 | 				} | 
 | 347 | 				fmt.Fprintln(w, "") | 
 | 348 | 			} else { | 
 | 349 | 				r.writeTestRunner(w, name, "Run"+name, r.tests) | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 350 | 			} | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 351 | 		}, | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 352 | 	} | 
 | 353 |  | 
| Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 354 | 	return entriesList | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 355 | } | 
 | 356 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 357 | func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) { | 
 | 358 | 	fmt.Fprintln(w, "") | 
| Sasha Smundak | 5c4729d | 2022-12-01 10:49:23 -0800 | [diff] [blame] | 359 | 	fmt.Fprintln(w, "include $(CLEAR_VARS)", " # java.robolectricTest") | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 360 | 	fmt.Fprintln(w, "LOCAL_MODULE :=", name) | 
| Sasha Smundak | dcb6129 | 2022-12-08 10:41:33 -0800 | [diff] [blame] | 361 | 	android.AndroidMkEmitAssignList(w, "LOCAL_JAVA_LIBRARIES", []string{module}, r.libs) | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 362 | 	fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for)) | 
| Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 363 | 	if r.roboSrcJar != nil { | 
 | 364 | 		fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String()) | 
 | 365 | 	} | 
| Sasha Smundak | dcb6129 | 2022-12-08 10:41:33 -0800 | [diff] [blame] | 366 | 	android.AndroidMkEmitAssignList(w, "LOCAL_ROBOTEST_FILES", tests) | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 367 | 	if t := r.robolectricProperties.Test_options.Timeout; t != nil { | 
 | 368 | 		fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t) | 
 | 369 | 	} | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 370 | 	if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" { | 
 | 371 | 		fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v) | 
 | 372 | 	} else { | 
 | 373 | 		fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk") | 
 | 374 | 	} | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 375 | } | 
 | 376 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 377 | // An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host | 
 | 378 | // instead of on a device.  It also generates a rule with the name of the module prefixed with "Run" that can be | 
 | 379 | // 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] | 380 | // | 
 | 381 | // The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless | 
 | 382 | // it is named BaseRobolectricTest.java.  The path to the each source file must exactly match the package | 
 | 383 | // name, or match the package name when the prefix "src/" is removed. | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 384 | func RobolectricTestFactory() android.Module { | 
 | 385 | 	module := &robolectricTest{} | 
 | 386 |  | 
| Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 387 | 	module.addHostProperties() | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 388 | 	module.AddProperties( | 
| Colin Cross | e323f3c | 2019-09-17 15:34:09 -0700 | [diff] [blame] | 389 | 		&module.Module.deviceProperties, | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 390 | 		&module.robolectricProperties, | 
 | 391 | 		&module.testProperties) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 392 |  | 
 | 393 | 	module.Module.dexpreopter.isTest = true | 
| Cole Faust | d57e8b2 | 2022-08-11 11:59:04 -0700 | [diff] [blame] | 394 | 	module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 395 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 396 | 	module.testProperties.Test_suites = []string{"robolectric-tests"} | 
 | 397 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 398 | 	InitJavaModule(module, android.DeviceSupported) | 
 | 399 | 	return module | 
 | 400 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 401 |  | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 402 | func (r *robolectricTest) InstallInTestcases() bool { return true } | 
 | 403 | func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) { | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 404 | 	return &r.forceOSType, &r.forceArchType | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 405 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 406 |  | 
 | 407 | func robolectricRuntimesFactory() android.Module { | 
 | 408 | 	module := &robolectricRuntimes{} | 
 | 409 | 	module.AddProperties(&module.props) | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 410 | 	android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 411 | 	return module | 
 | 412 | } | 
 | 413 |  | 
 | 414 | type robolectricRuntimesProperties struct { | 
 | 415 | 	Jars []string `android:"path"` | 
 | 416 | 	Lib  *string | 
 | 417 | } | 
 | 418 |  | 
 | 419 | type robolectricRuntimes struct { | 
 | 420 | 	android.ModuleBase | 
 | 421 |  | 
 | 422 | 	props robolectricRuntimesProperties | 
 | 423 |  | 
 | 424 | 	runtimes []android.InstallPath | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 425 |  | 
 | 426 | 	forceOSType   android.OsType | 
 | 427 | 	forceArchType android.ArchType | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 428 | } | 
 | 429 |  | 
 | 430 | func (r *robolectricRuntimes) TestSuites() []string { | 
 | 431 | 	return []string{"robolectric-tests"} | 
 | 432 | } | 
 | 433 |  | 
 | 434 | var _ android.TestSuiteModule = (*robolectricRuntimes)(nil) | 
 | 435 |  | 
 | 436 | func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 437 | 	if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 438 | 		ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib)) | 
 | 439 | 	} | 
 | 440 | } | 
 | 441 |  | 
 | 442 | func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 443 | 	if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os { | 
 | 444 | 		return | 
 | 445 | 	} | 
 | 446 |  | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 447 | 	r.forceOSType = ctx.Config().BuildOS | 
 | 448 | 	r.forceArchType = ctx.Config().BuildArch | 
 | 449 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 450 | 	files := android.PathsForModuleSrc(ctx, r.props.Jars) | 
 | 451 |  | 
 | 452 | 	androidAllDir := android.PathForModuleInstall(ctx, "android-all") | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 453 | 	for _, from := range files { | 
 | 454 | 		installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from) | 
 | 455 | 		r.runtimes = append(r.runtimes, installedRuntime) | 
 | 456 | 	} | 
 | 457 |  | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 458 | 	if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 459 | 		runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag) | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 460 | 		if runtimeFromSourceModule == nil { | 
 | 461 | 			if ctx.Config().AllowMissingDependencies() { | 
 | 462 | 				ctx.AddMissingDependencies([]string{String(r.props.Lib)}) | 
 | 463 | 			} else { | 
 | 464 | 				ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib)) | 
 | 465 | 			} | 
 | 466 | 			return | 
 | 467 | 		} | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 468 | 		runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "") | 
 | 469 |  | 
| Joseph Murphy | c964841 | 2021-07-20 13:58:15 -0700 | [diff] [blame] | 470 | 		// "TREE" name is essential here because it hooks into the "TREE" name in | 
 | 471 | 		// Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK | 
 | 472 | 		// in Robolectric configs. | 
 | 473 | 		runtimeName := "android-all-current-robolectric-r0.jar" | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 474 | 		installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar) | 
 | 475 | 		r.runtimes = append(r.runtimes, installedRuntime) | 
 | 476 | 	} | 
 | 477 | } | 
 | 478 |  | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 479 | func (r *robolectricRuntimes) InstallInTestcases() bool { return true } | 
 | 480 | func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) { | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 481 | 	return &r.forceOSType, &r.forceArchType | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 482 | } |