blob: 4e1997e4526e8e6caa73adf093b09700da18b2de [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
64func javaMockFS() android.MockFS {
65 mockFS := android.MockFS{
Colin Cross98be1bb2019-12-13 20:41:13 -080066 "api/current.txt": nil,
67 "api/removed.txt": nil,
68 "api/system-current.txt": nil,
69 "api/system-removed.txt": nil,
70 "api/test-current.txt": nil,
71 "api/test-removed.txt": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080072
Anton Hanssondff2c782020-12-21 17:10:01 +000073 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
74 "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 +010075
Liz Kammerdd849a82020-06-12 16:38:45 -070076 "bin.py": nil,
77 python.StubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%'
78 MAIN_FILE = '%main%'`),
79
Paul Duffin0c5bae52020-06-02 13:00:08 +010080 // For java_sdk_library
Colin Cross300b2662020-06-11 12:12:30 -070081 "api/module-lib-current.txt": nil,
82 "api/module-lib-removed.txt": nil,
83 "api/system-server-current.txt": nil,
84 "api/system-server-removed.txt": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080085 }
86
Anton Hanssondff2c782020-12-21 17:10:01 +000087 levels := []string{"14", "28", "29", "30", "current"}
88 libs := []string{
89 "android", "foo", "bar", "sdklib", "barney", "betty", "foo-shared_library",
90 "foo-no_shared_library", "core-for-system-modules", "quuz", "qux", "fred",
91 "runtime-library",
92 }
93 for k, v := range prebuiltApisFilesForLibs(levels, libs) {
94 mockFS[k] = v
95 }
96
Paul Duffin95bdab42021-03-08 21:48:46 +000097 return mockFS
98}
99
100func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config {
101 bp += GatherRequiredDepsForTest()
102
103 mockFS := javaMockFS()
104
Colin Crossf28329d2020-02-15 11:00:10 -0800105 cc.GatherRequiredFilesForTest(mockFS)
106
Colin Cross98be1bb2019-12-13 20:41:13 -0800107 for k, v := range fs {
108 mockFS[k] = v
109 }
110
Colin Crossb1974532019-02-15 10:37:39 -0800111 if env == nil {
112 env = make(map[string]string)
113 }
114 if env["ANDROID_JAVA8_HOME"] == "" {
115 env["ANDROID_JAVA8_HOME"] = "jdk8"
116 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 config := android.TestArchConfig(buildDir, env, bp, mockFS)
Colin Crossb1974532019-02-15 10:37:39 -0800118
Colin Crossb1974532019-02-15 10:37:39 -0800119 return config
120}
121
Anton Hanssondff2c782020-12-21 17:10:01 +0000122func prebuiltApisFilesForLibs(apiLevels []string, sdkLibs []string) map[string][]byte {
123 fs := make(map[string][]byte)
124 for _, level := range apiLevels {
125 for _, lib := range sdkLibs {
126 for _, scope := range []string{"public", "system", "module-lib", "system-server", "test"} {
127 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, scope, lib)] = nil
Anton Hansson370fd0b2021-01-22 15:05:04 +0000128 // No finalized API files for "current"
129 if level != "current" {
130 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, scope, lib)] = nil
131 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, scope, lib)] = nil
132 }
Anton Hanssondff2c782020-12-21 17:10:01 +0000133 }
134 }
135 fs[fmt.Sprintf("prebuilts/sdk/%s/public/framework.aidl", level)] = nil
136 }
137 return fs
138}
139
Paul Duffinc059c8c2021-01-20 17:13:52 +0000140// Register build components provided by this package that are needed by tests.
141//
142// In particular this must register all the components that are used in the `Android.bp` snippet
143// returned by GatherRequiredDepsForTest()
144func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
145 RegisterAARBuildComponents(ctx)
146 RegisterAppBuildComponents(ctx)
147 RegisterAppImportBuildComponents(ctx)
148 RegisterAppSetBuildComponents(ctx)
Paul Duffin3451e162021-01-20 15:16:56 +0000149 RegisterBootImageBuildComponents(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000150 RegisterDexpreoptBootJarsComponents(ctx)
151 RegisterDocsBuildComponents(ctx)
152 RegisterGenRuleBuildComponents(ctx)
153 RegisterJavaBuildComponents(ctx)
154 RegisterPrebuiltApisBuildComponents(ctx)
155 RegisterRuntimeResourceOverlayBuildComponents(ctx)
156 RegisterSdkLibraryBuildComponents(ctx)
157 RegisterStubsBuildComponents(ctx)
158 RegisterSystemModulesBuildComponents(ctx)
Paul Duffin635aa082021-01-25 19:11:24 +0000159
160 // Make sure that any tool related module types needed by dexpreopt have been registered.
161 dexpreopt.RegisterToolModulesForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000162}
163
164// Gather the module definitions needed by tests that depend upon code from this package.
165//
166// Returns an `Android.bp` snippet that defines the modules that are needed by this package.
Colin Crossb1974532019-02-15 10:37:39 -0800167func GatherRequiredDepsForTest() string {
168 var bp string
169
170 extraModules := []string{
171 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800172 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800173 "android_stubs_current",
174 "android_system_stubs_current",
175 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900176 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000177 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800178 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100179 "legacy.core.platform.api.stubs",
180 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800181 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700182 "kotlin-stdlib-jdk7",
183 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800184 "kotlin-annotations",
185 }
186
187 for _, extra := range extraModules {
188 bp += fmt.Sprintf(`
189 java_library {
190 name: "%s",
191 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100192 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100193 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700194 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800195 }
196 `, extra)
197 }
198
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100199 // For class loader context and <uses-library> tests.
200 dexpreoptModules := []string{"android.test.runner"}
201 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
202 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
203
204 for _, extra := range dexpreoptModules {
205 bp += fmt.Sprintf(`
206 java_library {
207 name: "%s",
208 srcs: ["a.java"],
209 sdk_version: "none",
210 system_modules: "stable-core-platform-api-stubs-system-modules",
211 compile_dex: true,
212 installable: true,
213 }
214 `, extra)
215 }
216
Colin Crossb1974532019-02-15 10:37:39 -0800217 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700218 java_library {
219 name: "framework",
220 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100221 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100222 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700223 aidl: {
224 export_include_dirs: ["framework/aidl"],
225 },
226 }
227
Colin Crossb1974532019-02-15 10:37:39 -0800228 android_app {
229 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100230 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100231 }`
Colin Crossb1974532019-02-15 10:37:39 -0800232
233 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100234 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100235 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100236 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800237 }
238
239 for _, extra := range systemModules {
240 bp += fmt.Sprintf(`
241 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100242 name: "%[1]s",
243 libs: ["%[1]s-lib"],
244 }
245 java_library {
246 name: "%[1]s-lib",
247 sdk_version: "none",
248 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800249 }
250 `, extra)
251 }
252
Paul Duffin635aa082021-01-25 19:11:24 +0000253 // Make sure that any tools needed for dexpreopting are defined.
254 bp += dexpreopt.BpToolModulesForTest()
255
Paul Duffin1ab61862021-01-20 17:44:53 +0000256 // Make sure that the dex_bootjars singleton module is instantiated for the tests.
257 bp += `
258 dex_bootjars {
259 name: "dex_bootjars",
260 }
261`
262
Colin Crossb1974532019-02-15 10:37:39 -0800263 return bp
264}
Paul Duffincee7e662020-07-09 17:32:57 +0100265
266func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
267 t.Helper()
268 module := ctx.ModuleForTests(name, variant).Module()
269 deps := []string{}
270 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
271 deps = append(deps, m.Name())
272 })
273 sort.Strings(deps)
274
275 if actual := deps; !reflect.DeepEqual(expected, actual) {
276 t.Errorf("expected %#q, found %#q", expected, actual)
277 }
278}
Paul Duffin4fd997b2021-02-03 20:06:33 +0000279
280func CheckHiddenAPIRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) {
Paul Duffin37856732021-02-26 14:24:15 +0000281 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +0000282 actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n"))
283 expected = strings.TrimSpace(expected)
284 if actual != expected {
285 t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual)
286 }
287}