blob: a1404a5aea4b9590503f14189ea7c9ea585c27b3 [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 Duffin95bdab42021-03-08 21:48:46 +000080func javaMockFS() android.MockFS {
81 mockFS := android.MockFS{
Anton Hanssondff2c782020-12-21 17:10:01 +000082 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
83 "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 +010084
Liz Kammerdd849a82020-06-12 16:38:45 -070085 "bin.py": nil,
86 python.StubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%'
87 MAIN_FILE = '%main%'`),
Colin Cross98be1bb2019-12-13 20:41:13 -080088 }
89
Anton Hanssondff2c782020-12-21 17:10:01 +000090 levels := []string{"14", "28", "29", "30", "current"}
91 libs := []string{
92 "android", "foo", "bar", "sdklib", "barney", "betty", "foo-shared_library",
93 "foo-no_shared_library", "core-for-system-modules", "quuz", "qux", "fred",
94 "runtime-library",
95 }
96 for k, v := range prebuiltApisFilesForLibs(levels, libs) {
97 mockFS[k] = v
98 }
99
Paul Duffin95bdab42021-03-08 21:48:46 +0000100 return mockFS
101}
102
103func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config {
104 bp += GatherRequiredDepsForTest()
105
106 mockFS := javaMockFS()
Paul Duffinbf028b52021-03-13 22:19:17 +0000107 mockFS.Merge(javaSdkLibraryFiles)
Paul Duffin95bdab42021-03-08 21:48:46 +0000108
Colin Crossf28329d2020-02-15 11:00:10 -0800109 cc.GatherRequiredFilesForTest(mockFS)
110
Colin Cross98be1bb2019-12-13 20:41:13 -0800111 for k, v := range fs {
112 mockFS[k] = v
113 }
114
Colin Crossb1974532019-02-15 10:37:39 -0800115 if env == nil {
116 env = make(map[string]string)
117 }
118 if env["ANDROID_JAVA8_HOME"] == "" {
119 env["ANDROID_JAVA8_HOME"] = "jdk8"
120 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800121 config := android.TestArchConfig(buildDir, env, bp, mockFS)
Colin Crossb1974532019-02-15 10:37:39 -0800122
Colin Crossb1974532019-02-15 10:37:39 -0800123 return config
124}
125
Anton Hanssondff2c782020-12-21 17:10:01 +0000126func prebuiltApisFilesForLibs(apiLevels []string, sdkLibs []string) map[string][]byte {
127 fs := make(map[string][]byte)
128 for _, level := range apiLevels {
129 for _, lib := range sdkLibs {
130 for _, scope := range []string{"public", "system", "module-lib", "system-server", "test"} {
131 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, scope, lib)] = nil
Anton Hansson370fd0b2021-01-22 15:05:04 +0000132 // No finalized API files for "current"
133 if level != "current" {
134 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, scope, lib)] = nil
135 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, scope, lib)] = nil
136 }
Anton Hanssondff2c782020-12-21 17:10:01 +0000137 }
138 }
139 fs[fmt.Sprintf("prebuilts/sdk/%s/public/framework.aidl", level)] = nil
140 }
141 return fs
142}
143
Paul Duffinc059c8c2021-01-20 17:13:52 +0000144// Register build components provided by this package that are needed by tests.
145//
146// In particular this must register all the components that are used in the `Android.bp` snippet
147// returned by GatherRequiredDepsForTest()
148func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
149 RegisterAARBuildComponents(ctx)
150 RegisterAppBuildComponents(ctx)
151 RegisterAppImportBuildComponents(ctx)
152 RegisterAppSetBuildComponents(ctx)
Paul Duffin3451e162021-01-20 15:16:56 +0000153 RegisterBootImageBuildComponents(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000154 RegisterDexpreoptBootJarsComponents(ctx)
155 RegisterDocsBuildComponents(ctx)
156 RegisterGenRuleBuildComponents(ctx)
157 RegisterJavaBuildComponents(ctx)
158 RegisterPrebuiltApisBuildComponents(ctx)
159 RegisterRuntimeResourceOverlayBuildComponents(ctx)
160 RegisterSdkLibraryBuildComponents(ctx)
161 RegisterStubsBuildComponents(ctx)
162 RegisterSystemModulesBuildComponents(ctx)
Paul Duffin635aa082021-01-25 19:11:24 +0000163
164 // Make sure that any tool related module types needed by dexpreopt have been registered.
165 dexpreopt.RegisterToolModulesForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000166}
167
168// Gather the module definitions needed by tests that depend upon code from this package.
169//
170// Returns an `Android.bp` snippet that defines the modules that are needed by this package.
Colin Crossb1974532019-02-15 10:37:39 -0800171func GatherRequiredDepsForTest() string {
172 var bp string
173
174 extraModules := []string{
175 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800176 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800177 "android_stubs_current",
178 "android_system_stubs_current",
179 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900180 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000181 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800182 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100183 "legacy.core.platform.api.stubs",
184 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800185 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700186 "kotlin-stdlib-jdk7",
187 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800188 "kotlin-annotations",
189 }
190
191 for _, extra := range extraModules {
192 bp += fmt.Sprintf(`
193 java_library {
194 name: "%s",
195 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100196 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100197 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700198 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800199 }
200 `, extra)
201 }
202
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100203 // For class loader context and <uses-library> tests.
204 dexpreoptModules := []string{"android.test.runner"}
205 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
206 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
207
208 for _, extra := range dexpreoptModules {
209 bp += fmt.Sprintf(`
210 java_library {
211 name: "%s",
212 srcs: ["a.java"],
213 sdk_version: "none",
214 system_modules: "stable-core-platform-api-stubs-system-modules",
215 compile_dex: true,
216 installable: true,
217 }
218 `, extra)
219 }
220
Colin Crossb1974532019-02-15 10:37:39 -0800221 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700222 java_library {
223 name: "framework",
224 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100225 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100226 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700227 aidl: {
228 export_include_dirs: ["framework/aidl"],
229 },
230 }
231
Colin Crossb1974532019-02-15 10:37:39 -0800232 android_app {
233 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100234 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100235 }`
Colin Crossb1974532019-02-15 10:37:39 -0800236
237 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100238 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100239 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100240 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800241 }
242
243 for _, extra := range systemModules {
244 bp += fmt.Sprintf(`
245 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100246 name: "%[1]s",
247 libs: ["%[1]s-lib"],
248 }
249 java_library {
250 name: "%[1]s-lib",
251 sdk_version: "none",
252 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800253 }
254 `, extra)
255 }
256
Paul Duffin635aa082021-01-25 19:11:24 +0000257 // Make sure that any tools needed for dexpreopting are defined.
258 bp += dexpreopt.BpToolModulesForTest()
259
Paul Duffin1ab61862021-01-20 17:44:53 +0000260 // Make sure that the dex_bootjars singleton module is instantiated for the tests.
261 bp += `
262 dex_bootjars {
263 name: "dex_bootjars",
264 }
265`
266
Colin Crossb1974532019-02-15 10:37:39 -0800267 return bp
268}
Paul Duffincee7e662020-07-09 17:32:57 +0100269
270func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
271 t.Helper()
272 module := ctx.ModuleForTests(name, variant).Module()
273 deps := []string{}
274 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
275 deps = append(deps, m.Name())
276 })
277 sort.Strings(deps)
278
279 if actual := deps; !reflect.DeepEqual(expected, actual) {
280 t.Errorf("expected %#q, found %#q", expected, actual)
281 }
282}
Paul Duffin4fd997b2021-02-03 20:06:33 +0000283
284func CheckHiddenAPIRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) {
Paul Duffin37856732021-02-26 14:24:15 +0000285 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +0000286 actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n"))
287 expected = strings.TrimSpace(expected)
288 if actual != expected {
289 t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual)
290 }
291}