blob: ef3f1247228d412ce51761f513b19676ed0f2ef0 [file] [log] [blame]
Romain Jobredeaux1282c422021-10-29 10:52:59 -04001// Copyright 2021 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 bp2build
16
17import (
18 "android/soong/android"
19 "android/soong/java"
20
21 "testing"
22)
23
Sam Delmerico3177a6e2022-06-21 19:28:33 +000024func runAndroidAppTestCase(t *testing.T, tc Bp2buildTestCase) {
Romain Jobredeaux1282c422021-10-29 10:52:59 -040025 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000026 RunBp2BuildTestCase(t, registerAndroidAppModuleTypes, tc)
Romain Jobredeaux1282c422021-10-29 10:52:59 -040027}
28
29func registerAndroidAppModuleTypes(ctx android.RegistrationContext) {
Jingwen Chen6817bbb2022-10-14 09:56:07 +000030 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Alix6c087cf2023-01-05 21:12:31 +000031 ctx.RegisterModuleType("java_library", java.LibraryFactory)
Romain Jobredeaux1282c422021-10-29 10:52:59 -040032}
33
34func TestMinimalAndroidApp(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000035 runAndroidAppTestCase(t, Bp2buildTestCase{
36 Description: "Android app - simple example",
37 ModuleTypeUnderTest: "android_app",
38 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
39 Filesystem: map[string]string{
Romain Jobredeaux1282c422021-10-29 10:52:59 -040040 "app.java": "",
41 "res/res.png": "",
42 "AndroidManifest.xml": "",
43 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000044 Blueprint: `
Romain Jobredeaux1282c422021-10-29 10:52:59 -040045android_app {
46 name: "TestApp",
47 srcs: ["app.java"],
48 sdk_version: "current",
49}
50`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000051 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000052 MakeBazelTarget("android_binary", "TestApp", AttrNameToString{
Romain Jobredeaux1282c422021-10-29 10:52:59 -040053 "srcs": `["app.java"]`,
54 "manifest": `"AndroidManifest.xml"`,
55 "resource_files": `["res/res.png"]`,
56 }),
57 }})
58}
59
60func TestAndroidAppAllSupportedFields(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000061 runAndroidAppTestCase(t, Bp2buildTestCase{
62 Description: "Android app - all supported fields",
63 ModuleTypeUnderTest: "android_app",
64 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
65 Filesystem: map[string]string{
Romain Jobredeaux1282c422021-10-29 10:52:59 -040066 "app.java": "",
67 "resa/res.png": "",
68 "resb/res.png": "",
69 "manifest/AndroidManifest.xml": "",
70 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000071 Blueprint: simpleModuleDoNotConvertBp2build("android_app", "static_lib_dep") + `
Romain Jobredeaux1282c422021-10-29 10:52:59 -040072android_app {
73 name: "TestApp",
74 srcs: ["app.java"],
75 sdk_version: "current",
76 package_name: "com.google",
77 resource_dirs: ["resa", "resb"],
78 manifest: "manifest/AndroidManifest.xml",
Vinh Tran3ac6daf2022-04-22 19:09:58 -040079 static_libs: ["static_lib_dep"],
80 java_version: "7",
Jingwen Chen6817bbb2022-10-14 09:56:07 +000081 certificate: "foocert",
Romain Jobredeaux1282c422021-10-29 10:52:59 -040082}
83`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000084 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000085 MakeBazelTarget("android_binary", "TestApp", AttrNameToString{
Romain Jobredeaux1282c422021-10-29 10:52:59 -040086 "srcs": `["app.java"]`,
87 "manifest": `"manifest/AndroidManifest.xml"`,
88 "resource_files": `[
89 "resa/res.png",
90 "resb/res.png",
91 ]`,
Jingwen Chen6817bbb2022-10-14 09:56:07 +000092 "custom_package": `"com.google"`,
93 "deps": `[":static_lib_dep"]`,
94 "javacopts": `["-source 1.7 -target 1.7"]`,
95 "certificate_name": `"foocert"`,
Romain Jobredeaux1282c422021-10-29 10:52:59 -040096 }),
97 }})
98}
Sam Delmericoe91d0302022-02-23 15:28:33 +000099
100func TestAndroidAppArchVariantSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000101 runAndroidAppTestCase(t, Bp2buildTestCase{
102 Description: "Android app - arch variant srcs",
103 ModuleTypeUnderTest: "android_app",
104 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
105 Filesystem: map[string]string{
Sam Delmericoe91d0302022-02-23 15:28:33 +0000106 "arm.java": "",
107 "x86.java": "",
108 "res/res.png": "",
109 "AndroidManifest.xml": "",
110 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000111 Blueprint: `
Sam Delmericoe91d0302022-02-23 15:28:33 +0000112android_app {
113 name: "TestApp",
114 sdk_version: "current",
115 arch: {
116 arm: {
117 srcs: ["arm.java"],
118 },
119 x86: {
120 srcs: ["x86.java"],
121 }
122 }
123}
124`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000125 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000126 MakeBazelTarget("android_binary", "TestApp", AttrNameToString{
Sam Delmericoe91d0302022-02-23 15:28:33 +0000127 "srcs": `select({
128 "//build/bazel/platforms/arch:arm": ["arm.java"],
129 "//build/bazel/platforms/arch:x86": ["x86.java"],
130 "//conditions:default": [],
131 })`,
132 "manifest": `"AndroidManifest.xml"`,
133 "resource_files": `["res/res.png"]`,
Sam Delmericoe91d0302022-02-23 15:28:33 +0000134 }),
135 }})
136}
Jingwen Chen6817bbb2022-10-14 09:56:07 +0000137
138func TestAndroidAppCertIsModule(t *testing.T) {
139 runAndroidAppTestCase(t, Bp2buildTestCase{
140 Description: "Android app - cert is module",
141 ModuleTypeUnderTest: "android_app",
142 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
143 Filesystem: map[string]string{},
144 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + `
145android_app {
146 name: "TestApp",
147 certificate: ":foocert",
148}
149`,
150 ExpectedBazelTargets: []string{
151 MakeBazelTarget("android_binary", "TestApp", AttrNameToString{
152 "certificate": `":foocert"`,
153 "manifest": `"AndroidManifest.xml"`,
154 "resource_files": `[]`,
155 }),
156 }})
157}
158
159func TestAndroidAppCertIsSrcFile(t *testing.T) {
160 runAndroidAppTestCase(t, Bp2buildTestCase{
161 Description: "Android app - cert is src file",
162 ModuleTypeUnderTest: "android_app",
163 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
164 Filesystem: map[string]string{
165 "foocert": "",
166 },
167 Blueprint: `
168android_app {
169 name: "TestApp",
170 certificate: "foocert",
171}
172`,
173 ExpectedBazelTargets: []string{
174 MakeBazelTarget("android_binary", "TestApp", AttrNameToString{
175 "certificate": `"foocert"`,
176 "manifest": `"AndroidManifest.xml"`,
177 "resource_files": `[]`,
178 }),
179 }})
180}
181
182func TestAndroidAppCertIsNotSrcOrModule(t *testing.T) {
183 runAndroidAppTestCase(t, Bp2buildTestCase{
184 Description: "Android app - cert is not src or module",
185 ModuleTypeUnderTest: "android_app",
186 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
187 Filesystem: map[string]string{
188 // deliberate empty
189 },
190 Blueprint: `
191android_app {
192 name: "TestApp",
193 certificate: "foocert",
194}
195`,
196 ExpectedBazelTargets: []string{
197 MakeBazelTarget("android_binary", "TestApp", AttrNameToString{
198 "certificate_name": `"foocert"`,
199 "manifest": `"AndroidManifest.xml"`,
200 "resource_files": `[]`,
201 }),
202 }})
203}
Alix6c087cf2023-01-05 21:12:31 +0000204
205func TestAndroidAppLibs(t *testing.T) {
206 runAndroidAppTestCase(t, Bp2buildTestCase{
207 Description: "Android app with libs",
208 ModuleTypeUnderTest: "android_app",
209 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
210 Filesystem: map[string]string{},
211 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + `
212android_app {
213 name: "foo",
214 libs: ["barLib"]
215}
216java_library{
217 name: "barLib",
218}
219`,
220 ExpectedBazelTargets: []string{
221 MakeBazelTarget("java_library", "barLib", AttrNameToString{}),
222 MakeNeverlinkDuplicateTarget("java_library", "barLib"),
223 MakeBazelTarget("android_binary", "foo", AttrNameToString{
224 "manifest": `"AndroidManifest.xml"`,
225 "resource_files": `[]`,
226 "deps": `[":barLib-neverlink"]`,
227 }),
228 }})
229}
Alix0856f9e2023-01-20 22:15:30 +0000230
231func TestAndroidAppKotlinSrcs(t *testing.T) {
232 runAndroidAppTestCase(t, Bp2buildTestCase{
233 Description: "Android app with kotlin sources and common_srcs",
234 ModuleTypeUnderTest: "android_app",
235 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
236 Filesystem: map[string]string{
237 "res/res.png": "",
238 },
239 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + `
240android_app {
241 name: "foo",
242 srcs: ["a.java", "b.kt"],
243 certificate: ":foocert",
244 manifest: "fooManifest.xml",
245 libs: ["barLib"]
246}
247java_library{
248 name: "barLib",
249}
250`,
251 ExpectedBazelTargets: []string{
252 MakeBazelTarget("java_library", "barLib", AttrNameToString{}),
253 MakeNeverlinkDuplicateTarget("java_library", "barLib"),
254 MakeBazelTarget("android_library", "foo_kt", AttrNameToString{
255 "srcs": `[
256 "a.java",
257 "b.kt",
258 ]`,
259 "manifest": `"fooManifest.xml"`,
260 "resource_files": `["res/res.png"]`,
261 "deps": `[":barLib-neverlink"]`,
262 }),
263 MakeBazelTarget("android_binary", "foo", AttrNameToString{
264 "deps": `[":foo_kt"]`,
265 "certificate": `":foocert"`,
266 "manifest": `"fooManifest.xml"`,
267 }),
268 }})
269}
270
271func TestAndroidAppCommonSrcs(t *testing.T) {
272 runAndroidAppTestCase(t, Bp2buildTestCase{
273 Description: "Android app with common_srcs",
274 ModuleTypeUnderTest: "android_app",
275 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
276 Filesystem: map[string]string{
277 "res/res.png": "",
278 },
279 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + `
280android_app {
281 name: "foo",
282 srcs: ["a.java"],
283 common_srcs: ["b.kt"],
284 certificate: "foocert",
285 manifest: "fooManifest.xml",
286 libs: ["barLib"],
287}
288java_library{
289 name: "barLib",
290}
291`,
292 ExpectedBazelTargets: []string{
293 MakeBazelTarget("java_library", "barLib", AttrNameToString{}),
294 MakeNeverlinkDuplicateTarget("java_library", "barLib"),
295 MakeBazelTarget("android_library", "foo_kt", AttrNameToString{
296 "srcs": `["a.java"]`,
297 "common_srcs": `["b.kt"]`,
298 "manifest": `"fooManifest.xml"`,
299 "resource_files": `["res/res.png"]`,
300 "deps": `[":barLib-neverlink"]`,
301 }),
302 MakeBazelTarget("android_binary", "foo", AttrNameToString{
303 "deps": `[":foo_kt"]`,
304 "certificate_name": `"foocert"`,
305 "manifest": `"fooManifest.xml"`,
306 }),
307 }})
308}
Alixf848bf82023-03-06 19:43:55 +0000309
310func TestAndroidAppKotlinCflags(t *testing.T) {
311 runAndroidAppTestCase(t, Bp2buildTestCase{
312 Description: "Android app with kotlincflags",
313 ModuleTypeUnderTest: "android_app",
314 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
315 Filesystem: map[string]string{
316 "res/res.png": "",
317 },
318 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + `
319android_app {
320 name: "foo",
321 srcs: ["a.java", "b.kt"],
322 certificate: ":foocert",
323 manifest: "fooManifest.xml",
324 kotlincflags: ["-flag1", "-flag2"],
325}
326`,
327 ExpectedBazelTargets: []string{
328 MakeBazelTarget("android_library", "foo_kt", AttrNameToString{
329 "srcs": `[
330 "a.java",
331 "b.kt",
332 ]`,
333 "manifest": `"fooManifest.xml"`,
334 "resource_files": `["res/res.png"]`,
335 "kotlincflags": `[
336 "-flag1",
337 "-flag2",
338 ]`,
339 }),
340 MakeBazelTarget("android_binary", "foo", AttrNameToString{
341 "deps": `[":foo_kt"]`,
342 "certificate": `":foocert"`,
343 "manifest": `"fooManifest.xml"`,
344 }),
345 }})
346}
Alixe5085eb2023-01-31 19:38:41 +0000347
348func TestAndroidAppMinSdkProvided(t *testing.T) {
349 runAndroidAppTestCase(t, Bp2buildTestCase{
350 Description: "Android app with value for min_sdk_version",
351 ModuleTypeUnderTest: "android_app",
352 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
353 Filesystem: map[string]string{},
354 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + `
355android_app {
356 name: "foo",
357 sdk_version: "current",
358 min_sdk_version: "24",
359}
360`,
361 ExpectedBazelTargets: []string{
362 MakeBazelTarget("android_binary", "foo", AttrNameToString{
363 "manifest": `"AndroidManifest.xml"`,
364 "resource_files": `[]`,
365 "manifest_values": `{
366 "minSdkVersion": "24",
367 }`,
368 }),
369 }})
370}
371
372func TestAndroidAppMinSdkDefaultToSdkVersion(t *testing.T) {
373 runAndroidAppTestCase(t, Bp2buildTestCase{
374 Description: "Android app with value for sdk_version",
375 ModuleTypeUnderTest: "android_app",
376 ModuleTypeUnderTestFactory: java.AndroidAppFactory,
377 Filesystem: map[string]string{},
378 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + `
379android_app {
380 name: "foo",
381 sdk_version: "30",
382}
383`,
384 ExpectedBazelTargets: []string{
385 MakeBazelTarget("android_binary", "foo", AttrNameToString{
386 "manifest": `"AndroidManifest.xml"`,
387 "resource_files": `[]`,
388 "manifest_values": `{
389 "minSdkVersion": "30",
390 }`,
391 }),
392 }})
393}