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