blob: e87a82c10fa3e895d3df7c8f90257bd26ff97a96 [file] [log] [blame]
Paul Duffina80fdec2019-12-03 15:25:00 +00001// Copyright (C) 2019 The Android Open Source Project
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 sdk
16
17import (
18 "testing"
19)
20
Paul Duffind835daa2019-11-30 17:49:09 +000021func testSdkWithJava(t *testing.T, bp string) *testSdkResult {
22 t.Helper()
23
24 fs := map[string][]byte{
25 "Test.java": nil,
26 "aidl/foo/bar/Test.aidl": nil,
27 }
28 return testSdkWithFs(t, bp, fs)
29}
30
Paul Duffina80fdec2019-12-03 15:25:00 +000031// Contains tests for SDK members provided by the java package.
32
33func TestBasicSdkWithJavaLibrary(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +000034 result := testSdkWithJava(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +000035 sdk {
36 name: "mysdk",
37 java_libs: ["myjavalib"],
38 }
39
40 sdk_snapshot {
41 name: "mysdk@1",
42 java_libs: ["sdkmember_mysdk_1"],
43 }
44
45 sdk_snapshot {
46 name: "mysdk@2",
47 java_libs: ["sdkmember_mysdk_2"],
48 }
49
50 java_import {
51 name: "sdkmember",
52 prefer: false,
53 host_supported: true,
54 }
55
56 java_import {
57 name: "sdkmember_mysdk_1",
58 sdk_member_name: "sdkmember",
59 host_supported: true,
60 }
61
62 java_import {
63 name: "sdkmember_mysdk_2",
64 sdk_member_name: "sdkmember",
65 host_supported: true,
66 }
67
68 java_library {
69 name: "myjavalib",
70 srcs: ["Test.java"],
71 libs: ["sdkmember"],
72 system_modules: "none",
73 sdk_version: "none",
74 compile_dex: true,
75 host_supported: true,
76 }
77
78 apex {
79 name: "myapex",
80 java_libs: ["myjavalib"],
81 uses_sdks: ["mysdk@1"],
82 key: "myapex.key",
83 certificate: ":myapex.cert",
84 }
85
86 apex {
87 name: "myapex2",
88 java_libs: ["myjavalib"],
89 uses_sdks: ["mysdk@2"],
90 key: "myapex.key",
91 certificate: ":myapex.cert",
92 }
93 `)
94
95 sdkMemberV1 := result.ctx.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output
96 sdkMemberV2 := result.ctx.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output
97
98 javalibForMyApex := result.ctx.ModuleForTests("myjavalib", "android_common_myapex")
99 javalibForMyApex2 := result.ctx.ModuleForTests("myjavalib", "android_common_myapex2")
100
101 // Depending on the uses_sdks value, different libs are linked
102 ensureListContains(t, pathsToStrings(javalibForMyApex.Rule("javac").Implicits), sdkMemberV1.String())
103 ensureListContains(t, pathsToStrings(javalibForMyApex2.Rule("javac").Implicits), sdkMemberV2.String())
104}
105
106func TestSnapshotWithJavaLibrary(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000107 result := testSdkWithJava(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +0000108 sdk {
109 name: "mysdk",
110 java_libs: ["myjavalib"],
111 }
112
113 java_library {
114 name: "myjavalib",
115 srcs: ["Test.java"],
116 aidl: {
117 export_include_dirs: ["aidl"],
118 },
119 system_modules: "none",
120 sdk_version: "none",
121 compile_dex: true,
122 host_supported: true,
123 }
124 `)
125
126 result.CheckSnapshot("mysdk", "android_common",
127 checkAndroidBpContents(`
128// This is auto-generated. DO NOT EDIT.
129
130java_import {
131 name: "mysdk_myjavalib@current",
132 sdk_member_name: "myjavalib",
133 jars: ["java/myjavalib.jar"],
134}
135
136java_import {
137 name: "myjavalib",
138 prefer: false,
139 jars: ["java/myjavalib.jar"],
140}
141
142sdk_snapshot {
143 name: "mysdk@current",
144 java_libs: ["mysdk_myjavalib@current"],
145}
146
147`),
148 checkAllCopyRules(`
149.intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar -> java/myjavalib.jar
150aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
151`),
152 )
153}
154
155func TestHostSnapshotWithJavaLibrary(t *testing.T) {
156 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
157 SkipIfNotLinux(t)
158
Paul Duffind835daa2019-11-30 17:49:09 +0000159 result := testSdkWithJava(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +0000160 sdk {
161 name: "mysdk",
162 device_supported: false,
163 host_supported: true,
164 java_libs: ["myjavalib"],
165 }
166
167 java_library {
168 name: "myjavalib",
169 device_supported: false,
170 host_supported: true,
171 srcs: ["Test.java"],
172 aidl: {
173 export_include_dirs: ["aidl"],
174 },
175 system_modules: "none",
176 sdk_version: "none",
177 compile_dex: true,
178 }
179 `)
180
181 result.CheckSnapshot("mysdk", "linux_glibc_common",
182 checkAndroidBpContents(`
183// This is auto-generated. DO NOT EDIT.
184
185java_import {
186 name: "mysdk_myjavalib@current",
187 sdk_member_name: "myjavalib",
188 device_supported: false,
189 host_supported: true,
190 jars: ["java/myjavalib.jar"],
191}
192
193java_import {
194 name: "myjavalib",
195 prefer: false,
196 device_supported: false,
197 host_supported: true,
198 jars: ["java/myjavalib.jar"],
199}
200
201sdk_snapshot {
202 name: "mysdk@current",
203 device_supported: false,
204 host_supported: true,
205 java_libs: ["mysdk_myjavalib@current"],
206}
207`),
208 checkAllCopyRules(`
209.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar
210aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
211`),
212 )
213}
214
Paul Duffind835daa2019-11-30 17:49:09 +0000215func testSdkWithDroidstubs(t *testing.T, bp string) *testSdkResult {
216 t.Helper()
217
218 fs := map[string][]byte{
219 "foo/bar/Foo.java": nil,
220 "stubs-sources/foo/bar/Foo.java": nil,
221 }
222 return testSdkWithFs(t, bp, fs)
223}
224
Paul Duffina80fdec2019-12-03 15:25:00 +0000225// Note: This test does not verify that a droidstubs can be referenced, either
226// directly or indirectly from an APEX as droidstubs can never be a part of an
227// apex.
228func TestBasicSdkWithDroidstubs(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000229 testSdkWithDroidstubs(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +0000230 sdk {
231 name: "mysdk",
232 stubs_sources: ["mystub"],
233 }
234 sdk_snapshot {
235 name: "mysdk@10",
236 stubs_sources: ["mystub_mysdk@10"],
237 }
238 prebuilt_stubs_sources {
239 name: "mystub_mysdk@10",
240 sdk_member_name: "mystub",
241 srcs: ["stubs-sources/foo/bar/Foo.java"],
242 }
243 droidstubs {
244 name: "mystub",
245 srcs: ["foo/bar/Foo.java"],
246 sdk_version: "none",
247 system_modules: "none",
248 }
249 java_library {
250 name: "myjavalib",
251 srcs: [":mystub"],
252 sdk_version: "none",
253 system_modules: "none",
254 }
255 `)
256}
257
258func TestSnapshotWithDroidstubs(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000259 result := testSdkWithDroidstubs(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +0000260 sdk {
261 name: "mysdk",
262 stubs_sources: ["myjavaapistubs"],
263 }
264
265 droidstubs {
266 name: "myjavaapistubs",
267 srcs: ["foo/bar/Foo.java"],
268 system_modules: "none",
269 sdk_version: "none",
270 }
271 `)
272
273 result.CheckSnapshot("mysdk", "android_common",
274 checkAndroidBpContents(`
275// This is auto-generated. DO NOT EDIT.
276
277prebuilt_stubs_sources {
278 name: "mysdk_myjavaapistubs@current",
279 sdk_member_name: "myjavaapistubs",
280 srcs: ["java/myjavaapistubs_stubs_sources"],
281}
282
283prebuilt_stubs_sources {
284 name: "myjavaapistubs",
285 prefer: false,
286 srcs: ["java/myjavaapistubs_stubs_sources"],
287}
288
289sdk_snapshot {
290 name: "mysdk@current",
291 stubs_sources: ["mysdk_myjavaapistubs@current"],
292}
293
294`),
295 checkAllCopyRules(""),
296 checkMergeZip(".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"),
297 )
298}
299
300func TestHostSnapshotWithDroidstubs(t *testing.T) {
301 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
302 SkipIfNotLinux(t)
303
Paul Duffind835daa2019-11-30 17:49:09 +0000304 result := testSdkWithDroidstubs(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +0000305 sdk {
306 name: "mysdk",
307 device_supported: false,
308 host_supported: true,
309 stubs_sources: ["myjavaapistubs"],
310 }
311
312 droidstubs {
313 name: "myjavaapistubs",
314 device_supported: false,
315 host_supported: true,
316 srcs: ["foo/bar/Foo.java"],
317 system_modules: "none",
318 sdk_version: "none",
319 }
320 `)
321
322 result.CheckSnapshot("mysdk", "linux_glibc_common",
323 checkAndroidBpContents(`
324// This is auto-generated. DO NOT EDIT.
325
326prebuilt_stubs_sources {
327 name: "mysdk_myjavaapistubs@current",
328 sdk_member_name: "myjavaapistubs",
329 device_supported: false,
330 host_supported: true,
331 srcs: ["java/myjavaapistubs_stubs_sources"],
332}
333
334prebuilt_stubs_sources {
335 name: "myjavaapistubs",
336 prefer: false,
337 device_supported: false,
338 host_supported: true,
339 srcs: ["java/myjavaapistubs_stubs_sources"],
340}
341
342sdk_snapshot {
343 name: "mysdk@current",
344 device_supported: false,
345 host_supported: true,
346 stubs_sources: ["mysdk_myjavaapistubs@current"],
347}
348`),
349 checkAllCopyRules(""),
350 checkMergeZip(".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"),
351 )
352}