blob: 56be7417b7ce5baad28e685f4b40048ea6eccbc6 [file] [log] [blame]
Jiyong Parkd1063c12019-07-17 20:08:41 +09001// 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 sdk
16
17import (
Jiyong Parkd1063c12019-07-17 20:08:41 +090018 "testing"
Paul Duffinb07fa512020-03-10 22:17:04 +000019
20 "github.com/google/blueprint/proptools"
Jiyong Parkd1063c12019-07-17 20:08:41 +090021)
22
Paul Duffin82d90432019-11-30 09:24:33 +000023// Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE.
24func TestMain(m *testing.M) {
25 runTestWithBuildDir(m)
Jiyong Parkd1063c12019-07-17 20:08:41 +090026}
27
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090028func TestDepNotInRequiredSdks(t *testing.T) {
29 testSdkError(t, `module "myjavalib".*depends on "otherlib".*that isn't part of the required SDKs:.*`, `
30 sdk {
31 name: "mysdk",
Paul Duffina0dbf432019-12-05 11:25:53 +000032 java_header_libs: ["sdkmember"],
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090033 }
34
35 sdk_snapshot {
36 name: "mysdk@1",
Paul Duffina0dbf432019-12-05 11:25:53 +000037 java_header_libs: ["sdkmember_mysdk_1"],
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090038 }
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_library {
53 name: "myjavalib",
54 srcs: ["Test.java"],
55 libs: [
56 "sdkmember",
57 "otherlib",
58 ],
59 system_modules: "none",
60 sdk_version: "none",
61 compile_dex: true,
62 host_supported: true,
Jooyung Han5e9013b2020-03-10 06:23:13 +090063 apex_available: ["myapex"],
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090064 }
65
66 // this lib is no in mysdk
67 java_library {
68 name: "otherlib",
69 srcs: ["Test.java"],
70 system_modules: "none",
71 sdk_version: "none",
72 compile_dex: true,
73 host_supported: true,
74 }
75
76 apex {
77 name: "myapex",
78 java_libs: ["myjavalib"],
79 uses_sdks: ["mysdk@1"],
80 key: "myapex.key",
81 certificate: ":myapex.cert",
82 }
83 `)
84}
Paul Duffin593b3c92019-12-05 14:31:48 +000085
86// Ensure that prebuilt modules have the same effective visibility as the source
87// modules.
88func TestSnapshotVisibility(t *testing.T) {
89 packageBp := `
90 package {
91 default_visibility: ["//other/foo"],
92 }
93
94 sdk {
95 name: "mysdk",
96 visibility: [
97 "//other/foo",
98 // This short form will be replaced with //package:__subpackages__ in the
99 // generated sdk_snapshot.
100 ":__subpackages__",
101 ],
102 java_header_libs: [
103 "myjavalib",
104 "mypublicjavalib",
105 "mydefaultedjavalib",
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100106 "myprivatejavalib",
Paul Duffin593b3c92019-12-05 14:31:48 +0000107 ],
108 }
109
110 java_library {
111 name: "myjavalib",
112 // Uses package default visibility
113 srcs: ["Test.java"],
114 system_modules: "none",
115 sdk_version: "none",
116 }
117
Paul Duffin44885e22020-02-19 16:10:09 +0000118 java_defaults {
119 name: "java-defaults",
Jooyung Han5e9013b2020-03-10 06:23:13 +0900120 visibility: ["//other/bar"],
Paul Duffin44885e22020-02-19 16:10:09 +0000121 }
122
Paul Duffin593b3c92019-12-05 14:31:48 +0000123 java_library {
124 name: "mypublicjavalib",
Paul Duffin44885e22020-02-19 16:10:09 +0000125 defaults: ["java-defaults"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000126 visibility: ["//visibility:public"],
127 srcs: ["Test.java"],
128 system_modules: "none",
129 sdk_version: "none",
130 }
131
132 java_defaults {
133 name: "myjavadefaults",
134 visibility: ["//other/bar"],
135 }
136
137 java_library {
138 name: "mydefaultedjavalib",
139 defaults: ["myjavadefaults"],
140 srcs: ["Test.java"],
141 system_modules: "none",
142 sdk_version: "none",
143 }
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100144
145 java_library {
146 name: "myprivatejavalib",
147 srcs: ["Test.java"],
148 visibility: ["//visibility:private"],
149 system_modules: "none",
150 sdk_version: "none",
151 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000152 `
153
154 result := testSdkWithFs(t, ``,
155 map[string][]byte{
156 "package/Test.java": nil,
157 "package/Android.bp": []byte(packageBp),
158 })
159
Paul Duffin1356d8c2020-02-25 19:26:33 +0000160 result.CheckSnapshot("mysdk", "package",
Paul Duffin593b3c92019-12-05 14:31:48 +0000161 checkAndroidBpContents(`
162// This is auto-generated. DO NOT EDIT.
163
164java_import {
165 name: "mysdk_myjavalib@current",
166 sdk_member_name: "myjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100167 visibility: [
168 "//other/foo",
169 "//package",
170 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000171 jars: ["java/myjavalib.jar"],
172}
173
174java_import {
175 name: "myjavalib",
176 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100177 visibility: [
178 "//other/foo",
179 "//package",
180 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000181 jars: ["java/myjavalib.jar"],
182}
183
184java_import {
185 name: "mysdk_mypublicjavalib@current",
186 sdk_member_name: "mypublicjavalib",
187 visibility: ["//visibility:public"],
188 jars: ["java/mypublicjavalib.jar"],
189}
190
191java_import {
192 name: "mypublicjavalib",
193 prefer: false,
194 visibility: ["//visibility:public"],
195 jars: ["java/mypublicjavalib.jar"],
196}
197
198java_import {
199 name: "mysdk_mydefaultedjavalib@current",
200 sdk_member_name: "mydefaultedjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100201 visibility: [
202 "//other/bar",
203 "//package",
204 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000205 jars: ["java/mydefaultedjavalib.jar"],
206}
207
208java_import {
209 name: "mydefaultedjavalib",
210 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100211 visibility: [
212 "//other/bar",
213 "//package",
214 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000215 jars: ["java/mydefaultedjavalib.jar"],
216}
217
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100218java_import {
219 name: "mysdk_myprivatejavalib@current",
220 sdk_member_name: "myprivatejavalib",
221 visibility: ["//package"],
222 jars: ["java/myprivatejavalib.jar"],
223}
224
225java_import {
226 name: "myprivatejavalib",
227 prefer: false,
228 visibility: ["//package"],
229 jars: ["java/myprivatejavalib.jar"],
230}
231
Paul Duffin593b3c92019-12-05 14:31:48 +0000232sdk_snapshot {
233 name: "mysdk@current",
234 visibility: [
Martin Stjernholm01407c52020-05-13 01:54:21 +0100235 "//other/foo",
Paul Duffin593b3c92019-12-05 14:31:48 +0000236 "//package:__subpackages__",
237 ],
238 java_header_libs: [
239 "mysdk_myjavalib@current",
240 "mysdk_mypublicjavalib@current",
241 "mysdk_mydefaultedjavalib@current",
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100242 "mysdk_myprivatejavalib@current",
Paul Duffin593b3c92019-12-05 14:31:48 +0000243 ],
244}
245`))
246}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000247
248func TestSDkInstall(t *testing.T) {
249 sdk := `
250 sdk {
251 name: "mysdk",
252 }
253 `
254 result := testSdkWithFs(t, ``,
255 map[string][]byte{
256 "Android.bp": []byte(sdk),
257 })
258
259 result.CheckSnapshot("mysdk", "",
260 checkAllOtherCopyRules(`.intermediates/mysdk/common_os/mysdk-current.zip -> mysdk-current.zip`),
261 )
262}
Paul Duffinb07fa512020-03-10 22:17:04 +0000263
264type EmbeddedPropertiesStruct struct {
Paul Duffin864e1b42020-05-06 10:23:19 +0100265 S_Embedded_Common string `android:"arch_variant"`
266 S_Embedded_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000267}
268
269type testPropertiesStruct struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100270 name string
Paul Duffinb07fa512020-03-10 22:17:04 +0000271 private string
272 Public_Kept string `sdk:"keep"`
273 S_Common string
Paul Duffin864e1b42020-05-06 10:23:19 +0100274 S_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000275 A_Common []string
Paul Duffin864e1b42020-05-06 10:23:19 +0100276 A_Different []string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000277 F_Common *bool
Paul Duffin864e1b42020-05-06 10:23:19 +0100278 F_Different *bool `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000279 EmbeddedPropertiesStruct
280}
281
Paul Duffinf34f6d82020-04-30 15:48:31 +0100282func (p *testPropertiesStruct) optimizableProperties() interface{} {
283 return p
284}
285
Paul Duffin4b8b7932020-05-06 12:35:38 +0100286func (p *testPropertiesStruct) String() string {
287 return p.name
288}
289
290var _ propertiesContainer = (*testPropertiesStruct)(nil)
291
Paul Duffinb07fa512020-03-10 22:17:04 +0000292func TestCommonValueOptimization(t *testing.T) {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100293 common := &testPropertiesStruct{name: "common"}
Paul Duffinf34f6d82020-04-30 15:48:31 +0100294 structs := []propertiesContainer{
Paul Duffinb07fa512020-03-10 22:17:04 +0000295 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100296 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000297 private: "common",
298 Public_Kept: "common",
299 S_Common: "common",
300 S_Different: "upper",
301 A_Common: []string{"first", "second"},
302 A_Different: []string{"alpha", "beta"},
303 F_Common: proptools.BoolPtr(false),
304 F_Different: proptools.BoolPtr(false),
305 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
306 S_Embedded_Common: "embedded_common",
307 S_Embedded_Different: "embedded_upper",
308 },
309 },
310 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100311 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000312 private: "common",
313 Public_Kept: "common",
314 S_Common: "common",
315 S_Different: "lower",
316 A_Common: []string{"first", "second"},
317 A_Different: []string{"alpha", "delta"},
318 F_Common: proptools.BoolPtr(false),
319 F_Different: proptools.BoolPtr(true),
320 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
321 S_Embedded_Common: "embedded_common",
322 S_Embedded_Different: "embedded_lower",
323 },
324 },
325 }
326
327 extractor := newCommonValueExtractor(common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000328
329 h := TestHelper{t}
Paul Duffinc459f892020-04-30 18:08:29 +0100330
331 err := extractor.extractCommonProperties(common, structs)
332 h.AssertDeepEquals("unexpected error", nil, err)
333
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100334 h.AssertDeepEquals("common properties not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000335 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100336 name: "common",
Paul Duffinb07fa512020-03-10 22:17:04 +0000337 private: "",
338 Public_Kept: "",
339 S_Common: "common",
340 S_Different: "",
341 A_Common: []string{"first", "second"},
342 A_Different: []string(nil),
343 F_Common: proptools.BoolPtr(false),
344 F_Different: nil,
345 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
346 S_Embedded_Common: "embedded_common",
347 S_Embedded_Different: "",
348 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100349 },
350 common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000351
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100352 h.AssertDeepEquals("updated properties[0] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000353 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100354 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000355 private: "common",
356 Public_Kept: "common",
357 S_Common: "",
358 S_Different: "upper",
359 A_Common: nil,
360 A_Different: []string{"alpha", "beta"},
361 F_Common: nil,
362 F_Different: proptools.BoolPtr(false),
363 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
364 S_Embedded_Common: "",
365 S_Embedded_Different: "embedded_upper",
366 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100367 },
368 structs[0])
Paul Duffinb07fa512020-03-10 22:17:04 +0000369
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100370 h.AssertDeepEquals("updated properties[1] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000371 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100372 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000373 private: "common",
374 Public_Kept: "common",
375 S_Common: "",
376 S_Different: "lower",
377 A_Common: nil,
378 A_Different: []string{"alpha", "delta"},
379 F_Common: nil,
380 F_Different: proptools.BoolPtr(true),
381 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
382 S_Embedded_Common: "",
383 S_Embedded_Different: "embedded_lower",
384 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100385 },
386 structs[1])
Paul Duffinb07fa512020-03-10 22:17:04 +0000387}
Paul Duffin864e1b42020-05-06 10:23:19 +0100388
389func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) {
390 common := &testPropertiesStruct{name: "common"}
391 structs := []propertiesContainer{
392 &testPropertiesStruct{
393 name: "struct-0",
394 S_Common: "should-be-but-is-not-common0",
395 },
396 &testPropertiesStruct{
397 name: "struct-1",
398 S_Common: "should-be-but-is-not-common1",
399 },
400 }
401
402 extractor := newCommonValueExtractor(common)
403
404 h := TestHelper{t}
405
406 err := extractor.extractCommonProperties(common, structs)
407 h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties:
408 "struct-0" has value "should-be-but-is-not-common0"
409 "struct-1" has value "should-be-but-is-not-common1"`, err)
410}