blob: f5698346d1f6bed9ba9a2e887949d8159c17ab14 [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
89 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, scope, lib)] = nil
90 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, scope, lib)] = nil
91 }
92 }
93 fs[fmt.Sprintf("prebuilts/sdk/%s/public/framework.aidl", level)] = nil
94 }
95 return fs
96}
97
Paul Duffinc059c8c2021-01-20 17:13:52 +000098// Register build components provided by this package that are needed by tests.
99//
100// In particular this must register all the components that are used in the `Android.bp` snippet
101// returned by GatherRequiredDepsForTest()
102func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
103 RegisterAARBuildComponents(ctx)
104 RegisterAppBuildComponents(ctx)
105 RegisterAppImportBuildComponents(ctx)
106 RegisterAppSetBuildComponents(ctx)
107 RegisterDexpreoptBootJarsComponents(ctx)
108 RegisterDocsBuildComponents(ctx)
109 RegisterGenRuleBuildComponents(ctx)
110 RegisterJavaBuildComponents(ctx)
111 RegisterPrebuiltApisBuildComponents(ctx)
112 RegisterRuntimeResourceOverlayBuildComponents(ctx)
113 RegisterSdkLibraryBuildComponents(ctx)
114 RegisterStubsBuildComponents(ctx)
115 RegisterSystemModulesBuildComponents(ctx)
Paul Duffin635aa082021-01-25 19:11:24 +0000116
117 // Make sure that any tool related module types needed by dexpreopt have been registered.
118 dexpreopt.RegisterToolModulesForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000119}
120
121// Gather the module definitions needed by tests that depend upon code from this package.
122//
123// Returns an `Android.bp` snippet that defines the modules that are needed by this package.
Colin Crossb1974532019-02-15 10:37:39 -0800124func GatherRequiredDepsForTest() string {
125 var bp string
126
127 extraModules := []string{
128 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800129 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800130 "android_stubs_current",
131 "android_system_stubs_current",
132 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900133 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000134 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800135 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100136 "legacy.core.platform.api.stubs",
137 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800138 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700139 "kotlin-stdlib-jdk7",
140 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800141 "kotlin-annotations",
142 }
143
144 for _, extra := range extraModules {
145 bp += fmt.Sprintf(`
146 java_library {
147 name: "%s",
148 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100149 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100150 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700151 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800152 }
153 `, extra)
154 }
155
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100156 // For class loader context and <uses-library> tests.
157 dexpreoptModules := []string{"android.test.runner"}
158 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
159 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
160
161 for _, extra := range dexpreoptModules {
162 bp += fmt.Sprintf(`
163 java_library {
164 name: "%s",
165 srcs: ["a.java"],
166 sdk_version: "none",
167 system_modules: "stable-core-platform-api-stubs-system-modules",
168 compile_dex: true,
169 installable: true,
170 }
171 `, extra)
172 }
173
Colin Crossb1974532019-02-15 10:37:39 -0800174 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700175 java_library {
176 name: "framework",
177 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100178 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100179 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700180 aidl: {
181 export_include_dirs: ["framework/aidl"],
182 },
183 }
184
Colin Crossb1974532019-02-15 10:37:39 -0800185 android_app {
186 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100187 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100188 }`
Colin Crossb1974532019-02-15 10:37:39 -0800189
190 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100191 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100192 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100193 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800194 }
195
196 for _, extra := range systemModules {
197 bp += fmt.Sprintf(`
198 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100199 name: "%[1]s",
200 libs: ["%[1]s-lib"],
201 }
202 java_library {
203 name: "%[1]s-lib",
204 sdk_version: "none",
205 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800206 }
207 `, extra)
208 }
209
Paul Duffin635aa082021-01-25 19:11:24 +0000210 // Make sure that any tools needed for dexpreopting are defined.
211 bp += dexpreopt.BpToolModulesForTest()
212
Paul Duffin1ab61862021-01-20 17:44:53 +0000213 // Make sure that the dex_bootjars singleton module is instantiated for the tests.
214 bp += `
215 dex_bootjars {
216 name: "dex_bootjars",
217 }
218`
219
Colin Crossb1974532019-02-15 10:37:39 -0800220 return bp
221}
Paul Duffincee7e662020-07-09 17:32:57 +0100222
223func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
224 t.Helper()
225 module := ctx.ModuleForTests(name, variant).Module()
226 deps := []string{}
227 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
228 deps = append(deps, m.Name())
229 })
230 sort.Strings(deps)
231
232 if actual := deps; !reflect.DeepEqual(expected, actual) {
233 t.Errorf("expected %#q, found %#q", expected, actual)
234 }
235}