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