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", |
| 158 | visibility: ["//other/foo:__pkg__"], |
| 159 | jars: ["java/myjavalib.jar"], |
| 160 | } |
| 161 | |
| 162 | java_import { |
| 163 | name: "myjavalib", |
| 164 | prefer: false, |
| 165 | visibility: ["//other/foo:__pkg__"], |
| 166 | jars: ["java/myjavalib.jar"], |
| 167 | } |
| 168 | |
| 169 | java_import { |
| 170 | name: "mysdk_mypublicjavalib@current", |
| 171 | sdk_member_name: "mypublicjavalib", |
| 172 | visibility: ["//visibility:public"], |
| 173 | jars: ["java/mypublicjavalib.jar"], |
| 174 | } |
| 175 | |
| 176 | java_import { |
| 177 | name: "mypublicjavalib", |
| 178 | prefer: false, |
| 179 | visibility: ["//visibility:public"], |
| 180 | jars: ["java/mypublicjavalib.jar"], |
| 181 | } |
| 182 | |
| 183 | java_import { |
| 184 | name: "mysdk_mydefaultedjavalib@current", |
| 185 | sdk_member_name: "mydefaultedjavalib", |
| 186 | visibility: ["//other/bar:__pkg__"], |
| 187 | jars: ["java/mydefaultedjavalib.jar"], |
| 188 | } |
| 189 | |
| 190 | java_import { |
| 191 | name: "mydefaultedjavalib", |
| 192 | prefer: false, |
| 193 | visibility: ["//other/bar:__pkg__"], |
| 194 | jars: ["java/mydefaultedjavalib.jar"], |
| 195 | } |
| 196 | |
| 197 | sdk_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 Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 211 | |
| 212 | func 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 Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 227 | |
| 228 | type EmbeddedPropertiesStruct struct { |
| 229 | S_Embedded_Common string |
| 230 | S_Embedded_Different string |
| 231 | } |
| 232 | |
| 233 | type 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 Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 245 | func (p *testPropertiesStruct) optimizableProperties() interface{} { |
| 246 | return p |
| 247 | } |
| 248 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 249 | func TestCommonValueOptimization(t *testing.T) { |
| 250 | common := &testPropertiesStruct{} |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 251 | structs := []propertiesContainer{ |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 252 | &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 Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 286 | h.AssertDeepEquals("common properties not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 287 | &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 Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 300 | }, |
| 301 | common) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 302 | |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 303 | h.AssertDeepEquals("updated properties[0] not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 304 | &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 Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 317 | }, |
| 318 | structs[0]) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 319 | |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 320 | h.AssertDeepEquals("updated properties[1] not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 321 | &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 Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 334 | }, |
| 335 | structs[1]) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 336 | } |