blob: bfa1e6b2a0e4088b9537303fe7e89c58dfba3bd5 [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
Colin Cross98be1bb2019-12-13 20:41:13 -080032func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config {
33 bp += GatherRequiredDepsForTest()
34
35 mockFS := map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -080036 "api/current.txt": nil,
37 "api/removed.txt": nil,
38 "api/system-current.txt": nil,
39 "api/system-removed.txt": nil,
40 "api/test-current.txt": nil,
41 "api/test-removed.txt": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080042
Anton Hanssondff2c782020-12-21 17:10:01 +000043 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
44 "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 +010045
Liz Kammerdd849a82020-06-12 16:38:45 -070046 "bin.py": nil,
47 python.StubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%'
48 MAIN_FILE = '%main%'`),
49
Paul Duffin0c5bae52020-06-02 13:00:08 +010050 // For java_sdk_library
Colin Cross300b2662020-06-11 12:12:30 -070051 "api/module-lib-current.txt": nil,
52 "api/module-lib-removed.txt": nil,
53 "api/system-server-current.txt": nil,
54 "api/system-server-removed.txt": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080055 }
56
Anton Hanssondff2c782020-12-21 17:10:01 +000057 levels := []string{"14", "28", "29", "30", "current"}
58 libs := []string{
59 "android", "foo", "bar", "sdklib", "barney", "betty", "foo-shared_library",
60 "foo-no_shared_library", "core-for-system-modules", "quuz", "qux", "fred",
61 "runtime-library",
62 }
63 for k, v := range prebuiltApisFilesForLibs(levels, libs) {
64 mockFS[k] = v
65 }
66
Colin Crossf28329d2020-02-15 11:00:10 -080067 cc.GatherRequiredFilesForTest(mockFS)
68
Colin Cross98be1bb2019-12-13 20:41:13 -080069 for k, v := range fs {
70 mockFS[k] = v
71 }
72
Colin Crossb1974532019-02-15 10:37:39 -080073 if env == nil {
74 env = make(map[string]string)
75 }
76 if env["ANDROID_JAVA8_HOME"] == "" {
77 env["ANDROID_JAVA8_HOME"] = "jdk8"
78 }
Colin Cross98be1bb2019-12-13 20:41:13 -080079 config := android.TestArchConfig(buildDir, env, bp, mockFS)
Colin Crossb1974532019-02-15 10:37:39 -080080
Colin Crossb1974532019-02-15 10:37:39 -080081 return config
82}
83
Anton Hanssondff2c782020-12-21 17:10:01 +000084func prebuiltApisFilesForLibs(apiLevels []string, sdkLibs []string) map[string][]byte {
85 fs := make(map[string][]byte)
86 for _, level := range apiLevels {
87 for _, lib := range sdkLibs {
88 for _, scope := range []string{"public", "system", "module-lib", "system-server", "test"} {
89 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, scope, lib)] = nil
Anton Hansson370fd0b2021-01-22 15:05:04 +000090 // No finalized API files for "current"
91 if level != "current" {
92 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, scope, lib)] = nil
93 fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, scope, lib)] = nil
94 }
Anton Hanssondff2c782020-12-21 17:10:01 +000095 }
96 }
97 fs[fmt.Sprintf("prebuilts/sdk/%s/public/framework.aidl", level)] = nil
98 }
99 return fs
100}
101
Paul Duffinc059c8c2021-01-20 17:13:52 +0000102// Register build components provided by this package that are needed by tests.
103//
104// In particular this must register all the components that are used in the `Android.bp` snippet
105// returned by GatherRequiredDepsForTest()
106func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
107 RegisterAARBuildComponents(ctx)
108 RegisterAppBuildComponents(ctx)
109 RegisterAppImportBuildComponents(ctx)
110 RegisterAppSetBuildComponents(ctx)
Paul Duffin3451e162021-01-20 15:16:56 +0000111 RegisterBootImageBuildComponents(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000112 RegisterDexpreoptBootJarsComponents(ctx)
113 RegisterDocsBuildComponents(ctx)
114 RegisterGenRuleBuildComponents(ctx)
115 RegisterJavaBuildComponents(ctx)
116 RegisterPrebuiltApisBuildComponents(ctx)
117 RegisterRuntimeResourceOverlayBuildComponents(ctx)
118 RegisterSdkLibraryBuildComponents(ctx)
119 RegisterStubsBuildComponents(ctx)
120 RegisterSystemModulesBuildComponents(ctx)
Paul Duffin635aa082021-01-25 19:11:24 +0000121
122 // Make sure that any tool related module types needed by dexpreopt have been registered.
123 dexpreopt.RegisterToolModulesForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000124}
125
126// Gather the module definitions needed by tests that depend upon code from this package.
127//
128// Returns an `Android.bp` snippet that defines the modules that are needed by this package.
Colin Crossb1974532019-02-15 10:37:39 -0800129func GatherRequiredDepsForTest() string {
130 var bp string
131
132 extraModules := []string{
133 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800134 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800135 "android_stubs_current",
136 "android_system_stubs_current",
137 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900138 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000139 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800140 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100141 "legacy.core.platform.api.stubs",
142 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800143 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700144 "kotlin-stdlib-jdk7",
145 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800146 "kotlin-annotations",
147 }
148
149 for _, extra := range extraModules {
150 bp += fmt.Sprintf(`
151 java_library {
152 name: "%s",
153 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100154 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100155 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700156 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800157 }
158 `, extra)
159 }
160
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100161 // For class loader context and <uses-library> tests.
162 dexpreoptModules := []string{"android.test.runner"}
163 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
164 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
165
166 for _, extra := range dexpreoptModules {
167 bp += fmt.Sprintf(`
168 java_library {
169 name: "%s",
170 srcs: ["a.java"],
171 sdk_version: "none",
172 system_modules: "stable-core-platform-api-stubs-system-modules",
173 compile_dex: true,
174 installable: true,
175 }
176 `, extra)
177 }
178
Colin Crossb1974532019-02-15 10:37:39 -0800179 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700180 java_library {
181 name: "framework",
182 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100183 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100184 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700185 aidl: {
186 export_include_dirs: ["framework/aidl"],
187 },
188 }
189
Colin Crossb1974532019-02-15 10:37:39 -0800190 android_app {
191 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100192 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100193 }`
Colin Crossb1974532019-02-15 10:37:39 -0800194
195 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100196 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100197 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100198 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800199 }
200
201 for _, extra := range systemModules {
202 bp += fmt.Sprintf(`
203 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100204 name: "%[1]s",
205 libs: ["%[1]s-lib"],
206 }
207 java_library {
208 name: "%[1]s-lib",
209 sdk_version: "none",
210 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800211 }
212 `, extra)
213 }
214
Paul Duffin635aa082021-01-25 19:11:24 +0000215 // Make sure that any tools needed for dexpreopting are defined.
216 bp += dexpreopt.BpToolModulesForTest()
217
Paul Duffin1ab61862021-01-20 17:44:53 +0000218 // Make sure that the dex_bootjars singleton module is instantiated for the tests.
219 bp += `
220 dex_bootjars {
221 name: "dex_bootjars",
222 }
223`
224
Colin Crossb1974532019-02-15 10:37:39 -0800225 return bp
226}
Paul Duffincee7e662020-07-09 17:32:57 +0100227
228func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
229 t.Helper()
230 module := ctx.ModuleForTests(name, variant).Module()
231 deps := []string{}
232 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
233 deps = append(deps, m.Name())
234 })
235 sort.Strings(deps)
236
237 if actual := deps; !reflect.DeepEqual(expected, actual) {
238 t.Errorf("expected %#q, found %#q", expected, actual)
239 }
240}
Paul Duffin4fd997b2021-02-03 20:06:33 +0000241
242func CheckHiddenAPIRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) {
Paul Duffin37856732021-02-26 14:24:15 +0000243 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +0000244 actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n"))
245 expected = strings.TrimSpace(expected)
246 if actual != expected {
247 t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual)
248 }
249}