1// 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"
19	"reflect"
20	"sort"
21	"testing"
22
23	"android/soong/android"
24	"android/soong/cc"
25	"android/soong/python"
26
27	"github.com/google/blueprint"
28)
29
30func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config {
31	bp += GatherRequiredDepsForTest()
32
33	mockFS := map[string][]byte{
34		"api/current.txt":        nil,
35		"api/removed.txt":        nil,
36		"api/system-current.txt": nil,
37		"api/system-removed.txt": nil,
38		"api/test-current.txt":   nil,
39		"api/test-removed.txt":   nil,
40
41		"prebuilts/sdk/14/public/android.jar":                      nil,
42		"prebuilts/sdk/14/public/framework.aidl":                   nil,
43		"prebuilts/sdk/14/system/android.jar":                      nil,
44		"prebuilts/sdk/17/public/android.jar":                      nil,
45		"prebuilts/sdk/17/public/framework.aidl":                   nil,
46		"prebuilts/sdk/17/system/android.jar":                      nil,
47		"prebuilts/sdk/29/public/android.jar":                      nil,
48		"prebuilts/sdk/29/public/framework.aidl":                   nil,
49		"prebuilts/sdk/29/system/android.jar":                      nil,
50		"prebuilts/sdk/29/system/foo.jar":                          nil,
51		"prebuilts/sdk/30/public/android.jar":                      nil,
52		"prebuilts/sdk/30/public/framework.aidl":                   nil,
53		"prebuilts/sdk/30/system/android.jar":                      nil,
54		"prebuilts/sdk/30/system/foo.jar":                          nil,
55		"prebuilts/sdk/30/public/core-for-system-modules.jar":      nil,
56		"prebuilts/sdk/current/core/android.jar":                   nil,
57		"prebuilts/sdk/current/public/android.jar":                 nil,
58		"prebuilts/sdk/current/public/framework.aidl":              nil,
59		"prebuilts/sdk/current/public/core.jar":                    nil,
60		"prebuilts/sdk/current/public/core-for-system-modules.jar": nil,
61		"prebuilts/sdk/current/system/android.jar":                 nil,
62		"prebuilts/sdk/current/test/android.jar":                   nil,
63		"prebuilts/sdk/28/public/api/foo.txt":                      nil,
64		"prebuilts/sdk/28/system/api/foo.txt":                      nil,
65		"prebuilts/sdk/28/test/api/foo.txt":                        nil,
66		"prebuilts/sdk/28/public/api/foo-removed.txt":              nil,
67		"prebuilts/sdk/28/system/api/foo-removed.txt":              nil,
68		"prebuilts/sdk/28/test/api/foo-removed.txt":                nil,
69		"prebuilts/sdk/28/public/api/bar.txt":                      nil,
70		"prebuilts/sdk/28/system/api/bar.txt":                      nil,
71		"prebuilts/sdk/28/test/api/bar.txt":                        nil,
72		"prebuilts/sdk/28/public/api/bar-removed.txt":              nil,
73		"prebuilts/sdk/28/system/api/bar-removed.txt":              nil,
74		"prebuilts/sdk/28/test/api/bar-removed.txt":                nil,
75		"prebuilts/sdk/30/public/api/foo.txt":                      nil,
76		"prebuilts/sdk/30/system/api/foo.txt":                      nil,
77		"prebuilts/sdk/30/test/api/foo.txt":                        nil,
78		"prebuilts/sdk/30/public/api/foo-removed.txt":              nil,
79		"prebuilts/sdk/30/system/api/foo-removed.txt":              nil,
80		"prebuilts/sdk/30/test/api/foo-removed.txt":                nil,
81		"prebuilts/sdk/30/public/api/bar.txt":                      nil,
82		"prebuilts/sdk/30/system/api/bar.txt":                      nil,
83		"prebuilts/sdk/30/test/api/bar.txt":                        nil,
84		"prebuilts/sdk/30/public/api/bar-removed.txt":              nil,
85		"prebuilts/sdk/30/system/api/bar-removed.txt":              nil,
86		"prebuilts/sdk/30/test/api/bar-removed.txt":                nil,
87		"prebuilts/sdk/tools/core-lambda-stubs.jar":                nil,
88		"prebuilts/sdk/Android.bp":                                 []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "30", "current"],}`),
89
90		"bin.py": nil,
91		python.StubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%'
92		MAIN_FILE = '%main%'`),
93
94		// For java_sdk_library
95		"api/module-lib-current.txt":                        nil,
96		"api/module-lib-removed.txt":                        nil,
97		"api/system-server-current.txt":                     nil,
98		"api/system-server-removed.txt":                     nil,
99		"build/soong/scripts/gen-java-current-api-files.sh": nil,
100	}
101
102	cc.GatherRequiredFilesForTest(mockFS)
103
104	for k, v := range fs {
105		mockFS[k] = v
106	}
107
108	if env == nil {
109		env = make(map[string]string)
110	}
111	if env["ANDROID_JAVA8_HOME"] == "" {
112		env["ANDROID_JAVA8_HOME"] = "jdk8"
113	}
114	config := android.TestArchConfig(buildDir, env, bp, mockFS)
115
116	return config
117}
118
119func GatherRequiredDepsForTest() string {
120	var bp string
121
122	extraModules := []string{
123		"core-lambda-stubs",
124		"ext",
125		"android_stubs_current",
126		"android_system_stubs_current",
127		"android_test_stubs_current",
128		"android_module_lib_stubs_current",
129		"android_system_server_stubs_current",
130		"core.current.stubs",
131		"legacy.core.platform.api.stubs",
132		"stable.core.platform.api.stubs",
133		"kotlin-stdlib",
134		"kotlin-stdlib-jdk7",
135		"kotlin-stdlib-jdk8",
136		"kotlin-annotations",
137	}
138
139	for _, extra := range extraModules {
140		bp += fmt.Sprintf(`
141			java_library {
142				name: "%s",
143				srcs: ["a.java"],
144				sdk_version: "none",
145				system_modules: "stable-core-platform-api-stubs-system-modules",
146			}
147		`, extra)
148	}
149
150	bp += `
151		java_library {
152			name: "framework",
153			srcs: ["a.java"],
154			sdk_version: "none",
155			system_modules: "stable-core-platform-api-stubs-system-modules",
156			aidl: {
157				export_include_dirs: ["framework/aidl"],
158			},
159		}
160
161		android_app {
162			name: "framework-res",
163			sdk_version: "core_platform",
164		}
165
166		java_library {
167			name: "android.hidl.base-V1.0-java",
168			srcs: ["a.java"],
169			sdk_version: "none",
170			system_modules: "stable-core-platform-api-stubs-system-modules",
171			installable: true,
172		}
173
174		java_library {
175			name: "android.hidl.manager-V1.0-java",
176			srcs: ["a.java"],
177			sdk_version: "none",
178			system_modules: "stable-core-platform-api-stubs-system-modules",
179			installable: true,
180		}
181
182		java_library {
183			name: "org.apache.http.legacy",
184			srcs: ["a.java"],
185			sdk_version: "none",
186			system_modules: "stable-core-platform-api-stubs-system-modules",
187			installable: true,
188		}
189
190		java_library {
191			name: "android.test.base",
192			srcs: ["a.java"],
193			sdk_version: "none",
194			system_modules: "stable-core-platform-api-stubs-system-modules",
195			installable: true,
196		}
197
198		java_library {
199			name: "android.test.mock",
200			srcs: ["a.java"],
201			sdk_version: "none",
202			system_modules: "stable-core-platform-api-stubs-system-modules",
203			installable: true,
204		}
205	`
206
207	systemModules := []string{
208		"core-current-stubs-system-modules",
209		"legacy-core-platform-api-stubs-system-modules",
210		"stable-core-platform-api-stubs-system-modules",
211	}
212
213	for _, extra := range systemModules {
214		bp += fmt.Sprintf(`
215			java_system_modules {
216				name: "%[1]s",
217				libs: ["%[1]s-lib"],
218			}
219			java_library {
220				name: "%[1]s-lib",
221				sdk_version: "none",
222				system_modules: "none",
223			}
224		`, extra)
225	}
226
227	return bp
228}
229
230func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
231	t.Helper()
232	module := ctx.ModuleForTests(name, variant).Module()
233	deps := []string{}
234	ctx.VisitDirectDeps(module, func(m blueprint.Module) {
235		deps = append(deps, m.Name())
236	})
237	sort.Strings(deps)
238
239	if actual := deps; !reflect.DeepEqual(expected, actual) {
240		t.Errorf("expected %#q, found %#q", expected, actual)
241	}
242}
243