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