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 ( |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "path/filepath" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 21 | "reflect" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 22 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | "testing" |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 25 | |
Jeongik Cha | 28df257 | 2019-11-11 10:46:36 +0900 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
| 27 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 28 | "android/soong/android" |
| 29 | "android/soong/cc" |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 30 | "android/soong/dexpreopt" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 31 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var buildDir string |
| 35 | |
| 36 | func setUp() { |
| 37 | var err error |
| 38 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 39 | if err != nil { |
| 40 | panic(err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func tearDown() { |
| 45 | os.RemoveAll(buildDir) |
| 46 | } |
| 47 | |
| 48 | func TestMain(m *testing.M) { |
| 49 | run := func() int { |
| 50 | setUp() |
| 51 | defer tearDown() |
| 52 | |
| 53 | return m.Run() |
| 54 | } |
| 55 | |
| 56 | os.Exit(run()) |
| 57 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 58 | |
| 59 | func testConfig(env map[string]string) android.Config { |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 60 | return TestConfig(buildDir, env) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 63 | func testContext(bp string, fs map[string][]byte) *android.TestContext { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 64 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 65 | ctx := android.NewTestArchContext() |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 66 | ctx.RegisterModuleType("android_app", AndroidAppFactory) |
| 67 | ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) |
| 68 | ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) |
| 69 | ctx.RegisterModuleType("android_library", AndroidLibraryFactory) |
| 70 | ctx.RegisterModuleType("android_test", AndroidTestFactory) |
| 71 | ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) |
| 72 | ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) |
| 73 | ctx.RegisterModuleType("java_binary", BinaryFactory) |
| 74 | ctx.RegisterModuleType("java_binary_host", BinaryHostFactory) |
| 75 | ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory) |
| 76 | ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory) |
| 77 | ctx.RegisterModuleType("java_library", LibraryFactory) |
| 78 | ctx.RegisterModuleType("java_library_host", LibraryHostFactory) |
| 79 | ctx.RegisterModuleType("java_test", TestFactory) |
| 80 | ctx.RegisterModuleType("java_import", ImportFactory) |
| 81 | ctx.RegisterModuleType("java_import_host", ImportFactoryHost) |
| 82 | ctx.RegisterModuleType("java_defaults", defaultsFactory) |
| 83 | ctx.RegisterModuleType("java_system_modules", SystemModulesFactory) |
| 84 | ctx.RegisterModuleType("java_genrule", genRuleFactory) |
| 85 | ctx.RegisterModuleType("java_plugin", PluginFactory) |
| 86 | ctx.RegisterModuleType("dex_import", DexImportFactory) |
| 87 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
| 88 | ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) |
| 89 | ctx.RegisterModuleType("droiddoc", DroiddocFactory) |
| 90 | ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory) |
| 91 | ctx.RegisterModuleType("droiddoc_template", ExportedDroiddocDirFactory) |
| 92 | ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory) |
| 93 | ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory) |
| 94 | ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory) |
| 95 | ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) |
| 96 | ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) |
| 97 | ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 98 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 99 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 100 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 101 | ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) { |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 102 | ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel() |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 103 | }) |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 104 | ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 105 | ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory)) |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 106 | ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 107 | |
| 108 | // Register module types and mutators from cc needed for JNI testing |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 109 | ctx.RegisterModuleType("cc_library", cc.LibraryFactory) |
| 110 | ctx.RegisterModuleType("cc_object", cc.ObjectFactory) |
| 111 | ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) |
| 112 | ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory) |
| 113 | ctx.RegisterModuleType("ndk_prebuilt_shared_stl", cc.NdkPrebuiltSharedStlFactory) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 114 | ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 115 | ctx.BottomUp("link", cc.LinkageMutator).Parallel() |
| 116 | ctx.BottomUp("begin", cc.BeginMutator).Parallel() |
| 117 | }) |
| 118 | |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 119 | bp += GatherRequiredDepsForTest() |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 120 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 121 | mockFS := map[string][]byte{ |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 122 | "Android.bp": []byte(bp), |
| 123 | "a.java": nil, |
| 124 | "b.java": nil, |
| 125 | "c.java": nil, |
| 126 | "b.kt": nil, |
| 127 | "a.jar": nil, |
| 128 | "b.jar": nil, |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 129 | "APP_NOTICE": nil, |
| 130 | "GENRULE_NOTICE": nil, |
| 131 | "LIB_NOTICE": nil, |
| 132 | "TOOL_NOTICE": nil, |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 133 | "java-res/a/a": nil, |
| 134 | "java-res/b/b": nil, |
| 135 | "java-res2/a": nil, |
| 136 | "java-fg/a.java": nil, |
| 137 | "java-fg/b.java": nil, |
| 138 | "java-fg/c.java": nil, |
| 139 | "api/current.txt": nil, |
| 140 | "api/removed.txt": nil, |
| 141 | "api/system-current.txt": nil, |
| 142 | "api/system-removed.txt": nil, |
| 143 | "api/test-current.txt": nil, |
| 144 | "api/test-removed.txt": nil, |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 145 | "framework/aidl/a.aidl": nil, |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 146 | |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 147 | "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so": nil, |
| 148 | |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 149 | "prebuilts/sdk/14/public/android.jar": nil, |
| 150 | "prebuilts/sdk/14/public/framework.aidl": nil, |
| 151 | "prebuilts/sdk/14/system/android.jar": nil, |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 152 | "prebuilts/sdk/17/public/android.jar": nil, |
| 153 | "prebuilts/sdk/17/public/framework.aidl": nil, |
| 154 | "prebuilts/sdk/17/system/android.jar": nil, |
Colin Cross | 74362a4 | 2019-10-28 08:05:02 -0700 | [diff] [blame] | 155 | "prebuilts/sdk/29/public/android.jar": nil, |
| 156 | "prebuilts/sdk/29/public/framework.aidl": nil, |
| 157 | "prebuilts/sdk/29/system/android.jar": nil, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 158 | "prebuilts/sdk/current/core/android.jar": nil, |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 159 | "prebuilts/sdk/current/public/android.jar": nil, |
| 160 | "prebuilts/sdk/current/public/framework.aidl": nil, |
| 161 | "prebuilts/sdk/current/public/core.jar": nil, |
| 162 | "prebuilts/sdk/current/system/android.jar": nil, |
| 163 | "prebuilts/sdk/current/test/android.jar": nil, |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 164 | "prebuilts/sdk/28/public/api/foo.txt": nil, |
| 165 | "prebuilts/sdk/28/system/api/foo.txt": nil, |
| 166 | "prebuilts/sdk/28/test/api/foo.txt": nil, |
| 167 | "prebuilts/sdk/28/public/api/foo-removed.txt": nil, |
| 168 | "prebuilts/sdk/28/system/api/foo-removed.txt": nil, |
| 169 | "prebuilts/sdk/28/test/api/foo-removed.txt": nil, |
| 170 | "prebuilts/sdk/28/public/api/bar.txt": nil, |
| 171 | "prebuilts/sdk/28/system/api/bar.txt": nil, |
| 172 | "prebuilts/sdk/28/test/api/bar.txt": nil, |
| 173 | "prebuilts/sdk/28/public/api/bar-removed.txt": nil, |
| 174 | "prebuilts/sdk/28/system/api/bar-removed.txt": nil, |
| 175 | "prebuilts/sdk/28/test/api/bar-removed.txt": nil, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 176 | "prebuilts/sdk/tools/core-lambda-stubs.jar": nil, |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 177 | "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`), |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 178 | |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 179 | "prebuilts/apk/app.apk": nil, |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 180 | "prebuilts/apk/app_arm.apk": nil, |
| 181 | "prebuilts/apk/app_arm64.apk": nil, |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 182 | "prebuilts/apk/app_xhdpi.apk": nil, |
| 183 | "prebuilts/apk/app_xxhdpi.apk": nil, |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 184 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 185 | // For framework-res, which is an implicit dependency for framework |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 186 | "AndroidManifest.xml": nil, |
| 187 | "build/make/target/product/security/testkey": nil, |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 188 | |
| 189 | "build/soong/scripts/jar-wrapper.sh": nil, |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 190 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 191 | "build/make/core/verify_uses_libraries.sh": nil, |
| 192 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 193 | "build/make/core/proguard.flags": nil, |
| 194 | "build/make/core/proguard_basic_keeps.flags": nil, |
| 195 | |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 196 | "jdk8/jre/lib/jce.jar": nil, |
| 197 | "jdk8/jre/lib/rt.jar": nil, |
Nan Zhang | 61819ce | 2018-05-04 18:49:16 -0700 | [diff] [blame] | 198 | "jdk8/lib/tools.jar": nil, |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 199 | |
| 200 | "bar-doc/a.java": nil, |
| 201 | "bar-doc/b.java": nil, |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 202 | "bar-doc/IFoo.aidl": nil, |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 203 | "bar-doc/IBar.aidl": nil, |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 204 | "bar-doc/known_oj_tags.txt": nil, |
| 205 | "external/doclava/templates-sdk": nil, |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 206 | |
| 207 | "cert/new_cert.x509.pem": nil, |
| 208 | "cert/new_cert.pk8": nil, |
Jaewoong Jung | b28eb5f | 2019-08-27 15:01:50 -0700 | [diff] [blame] | 209 | |
| 210 | "testdata/data": nil, |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 211 | |
| 212 | "stubs-sources/foo/Foo.java": nil, |
| 213 | "stubs/sources/foo/Foo.java": nil, |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | for k, v := range fs { |
| 217 | mockFS[k] = v |
| 218 | } |
| 219 | |
| 220 | ctx.MockFileSystem(mockFS) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 221 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 222 | return ctx |
| 223 | } |
| 224 | |
| 225 | func run(t *testing.T, ctx *android.TestContext, config android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 226 | t.Helper() |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 227 | |
| 228 | pathCtx := android.PathContextForTesting(config, nil) |
| 229 | setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
| 230 | |
| 231 | ctx.Register() |
Paul Duffin | baccf7e | 2019-06-11 11:58:30 +0100 | [diff] [blame] | 232 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 233 | android.FailIfErrored(t, errs) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 234 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 235 | android.FailIfErrored(t, errs) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 238 | 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] | 239 | t.Helper() |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 240 | return testJavaErrorWithConfig(t, pattern, bp, testConfig(nil)) |
| 241 | } |
| 242 | |
| 243 | func testJavaErrorWithConfig(t *testing.T, pattern string, bp string, config android.Config) (*android.TestContext, android.Config) { |
| 244 | t.Helper() |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 245 | ctx := testContext(bp, nil) |
| 246 | |
| 247 | pathCtx := android.PathContextForTesting(config, nil) |
| 248 | setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
| 249 | |
| 250 | ctx.Register() |
| 251 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 252 | if len(errs) > 0 { |
| 253 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 254 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 255 | } |
| 256 | _, errs = ctx.PrepareBuildActions(config) |
| 257 | if len(errs) > 0 { |
| 258 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 259 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 263 | |
| 264 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 265 | } |
| 266 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 267 | func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 268 | t.Helper() |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 269 | return testJavaWithConfig(t, bp, testConfig(nil)) |
| 270 | } |
| 271 | |
| 272 | func testJavaWithConfig(t *testing.T, bp string, config android.Config) (*android.TestContext, android.Config) { |
| 273 | t.Helper() |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 274 | ctx := testContext(bp, nil) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 275 | run(t, ctx, config) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 276 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 277 | return ctx, config |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 280 | func moduleToPath(name string) string { |
| 281 | switch { |
| 282 | case name == `""`: |
| 283 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 284 | case strings.HasSuffix(name, ".jar"): |
| 285 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 286 | default: |
| 287 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame^] | 291 | func TestJavaLinkType(t *testing.T) { |
| 292 | testJava(t, ` |
| 293 | java_library { |
| 294 | name: "foo", |
| 295 | srcs: ["a.java"], |
| 296 | libs: ["bar"], |
| 297 | static_libs: ["baz"], |
| 298 | } |
| 299 | |
| 300 | java_library { |
| 301 | name: "bar", |
| 302 | sdk_version: "current", |
| 303 | srcs: ["b.java"], |
| 304 | } |
| 305 | |
| 306 | java_library { |
| 307 | name: "baz", |
| 308 | sdk_version: "system_current", |
| 309 | srcs: ["c.java"], |
| 310 | } |
| 311 | `) |
| 312 | |
| 313 | testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", ` |
| 314 | java_library { |
| 315 | name: "foo", |
| 316 | srcs: ["a.java"], |
| 317 | libs: ["bar"], |
| 318 | sdk_version: "current", |
| 319 | static_libs: ["baz"], |
| 320 | } |
| 321 | |
| 322 | java_library { |
| 323 | name: "bar", |
| 324 | sdk_version: "current", |
| 325 | srcs: ["b.java"], |
| 326 | } |
| 327 | |
| 328 | java_library { |
| 329 | name: "baz", |
| 330 | sdk_version: "system_current", |
| 331 | srcs: ["c.java"], |
| 332 | } |
| 333 | `) |
| 334 | |
| 335 | testJava(t, ` |
| 336 | java_library { |
| 337 | name: "foo", |
| 338 | srcs: ["a.java"], |
| 339 | libs: ["bar"], |
| 340 | sdk_version: "system_current", |
| 341 | static_libs: ["baz"], |
| 342 | } |
| 343 | |
| 344 | java_library { |
| 345 | name: "bar", |
| 346 | sdk_version: "current", |
| 347 | srcs: ["b.java"], |
| 348 | } |
| 349 | |
| 350 | java_library { |
| 351 | name: "baz", |
| 352 | sdk_version: "system_current", |
| 353 | srcs: ["c.java"], |
| 354 | } |
| 355 | `) |
| 356 | |
| 357 | testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", ` |
| 358 | java_library { |
| 359 | name: "foo", |
| 360 | srcs: ["a.java"], |
| 361 | libs: ["bar"], |
| 362 | sdk_version: "system_current", |
| 363 | static_libs: ["baz"], |
| 364 | } |
| 365 | |
| 366 | java_library { |
| 367 | name: "bar", |
| 368 | sdk_version: "current", |
| 369 | srcs: ["b.java"], |
| 370 | } |
| 371 | |
| 372 | java_library { |
| 373 | name: "baz", |
| 374 | srcs: ["c.java"], |
| 375 | } |
| 376 | `) |
| 377 | } |
| 378 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 379 | func TestSimple(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 380 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 381 | java_library { |
| 382 | name: "foo", |
| 383 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 384 | libs: ["bar"], |
| 385 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | java_library { |
| 389 | name: "bar", |
| 390 | srcs: ["b.java"], |
| 391 | } |
| 392 | |
| 393 | java_library { |
| 394 | name: "baz", |
| 395 | srcs: ["c.java"], |
| 396 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 397 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 398 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 399 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 400 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 401 | |
| 402 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 403 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 404 | } |
| 405 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 406 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 407 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 408 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 409 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 410 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 411 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 414 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 415 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 419 | 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] | 420 | } |
| 421 | } |
| 422 | |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 423 | func TestExportedPlugins(t *testing.T) { |
| 424 | type Result struct { |
| 425 | library string |
| 426 | processors string |
| 427 | } |
| 428 | var tests = []struct { |
| 429 | name string |
| 430 | extra string |
| 431 | results []Result |
| 432 | }{ |
| 433 | { |
| 434 | name: "Exported plugin is not a direct plugin", |
| 435 | extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`, |
| 436 | results: []Result{{library: "exports", processors: "-proc:none"}}, |
| 437 | }, |
| 438 | { |
| 439 | name: "Exports plugin to dependee", |
| 440 | extra: ` |
| 441 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 442 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 443 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 444 | `, |
| 445 | results: []Result{ |
| 446 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 447 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 448 | }, |
| 449 | }, |
| 450 | { |
| 451 | name: "Exports plugin to android_library", |
| 452 | extra: ` |
| 453 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 454 | android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 455 | android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 456 | `, |
| 457 | results: []Result{ |
| 458 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 459 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 460 | }, |
| 461 | }, |
| 462 | { |
| 463 | name: "Exports plugin is not propagated via transitive deps", |
| 464 | extra: ` |
| 465 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 466 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 467 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]} |
| 468 | `, |
| 469 | results: []Result{ |
| 470 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 471 | {library: "bar", processors: "-proc:none"}, |
| 472 | }, |
| 473 | }, |
| 474 | { |
| 475 | name: "Exports plugin appends to plugins", |
| 476 | extra: ` |
| 477 | java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"} |
| 478 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 479 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]} |
| 480 | `, |
| 481 | results: []Result{ |
| 482 | {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"}, |
| 483 | }, |
| 484 | }, |
| 485 | } |
| 486 | |
| 487 | for _, test := range tests { |
| 488 | t.Run(test.name, func(t *testing.T) { |
| 489 | ctx, _ := testJava(t, ` |
| 490 | java_plugin { |
| 491 | name: "plugin", |
| 492 | processor_class: "com.android.TestPlugin", |
| 493 | } |
| 494 | `+test.extra) |
| 495 | |
| 496 | for _, want := range test.results { |
| 497 | javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac") |
| 498 | if javac.Args["processor"] != want.processors { |
| 499 | t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"]) |
| 500 | } |
| 501 | } |
| 502 | }) |
| 503 | } |
| 504 | } |
| 505 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 506 | func TestSdkVersionByPartition(t *testing.T) { |
| 507 | 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] | 508 | java_library { |
| 509 | name: "foo", |
| 510 | srcs: ["a.java"], |
| 511 | vendor: true, |
| 512 | } |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 513 | `) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 514 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 515 | testJava(t, ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 516 | java_library { |
| 517 | name: "bar", |
| 518 | srcs: ["b.java"], |
| 519 | } |
| 520 | `) |
| 521 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 522 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 523 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 524 | config := testConfig(nil) |
| 525 | config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
| 526 | bp := ` |
| 527 | java_library { |
| 528 | name: "foo", |
| 529 | srcs: ["a.java"], |
| 530 | product_specific: true, |
| 531 | } |
| 532 | ` |
| 533 | if enforce { |
| 534 | testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", bp, config) |
| 535 | } else { |
| 536 | testJavaWithConfig(t, bp, config) |
| 537 | } |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 541 | func TestArchSpecific(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 542 | ctx, _ := testJava(t, ` |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 543 | java_library { |
| 544 | name: "foo", |
| 545 | srcs: ["a.java"], |
| 546 | target: { |
| 547 | android: { |
| 548 | srcs: ["b.java"], |
| 549 | }, |
| 550 | }, |
| 551 | } |
| 552 | `) |
| 553 | |
| 554 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 555 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 556 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 557 | } |
| 558 | } |
| 559 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 560 | func TestBinary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 561 | ctx, _ := testJava(t, ` |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 562 | java_library_host { |
| 563 | name: "foo", |
| 564 | srcs: ["a.java"], |
| 565 | } |
| 566 | |
| 567 | java_binary_host { |
| 568 | name: "bar", |
| 569 | srcs: ["b.java"], |
| 570 | static_libs: ["foo"], |
| 571 | } |
| 572 | `) |
| 573 | |
| 574 | buildOS := android.BuildOs.String() |
| 575 | |
| 576 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 577 | barJar := bar.Output("bar.jar").Output.String() |
| 578 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 579 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 580 | |
| 581 | // Test that the install binary wrapper depends on the installed jar file |
| 582 | if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar { |
| 583 | t.Errorf("expected binary wrapper implicits [%q], got %v", |
| 584 | barJar, barWrapperDeps) |
| 585 | } |
| 586 | |
| 587 | } |
| 588 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 589 | func TestPrebuilts(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 590 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 591 | java_library { |
| 592 | name: "foo", |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 593 | srcs: ["a.java", ":stubs-source"], |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 594 | libs: ["bar", "sdklib"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 595 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 598 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 599 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 600 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 603 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 604 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 605 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 606 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 607 | |
| 608 | dex_import { |
| 609 | name: "qux", |
| 610 | jars: ["b.jar"], |
| 611 | } |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 612 | |
| 613 | java_sdk_library_import { |
| 614 | name: "sdklib", |
| 615 | jars: ["b.jar"], |
| 616 | } |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 617 | |
| 618 | prebuilt_stubs_sources { |
| 619 | name: "stubs-source", |
| 620 | srcs: ["stubs/sources/**/*.java"], |
| 621 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 622 | `) |
| 623 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 624 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 625 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 626 | barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output |
| 627 | bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 628 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 629 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 630 | inputs := []string{} |
| 631 | for _, p := range javac.BuildParams.Inputs { |
| 632 | inputs = append(inputs, p.String()) |
| 633 | } |
| 634 | |
| 635 | expected := []string{ |
| 636 | "a.java", |
| 637 | "stubs/sources/foo/Foo.java", |
| 638 | } |
| 639 | if !reflect.DeepEqual(expected, inputs) { |
| 640 | t.Errorf("foo inputs incorrect: expected %q, found %q", expected, inputs) |
| 641 | } |
| 642 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 643 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 644 | 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] | 645 | } |
| 646 | |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 647 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 648 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 649 | } |
| 650 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 651 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 652 | 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] | 653 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 654 | |
| 655 | ctx.ModuleForTests("qux", "android_common").Rule("Cp") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 658 | func TestDefaults(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 659 | ctx, _ := testJava(t, ` |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 660 | java_defaults { |
| 661 | name: "defaults", |
| 662 | srcs: ["a.java"], |
| 663 | libs: ["bar"], |
| 664 | static_libs: ["baz"], |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 665 | optimize: {enabled: false}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | java_library { |
| 669 | name: "foo", |
| 670 | defaults: ["defaults"], |
| 671 | } |
| 672 | |
| 673 | java_library { |
| 674 | name: "bar", |
| 675 | srcs: ["b.java"], |
| 676 | } |
| 677 | |
| 678 | java_library { |
| 679 | name: "baz", |
| 680 | srcs: ["c.java"], |
| 681 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 682 | |
| 683 | android_test { |
| 684 | name: "atestOptimize", |
| 685 | defaults: ["defaults"], |
| 686 | optimize: {enabled: true}, |
| 687 | } |
| 688 | |
| 689 | android_test { |
| 690 | name: "atestNoOptimize", |
| 691 | defaults: ["defaults"], |
| 692 | } |
| 693 | |
| 694 | android_test { |
| 695 | name: "atestDefault", |
| 696 | srcs: ["a.java"], |
| 697 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 698 | `) |
| 699 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 700 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 701 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 702 | |
| 703 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 704 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 705 | } |
| 706 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 707 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 708 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 709 | 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] | 710 | } |
| 711 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 712 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 713 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 714 | 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] | 715 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 716 | |
| 717 | atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8") |
| 718 | if atestOptimize.Output == nil { |
| 719 | t.Errorf("atestOptimize should optimize APK") |
| 720 | } |
| 721 | |
| 722 | atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8") |
| 723 | if atestNoOptimize.Output == nil { |
| 724 | t.Errorf("atestNoOptimize should not optimize APK") |
| 725 | } |
| 726 | |
| 727 | atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8") |
| 728 | if atestDefault.Output == nil { |
| 729 | t.Errorf("atestDefault should optimize APK") |
| 730 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 733 | func TestResources(t *testing.T) { |
| 734 | var table = []struct { |
| 735 | name string |
| 736 | prop string |
| 737 | extra string |
| 738 | args string |
| 739 | }{ |
| 740 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 741 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 742 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 743 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 744 | 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] | 745 | }, |
| 746 | { |
| 747 | // Test that a module with java_resources includes the files |
| 748 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 749 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 750 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 751 | }, |
| 752 | { |
| 753 | // Test that a module with a filegroup in java_resources includes the files with the |
| 754 | // path prefix |
| 755 | name: "resource filegroup", |
| 756 | prop: `java_resources: [":foo-res"]`, |
| 757 | extra: ` |
| 758 | filegroup { |
| 759 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 760 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 761 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 762 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 763 | 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] | 764 | }, |
| 765 | { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 766 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 767 | name: "wildcard dirs", |
| 768 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 769 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 770 | }, |
| 771 | { |
| 772 | // Test that a module exclude_java_resource_dirs excludes the files |
| 773 | name: "wildcard dirs", |
| 774 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 775 | args: "-C java-res/a -f java-res/a/a", |
| 776 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 777 | { |
| 778 | // Test wildcards in java_resources |
| 779 | name: "wildcard files", |
| 780 | prop: `java_resources: ["java-res/**/*"]`, |
| 781 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 782 | }, |
| 783 | { |
| 784 | // Test exclude_java_resources with java_resources |
| 785 | name: "wildcard files with exclude", |
| 786 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 787 | args: "-C . -f java-res/a/a", |
| 788 | }, |
| 789 | { |
| 790 | // Test exclude_java_resources with java_resource_dirs |
| 791 | name: "resource dirs with exclude files", |
| 792 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 793 | args: "-C java-res -f java-res/a/a", |
| 794 | }, |
| 795 | { |
| 796 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 797 | name: "resource dirs with exclude files", |
| 798 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 799 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 800 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | for _, test := range table { |
| 804 | t.Run(test.name, func(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 805 | ctx, _ := testJava(t, ` |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 806 | java_library { |
| 807 | name: "foo", |
| 808 | srcs: [ |
| 809 | "a.java", |
| 810 | "b.java", |
| 811 | "c.java", |
| 812 | ], |
| 813 | `+test.prop+`, |
| 814 | } |
| 815 | `+test.extra) |
| 816 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 817 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 818 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 819 | |
| 820 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 821 | t.Errorf("foo combined jars %v does not contain %q", |
| 822 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 823 | } |
| 824 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 825 | if fooRes.Args["jarArgs"] != test.args { |
| 826 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 827 | fooRes.Args["jarArgs"], test.args) |
| 828 | } |
| 829 | }) |
| 830 | } |
| 831 | } |
| 832 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 833 | func TestIncludeSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 834 | ctx, _ := testJava(t, ` |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 835 | java_library { |
| 836 | name: "foo", |
| 837 | srcs: [ |
| 838 | "a.java", |
| 839 | "b.java", |
| 840 | "c.java", |
| 841 | ], |
| 842 | include_srcs: true, |
| 843 | } |
| 844 | |
| 845 | java_library { |
| 846 | name: "bar", |
| 847 | srcs: [ |
| 848 | "a.java", |
| 849 | "b.java", |
| 850 | "c.java", |
| 851 | ], |
| 852 | java_resource_dirs: ["java-res"], |
| 853 | include_srcs: true, |
| 854 | } |
| 855 | `) |
| 856 | |
| 857 | // Test a library with include_srcs: true |
| 858 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
| 859 | fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar") |
| 860 | |
| 861 | if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) { |
| 862 | t.Errorf("foo combined jars %v does not contain %q", w, g) |
| 863 | } |
| 864 | |
| 865 | if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 866 | t.Errorf("foo source jar args %q is not %q", w, g) |
| 867 | } |
| 868 | |
| 869 | // Test a library with include_srcs: true and resources |
| 870 | bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar") |
| 871 | barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar") |
| 872 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
| 873 | barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar") |
| 874 | |
| 875 | if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 876 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 877 | } |
| 878 | |
| 879 | if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 880 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 881 | } |
| 882 | |
| 883 | if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) { |
| 884 | t.Errorf("bar combined jars %v does not contain %q", w, g) |
| 885 | } |
| 886 | |
| 887 | if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 888 | t.Errorf("bar source jar args %q is not %q", w, g) |
| 889 | } |
| 890 | |
| 891 | if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w { |
| 892 | t.Errorf("bar resource jar args %q is not %q", w, g) |
| 893 | } |
| 894 | } |
| 895 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 896 | func TestGeneratedSources(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 897 | ctx, _ := testJava(t, ` |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 898 | java_library { |
| 899 | name: "foo", |
| 900 | srcs: [ |
| 901 | "a*.java", |
| 902 | ":gen", |
| 903 | "b*.java", |
| 904 | ], |
| 905 | } |
| 906 | |
| 907 | genrule { |
| 908 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 909 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 910 | out: ["gen.java"], |
| 911 | } |
| 912 | `) |
| 913 | |
| 914 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 915 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 916 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 917 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 918 | 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] | 919 | } |
| 920 | |
| 921 | if len(javac.Inputs) != 3 || |
| 922 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 923 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 924 | javac.Inputs[2].String() != "b.java" { |
| 925 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 926 | } |
| 927 | } |
| 928 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 929 | func TestTurbine(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 930 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 931 | java_library { |
| 932 | name: "foo", |
| 933 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 934 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | java_library { |
| 938 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 939 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 940 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 941 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | java_library { |
| 945 | name: "baz", |
| 946 | srcs: ["c.java"], |
| 947 | libs: ["bar"], |
| 948 | sdk_version: "14", |
| 949 | } |
| 950 | `) |
| 951 | |
| 952 | fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine") |
| 953 | barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine") |
| 954 | barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 955 | barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine") |
| 956 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 957 | |
| 958 | if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" { |
| 959 | t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs) |
| 960 | } |
| 961 | |
| 962 | fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 963 | if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) { |
| 964 | t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar) |
| 965 | } |
| 966 | if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) { |
| 967 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar) |
| 968 | } |
| 969 | if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar { |
| 970 | t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar) |
| 971 | } |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 972 | if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") { |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 973 | 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] | 974 | "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
| 978 | func TestSharding(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 979 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 980 | java_library { |
| 981 | name: "bar", |
| 982 | srcs: ["a.java","b.java","c.java"], |
| 983 | javac_shard_size: 1 |
| 984 | } |
| 985 | `) |
| 986 | |
| 987 | barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 988 | for i := 0; i < 3; i++ { |
| 989 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
| 990 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 991 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 996 | func TestDroiddoc(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 997 | ctx, _ := testJava(t, ` |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 998 | droiddoc_template { |
| 999 | name: "droiddoc-templates-sdk", |
| 1000 | path: ".", |
| 1001 | } |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 1002 | filegroup { |
| 1003 | name: "bar-doc-aidl-srcs", |
| 1004 | srcs: ["bar-doc/IBar.aidl"], |
| 1005 | path: "bar-doc", |
| 1006 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1007 | droiddoc { |
| 1008 | name: "bar-doc", |
| 1009 | srcs: [ |
| 1010 | "bar-doc/*.java", |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 1011 | "bar-doc/IFoo.aidl", |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 1012 | ":bar-doc-aidl-srcs", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1013 | ], |
| 1014 | exclude_srcs: [ |
| 1015 | "bar-doc/b.java" |
| 1016 | ], |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 1017 | custom_template: "droiddoc-templates-sdk", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1018 | hdf: [ |
| 1019 | "android.whichdoc offline", |
| 1020 | ], |
| 1021 | knowntags: [ |
| 1022 | "bar-doc/known_oj_tags.txt", |
| 1023 | ], |
| 1024 | proofread_file: "libcore-proofread.txt", |
| 1025 | todo_file: "libcore-docs-todo.html", |
| 1026 | args: "-offlinemode -title \"libcore\"", |
| 1027 | } |
| 1028 | `) |
| 1029 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 1030 | barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc") |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 1031 | var javaSrcs []string |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 1032 | for _, i := range barDoc.Inputs { |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 1033 | javaSrcs = append(javaSrcs, i.Base()) |
| 1034 | } |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 1035 | if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" { |
| 1036 | t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs) |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 1037 | } |
| 1038 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 1039 | aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl") |
| 1040 | if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) { |
| 1041 | t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g) |
| 1042 | } |
| 1043 | |
| 1044 | if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) { |
| 1045 | t.Errorf("aidl inputs must be %q, but was %q", w, g) |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 1046 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1049 | func TestJarGenrules(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1050 | ctx, _ := testJava(t, ` |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1051 | java_library { |
| 1052 | name: "foo", |
| 1053 | srcs: ["a.java"], |
| 1054 | } |
| 1055 | |
| 1056 | java_genrule { |
| 1057 | name: "jargen", |
| 1058 | tool_files: ["b.java"], |
| 1059 | cmd: "$(location b.java) $(in) $(out)", |
| 1060 | out: ["jargen.jar"], |
| 1061 | srcs: [":foo"], |
| 1062 | } |
| 1063 | |
| 1064 | java_library { |
| 1065 | name: "bar", |
| 1066 | static_libs: ["jargen"], |
| 1067 | srcs: ["c.java"], |
| 1068 | } |
| 1069 | |
| 1070 | java_library { |
| 1071 | name: "baz", |
| 1072 | libs: ["jargen"], |
| 1073 | srcs: ["c.java"], |
| 1074 | } |
| 1075 | `) |
| 1076 | |
| 1077 | foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") |
| 1078 | jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") |
| 1079 | bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") |
| 1080 | baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") |
| 1081 | barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 1082 | |
| 1083 | if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() { |
| 1084 | t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings()) |
| 1085 | } |
| 1086 | |
| 1087 | if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { |
| 1088 | t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) |
| 1089 | } |
| 1090 | |
| 1091 | if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { |
| 1092 | t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) |
| 1093 | } |
| 1094 | |
| 1095 | if len(barCombined.Inputs) != 2 || |
| 1096 | barCombined.Inputs[0].String() != bar.Output.String() || |
| 1097 | barCombined.Inputs[1].String() != jargen.Output.String() { |
| 1098 | t.Errorf("bar combined jar inputs %v is not [%q, %q]", |
| 1099 | barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) |
| 1100 | } |
| 1101 | } |
| 1102 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1103 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1104 | ctx, _ := testJava(t, ` |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1105 | java_library { |
| 1106 | name: "foo", |
| 1107 | srcs: ["a.java", ":foo-srcs"], |
| 1108 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1109 | } |
| 1110 | |
| 1111 | filegroup { |
| 1112 | name: "foo-srcs", |
| 1113 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1114 | } |
| 1115 | |
| 1116 | filegroup { |
| 1117 | name: "foo-excludes", |
| 1118 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1119 | } |
| 1120 | `) |
| 1121 | |
| 1122 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1123 | |
| 1124 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1125 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1126 | } |
| 1127 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1128 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1129 | func TestJavaLibrary(t *testing.T) { |
| 1130 | config := testConfig(nil) |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1131 | ctx := testContext("", map[string][]byte{ |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1132 | "libcore/Android.bp": []byte(` |
| 1133 | java_library { |
| 1134 | name: "core", |
| 1135 | sdk_version: "none", |
| 1136 | system_modules: "none", |
| 1137 | }`), |
| 1138 | }) |
| 1139 | run(t, ctx, config) |
| 1140 | } |
| 1141 | |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1142 | func TestJavaSdkLibrary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1143 | ctx, _ := testJava(t, ` |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1144 | droiddoc_template { |
| 1145 | name: "droiddoc-templates-sdk", |
| 1146 | path: ".", |
| 1147 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1148 | java_sdk_library { |
| 1149 | name: "foo", |
| 1150 | srcs: ["a.java", "b.java"], |
| 1151 | api_packages: ["foo"], |
| 1152 | } |
| 1153 | java_sdk_library { |
| 1154 | name: "bar", |
| 1155 | srcs: ["a.java", "b.java"], |
| 1156 | api_packages: ["bar"], |
| 1157 | } |
| 1158 | java_library { |
| 1159 | name: "baz", |
| 1160 | srcs: ["c.java"], |
| 1161 | libs: ["foo", "bar"], |
| 1162 | sdk_version: "system_current", |
| 1163 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1164 | java_library { |
| 1165 | name: "qux", |
| 1166 | srcs: ["c.java"], |
| 1167 | libs: ["baz"], |
| 1168 | sdk_version: "system_current", |
| 1169 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1170 | `) |
| 1171 | |
| 1172 | // check the existence of the internal modules |
| 1173 | ctx.ModuleForTests("foo", "android_common") |
| 1174 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common") |
| 1175 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1176 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1177 | ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common") |
| 1178 | ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1179 | ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common") |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 1180 | ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1181 | ctx.ModuleForTests("foo.api.public.28", "") |
| 1182 | ctx.ModuleForTests("foo.api.system.28", "") |
| 1183 | ctx.ModuleForTests("foo.api.test.28", "") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1184 | |
| 1185 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1186 | // tests if baz is actually linked to the stubs lib |
| 1187 | if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") { |
| 1188 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
| 1189 | "foo.stubs.system.jar") |
| 1190 | } |
| 1191 | // ... and not to the impl lib |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 1192 | if strings.Contains(bazJavac.Args["classpath"], "foo.jar") { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1193 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 1194 | "foo.jar") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1195 | } |
| 1196 | // test if baz is not linked to the system variant of foo |
| 1197 | if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") { |
| 1198 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
| 1199 | "foo.stubs.jar") |
| 1200 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1201 | |
| 1202 | // test if baz has exported SDK lib names foo and bar to qux |
| 1203 | qux := ctx.ModuleForTests("qux", "android_common") |
| 1204 | if quxLib, ok := qux.Module().(*Library); ok { |
| 1205 | sdkLibs := quxLib.ExportedSdkLibs() |
| 1206 | if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) { |
| 1207 | t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs) |
| 1208 | } |
| 1209 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1210 | } |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1211 | |
| 1212 | var compilerFlagsTestCases = []struct { |
| 1213 | in string |
| 1214 | out bool |
| 1215 | }{ |
| 1216 | { |
| 1217 | in: "a", |
| 1218 | out: false, |
| 1219 | }, |
| 1220 | { |
| 1221 | in: "-a", |
| 1222 | out: true, |
| 1223 | }, |
| 1224 | { |
| 1225 | in: "-no-jdk", |
| 1226 | out: false, |
| 1227 | }, |
| 1228 | { |
| 1229 | in: "-no-stdlib", |
| 1230 | out: false, |
| 1231 | }, |
| 1232 | { |
| 1233 | in: "-kotlin-home", |
| 1234 | out: false, |
| 1235 | }, |
| 1236 | { |
| 1237 | in: "-kotlin-home /some/path", |
| 1238 | out: false, |
| 1239 | }, |
| 1240 | { |
| 1241 | in: "-include-runtime", |
| 1242 | out: false, |
| 1243 | }, |
| 1244 | { |
| 1245 | in: "-Xintellij-plugin-root", |
| 1246 | out: false, |
| 1247 | }, |
| 1248 | } |
| 1249 | |
| 1250 | type mockContext struct { |
| 1251 | android.ModuleContext |
| 1252 | result bool |
| 1253 | } |
| 1254 | |
| 1255 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 1256 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 1257 | ctx.result = false |
| 1258 | } |
| 1259 | |
| 1260 | func TestCompilerFlags(t *testing.T) { |
| 1261 | for _, testCase := range compilerFlagsTestCases { |
| 1262 | ctx := &mockContext{result: true} |
| 1263 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 1264 | if ctx.result != testCase.out { |
| 1265 | t.Errorf("incorrect output:") |
| 1266 | t.Errorf(" input: %#v", testCase.in) |
| 1267 | t.Errorf(" expected: %#v", testCase.out) |
| 1268 | t.Errorf(" got: %#v", ctx.result) |
| 1269 | } |
| 1270 | } |
| 1271 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1272 | |
| 1273 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 1274 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
| 1275 | variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests() |
| 1276 | flags := strings.Split(variables["javacFlags"], " ") |
| 1277 | got := "" |
| 1278 | for _, flag := range flags { |
| 1279 | keyEnd := strings.Index(flag, "=") |
| 1280 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 1281 | got = flag[keyEnd+1:] |
| 1282 | break |
| 1283 | } |
| 1284 | } |
| 1285 | if expected != got { |
| 1286 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | func TestPatchModule(t *testing.T) { |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1291 | t.Run("Java language level 8", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1292 | // Test with legacy javac -source 1.8 -target 1.8 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1293 | bp := ` |
| 1294 | java_library { |
| 1295 | name: "foo", |
| 1296 | srcs: ["a.java"], |
| 1297 | java_version: "1.8", |
| 1298 | } |
| 1299 | |
| 1300 | java_library { |
| 1301 | name: "bar", |
| 1302 | srcs: ["b.java"], |
| 1303 | sdk_version: "none", |
| 1304 | system_modules: "none", |
| 1305 | patch_module: "java.base", |
| 1306 | java_version: "1.8", |
| 1307 | } |
| 1308 | |
| 1309 | java_library { |
| 1310 | name: "baz", |
| 1311 | srcs: ["c.java"], |
| 1312 | patch_module: "java.base", |
| 1313 | java_version: "1.8", |
| 1314 | } |
| 1315 | ` |
| 1316 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1317 | |
| 1318 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1319 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 1320 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 1321 | }) |
| 1322 | |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1323 | t.Run("Java language level 9", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1324 | // Test with default javac -source 9 -target 9 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1325 | bp := ` |
| 1326 | java_library { |
| 1327 | name: "foo", |
| 1328 | srcs: ["a.java"], |
| 1329 | } |
| 1330 | |
| 1331 | java_library { |
| 1332 | name: "bar", |
| 1333 | srcs: ["b.java"], |
| 1334 | sdk_version: "none", |
| 1335 | system_modules: "none", |
| 1336 | patch_module: "java.base", |
| 1337 | } |
| 1338 | |
| 1339 | java_library { |
| 1340 | name: "baz", |
| 1341 | srcs: ["c.java"], |
| 1342 | patch_module: "java.base", |
| 1343 | } |
| 1344 | ` |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1345 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1346 | |
| 1347 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1348 | expected := "java.base=.:" + buildDir |
| 1349 | checkPatchModuleFlag(t, ctx, "bar", expected) |
Insun Kang | 2343386 | 2019-01-24 20:46:28 +0900 | [diff] [blame] | 1350 | expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework"), moduleToPath("updatable_media_stubs")}, ":") |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1351 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 1352 | }) |
| 1353 | } |