blob: 0b1e2eb741040c8b9639380b9aa1d94e77f95dfc [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)
116}
117
118// Gather the module definitions needed by tests that depend upon code from this package.
119//
120// Returns an `Android.bp` snippet that defines the modules that are needed by this package.
Colin Crossb1974532019-02-15 10:37:39 -0800121func GatherRequiredDepsForTest() string {
122 var bp string
123
124 extraModules := []string{
125 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800126 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800127 "android_stubs_current",
128 "android_system_stubs_current",
129 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900130 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000131 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800132 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100133 "legacy.core.platform.api.stubs",
134 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800135 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700136 "kotlin-stdlib-jdk7",
137 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800138 "kotlin-annotations",
139 }
140
141 for _, extra := range extraModules {
142 bp += fmt.Sprintf(`
143 java_library {
144 name: "%s",
145 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100146 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100147 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700148 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800149 }
150 `, extra)
151 }
152
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100153 // For class loader context and <uses-library> tests.
154 dexpreoptModules := []string{"android.test.runner"}
155 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
156 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
157
158 for _, extra := range dexpreoptModules {
159 bp += fmt.Sprintf(`
160 java_library {
161 name: "%s",
162 srcs: ["a.java"],
163 sdk_version: "none",
164 system_modules: "stable-core-platform-api-stubs-system-modules",
165 compile_dex: true,
166 installable: true,
167 }
168 `, extra)
169 }
170
Colin Crossb1974532019-02-15 10:37:39 -0800171 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700172 java_library {
173 name: "framework",
174 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100175 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100176 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700177 aidl: {
178 export_include_dirs: ["framework/aidl"],
179 },
180 }
181
Colin Crossb1974532019-02-15 10:37:39 -0800182 android_app {
183 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100184 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100185 }`
Colin Crossb1974532019-02-15 10:37:39 -0800186
187 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100188 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100189 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100190 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800191 }
192
193 for _, extra := range systemModules {
194 bp += fmt.Sprintf(`
195 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100196 name: "%[1]s",
197 libs: ["%[1]s-lib"],
198 }
199 java_library {
200 name: "%[1]s-lib",
201 sdk_version: "none",
202 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800203 }
204 `, extra)
205 }
206
Paul Duffin1ab61862021-01-20 17:44:53 +0000207 // Make sure that the dex_bootjars singleton module is instantiated for the tests.
208 bp += `
209 dex_bootjars {
210 name: "dex_bootjars",
211 }
212`
213
Colin Crossb1974532019-02-15 10:37:39 -0800214 return bp
215}
Paul Duffincee7e662020-07-09 17:32:57 +0100216
217func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
218 t.Helper()
219 module := ctx.ModuleForTests(name, variant).Module()
220 deps := []string{}
221 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
222 deps = append(deps, m.Name())
223 })
224 sort.Strings(deps)
225
226 if actual := deps; !reflect.DeepEqual(expected, actual) {
227 t.Errorf("expected %#q, found %#q", expected, actual)
228 }
229}