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