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