blob: 898ecea6839eaa5e383b6ac9f400db3aa9cab9a3 [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",
158 visibility: ["//other/foo:__pkg__"],
159 jars: ["java/myjavalib.jar"],
160}
161
162java_import {
163 name: "myjavalib",
164 prefer: false,
165 visibility: ["//other/foo:__pkg__"],
166 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",
186 visibility: ["//other/bar:__pkg__"],
187 jars: ["java/mydefaultedjavalib.jar"],
188}
189
190java_import {
191 name: "mydefaultedjavalib",
192 prefer: false,
193 visibility: ["//other/bar:__pkg__"],
194 jars: ["java/mydefaultedjavalib.jar"],
195}
196
197sdk_snapshot {
198 name: "mysdk@current",
199 visibility: [
200 "//other/foo:__pkg__",
201 "//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 Duffinee209622020-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 Duffinee209622020-05-06 10:23:19 +0100238 S_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000239 A_Common []string
Paul Duffinee209622020-05-06 10:23:19 +0100240 A_Different []string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000241 F_Common *bool
Paul Duffinee209622020-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)
Greg Kaisere08e03f2020-05-08 19:12:34 +0000292 extractor.extractCommonProperties(common, structs)
Paul Duffinb07fa512020-03-10 22:17:04 +0000293
294 h := TestHelper{t}
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100295 h.AssertDeepEquals("common properties not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000296 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100297 name: "common",
Paul Duffinb07fa512020-03-10 22:17:04 +0000298 private: "",
299 Public_Kept: "",
300 S_Common: "common",
301 S_Different: "",
302 A_Common: []string{"first", "second"},
303 A_Different: []string(nil),
304 F_Common: proptools.BoolPtr(false),
305 F_Different: nil,
306 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
307 S_Embedded_Common: "embedded_common",
308 S_Embedded_Different: "",
309 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100310 },
311 common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000312
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100313 h.AssertDeepEquals("updated properties[0] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000314 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100315 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000316 private: "common",
317 Public_Kept: "common",
318 S_Common: "",
319 S_Different: "upper",
320 A_Common: nil,
321 A_Different: []string{"alpha", "beta"},
322 F_Common: nil,
323 F_Different: proptools.BoolPtr(false),
324 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
325 S_Embedded_Common: "",
326 S_Embedded_Different: "embedded_upper",
327 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100328 },
329 structs[0])
Paul Duffinb07fa512020-03-10 22:17:04 +0000330
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100331 h.AssertDeepEquals("updated properties[1] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000332 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100333 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000334 private: "common",
335 Public_Kept: "common",
336 S_Common: "",
337 S_Different: "lower",
338 A_Common: nil,
339 A_Different: []string{"alpha", "delta"},
340 F_Common: nil,
341 F_Different: proptools.BoolPtr(true),
342 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
343 S_Embedded_Common: "",
344 S_Embedded_Different: "embedded_lower",
345 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100346 },
347 structs[1])
Paul Duffinb07fa512020-03-10 22:17:04 +0000348}
Paul Duffinee209622020-05-06 10:23:19 +0100349
350func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) {
351 common := &testPropertiesStruct{name: "common"}
352 structs := []propertiesContainer{
353 &testPropertiesStruct{
354 name: "struct-0",
355 S_Common: "should-be-but-is-not-common0",
356 },
357 &testPropertiesStruct{
358 name: "struct-1",
359 S_Common: "should-be-but-is-not-common1",
360 },
361 }
362
363 extractor := newCommonValueExtractor(common)
364
365 h := TestHelper{t}
366
367 err := extractor.extractCommonProperties(common, structs)
368 h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties:
369 "struct-0" has value "should-be-but-is-not-common0"
370 "struct-1" has value "should-be-but-is-not-common1"`, err)
371}