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