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