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