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