blob: eb514342b89d8af206ee55697cabfb5694e406ad [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 Stjernholm01407c52020-05-13 01:54:21 +0100158 visibility: ["//other/foo"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000159 jars: ["java/myjavalib.jar"],
160}
161
162java_import {
163 name: "myjavalib",
164 prefer: false,
Martin Stjernholm01407c52020-05-13 01:54:21 +0100165 visibility: ["//other/foo"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000166 jars: ["java/myjavalib.jar"],
167}
168
169java_import {
170 name: "mysdk_mypublicjavalib@current",
171 sdk_member_name: "mypublicjavalib",
172 visibility: ["//visibility:public"],
173 jars: ["java/mypublicjavalib.jar"],
174}
175
176java_import {
177 name: "mypublicjavalib",
178 prefer: false,
179 visibility: ["//visibility:public"],
180 jars: ["java/mypublicjavalib.jar"],
181}
182
183java_import {
184 name: "mysdk_mydefaultedjavalib@current",
185 sdk_member_name: "mydefaultedjavalib",
Martin Stjernholm01407c52020-05-13 01:54:21 +0100186 visibility: ["//other/bar"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000187 jars: ["java/mydefaultedjavalib.jar"],
188}
189
190java_import {
191 name: "mydefaultedjavalib",
192 prefer: false,
Martin Stjernholm01407c52020-05-13 01:54:21 +0100193 visibility: ["//other/bar"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000194 jars: ["java/mydefaultedjavalib.jar"],
195}
196
197sdk_snapshot {
198 name: "mysdk@current",
199 visibility: [
Martin Stjernholm01407c52020-05-13 01:54:21 +0100200 "//other/foo",
Paul Duffin593b3c92019-12-05 14:31:48 +0000201 "//package:__subpackages__",
202 ],
203 java_header_libs: [
204 "mysdk_myjavalib@current",
205 "mysdk_mypublicjavalib@current",
206 "mysdk_mydefaultedjavalib@current",
207 ],
208}
209`))
210}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000211
212func TestSDkInstall(t *testing.T) {
213 sdk := `
214 sdk {
215 name: "mysdk",
216 }
217 `
218 result := testSdkWithFs(t, ``,
219 map[string][]byte{
220 "Android.bp": []byte(sdk),
221 })
222
223 result.CheckSnapshot("mysdk", "",
224 checkAllOtherCopyRules(`.intermediates/mysdk/common_os/mysdk-current.zip -> mysdk-current.zip`),
225 )
226}
Paul Duffinb07fa512020-03-10 22:17:04 +0000227
228type EmbeddedPropertiesStruct struct {
Paul Duffin864e1b42020-05-06 10:23:19 +0100229 S_Embedded_Common string `android:"arch_variant"`
230 S_Embedded_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000231}
232
233type testPropertiesStruct struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100234 name string
Paul Duffinb07fa512020-03-10 22:17:04 +0000235 private string
236 Public_Kept string `sdk:"keep"`
237 S_Common string
Paul Duffin864e1b42020-05-06 10:23:19 +0100238 S_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000239 A_Common []string
Paul Duffin864e1b42020-05-06 10:23:19 +0100240 A_Different []string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000241 F_Common *bool
Paul Duffin864e1b42020-05-06 10:23:19 +0100242 F_Different *bool `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000243 EmbeddedPropertiesStruct
244}
245
Paul Duffinf34f6d82020-04-30 15:48:31 +0100246func (p *testPropertiesStruct) optimizableProperties() interface{} {
247 return p
248}
249
Paul Duffin4b8b7932020-05-06 12:35:38 +0100250func (p *testPropertiesStruct) String() string {
251 return p.name
252}
253
254var _ propertiesContainer = (*testPropertiesStruct)(nil)
255
Paul Duffinb07fa512020-03-10 22:17:04 +0000256func TestCommonValueOptimization(t *testing.T) {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100257 common := &testPropertiesStruct{name: "common"}
Paul Duffinf34f6d82020-04-30 15:48:31 +0100258 structs := []propertiesContainer{
Paul Duffinb07fa512020-03-10 22:17:04 +0000259 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100260 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000261 private: "common",
262 Public_Kept: "common",
263 S_Common: "common",
264 S_Different: "upper",
265 A_Common: []string{"first", "second"},
266 A_Different: []string{"alpha", "beta"},
267 F_Common: proptools.BoolPtr(false),
268 F_Different: proptools.BoolPtr(false),
269 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
270 S_Embedded_Common: "embedded_common",
271 S_Embedded_Different: "embedded_upper",
272 },
273 },
274 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100275 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000276 private: "common",
277 Public_Kept: "common",
278 S_Common: "common",
279 S_Different: "lower",
280 A_Common: []string{"first", "second"},
281 A_Different: []string{"alpha", "delta"},
282 F_Common: proptools.BoolPtr(false),
283 F_Different: proptools.BoolPtr(true),
284 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
285 S_Embedded_Common: "embedded_common",
286 S_Embedded_Different: "embedded_lower",
287 },
288 },
289 }
290
291 extractor := newCommonValueExtractor(common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000292
293 h := TestHelper{t}
Paul Duffinc459f892020-04-30 18:08:29 +0100294
295 err := extractor.extractCommonProperties(common, structs)
296 h.AssertDeepEquals("unexpected error", nil, err)
297
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100298 h.AssertDeepEquals("common properties not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000299 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100300 name: "common",
Paul Duffinb07fa512020-03-10 22:17:04 +0000301 private: "",
302 Public_Kept: "",
303 S_Common: "common",
304 S_Different: "",
305 A_Common: []string{"first", "second"},
306 A_Different: []string(nil),
307 F_Common: proptools.BoolPtr(false),
308 F_Different: nil,
309 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
310 S_Embedded_Common: "embedded_common",
311 S_Embedded_Different: "",
312 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100313 },
314 common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000315
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100316 h.AssertDeepEquals("updated properties[0] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000317 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100318 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000319 private: "common",
320 Public_Kept: "common",
321 S_Common: "",
322 S_Different: "upper",
323 A_Common: nil,
324 A_Different: []string{"alpha", "beta"},
325 F_Common: nil,
326 F_Different: proptools.BoolPtr(false),
327 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
328 S_Embedded_Common: "",
329 S_Embedded_Different: "embedded_upper",
330 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100331 },
332 structs[0])
Paul Duffinb07fa512020-03-10 22:17:04 +0000333
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100334 h.AssertDeepEquals("updated properties[1] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000335 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100336 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000337 private: "common",
338 Public_Kept: "common",
339 S_Common: "",
340 S_Different: "lower",
341 A_Common: nil,
342 A_Different: []string{"alpha", "delta"},
343 F_Common: nil,
344 F_Different: proptools.BoolPtr(true),
345 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
346 S_Embedded_Common: "",
347 S_Embedded_Different: "embedded_lower",
348 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100349 },
350 structs[1])
Paul Duffinb07fa512020-03-10 22:17:04 +0000351}
Paul Duffin864e1b42020-05-06 10:23:19 +0100352
353func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) {
354 common := &testPropertiesStruct{name: "common"}
355 structs := []propertiesContainer{
356 &testPropertiesStruct{
357 name: "struct-0",
358 S_Common: "should-be-but-is-not-common0",
359 },
360 &testPropertiesStruct{
361 name: "struct-1",
362 S_Common: "should-be-but-is-not-common1",
363 },
364 }
365
366 extractor := newCommonValueExtractor(common)
367
368 h := TestHelper{t}
369
370 err := extractor.extractCommonProperties(common, structs)
371 h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties:
372 "struct-0" has value "should-be-but-is-not-common0"
373 "struct-1" has value "should-be-but-is-not-common1"`, err)
374}