blob: fc4e4775b1dde62b6523ca1cecd798c22ca05978 [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
Colin Crossb1974532019-02-15 10:37:39 -080098func GatherRequiredDepsForTest() string {
99 var bp string
100
101 extraModules := []string{
102 "core-lambda-stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800103 "ext",
Colin Crossb1974532019-02-15 10:37:39 -0800104 "android_stubs_current",
105 "android_system_stubs_current",
106 "android_test_stubs_current",
Jiyong Park50146e92020-01-30 18:00:15 +0900107 "android_module_lib_stubs_current",
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000108 "android_system_server_stubs_current",
Colin Crossb1974532019-02-15 10:37:39 -0800109 "core.current.stubs",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100110 "legacy.core.platform.api.stubs",
111 "stable.core.platform.api.stubs",
Colin Crossb1974532019-02-15 10:37:39 -0800112 "kotlin-stdlib",
Colin Cross0b03d972019-05-13 11:06:25 -0700113 "kotlin-stdlib-jdk7",
114 "kotlin-stdlib-jdk8",
Colin Crossb1974532019-02-15 10:37:39 -0800115 "kotlin-annotations",
116 }
117
118 for _, extra := range extraModules {
119 bp += fmt.Sprintf(`
120 java_library {
121 name: "%s",
122 srcs: ["a.java"],
Paul Duffin52d398a2019-06-11 12:31:14 +0100123 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100124 system_modules: "stable-core-platform-api-stubs-system-modules",
Liz Kammer5ca3a622020-08-05 15:40:41 -0700125 compile_dex: true,
Colin Crossb1974532019-02-15 10:37:39 -0800126 }
127 `, extra)
128 }
129
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100130 // For class loader context and <uses-library> tests.
131 dexpreoptModules := []string{"android.test.runner"}
132 dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...)
133 dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...)
134
135 for _, extra := range dexpreoptModules {
136 bp += fmt.Sprintf(`
137 java_library {
138 name: "%s",
139 srcs: ["a.java"],
140 sdk_version: "none",
141 system_modules: "stable-core-platform-api-stubs-system-modules",
142 compile_dex: true,
143 installable: true,
144 }
145 `, extra)
146 }
147
Colin Crossb1974532019-02-15 10:37:39 -0800148 bp += `
Colin Cross3047fa22019-04-18 10:56:44 -0700149 java_library {
150 name: "framework",
151 srcs: ["a.java"],
Paul Duffina3d09862019-06-11 13:40:47 +0100152 sdk_version: "none",
Pete Gillin84c38072020-07-09 18:03:41 +0100153 system_modules: "stable-core-platform-api-stubs-system-modules",
Colin Cross3047fa22019-04-18 10:56:44 -0700154 aidl: {
155 export_include_dirs: ["framework/aidl"],
156 },
157 }
158
Colin Crossb1974532019-02-15 10:37:39 -0800159 android_app {
160 name: "framework-res",
Paul Duffin50c217c2019-06-12 13:25:22 +0100161 sdk_version: "core_platform",
Ulya Trafimovich24813e12020-10-07 15:05:21 +0100162 }`
Colin Crossb1974532019-02-15 10:37:39 -0800163
164 systemModules := []string{
Neil Fullerba88c412018-10-21 22:57:26 +0100165 "core-current-stubs-system-modules",
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100166 "legacy-core-platform-api-stubs-system-modules",
Pete Gillin40a06422020-07-01 10:59:00 +0100167 "stable-core-platform-api-stubs-system-modules",
Colin Crossb1974532019-02-15 10:37:39 -0800168 }
169
170 for _, extra := range systemModules {
171 bp += fmt.Sprintf(`
172 java_system_modules {
Paul Duffin68289b02019-09-20 13:50:52 +0100173 name: "%[1]s",
174 libs: ["%[1]s-lib"],
175 }
176 java_library {
177 name: "%[1]s-lib",
178 sdk_version: "none",
179 system_modules: "none",
Colin Crossb1974532019-02-15 10:37:39 -0800180 }
181 `, extra)
182 }
183
184 return bp
185}
Paul Duffincee7e662020-07-09 17:32:57 +0100186
187func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
188 t.Helper()
189 module := ctx.ModuleForTests(name, variant).Module()
190 deps := []string{}
191 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
192 deps = append(deps, m.Name())
193 })
194 sort.Strings(deps)
195
196 if actual := deps; !reflect.DeepEqual(expected, actual) {
197 t.Errorf("expected %#q, found %#q", expected, actual)
198 }
199}