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