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