Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 18 | "testing" |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 19 | |
| 20 | "github.com/google/blueprint/proptools" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 21 | ) |
| 22 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 23 | // Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE. |
| 24 | func TestMain(m *testing.M) { |
| 25 | runTestWithBuildDir(m) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 26 | } |
| 27 | |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 28 | func 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 Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 32 | java_header_libs: ["sdkmember"], |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | sdk_snapshot { |
| 36 | name: "mysdk@1", |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 37 | java_header_libs: ["sdkmember_mysdk_1"], |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 38 | } |
| 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 Han | 5e9013b | 2020-03-10 06:23:13 +0900 | [diff] [blame] | 63 | apex_available: ["myapex"], |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 64 | } |
| 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 Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 85 | |
| 86 | // Ensure that prebuilt modules have the same effective visibility as the source |
| 87 | // modules. |
| 88 | func 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 Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 117 | java_defaults { |
| 118 | name: "java-defaults", |
Jooyung Han | 5e9013b | 2020-03-10 06:23:13 +0900 | [diff] [blame] | 119 | visibility: ["//other/bar"], |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 122 | java_library { |
| 123 | name: "mypublicjavalib", |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 124 | defaults: ["java-defaults"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 125 | 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 Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 151 | result.CheckSnapshot("mysdk", "package", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 152 | checkAndroidBpContents(` |
| 153 | // This is auto-generated. DO NOT EDIT. |
| 154 | |
| 155 | java_import { |
| 156 | name: "mysdk_myjavalib@current", |
| 157 | sdk_member_name: "myjavalib", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame^] | 158 | visibility: [ |
| 159 | "//other/foo", |
| 160 | "//package", |
| 161 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 162 | jars: ["java/myjavalib.jar"], |
| 163 | } |
| 164 | |
| 165 | java_import { |
| 166 | name: "myjavalib", |
| 167 | prefer: false, |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame^] | 168 | visibility: [ |
| 169 | "//other/foo", |
| 170 | "//package", |
| 171 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 172 | jars: ["java/myjavalib.jar"], |
| 173 | } |
| 174 | |
| 175 | java_import { |
| 176 | name: "mysdk_mypublicjavalib@current", |
| 177 | sdk_member_name: "mypublicjavalib", |
| 178 | visibility: ["//visibility:public"], |
| 179 | jars: ["java/mypublicjavalib.jar"], |
| 180 | } |
| 181 | |
| 182 | java_import { |
| 183 | name: "mypublicjavalib", |
| 184 | prefer: false, |
| 185 | visibility: ["//visibility:public"], |
| 186 | jars: ["java/mypublicjavalib.jar"], |
| 187 | } |
| 188 | |
| 189 | java_import { |
| 190 | name: "mysdk_mydefaultedjavalib@current", |
| 191 | sdk_member_name: "mydefaultedjavalib", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame^] | 192 | visibility: [ |
| 193 | "//other/bar", |
| 194 | "//package", |
| 195 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 196 | jars: ["java/mydefaultedjavalib.jar"], |
| 197 | } |
| 198 | |
| 199 | java_import { |
| 200 | name: "mydefaultedjavalib", |
| 201 | prefer: false, |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame^] | 202 | visibility: [ |
| 203 | "//other/bar", |
| 204 | "//package", |
| 205 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 206 | jars: ["java/mydefaultedjavalib.jar"], |
| 207 | } |
| 208 | |
| 209 | sdk_snapshot { |
| 210 | name: "mysdk@current", |
| 211 | visibility: [ |
Martin Stjernholm | 01407c5 | 2020-05-13 01:54:21 +0100 | [diff] [blame] | 212 | "//other/foo", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 213 | "//package:__subpackages__", |
| 214 | ], |
| 215 | java_header_libs: [ |
| 216 | "mysdk_myjavalib@current", |
| 217 | "mysdk_mypublicjavalib@current", |
| 218 | "mysdk_mydefaultedjavalib@current", |
| 219 | ], |
| 220 | } |
| 221 | `)) |
| 222 | } |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 223 | |
| 224 | func 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 Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 239 | |
| 240 | type EmbeddedPropertiesStruct struct { |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 241 | S_Embedded_Common string `android:"arch_variant"` |
| 242 | S_Embedded_Different string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | type testPropertiesStruct struct { |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 246 | name string |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 247 | private string |
| 248 | Public_Kept string `sdk:"keep"` |
| 249 | S_Common string |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 250 | S_Different string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 251 | A_Common []string |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 252 | A_Different []string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 253 | F_Common *bool |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 254 | F_Different *bool `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 255 | EmbeddedPropertiesStruct |
| 256 | } |
| 257 | |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 258 | func (p *testPropertiesStruct) optimizableProperties() interface{} { |
| 259 | return p |
| 260 | } |
| 261 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 262 | func (p *testPropertiesStruct) String() string { |
| 263 | return p.name |
| 264 | } |
| 265 | |
| 266 | var _ propertiesContainer = (*testPropertiesStruct)(nil) |
| 267 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 268 | func TestCommonValueOptimization(t *testing.T) { |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 269 | common := &testPropertiesStruct{name: "common"} |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 270 | structs := []propertiesContainer{ |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 271 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 272 | name: "struct-0", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 273 | 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 Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 287 | name: "struct-1", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 288 | 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 Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 304 | |
| 305 | h := TestHelper{t} |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 306 | |
| 307 | err := extractor.extractCommonProperties(common, structs) |
| 308 | h.AssertDeepEquals("unexpected error", nil, err) |
| 309 | |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 310 | h.AssertDeepEquals("common properties not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 311 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 312 | name: "common", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 313 | 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 Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 325 | }, |
| 326 | common) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 327 | |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 328 | h.AssertDeepEquals("updated properties[0] not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 329 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 330 | name: "struct-0", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 331 | 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 Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 343 | }, |
| 344 | structs[0]) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 345 | |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 346 | h.AssertDeepEquals("updated properties[1] not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 347 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 348 | name: "struct-1", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 349 | 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 Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 361 | }, |
| 362 | structs[1]) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 363 | } |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 364 | |
| 365 | func 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 | } |