blob: 7ae3a03b988562b148aed3485cf0cf0245d78ee0 [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 {
229 S_Embedded_Common string
230 S_Embedded_Different string
231}
232
233type testPropertiesStruct struct {
234 private string
235 Public_Kept string `sdk:"keep"`
236 S_Common string
237 S_Different string
238 A_Common []string
239 A_Different []string
240 F_Common *bool
241 F_Different *bool
242 EmbeddedPropertiesStruct
243}
244
Paul Duffinf34f6d82020-04-30 15:48:31 +0100245func (p *testPropertiesStruct) optimizableProperties() interface{} {
246 return p
247}
248
Paul Duffinb07fa512020-03-10 22:17:04 +0000249func TestCommonValueOptimization(t *testing.T) {
250 common := &testPropertiesStruct{}
Paul Duffinf34f6d82020-04-30 15:48:31 +0100251 structs := []propertiesContainer{
Paul Duffinb07fa512020-03-10 22:17:04 +0000252 &testPropertiesStruct{
253 private: "common",
254 Public_Kept: "common",
255 S_Common: "common",
256 S_Different: "upper",
257 A_Common: []string{"first", "second"},
258 A_Different: []string{"alpha", "beta"},
259 F_Common: proptools.BoolPtr(false),
260 F_Different: proptools.BoolPtr(false),
261 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
262 S_Embedded_Common: "embedded_common",
263 S_Embedded_Different: "embedded_upper",
264 },
265 },
266 &testPropertiesStruct{
267 private: "common",
268 Public_Kept: "common",
269 S_Common: "common",
270 S_Different: "lower",
271 A_Common: []string{"first", "second"},
272 A_Different: []string{"alpha", "delta"},
273 F_Common: proptools.BoolPtr(false),
274 F_Different: proptools.BoolPtr(true),
275 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
276 S_Embedded_Common: "embedded_common",
277 S_Embedded_Different: "embedded_lower",
278 },
279 },
280 }
281
282 extractor := newCommonValueExtractor(common)
283 extractor.extractCommonProperties(common, structs)
284
285 h := TestHelper{t}
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100286 h.AssertDeepEquals("common properties not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000287 &testPropertiesStruct{
288 private: "",
289 Public_Kept: "",
290 S_Common: "common",
291 S_Different: "",
292 A_Common: []string{"first", "second"},
293 A_Different: []string(nil),
294 F_Common: proptools.BoolPtr(false),
295 F_Different: nil,
296 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
297 S_Embedded_Common: "embedded_common",
298 S_Embedded_Different: "",
299 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100300 },
301 common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000302
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100303 h.AssertDeepEquals("updated properties[0] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000304 &testPropertiesStruct{
305 private: "common",
306 Public_Kept: "common",
307 S_Common: "",
308 S_Different: "upper",
309 A_Common: nil,
310 A_Different: []string{"alpha", "beta"},
311 F_Common: nil,
312 F_Different: proptools.BoolPtr(false),
313 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
314 S_Embedded_Common: "",
315 S_Embedded_Different: "embedded_upper",
316 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100317 },
318 structs[0])
Paul Duffinb07fa512020-03-10 22:17:04 +0000319
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100320 h.AssertDeepEquals("updated properties[1] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000321 &testPropertiesStruct{
322 private: "common",
323 Public_Kept: "common",
324 S_Common: "",
325 S_Different: "lower",
326 A_Common: nil,
327 A_Different: []string{"alpha", "delta"},
328 F_Common: nil,
329 F_Different: proptools.BoolPtr(true),
330 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
331 S_Embedded_Common: "",
332 S_Embedded_Different: "embedded_lower",
333 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100334 },
335 structs[1])
Paul Duffinb07fa512020-03-10 22:17:04 +0000336}