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