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