blob: 0b1f2d137295b35e7bdf9f82900e446ebc7c3a24 [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)
110 RegisterDexpreoptBootJarsComponents(ctx)
111 RegisterDocsBuildComponents(ctx)
112 RegisterGenRuleBuildComponents(ctx)
113 RegisterJavaBuildComponents(ctx)
114 RegisterPrebuiltApisBuildComponents(ctx)
115 RegisterRuntimeResourceOverlayBuildComponents(ctx)
116 RegisterSdkLibraryBuildComponents(ctx)
117 RegisterStubsBuildComponents(ctx)
118 RegisterSystemModulesBuildComponents(ctx)
Paul Duffin635aa082021-01-25 19:11:24 +0000119
120 // Make sure that any tool related module types needed by dexpreopt have been registered.
121 dexpreopt.RegisterToolModulesForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000122}
123
124// Gather the module definitions needed by tests that depend upon code from this package.
125//
126// Returns an `Android.bp` snippet that defines the modules that are needed by this package.
Colin Crossb1974532019-02-15 10:37:39 -0800127func GatherRequiredDepsForTest() string {
128 var bp string
129
130 extraModules := []string{
131 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800132 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800133 "android_stubs_current",
134 "android_system_stubs_current",
135 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900136 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000137 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800138 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100139 "legacy.core.platform.api.stubs",
140 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800141 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700142 "kotlin-stdlib-jdk7",
143 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800144 "kotlin-annotations",
145 }
146
147 for _, extra := range extraModules {
148 bp += fmt.Sprintf(`
149 java_library {
150 name: "%s",
151 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100152 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100153 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700154 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800155 }
156 `, extra)
157 }
158
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100159 // For class loader context and <uses-library> tests.
160 dexpreoptModules := []string{"android.test.runner"}
161 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
162 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
163
164 for _, extra := range dexpreoptModules {
165 bp += fmt.Sprintf(`
166 java_library {
167 name: "%s",
168 srcs: ["a.java"],
169 sdk_version: "none",
170 system_modules: "stable-core-platform-api-stubs-system-modules",
171 compile_dex: true,
172 installable: true,
173 }
174 `, extra)
175 }
176
Colin Crossb1974532019-02-15 10:37:39 -0800177 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700178 java_library {
179 name: "framework",
180 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100181 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100182 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700183 aidl: {
184 export_include_dirs: ["framework/aidl"],
185 },
186 }
187
Colin Crossb1974532019-02-15 10:37:39 -0800188 android_app {
189 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100190 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100191 }`
Colin Crossb1974532019-02-15 10:37:39 -0800192
193 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100194 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100195 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100196 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800197 }
198
199 for _, extra := range systemModules {
200 bp += fmt.Sprintf(`
201 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100202 name: "%[1]s",
203 libs: ["%[1]s-lib"],
204 }
205 java_library {
206 name: "%[1]s-lib",
207 sdk_version: "none",
208 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800209 }
210 `, extra)
211 }
212
Paul Duffin635aa082021-01-25 19:11:24 +0000213 // Make sure that any tools needed for dexpreopting are defined.
214 bp += dexpreopt.BpToolModulesForTest()
215
Paul Duffin1ab61862021-01-20 17:44:53 +0000216 // Make sure that the dex_bootjars singleton module is instantiated for the tests.
217 bp += `
218 dex_bootjars {
219 name: "dex_bootjars",
220 }
221`
222
Colin Crossb1974532019-02-15 10:37:39 -0800223 return bp
224}
Paul Duffincee7e662020-07-09 17:32:57 +0100225
226func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
227 t.Helper()
228 module := ctx.ModuleForTests(name, variant).Module()
229 deps := []string{}
230 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
231 deps = append(deps, m.Name())
232 })
233 sort.Strings(deps)
234
235 if actual := deps; !reflect.DeepEqual(expected, actual) {
236 t.Errorf("expected %#q, found %#q", expected, actual)
237 }
238}