Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 ( |
Paul Duffin | 1a39332 | 2020-11-18 16:36:47 +0000 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 22 | "reflect" |
Paul Duffin | daaa332 | 2020-05-26 18:13:57 +0100 | [diff] [blame] | 23 | "regexp" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 24 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 25 | "strings" |
| 26 | "testing" |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 27 | |
Jeongik Cha | 28df257 | 2019-11-11 10:46:36 +0900 | [diff] [blame] | 28 | "github.com/google/blueprint/proptools" |
| 29 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 30 | "android/soong/android" |
| 31 | "android/soong/cc" |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 32 | "android/soong/dexpreopt" |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 33 | "android/soong/genrule" |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 34 | "android/soong/python" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | var buildDir string |
| 38 | |
| 39 | func setUp() { |
| 40 | var err error |
| 41 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 42 | if err != nil { |
| 43 | panic(err) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func tearDown() { |
| 48 | os.RemoveAll(buildDir) |
| 49 | } |
| 50 | |
Paul Duffin | 70d3bee | 2021-03-21 11:26:05 +0000 | [diff] [blame] | 51 | // Legacy factory to use to create fixtures for tests in this package. |
| 52 | // |
| 53 | // deprecated: See prepareForJavaTest |
| 54 | var javaFixtureFactory = android.NewFixtureFactory( |
| 55 | &buildDir, |
| 56 | prepareForJavaTest, |
| 57 | ) |
Paul Duffin | afeee22 | 2021-03-17 00:36:35 +0000 | [diff] [blame] | 58 | |
Paul Duffin | 70d3bee | 2021-03-21 11:26:05 +0000 | [diff] [blame] | 59 | // Legacy preparer used for running tests within the java package. |
| 60 | // |
| 61 | // This includes everything that was needed to run any test in the java package prior to the |
| 62 | // introduction of the test fixtures. Tests that are being converted to use fixtures directly |
| 63 | // rather than through the testJava...() methods should avoid using this and instead use the |
| 64 | // various preparers directly, using android.GroupFixturePreparers(...) to group them when |
| 65 | // necessary. |
| 66 | // |
| 67 | // deprecated |
| 68 | var prepareForJavaTest = android.GroupFixturePreparers( |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 69 | genrule.PrepareForTestWithGenRuleBuildComponents, |
| 70 | // Get the CC build components but not default modules. |
| 71 | cc.PrepareForTestWithCcBuildComponents, |
| 72 | // Include all the default java modules. |
| 73 | PrepareForTestWithJavaDefaultModules, |
Paul Duffin | 42da69d | 2021-03-22 13:41:36 +0000 | [diff] [blame] | 74 | PrepareForTestWithOverlayBuildComponents, |
Paul Duffin | 220ddd7 | 2021-03-17 23:54:30 +0000 | [diff] [blame] | 75 | python.PrepareForTestWithPythonBuildComponents, |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 76 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 77 | ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory) |
| 78 | }), |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 79 | dexpreopt.PrepareForTestWithDexpreopt, |
| 80 | ) |
| 81 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 82 | func TestMain(m *testing.M) { |
| 83 | run := func() int { |
| 84 | setUp() |
| 85 | defer tearDown() |
| 86 | |
| 87 | return m.Run() |
| 88 | } |
| 89 | |
| 90 | os.Exit(run()) |
| 91 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 92 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 93 | // testConfig is a legacy way of creating a test Config for testing java modules. |
| 94 | // |
| 95 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 96 | // |
| 97 | // deprecated |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 98 | func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config { |
Paul Duffin | 9f04524 | 2021-01-21 15:05:11 +0000 | [diff] [blame] | 99 | return TestConfig(buildDir, env, bp, fs) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 102 | // testContext is a legacy way of creating a TestContext for testing java modules. |
| 103 | // |
| 104 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 105 | // |
| 106 | // deprecated |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 107 | func testContext(config android.Config) *android.TestContext { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 108 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 109 | ctx := android.NewTestArchContext(config) |
Paul Duffin | c059c8c | 2021-01-20 17:13:52 +0000 | [diff] [blame] | 110 | RegisterRequiredBuildComponentsForTest(ctx) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 111 | ctx.RegisterModuleType("java_plugin", PluginFactory) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 112 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 113 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 114 | ctx.PreArchMutators(android.RegisterComponentsMutator) |
Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 115 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 116 | ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators) |
Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 117 | ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory) |
| 118 | ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 119 | |
Paul Duffin | 021f4e5 | 2020-07-30 16:04:17 +0100 | [diff] [blame] | 120 | android.RegisterPrebuiltMutators(ctx) |
| 121 | |
Paul Duffin | d6ceb86 | 2021-03-04 23:02:31 +0000 | [diff] [blame] | 122 | genrule.RegisterGenruleBuildComponents(ctx) |
| 123 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 124 | // Register module types and mutators from cc needed for JNI testing |
Paul Duffin | 77980a8 | 2019-12-19 16:01:36 +0000 | [diff] [blame] | 125 | cc.RegisterRequiredBuildComponentsForTest(ctx) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 126 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 127 | return ctx |
| 128 | } |
| 129 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 130 | // run is a legacy way of running tests of java modules. |
| 131 | // |
| 132 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 133 | // |
| 134 | // deprecated |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 135 | func run(t *testing.T, ctx *android.TestContext, config android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 136 | t.Helper() |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 137 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 138 | pathCtx := android.PathContextForTesting(config) |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 139 | dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 140 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 141 | ctx.Register() |
Paul Duffin | baccf7e | 2019-06-11 11:58:30 +0100 | [diff] [blame] | 142 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 143 | android.FailIfErrored(t, errs) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 144 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 145 | android.FailIfErrored(t, errs) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 148 | // testJavaError is a legacy way of running tests of java modules that expect errors. |
| 149 | // |
| 150 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 151 | // |
| 152 | // deprecated |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 153 | func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) { |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 154 | t.Helper() |
Paul Duffin | 6bac49c | 2021-03-12 21:28:15 +0000 | [diff] [blame] | 155 | result := javaFixtureFactory. |
| 156 | Extend(dexpreopt.PrepareForTestWithDexpreopt). |
| 157 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). |
| 158 | RunTestWithBp(t, bp) |
| 159 | return result.TestContext, result.Config |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 160 | } |
| 161 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 162 | // testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors. |
| 163 | // |
| 164 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 165 | // |
| 166 | // deprecated |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 167 | func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 168 | t.Helper() |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 169 | // This must be done on the supplied config and not as part of the fixture because any changes to |
| 170 | // the fixture's config will be ignored when RunTestWithConfig replaces it. |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 171 | pathCtx := android.PathContextForTesting(config) |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 172 | dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 173 | result := javaFixtureFactory. |
| 174 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). |
| 175 | RunTestWithConfig(t, config) |
| 176 | return result.TestContext, result.Config |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 179 | // runWithErrors is a legacy way of running tests of java modules that expect errors. |
| 180 | // |
| 181 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 182 | // |
| 183 | // deprecated |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 184 | func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 185 | ctx.Register() |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 186 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 187 | if len(errs) > 0 { |
| 188 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 189 | return |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 190 | } |
| 191 | _, errs = ctx.PrepareBuildActions(config) |
| 192 | if len(errs) > 0 { |
| 193 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 194 | return |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 198 | return |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 199 | } |
| 200 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 201 | // testJavaWithFS runs tests using the javaFixtureFactory |
| 202 | // |
| 203 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 204 | // |
| 205 | // deprecated |
| 206 | func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 207 | t.Helper() |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 208 | result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp) |
| 209 | return result.TestContext, result.Config |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 212 | // testJava runs tests using the javaFixtureFactory |
| 213 | // |
| 214 | // Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it |
| 215 | // much easier to customize the test behavior. |
| 216 | // |
| 217 | // If it is necessary to customize the behavior of an existing test that uses this then please first |
| 218 | // convert the test to using javaFixtureFactory first and then in a following change add the |
| 219 | // appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify |
| 220 | // that it did not change the test behavior unexpectedly. |
| 221 | // |
| 222 | // deprecated |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 223 | func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 224 | t.Helper() |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 225 | result := javaFixtureFactory.RunTestWithBp(t, bp) |
| 226 | return result.TestContext, result.Config |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 227 | } |
| 228 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 229 | // testJavaWithConfig runs tests using the javaFixtureFactory |
| 230 | // |
| 231 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 232 | // |
| 233 | // deprecated |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 234 | func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 235 | t.Helper() |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 236 | result := javaFixtureFactory.RunTestWithConfig(t, config) |
| 237 | return result.TestContext, result.Config |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 240 | func moduleToPath(name string) string { |
| 241 | switch { |
| 242 | case name == `""`: |
| 243 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 244 | case strings.HasSuffix(name, ".jar"): |
| 245 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 246 | default: |
| 247 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 251 | // defaultModuleToPath constructs a path to the turbine generate jar for a default test module that |
| 252 | // is defined in PrepareForIntegrationTestWithJava |
| 253 | func defaultModuleToPath(name string) string { |
Paul Duffin | 76101fa | 2021-03-13 09:55:25 +0000 | [diff] [blame] | 254 | switch { |
| 255 | case name == `""`: |
| 256 | return name |
| 257 | case strings.HasSuffix(name, ".jar"): |
| 258 | return name |
| 259 | default: |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 260 | return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar") |
Paul Duffin | 76101fa | 2021-03-13 09:55:25 +0000 | [diff] [blame] | 261 | } |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 264 | func TestJavaLinkType(t *testing.T) { |
| 265 | testJava(t, ` |
| 266 | java_library { |
| 267 | name: "foo", |
| 268 | srcs: ["a.java"], |
| 269 | libs: ["bar"], |
| 270 | static_libs: ["baz"], |
| 271 | } |
| 272 | |
| 273 | java_library { |
| 274 | name: "bar", |
| 275 | sdk_version: "current", |
| 276 | srcs: ["b.java"], |
| 277 | } |
| 278 | |
| 279 | java_library { |
| 280 | name: "baz", |
| 281 | sdk_version: "system_current", |
| 282 | srcs: ["c.java"], |
| 283 | } |
| 284 | `) |
| 285 | |
Steven Moreland | 0029898 | 2020-11-17 21:44:36 +0000 | [diff] [blame] | 286 | testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", ` |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 287 | java_library { |
| 288 | name: "foo", |
| 289 | srcs: ["a.java"], |
| 290 | libs: ["bar"], |
| 291 | sdk_version: "current", |
| 292 | static_libs: ["baz"], |
| 293 | } |
| 294 | |
| 295 | java_library { |
| 296 | name: "bar", |
| 297 | sdk_version: "current", |
| 298 | srcs: ["b.java"], |
| 299 | } |
| 300 | |
| 301 | java_library { |
| 302 | name: "baz", |
| 303 | sdk_version: "system_current", |
| 304 | srcs: ["c.java"], |
| 305 | } |
| 306 | `) |
| 307 | |
| 308 | testJava(t, ` |
| 309 | java_library { |
| 310 | name: "foo", |
| 311 | srcs: ["a.java"], |
| 312 | libs: ["bar"], |
| 313 | sdk_version: "system_current", |
| 314 | static_libs: ["baz"], |
| 315 | } |
| 316 | |
| 317 | java_library { |
| 318 | name: "bar", |
| 319 | sdk_version: "current", |
| 320 | srcs: ["b.java"], |
| 321 | } |
| 322 | |
| 323 | java_library { |
| 324 | name: "baz", |
| 325 | sdk_version: "system_current", |
| 326 | srcs: ["c.java"], |
| 327 | } |
| 328 | `) |
| 329 | |
Steven Moreland | 0029898 | 2020-11-17 21:44:36 +0000 | [diff] [blame] | 330 | testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", ` |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 331 | java_library { |
| 332 | name: "foo", |
| 333 | srcs: ["a.java"], |
| 334 | libs: ["bar"], |
| 335 | sdk_version: "system_current", |
| 336 | static_libs: ["baz"], |
| 337 | } |
| 338 | |
| 339 | java_library { |
| 340 | name: "bar", |
| 341 | sdk_version: "current", |
| 342 | srcs: ["b.java"], |
| 343 | } |
| 344 | |
| 345 | java_library { |
| 346 | name: "baz", |
| 347 | srcs: ["c.java"], |
| 348 | } |
| 349 | `) |
| 350 | } |
| 351 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 352 | func TestSimple(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 353 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 354 | java_library { |
| 355 | name: "foo", |
| 356 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 357 | libs: ["bar"], |
| 358 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | java_library { |
| 362 | name: "bar", |
| 363 | srcs: ["b.java"], |
| 364 | } |
| 365 | |
| 366 | java_library { |
| 367 | name: "baz", |
| 368 | srcs: ["c.java"], |
| 369 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 370 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 371 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 372 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 373 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 374 | |
| 375 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 376 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 377 | } |
| 378 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 379 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 380 | barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 381 | bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 382 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 383 | android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 384 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 385 | android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 386 | |
| 387 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 388 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 392 | func TestExportedPlugins(t *testing.T) { |
| 393 | type Result struct { |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 394 | library string |
| 395 | processors string |
| 396 | disableTurbine bool |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 397 | } |
| 398 | var tests = []struct { |
| 399 | name string |
| 400 | extra string |
| 401 | results []Result |
| 402 | }{ |
| 403 | { |
| 404 | name: "Exported plugin is not a direct plugin", |
| 405 | extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`, |
| 406 | results: []Result{{library: "exports", processors: "-proc:none"}}, |
| 407 | }, |
| 408 | { |
| 409 | name: "Exports plugin to dependee", |
| 410 | extra: ` |
| 411 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 412 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 413 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 414 | `, |
| 415 | results: []Result{ |
| 416 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 417 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 418 | }, |
| 419 | }, |
| 420 | { |
| 421 | name: "Exports plugin to android_library", |
| 422 | extra: ` |
| 423 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 424 | android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 425 | android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 426 | `, |
| 427 | results: []Result{ |
| 428 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 429 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 430 | }, |
| 431 | }, |
| 432 | { |
| 433 | name: "Exports plugin is not propagated via transitive deps", |
| 434 | extra: ` |
| 435 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 436 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 437 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]} |
| 438 | `, |
| 439 | results: []Result{ |
| 440 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 441 | {library: "bar", processors: "-proc:none"}, |
| 442 | }, |
| 443 | }, |
| 444 | { |
| 445 | name: "Exports plugin appends to plugins", |
| 446 | extra: ` |
| 447 | java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"} |
| 448 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 449 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]} |
| 450 | `, |
| 451 | results: []Result{ |
| 452 | {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"}, |
| 453 | }, |
| 454 | }, |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 455 | { |
| 456 | name: "Exports plugin to with generates_api to dependee", |
| 457 | extra: ` |
| 458 | java_library{name: "exports", exported_plugins: ["plugin_generates_api"]} |
| 459 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 460 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 461 | `, |
| 462 | results: []Result{ |
| 463 | {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true}, |
| 464 | {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true}, |
| 465 | }, |
| 466 | }, |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | for _, test := range tests { |
| 470 | t.Run(test.name, func(t *testing.T) { |
| 471 | ctx, _ := testJava(t, ` |
| 472 | java_plugin { |
| 473 | name: "plugin", |
| 474 | processor_class: "com.android.TestPlugin", |
| 475 | } |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 476 | java_plugin { |
| 477 | name: "plugin_generates_api", |
| 478 | generates_api: true, |
| 479 | processor_class: "com.android.TestPlugin", |
| 480 | } |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 481 | `+test.extra) |
| 482 | |
| 483 | for _, want := range test.results { |
| 484 | javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac") |
| 485 | if javac.Args["processor"] != want.processors { |
| 486 | t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"]) |
| 487 | } |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 488 | turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine") |
| 489 | disableTurbine := turbine.BuildParams.Rule == nil |
| 490 | if disableTurbine != want.disableTurbine { |
| 491 | t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine) |
| 492 | } |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 493 | } |
| 494 | }) |
| 495 | } |
| 496 | } |
| 497 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 498 | func TestSdkVersionByPartition(t *testing.T) { |
| 499 | testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 500 | java_library { |
| 501 | name: "foo", |
| 502 | srcs: ["a.java"], |
| 503 | vendor: true, |
| 504 | } |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 505 | `) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 506 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 507 | testJava(t, ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 508 | java_library { |
| 509 | name: "bar", |
| 510 | srcs: ["b.java"], |
| 511 | } |
| 512 | `) |
| 513 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 514 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 515 | bp := ` |
| 516 | java_library { |
| 517 | name: "foo", |
| 518 | srcs: ["a.java"], |
| 519 | product_specific: true, |
| 520 | } |
| 521 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 522 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 523 | errorHandler := android.FixtureExpectsNoErrors |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 524 | if enforce { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 525 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product") |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 526 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 527 | |
| 528 | android.GroupFixturePreparers( |
| 529 | PrepareForTestWithJavaDefaultModules, |
| 530 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 531 | variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
| 532 | }), |
| 533 | ). |
| 534 | ExtendWithErrorHandler(errorHandler). |
| 535 | RunTestWithBp(t, bp) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 539 | func TestArchSpecific(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 540 | ctx, _ := testJava(t, ` |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 541 | java_library { |
| 542 | name: "foo", |
| 543 | srcs: ["a.java"], |
| 544 | target: { |
| 545 | android: { |
| 546 | srcs: ["b.java"], |
| 547 | }, |
| 548 | }, |
| 549 | } |
| 550 | `) |
| 551 | |
| 552 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 553 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 554 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 555 | } |
| 556 | } |
| 557 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 558 | func TestBinary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 559 | ctx, _ := testJava(t, ` |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 560 | java_library_host { |
| 561 | name: "foo", |
| 562 | srcs: ["a.java"], |
| 563 | } |
| 564 | |
| 565 | java_binary_host { |
| 566 | name: "bar", |
| 567 | srcs: ["b.java"], |
| 568 | static_libs: ["foo"], |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 569 | jni_libs: ["libjni"], |
| 570 | } |
| 571 | |
| 572 | cc_library_shared { |
| 573 | name: "libjni", |
| 574 | host_supported: true, |
| 575 | device_supported: false, |
| 576 | stl: "none", |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 577 | } |
| 578 | `) |
| 579 | |
| 580 | buildOS := android.BuildOs.String() |
| 581 | |
| 582 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 583 | barJar := bar.Output("bar.jar").Output.String() |
| 584 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 585 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 586 | |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 587 | libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared") |
| 588 | libjniSO := libjni.Rule("Cp").Output.String() |
| 589 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 590 | // Test that the install binary wrapper depends on the installed jar file |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 591 | if g, w := barWrapperDeps, barJar; !android.InList(w, g) { |
| 592 | t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 593 | } |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 594 | |
| 595 | // Test that the install binary wrapper depends on the installed JNI libraries |
| 596 | if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) { |
| 597 | t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 598 | } |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 599 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 600 | |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 601 | func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) { |
| 602 | bp := ` |
| 603 | java_library { |
| 604 | name: "target_library", |
| 605 | srcs: ["a.java"], |
| 606 | } |
| 607 | |
| 608 | java_binary_host { |
| 609 | name: "host_binary", |
| 610 | srcs: ["b.java"], |
| 611 | } |
| 612 | ` |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 613 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 614 | result := android.GroupFixturePreparers( |
| 615 | PrepareForTestWithJavaDefaultModules, |
| 616 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 617 | variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true) |
| 618 | }), |
| 619 | ).RunTestWithBp(t, bp) |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 620 | |
Liz Kammer | 7941b30 | 2020-07-28 13:27:34 -0700 | [diff] [blame] | 621 | // first, check that the -g flag is added to target modules |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 622 | targetLibrary := result.ModuleForTests("target_library", "android_common") |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 623 | targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"] |
| 624 | if !strings.Contains(targetJavaFlags, "-g:source,lines") { |
| 625 | t.Errorf("target library javac flags %v should contain "+ |
| 626 | "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags) |
| 627 | } |
| 628 | |
| 629 | // check that -g is not overridden for host modules |
| 630 | buildOS := android.BuildOs.String() |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 631 | hostBinary := result.ModuleForTests("host_binary", buildOS+"_common") |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 632 | hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"] |
| 633 | if strings.Contains(hostJavaFlags, "-g:source,lines") { |
| 634 | t.Errorf("java_binary_host javac flags %v should not have "+ |
| 635 | "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags) |
| 636 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 639 | func TestPrebuilts(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 640 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 641 | java_library { |
| 642 | name: "foo", |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 643 | srcs: ["a.java", ":stubs-source"], |
Paul Duffin | fcfd791 | 2020-01-31 17:54:30 +0000 | [diff] [blame] | 644 | libs: ["bar", "sdklib"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 645 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 648 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 649 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 650 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 653 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 654 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 655 | jars: ["b.jar"], |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 656 | sdk_version: "current", |
| 657 | compile_dex: true, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 658 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 659 | |
| 660 | dex_import { |
| 661 | name: "qux", |
| 662 | jars: ["b.jar"], |
| 663 | } |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 664 | |
| 665 | java_sdk_library_import { |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 666 | name: "sdklib", |
| 667 | public: { |
| 668 | jars: ["c.jar"], |
| 669 | }, |
| 670 | } |
| 671 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 672 | prebuilt_stubs_sources { |
| 673 | name: "stubs-source", |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 674 | srcs: ["stubs/sources"], |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 675 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 676 | |
| 677 | java_test_import { |
| 678 | name: "test", |
| 679 | jars: ["a.jar"], |
| 680 | test_suites: ["cts"], |
| 681 | test_config: "AndroidTest.xml", |
| 682 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 683 | `) |
| 684 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 685 | fooModule := ctx.ModuleForTests("foo", "android_common") |
| 686 | javac := fooModule.Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 687 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 688 | barModule := ctx.ModuleForTests("bar", "android_common") |
| 689 | barJar := barModule.Rule("combineJar").Output |
| 690 | bazModule := ctx.ModuleForTests("baz", "android_common") |
| 691 | bazJar := bazModule.Rule("combineJar").Output |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 692 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 693 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 694 | fooLibrary := fooModule.Module().(*Library) |
| 695 | assertDeepEquals(t, "foo java sources incorrect", |
| 696 | []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings()) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 697 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 698 | assertDeepEquals(t, "foo java source jars incorrect", |
| 699 | []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"}, |
| 700 | android.NormalizePathsForTesting(fooLibrary.compiledSrcJars)) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 701 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 702 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 703 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 706 | barDexJar := barModule.Module().(*Import).DexJarBuildPath() |
| 707 | if barDexJar != nil { |
| 708 | t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar) |
| 709 | } |
| 710 | |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 711 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 712 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 713 | } |
| 714 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 715 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 716 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 717 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 718 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 719 | bazDexJar := bazModule.Module().(*Import).DexJarBuildPath() |
| 720 | expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar" |
| 721 | android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 722 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 723 | ctx.ModuleForTests("qux", "android_common").Rule("Cp") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 726 | func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) { |
| 727 | if !reflect.DeepEqual(expected, actual) { |
| 728 | t.Errorf("%s: expected %q, found %q", message, expected, actual) |
| 729 | } |
| 730 | } |
| 731 | |
Paul Duffin | 1a39332 | 2020-11-18 16:36:47 +0000 | [diff] [blame] | 732 | func TestPrebuiltStubsSources(t *testing.T) { |
| 733 | test := func(t *testing.T, sourcesPath string, expectedInputs []string) { |
| 734 | ctx, _ := testJavaWithFS(t, fmt.Sprintf(` |
| 735 | prebuilt_stubs_sources { |
| 736 | name: "stubs-source", |
| 737 | srcs: ["%s"], |
| 738 | }`, sourcesPath), map[string][]byte{ |
| 739 | "stubs/sources/pkg/A.java": nil, |
| 740 | "stubs/sources/pkg/B.java": nil, |
| 741 | }) |
| 742 | |
| 743 | zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src") |
| 744 | if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) { |
| 745 | t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual) |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | t.Run("empty/missing directory", func(t *testing.T) { |
| 750 | test(t, "empty-directory", []string{}) |
| 751 | }) |
| 752 | |
| 753 | t.Run("non-empty set of sources", func(t *testing.T) { |
| 754 | test(t, "stubs/sources", []string{ |
| 755 | "stubs/sources/pkg/A.java", |
| 756 | "stubs/sources/pkg/B.java", |
| 757 | }) |
| 758 | }) |
| 759 | } |
| 760 | |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 761 | func TestJavaSdkLibraryImport(t *testing.T) { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 762 | result := javaFixtureFactory.RunTestWithBp(t, ` |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 763 | java_library { |
| 764 | name: "foo", |
| 765 | srcs: ["a.java"], |
| 766 | libs: ["sdklib"], |
| 767 | sdk_version: "current", |
| 768 | } |
| 769 | |
| 770 | java_library { |
| 771 | name: "foo.system", |
| 772 | srcs: ["a.java"], |
| 773 | libs: ["sdklib"], |
| 774 | sdk_version: "system_current", |
| 775 | } |
| 776 | |
| 777 | java_library { |
| 778 | name: "foo.test", |
| 779 | srcs: ["a.java"], |
| 780 | libs: ["sdklib"], |
| 781 | sdk_version: "test_current", |
| 782 | } |
| 783 | |
| 784 | java_sdk_library_import { |
| 785 | name: "sdklib", |
| 786 | public: { |
| 787 | jars: ["a.jar"], |
| 788 | }, |
| 789 | system: { |
| 790 | jars: ["b.jar"], |
| 791 | }, |
| 792 | test: { |
| 793 | jars: ["c.jar"], |
Paul Duffin | 0f8faff | 2020-05-20 16:18:00 +0100 | [diff] [blame] | 794 | stub_srcs: ["c.java"], |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 795 | }, |
| 796 | } |
| 797 | `) |
| 798 | |
| 799 | for _, scope := range []string{"", ".system", ".test"} { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 800 | fooModule := result.ModuleForTests("foo"+scope, "android_common") |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 801 | javac := fooModule.Rule("javac") |
| 802 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 803 | sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output |
| 804 | android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String()) |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 805 | } |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 806 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 807 | CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{ |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 808 | `prebuilt_sdklib.stubs`, |
| 809 | `prebuilt_sdklib.stubs.source.test`, |
| 810 | `prebuilt_sdklib.stubs.system`, |
| 811 | `prebuilt_sdklib.stubs.test`, |
| 812 | }) |
| 813 | } |
| 814 | |
| 815 | func TestJavaSdkLibraryImport_WithSource(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 816 | result := javaFixtureFactory.Extend( |
| 817 | PrepareForTestWithJavaSdkLibraryFiles, |
| 818 | FixtureWithLastReleaseApis("sdklib"), |
| 819 | ).RunTestWithBp(t, ` |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 820 | java_sdk_library { |
| 821 | name: "sdklib", |
| 822 | srcs: ["a.java"], |
| 823 | sdk_version: "none", |
| 824 | system_modules: "none", |
| 825 | public: { |
| 826 | enabled: true, |
| 827 | }, |
| 828 | } |
| 829 | |
| 830 | java_sdk_library_import { |
| 831 | name: "sdklib", |
| 832 | public: { |
| 833 | jars: ["a.jar"], |
| 834 | }, |
| 835 | } |
| 836 | `) |
| 837 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 838 | CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{ |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 839 | `dex2oatd`, |
| 840 | `prebuilt_sdklib`, |
| 841 | `sdklib.impl`, |
| 842 | `sdklib.stubs`, |
| 843 | `sdklib.stubs.source`, |
| 844 | `sdklib.xml`, |
| 845 | }) |
| 846 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 847 | CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{ |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 848 | `prebuilt_sdklib.stubs`, |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 849 | `sdklib.impl`, |
| 850 | // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the |
| 851 | // dependency is added after prebuilts may have been renamed and so has to use |
| 852 | // the renamed name. |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 853 | `sdklib.xml`, |
| 854 | }) |
| 855 | } |
| 856 | |
| 857 | func TestJavaSdkLibraryImport_Preferred(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 858 | result := javaFixtureFactory.Extend( |
| 859 | PrepareForTestWithJavaSdkLibraryFiles, |
| 860 | FixtureWithLastReleaseApis("sdklib"), |
| 861 | ).RunTestWithBp(t, ` |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 862 | java_sdk_library { |
| 863 | name: "sdklib", |
| 864 | srcs: ["a.java"], |
| 865 | sdk_version: "none", |
| 866 | system_modules: "none", |
| 867 | public: { |
| 868 | enabled: true, |
| 869 | }, |
| 870 | } |
| 871 | |
| 872 | java_sdk_library_import { |
| 873 | name: "sdklib", |
| 874 | prefer: true, |
| 875 | public: { |
| 876 | jars: ["a.jar"], |
| 877 | }, |
| 878 | } |
| 879 | `) |
| 880 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 881 | CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{ |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 882 | `dex2oatd`, |
| 883 | `prebuilt_sdklib`, |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 884 | `sdklib.impl`, |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 885 | `sdklib.stubs`, |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 886 | `sdklib.stubs.source`, |
| 887 | `sdklib.xml`, |
| 888 | }) |
| 889 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 890 | CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{ |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 891 | `prebuilt_sdklib.stubs`, |
| 892 | `sdklib.impl`, |
| 893 | `sdklib.xml`, |
| 894 | }) |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 895 | } |
| 896 | |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 897 | func TestJavaSdkLibraryEnforce(t *testing.T) { |
| 898 | partitionToBpOption := func(partition string) string { |
| 899 | switch partition { |
| 900 | case "system": |
| 901 | return "" |
| 902 | case "vendor": |
| 903 | return "soc_specific: true," |
| 904 | case "product": |
| 905 | return "product_specific: true," |
| 906 | default: |
| 907 | panic("Invalid partition group name: " + partition) |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | type testConfigInfo struct { |
| 912 | libraryType string |
| 913 | fromPartition string |
| 914 | toPartition string |
| 915 | enforceVendorInterface bool |
| 916 | enforceProductInterface bool |
| 917 | enforceJavaSdkLibraryCheck bool |
| 918 | allowList []string |
| 919 | } |
| 920 | |
Paul Duffin | 05f72de | 2021-03-12 21:28:51 +0000 | [diff] [blame] | 921 | createPreparer := func(info testConfigInfo) android.FixturePreparer { |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 922 | bpFileTemplate := ` |
| 923 | java_library { |
| 924 | name: "foo", |
| 925 | srcs: ["foo.java"], |
| 926 | libs: ["bar"], |
| 927 | sdk_version: "current", |
| 928 | %s |
| 929 | } |
| 930 | |
| 931 | %s { |
| 932 | name: "bar", |
| 933 | srcs: ["bar.java"], |
| 934 | sdk_version: "current", |
| 935 | %s |
| 936 | } |
| 937 | ` |
| 938 | |
| 939 | bpFile := fmt.Sprintf(bpFileTemplate, |
| 940 | partitionToBpOption(info.fromPartition), |
| 941 | info.libraryType, |
| 942 | partitionToBpOption(info.toPartition)) |
| 943 | |
Paul Duffin | 05f72de | 2021-03-12 21:28:51 +0000 | [diff] [blame] | 944 | return android.GroupFixturePreparers( |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 945 | PrepareForTestWithJavaSdkLibraryFiles, |
| 946 | FixtureWithLastReleaseApis("bar"), |
Paul Duffin | 05f72de | 2021-03-12 21:28:51 +0000 | [diff] [blame] | 947 | android.FixtureWithRootAndroidBp(bpFile), |
| 948 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 949 | variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface) |
| 950 | if info.enforceVendorInterface { |
| 951 | variables.DeviceVndkVersion = proptools.StringPtr("current") |
| 952 | } |
| 953 | variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck) |
| 954 | variables.InterPartitionJavaLibraryAllowList = info.allowList |
| 955 | }), |
| 956 | ) |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 957 | } |
| 958 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 959 | runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) { |
| 960 | t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) { |
Paul Duffin | 05f72de | 2021-03-12 21:28:51 +0000 | [diff] [blame] | 961 | errorHandler := android.FixtureExpectsNoErrors |
| 962 | if expectedErrorPattern != "" { |
| 963 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern) |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 964 | } |
Paul Duffin | 05f72de | 2021-03-12 21:28:51 +0000 | [diff] [blame] | 965 | javaFixtureFactory.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info)) |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 966 | }) |
| 967 | } |
| 968 | |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 969 | errorMessage := "is not allowed across the partitions" |
| 970 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 971 | runTest(t, testConfigInfo{ |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 972 | libraryType: "java_library", |
| 973 | fromPartition: "product", |
| 974 | toPartition: "system", |
| 975 | enforceVendorInterface: true, |
| 976 | enforceProductInterface: true, |
| 977 | enforceJavaSdkLibraryCheck: false, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 978 | }, "") |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 979 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 980 | runTest(t, testConfigInfo{ |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 981 | libraryType: "java_library", |
| 982 | fromPartition: "product", |
| 983 | toPartition: "system", |
| 984 | enforceVendorInterface: true, |
| 985 | enforceProductInterface: false, |
| 986 | enforceJavaSdkLibraryCheck: true, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 987 | }, "") |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 988 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 989 | runTest(t, testConfigInfo{ |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 990 | libraryType: "java_library", |
| 991 | fromPartition: "product", |
| 992 | toPartition: "system", |
| 993 | enforceVendorInterface: true, |
| 994 | enforceProductInterface: true, |
| 995 | enforceJavaSdkLibraryCheck: true, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 996 | }, errorMessage) |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 997 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 998 | runTest(t, testConfigInfo{ |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 999 | libraryType: "java_library", |
| 1000 | fromPartition: "vendor", |
| 1001 | toPartition: "system", |
| 1002 | enforceVendorInterface: true, |
| 1003 | enforceProductInterface: true, |
| 1004 | enforceJavaSdkLibraryCheck: true, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1005 | }, errorMessage) |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 1006 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1007 | runTest(t, testConfigInfo{ |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 1008 | libraryType: "java_library", |
| 1009 | fromPartition: "vendor", |
| 1010 | toPartition: "system", |
| 1011 | enforceVendorInterface: true, |
| 1012 | enforceProductInterface: true, |
| 1013 | enforceJavaSdkLibraryCheck: true, |
| 1014 | allowList: []string{"bar"}, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1015 | }, "") |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 1016 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1017 | runTest(t, testConfigInfo{ |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 1018 | libraryType: "java_library", |
| 1019 | fromPartition: "vendor", |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 1020 | toPartition: "product", |
| 1021 | enforceVendorInterface: true, |
| 1022 | enforceProductInterface: true, |
| 1023 | enforceJavaSdkLibraryCheck: true, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1024 | }, errorMessage) |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 1025 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1026 | runTest(t, testConfigInfo{ |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 1027 | libraryType: "java_sdk_library", |
| 1028 | fromPartition: "product", |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 1029 | toPartition: "system", |
| 1030 | enforceVendorInterface: true, |
| 1031 | enforceProductInterface: true, |
| 1032 | enforceJavaSdkLibraryCheck: true, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1033 | }, "") |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 1034 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1035 | runTest(t, testConfigInfo{ |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 1036 | libraryType: "java_sdk_library", |
| 1037 | fromPartition: "vendor", |
| 1038 | toPartition: "system", |
| 1039 | enforceVendorInterface: true, |
| 1040 | enforceProductInterface: true, |
| 1041 | enforceJavaSdkLibraryCheck: true, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1042 | }, "") |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 1043 | |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1044 | runTest(t, testConfigInfo{ |
JaeMan Park | 90e7535 | 2021-01-14 11:56:56 +0900 | [diff] [blame] | 1045 | libraryType: "java_sdk_library", |
| 1046 | fromPartition: "vendor", |
| 1047 | toPartition: "product", |
| 1048 | enforceVendorInterface: true, |
| 1049 | enforceProductInterface: true, |
| 1050 | enforceJavaSdkLibraryCheck: true, |
Paul Duffin | aa6caa7 | 2021-03-13 16:50:15 +0000 | [diff] [blame] | 1051 | }, "") |
JaeMan Park | ff71556 | 2020-10-19 17:25:58 +0900 | [diff] [blame] | 1052 | } |
| 1053 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 1054 | func TestDefaults(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1055 | ctx, _ := testJava(t, ` |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 1056 | java_defaults { |
| 1057 | name: "defaults", |
| 1058 | srcs: ["a.java"], |
| 1059 | libs: ["bar"], |
| 1060 | static_libs: ["baz"], |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 1061 | optimize: {enabled: false}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | java_library { |
| 1065 | name: "foo", |
| 1066 | defaults: ["defaults"], |
| 1067 | } |
| 1068 | |
| 1069 | java_library { |
| 1070 | name: "bar", |
| 1071 | srcs: ["b.java"], |
| 1072 | } |
| 1073 | |
| 1074 | java_library { |
| 1075 | name: "baz", |
| 1076 | srcs: ["c.java"], |
| 1077 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 1078 | |
| 1079 | android_test { |
| 1080 | name: "atestOptimize", |
| 1081 | defaults: ["defaults"], |
| 1082 | optimize: {enabled: true}, |
| 1083 | } |
| 1084 | |
| 1085 | android_test { |
| 1086 | name: "atestNoOptimize", |
| 1087 | defaults: ["defaults"], |
| 1088 | } |
| 1089 | |
| 1090 | android_test { |
| 1091 | name: "atestDefault", |
| 1092 | srcs: ["a.java"], |
| 1093 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 1094 | `) |
| 1095 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1096 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 1097 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 1098 | |
| 1099 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 1100 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 1101 | } |
| 1102 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1103 | barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 1104 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 1105 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 1108 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 1109 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 1110 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 1111 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 1112 | |
| 1113 | atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8") |
| 1114 | if atestOptimize.Output == nil { |
| 1115 | t.Errorf("atestOptimize should optimize APK") |
| 1116 | } |
| 1117 | |
| 1118 | atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8") |
| 1119 | if atestNoOptimize.Output == nil { |
| 1120 | t.Errorf("atestNoOptimize should not optimize APK") |
| 1121 | } |
| 1122 | |
| 1123 | atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8") |
| 1124 | if atestDefault.Output == nil { |
| 1125 | t.Errorf("atestDefault should optimize APK") |
| 1126 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1129 | func TestResources(t *testing.T) { |
| 1130 | var table = []struct { |
| 1131 | name string |
| 1132 | prop string |
| 1133 | extra string |
| 1134 | args string |
| 1135 | }{ |
| 1136 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 1137 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1138 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 1139 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 1140 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1141 | }, |
| 1142 | { |
| 1143 | // Test that a module with java_resources includes the files |
| 1144 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 1145 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 1146 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1147 | }, |
| 1148 | { |
| 1149 | // Test that a module with a filegroup in java_resources includes the files with the |
| 1150 | // path prefix |
| 1151 | name: "resource filegroup", |
| 1152 | prop: `java_resources: [":foo-res"]`, |
| 1153 | extra: ` |
| 1154 | filegroup { |
| 1155 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 1156 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 1157 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1158 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 1159 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1160 | }, |
| 1161 | { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 1162 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 1163 | name: "wildcard dirs", |
| 1164 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 1165 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 1166 | }, |
| 1167 | { |
| 1168 | // Test that a module exclude_java_resource_dirs excludes the files |
| 1169 | name: "wildcard dirs", |
| 1170 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 1171 | args: "-C java-res/a -f java-res/a/a", |
| 1172 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 1173 | { |
| 1174 | // Test wildcards in java_resources |
| 1175 | name: "wildcard files", |
| 1176 | prop: `java_resources: ["java-res/**/*"]`, |
| 1177 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 1178 | }, |
| 1179 | { |
| 1180 | // Test exclude_java_resources with java_resources |
| 1181 | name: "wildcard files with exclude", |
| 1182 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 1183 | args: "-C . -f java-res/a/a", |
| 1184 | }, |
| 1185 | { |
| 1186 | // Test exclude_java_resources with java_resource_dirs |
| 1187 | name: "resource dirs with exclude files", |
| 1188 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 1189 | args: "-C java-res -f java-res/a/a", |
| 1190 | }, |
| 1191 | { |
| 1192 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 1193 | name: "resource dirs with exclude files", |
| 1194 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 1195 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 1196 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | for _, test := range table { |
| 1200 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1201 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1202 | java_library { |
| 1203 | name: "foo", |
| 1204 | srcs: [ |
| 1205 | "a.java", |
| 1206 | "b.java", |
| 1207 | "c.java", |
| 1208 | ], |
| 1209 | `+test.prop+`, |
| 1210 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1211 | `+test.extra, |
| 1212 | map[string][]byte{ |
| 1213 | "java-res/a/a": nil, |
| 1214 | "java-res/b/b": nil, |
| 1215 | "java-res2/a": nil, |
| 1216 | }, |
| 1217 | ) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1218 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 1219 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 1220 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1221 | |
| 1222 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 1223 | t.Errorf("foo combined jars %v does not contain %q", |
| 1224 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 1225 | } |
| 1226 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 1227 | if fooRes.Args["jarArgs"] != test.args { |
| 1228 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 1229 | fooRes.Args["jarArgs"], test.args) |
| 1230 | } |
| 1231 | }) |
| 1232 | } |
| 1233 | } |
| 1234 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 1235 | func TestIncludeSrcs(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1236 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 1237 | java_library { |
| 1238 | name: "foo", |
| 1239 | srcs: [ |
| 1240 | "a.java", |
| 1241 | "b.java", |
| 1242 | "c.java", |
| 1243 | ], |
| 1244 | include_srcs: true, |
| 1245 | } |
| 1246 | |
| 1247 | java_library { |
| 1248 | name: "bar", |
| 1249 | srcs: [ |
| 1250 | "a.java", |
| 1251 | "b.java", |
| 1252 | "c.java", |
| 1253 | ], |
| 1254 | java_resource_dirs: ["java-res"], |
| 1255 | include_srcs: true, |
| 1256 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1257 | `, map[string][]byte{ |
| 1258 | "java-res/a/a": nil, |
| 1259 | "java-res/b/b": nil, |
| 1260 | "java-res2/a": nil, |
| 1261 | }) |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 1262 | |
| 1263 | // Test a library with include_srcs: true |
| 1264 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
| 1265 | fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar") |
| 1266 | |
| 1267 | if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) { |
| 1268 | t.Errorf("foo combined jars %v does not contain %q", w, g) |
| 1269 | } |
| 1270 | |
| 1271 | if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 1272 | t.Errorf("foo source jar args %q is not %q", w, g) |
| 1273 | } |
| 1274 | |
| 1275 | // Test a library with include_srcs: true and resources |
| 1276 | bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar") |
| 1277 | barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar") |
| 1278 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
| 1279 | barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar") |
| 1280 | |
| 1281 | if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 1282 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 1283 | } |
| 1284 | |
| 1285 | if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 1286 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 1287 | } |
| 1288 | |
| 1289 | if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) { |
| 1290 | t.Errorf("bar combined jars %v does not contain %q", w, g) |
| 1291 | } |
| 1292 | |
| 1293 | if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 1294 | t.Errorf("bar source jar args %q is not %q", w, g) |
| 1295 | } |
| 1296 | |
| 1297 | if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w { |
| 1298 | t.Errorf("bar resource jar args %q is not %q", w, g) |
| 1299 | } |
| 1300 | } |
| 1301 | |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1302 | func TestJavaLint(t *testing.T) { |
| 1303 | ctx, _ := testJavaWithFS(t, ` |
| 1304 | java_library { |
| 1305 | name: "foo", |
| 1306 | srcs: [ |
| 1307 | "a.java", |
| 1308 | "b.java", |
| 1309 | "c.java", |
| 1310 | ], |
| 1311 | min_sdk_version: "29", |
| 1312 | sdk_version: "system_current", |
| 1313 | } |
| 1314 | `, map[string][]byte{ |
| 1315 | "lint-baseline.xml": nil, |
| 1316 | }) |
| 1317 | |
| 1318 | foo := ctx.ModuleForTests("foo", "android_common") |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1319 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 1320 | sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto")) |
| 1321 | if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") { |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1322 | t.Error("did not pass --baseline flag") |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | func TestJavaLintWithoutBaseline(t *testing.T) { |
| 1327 | ctx, _ := testJavaWithFS(t, ` |
| 1328 | java_library { |
| 1329 | name: "foo", |
| 1330 | srcs: [ |
| 1331 | "a.java", |
| 1332 | "b.java", |
| 1333 | "c.java", |
| 1334 | ], |
| 1335 | min_sdk_version: "29", |
| 1336 | sdk_version: "system_current", |
| 1337 | } |
| 1338 | `, map[string][]byte{}) |
| 1339 | |
| 1340 | foo := ctx.ModuleForTests("foo", "android_common") |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1341 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 1342 | sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto")) |
| 1343 | if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") { |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1344 | t.Error("passed --baseline flag for non existent file") |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1349 | config := TestConfig(t.TempDir(), |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1350 | nil, |
| 1351 | ` |
| 1352 | java_library { |
| 1353 | name: "foo", |
| 1354 | srcs: [ |
| 1355 | ], |
| 1356 | min_sdk_version: "29", |
| 1357 | sdk_version: "system_current", |
| 1358 | lint: { |
| 1359 | baseline_filename: "mybaseline.xml", |
| 1360 | }, |
| 1361 | } |
| 1362 | `, map[string][]byte{ |
| 1363 | "build/soong/java/lint_defaults.txt": nil, |
| 1364 | "prebuilts/cmdline-tools/tools/bin/lint": nil, |
| 1365 | "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil, |
| 1366 | "framework/aidl": nil, |
| 1367 | "a.java": nil, |
| 1368 | "AndroidManifest.xml": nil, |
| 1369 | "build/make/target/product/security": nil, |
| 1370 | }) |
| 1371 | config.TestAllowNonExistentPaths = false |
| 1372 | testJavaErrorWithConfig(t, |
| 1373 | "source path \"mybaseline.xml\" does not exist", |
| 1374 | config, |
| 1375 | ) |
| 1376 | } |
| 1377 | |
| 1378 | func TestJavaLintUsesCorrectBpConfig(t *testing.T) { |
| 1379 | ctx, _ := testJavaWithFS(t, ` |
| 1380 | java_library { |
| 1381 | name: "foo", |
| 1382 | srcs: [ |
| 1383 | "a.java", |
| 1384 | "b.java", |
| 1385 | "c.java", |
| 1386 | ], |
| 1387 | min_sdk_version: "29", |
| 1388 | sdk_version: "system_current", |
| 1389 | lint: { |
| 1390 | error_checks: ["SomeCheck"], |
| 1391 | baseline_filename: "mybaseline.xml", |
| 1392 | }, |
| 1393 | } |
| 1394 | `, map[string][]byte{ |
| 1395 | "mybaseline.xml": nil, |
| 1396 | }) |
| 1397 | |
| 1398 | foo := ctx.ModuleForTests("foo", "android_common") |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1399 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 1400 | sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto")) |
| 1401 | if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") { |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1402 | t.Error("did not use the correct file for baseline") |
| 1403 | } |
| 1404 | } |
| 1405 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 1406 | func TestGeneratedSources(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1407 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 1408 | java_library { |
| 1409 | name: "foo", |
| 1410 | srcs: [ |
| 1411 | "a*.java", |
| 1412 | ":gen", |
| 1413 | "b*.java", |
| 1414 | ], |
| 1415 | } |
| 1416 | |
| 1417 | genrule { |
| 1418 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 1419 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 1420 | out: ["gen.java"], |
| 1421 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1422 | `, map[string][]byte{ |
| 1423 | "a.java": nil, |
| 1424 | "b.java": nil, |
| 1425 | }) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 1426 | |
| 1427 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1428 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 1429 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 1430 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 1431 | t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String()) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | if len(javac.Inputs) != 3 || |
| 1435 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 1436 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 1437 | javac.Inputs[2].String() != "b.java" { |
| 1438 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 1439 | } |
| 1440 | } |
| 1441 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1442 | func TestTurbine(t *testing.T) { |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 1443 | result := javaFixtureFactory. |
| 1444 | Extend(FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})). |
| 1445 | RunTestWithBp(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1446 | java_library { |
| 1447 | name: "foo", |
| 1448 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 1449 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | java_library { |
| 1453 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 1454 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1455 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 1456 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | java_library { |
| 1460 | name: "baz", |
| 1461 | srcs: ["c.java"], |
| 1462 | libs: ["bar"], |
| 1463 | sdk_version: "14", |
| 1464 | } |
| 1465 | `) |
| 1466 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1467 | fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine").RelativeToTop() |
| 1468 | barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine").RelativeToTop() |
| 1469 | barJavac := result.ModuleForTests("bar", "android_common").Rule("javac").RelativeToTop() |
| 1470 | barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine").RelativeToTop() |
| 1471 | bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac").RelativeToTop() |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1472 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1473 | android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs) |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1474 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1475 | fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 1476 | barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar") |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 1477 | android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar) |
| 1478 | android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar) |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1479 | android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs) |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 1480 | android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | func TestSharding(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1484 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1485 | java_library { |
| 1486 | name: "bar", |
| 1487 | srcs: ["a.java","b.java","c.java"], |
| 1488 | javac_shard_size: 1 |
| 1489 | } |
| 1490 | `) |
| 1491 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1492 | barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1493 | for i := 0; i < 3; i++ { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1494 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)).RelativeToTop() |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1495 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 1496 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1501 | func TestDroiddoc(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1502 | ctx, _ := testJavaWithFS(t, ` |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 1503 | droiddoc_exported_dir { |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 1504 | name: "droiddoc-templates-sdk", |
| 1505 | path: ".", |
| 1506 | } |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 1507 | filegroup { |
| 1508 | name: "bar-doc-aidl-srcs", |
| 1509 | srcs: ["bar-doc/IBar.aidl"], |
| 1510 | path: "bar-doc", |
| 1511 | } |
Liz Kammer | e1ab250 | 2020-09-10 15:29:25 +0000 | [diff] [blame] | 1512 | droidstubs { |
| 1513 | name: "bar-stubs", |
Liz Kammer | 3a55c91 | 2020-08-14 12:14:02 -0700 | [diff] [blame] | 1514 | srcs: [ |
Steve Kim | 3666c70 | 2020-09-01 17:58:01 +0000 | [diff] [blame] | 1515 | "bar-doc/a.java", |
Liz Kammer | 3a55c91 | 2020-08-14 12:14:02 -0700 | [diff] [blame] | 1516 | ], |
Steve Kim | 3666c70 | 2020-09-01 17:58:01 +0000 | [diff] [blame] | 1517 | exclude_srcs: [ |
| 1518 | "bar-doc/b.java" |
| 1519 | ], |
Liz Kammer | e1ab250 | 2020-09-10 15:29:25 +0000 | [diff] [blame] | 1520 | api_levels_annotations_dirs: [ |
| 1521 | "droiddoc-templates-sdk", |
| 1522 | ], |
| 1523 | api_levels_annotations_enabled: true, |
| 1524 | } |
| 1525 | droiddoc { |
| 1526 | name: "bar-doc", |
| 1527 | srcs: [ |
| 1528 | ":bar-stubs", |
| 1529 | "bar-doc/IFoo.aidl", |
| 1530 | ":bar-doc-aidl-srcs", |
| 1531 | ], |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 1532 | custom_template: "droiddoc-templates-sdk", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1533 | hdf: [ |
| 1534 | "android.whichdoc offline", |
| 1535 | ], |
| 1536 | knowntags: [ |
| 1537 | "bar-doc/known_oj_tags.txt", |
| 1538 | ], |
| 1539 | proofread_file: "libcore-proofread.txt", |
| 1540 | todo_file: "libcore-docs-todo.html", |
Liz Kammer | 585cac2 | 2020-07-06 09:12:57 -0700 | [diff] [blame] | 1541 | flags: ["-offlinemode -title \"libcore\""], |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1542 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1543 | `, |
| 1544 | map[string][]byte{ |
| 1545 | "bar-doc/a.java": nil, |
| 1546 | "bar-doc/b.java": nil, |
| 1547 | }) |
Liz Kammer | e1ab250 | 2020-09-10 15:29:25 +0000 | [diff] [blame] | 1548 | barStubs := ctx.ModuleForTests("bar-stubs", "android_common") |
| 1549 | barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("") |
| 1550 | if err != nil { |
| 1551 | t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err) |
| 1552 | } |
| 1553 | if len(barStubsOutputs) != 1 { |
| 1554 | t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs) |
Liz Kammer | 1e2ee12 | 2020-07-30 15:07:22 -0700 | [diff] [blame] | 1555 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1556 | |
Liz Kammer | e1ab250 | 2020-09-10 15:29:25 +0000 | [diff] [blame] | 1557 | barStubsOutput := barStubsOutputs[0] |
| 1558 | barDoc := ctx.ModuleForTests("bar-doc", "android_common") |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1559 | javaDoc := barDoc.Rule("javadoc").RelativeToTop() |
Liz Kammer | e1ab250 | 2020-09-10 15:29:25 +0000 | [diff] [blame] | 1560 | if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) { |
| 1561 | t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g) |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 1562 | } |
| 1563 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1564 | expected := "-sourcepath out/soong/.intermediates/bar-doc/android_common/srcjars " |
Liz Kammer | e1ab250 | 2020-09-10 15:29:25 +0000 | [diff] [blame] | 1565 | if !strings.Contains(javaDoc.RuleParams.Command, expected) { |
| 1566 | t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command) |
| 1567 | } |
| 1568 | |
| 1569 | aidl := barDoc.Rule("aidl") |
| 1570 | if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) { |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 1571 | t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g) |
| 1572 | } |
| 1573 | |
| 1574 | if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) { |
| 1575 | t.Errorf("aidl inputs must be %q, but was %q", w, g) |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 1576 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1577 | } |
| 1578 | |
Liz Kammer | 585cac2 | 2020-07-06 09:12:57 -0700 | [diff] [blame] | 1579 | func TestDroiddocArgsAndFlagsCausesError(t *testing.T) { |
| 1580 | testJavaError(t, "flags is set. Cannot set args", ` |
| 1581 | droiddoc_exported_dir { |
| 1582 | name: "droiddoc-templates-sdk", |
| 1583 | path: ".", |
| 1584 | } |
| 1585 | filegroup { |
| 1586 | name: "bar-doc-aidl-srcs", |
| 1587 | srcs: ["bar-doc/IBar.aidl"], |
| 1588 | path: "bar-doc", |
| 1589 | } |
Liz Kammer | e1ab250 | 2020-09-10 15:29:25 +0000 | [diff] [blame] | 1590 | droidstubs { |
| 1591 | name: "bar-stubs", |
Liz Kammer | 3a55c91 | 2020-08-14 12:14:02 -0700 | [diff] [blame] | 1592 | srcs: [ |
Steve Kim | 3666c70 | 2020-09-01 17:58:01 +0000 | [diff] [blame] | 1593 | "bar-doc/a.java", |
Liz Kammer | 3a55c91 | 2020-08-14 12:14:02 -0700 | [diff] [blame] | 1594 | ], |
Steve Kim | 3666c70 | 2020-09-01 17:58:01 +0000 | [diff] [blame] | 1595 | exclude_srcs: [ |
| 1596 | "bar-doc/b.java" |
| 1597 | ], |
Liz Kammer | e1ab250 | 2020-09-10 15:29:25 +0000 | [diff] [blame] | 1598 | api_levels_annotations_dirs: [ |
| 1599 | "droiddoc-templates-sdk", |
| 1600 | ], |
| 1601 | api_levels_annotations_enabled: true, |
| 1602 | } |
| 1603 | droiddoc { |
| 1604 | name: "bar-doc", |
| 1605 | srcs: [ |
| 1606 | ":bar-stubs", |
| 1607 | "bar-doc/IFoo.aidl", |
| 1608 | ":bar-doc-aidl-srcs", |
| 1609 | ], |
Liz Kammer | 585cac2 | 2020-07-06 09:12:57 -0700 | [diff] [blame] | 1610 | custom_template: "droiddoc-templates-sdk", |
| 1611 | hdf: [ |
| 1612 | "android.whichdoc offline", |
| 1613 | ], |
| 1614 | knowntags: [ |
| 1615 | "bar-doc/known_oj_tags.txt", |
| 1616 | ], |
| 1617 | proofread_file: "libcore-proofread.txt", |
| 1618 | todo_file: "libcore-docs-todo.html", |
| 1619 | flags: ["-offlinemode -title \"libcore\""], |
| 1620 | args: "-offlinemode -title \"libcore\"", |
| 1621 | } |
| 1622 | `) |
| 1623 | } |
| 1624 | |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1625 | func TestDroidstubs(t *testing.T) { |
| 1626 | ctx, _ := testJavaWithFS(t, ` |
| 1627 | droiddoc_exported_dir { |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1628 | name: "droiddoc-templates-sdk", |
| 1629 | path: ".", |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | droidstubs { |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1633 | name: "bar-stubs", |
| 1634 | srcs: ["bar-doc/a.java"], |
| 1635 | api_levels_annotations_dirs: ["droiddoc-templates-sdk"], |
| 1636 | api_levels_annotations_enabled: true, |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | droidstubs { |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1640 | name: "bar-stubs-other", |
| 1641 | srcs: ["bar-doc/a.java"], |
| 1642 | high_mem: true, |
| 1643 | api_levels_annotations_dirs: ["droiddoc-templates-sdk"], |
| 1644 | api_levels_annotations_enabled: true, |
| 1645 | api_levels_jar_filename: "android.other.jar", |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1646 | } |
| 1647 | `, |
| 1648 | map[string][]byte{ |
| 1649 | "bar-doc/a.java": nil, |
| 1650 | }) |
| 1651 | testcases := []struct { |
| 1652 | moduleName string |
| 1653 | expectedJarFilename string |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1654 | high_mem bool |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1655 | }{ |
| 1656 | { |
| 1657 | moduleName: "bar-stubs", |
| 1658 | expectedJarFilename: "android.jar", |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1659 | high_mem: false, |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1660 | }, |
| 1661 | { |
| 1662 | moduleName: "bar-stubs-other", |
| 1663 | expectedJarFilename: "android.other.jar", |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1664 | high_mem: true, |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1665 | }, |
| 1666 | } |
| 1667 | for _, c := range testcases { |
| 1668 | m := ctx.ModuleForTests(c.moduleName, "android_common") |
| 1669 | metalava := m.Rule("metalava") |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1670 | rp := metalava.RuleParams |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1671 | expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1672 | if actual := rp.Command; !strings.Contains(actual, expected) { |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1673 | t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual) |
| 1674 | } |
Anton Hansson | 52ac73d | 2020-10-26 09:57:40 +0000 | [diff] [blame] | 1675 | |
| 1676 | if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem { |
| 1677 | t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual) |
| 1678 | } |
Liz Kammer | 3d894b7 | 2020-08-04 09:55:13 -0700 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1682 | func TestDroidstubsWithSystemModules(t *testing.T) { |
| 1683 | ctx, _ := testJava(t, ` |
| 1684 | droidstubs { |
| 1685 | name: "stubs-source-system-modules", |
| 1686 | srcs: [ |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1687 | "bar-doc/a.java", |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1688 | ], |
| 1689 | sdk_version: "none", |
| 1690 | system_modules: "source-system-modules", |
| 1691 | } |
| 1692 | |
| 1693 | java_library { |
| 1694 | name: "source-jar", |
| 1695 | srcs: [ |
| 1696 | "a.java", |
| 1697 | ], |
| 1698 | } |
| 1699 | |
| 1700 | java_system_modules { |
| 1701 | name: "source-system-modules", |
| 1702 | libs: ["source-jar"], |
| 1703 | } |
| 1704 | |
| 1705 | droidstubs { |
| 1706 | name: "stubs-prebuilt-system-modules", |
| 1707 | srcs: [ |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 1708 | "bar-doc/a.java", |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1709 | ], |
| 1710 | sdk_version: "none", |
| 1711 | system_modules: "prebuilt-system-modules", |
| 1712 | } |
| 1713 | |
| 1714 | java_import { |
| 1715 | name: "prebuilt-jar", |
| 1716 | jars: ["a.jar"], |
| 1717 | } |
| 1718 | |
| 1719 | java_system_modules_import { |
| 1720 | name: "prebuilt-system-modules", |
| 1721 | libs: ["prebuilt-jar"], |
| 1722 | } |
| 1723 | `) |
| 1724 | |
| 1725 | checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar") |
| 1726 | |
| 1727 | checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar") |
| 1728 | } |
| 1729 | |
| 1730 | func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) { |
| 1731 | metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava") |
| 1732 | var systemJars []string |
| 1733 | for _, i := range metalavaRule.Implicits { |
| 1734 | systemJars = append(systemJars, i.Base()) |
| 1735 | } |
Ramy Medhat | c7965cd | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 1736 | if len(systemJars) < 1 || systemJars[0] != systemJar { |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1737 | t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars) |
| 1738 | } |
| 1739 | } |
| 1740 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1741 | func TestJarGenrules(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1742 | ctx, _ := testJava(t, ` |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1743 | java_library { |
| 1744 | name: "foo", |
| 1745 | srcs: ["a.java"], |
| 1746 | } |
| 1747 | |
| 1748 | java_genrule { |
| 1749 | name: "jargen", |
| 1750 | tool_files: ["b.java"], |
| 1751 | cmd: "$(location b.java) $(in) $(out)", |
| 1752 | out: ["jargen.jar"], |
| 1753 | srcs: [":foo"], |
| 1754 | } |
| 1755 | |
| 1756 | java_library { |
| 1757 | name: "bar", |
| 1758 | static_libs: ["jargen"], |
| 1759 | srcs: ["c.java"], |
| 1760 | } |
| 1761 | |
| 1762 | java_library { |
| 1763 | name: "baz", |
| 1764 | libs: ["jargen"], |
| 1765 | srcs: ["c.java"], |
| 1766 | } |
| 1767 | `) |
| 1768 | |
| 1769 | foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") |
| 1770 | jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") |
| 1771 | bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") |
| 1772 | baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") |
| 1773 | barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 1774 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 1775 | if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) { |
| 1776 | t.Errorf("expected jargen inputs [%q], got %q", w, g) |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { |
| 1780 | t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) |
| 1781 | } |
| 1782 | |
| 1783 | if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { |
| 1784 | t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) |
| 1785 | } |
| 1786 | |
| 1787 | if len(barCombined.Inputs) != 2 || |
| 1788 | barCombined.Inputs[0].String() != bar.Output.String() || |
| 1789 | barCombined.Inputs[1].String() != jargen.Output.String() { |
| 1790 | t.Errorf("bar combined jar inputs %v is not [%q, %q]", |
| 1791 | barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) |
| 1792 | } |
| 1793 | } |
| 1794 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1795 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1796 | ctx, _ := testJava(t, ` |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1797 | java_library { |
| 1798 | name: "foo", |
| 1799 | srcs: ["a.java", ":foo-srcs"], |
| 1800 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1801 | } |
| 1802 | |
| 1803 | filegroup { |
| 1804 | name: "foo-srcs", |
| 1805 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1806 | } |
| 1807 | |
| 1808 | filegroup { |
| 1809 | name: "foo-excludes", |
| 1810 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1811 | } |
| 1812 | `) |
| 1813 | |
| 1814 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1815 | |
| 1816 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1817 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1818 | } |
| 1819 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1820 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1821 | func TestJavaLibrary(t *testing.T) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1822 | testJavaWithFS(t, "", map[string][]byte{ |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1823 | "libcore/Android.bp": []byte(` |
| 1824 | java_library { |
| 1825 | name: "core", |
| 1826 | sdk_version: "none", |
| 1827 | system_modules: "none", |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | filegroup { |
| 1831 | name: "core-jar", |
| 1832 | srcs: [":core{.jar}"], |
| 1833 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1834 | `), |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1835 | }) |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | func TestJavaImport(t *testing.T) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1839 | testJavaWithFS(t, "", map[string][]byte{ |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1840 | "libcore/Android.bp": []byte(` |
| 1841 | java_import { |
| 1842 | name: "core", |
| 1843 | sdk_version: "none", |
| 1844 | } |
| 1845 | |
| 1846 | filegroup { |
| 1847 | name: "core-jar", |
| 1848 | srcs: [":core{.jar}"], |
| 1849 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 1850 | `), |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1851 | }) |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1852 | } |
| 1853 | |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1854 | func TestJavaSdkLibrary(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 1855 | result := javaFixtureFactory.Extend( |
| 1856 | PrepareForTestWithJavaSdkLibraryFiles, |
| 1857 | FixtureWithPrebuiltApis(map[string][]string{ |
| 1858 | "28": {"foo"}, |
| 1859 | "29": {"foo"}, |
| 1860 | "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"}, |
| 1861 | }), |
| 1862 | ).RunTestWithBp(t, ` |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 1863 | droiddoc_exported_dir { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1864 | name: "droiddoc-templates-sdk", |
| 1865 | path: ".", |
| 1866 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1867 | java_sdk_library { |
| 1868 | name: "foo", |
| 1869 | srcs: ["a.java", "b.java"], |
| 1870 | api_packages: ["foo"], |
| 1871 | } |
| 1872 | java_sdk_library { |
| 1873 | name: "bar", |
| 1874 | srcs: ["a.java", "b.java"], |
| 1875 | api_packages: ["bar"], |
| 1876 | } |
| 1877 | java_library { |
| 1878 | name: "baz", |
| 1879 | srcs: ["c.java"], |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1880 | libs: ["foo", "bar.stubs"], |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1881 | sdk_version: "system_current", |
| 1882 | } |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1883 | java_sdk_library { |
| 1884 | name: "barney", |
| 1885 | srcs: ["c.java"], |
| 1886 | api_only: true, |
| 1887 | } |
| 1888 | java_sdk_library { |
| 1889 | name: "betty", |
| 1890 | srcs: ["c.java"], |
| 1891 | shared_library: false, |
| 1892 | } |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1893 | java_sdk_library_import { |
| 1894 | name: "quuz", |
| 1895 | public: { |
| 1896 | jars: ["c.jar"], |
| 1897 | }, |
| 1898 | } |
| 1899 | java_sdk_library_import { |
| 1900 | name: "fred", |
| 1901 | public: { |
| 1902 | jars: ["b.jar"], |
| 1903 | }, |
| 1904 | } |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1905 | java_sdk_library_import { |
| 1906 | name: "wilma", |
| 1907 | public: { |
| 1908 | jars: ["b.jar"], |
| 1909 | }, |
| 1910 | shared_library: false, |
| 1911 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1912 | java_library { |
| 1913 | name: "qux", |
| 1914 | srcs: ["c.java"], |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1915 | libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"], |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1916 | sdk_version: "system_current", |
| 1917 | } |
Paul Duffin | 726d23c | 2020-01-22 16:30:37 +0000 | [diff] [blame] | 1918 | java_library { |
| 1919 | name: "baz-test", |
| 1920 | srcs: ["c.java"], |
| 1921 | libs: ["foo"], |
| 1922 | sdk_version: "test_current", |
| 1923 | } |
Paul Duffin | a2db18f | 2020-01-22 17:11:15 +0000 | [diff] [blame] | 1924 | java_library { |
| 1925 | name: "baz-29", |
| 1926 | srcs: ["c.java"], |
| 1927 | libs: ["foo"], |
| 1928 | sdk_version: "system_29", |
| 1929 | } |
Paul Duffin | fb6ae5b | 2020-09-30 15:17:25 +0100 | [diff] [blame] | 1930 | java_library { |
| 1931 | name: "baz-module-30", |
| 1932 | srcs: ["c.java"], |
| 1933 | libs: ["foo"], |
| 1934 | sdk_version: "module_30", |
| 1935 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1936 | `) |
| 1937 | |
| 1938 | // check the existence of the internal modules |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1939 | result.ModuleForTests("foo", "android_common") |
| 1940 | result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common") |
| 1941 | result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common") |
| 1942 | result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common") |
| 1943 | result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common") |
| 1944 | result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common") |
| 1945 | result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common") |
| 1946 | result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common") |
| 1947 | result.ModuleForTests("foo.api.public.28", "") |
| 1948 | result.ModuleForTests("foo.api.system.28", "") |
| 1949 | result.ModuleForTests("foo.api.test.28", "") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1950 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1951 | bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1952 | // tests if baz is actually linked to the stubs lib |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1953 | android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1954 | // ... and not to the impl lib |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1955 | android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1956 | // test if baz is not linked to the system variant of foo |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1957 | android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar") |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1958 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1959 | bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac") |
Paul Duffin | 726d23c | 2020-01-22 16:30:37 +0000 | [diff] [blame] | 1960 | // tests if baz-test is actually linked to the test stubs lib |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1961 | android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar") |
Paul Duffin | 726d23c | 2020-01-22 16:30:37 +0000 | [diff] [blame] | 1962 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1963 | baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac") |
Paul Duffin | a2db18f | 2020-01-22 17:11:15 +0000 | [diff] [blame] | 1964 | // tests if baz-29 is actually linked to the system 29 stubs lib |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1965 | android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") |
Paul Duffin | a2db18f | 2020-01-22 17:11:15 +0000 | [diff] [blame] | 1966 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1967 | bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac") |
Paul Duffin | fb6ae5b | 2020-09-30 15:17:25 +0100 | [diff] [blame] | 1968 | // tests if "baz-module-30" is actually linked to the module 30 stubs lib |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1969 | android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") |
Paul Duffin | fb6ae5b | 2020-09-30 15:17:25 +0100 | [diff] [blame] | 1970 | |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1971 | // test if baz has exported SDK lib names foo and bar to qux |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1972 | qux := result.ModuleForTests("qux", "android_common") |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1973 | if quxLib, ok := qux.Module().(*Library); ok { |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 1974 | sdkLibs := quxLib.ClassLoaderContexts().UsesLibs() |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 1975 | android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1976 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1977 | } |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1978 | |
Anton Hansson | 7f66efa | 2020-10-08 14:47:23 +0100 | [diff] [blame] | 1979 | func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 1980 | result := javaFixtureFactory.Extend( |
| 1981 | PrepareForTestWithJavaSdkLibraryFiles, |
| 1982 | FixtureWithLastReleaseApis("sdklib"), |
| 1983 | ).RunTestWithBp(t, ` |
Anton Hansson | 7f66efa | 2020-10-08 14:47:23 +0100 | [diff] [blame] | 1984 | java_sdk_library { |
Anton Hansson | dff2c78 | 2020-12-21 17:10:01 +0000 | [diff] [blame] | 1985 | name: "sdklib", |
Anton Hansson | 7f66efa | 2020-10-08 14:47:23 +0100 | [diff] [blame] | 1986 | srcs: ["a.java"], |
| 1987 | impl_only_libs: ["foo"], |
| 1988 | stub_only_libs: ["bar"], |
| 1989 | } |
| 1990 | java_library { |
| 1991 | name: "foo", |
| 1992 | srcs: ["a.java"], |
| 1993 | sdk_version: "current", |
| 1994 | } |
| 1995 | java_library { |
| 1996 | name: "bar", |
| 1997 | srcs: ["a.java"], |
| 1998 | sdk_version: "current", |
| 1999 | } |
| 2000 | `) |
| 2001 | |
Anton Hansson | dff2c78 | 2020-12-21 17:10:01 +0000 | [diff] [blame] | 2002 | for _, implName := range []string{"sdklib", "sdklib.impl"} { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2003 | implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"] |
Anton Hansson | 7f66efa | 2020-10-08 14:47:23 +0100 | [diff] [blame] | 2004 | if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") { |
| 2005 | t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp) |
| 2006 | } |
| 2007 | } |
Anton Hansson | dff2c78 | 2020-12-21 17:10:01 +0000 | [diff] [blame] | 2008 | stubName := apiScopePublic.stubsLibraryModuleName("sdklib") |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2009 | stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"] |
Anton Hansson | 7f66efa | 2020-10-08 14:47:23 +0100 | [diff] [blame] | 2010 | if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") { |
| 2011 | t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp) |
| 2012 | } |
| 2013 | } |
| 2014 | |
Paul Duffin | daaa332 | 2020-05-26 18:13:57 +0100 | [diff] [blame] | 2015 | func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2016 | result := javaFixtureFactory.Extend( |
| 2017 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2018 | FixtureWithLastReleaseApis("foo"), |
| 2019 | ).RunTestWithBp(t, ` |
Paul Duffin | daaa332 | 2020-05-26 18:13:57 +0100 | [diff] [blame] | 2020 | java_sdk_library { |
| 2021 | name: "foo", |
| 2022 | srcs: ["a.java"], |
| 2023 | api_only: true, |
| 2024 | public: { |
| 2025 | enabled: true, |
| 2026 | }, |
| 2027 | } |
| 2028 | |
| 2029 | java_library { |
| 2030 | name: "bar", |
| 2031 | srcs: ["b.java"], |
| 2032 | libs: ["foo"], |
| 2033 | } |
| 2034 | `) |
| 2035 | |
| 2036 | // The bar library should depend on the stubs jar. |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2037 | barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac") |
Paul Duffin | daaa332 | 2020-05-26 18:13:57 +0100 | [diff] [blame] | 2038 | if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) { |
| 2039 | t.Errorf("expected %q, found %#q", expected, actual) |
| 2040 | } |
| 2041 | } |
| 2042 | |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 2043 | func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2044 | javaFixtureFactory.Extend( |
| 2045 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2046 | FixtureWithLastReleaseApis("foo"), |
| 2047 | ).RunTestWithBp(t, ` |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 2048 | java_sdk_library { |
| 2049 | name: "foo", |
| 2050 | srcs: ["a.java"], |
| 2051 | api_packages: ["foo"], |
| 2052 | public: { |
| 2053 | enabled: true, |
| 2054 | }, |
| 2055 | } |
| 2056 | |
| 2057 | java_library { |
| 2058 | name: "bar", |
| 2059 | srcs: ["b.java", ":foo{.public.stubs.source}"], |
| 2060 | } |
| 2061 | `) |
| 2062 | } |
| 2063 | |
| 2064 | func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2065 | javaFixtureFactory. |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2066 | Extend( |
| 2067 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2068 | FixtureWithLastReleaseApis("foo"), |
| 2069 | ). |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2070 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)). |
| 2071 | RunTestWithBp(t, ` |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 2072 | java_sdk_library { |
| 2073 | name: "foo", |
| 2074 | srcs: ["a.java"], |
| 2075 | api_packages: ["foo"], |
| 2076 | public: { |
| 2077 | enabled: true, |
| 2078 | }, |
| 2079 | } |
| 2080 | |
| 2081 | java_library { |
| 2082 | name: "bar", |
| 2083 | srcs: ["b.java", ":foo{.system.stubs.source}"], |
| 2084 | } |
| 2085 | `) |
| 2086 | } |
| 2087 | |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 2088 | func TestJavaSdkLibrary_Deps(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2089 | result := javaFixtureFactory.Extend( |
| 2090 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2091 | FixtureWithLastReleaseApis("sdklib"), |
| 2092 | ).RunTestWithBp(t, ` |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 2093 | java_sdk_library { |
| 2094 | name: "sdklib", |
| 2095 | srcs: ["a.java"], |
| 2096 | sdk_version: "none", |
| 2097 | system_modules: "none", |
| 2098 | public: { |
| 2099 | enabled: true, |
| 2100 | }, |
| 2101 | } |
| 2102 | `) |
| 2103 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2104 | CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{ |
Paul Duffin | ca8d9a5 | 2020-06-26 22:20:25 +0100 | [diff] [blame] | 2105 | `dex2oatd`, |
| 2106 | `sdklib.impl`, |
| 2107 | `sdklib.stubs`, |
| 2108 | `sdklib.stubs.source`, |
| 2109 | `sdklib.xml`, |
| 2110 | }) |
| 2111 | } |
| 2112 | |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 2113 | func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2114 | javaFixtureFactory.RunTestWithBp(t, ` |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 2115 | java_sdk_library_import { |
| 2116 | name: "foo", |
| 2117 | public: { |
| 2118 | jars: ["a.jar"], |
| 2119 | stub_srcs: ["a.java"], |
| 2120 | current_api: "api/current.txt", |
| 2121 | removed_api: "api/removed.txt", |
| 2122 | }, |
| 2123 | } |
| 2124 | |
| 2125 | java_library { |
| 2126 | name: "bar", |
| 2127 | srcs: [":foo{.public.stubs.source}"], |
| 2128 | java_resources: [ |
| 2129 | ":foo{.public.api.txt}", |
| 2130 | ":foo{.public.removed-api.txt}", |
| 2131 | ], |
| 2132 | } |
| 2133 | `) |
| 2134 | } |
| 2135 | |
| 2136 | func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) { |
| 2137 | bp := ` |
| 2138 | java_sdk_library_import { |
| 2139 | name: "foo", |
| 2140 | public: { |
| 2141 | jars: ["a.jar"], |
| 2142 | }, |
| 2143 | } |
| 2144 | ` |
| 2145 | |
| 2146 | t.Run("stubs.source", func(t *testing.T) { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2147 | javaFixtureFactory. |
| 2148 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)). |
| 2149 | RunTestWithBp(t, bp+` |
| 2150 | java_library { |
| 2151 | name: "bar", |
| 2152 | srcs: [":foo{.public.stubs.source}"], |
| 2153 | java_resources: [ |
| 2154 | ":foo{.public.api.txt}", |
| 2155 | ":foo{.public.removed-api.txt}", |
| 2156 | ], |
| 2157 | } |
| 2158 | `) |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 2159 | }) |
| 2160 | |
| 2161 | t.Run("api.txt", func(t *testing.T) { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2162 | javaFixtureFactory. |
| 2163 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)). |
| 2164 | RunTestWithBp(t, bp+` |
| 2165 | java_library { |
| 2166 | name: "bar", |
| 2167 | srcs: ["a.java"], |
| 2168 | java_resources: [ |
| 2169 | ":foo{.public.api.txt}", |
| 2170 | ], |
| 2171 | } |
| 2172 | `) |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 2173 | }) |
| 2174 | |
| 2175 | t.Run("removed-api.txt", func(t *testing.T) { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2176 | javaFixtureFactory. |
| 2177 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)). |
| 2178 | RunTestWithBp(t, bp+` |
| 2179 | java_library { |
| 2180 | name: "bar", |
| 2181 | srcs: ["a.java"], |
| 2182 | java_resources: [ |
| 2183 | ":foo{.public.removed-api.txt}", |
| 2184 | ], |
| 2185 | } |
| 2186 | `) |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 2187 | }) |
| 2188 | } |
| 2189 | |
Paul Duffin | 3375e35 | 2020-04-28 10:44:03 +0100 | [diff] [blame] | 2190 | func TestJavaSdkLibrary_InvalidScopes(t *testing.T) { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2191 | javaFixtureFactory. |
| 2192 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)). |
| 2193 | RunTestWithBp(t, ` |
| 2194 | java_sdk_library { |
| 2195 | name: "foo", |
| 2196 | srcs: ["a.java", "b.java"], |
| 2197 | api_packages: ["foo"], |
| 2198 | // Explicitly disable public to test the check that ensures the set of enabled |
| 2199 | // scopes is consistent. |
| 2200 | public: { |
| 2201 | enabled: false, |
| 2202 | }, |
| 2203 | system: { |
| 2204 | enabled: true, |
| 2205 | }, |
| 2206 | } |
Paul Duffin | 3375e35 | 2020-04-28 10:44:03 +0100 | [diff] [blame] | 2207 | `) |
| 2208 | } |
| 2209 | |
Paul Duffin | 87a05a3 | 2020-05-12 11:50:28 +0100 | [diff] [blame] | 2210 | func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2211 | javaFixtureFactory.Extend( |
| 2212 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2213 | FixtureWithLastReleaseApis("foo"), |
| 2214 | ).RunTestWithBp(t, ` |
Paul Duffin | 87a05a3 | 2020-05-12 11:50:28 +0100 | [diff] [blame] | 2215 | java_sdk_library { |
| 2216 | name: "foo", |
| 2217 | srcs: ["a.java", "b.java"], |
| 2218 | api_packages: ["foo"], |
| 2219 | system: { |
| 2220 | enabled: true, |
| 2221 | sdk_version: "module_current", |
| 2222 | }, |
| 2223 | } |
| 2224 | `) |
| 2225 | } |
| 2226 | |
Paul Duffin | 0c5bae5 | 2020-06-02 13:00:08 +0100 | [diff] [blame] | 2227 | func TestJavaSdkLibrary_ModuleLib(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2228 | javaFixtureFactory.Extend( |
| 2229 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2230 | FixtureWithLastReleaseApis("foo"), |
| 2231 | ).RunTestWithBp(t, ` |
Paul Duffin | 0c5bae5 | 2020-06-02 13:00:08 +0100 | [diff] [blame] | 2232 | java_sdk_library { |
| 2233 | name: "foo", |
| 2234 | srcs: ["a.java", "b.java"], |
| 2235 | api_packages: ["foo"], |
| 2236 | system: { |
| 2237 | enabled: true, |
| 2238 | }, |
| 2239 | module_lib: { |
| 2240 | enabled: true, |
| 2241 | }, |
| 2242 | } |
| 2243 | `) |
| 2244 | } |
| 2245 | |
| 2246 | func TestJavaSdkLibrary_SystemServer(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2247 | javaFixtureFactory.Extend( |
| 2248 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2249 | FixtureWithLastReleaseApis("foo"), |
| 2250 | ).RunTestWithBp(t, ` |
Paul Duffin | 0c5bae5 | 2020-06-02 13:00:08 +0100 | [diff] [blame] | 2251 | java_sdk_library { |
| 2252 | name: "foo", |
| 2253 | srcs: ["a.java", "b.java"], |
| 2254 | api_packages: ["foo"], |
| 2255 | system: { |
| 2256 | enabled: true, |
| 2257 | }, |
| 2258 | system_server: { |
| 2259 | enabled: true, |
| 2260 | }, |
| 2261 | } |
| 2262 | `) |
| 2263 | } |
| 2264 | |
Paul Duffin | 803a956 | 2020-05-20 11:52:25 +0100 | [diff] [blame] | 2265 | func TestJavaSdkLibrary_MissingScope(t *testing.T) { |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2266 | javaFixtureFactory. |
| 2267 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)). |
| 2268 | RunTestWithBp(t, ` |
| 2269 | java_sdk_library { |
| 2270 | name: "foo", |
| 2271 | srcs: ["a.java"], |
| 2272 | public: { |
| 2273 | enabled: false, |
| 2274 | }, |
| 2275 | } |
Paul Duffin | 803a956 | 2020-05-20 11:52:25 +0100 | [diff] [blame] | 2276 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2277 | java_library { |
| 2278 | name: "baz", |
| 2279 | srcs: ["a.java"], |
| 2280 | libs: ["foo"], |
| 2281 | sdk_version: "module_current", |
| 2282 | } |
Paul Duffin | 803a956 | 2020-05-20 11:52:25 +0100 | [diff] [blame] | 2283 | `) |
| 2284 | } |
| 2285 | |
| 2286 | func TestJavaSdkLibrary_FallbackScope(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2287 | javaFixtureFactory.Extend( |
| 2288 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2289 | FixtureWithLastReleaseApis("foo"), |
| 2290 | ).RunTestWithBp(t, ` |
Paul Duffin | 803a956 | 2020-05-20 11:52:25 +0100 | [diff] [blame] | 2291 | java_sdk_library { |
| 2292 | name: "foo", |
| 2293 | srcs: ["a.java"], |
| 2294 | system: { |
| 2295 | enabled: true, |
| 2296 | }, |
| 2297 | } |
| 2298 | |
| 2299 | java_library { |
| 2300 | name: "baz", |
| 2301 | srcs: ["a.java"], |
| 2302 | libs: ["foo"], |
| 2303 | // foo does not have module-lib scope so it should fallback to system |
| 2304 | sdk_version: "module_current", |
| 2305 | } |
| 2306 | `) |
| 2307 | } |
| 2308 | |
Jiyong Park | 932cdfe | 2020-05-28 00:19:53 +0900 | [diff] [blame] | 2309 | func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) { |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 2310 | result := javaFixtureFactory.Extend( |
| 2311 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2312 | FixtureWithLastReleaseApis("foo"), |
| 2313 | ).RunTestWithBp(t, ` |
Jiyong Park | 932cdfe | 2020-05-28 00:19:53 +0900 | [diff] [blame] | 2314 | java_sdk_library { |
| 2315 | name: "foo", |
| 2316 | srcs: ["a.java"], |
| 2317 | system: { |
| 2318 | enabled: true, |
| 2319 | }, |
| 2320 | default_to_stubs: true, |
| 2321 | } |
| 2322 | |
| 2323 | java_library { |
| 2324 | name: "baz", |
| 2325 | srcs: ["a.java"], |
| 2326 | libs: ["foo"], |
| 2327 | // does not have sdk_version set, should fallback to module, |
| 2328 | // which will then fallback to system because the module scope |
| 2329 | // is not enabled. |
| 2330 | } |
| 2331 | `) |
| 2332 | // The baz library should depend on the system stubs jar. |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 2333 | bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac") |
Jiyong Park | 932cdfe | 2020-05-28 00:19:53 +0900 | [diff] [blame] | 2334 | if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) { |
| 2335 | t.Errorf("expected %q, found %#q", expected, actual) |
| 2336 | } |
| 2337 | } |
| 2338 | |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 2339 | var compilerFlagsTestCases = []struct { |
| 2340 | in string |
| 2341 | out bool |
| 2342 | }{ |
| 2343 | { |
| 2344 | in: "a", |
| 2345 | out: false, |
| 2346 | }, |
| 2347 | { |
| 2348 | in: "-a", |
| 2349 | out: true, |
| 2350 | }, |
| 2351 | { |
| 2352 | in: "-no-jdk", |
| 2353 | out: false, |
| 2354 | }, |
| 2355 | { |
| 2356 | in: "-no-stdlib", |
| 2357 | out: false, |
| 2358 | }, |
| 2359 | { |
| 2360 | in: "-kotlin-home", |
| 2361 | out: false, |
| 2362 | }, |
| 2363 | { |
| 2364 | in: "-kotlin-home /some/path", |
| 2365 | out: false, |
| 2366 | }, |
| 2367 | { |
| 2368 | in: "-include-runtime", |
| 2369 | out: false, |
| 2370 | }, |
| 2371 | { |
| 2372 | in: "-Xintellij-plugin-root", |
| 2373 | out: false, |
| 2374 | }, |
| 2375 | } |
| 2376 | |
| 2377 | type mockContext struct { |
| 2378 | android.ModuleContext |
| 2379 | result bool |
| 2380 | } |
| 2381 | |
| 2382 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 2383 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 2384 | ctx.result = false |
| 2385 | } |
| 2386 | |
| 2387 | func TestCompilerFlags(t *testing.T) { |
| 2388 | for _, testCase := range compilerFlagsTestCases { |
| 2389 | ctx := &mockContext{result: true} |
| 2390 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 2391 | if ctx.result != testCase.out { |
| 2392 | t.Errorf("incorrect output:") |
| 2393 | t.Errorf(" input: %#v", testCase.in) |
| 2394 | t.Errorf(" expected: %#v", testCase.out) |
| 2395 | t.Errorf(" got: %#v", ctx.result) |
| 2396 | } |
| 2397 | } |
| 2398 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 2399 | |
| 2400 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 2401 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 2402 | variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop() |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 2403 | flags := strings.Split(variables["javacFlags"], " ") |
| 2404 | got := "" |
| 2405 | for _, flag := range flags { |
| 2406 | keyEnd := strings.Index(flag, "=") |
| 2407 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 2408 | got = flag[keyEnd+1:] |
| 2409 | break |
| 2410 | } |
| 2411 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 2412 | if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) { |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 2413 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | func TestPatchModule(t *testing.T) { |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 2418 | t.Run("Java language level 8", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 2419 | // Test with legacy javac -source 1.8 -target 1.8 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 2420 | bp := ` |
| 2421 | java_library { |
| 2422 | name: "foo", |
| 2423 | srcs: ["a.java"], |
| 2424 | java_version: "1.8", |
| 2425 | } |
| 2426 | |
| 2427 | java_library { |
| 2428 | name: "bar", |
| 2429 | srcs: ["b.java"], |
| 2430 | sdk_version: "none", |
| 2431 | system_modules: "none", |
| 2432 | patch_module: "java.base", |
| 2433 | java_version: "1.8", |
| 2434 | } |
| 2435 | |
| 2436 | java_library { |
| 2437 | name: "baz", |
| 2438 | srcs: ["c.java"], |
| 2439 | patch_module: "java.base", |
| 2440 | java_version: "1.8", |
| 2441 | } |
| 2442 | ` |
| 2443 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 2444 | |
| 2445 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 2446 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 2447 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 2448 | }) |
| 2449 | |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 2450 | t.Run("Java language level 9", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 2451 | // Test with default javac -source 9 -target 9 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 2452 | bp := ` |
| 2453 | java_library { |
| 2454 | name: "foo", |
| 2455 | srcs: ["a.java"], |
| 2456 | } |
| 2457 | |
| 2458 | java_library { |
| 2459 | name: "bar", |
| 2460 | srcs: ["b.java"], |
| 2461 | sdk_version: "none", |
| 2462 | system_modules: "none", |
| 2463 | patch_module: "java.base", |
| 2464 | } |
| 2465 | |
| 2466 | java_library { |
| 2467 | name: "baz", |
Jingwen Chen | 5136a6e | 2020-10-30 01:01:35 -0400 | [diff] [blame] | 2468 | srcs: [ |
| 2469 | "c.java", |
| 2470 | // Tests for b/150878007 |
| 2471 | "dir/d.java", |
| 2472 | "dir2/e.java", |
| 2473 | "dir2/f.java", |
| 2474 | "nested/dir/g.java" |
| 2475 | ], |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 2476 | patch_module: "java.base", |
| 2477 | } |
| 2478 | ` |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 2479 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 2480 | |
| 2481 | checkPatchModuleFlag(t, ctx, "foo", "") |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 2482 | expected := "java.base=.:out/soong" |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 2483 | checkPatchModuleFlag(t, ctx, "bar", expected) |
Jingwen Chen | 5136a6e | 2020-10-30 01:01:35 -0400 | [diff] [blame] | 2484 | expected = "java.base=" + strings.Join([]string{ |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 2485 | ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":") |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 2486 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 2487 | }) |
| 2488 | } |
Paul Duffin | a7b9f42 | 2020-01-10 17:12:18 +0000 | [diff] [blame] | 2489 | |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 2490 | func TestJavaLibraryWithSystemModules(t *testing.T) { |
| 2491 | ctx, _ := testJava(t, ` |
| 2492 | java_library { |
| 2493 | name: "lib-with-source-system-modules", |
| 2494 | srcs: [ |
| 2495 | "a.java", |
| 2496 | ], |
| 2497 | sdk_version: "none", |
| 2498 | system_modules: "source-system-modules", |
| 2499 | } |
| 2500 | |
| 2501 | java_library { |
| 2502 | name: "source-jar", |
| 2503 | srcs: [ |
| 2504 | "a.java", |
| 2505 | ], |
| 2506 | } |
| 2507 | |
| 2508 | java_system_modules { |
| 2509 | name: "source-system-modules", |
| 2510 | libs: ["source-jar"], |
| 2511 | } |
| 2512 | |
| 2513 | java_library { |
| 2514 | name: "lib-with-prebuilt-system-modules", |
| 2515 | srcs: [ |
| 2516 | "a.java", |
| 2517 | ], |
| 2518 | sdk_version: "none", |
| 2519 | system_modules: "prebuilt-system-modules", |
| 2520 | } |
| 2521 | |
| 2522 | java_import { |
| 2523 | name: "prebuilt-jar", |
| 2524 | jars: ["a.jar"], |
| 2525 | } |
| 2526 | |
| 2527 | java_system_modules_import { |
| 2528 | name: "prebuilt-system-modules", |
| 2529 | libs: ["prebuilt-jar"], |
| 2530 | } |
| 2531 | `) |
| 2532 | |
| 2533 | checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar") |
| 2534 | |
| 2535 | checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar") |
| 2536 | } |
| 2537 | |
| 2538 | func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) { |
| 2539 | javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac") |
| 2540 | bootClasspath := javacRule.Args["bootClasspath"] |
| 2541 | if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) { |
| 2542 | t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath) |
| 2543 | } |
| 2544 | } |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 2545 | |
| 2546 | func TestAidlExportIncludeDirsFromImports(t *testing.T) { |
| 2547 | ctx, _ := testJava(t, ` |
| 2548 | java_library { |
| 2549 | name: "foo", |
| 2550 | srcs: ["aidl/foo/IFoo.aidl"], |
| 2551 | libs: ["bar"], |
| 2552 | } |
| 2553 | |
| 2554 | java_import { |
| 2555 | name: "bar", |
| 2556 | jars: ["a.jar"], |
| 2557 | aidl: { |
| 2558 | export_include_dirs: ["aidl/bar"], |
| 2559 | }, |
| 2560 | } |
| 2561 | `) |
| 2562 | |
| 2563 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 2564 | expectedAidlFlag := "-Iaidl/bar" |
| 2565 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 2566 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 2567 | } |
| 2568 | } |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 2569 | |
Jooyung Han | e197d8b | 2021-01-05 10:33:16 +0900 | [diff] [blame] | 2570 | func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) { |
| 2571 | ctx, _ := testJava(t, ` |
| 2572 | java_library { |
| 2573 | name: "foo", |
| 2574 | srcs: ["aidl/foo/IFoo.aidl"], |
| 2575 | aidl: { flags: ["-Werror"], }, |
| 2576 | } |
| 2577 | `) |
| 2578 | |
| 2579 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 2580 | expectedAidlFlag := "-Werror" |
| 2581 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 2582 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 2583 | } |
| 2584 | } |
| 2585 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 2586 | func TestDataNativeBinaries(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 2587 | ctx, _ := testJava(t, ` |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 2588 | java_test_host { |
| 2589 | name: "foo", |
| 2590 | srcs: ["a.java"], |
| 2591 | data_native_bins: ["bin"] |
| 2592 | } |
| 2593 | |
| 2594 | python_binary_host { |
| 2595 | name: "bin", |
| 2596 | srcs: ["bin.py"], |
| 2597 | } |
| 2598 | `) |
| 2599 | |
| 2600 | buildOS := android.BuildOs.String() |
| 2601 | |
| 2602 | test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 2603 | entries := android.AndroidMkEntriesForTest(t, ctx, test)[0] |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 2604 | expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"} |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 2605 | actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"] |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame^] | 2606 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 2607 | } |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 2608 | |
| 2609 | func TestDefaultInstallable(t *testing.T) { |
| 2610 | ctx, _ := testJava(t, ` |
| 2611 | java_test_host { |
| 2612 | name: "foo" |
| 2613 | } |
| 2614 | `) |
| 2615 | |
| 2616 | buildOS := android.BuildOs.String() |
| 2617 | module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost) |
| 2618 | assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true), |
| 2619 | module.properties.Installable) |
| 2620 | } |