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 | cab89b5 | 2024-04-12 21:52:07 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 125 | ctx.AddVariationDependencies(nil, staticLibTag, robolectricDefaultLibs...) |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 126 | |
| 127 | ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 128 | |
Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 129 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), |
| 130 | roboRuntimesTag, "robolectric-android-all-prebuilts") |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 131 | |
Jihoon Kang | 371a037 | 2024-10-01 16:44:41 +0000 | [diff] [blame] | 132 | for _, lib := range r.robolectricProperties.Jni_libs.GetOrDefault(ctx, nil) { |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 133 | ctx.AddVariationDependencies(ctx.Config().BuildOSTarget.Variations(), jniLibTag, lib) |
| 134 | } |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 138 | r.forceOSType = ctx.Config().BuildOS |
| 139 | r.forceArchType = ctx.Config().BuildArch |
| 140 | |
Rex Hoffman | e025104 | 2025-01-13 03:46:41 +0000 | [diff] [blame^] | 141 | var options []tradefed.Option |
| 142 | options = append(options, tradefed.Option{Name: "java-flags", Value: "-Drobolectric=true"}) |
| 143 | if proptools.BoolDefault(r.robolectricProperties.Strict_mode, true) { |
| 144 | options = append(options, tradefed.Option{Name: "java-flags", Value: "-Drobolectric.strict.mode=true"}) |
| 145 | } |
| 146 | |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 147 | r.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ |
| 148 | TestConfigProp: r.testProperties.Test_config, |
| 149 | TestConfigTemplateProp: r.testProperties.Test_config_template, |
| 150 | TestSuites: r.testProperties.Test_suites, |
Rex Hoffman | e025104 | 2025-01-13 03:46:41 +0000 | [diff] [blame^] | 151 | TestRunnerOptions: options, |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 152 | AutoGenConfig: r.testProperties.Auto_gen_config, |
| 153 | DeviceTemplate: "${RobolectricTestConfigTemplate}", |
| 154 | HostTemplate: "${RobolectricTestConfigTemplate}", |
| 155 | }) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 156 | r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data) |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 157 | r.data = append(r.data, android.PathsForModuleSrc(ctx, r.testProperties.Device_common_data)...) |
| 158 | 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] | 159 | 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] | 160 | |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 161 | var ok bool |
| 162 | var instrumentedApp *AndroidApp |
| 163 | |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 164 | // TODO: this inserts paths to built files into the test, it should really be inserting the contents. |
| 165 | instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag) |
| 166 | |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 167 | if len(instrumented) == 1 { |
| 168 | instrumentedApp, ok = instrumented[0].(*AndroidApp) |
| 169 | if !ok { |
| 170 | ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app") |
| 171 | } |
| 172 | } else if !ctx.Config().AllowMissingDependencies() { |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 173 | panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented))) |
| 174 | } |
| 175 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 176 | var resourceApk android.Path |
| 177 | var manifest android.Path |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 178 | if instrumentedApp != nil { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 179 | manifest = instrumentedApp.mergedManifestFile |
| 180 | resourceApk = instrumentedApp.outputFile |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 181 | } |
Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 182 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 183 | roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar") |
| 184 | generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar) |
| 185 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 186 | extraCombinedJars := android.Paths{roboTestConfigJar} |
Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 187 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 188 | handleLibDeps := func(dep android.Module) { |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 189 | if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) { |
Colin Cross | 7727c7f | 2024-07-18 15:36:32 -0700 | [diff] [blame] | 190 | if m, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 191 | extraCombinedJars = append(extraCombinedJars, m.ImplementationAndResourcesJars...) |
Colin Cross | 7727c7f | 2024-07-18 15:36:32 -0700 | [diff] [blame] | 192 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 196 | for _, dep := range ctx.GetDirectDepsWithTag(libTag) { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 197 | handleLibDeps(dep) |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 198 | } |
| 199 | for _, dep := range ctx.GetDirectDepsWithTag(sdkLibTag) { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 200 | handleLibDeps(dep) |
Kevin Liu | cab89b5 | 2024-04-12 21:52:07 +0000 | [diff] [blame] | 201 | } |
| 202 | // handle the runtimeOnly tag for strict_mode |
Rex Hoffman | 39e3e08 | 2024-12-29 07:29:56 +0000 | [diff] [blame] | 203 | for _, dep := range ctx.GetDirectDepsWithTag(roboRuntimeOnlyDepTag) { |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 204 | handleLibDeps(dep) |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 207 | if instrumentedApp != nil { |
| 208 | extraCombinedJars = append(extraCombinedJars, instrumentedApp.implementationAndResourcesJar) |
Priyanka Advani (xWF) | 7285367 | 2024-09-04 20:15:58 +0000 | [diff] [blame] | 209 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 210 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 211 | r.stem = proptools.StringDefault(r.overridableProperties.Stem, ctx.ModuleName()) |
| 212 | r.classLoaderContexts = r.usesLibrary.classLoaderContextForUsesLibDeps(ctx) |
| 213 | r.dexpreopter.disableDexpreopt() |
Yu Liu | 460cf37 | 2025-01-10 00:34:06 +0000 | [diff] [blame] | 214 | javaInfo := r.compile(ctx, nil, nil, nil, extraCombinedJars) |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 215 | |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 216 | installPath := android.PathForModuleInstall(ctx, r.BaseModuleName()) |
Colin Cross | 09ad3a6 | 2023-11-15 12:29:33 -0800 | [diff] [blame] | 217 | var installDeps android.InstallPaths |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 218 | |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 219 | for _, data := range r.data { |
| 220 | installedData := ctx.InstallFile(installPath, data.Rel(), data) |
| 221 | installDeps = append(installDeps, installedData) |
| 222 | } |
| 223 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 224 | if manifest != nil { |
| 225 | r.data = append(r.data, manifest) |
| 226 | installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", manifest) |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 227 | installDeps = append(installDeps, installedManifest) |
| 228 | } |
| 229 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 230 | if resourceApk != nil { |
| 231 | r.data = append(r.data, resourceApk) |
| 232 | installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", resourceApk) |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 233 | installDeps = append(installDeps, installedResourceApk) |
| 234 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 235 | |
| 236 | runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 237 | for _, runtime := range runtimes.(*robolectricRuntimes).runtimes { |
| 238 | installDeps = append(installDeps, runtime) |
| 239 | } |
Colin Cross | f521efa | 2023-05-19 09:47:09 -0700 | [diff] [blame] | 240 | |
| 241 | installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig) |
| 242 | installDeps = append(installDeps, installedConfig) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 243 | |
Jamie Garside | 56f2b70 | 2024-07-09 12:00:12 +0100 | [diff] [blame] | 244 | soInstallPath := installPath.Join(ctx, getLibPath(r.forceArchType)) |
| 245 | for _, jniLib := range collectTransitiveJniDeps(ctx) { |
| 246 | installJni := ctx.InstallFile(soInstallPath, jniLib.path.Base(), jniLib.path) |
| 247 | installDeps = append(installDeps, installJni) |
| 248 | } |
| 249 | |
Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 250 | r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.outputFile, installDeps...) |
Yu Liu | 460cf37 | 2025-01-10 00:34:06 +0000 | [diff] [blame] | 251 | |
| 252 | if javaInfo != nil { |
| 253 | setExtraJavaInfo(ctx, r, javaInfo) |
| 254 | android.SetProvider(ctx, JavaInfoProvider, javaInfo) |
| 255 | } |
Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 258 | func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 259 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 260 | |
| 261 | outputDir := outputFile.InSameDir(ctx) |
| 262 | configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties") |
| 263 | rule.Temporary(configFile) |
| 264 | rule.Command().Text("rm -f").Output(outputFile).Output(configFile) |
| 265 | rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String()) |
| 266 | rule.Command(). |
| 267 | Text("("). |
| 268 | Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()). |
| 269 | Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()). |
| 270 | Text(") >>").Output(configFile) |
| 271 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 272 | BuiltTool("soong_zip"). |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 273 | FlagWithArg("-C ", outputDir.String()). |
| 274 | FlagWithInput("-f ", configFile). |
| 275 | FlagWithOutput("-o ", outputFile) |
| 276 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 277 | rule.Build("generate_test_config_samedir", "generate test_config.properties") |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 280 | func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries { |
| 281 | entriesList := r.Library.AndroidMkEntries() |
| 282 | entries := &entriesList[0] |
Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 283 | entries.ExtraEntries = append(entries.ExtraEntries, |
| 284 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 285 | entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true) |
nelsonli | 3f97ea2 | 2022-11-21 21:37:56 +0800 | [diff] [blame] | 286 | entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", "robolectric-tests") |
Yike | 0fcf90a | 2023-02-14 12:17:32 +0800 | [diff] [blame] | 287 | if r.testConfig != nil { |
| 288 | entries.SetPath("LOCAL_FULL_TEST_CONFIG", r.testConfig) |
| 289 | } |
Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 290 | }) |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 291 | return entriesList |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // 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] | 295 | // instead of on a device. |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 296 | func RobolectricTestFactory() android.Module { |
| 297 | module := &robolectricTest{} |
| 298 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 299 | module.addHostProperties() |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 300 | module.AddProperties( |
Colin Cross | e323f3c | 2019-09-17 15:34:09 -0700 | [diff] [blame] | 301 | &module.Module.deviceProperties, |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 302 | &module.robolectricProperties, |
| 303 | &module.testProperties) |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 304 | |
| 305 | module.Module.dexpreopter.isTest = true |
Cole Faust | c731528 | 2025-01-10 15:37:01 -0800 | [diff] [blame] | 306 | module.Module.linter.properties.Lint.Test_module_type = proptools.BoolPtr(true) |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 307 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 308 | module.testProperties.Test_suites = []string{"robolectric-tests"} |
| 309 | |
Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 310 | InitJavaModule(module, android.DeviceSupported) |
| 311 | return module |
| 312 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 313 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 314 | func (r *robolectricTest) InstallInTestcases() bool { return true } |
| 315 | func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 316 | return &r.forceOSType, &r.forceArchType |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 317 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 318 | |
| 319 | func robolectricRuntimesFactory() android.Module { |
| 320 | module := &robolectricRuntimes{} |
| 321 | module.AddProperties(&module.props) |
Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 322 | android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 323 | return module |
| 324 | } |
| 325 | |
| 326 | type robolectricRuntimesProperties struct { |
| 327 | Jars []string `android:"path"` |
| 328 | Lib *string |
| 329 | } |
| 330 | |
| 331 | type robolectricRuntimes struct { |
| 332 | android.ModuleBase |
| 333 | |
| 334 | props robolectricRuntimesProperties |
| 335 | |
| 336 | runtimes []android.InstallPath |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 337 | |
| 338 | forceOSType android.OsType |
| 339 | forceArchType android.ArchType |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | func (r *robolectricRuntimes) TestSuites() []string { |
| 343 | return []string{"robolectric-tests"} |
| 344 | } |
| 345 | |
| 346 | var _ android.TestSuiteModule = (*robolectricRuntimes)(nil) |
| 347 | |
| 348 | func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 349 | if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 350 | ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib)) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 355 | if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os { |
| 356 | return |
| 357 | } |
| 358 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 359 | r.forceOSType = ctx.Config().BuildOS |
| 360 | r.forceArchType = ctx.Config().BuildArch |
| 361 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 362 | files := android.PathsForModuleSrc(ctx, r.props.Jars) |
| 363 | |
| 364 | androidAllDir := android.PathForModuleInstall(ctx, "android-all") |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 365 | for _, from := range files { |
| 366 | installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from) |
| 367 | r.runtimes = append(r.runtimes, installedRuntime) |
| 368 | } |
| 369 | |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 370 | if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 371 | runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag) |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 372 | if runtimeFromSourceModule == nil { |
| 373 | if ctx.Config().AllowMissingDependencies() { |
| 374 | ctx.AddMissingDependencies([]string{String(r.props.Lib)}) |
| 375 | } else { |
| 376 | ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib)) |
| 377 | } |
| 378 | return |
| 379 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 380 | runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "") |
| 381 | |
Joseph Murphy | c964841 | 2021-07-20 13:58:15 -0700 | [diff] [blame] | 382 | // "TREE" name is essential here because it hooks into the "TREE" name in |
| 383 | // Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK |
| 384 | // in Robolectric configs. |
| 385 | runtimeName := "android-all-current-robolectric-r0.jar" |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 386 | installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar) |
| 387 | r.runtimes = append(r.runtimes, installedRuntime) |
| 388 | } |
| 389 | } |
| 390 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 391 | func (r *robolectricRuntimes) InstallInTestcases() bool { return true } |
| 392 | func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 393 | return &r.forceOSType, &r.forceArchType |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 394 | } |