blob: 896bcf8dfe2e761a3b1ff6ffbf1f64e273f97b58 [file] [log] [blame]
Colin Crossb1974532019-02-15 10:37:39 -08001// 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
15package java
16
17import (
18 "fmt"
Paul Duffincee7e662020-07-09 17:32:57 +010019 "reflect"
20 "sort"
Paul Duffin4fd997b2021-02-03 20:06:33 +000021 "strings"
Paul Duffincee7e662020-07-09 17:32:57 +010022 "testing"
Colin Crossb1974532019-02-15 10:37:39 -080023
24 "android/soong/android"
Colin Crossf28329d2020-02-15 11:00:10 -080025 "android/soong/cc"
Ulya Trafimovich24813e12020-10-07 15:05:21 +010026 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070027 "android/soong/python"
28
Paul Duffincee7e662020-07-09 17:32:57 +010029 "github.com/google/blueprint"
Colin Crossb1974532019-02-15 10:37:39 -080030)
31
Paul Duffin95bdab42021-03-08 21:48:46 +000032const defaultJavaDir = "default/java"
Colin Cross98be1bb2019-12-13 20:41:13 -080033
Paul Duffin95bdab42021-03-08 21:48:46 +000034// Test fixture preparer that will register most java build components.
35//
36// Singletons and mutators should only be added here if they are needed for a majority of java
37// module types, otherwise they should be added under a separate preparer to allow them to be
38// selected only when needed to reduce test execution time.
39//
40// Module types do not have much of an overhead unless they are used so this should include as many
41// module types as possible. The exceptions are those module types that require mutators and/or
42// singletons in order to function in which case they should be kept together in a separate
43// preparer.
44var PrepareForTestWithJavaBuildComponents = android.FixtureRegisterWithContext(RegisterRequiredBuildComponentsForTest)
45
46// Test fixture preparer that will define default java modules, e.g. standard prebuilt modules.
47var PrepareForTestWithJavaDefaultModules = android.GroupFixturePreparers(
48 // Make sure that mutators and module types, e.g. prebuilt mutators available.
49 android.PrepareForTestWithAndroidBuildComponents,
50 // Make sure that all the module types used in the defaults are registered.
51 PrepareForTestWithJavaBuildComponents,
52 // The java default module definitions.
53 android.FixtureAddTextFile(defaultJavaDir+"/Android.bp", GatherRequiredDepsForTest()),
54)
55
56// Prepare a fixture to use all java module types, mutators and singletons fully.
57//
58// This should only be used by tests that want to run with as much of the build enabled as possible.
59var PrepareForIntegrationTestWithJava = android.GroupFixturePreparers(
60 cc.PrepareForIntegrationTestWithCc,
61 PrepareForTestWithJavaDefaultModules,
62)
63
Paul Duffinbf028b52021-03-13 22:19:17 +000064// Prepare a fixture with the standard files required by a java_sdk_library module.
65var PrepareForTestWithJavaSdkLibraryFiles = android.FixtureMergeMockFs(javaSdkLibraryFiles)
66
67var javaSdkLibraryFiles = android.MockFS{
68 "api/current.txt": nil,
69 "api/removed.txt": nil,
70 "api/system-current.txt": nil,
71 "api/system-removed.txt": nil,
72 "api/test-current.txt": nil,
73 "api/test-removed.txt": nil,
74 "api/module-lib-current.txt": nil,
75 "api/module-lib-removed.txt": nil,
76 "api/system-server-current.txt": nil,
77 "api/system-server-removed.txt": nil,
78}
79
Paul Duffin2ff6d1b2021-03-13 22:37:27 +000080// FixtureWithLastReleaseApis creates a preparer that creates prebuilt versions of the specified
81// modules for the `last` API release. By `last` it just means last in the list of supplied versions
82// and as this only provides one version it can be any value.
83//
84// This uses FixtureWithPrebuiltApis under the covers so the limitations of that apply to this.
85func FixtureWithLastReleaseApis(moduleNames ...string) android.FixturePreparer {
86 return FixtureWithPrebuiltApis(map[string][]string{
87 "30": moduleNames,
88 })
89}
90
91// PrepareForTestWithPrebuiltsOfCurrentApi is a preparer that creates prebuilt versions of the
92// standard modules for the current version.
93//
94// This uses FixtureWithPrebuiltApis under the covers so the limitations of that apply to this.
95var PrepareForTestWithPrebuiltsOfCurrentApi = FixtureWithPrebuiltApis(map[string][]string{
96 "current": {},
97 // Can't have current on its own as it adds a prebuilt_apis module but doesn't add any
98 // .txt files which causes the prebuilt_apis module to fail.
99 "30": {},
100})
101
102// FixtureWithPrebuiltApis creates a preparer that will define prebuilt api modules for the
103// specified releases and modules.
104//
105// The supplied map keys are the releases, e.g. current, 29, 30, etc. The values are a list of
106// modules for that release. Due to limitations in the prebuilt_apis module which this preparer
107// uses the set of releases must include at least one numbered release, i.e. it cannot just include
108// "current".
109//
110// This defines a file in the mock file system in a predefined location (prebuilts/sdk/Android.bp)
111// and so only one instance of this can be used in each fixture.
112func FixtureWithPrebuiltApis(release2Modules map[string][]string) android.FixturePreparer {
113 mockFS := android.MockFS{}
114 path := "prebuilts/sdk/Android.bp"
115
116 bp := fmt.Sprintf(`
117 prebuilt_apis {
118 name: "sdk",
119 api_dirs: ["%s"],
120 imports_sdk_version: "none",
121 imports_compile_dex: true,
122 }
123 `, strings.Join(android.SortedStringKeys(release2Modules), `", "`))
124
125 for release, modules := range release2Modules {
126 libs := append([]string{"android", "core-for-system-modules"}, modules...)
127 mockFS.Merge(prebuiltApisFilesForLibs([]string{release}, libs))
128 }
129 return android.GroupFixturePreparers(
130 // A temporary measure to discard the definitions provided by default by javaMockFS() to allow
131 // the changes that use this preparer to fix tests to be separated from the change to remove
132 // javaMockFS().
133 android.FixtureModifyMockFS(func(fs android.MockFS) {
134 for k, _ := range fs {
135 if strings.HasPrefix(k, "prebuilts/sdk/") {
136 delete(fs, k)
137 }
138 }
139 }),
140 android.FixtureAddTextFile(path, bp),
141 android.FixtureMergeMockFs(mockFS),
142 )
143}
144
Paul Duffin95bdab42021-03-08 21:48:46 +0000145func javaMockFS() android.MockFS {
146 mockFS := android.MockFS{
Anton Hanssondff2c782020-12-21 17:10:01 +0000147 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
148 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "30", "current"], imports_sdk_version: "none", imports_compile_dex:true,}`),
Paul Duffin0c5bae52020-06-02 13:00:08 +0100149
Liz Kammerdd849a82020-06-12 16:38:45 -0700150 "bin.py": nil,
151 python.StubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%'
152 MAIN_FILE = '%main%'`),
Colin Cross98be1bb2019-12-13 20:41:13 -0800153 }
154
Anton Hanssondff2c782020-12-21 17:10:01 +0000155 levels := []string{"14", "28", "29", "30", "current"}
156 libs := []string{
157 "android", "foo", "bar", "sdklib", "barney", "betty", "foo-shared_library",
158 "foo-no_shared_library", "core-for-system-modules", "quuz", "qux", "fred",
159 "runtime-library",
160 }
161 for k, v := range prebuiltApisFilesForLibs(levels, libs) {
162 mockFS[k] = v
163 }
164
Paul Duffin95bdab42021-03-08 21:48:46 +0000165 return mockFS
166}
167
168func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config {
169 bp += GatherRequiredDepsForTest()
170
171 mockFS := javaMockFS()
Paul Duffinbf028b52021-03-13 22:19:17 +0000172 mockFS.Merge(javaSdkLibraryFiles)
Paul Duffin95bdab42021-03-08 21:48:46 +0000173
Colin Crossf28329d2020-02-15 11:00:10 -0800174 cc.GatherRequiredFilesForTest(mockFS)
175
Colin Cross98be1bb2019-12-13 20:41:13 -0800176 for k, v := range fs {
177 mockFS[k] = v
178 }
179
Colin Crossb1974532019-02-15 10:37:39 -0800180 if env == nil {
181 env = make(map[string]string)
182 }
183 if env["ANDROID_JAVA8_HOME"] == "" {
184 env["ANDROID_JAVA8_HOME"] = "jdk8"
185 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800186 config := android.TestArchConfig(buildDir, env, bp, mockFS)
Colin Crossb1974532019-02-15 10:37:39 -0800187
Colin Crossb1974532019-02-15 10:37:39 -0800188 return config
189}
190
Anton Hanssondff2c782020-12-21 17:10:01 +0000191func prebuiltApisFilesForLibs(apiLevels []string, sdkLibs []string) map[string][]byte {
192 fs := make(map[string][]byte)
193 for _, level := range apiLevels {
194 for _, lib := range sdkLibs {
195 for _, scope := range []string{"public", "system", "module-lib", "system-server", "test"} {
196 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, scope, lib)] = nil
Anton Hansson370fd0b2021-01-22 15:05:04 +0000197 // No finalized API files for "current"
198 if level != "current" {
199 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, scope, lib)] = nil
200 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, scope, lib)] = nil
201 }
Anton Hanssondff2c782020-12-21 17:10:01 +0000202 }
203 }
204 fs[fmt.Sprintf("prebuilts/sdk/%s/public/framework.aidl", level)] = nil
205 }
206 return fs
207}
208
Paul Duffinc059c8c2021-01-20 17:13:52 +0000209// Register build components provided by this package that are needed by tests.
210//
211// In particular this must register all the components that are used in the `Android.bp` snippet
212// returned by GatherRequiredDepsForTest()
213func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
214 RegisterAARBuildComponents(ctx)
215 RegisterAppBuildComponents(ctx)
216 RegisterAppImportBuildComponents(ctx)
217 RegisterAppSetBuildComponents(ctx)
Paul Duffin3451e162021-01-20 15:16:56 +0000218 RegisterBootImageBuildComponents(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000219 RegisterDexpreoptBootJarsComponents(ctx)
220 RegisterDocsBuildComponents(ctx)
221 RegisterGenRuleBuildComponents(ctx)
222 RegisterJavaBuildComponents(ctx)
223 RegisterPrebuiltApisBuildComponents(ctx)
224 RegisterRuntimeResourceOverlayBuildComponents(ctx)
225 RegisterSdkLibraryBuildComponents(ctx)
226 RegisterStubsBuildComponents(ctx)
227 RegisterSystemModulesBuildComponents(ctx)
Paul Duffin635aa082021-01-25 19:11:24 +0000228
229 // Make sure that any tool related module types needed by dexpreopt have been registered.
230 dexpreopt.RegisterToolModulesForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000231}
232
233// Gather the module definitions needed by tests that depend upon code from this package.
234//
235// Returns an `Android.bp` snippet that defines the modules that are needed by this package.
Colin Crossb1974532019-02-15 10:37:39 -0800236func GatherRequiredDepsForTest() string {
237 var bp string
238
239 extraModules := []string{
240 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800241 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800242 "android_stubs_current",
243 "android_system_stubs_current",
244 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900245 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000246 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800247 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100248 "legacy.core.platform.api.stubs",
249 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800250 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700251 "kotlin-stdlib-jdk7",
252 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800253 "kotlin-annotations",
254 }
255
256 for _, extra := range extraModules {
257 bp += fmt.Sprintf(`
258 java_library {
259 name: "%s",
260 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100261 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100262 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700263 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800264 }
265 `, extra)
266 }
267
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100268 // For class loader context and <uses-library> tests.
269 dexpreoptModules := []string{"android.test.runner"}
270 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
271 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
272
273 for _, extra := range dexpreoptModules {
274 bp += fmt.Sprintf(`
275 java_library {
276 name: "%s",
277 srcs: ["a.java"],
278 sdk_version: "none",
279 system_modules: "stable-core-platform-api-stubs-system-modules",
280 compile_dex: true,
281 installable: true,
282 }
283 `, extra)
284 }
285
Colin Crossb1974532019-02-15 10:37:39 -0800286 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700287 java_library {
288 name: "framework",
289 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100290 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100291 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700292 aidl: {
293 export_include_dirs: ["framework/aidl"],
294 },
295 }
296
Colin Crossb1974532019-02-15 10:37:39 -0800297 android_app {
298 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100299 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100300 }`
Colin Crossb1974532019-02-15 10:37:39 -0800301
302 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100303 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100304 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100305 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800306 }
307
308 for _, extra := range systemModules {
309 bp += fmt.Sprintf(`
310 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100311 name: "%[1]s",
312 libs: ["%[1]s-lib"],
313 }
314 java_library {
315 name: "%[1]s-lib",
316 sdk_version: "none",
317 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800318 }
319 `, extra)
320 }
321
Paul Duffin635aa082021-01-25 19:11:24 +0000322 // Make sure that any tools needed for dexpreopting are defined.
323 bp += dexpreopt.BpToolModulesForTest()
324
Paul Duffin1ab61862021-01-20 17:44:53 +0000325 // Make sure that the dex_bootjars singleton module is instantiated for the tests.
326 bp += `
327 dex_bootjars {
328 name: "dex_bootjars",
329 }
330`
331
Colin Crossb1974532019-02-15 10:37:39 -0800332 return bp
333}
Paul Duffincee7e662020-07-09 17:32:57 +0100334
335func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
336 t.Helper()
337 module := ctx.ModuleForTests(name, variant).Module()
338 deps := []string{}
339 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
340 deps = append(deps, m.Name())
341 })
342 sort.Strings(deps)
343
344 if actual := deps; !reflect.DeepEqual(expected, actual) {
345 t.Errorf("expected %#q, found %#q", expected, actual)
346 }
347}
Paul Duffin4fd997b2021-02-03 20:06:33 +0000348
349func CheckHiddenAPIRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) {
Paul Duffin37856732021-02-26 14:24:15 +0000350 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +0000351 actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n"))
352 expected = strings.TrimSpace(expected)
353 if actual != expected {
354 t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual)
355 }
356}