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