blob: 9bbf410b716deea5300bab17383ee9400114be42 [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",
106 ],
107 }
108
109 java_library {
110 name: "myjavalib",
111 // Uses package default visibility
112 srcs: ["Test.java"],
113 system_modules: "none",
114 sdk_version: "none",
115 }
116
Paul Duffin44885e22020-02-19 16:10:09 +0000117 java_defaults {
118 name: "java-defaults",
Jooyung Han5e9013b2020-03-10 06:23:13 +0900119 visibility: ["//other/bar"],
Paul Duffin44885e22020-02-19 16:10:09 +0000120 }
121
Paul Duffin593b3c92019-12-05 14:31:48 +0000122 java_library {
123 name: "mypublicjavalib",
Paul Duffin44885e22020-02-19 16:10:09 +0000124 defaults: ["java-defaults"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000125 visibility: ["//visibility:public"],
126 srcs: ["Test.java"],
127 system_modules: "none",
128 sdk_version: "none",
129 }
130
131 java_defaults {
132 name: "myjavadefaults",
133 visibility: ["//other/bar"],
134 }
135
136 java_library {
137 name: "mydefaultedjavalib",
138 defaults: ["myjavadefaults"],
139 srcs: ["Test.java"],
140 system_modules: "none",
141 sdk_version: "none",
142 }
143 `
144
145 result := testSdkWithFs(t, ``,
146 map[string][]byte{
147 "package/Test.java": nil,
148 "package/Android.bp": []byte(packageBp),
149 })
150
Paul Duffin1356d8c2020-02-25 19:26:33 +0000151 result.CheckSnapshot("mysdk", "package",
Paul Duffin593b3c92019-12-05 14:31:48 +0000152 checkAndroidBpContents(`
153// This is auto-generated. DO NOT EDIT.
154
155java_import {
156 name: "mysdk_myjavalib@current",
157 sdk_member_name: "myjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100158 visibility: [
159 "//other/foo",
160 "//package",
161 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000162 jars: ["java/myjavalib.jar"],
163}
164
165java_import {
166 name: "myjavalib",
167 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100168 visibility: [
169 "//other/foo",
170 "//package",
171 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000172 jars: ["java/myjavalib.jar"],
173}
174
175java_import {
176 name: "mysdk_mypublicjavalib@current",
177 sdk_member_name: "mypublicjavalib",
178 visibility: ["//visibility:public"],
179 jars: ["java/mypublicjavalib.jar"],
180}
181
182java_import {
183 name: "mypublicjavalib",
184 prefer: false,
185 visibility: ["//visibility:public"],
186 jars: ["java/mypublicjavalib.jar"],
187}
188
189java_import {
190 name: "mysdk_mydefaultedjavalib@current",
191 sdk_member_name: "mydefaultedjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100192 visibility: [
193 "//other/bar",
194 "//package",
195 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000196 jars: ["java/mydefaultedjavalib.jar"],
197}
198
199java_import {
200 name: "mydefaultedjavalib",
201 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100202 visibility: [
203 "//other/bar",
204 "//package",
205 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000206 jars: ["java/mydefaultedjavalib.jar"],
207}
208
209sdk_snapshot {
210 name: "mysdk@current",
211 visibility: [
Martin Stjernholm01407c52020-05-13 01:54:21 +0100212 "//other/foo",
Paul Duffin593b3c92019-12-05 14:31:48 +0000213 "//package:__subpackages__",
214 ],
215 java_header_libs: [
216 "mysdk_myjavalib@current",
217 "mysdk_mypublicjavalib@current",
218 "mysdk_mydefaultedjavalib@current",
219 ],
220}
221`))
222}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000223
224func TestSDkInstall(t *testing.T) {
225 sdk := `
226 sdk {
227 name: "mysdk",
228 }
229 `
230 result := testSdkWithFs(t, ``,
231 map[string][]byte{
232 "Android.bp": []byte(sdk),
233 })
234
235 result.CheckSnapshot("mysdk", "",
236 checkAllOtherCopyRules(`.intermediates/mysdk/common_os/mysdk-current.zip -> mysdk-current.zip`),
237 )
238}
Paul Duffinb07fa512020-03-10 22:17:04 +0000239
240type EmbeddedPropertiesStruct struct {
Paul Duffin864e1b42020-05-06 10:23:19 +0100241 S_Embedded_Common string `android:"arch_variant"`
242 S_Embedded_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000243}
244
245type testPropertiesStruct struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100246 name string
Paul Duffinb07fa512020-03-10 22:17:04 +0000247 private string
248 Public_Kept string `sdk:"keep"`
249 S_Common string
Paul Duffin864e1b42020-05-06 10:23:19 +0100250 S_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000251 A_Common []string
Paul Duffin864e1b42020-05-06 10:23:19 +0100252 A_Different []string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000253 F_Common *bool
Paul Duffin864e1b42020-05-06 10:23:19 +0100254 F_Different *bool `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000255 EmbeddedPropertiesStruct
256}
257
Paul Duffinf34f6d82020-04-30 15:48:31 +0100258func (p *testPropertiesStruct) optimizableProperties() interface{} {
259 return p
260}
261
Paul Duffin4b8b7932020-05-06 12:35:38 +0100262func (p *testPropertiesStruct) String() string {
263 return p.name
264}
265
266var _ propertiesContainer = (*testPropertiesStruct)(nil)
267
Paul Duffinb07fa512020-03-10 22:17:04 +0000268func TestCommonValueOptimization(t *testing.T) {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100269 common := &testPropertiesStruct{name: "common"}
Paul Duffinf34f6d82020-04-30 15:48:31 +0100270 structs := []propertiesContainer{
Paul Duffinb07fa512020-03-10 22:17:04 +0000271 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100272 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000273 private: "common",
274 Public_Kept: "common",
275 S_Common: "common",
276 S_Different: "upper",
277 A_Common: []string{"first", "second"},
278 A_Different: []string{"alpha", "beta"},
279 F_Common: proptools.BoolPtr(false),
280 F_Different: proptools.BoolPtr(false),
281 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
282 S_Embedded_Common: "embedded_common",
283 S_Embedded_Different: "embedded_upper",
284 },
285 },
286 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100287 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000288 private: "common",
289 Public_Kept: "common",
290 S_Common: "common",
291 S_Different: "lower",
292 A_Common: []string{"first", "second"},
293 A_Different: []string{"alpha", "delta"},
294 F_Common: proptools.BoolPtr(false),
295 F_Different: proptools.BoolPtr(true),
296 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
297 S_Embedded_Common: "embedded_common",
298 S_Embedded_Different: "embedded_lower",
299 },
300 },
301 }
302
303 extractor := newCommonValueExtractor(common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000304
305 h := TestHelper{t}
Paul Duffinc459f892020-04-30 18:08:29 +0100306
307 err := extractor.extractCommonProperties(common, structs)
308 h.AssertDeepEquals("unexpected error", nil, err)
309
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100310 h.AssertDeepEquals("common properties not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000311 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100312 name: "common",
Paul Duffinb07fa512020-03-10 22:17:04 +0000313 private: "",
314 Public_Kept: "",
315 S_Common: "common",
316 S_Different: "",
317 A_Common: []string{"first", "second"},
318 A_Different: []string(nil),
319 F_Common: proptools.BoolPtr(false),
320 F_Different: nil,
321 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
322 S_Embedded_Common: "embedded_common",
323 S_Embedded_Different: "",
324 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100325 },
326 common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000327
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100328 h.AssertDeepEquals("updated properties[0] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000329 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100330 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000331 private: "common",
332 Public_Kept: "common",
333 S_Common: "",
334 S_Different: "upper",
335 A_Common: nil,
336 A_Different: []string{"alpha", "beta"},
337 F_Common: nil,
338 F_Different: proptools.BoolPtr(false),
339 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
340 S_Embedded_Common: "",
341 S_Embedded_Different: "embedded_upper",
342 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100343 },
344 structs[0])
Paul Duffinb07fa512020-03-10 22:17:04 +0000345
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100346 h.AssertDeepEquals("updated properties[1] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000347 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100348 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000349 private: "common",
350 Public_Kept: "common",
351 S_Common: "",
352 S_Different: "lower",
353 A_Common: nil,
354 A_Different: []string{"alpha", "delta"},
355 F_Common: nil,
356 F_Different: proptools.BoolPtr(true),
357 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
358 S_Embedded_Common: "",
359 S_Embedded_Different: "embedded_lower",
360 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100361 },
362 structs[1])
Paul Duffinb07fa512020-03-10 22:17:04 +0000363}
Paul Duffin864e1b42020-05-06 10:23:19 +0100364
365func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) {
366 common := &testPropertiesStruct{name: "common"}
367 structs := []propertiesContainer{
368 &testPropertiesStruct{
369 name: "struct-0",
370 S_Common: "should-be-but-is-not-common0",
371 },
372 &testPropertiesStruct{
373 name: "struct-1",
374 S_Common: "should-be-but-is-not-common1",
375 },
376 }
377
378 extractor := newCommonValueExtractor(common)
379
380 h := TestHelper{t}
381
382 err := extractor.extractCommonProperties(common, structs)
383 h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties:
384 "struct-0" has value "should-be-but-is-not-common0"
385 "struct-1" has value "should-be-but-is-not-common1"`, err)
386}