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", |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 106 | "myprivatejavalib", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 107 | ], |
| 108 | } |
| 109 | |
| 110 | java_library { |
| 111 | name: "myjavalib", |
| 112 | // Uses package default visibility |
| 113 | srcs: ["Test.java"], |
| 114 | system_modules: "none", |
| 115 | sdk_version: "none", |
| 116 | } |
| 117 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 118 | java_defaults { |
| 119 | name: "java-defaults", |
Jooyung Han | 5e9013b | 2020-03-10 06:23:13 +0900 | [diff] [blame] | 120 | visibility: ["//other/bar"], |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 123 | java_library { |
| 124 | name: "mypublicjavalib", |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 125 | defaults: ["java-defaults"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 126 | visibility: ["//visibility:public"], |
| 127 | srcs: ["Test.java"], |
| 128 | system_modules: "none", |
| 129 | sdk_version: "none", |
| 130 | } |
| 131 | |
| 132 | java_defaults { |
| 133 | name: "myjavadefaults", |
| 134 | visibility: ["//other/bar"], |
| 135 | } |
| 136 | |
| 137 | java_library { |
| 138 | name: "mydefaultedjavalib", |
| 139 | defaults: ["myjavadefaults"], |
| 140 | srcs: ["Test.java"], |
| 141 | system_modules: "none", |
| 142 | sdk_version: "none", |
| 143 | } |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 144 | |
| 145 | java_library { |
| 146 | name: "myprivatejavalib", |
| 147 | srcs: ["Test.java"], |
| 148 | visibility: ["//visibility:private"], |
| 149 | system_modules: "none", |
| 150 | sdk_version: "none", |
| 151 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 152 | ` |
| 153 | |
| 154 | result := testSdkWithFs(t, ``, |
| 155 | map[string][]byte{ |
| 156 | "package/Test.java": nil, |
| 157 | "package/Android.bp": []byte(packageBp), |
| 158 | }) |
| 159 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 160 | result.CheckSnapshot("mysdk", "package", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 161 | checkAndroidBpContents(` |
| 162 | // This is auto-generated. DO NOT EDIT. |
| 163 | |
| 164 | java_import { |
| 165 | name: "mysdk_myjavalib@current", |
| 166 | sdk_member_name: "myjavalib", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 167 | visibility: [ |
| 168 | "//other/foo", |
| 169 | "//package", |
| 170 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 171 | jars: ["java/myjavalib.jar"], |
| 172 | } |
| 173 | |
| 174 | java_import { |
| 175 | name: "myjavalib", |
| 176 | prefer: false, |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 177 | visibility: [ |
| 178 | "//other/foo", |
| 179 | "//package", |
| 180 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 181 | jars: ["java/myjavalib.jar"], |
| 182 | } |
| 183 | |
| 184 | java_import { |
| 185 | name: "mysdk_mypublicjavalib@current", |
| 186 | sdk_member_name: "mypublicjavalib", |
| 187 | visibility: ["//visibility:public"], |
| 188 | jars: ["java/mypublicjavalib.jar"], |
| 189 | } |
| 190 | |
| 191 | java_import { |
| 192 | name: "mypublicjavalib", |
| 193 | prefer: false, |
| 194 | visibility: ["//visibility:public"], |
| 195 | jars: ["java/mypublicjavalib.jar"], |
| 196 | } |
| 197 | |
| 198 | java_import { |
| 199 | name: "mysdk_mydefaultedjavalib@current", |
| 200 | sdk_member_name: "mydefaultedjavalib", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 201 | visibility: [ |
| 202 | "//other/bar", |
| 203 | "//package", |
| 204 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 205 | jars: ["java/mydefaultedjavalib.jar"], |
| 206 | } |
| 207 | |
| 208 | java_import { |
| 209 | name: "mydefaultedjavalib", |
| 210 | prefer: false, |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 211 | visibility: [ |
| 212 | "//other/bar", |
| 213 | "//package", |
| 214 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 215 | jars: ["java/mydefaultedjavalib.jar"], |
| 216 | } |
| 217 | |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 218 | java_import { |
| 219 | name: "mysdk_myprivatejavalib@current", |
| 220 | sdk_member_name: "myprivatejavalib", |
| 221 | visibility: ["//package"], |
| 222 | jars: ["java/myprivatejavalib.jar"], |
| 223 | } |
| 224 | |
| 225 | java_import { |
| 226 | name: "myprivatejavalib", |
| 227 | prefer: false, |
| 228 | visibility: ["//package"], |
| 229 | jars: ["java/myprivatejavalib.jar"], |
| 230 | } |
| 231 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 232 | sdk_snapshot { |
| 233 | name: "mysdk@current", |
| 234 | visibility: [ |
Martin Stjernholm | 01407c5 | 2020-05-13 01:54:21 +0100 | [diff] [blame] | 235 | "//other/foo", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 236 | "//package:__subpackages__", |
| 237 | ], |
| 238 | java_header_libs: [ |
| 239 | "mysdk_myjavalib@current", |
| 240 | "mysdk_mypublicjavalib@current", |
| 241 | "mysdk_mydefaultedjavalib@current", |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 242 | "mysdk_myprivatejavalib@current", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 243 | ], |
| 244 | } |
| 245 | `)) |
| 246 | } |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 247 | |
| 248 | func TestSDkInstall(t *testing.T) { |
| 249 | sdk := ` |
| 250 | sdk { |
| 251 | name: "mysdk", |
| 252 | } |
| 253 | ` |
| 254 | result := testSdkWithFs(t, ``, |
| 255 | map[string][]byte{ |
| 256 | "Android.bp": []byte(sdk), |
| 257 | }) |
| 258 | |
| 259 | result.CheckSnapshot("mysdk", "", |
| 260 | checkAllOtherCopyRules(`.intermediates/mysdk/common_os/mysdk-current.zip -> mysdk-current.zip`), |
| 261 | ) |
| 262 | } |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 263 | |
| 264 | type EmbeddedPropertiesStruct struct { |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 265 | S_Embedded_Common string `android:"arch_variant"` |
| 266 | S_Embedded_Different string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | type testPropertiesStruct struct { |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 270 | name string |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 271 | private string |
| 272 | Public_Kept string `sdk:"keep"` |
| 273 | S_Common string |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 274 | S_Different string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 275 | A_Common []string |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 276 | A_Different []string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 277 | F_Common *bool |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 278 | F_Different *bool `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 279 | EmbeddedPropertiesStruct |
| 280 | } |
| 281 | |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 282 | func (p *testPropertiesStruct) optimizableProperties() interface{} { |
| 283 | return p |
| 284 | } |
| 285 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 286 | func (p *testPropertiesStruct) String() string { |
| 287 | return p.name |
| 288 | } |
| 289 | |
| 290 | var _ propertiesContainer = (*testPropertiesStruct)(nil) |
| 291 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 292 | func TestCommonValueOptimization(t *testing.T) { |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 293 | common := &testPropertiesStruct{name: "common"} |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 294 | structs := []propertiesContainer{ |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 295 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 296 | name: "struct-0", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 297 | private: "common", |
| 298 | Public_Kept: "common", |
| 299 | S_Common: "common", |
| 300 | S_Different: "upper", |
| 301 | A_Common: []string{"first", "second"}, |
| 302 | A_Different: []string{"alpha", "beta"}, |
| 303 | F_Common: proptools.BoolPtr(false), |
| 304 | F_Different: proptools.BoolPtr(false), |
| 305 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 306 | S_Embedded_Common: "embedded_common", |
| 307 | S_Embedded_Different: "embedded_upper", |
| 308 | }, |
| 309 | }, |
| 310 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 311 | name: "struct-1", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 312 | private: "common", |
| 313 | Public_Kept: "common", |
| 314 | S_Common: "common", |
| 315 | S_Different: "lower", |
| 316 | A_Common: []string{"first", "second"}, |
| 317 | A_Different: []string{"alpha", "delta"}, |
| 318 | F_Common: proptools.BoolPtr(false), |
| 319 | F_Different: proptools.BoolPtr(true), |
| 320 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 321 | S_Embedded_Common: "embedded_common", |
| 322 | S_Embedded_Different: "embedded_lower", |
| 323 | }, |
| 324 | }, |
| 325 | } |
| 326 | |
| 327 | extractor := newCommonValueExtractor(common) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 328 | |
| 329 | h := TestHelper{t} |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 330 | |
| 331 | err := extractor.extractCommonProperties(common, structs) |
| 332 | h.AssertDeepEquals("unexpected error", nil, err) |
| 333 | |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 334 | h.AssertDeepEquals("common properties not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 335 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 336 | name: "common", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 337 | private: "", |
| 338 | Public_Kept: "", |
| 339 | S_Common: "common", |
| 340 | S_Different: "", |
| 341 | A_Common: []string{"first", "second"}, |
| 342 | A_Different: []string(nil), |
| 343 | F_Common: proptools.BoolPtr(false), |
| 344 | F_Different: nil, |
| 345 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 346 | S_Embedded_Common: "embedded_common", |
| 347 | S_Embedded_Different: "", |
| 348 | }, |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 349 | }, |
| 350 | common) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 351 | |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 352 | h.AssertDeepEquals("updated properties[0] not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 353 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 354 | name: "struct-0", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 355 | private: "common", |
| 356 | Public_Kept: "common", |
| 357 | S_Common: "", |
| 358 | S_Different: "upper", |
| 359 | A_Common: nil, |
| 360 | A_Different: []string{"alpha", "beta"}, |
| 361 | F_Common: nil, |
| 362 | F_Different: proptools.BoolPtr(false), |
| 363 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 364 | S_Embedded_Common: "", |
| 365 | S_Embedded_Different: "embedded_upper", |
| 366 | }, |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 367 | }, |
| 368 | structs[0]) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 369 | |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 370 | h.AssertDeepEquals("updated properties[1] not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 371 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 372 | name: "struct-1", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 373 | private: "common", |
| 374 | Public_Kept: "common", |
| 375 | S_Common: "", |
| 376 | S_Different: "lower", |
| 377 | A_Common: nil, |
| 378 | A_Different: []string{"alpha", "delta"}, |
| 379 | F_Common: nil, |
| 380 | F_Different: proptools.BoolPtr(true), |
| 381 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 382 | S_Embedded_Common: "", |
| 383 | S_Embedded_Different: "embedded_lower", |
| 384 | }, |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 385 | }, |
| 386 | structs[1]) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 387 | } |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 388 | |
| 389 | func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) { |
| 390 | common := &testPropertiesStruct{name: "common"} |
| 391 | structs := []propertiesContainer{ |
| 392 | &testPropertiesStruct{ |
| 393 | name: "struct-0", |
| 394 | S_Common: "should-be-but-is-not-common0", |
| 395 | }, |
| 396 | &testPropertiesStruct{ |
| 397 | name: "struct-1", |
| 398 | S_Common: "should-be-but-is-not-common1", |
| 399 | }, |
| 400 | } |
| 401 | |
| 402 | extractor := newCommonValueExtractor(common) |
| 403 | |
| 404 | h := TestHelper{t} |
| 405 | |
| 406 | err := extractor.extractCommonProperties(common, structs) |
| 407 | h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties: |
| 408 | "struct-0" has value "should-be-but-is-not-common0" |
| 409 | "struct-1" has value "should-be-but-is-not-common1"`, err) |
| 410 | } |