Colin Cross | 1d2b6b3 | 2021-06-01 13:18:08 -0700 | [diff] [blame] | 1 | // Copyright 2021 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 java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "fmt" |
| 20 | "path/filepath" |
| 21 | "regexp" |
| 22 | "testing" |
| 23 | |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | func TestJavaSdkLibrary(t *testing.T) { |
| 28 | result := android.GroupFixturePreparers( |
| 29 | prepareForJavaTest, |
| 30 | PrepareForTestWithJavaSdkLibraryFiles, |
| 31 | FixtureWithPrebuiltApis(map[string][]string{ |
| 32 | "28": {"foo"}, |
| 33 | "29": {"foo"}, |
| 34 | "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"}, |
| 35 | }), |
| 36 | ).RunTestWithBp(t, ` |
| 37 | droiddoc_exported_dir { |
| 38 | name: "droiddoc-templates-sdk", |
| 39 | path: ".", |
| 40 | } |
| 41 | java_sdk_library { |
| 42 | name: "foo", |
| 43 | srcs: ["a.java", "b.java"], |
| 44 | api_packages: ["foo"], |
| 45 | } |
| 46 | java_sdk_library { |
| 47 | name: "bar", |
| 48 | srcs: ["a.java", "b.java"], |
| 49 | api_packages: ["bar"], |
| 50 | } |
| 51 | java_library { |
| 52 | name: "baz", |
| 53 | srcs: ["c.java"], |
| 54 | libs: ["foo", "bar.stubs"], |
| 55 | sdk_version: "system_current", |
| 56 | } |
| 57 | java_sdk_library { |
| 58 | name: "barney", |
| 59 | srcs: ["c.java"], |
| 60 | api_only: true, |
| 61 | } |
| 62 | java_sdk_library { |
| 63 | name: "betty", |
| 64 | srcs: ["c.java"], |
| 65 | shared_library: false, |
| 66 | } |
| 67 | java_sdk_library_import { |
| 68 | name: "quuz", |
| 69 | public: { |
| 70 | jars: ["c.jar"], |
| 71 | }, |
| 72 | } |
| 73 | java_sdk_library_import { |
| 74 | name: "fred", |
| 75 | public: { |
| 76 | jars: ["b.jar"], |
| 77 | }, |
| 78 | } |
| 79 | java_sdk_library_import { |
| 80 | name: "wilma", |
| 81 | public: { |
| 82 | jars: ["b.jar"], |
| 83 | }, |
| 84 | shared_library: false, |
| 85 | } |
| 86 | java_library { |
| 87 | name: "qux", |
| 88 | srcs: ["c.java"], |
| 89 | libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"], |
| 90 | sdk_version: "system_current", |
| 91 | } |
| 92 | java_library { |
| 93 | name: "baz-test", |
| 94 | srcs: ["c.java"], |
| 95 | libs: ["foo"], |
| 96 | sdk_version: "test_current", |
| 97 | } |
| 98 | java_library { |
| 99 | name: "baz-29", |
| 100 | srcs: ["c.java"], |
| 101 | libs: ["foo"], |
| 102 | sdk_version: "system_29", |
| 103 | } |
| 104 | java_library { |
| 105 | name: "baz-module-30", |
| 106 | srcs: ["c.java"], |
| 107 | libs: ["foo"], |
| 108 | sdk_version: "module_30", |
| 109 | } |
| 110 | `) |
| 111 | |
| 112 | // check the existence of the internal modules |
Paul Duffin | b97b157 | 2021-04-29 21:50:40 +0100 | [diff] [blame] | 113 | foo := result.ModuleForTests("foo", "android_common") |
Colin Cross | 1d2b6b3 | 2021-06-01 13:18:08 -0700 | [diff] [blame] | 114 | result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common") |
| 115 | result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common") |
| 116 | result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common") |
| 117 | result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common") |
| 118 | result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common") |
| 119 | result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common") |
| 120 | result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common") |
| 121 | result.ModuleForTests("foo.api.public.28", "") |
| 122 | result.ModuleForTests("foo.api.system.28", "") |
| 123 | result.ModuleForTests("foo.api.test.28", "") |
| 124 | |
Paul Duffin | 9fc208e | 2021-06-27 20:42:04 +0100 | [diff] [blame] | 125 | exportedComponentsInfo := result.ModuleProvider(foo.Module(), android.ExportedComponentsInfoProvider).(android.ExportedComponentsInfo) |
Paul Duffin | b97b157 | 2021-04-29 21:50:40 +0100 | [diff] [blame] | 126 | expectedFooExportedComponents := []string{ |
| 127 | "foo.stubs", |
| 128 | "foo.stubs.source", |
| 129 | "foo.stubs.source.system", |
| 130 | "foo.stubs.source.test", |
| 131 | "foo.stubs.system", |
| 132 | "foo.stubs.test", |
| 133 | } |
| 134 | android.AssertArrayString(t, "foo exported components", expectedFooExportedComponents, exportedComponentsInfo.Components) |
| 135 | |
Colin Cross | 1d2b6b3 | 2021-06-01 13:18:08 -0700 | [diff] [blame] | 136 | bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac") |
| 137 | // tests if baz is actually linked to the stubs lib |
| 138 | android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar") |
| 139 | // ... and not to the impl lib |
| 140 | android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar") |
| 141 | // test if baz is not linked to the system variant of foo |
| 142 | android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar") |
| 143 | |
| 144 | bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac") |
| 145 | // tests if baz-test is actually linked to the test stubs lib |
| 146 | android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar") |
| 147 | |
| 148 | baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac") |
| 149 | // tests if baz-29 is actually linked to the system 29 stubs lib |
| 150 | android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") |
| 151 | |
| 152 | bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac") |
| 153 | // tests if "baz-module-30" is actually linked to the module 30 stubs lib |
| 154 | android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") |
| 155 | |
| 156 | // test if baz has exported SDK lib names foo and bar to qux |
| 157 | qux := result.ModuleForTests("qux", "android_common") |
| 158 | if quxLib, ok := qux.Module().(*Library); ok { |
Ulya Trafimovich | fc0f6e3 | 2021-08-12 16:16:11 +0100 | [diff] [blame^] | 159 | requiredSdkLibs, optionalSdkLibs := quxLib.ClassLoaderContexts().UsesLibs() |
| 160 | android.AssertDeepEquals(t, "qux exports (required)", []string{"foo", "bar", "fred", "quuz"}, requiredSdkLibs) |
| 161 | android.AssertDeepEquals(t, "qux exports (optional)", []string{}, optionalSdkLibs) |
Colin Cross | 1d2b6b3 | 2021-06-01 13:18:08 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) { |
| 166 | result := android.GroupFixturePreparers( |
| 167 | prepareForJavaTest, |
| 168 | PrepareForTestWithJavaSdkLibraryFiles, |
| 169 | FixtureWithLastReleaseApis("sdklib"), |
| 170 | ).RunTestWithBp(t, ` |
| 171 | java_sdk_library { |
| 172 | name: "sdklib", |
| 173 | srcs: ["a.java"], |
| 174 | libs: ["lib"], |
| 175 | static_libs: ["static-lib"], |
| 176 | impl_only_libs: ["impl-only-lib"], |
| 177 | stub_only_libs: ["stub-only-lib"], |
| 178 | stub_only_static_libs: ["stub-only-static-lib"], |
| 179 | } |
| 180 | java_defaults { |
| 181 | name: "defaults", |
| 182 | srcs: ["a.java"], |
| 183 | sdk_version: "current", |
| 184 | } |
| 185 | java_library { name: "lib", defaults: ["defaults"] } |
| 186 | java_library { name: "static-lib", defaults: ["defaults"] } |
| 187 | java_library { name: "impl-only-lib", defaults: ["defaults"] } |
| 188 | java_library { name: "stub-only-lib", defaults: ["defaults"] } |
| 189 | java_library { name: "stub-only-static-lib", defaults: ["defaults"] } |
| 190 | `) |
| 191 | var expectations = []struct { |
| 192 | lib string |
| 193 | on_impl_classpath bool |
| 194 | on_stub_classpath bool |
| 195 | in_impl_combined bool |
| 196 | in_stub_combined bool |
| 197 | }{ |
| 198 | {lib: "lib", on_impl_classpath: true}, |
| 199 | {lib: "static-lib", in_impl_combined: true}, |
| 200 | {lib: "impl-only-lib", on_impl_classpath: true}, |
| 201 | {lib: "stub-only-lib", on_stub_classpath: true}, |
| 202 | {lib: "stub-only-static-lib", in_stub_combined: true}, |
| 203 | } |
| 204 | verify := func(sdklib, dep string, cp, combined bool) { |
| 205 | sdklibCp := result.ModuleForTests(sdklib, "android_common").Rule("javac").Args["classpath"] |
| 206 | expected := cp || combined // Every combined jar is also on the classpath. |
| 207 | android.AssertStringContainsEquals(t, "bad classpath for "+sdklib, sdklibCp, "/"+dep+".jar", expected) |
| 208 | |
| 209 | combineJarInputs := result.ModuleForTests(sdklib, "android_common").Rule("combineJar").Inputs.Strings() |
| 210 | depPath := filepath.Join("out", "soong", ".intermediates", dep, "android_common", "turbine-combined", dep+".jar") |
| 211 | android.AssertStringListContainsEquals(t, "bad combined inputs for "+sdklib, combineJarInputs, depPath, combined) |
| 212 | } |
| 213 | for _, expectation := range expectations { |
| 214 | verify("sdklib", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined) |
| 215 | verify("sdklib.impl", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined) |
| 216 | |
| 217 | stubName := apiScopePublic.stubsLibraryModuleName("sdklib") |
| 218 | verify(stubName, expectation.lib, expectation.on_stub_classpath, expectation.in_stub_combined) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) { |
| 223 | result := android.GroupFixturePreparers( |
| 224 | prepareForJavaTest, |
| 225 | PrepareForTestWithJavaSdkLibraryFiles, |
| 226 | FixtureWithLastReleaseApis("foo"), |
| 227 | ).RunTestWithBp(t, ` |
| 228 | java_sdk_library { |
| 229 | name: "foo", |
| 230 | srcs: ["a.java"], |
| 231 | api_only: true, |
| 232 | public: { |
| 233 | enabled: true, |
| 234 | }, |
| 235 | } |
| 236 | |
| 237 | java_library { |
| 238 | name: "bar", |
| 239 | srcs: ["b.java"], |
| 240 | libs: ["foo"], |
| 241 | } |
| 242 | `) |
| 243 | |
| 244 | // The bar library should depend on the stubs jar. |
| 245 | barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac") |
| 246 | if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) { |
| 247 | t.Errorf("expected %q, found %#q", expected, actual) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) { |
| 252 | android.GroupFixturePreparers( |
| 253 | prepareForJavaTest, |
| 254 | PrepareForTestWithJavaSdkLibraryFiles, |
| 255 | FixtureWithLastReleaseApis("foo"), |
| 256 | ).RunTestWithBp(t, ` |
| 257 | java_sdk_library { |
| 258 | name: "foo", |
| 259 | srcs: ["a.java"], |
| 260 | api_packages: ["foo"], |
| 261 | public: { |
| 262 | enabled: true, |
| 263 | }, |
| 264 | } |
| 265 | |
| 266 | java_library { |
| 267 | name: "bar", |
| 268 | srcs: ["b.java", ":foo{.public.stubs.source}"], |
| 269 | } |
| 270 | `) |
| 271 | } |
| 272 | |
| 273 | func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) { |
| 274 | android.GroupFixturePreparers( |
| 275 | prepareForJavaTest, |
| 276 | PrepareForTestWithJavaSdkLibraryFiles, |
| 277 | FixtureWithLastReleaseApis("foo"), |
| 278 | ). |
| 279 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)). |
| 280 | RunTestWithBp(t, ` |
| 281 | java_sdk_library { |
| 282 | name: "foo", |
| 283 | srcs: ["a.java"], |
| 284 | api_packages: ["foo"], |
| 285 | public: { |
| 286 | enabled: true, |
| 287 | }, |
| 288 | } |
| 289 | |
| 290 | java_library { |
| 291 | name: "bar", |
| 292 | srcs: ["b.java", ":foo{.system.stubs.source}"], |
| 293 | } |
| 294 | `) |
| 295 | } |
| 296 | |
| 297 | func TestJavaSdkLibrary_Deps(t *testing.T) { |
| 298 | result := android.GroupFixturePreparers( |
| 299 | prepareForJavaTest, |
| 300 | PrepareForTestWithJavaSdkLibraryFiles, |
| 301 | FixtureWithLastReleaseApis("sdklib"), |
| 302 | ).RunTestWithBp(t, ` |
| 303 | java_sdk_library { |
| 304 | name: "sdklib", |
| 305 | srcs: ["a.java"], |
| 306 | sdk_version: "none", |
| 307 | system_modules: "none", |
| 308 | public: { |
| 309 | enabled: true, |
| 310 | }, |
| 311 | } |
| 312 | `) |
| 313 | |
| 314 | CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{ |
| 315 | `dex2oatd`, |
| 316 | `sdklib.impl`, |
| 317 | `sdklib.stubs`, |
| 318 | `sdklib.stubs.source`, |
| 319 | `sdklib.xml`, |
| 320 | }) |
| 321 | } |
| 322 | |
| 323 | func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) { |
| 324 | prepareForJavaTest.RunTestWithBp(t, ` |
| 325 | java_sdk_library_import { |
| 326 | name: "foo", |
| 327 | public: { |
| 328 | jars: ["a.jar"], |
| 329 | stub_srcs: ["a.java"], |
| 330 | current_api: "api/current.txt", |
| 331 | removed_api: "api/removed.txt", |
| 332 | }, |
| 333 | } |
| 334 | |
| 335 | java_library { |
| 336 | name: "bar", |
| 337 | srcs: [":foo{.public.stubs.source}"], |
| 338 | java_resources: [ |
| 339 | ":foo{.public.api.txt}", |
| 340 | ":foo{.public.removed-api.txt}", |
| 341 | ], |
| 342 | } |
| 343 | `) |
| 344 | } |
| 345 | |
| 346 | func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) { |
| 347 | bp := ` |
| 348 | java_sdk_library_import { |
| 349 | name: "foo", |
| 350 | public: { |
| 351 | jars: ["a.jar"], |
| 352 | }, |
| 353 | } |
| 354 | ` |
| 355 | |
| 356 | t.Run("stubs.source", func(t *testing.T) { |
| 357 | prepareForJavaTest. |
| 358 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)). |
| 359 | RunTestWithBp(t, bp+` |
| 360 | java_library { |
| 361 | name: "bar", |
| 362 | srcs: [":foo{.public.stubs.source}"], |
| 363 | java_resources: [ |
| 364 | ":foo{.public.api.txt}", |
| 365 | ":foo{.public.removed-api.txt}", |
| 366 | ], |
| 367 | } |
| 368 | `) |
| 369 | }) |
| 370 | |
| 371 | t.Run("api.txt", func(t *testing.T) { |
| 372 | prepareForJavaTest. |
| 373 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)). |
| 374 | RunTestWithBp(t, bp+` |
| 375 | java_library { |
| 376 | name: "bar", |
| 377 | srcs: ["a.java"], |
| 378 | java_resources: [ |
| 379 | ":foo{.public.api.txt}", |
| 380 | ], |
| 381 | } |
| 382 | `) |
| 383 | }) |
| 384 | |
| 385 | t.Run("removed-api.txt", func(t *testing.T) { |
| 386 | prepareForJavaTest. |
| 387 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)). |
| 388 | RunTestWithBp(t, bp+` |
| 389 | java_library { |
| 390 | name: "bar", |
| 391 | srcs: ["a.java"], |
| 392 | java_resources: [ |
| 393 | ":foo{.public.removed-api.txt}", |
| 394 | ], |
| 395 | } |
| 396 | `) |
| 397 | }) |
| 398 | } |
| 399 | |
| 400 | func TestJavaSdkLibrary_InvalidScopes(t *testing.T) { |
| 401 | prepareForJavaTest. |
| 402 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)). |
| 403 | RunTestWithBp(t, ` |
| 404 | java_sdk_library { |
| 405 | name: "foo", |
| 406 | srcs: ["a.java", "b.java"], |
| 407 | api_packages: ["foo"], |
| 408 | // Explicitly disable public to test the check that ensures the set of enabled |
| 409 | // scopes is consistent. |
| 410 | public: { |
| 411 | enabled: false, |
| 412 | }, |
| 413 | system: { |
| 414 | enabled: true, |
| 415 | }, |
| 416 | } |
| 417 | `) |
| 418 | } |
| 419 | |
| 420 | func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) { |
| 421 | android.GroupFixturePreparers( |
| 422 | prepareForJavaTest, |
| 423 | PrepareForTestWithJavaSdkLibraryFiles, |
| 424 | FixtureWithLastReleaseApis("foo"), |
| 425 | ).RunTestWithBp(t, ` |
| 426 | java_sdk_library { |
| 427 | name: "foo", |
| 428 | srcs: ["a.java", "b.java"], |
| 429 | api_packages: ["foo"], |
| 430 | system: { |
| 431 | enabled: true, |
| 432 | sdk_version: "module_current", |
| 433 | }, |
| 434 | } |
| 435 | `) |
| 436 | } |
| 437 | |
| 438 | func TestJavaSdkLibrary_ModuleLib(t *testing.T) { |
| 439 | android.GroupFixturePreparers( |
| 440 | prepareForJavaTest, |
| 441 | PrepareForTestWithJavaSdkLibraryFiles, |
| 442 | FixtureWithLastReleaseApis("foo"), |
| 443 | ).RunTestWithBp(t, ` |
| 444 | java_sdk_library { |
| 445 | name: "foo", |
| 446 | srcs: ["a.java", "b.java"], |
| 447 | api_packages: ["foo"], |
| 448 | system: { |
| 449 | enabled: true, |
| 450 | }, |
| 451 | module_lib: { |
| 452 | enabled: true, |
| 453 | }, |
| 454 | } |
| 455 | `) |
| 456 | } |
| 457 | |
| 458 | func TestJavaSdkLibrary_SystemServer(t *testing.T) { |
| 459 | android.GroupFixturePreparers( |
| 460 | prepareForJavaTest, |
| 461 | PrepareForTestWithJavaSdkLibraryFiles, |
| 462 | FixtureWithLastReleaseApis("foo"), |
| 463 | ).RunTestWithBp(t, ` |
| 464 | java_sdk_library { |
| 465 | name: "foo", |
| 466 | srcs: ["a.java", "b.java"], |
| 467 | api_packages: ["foo"], |
| 468 | system: { |
| 469 | enabled: true, |
| 470 | }, |
| 471 | system_server: { |
| 472 | enabled: true, |
| 473 | }, |
| 474 | } |
| 475 | `) |
| 476 | } |
| 477 | |
| 478 | func TestJavaSdkLibrary_MissingScope(t *testing.T) { |
| 479 | prepareForJavaTest. |
| 480 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)). |
| 481 | RunTestWithBp(t, ` |
| 482 | java_sdk_library { |
| 483 | name: "foo", |
| 484 | srcs: ["a.java"], |
| 485 | public: { |
| 486 | enabled: false, |
| 487 | }, |
| 488 | } |
| 489 | |
| 490 | java_library { |
| 491 | name: "baz", |
| 492 | srcs: ["a.java"], |
| 493 | libs: ["foo"], |
| 494 | sdk_version: "module_current", |
| 495 | } |
| 496 | `) |
| 497 | } |
| 498 | |
| 499 | func TestJavaSdkLibrary_FallbackScope(t *testing.T) { |
| 500 | android.GroupFixturePreparers( |
| 501 | prepareForJavaTest, |
| 502 | PrepareForTestWithJavaSdkLibraryFiles, |
| 503 | FixtureWithLastReleaseApis("foo"), |
| 504 | ).RunTestWithBp(t, ` |
| 505 | java_sdk_library { |
| 506 | name: "foo", |
| 507 | srcs: ["a.java"], |
| 508 | system: { |
| 509 | enabled: true, |
| 510 | }, |
| 511 | } |
| 512 | |
| 513 | java_library { |
| 514 | name: "baz", |
| 515 | srcs: ["a.java"], |
| 516 | libs: ["foo"], |
| 517 | // foo does not have module-lib scope so it should fallback to system |
| 518 | sdk_version: "module_current", |
| 519 | } |
| 520 | `) |
| 521 | } |
| 522 | |
| 523 | func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) { |
| 524 | result := android.GroupFixturePreparers( |
| 525 | prepareForJavaTest, |
| 526 | PrepareForTestWithJavaSdkLibraryFiles, |
| 527 | FixtureWithLastReleaseApis("foo"), |
| 528 | ).RunTestWithBp(t, ` |
| 529 | java_sdk_library { |
| 530 | name: "foo", |
| 531 | srcs: ["a.java"], |
| 532 | system: { |
| 533 | enabled: true, |
| 534 | }, |
| 535 | default_to_stubs: true, |
| 536 | } |
| 537 | |
| 538 | java_library { |
| 539 | name: "baz", |
| 540 | srcs: ["a.java"], |
| 541 | libs: ["foo"], |
| 542 | // does not have sdk_version set, should fallback to module, |
| 543 | // which will then fallback to system because the module scope |
| 544 | // is not enabled. |
| 545 | } |
| 546 | `) |
| 547 | // The baz library should depend on the system stubs jar. |
| 548 | bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac") |
| 549 | if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) { |
| 550 | t.Errorf("expected %q, found %#q", expected, actual) |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | func TestJavaSdkLibraryImport(t *testing.T) { |
| 555 | result := prepareForJavaTest.RunTestWithBp(t, ` |
| 556 | java_library { |
| 557 | name: "foo", |
| 558 | srcs: ["a.java"], |
| 559 | libs: ["sdklib"], |
| 560 | sdk_version: "current", |
| 561 | } |
| 562 | |
| 563 | java_library { |
| 564 | name: "foo.system", |
| 565 | srcs: ["a.java"], |
| 566 | libs: ["sdklib"], |
| 567 | sdk_version: "system_current", |
| 568 | } |
| 569 | |
| 570 | java_library { |
| 571 | name: "foo.test", |
| 572 | srcs: ["a.java"], |
| 573 | libs: ["sdklib"], |
| 574 | sdk_version: "test_current", |
| 575 | } |
| 576 | |
| 577 | java_sdk_library_import { |
| 578 | name: "sdklib", |
| 579 | public: { |
| 580 | jars: ["a.jar"], |
| 581 | }, |
| 582 | system: { |
| 583 | jars: ["b.jar"], |
| 584 | }, |
| 585 | test: { |
| 586 | jars: ["c.jar"], |
| 587 | stub_srcs: ["c.java"], |
| 588 | }, |
| 589 | } |
| 590 | `) |
| 591 | |
| 592 | for _, scope := range []string{"", ".system", ".test"} { |
| 593 | fooModule := result.ModuleForTests("foo"+scope, "android_common") |
| 594 | javac := fooModule.Rule("javac") |
| 595 | |
| 596 | sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output |
| 597 | android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String()) |
| 598 | } |
| 599 | |
| 600 | CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{ |
| 601 | `prebuilt_sdklib.stubs`, |
| 602 | `prebuilt_sdklib.stubs.source.test`, |
| 603 | `prebuilt_sdklib.stubs.system`, |
| 604 | `prebuilt_sdklib.stubs.test`, |
| 605 | }) |
| 606 | } |
| 607 | |
| 608 | func TestJavaSdkLibraryImport_WithSource(t *testing.T) { |
| 609 | result := android.GroupFixturePreparers( |
| 610 | prepareForJavaTest, |
| 611 | PrepareForTestWithJavaSdkLibraryFiles, |
| 612 | FixtureWithLastReleaseApis("sdklib"), |
| 613 | ).RunTestWithBp(t, ` |
| 614 | java_sdk_library { |
| 615 | name: "sdklib", |
| 616 | srcs: ["a.java"], |
| 617 | sdk_version: "none", |
| 618 | system_modules: "none", |
| 619 | public: { |
| 620 | enabled: true, |
| 621 | }, |
| 622 | } |
| 623 | |
| 624 | java_sdk_library_import { |
| 625 | name: "sdklib", |
| 626 | public: { |
| 627 | jars: ["a.jar"], |
| 628 | }, |
| 629 | } |
| 630 | `) |
| 631 | |
| 632 | CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{ |
| 633 | `dex2oatd`, |
| 634 | `prebuilt_sdklib`, |
| 635 | `sdklib.impl`, |
| 636 | `sdklib.stubs`, |
| 637 | `sdklib.stubs.source`, |
| 638 | `sdklib.xml`, |
| 639 | }) |
| 640 | |
| 641 | CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{ |
| 642 | `prebuilt_sdklib.stubs`, |
| 643 | `sdklib.impl`, |
| 644 | // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the |
| 645 | // dependency is added after prebuilts may have been renamed and so has to use |
| 646 | // the renamed name. |
| 647 | `sdklib.xml`, |
| 648 | }) |
| 649 | } |
| 650 | |
| 651 | func TestJavaSdkLibraryImport_Preferred(t *testing.T) { |
| 652 | result := android.GroupFixturePreparers( |
| 653 | prepareForJavaTest, |
| 654 | PrepareForTestWithJavaSdkLibraryFiles, |
| 655 | FixtureWithLastReleaseApis("sdklib"), |
| 656 | ).RunTestWithBp(t, ` |
| 657 | java_sdk_library { |
| 658 | name: "sdklib", |
| 659 | srcs: ["a.java"], |
| 660 | sdk_version: "none", |
| 661 | system_modules: "none", |
| 662 | public: { |
| 663 | enabled: true, |
| 664 | }, |
| 665 | } |
| 666 | |
| 667 | java_sdk_library_import { |
| 668 | name: "sdklib", |
| 669 | prefer: true, |
| 670 | public: { |
| 671 | jars: ["a.jar"], |
| 672 | }, |
| 673 | } |
| 674 | `) |
| 675 | |
| 676 | CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{ |
| 677 | `dex2oatd`, |
| 678 | `prebuilt_sdklib`, |
| 679 | `sdklib.impl`, |
| 680 | `sdklib.stubs`, |
| 681 | `sdklib.stubs.source`, |
| 682 | `sdklib.xml`, |
| 683 | }) |
| 684 | |
| 685 | CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{ |
| 686 | `prebuilt_sdklib.stubs`, |
| 687 | `sdklib.impl`, |
| 688 | `sdklib.xml`, |
| 689 | }) |
| 690 | } |
| 691 | |
| 692 | func TestJavaSdkLibraryEnforce(t *testing.T) { |
| 693 | partitionToBpOption := func(partition string) string { |
| 694 | switch partition { |
| 695 | case "system": |
| 696 | return "" |
| 697 | case "vendor": |
| 698 | return "soc_specific: true," |
| 699 | case "product": |
| 700 | return "product_specific: true," |
| 701 | default: |
| 702 | panic("Invalid partition group name: " + partition) |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | type testConfigInfo struct { |
| 707 | libraryType string |
| 708 | fromPartition string |
| 709 | toPartition string |
| 710 | enforceVendorInterface bool |
| 711 | enforceProductInterface bool |
| 712 | enforceJavaSdkLibraryCheck bool |
| 713 | allowList []string |
| 714 | } |
| 715 | |
| 716 | createPreparer := func(info testConfigInfo) android.FixturePreparer { |
| 717 | bpFileTemplate := ` |
| 718 | java_library { |
| 719 | name: "foo", |
| 720 | srcs: ["foo.java"], |
| 721 | libs: ["bar"], |
| 722 | sdk_version: "current", |
| 723 | %s |
| 724 | } |
| 725 | |
| 726 | %s { |
| 727 | name: "bar", |
| 728 | srcs: ["bar.java"], |
| 729 | sdk_version: "current", |
| 730 | %s |
| 731 | } |
| 732 | ` |
| 733 | |
| 734 | bpFile := fmt.Sprintf(bpFileTemplate, |
| 735 | partitionToBpOption(info.fromPartition), |
| 736 | info.libraryType, |
| 737 | partitionToBpOption(info.toPartition)) |
| 738 | |
| 739 | return android.GroupFixturePreparers( |
| 740 | PrepareForTestWithJavaSdkLibraryFiles, |
| 741 | FixtureWithLastReleaseApis("bar"), |
| 742 | android.FixtureWithRootAndroidBp(bpFile), |
| 743 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 744 | variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface) |
| 745 | if info.enforceVendorInterface { |
| 746 | variables.DeviceVndkVersion = proptools.StringPtr("current") |
| 747 | } |
| 748 | variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck) |
| 749 | variables.InterPartitionJavaLibraryAllowList = info.allowList |
| 750 | }), |
| 751 | ) |
| 752 | } |
| 753 | |
| 754 | runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) { |
| 755 | t.Run(fmt.Sprintf("%v", info), func(t *testing.T) { |
| 756 | errorHandler := android.FixtureExpectsNoErrors |
| 757 | if expectedErrorPattern != "" { |
| 758 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern) |
| 759 | } |
| 760 | android.GroupFixturePreparers( |
| 761 | prepareForJavaTest, |
| 762 | createPreparer(info), |
| 763 | ). |
| 764 | ExtendWithErrorHandler(errorHandler). |
| 765 | RunTest(t) |
| 766 | }) |
| 767 | } |
| 768 | |
| 769 | errorMessage := "is not allowed across the partitions" |
| 770 | |
| 771 | runTest(t, testConfigInfo{ |
| 772 | libraryType: "java_library", |
| 773 | fromPartition: "product", |
| 774 | toPartition: "system", |
| 775 | enforceVendorInterface: true, |
| 776 | enforceProductInterface: true, |
| 777 | enforceJavaSdkLibraryCheck: false, |
| 778 | }, "") |
| 779 | |
| 780 | runTest(t, testConfigInfo{ |
| 781 | libraryType: "java_library", |
| 782 | fromPartition: "product", |
| 783 | toPartition: "system", |
| 784 | enforceVendorInterface: true, |
| 785 | enforceProductInterface: false, |
| 786 | enforceJavaSdkLibraryCheck: true, |
| 787 | }, "") |
| 788 | |
| 789 | runTest(t, testConfigInfo{ |
| 790 | libraryType: "java_library", |
| 791 | fromPartition: "product", |
| 792 | toPartition: "system", |
| 793 | enforceVendorInterface: true, |
| 794 | enforceProductInterface: true, |
| 795 | enforceJavaSdkLibraryCheck: true, |
| 796 | }, errorMessage) |
| 797 | |
| 798 | runTest(t, testConfigInfo{ |
| 799 | libraryType: "java_library", |
| 800 | fromPartition: "vendor", |
| 801 | toPartition: "system", |
| 802 | enforceVendorInterface: true, |
| 803 | enforceProductInterface: true, |
| 804 | enforceJavaSdkLibraryCheck: true, |
| 805 | }, errorMessage) |
| 806 | |
| 807 | runTest(t, testConfigInfo{ |
| 808 | libraryType: "java_library", |
| 809 | fromPartition: "vendor", |
| 810 | toPartition: "system", |
| 811 | enforceVendorInterface: true, |
| 812 | enforceProductInterface: true, |
| 813 | enforceJavaSdkLibraryCheck: true, |
| 814 | allowList: []string{"bar"}, |
| 815 | }, "") |
| 816 | |
| 817 | runTest(t, testConfigInfo{ |
| 818 | libraryType: "java_library", |
| 819 | fromPartition: "vendor", |
| 820 | toPartition: "product", |
| 821 | enforceVendorInterface: true, |
| 822 | enforceProductInterface: true, |
| 823 | enforceJavaSdkLibraryCheck: true, |
| 824 | }, errorMessage) |
| 825 | |
| 826 | runTest(t, testConfigInfo{ |
| 827 | libraryType: "java_sdk_library", |
| 828 | fromPartition: "product", |
| 829 | toPartition: "system", |
| 830 | enforceVendorInterface: true, |
| 831 | enforceProductInterface: true, |
| 832 | enforceJavaSdkLibraryCheck: true, |
| 833 | }, "") |
| 834 | |
| 835 | runTest(t, testConfigInfo{ |
| 836 | libraryType: "java_sdk_library", |
| 837 | fromPartition: "vendor", |
| 838 | toPartition: "system", |
| 839 | enforceVendorInterface: true, |
| 840 | enforceProductInterface: true, |
| 841 | enforceJavaSdkLibraryCheck: true, |
| 842 | }, "") |
| 843 | |
| 844 | runTest(t, testConfigInfo{ |
| 845 | libraryType: "java_sdk_library", |
| 846 | fromPartition: "vendor", |
| 847 | toPartition: "product", |
| 848 | enforceVendorInterface: true, |
| 849 | enforceProductInterface: true, |
| 850 | enforceJavaSdkLibraryCheck: true, |
| 851 | }, "") |
| 852 | } |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 853 | |
| 854 | func TestJavaSdkLibraryDist(t *testing.T) { |
| 855 | result := android.GroupFixturePreparers( |
| 856 | PrepareForTestWithJavaBuildComponents, |
| 857 | PrepareForTestWithJavaDefaultModules, |
| 858 | PrepareForTestWithJavaSdkLibraryFiles, |
Colin Cross | 67c17ae | 2021-06-02 13:02:51 -0700 | [diff] [blame] | 859 | FixtureWithLastReleaseApis( |
| 860 | "sdklib_no_group", |
| 861 | "sdklib_group_foo", |
| 862 | "sdklib_owner_foo", |
| 863 | "foo"), |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 864 | ).RunTestWithBp(t, ` |
| 865 | java_sdk_library { |
Colin Cross | 59b92bf | 2021-06-01 14:07:56 -0700 | [diff] [blame] | 866 | name: "sdklib_no_group", |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 867 | srcs: ["foo.java"], |
| 868 | } |
| 869 | |
| 870 | java_sdk_library { |
Colin Cross | 986b69a | 2021-06-01 13:13:40 -0700 | [diff] [blame] | 871 | name: "sdklib_group_foo", |
Colin Cross | 986b69a | 2021-06-01 13:13:40 -0700 | [diff] [blame] | 872 | srcs: ["foo.java"], |
| 873 | dist_group: "foo", |
| 874 | } |
| 875 | |
| 876 | java_sdk_library { |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 877 | name: "sdklib_owner_foo", |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 878 | srcs: ["foo.java"], |
| 879 | owner: "foo", |
| 880 | } |
| 881 | |
| 882 | java_sdk_library { |
| 883 | name: "sdklib_stem_foo", |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 884 | srcs: ["foo.java"], |
| 885 | dist_stem: "foo", |
| 886 | } |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 887 | `) |
| 888 | |
| 889 | type testCase struct { |
| 890 | module string |
| 891 | distDir string |
| 892 | distStem string |
| 893 | } |
| 894 | testCases := []testCase{ |
| 895 | { |
Colin Cross | 59b92bf | 2021-06-01 14:07:56 -0700 | [diff] [blame] | 896 | module: "sdklib_no_group", |
Colin Cross | 3dd6625 | 2021-06-01 14:05:09 -0700 | [diff] [blame] | 897 | distDir: "apistubs/unknown/public", |
Colin Cross | 59b92bf | 2021-06-01 14:07:56 -0700 | [diff] [blame] | 898 | distStem: "sdklib_no_group.jar", |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 899 | }, |
| 900 | { |
Colin Cross | 986b69a | 2021-06-01 13:13:40 -0700 | [diff] [blame] | 901 | module: "sdklib_group_foo", |
| 902 | distDir: "apistubs/foo/public", |
| 903 | distStem: "sdklib_group_foo.jar", |
| 904 | }, |
| 905 | { |
Colin Cross | 59b92bf | 2021-06-01 14:07:56 -0700 | [diff] [blame] | 906 | // Owner doesn't affect distDir after b/186723288. |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 907 | module: "sdklib_owner_foo", |
Colin Cross | 59b92bf | 2021-06-01 14:07:56 -0700 | [diff] [blame] | 908 | distDir: "apistubs/unknown/public", |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 909 | distStem: "sdklib_owner_foo.jar", |
| 910 | }, |
| 911 | { |
| 912 | module: "sdklib_stem_foo", |
Colin Cross | 3dd6625 | 2021-06-01 14:05:09 -0700 | [diff] [blame] | 913 | distDir: "apistubs/unknown/public", |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 914 | distStem: "foo.jar", |
| 915 | }, |
Colin Cross | 30c491b | 2021-06-01 13:39:09 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | for _, tt := range testCases { |
| 919 | t.Run(tt.module, func(t *testing.T) { |
| 920 | m := result.ModuleForTests(tt.module+".stubs", "android_common").Module().(*Library) |
| 921 | dists := m.Dists() |
| 922 | if len(dists) != 1 { |
| 923 | t.Fatalf("expected exactly 1 dist entry, got %d", len(dists)) |
| 924 | } |
| 925 | if g, w := String(dists[0].Dir), tt.distDir; g != w { |
| 926 | t.Errorf("expected dist dir %q, got %q", w, g) |
| 927 | } |
| 928 | if g, w := String(dists[0].Dest), tt.distStem; g != w { |
| 929 | t.Errorf("expected dist stem %q, got %q", w, g) |
| 930 | } |
| 931 | }) |
| 932 | } |
| 933 | } |