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