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" |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 21 | "android/soong/java/config" |
| 22 | "android/soong/tradefed" |
Cole Faust | c731528 | 2025-01-10 15:37:01 -0800 | [diff] [blame^] | 23 | |
Rex Hoffman | 39e3e08 | 2024-12-29 07:29:56 +0000 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Cole Faust | d57e8b2 | 2022-08-11 11:59:04 -0700 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func init() { |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 29 | RegisterRobolectricBuildComponents(android.InitRegistrationContext) |
| 30 | } |
| 31 | |
Rex Hoffman | 39e3e08 | 2024-12-29 07:29:56 +0000 | [diff] [blame] | 32 | type roboRuntimeOnlyDependencyTag struct { |
| 33 | blueprint.BaseDependencyTag |
| 34 | } |
| 35 | |
| 36 | var roboRuntimeOnlyDepTag roboRuntimeOnlyDependencyTag |
| 37 | |
| 38 | // Mark this tag so dependencies that use it are excluded from visibility enforcement. |
| 39 | func (t roboRuntimeOnlyDependencyTag) ExcludeFromVisibilityEnforcement() {} |
| 40 | |
| 41 | var _ android.ExcludeFromVisibilityEnforcementTag = roboRuntimeOnlyDepTag |
| 42 | |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 43 | func RegisterRobolectricBuildComponents(ctx android.RegistrationContext) { |
| 44 | ctx.RegisterModuleType("android_robolectric_test", RobolectricTestFactory) |
| 45 | ctx.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory) |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | var robolectricDefaultLibs = []string{ |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 49 | "mockito-robolectric-prebuilt", |
Krzysztof KosiĆski | 5a55439 | 2023-10-07 19:59:58 +0000 | [diff] [blame] | 50 | "truth", |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 51 | // TODO(ccross): this is not needed at link time |
| 52 | "junitxml", |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 55 | const robolectricCurrentLib = "Robolectric_all-target" |
Rex Hoffman | eef7fca | 2024-12-26 13:38:30 -0800 | [diff] [blame] | 56 | const clearcutJunitLib = "ClearcutJunitListenerAar" |
Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 57 | const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt" |
| 58 | |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 59 | var ( |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 60 | roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"} |
| 61 | roboRuntimesTag = dependencyTag{name: "roboRuntimes"} |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 62 | ) |
| 63 | |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 64 | type robolectricProperties struct { |
| 65 | // The name of the android_app module that the tests will run against. |
| 66 | Instrumentation_for *string |
| 67 | |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 68 | // Additional libraries for which coverage data should be generated |
| 69 | Coverage_libs []string |
| 70 | |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 71 | Test_options struct { |
| 72 | // Timeout in seconds when running the tests. |
Colin Cross | 2f9a7c8 | 2019-05-30 11:16:26 -0700 | [diff] [blame] | 73 | Timeout *int64 |
Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 74 | |
| 75 | // Number of shards to use when running the tests. |
| 76 | Shards *int64 |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 77 | } |
Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 78 | |
Yuichiro Hanada | e42ac1c | 2023-12-08 09:24:40 +0900 | [diff] [blame] | 79 | // 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] | 80 | // to use. /external/robolectric closely tracks github's master, and will fully replace /external/robolectric-shadows |
| 81 | Upstream *bool |
Kevin Liu | cab89b5 | 2024-04-12 21:52:07 +0000 | [diff] [blame] | 82 | |
| 83 | // Use strict mode to limit access of Robolectric API directly. See go/roboStrictMode |
| 84 | Strict_mode *bool |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 85 | |
Jihoon Kang | 371a037 | 2024-10-01 16:44:41 +0000 | [diff] [blame] | 86 | Jni_libs proptools.Configurable[[]string] |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | type robolectricTest struct { |
| 90 | Library |
| 91 | |
| 92 | robolectricProperties robolectricProperties |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 93 | testProperties testProperties |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 94 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 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 | |
Rex Hoffman | 39e3e08 | 2024-12-29 07:29:56 +0000 | [diff] [blame] | 117 | ctx.AddVariationDependencies(nil, roboRuntimeOnlyDepTag, clearcutJunitLib) |
Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 118 | |
Kevin Liu | 51349b8 | 2024-06-28 15:02:12 +0000 | [diff] [blame] | 119 | if proptools.BoolDefault(r.robolectricProperties.Strict_mode, true) { |
Rex Hoffman | 39e3e08 | 2024-12-29 07:29:56 +0000 | [diff] [blame] | 120 | ctx.AddVariationDependencies(nil, roboRuntimeOnlyDepTag, robolectricCurrentLib) |
Kevin Liu | 51349b8 | 2024-06-28 15:02:12 +0000 | [diff] [blame] | 121 | } else { |
Rex Hoffman | 39e3e08 | 2024-12-29 07:29:56 +0000 | [diff] [blame] | 122 | ctx.AddVariationDependencies(nil, staticLibTag, robolectricCurrentLib) |
Kevin Liu | 51349b8 | 2024-06-28 15:02:12 +0000 | [diff] [blame] | 123 | // opting out from strict mode, robolectric_non_strict_mode_permission lib should be added |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 124 | ctx.AddVariationDependencies(nil, staticLibTag, "robolectric_non_strict_mode_permission") |
Kevin Liu | cab89b5 | 2024-04-12 21:52:07 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 127 | ctx.AddVariationDependencies(nil, staticLibTag, robolectricDefaultLibs...) |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 128 | |
| 129 | ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 130 | |
Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 131 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), |
| 132 | roboRuntimesTag, "robolectric-android-all-prebuilts") |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 133 | |
Jihoon Kang | 371a037 | 2024-10-01 16:44:41 +0000 | [diff] [blame] | 134 | for _, lib := range r.robolectricProperties.Jni_libs.GetOrDefault(ctx, nil) { |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 135 | ctx.AddVariationDependencies(ctx.Config().BuildOSTarget.Variations(), jniLibTag, lib) |
| 136 | } |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 140 | r.forceOSType = ctx.Config().BuildOS |
| 141 | r.forceArchType = ctx.Config().BuildArch |
| 142 | |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 143 | r.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ |
| 144 | TestConfigProp: r.testProperties.Test_config, |
| 145 | TestConfigTemplateProp: r.testProperties.Test_config_template, |
| 146 | TestSuites: r.testProperties.Test_suites, |
| 147 | AutoGenConfig: r.testProperties.Auto_gen_config, |
| 148 | DeviceTemplate: "${RobolectricTestConfigTemplate}", |
| 149 | HostTemplate: "${RobolectricTestConfigTemplate}", |
| 150 | }) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 151 | r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data) |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 152 | r.data = append(r.data, android.PathsForModuleSrc(ctx, r.testProperties.Device_common_data)...) |
| 153 | r.data = append(r.data, android.PathsForModuleSrc(ctx, r.testProperties.Device_first_data)...) |
Cole Faust | 18f03f1 | 2024-10-23 14:51:11 -0700 | [diff] [blame] | 154 | r.data = append(r.data, android.PathsForModuleSrc(ctx, r.testProperties.Device_first_prefer32_data)...) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 155 | |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 156 | var ok bool |
| 157 | var instrumentedApp *AndroidApp |
| 158 | |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 159 | // TODO: this inserts paths to built files into the test, it should really be inserting the contents. |
| 160 | instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag) |
| 161 | |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 162 | if len(instrumented) == 1 { |
| 163 | instrumentedApp, ok = instrumented[0].(*AndroidApp) |
| 164 | if !ok { |
| 165 | ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app") |
| 166 | } |
| 167 | } else if !ctx.Config().AllowMissingDependencies() { |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 168 | panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented))) |
| 169 | } |
| 170 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 171 | var resourceApk android.Path |
| 172 | var manifest android.Path |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 173 | if instrumentedApp != nil { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 174 | manifest = instrumentedApp.mergedManifestFile |
| 175 | resourceApk = instrumentedApp.outputFile |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 176 | } |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 177 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 178 | roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar") |
| 179 | generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar) |
| 180 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 181 | extraCombinedJars := android.Paths{roboTestConfigJar} |
Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 182 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 183 | handleLibDeps := func(dep android.Module) { |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 184 | if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) { |
Colin Cross | 7727c7f | 2024-07-18 15:36:32 -0700 | [diff] [blame] | 185 | if m, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 186 | extraCombinedJars = append(extraCombinedJars, m.ImplementationAndResourcesJars...) |
Colin Cross | 7727c7f | 2024-07-18 15:36:32 -0700 | [diff] [blame] | 187 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 191 | for _, dep := range ctx.GetDirectDepsWithTag(libTag) { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 192 | handleLibDeps(dep) |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 193 | } |
| 194 | for _, dep := range ctx.GetDirectDepsWithTag(sdkLibTag) { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 195 | handleLibDeps(dep) |
Kevin Liu | cab89b5 | 2024-04-12 21:52:07 +0000 | [diff] [blame] | 196 | } |
| 197 | // handle the runtimeOnly tag for strict_mode |
Rex Hoffman | 39e3e08 | 2024-12-29 07:29:56 +0000 | [diff] [blame] | 198 | for _, dep := range ctx.GetDirectDepsWithTag(roboRuntimeOnlyDepTag) { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 199 | handleLibDeps(dep) |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 202 | if instrumentedApp != nil { |
| 203 | extraCombinedJars = append(extraCombinedJars, instrumentedApp.implementationAndResourcesJar) |
Priyanka Advani (xWF) | 7285367 | 2024-09-04 20:15:58 +0000 | [diff] [blame] | 204 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 205 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 206 | r.stem = proptools.StringDefault(r.overridableProperties.Stem, ctx.ModuleName()) |
| 207 | r.classLoaderContexts = r.usesLibrary.classLoaderContextForUsesLibDeps(ctx) |
| 208 | r.dexpreopter.disableDexpreopt() |
| 209 | r.compile(ctx, nil, nil, nil, extraCombinedJars) |
| 210 | |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 211 | installPath := android.PathForModuleInstall(ctx, r.BaseModuleName()) |
Colin Cross | 09ad3a6 | 2023-11-15 12:29:33 -0800 | [diff] [blame] | 212 | var installDeps android.InstallPaths |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 213 | |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 214 | for _, data := range r.data { |
| 215 | installedData := ctx.InstallFile(installPath, data.Rel(), data) |
| 216 | installDeps = append(installDeps, installedData) |
| 217 | } |
| 218 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 219 | if manifest != nil { |
| 220 | r.data = append(r.data, manifest) |
| 221 | installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", manifest) |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 222 | installDeps = append(installDeps, installedManifest) |
| 223 | } |
| 224 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 225 | if resourceApk != nil { |
| 226 | r.data = append(r.data, resourceApk) |
| 227 | installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", resourceApk) |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 228 | installDeps = append(installDeps, installedResourceApk) |
| 229 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 230 | |
| 231 | runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 232 | for _, runtime := range runtimes.(*robolectricRuntimes).runtimes { |
| 233 | installDeps = append(installDeps, runtime) |
| 234 | } |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 235 | |
| 236 | installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig) |
| 237 | installDeps = append(installDeps, installedConfig) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 238 | |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 239 | soInstallPath := installPath.Join(ctx, getLibPath(r.forceArchType)) |
| 240 | for _, jniLib := range collectTransitiveJniDeps(ctx) { |
| 241 | installJni := ctx.InstallFile(soInstallPath, jniLib.path.Base(), jniLib.path) |
| 242 | installDeps = append(installDeps, installJni) |
| 243 | } |
| 244 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 245 | r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.outputFile, installDeps...) |
Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 248 | func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 249 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 250 | |
| 251 | outputDir := outputFile.InSameDir(ctx) |
| 252 | configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties") |
| 253 | rule.Temporary(configFile) |
| 254 | rule.Command().Text("rm -f").Output(outputFile).Output(configFile) |
| 255 | rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String()) |
| 256 | rule.Command(). |
| 257 | Text("("). |
| 258 | Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()). |
| 259 | Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()). |
| 260 | Text(") >>").Output(configFile) |
| 261 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 262 | BuiltTool("soong_zip"). |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 263 | FlagWithArg("-C ", outputDir.String()). |
| 264 | FlagWithInput("-f ", configFile). |
| 265 | FlagWithOutput("-o ", outputFile) |
| 266 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 267 | rule.Build("generate_test_config_samedir", "generate test_config.properties") |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 270 | func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries { |
| 271 | entriesList := r.Library.AndroidMkEntries() |
| 272 | entries := &entriesList[0] |
Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 273 | entries.ExtraEntries = append(entries.ExtraEntries, |
| 274 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 275 | entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true) |
nelsonli | 3f97ea2 | 2022-11-21 21:37:56 +0800 | [diff] [blame] | 276 | entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", "robolectric-tests") |
Yike | 0fcf90a | 2023-02-14 12:17:32 +0800 | [diff] [blame] | 277 | if r.testConfig != nil { |
| 278 | entries.SetPath("LOCAL_FULL_TEST_CONFIG", r.testConfig) |
| 279 | } |
Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 280 | }) |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 281 | return entriesList |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 285 | // instead of on a device. |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 286 | func RobolectricTestFactory() android.Module { |
| 287 | module := &robolectricTest{} |
| 288 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 289 | module.addHostProperties() |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 290 | module.AddProperties( |
Colin Cross | e323f3c | 2019-09-17 15:34:09 -0700 | [diff] [blame] | 291 | &module.Module.deviceProperties, |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 292 | &module.robolectricProperties, |
| 293 | &module.testProperties) |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 294 | |
| 295 | module.Module.dexpreopter.isTest = true |
Cole Faust | c731528 | 2025-01-10 15:37:01 -0800 | [diff] [blame^] | 296 | module.Module.linter.properties.Lint.Test_module_type = proptools.BoolPtr(true) |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 297 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 298 | module.testProperties.Test_suites = []string{"robolectric-tests"} |
| 299 | |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 300 | InitJavaModule(module, android.DeviceSupported) |
| 301 | return module |
| 302 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 303 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 304 | func (r *robolectricTest) InstallInTestcases() bool { return true } |
| 305 | func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 306 | return &r.forceOSType, &r.forceArchType |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 307 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 308 | |
| 309 | func robolectricRuntimesFactory() android.Module { |
| 310 | module := &robolectricRuntimes{} |
| 311 | module.AddProperties(&module.props) |
Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 312 | android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 313 | return module |
| 314 | } |
| 315 | |
| 316 | type robolectricRuntimesProperties struct { |
| 317 | Jars []string `android:"path"` |
| 318 | Lib *string |
| 319 | } |
| 320 | |
| 321 | type robolectricRuntimes struct { |
| 322 | android.ModuleBase |
| 323 | |
| 324 | props robolectricRuntimesProperties |
| 325 | |
| 326 | runtimes []android.InstallPath |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 327 | |
| 328 | forceOSType android.OsType |
| 329 | forceArchType android.ArchType |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | func (r *robolectricRuntimes) TestSuites() []string { |
| 333 | return []string{"robolectric-tests"} |
| 334 | } |
| 335 | |
| 336 | var _ android.TestSuiteModule = (*robolectricRuntimes)(nil) |
| 337 | |
| 338 | func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 339 | if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 340 | ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib)) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 345 | if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os { |
| 346 | return |
| 347 | } |
| 348 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 349 | r.forceOSType = ctx.Config().BuildOS |
| 350 | r.forceArchType = ctx.Config().BuildArch |
| 351 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 352 | files := android.PathsForModuleSrc(ctx, r.props.Jars) |
| 353 | |
| 354 | androidAllDir := android.PathForModuleInstall(ctx, "android-all") |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 355 | for _, from := range files { |
| 356 | installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from) |
| 357 | r.runtimes = append(r.runtimes, installedRuntime) |
| 358 | } |
| 359 | |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 360 | if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 361 | runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag) |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 362 | if runtimeFromSourceModule == nil { |
| 363 | if ctx.Config().AllowMissingDependencies() { |
| 364 | ctx.AddMissingDependencies([]string{String(r.props.Lib)}) |
| 365 | } else { |
| 366 | ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib)) |
| 367 | } |
| 368 | return |
| 369 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 370 | runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "") |
| 371 | |
Joseph Murphy | c964841 | 2021-07-20 13:58:15 -0700 | [diff] [blame] | 372 | // "TREE" name is essential here because it hooks into the "TREE" name in |
| 373 | // Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK |
| 374 | // in Robolectric configs. |
| 375 | runtimeName := "android-all-current-robolectric-r0.jar" |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 376 | installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar) |
| 377 | r.runtimes = append(r.runtimes, installedRuntime) |
| 378 | } |
| 379 | } |
| 380 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 381 | func (r *robolectricRuntimes) InstallInTestcases() bool { return true } |
| 382 | func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 383 | return &r.forceOSType, &r.forceArchType |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 384 | } |