blob: 31ff47ff4ccf59ad18d7f9a7796db5a3be1ecaa7 [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"
21 "testing"
Colin Crossb1974532019-02-15 10:37:39 -080022
23 "android/soong/android"
Colin Crossf28329d2020-02-15 11:00:10 -080024 "android/soong/cc"
Ulya Trafimovich24813e12020-10-07 15:05:21 +010025 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070026 "android/soong/python"
27
Paul Duffincee7e662020-07-09 17:32:57 +010028 "github.com/google/blueprint"
Colin Crossb1974532019-02-15 10:37:39 -080029)
30
Colin Cross98be1bb2019-12-13 20:41:13 -080031func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config {
32 bp += GatherRequiredDepsForTest()
33
34 mockFS := map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -080035 "api/current.txt": nil,
36 "api/removed.txt": nil,
37 "api/system-current.txt": nil,
38 "api/system-removed.txt": nil,
39 "api/test-current.txt": nil,
40 "api/test-removed.txt": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080041
Anton Hanssondff2c782020-12-21 17:10:01 +000042 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
43 "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 +010044
Liz Kammerdd849a82020-06-12 16:38:45 -070045 "bin.py": nil,
46 python.StubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%'
47 MAIN_FILE = '%main%'`),
48
Paul Duffin0c5bae52020-06-02 13:00:08 +010049 // For java_sdk_library
Colin Cross300b2662020-06-11 12:12:30 -070050 "api/module-lib-current.txt": nil,
51 "api/module-lib-removed.txt": nil,
52 "api/system-server-current.txt": nil,
53 "api/system-server-removed.txt": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080054 }
55
Anton Hanssondff2c782020-12-21 17:10:01 +000056 levels := []string{"14", "28", "29", "30", "current"}
57 libs := []string{
58 "android", "foo", "bar", "sdklib", "barney", "betty", "foo-shared_library",
59 "foo-no_shared_library", "core-for-system-modules", "quuz", "qux", "fred",
60 "runtime-library",
61 }
62 for k, v := range prebuiltApisFilesForLibs(levels, libs) {
63 mockFS[k] = v
64 }
65
Colin Crossf28329d2020-02-15 11:00:10 -080066 cc.GatherRequiredFilesForTest(mockFS)
67
Colin Cross98be1bb2019-12-13 20:41:13 -080068 for k, v := range fs {
69 mockFS[k] = v
70 }
71
Colin Crossb1974532019-02-15 10:37:39 -080072 if env == nil {
73 env = make(map[string]string)
74 }
75 if env["ANDROID_JAVA8_HOME"] == "" {
76 env["ANDROID_JAVA8_HOME"] = "jdk8"
77 }
Colin Cross98be1bb2019-12-13 20:41:13 -080078 config := android.TestArchConfig(buildDir, env, bp, mockFS)
Colin Crossb1974532019-02-15 10:37:39 -080079
Colin Crossb1974532019-02-15 10:37:39 -080080 return config
81}
82
Anton Hanssondff2c782020-12-21 17:10:01 +000083func prebuiltApisFilesForLibs(apiLevels []string, sdkLibs []string) map[string][]byte {
84 fs := make(map[string][]byte)
85 for _, level := range apiLevels {
86 for _, lib := range sdkLibs {
87 for _, scope := range []string{"public", "system", "module-lib", "system-server", "test"} {
88 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, scope, lib)] = nil
Anton Hansson370fd0b2021-01-22 15:05:04 +000089 // No finalized API files for "current"
90 if level != "current" {
91 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, scope, lib)] = nil
92 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, scope, lib)] = nil
93 }
Anton Hanssondff2c782020-12-21 17:10:01 +000094 }
95 }
96 fs[fmt.Sprintf("prebuilts/sdk/%s/public/framework.aidl", level)] = nil
97 }
98 return fs
99}
100
Paul Duffinc059c8c2021-01-20 17:13:52 +0000101// Register build components provided by this package that are needed by tests.
102//
103// In particular this must register all the components that are used in the `Android.bp` snippet
104// returned by GatherRequiredDepsForTest()
105func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
106 RegisterAARBuildComponents(ctx)
107 RegisterAppBuildComponents(ctx)
108 RegisterAppImportBuildComponents(ctx)
109 RegisterAppSetBuildComponents(ctx)
Paul Duffin3451e162021-01-20 15:16:56 +0000110 RegisterBootImageBuildComponents(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000111 RegisterDexpreoptBootJarsComponents(ctx)
112 RegisterDocsBuildComponents(ctx)
113 RegisterGenRuleBuildComponents(ctx)
114 RegisterJavaBuildComponents(ctx)
115 RegisterPrebuiltApisBuildComponents(ctx)
116 RegisterRuntimeResourceOverlayBuildComponents(ctx)
117 RegisterSdkLibraryBuildComponents(ctx)
118 RegisterStubsBuildComponents(ctx)
119 RegisterSystemModulesBuildComponents(ctx)
Paul Duffin635aa082021-01-25 19:11:24 +0000120
121 // Make sure that any tool related module types needed by dexpreopt have been registered.
122 dexpreopt.RegisterToolModulesForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000123}
124
125// Gather the module definitions needed by tests that depend upon code from this package.
126//
127// Returns an `Android.bp` snippet that defines the modules that are needed by this package.
Colin Crossb1974532019-02-15 10:37:39 -0800128func GatherRequiredDepsForTest() string {
129 var bp string
130
131 extraModules := []string{
132 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800133 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800134 "android_stubs_current",
135 "android_system_stubs_current",
136 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900137 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000138 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800139 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100140 "legacy.core.platform.api.stubs",
141 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800142 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700143 "kotlin-stdlib-jdk7",
144 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800145 "kotlin-annotations",
146 }
147
148 for _, extra := range extraModules {
149 bp += fmt.Sprintf(`
150 java_library {
151 name: "%s",
152 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100153 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100154 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700155 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800156 }
157 `, extra)
158 }
159
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100160 // For class loader context and <uses-library> tests.
161 dexpreoptModules := []string{"android.test.runner"}
162 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
163 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
164
165 for _, extra := range dexpreoptModules {
166 bp += fmt.Sprintf(`
167 java_library {
168 name: "%s",
169 srcs: ["a.java"],
170 sdk_version: "none",
171 system_modules: "stable-core-platform-api-stubs-system-modules",
172 compile_dex: true,
173 installable: true,
174 }
175 `, extra)
176 }
177
Colin Crossb1974532019-02-15 10:37:39 -0800178 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700179 java_library {
180 name: "framework",
181 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100182 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100183 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700184 aidl: {
185 export_include_dirs: ["framework/aidl"],
186 },
187 }
188
Colin Crossb1974532019-02-15 10:37:39 -0800189 android_app {
190 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100191 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100192 }`
Colin Crossb1974532019-02-15 10:37:39 -0800193
194 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100195 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100196 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100197 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800198 }
199
200 for _, extra := range systemModules {
201 bp += fmt.Sprintf(`
202 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100203 name: "%[1]s",
204 libs: ["%[1]s-lib"],
205 }
206 java_library {
207 name: "%[1]s-lib",
208 sdk_version: "none",
209 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800210 }
211 `, extra)
212 }
213
Paul Duffin635aa082021-01-25 19:11:24 +0000214 // Make sure that any tools needed for dexpreopting are defined.
215 bp += dexpreopt.BpToolModulesForTest()
216
Paul Duffin1ab61862021-01-20 17:44:53 +0000217 // Make sure that the dex_bootjars singleton module is instantiated for the tests.
218 bp += `
219 dex_bootjars {
220 name: "dex_bootjars",
221 }
Paul Duffin3451e162021-01-20 15:16:56 +0000222
223 boot_image {
224 name: "art-boot-image",
225 image_name: "art",
226 }
227
228 boot_image {
229 name: "framework-boot-image",
230 image_name: "boot",
231 }
Paul Duffin1ab61862021-01-20 17:44:53 +0000232`
233
Colin Crossb1974532019-02-15 10:37:39 -0800234 return bp
235}
Paul Duffincee7e662020-07-09 17:32:57 +0100236
237func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
238 t.Helper()
239 module := ctx.ModuleForTests(name, variant).Module()
240 deps := []string{}
241 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
242 deps = append(deps, m.Name())
243 })
244 sort.Strings(deps)
245
246 if actual := deps; !reflect.DeepEqual(expected, actual) {
247 t.Errorf("expected %#q, found %#q", expected, actual)
248 }
249}