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