blob: 628fec3f845df3a5e4066ee911f17acb4f69e059 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// 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
15package java
16
17import (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "io/ioutil"
19 "os"
20 "path/filepath"
Nan Zhang61eaedb2017-11-02 13:28:15 -070021 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070022 "strings"
23 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070024
Colin Crossa4f08812018-10-02 22:03:40 -070025 "android/soong/android"
26 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080027 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070028 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070029)
30
31var buildDir string
32
33func setUp() {
34 var err error
35 buildDir, err = ioutil.TempDir("", "soong_java_test")
36 if err != nil {
37 panic(err)
38 }
39}
40
41func tearDown() {
42 os.RemoveAll(buildDir)
43}
44
45func 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 Cross527012a2017-11-30 22:56:16 -080055
56func testConfig(env map[string]string) android.Config {
Colin Crosse4759b92019-02-15 10:37:39 -080057 return TestConfig(buildDir, env)
Colin Cross1369cdb2017-09-29 17:58:17 -070058}
59
Colin Cross527012a2017-11-30 22:56:16 -080060func testContext(config android.Config, bp string,
61 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070062
Colin Cross4c428df2017-09-15 17:36:05 -070063 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070064 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Jaewoong Jung2ad817c2019-01-18 14:27:16 -080065 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(AndroidAppCertificateFactory))
Jaewoong Jungccbb3932019-04-15 09:48:31 -070066 ctx.RegisterModuleType("android_app_import", android.ModuleFactoryAdaptor(AndroidAppImportFactory))
Colin Crossa97c5d32018-03-28 14:58:31 -070067 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070068 ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
Colin Cross638149e2019-01-05 22:12:12 -080069 ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
70 ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080071 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080072 ctx.RegisterModuleType("java_device_for_host", android.ModuleFactoryAdaptor(DeviceForHostFactory))
73 ctx.RegisterModuleType("java_host_for_device", android.ModuleFactoryAdaptor(HostForDeviceFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070074 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070075 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross638149e2019-01-05 22:12:12 -080076 ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070077 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080078 ctx.RegisterModuleType("java_import_host", android.ModuleFactoryAdaptor(ImportFactoryHost))
Colin Cross89536d42017-07-07 14:35:50 -070079 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070080 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080081 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Crossbe9cdb82019-01-21 21:37:16 -080082 ctx.RegisterModuleType("java_plugin", android.ModuleFactoryAdaptor(PluginFactory))
Colin Cross42be7612019-02-21 18:12:14 -080083 ctx.RegisterModuleType("dex_import", android.ModuleFactoryAdaptor(DexImportFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070084 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070085 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080086 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
87 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070088 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090089 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(SdkLibraryFactory))
Colin Cross79c7c262019-04-17 11:11:46 -070090 ctx.RegisterModuleType("java_sdk_library_import", android.ModuleFactoryAdaptor(sdkLibraryImportFactory))
Jaewoong Jung525443a2019-02-28 15:35:54 -080091 ctx.RegisterModuleType("override_android_app", android.ModuleFactoryAdaptor(OverrideAndroidAppModuleFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090092 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(PrebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070093 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
94 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070095 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090096 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090097 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090098 })
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070099 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800100 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -0700101 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700102
103 // Register module types and mutators from cc needed for JNI testing
104 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
105 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
106 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Colin Cross47fa9d32019-03-26 10:51:39 -0700107 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jaewoong Jung710756a2019-06-04 11:53:47 -0700108 ctx.RegisterModuleType("ndk_prebuilt_shared_stl", android.ModuleFactoryAdaptor(cc.NdkPrebuiltSharedStlFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700109 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
110 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
111 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
112 })
113
Colin Crosse4759b92019-02-15 10:37:39 -0800114 bp += GatherRequiredDepsForTest()
Colin Cross1369cdb2017-09-29 17:58:17 -0700115
Colin Cross824bee32017-11-22 17:27:51 -0800116 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900117 "Android.bp": []byte(bp),
118 "a.java": nil,
119 "b.java": nil,
120 "c.java": nil,
121 "b.kt": nil,
122 "a.jar": nil,
123 "b.jar": nil,
124 "java-res/a/a": nil,
125 "java-res/b/b": nil,
126 "java-res2/a": nil,
127 "java-fg/a.java": nil,
128 "java-fg/b.java": nil,
129 "java-fg/c.java": nil,
130 "api/current.txt": nil,
131 "api/removed.txt": nil,
132 "api/system-current.txt": nil,
133 "api/system-removed.txt": nil,
134 "api/test-current.txt": nil,
135 "api/test-removed.txt": nil,
Colin Cross3047fa22019-04-18 10:56:44 -0700136 "framework/aidl/a.aidl": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700137
Jaewoong Jung710756a2019-06-04 11:53:47 -0700138 "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so": nil,
139
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100140 "prebuilts/sdk/14/public/android.jar": nil,
141 "prebuilts/sdk/14/public/framework.aidl": nil,
142 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800143 "prebuilts/sdk/17/public/android.jar": nil,
144 "prebuilts/sdk/17/public/framework.aidl": nil,
145 "prebuilts/sdk/17/system/android.jar": nil,
Colin Crossff0daf42019-04-02 16:10:56 -0700146 "prebuilts/sdk/25/public/android.jar": nil,
147 "prebuilts/sdk/25/public/framework.aidl": nil,
148 "prebuilts/sdk/25/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700149 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100150 "prebuilts/sdk/current/public/android.jar": nil,
151 "prebuilts/sdk/current/public/framework.aidl": nil,
152 "prebuilts/sdk/current/public/core.jar": nil,
153 "prebuilts/sdk/current/system/android.jar": nil,
154 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900155 "prebuilts/sdk/28/public/api/foo.txt": nil,
156 "prebuilts/sdk/28/system/api/foo.txt": nil,
157 "prebuilts/sdk/28/test/api/foo.txt": nil,
158 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
159 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
160 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
161 "prebuilts/sdk/28/public/api/bar.txt": nil,
162 "prebuilts/sdk/28/system/api/bar.txt": nil,
163 "prebuilts/sdk/28/test/api/bar.txt": nil,
164 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
165 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
166 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700167 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900168 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800169
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700170 "prebuilts/apk/app.apk": nil,
171 "prebuilts/apk/app_xhdpi.apk": nil,
172 "prebuilts/apk/app_xxhdpi.apk": nil,
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700173
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800174 // For framework-res, which is an implicit dependency for framework
Dan Willemsen412160e2019-04-09 21:36:26 -0700175 "AndroidManifest.xml": nil,
176 "build/make/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800177
178 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800179
Colin Cross50ddcc42019-05-16 12:28:22 -0700180 "build/make/core/verify_uses_libraries.sh": nil,
181
Colin Cross66dbc0b2017-12-28 12:23:20 -0800182 "build/make/core/proguard.flags": nil,
183 "build/make/core/proguard_basic_keeps.flags": nil,
184
Colin Cross7fdd2b72018-01-02 18:14:25 -0800185 "jdk8/jre/lib/jce.jar": nil,
186 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700187 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800188
189 "bar-doc/a.java": nil,
190 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900191 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800192 "bar-doc/known_oj_tags.txt": nil,
193 "external/doclava/templates-sdk": nil,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800194
195 "cert/new_cert.x509.pem": nil,
196 "cert/new_cert.pk8": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800197 }
198
199 for k, v := range fs {
200 mockFS[k] = v
201 }
202
203 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700204
Colin Cross527012a2017-11-30 22:56:16 -0800205 return ctx
206}
207
208func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800209 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800210
211 pathCtx := android.PathContextForTesting(config, nil)
212 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
213
214 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100215 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800216 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700217 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800218 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800219}
220
221func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800222 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800223 config := testConfig(nil)
224 ctx := testContext(config, bp, nil)
225 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700226
227 return ctx
228}
229
Colin Cross2acdae82017-09-15 19:44:24 -0700230func moduleToPath(name string) string {
231 switch {
232 case name == `""`:
233 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700234 case strings.HasSuffix(name, ".jar"):
235 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700236 default:
237 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700238 }
239}
240
Colin Cross72bb3632017-07-13 16:23:21 -0700241func TestSimple(t *testing.T) {
242 ctx := testJava(t, `
243 java_library {
244 name: "foo",
245 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700246 libs: ["bar"],
247 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700248 }
249
250 java_library {
251 name: "bar",
252 srcs: ["b.java"],
253 }
254
255 java_library {
256 name: "baz",
257 srcs: ["c.java"],
258 }
Colin Crossd5934c82017-10-02 13:55:26 -0700259 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700260
Colin Cross4c428df2017-09-15 17:36:05 -0700261 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700262 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700263
264 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
265 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
266 }
267
Colin Cross1ee23172017-10-18 14:44:18 -0700268 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700269 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
270 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700271
Nan Zhanged19fc32017-10-19 13:06:22 -0700272 if !strings.Contains(javac.Args["classpath"], barTurbine) {
273 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700274 }
275
Nan Zhanged19fc32017-10-19 13:06:22 -0700276 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
277 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700278 }
279
280 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
281 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700282 }
283}
284
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900285func TestSdkVersion(t *testing.T) {
286 ctx := testJava(t, `
287 java_library {
288 name: "foo",
289 srcs: ["a.java"],
290 vendor: true,
291 }
292
293 java_library {
294 name: "bar",
295 srcs: ["b.java"],
296 }
297 `)
298
299 foo := ctx.ModuleForTests("foo", "android_common").Module().(*Library)
300 bar := ctx.ModuleForTests("bar", "android_common").Module().(*Library)
301
302 if foo.sdkVersion() != "system_current" {
303 t.Errorf("If sdk version of vendor module is empty, it must change to system_current.")
304 }
305
306 if bar.sdkVersion() != "" {
307 t.Errorf("If sdk version of non-vendor module is empty, it keeps empty.")
308 }
309}
310
Colin Crossd5934c82017-10-02 13:55:26 -0700311func TestArchSpecific(t *testing.T) {
312 ctx := testJava(t, `
313 java_library {
314 name: "foo",
315 srcs: ["a.java"],
316 target: {
317 android: {
318 srcs: ["b.java"],
319 },
320 },
321 }
322 `)
323
324 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
325 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
326 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
327 }
328}
329
Colin Cross6b4a32d2017-12-05 13:42:45 -0800330func TestBinary(t *testing.T) {
331 ctx := testJava(t, `
332 java_library_host {
333 name: "foo",
334 srcs: ["a.java"],
335 }
336
337 java_binary_host {
338 name: "bar",
339 srcs: ["b.java"],
340 static_libs: ["foo"],
341 }
342 `)
343
344 buildOS := android.BuildOs.String()
345
346 bar := ctx.ModuleForTests("bar", buildOS+"_common")
347 barJar := bar.Output("bar.jar").Output.String()
348 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
349 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
350
351 // Test that the install binary wrapper depends on the installed jar file
352 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
353 t.Errorf("expected binary wrapper implicits [%q], got %v",
354 barJar, barWrapperDeps)
355 }
356
357}
358
Colin Cross72bb3632017-07-13 16:23:21 -0700359func TestPrebuilts(t *testing.T) {
360 ctx := testJava(t, `
361 java_library {
362 name: "foo",
363 srcs: ["a.java"],
Colin Cross79c7c262019-04-17 11:11:46 -0700364 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700365 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700366 }
367
Colin Cross74d73e22017-08-02 11:05:49 -0700368 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700369 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700370 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700371 }
372
Colin Cross74d73e22017-08-02 11:05:49 -0700373 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700374 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700375 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700376 }
Colin Cross42be7612019-02-21 18:12:14 -0800377
378 dex_import {
379 name: "qux",
380 jars: ["b.jar"],
381 }
Colin Cross79c7c262019-04-17 11:11:46 -0700382
383 java_sdk_library_import {
384 name: "sdklib",
385 jars: ["b.jar"],
386 }
Colin Cross72bb3632017-07-13 16:23:21 -0700387 `)
388
Colin Cross4c428df2017-09-15 17:36:05 -0700389 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700390 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700391 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
392 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700393 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700394
Colin Cross37f6d792018-07-12 12:28:41 -0700395 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
396 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700397 }
398
Colin Cross79c7c262019-04-17 11:11:46 -0700399 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
400 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
401 }
402
Colin Cross37f6d792018-07-12 12:28:41 -0700403 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
404 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700405 }
Colin Cross42be7612019-02-21 18:12:14 -0800406
407 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700408}
409
Colin Cross89536d42017-07-07 14:35:50 -0700410func TestDefaults(t *testing.T) {
411 ctx := testJava(t, `
412 java_defaults {
413 name: "defaults",
414 srcs: ["a.java"],
415 libs: ["bar"],
416 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700417 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700418 }
419
420 java_library {
421 name: "foo",
422 defaults: ["defaults"],
423 }
424
425 java_library {
426 name: "bar",
427 srcs: ["b.java"],
428 }
429
430 java_library {
431 name: "baz",
432 srcs: ["c.java"],
433 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700434
435 android_test {
436 name: "atestOptimize",
437 defaults: ["defaults"],
438 optimize: {enabled: true},
439 }
440
441 android_test {
442 name: "atestNoOptimize",
443 defaults: ["defaults"],
444 }
445
446 android_test {
447 name: "atestDefault",
448 srcs: ["a.java"],
449 }
Colin Cross89536d42017-07-07 14:35:50 -0700450 `)
451
Colin Cross4c428df2017-09-15 17:36:05 -0700452 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700453 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700454
455 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
456 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
457 }
458
Nan Zhanged19fc32017-10-19 13:06:22 -0700459 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
460 if !strings.Contains(javac.Args["classpath"], barTurbine) {
461 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700462 }
463
Colin Cross1ee23172017-10-18 14:44:18 -0700464 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700465 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
466 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700467 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700468
469 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
470 if atestOptimize.Output == nil {
471 t.Errorf("atestOptimize should optimize APK")
472 }
473
474 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
475 if atestNoOptimize.Output == nil {
476 t.Errorf("atestNoOptimize should not optimize APK")
477 }
478
479 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
480 if atestDefault.Output == nil {
481 t.Errorf("atestDefault should optimize APK")
482 }
Colin Cross89536d42017-07-07 14:35:50 -0700483}
484
Colin Cross0f37af02017-09-27 17:42:05 -0700485func TestResources(t *testing.T) {
486 var table = []struct {
487 name string
488 prop string
489 extra string
490 args string
491 }{
492 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700493 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700494 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800495 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700496 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700497 },
498 {
499 // Test that a module with java_resources includes the files
500 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700501 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
502 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700503 },
504 {
505 // Test that a module with a filegroup in java_resources includes the files with the
506 // path prefix
507 name: "resource filegroup",
508 prop: `java_resources: [":foo-res"]`,
509 extra: `
510 filegroup {
511 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800512 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700513 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700514 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700515 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700516 },
517 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700518 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
519 name: "wildcard dirs",
520 prop: `java_resource_dirs: ["java-res/*"]`,
521 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
522 },
523 {
524 // Test that a module exclude_java_resource_dirs excludes the files
525 name: "wildcard dirs",
526 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
527 args: "-C java-res/a -f java-res/a/a",
528 },
Colin Crosscedd4762018-09-13 11:26:19 -0700529 {
530 // Test wildcards in java_resources
531 name: "wildcard files",
532 prop: `java_resources: ["java-res/**/*"]`,
533 args: "-C . -f java-res/a/a -f java-res/b/b",
534 },
535 {
536 // Test exclude_java_resources with java_resources
537 name: "wildcard files with exclude",
538 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
539 args: "-C . -f java-res/a/a",
540 },
541 {
542 // Test exclude_java_resources with java_resource_dirs
543 name: "resource dirs with exclude files",
544 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
545 args: "-C java-res -f java-res/a/a",
546 },
547 {
548 // Test exclude_java_resource_dirs with java_resource_dirs
549 name: "resource dirs with exclude files",
550 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
551 args: "-C java-res -f java-res/a/a -f java-res/b/b",
552 },
Colin Cross0f37af02017-09-27 17:42:05 -0700553 }
554
555 for _, test := range table {
556 t.Run(test.name, func(t *testing.T) {
557 ctx := testJava(t, `
558 java_library {
559 name: "foo",
560 srcs: [
561 "a.java",
562 "b.java",
563 "c.java",
564 ],
565 `+test.prop+`,
566 }
567 `+test.extra)
568
Colin Cross331a1212018-08-15 20:40:52 -0700569 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700570 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700571
572 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
573 t.Errorf("foo combined jars %v does not contain %q",
574 foo.Inputs.Strings(), fooRes.Output.String())
575 }
576
Colin Crossaf9c55b2017-10-03 14:50:08 -0700577 if fooRes.Args["jarArgs"] != test.args {
578 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700579 fooRes.Args["jarArgs"], test.args)
580 }
581 })
582 }
583}
584
Colin Cross0c4ce212019-05-03 15:28:19 -0700585func TestIncludeSrcs(t *testing.T) {
586 ctx := testJava(t, `
587 java_library {
588 name: "foo",
589 srcs: [
590 "a.java",
591 "b.java",
592 "c.java",
593 ],
594 include_srcs: true,
595 }
596
597 java_library {
598 name: "bar",
599 srcs: [
600 "a.java",
601 "b.java",
602 "c.java",
603 ],
604 java_resource_dirs: ["java-res"],
605 include_srcs: true,
606 }
607 `)
608
609 // Test a library with include_srcs: true
610 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
611 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
612
613 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
614 t.Errorf("foo combined jars %v does not contain %q", w, g)
615 }
616
617 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
618 t.Errorf("foo source jar args %q is not %q", w, g)
619 }
620
621 // Test a library with include_srcs: true and resources
622 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
623 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
624 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
625 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
626
627 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
628 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
629 }
630
631 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
632 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
633 }
634
635 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
636 t.Errorf("bar combined jars %v does not contain %q", w, g)
637 }
638
639 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
640 t.Errorf("bar source jar args %q is not %q", w, g)
641 }
642
643 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
644 t.Errorf("bar resource jar args %q is not %q", w, g)
645 }
646}
647
Colin Cross54190b32017-10-09 15:34:10 -0700648func TestGeneratedSources(t *testing.T) {
649 ctx := testJava(t, `
650 java_library {
651 name: "foo",
652 srcs: [
653 "a*.java",
654 ":gen",
655 "b*.java",
656 ],
657 }
658
659 genrule {
660 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800661 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700662 out: ["gen.java"],
663 }
664 `)
665
666 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
667 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
668
Colin Cross15e86d92017-10-20 15:07:08 -0700669 if filepath.Base(genrule.Output.String()) != "gen.java" {
670 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700671 }
672
673 if len(javac.Inputs) != 3 ||
674 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700675 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700676 javac.Inputs[2].String() != "b.java" {
677 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
678 }
679}
680
Nan Zhang61eaedb2017-11-02 13:28:15 -0700681func TestTurbine(t *testing.T) {
682 ctx := testJava(t, `
683 java_library {
684 name: "foo",
685 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900686 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700687 }
688
689 java_library {
690 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800691 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700692 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900693 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700694 }
695
696 java_library {
697 name: "baz",
698 srcs: ["c.java"],
699 libs: ["bar"],
700 sdk_version: "14",
701 }
702 `)
703
704 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
705 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
706 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
707 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
708 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
709
710 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
711 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
712 }
713
714 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
715 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
716 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
717 }
718 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
719 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
720 }
721 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
722 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
723 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100724 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700725 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100726 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700727 }
728}
729
730func TestSharding(t *testing.T) {
731 ctx := testJava(t, `
732 java_library {
733 name: "bar",
734 srcs: ["a.java","b.java","c.java"],
735 javac_shard_size: 1
736 }
737 `)
738
739 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
740 for i := 0; i < 3; i++ {
741 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
742 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
743 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
744 }
745 }
746}
747
Nan Zhang581fd212018-01-10 16:06:12 -0800748func TestDroiddoc(t *testing.T) {
749 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800750 droiddoc_template {
751 name: "droiddoc-templates-sdk",
752 path: ".",
753 }
Nan Zhang581fd212018-01-10 16:06:12 -0800754 droiddoc {
755 name: "bar-doc",
756 srcs: [
757 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900758 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800759 ],
760 exclude_srcs: [
761 "bar-doc/b.java"
762 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800763 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800764 hdf: [
765 "android.whichdoc offline",
766 ],
767 knowntags: [
768 "bar-doc/known_oj_tags.txt",
769 ],
770 proofread_file: "libcore-proofread.txt",
771 todo_file: "libcore-docs-todo.html",
772 args: "-offlinemode -title \"libcore\"",
773 }
774 `)
775
Nan Zhang3bb14202018-02-28 10:14:25 -0800776 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
777 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800778 if stubsJar != barDoc.Output.String() {
779 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
780 }
Jiyong Park1e440682018-05-23 18:42:04 +0900781 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
782 var javaSrcs []string
783 for _, i := range inputs {
784 javaSrcs = append(javaSrcs, i.Base())
785 }
786 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
787 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
788 }
Nan Zhang581fd212018-01-10 16:06:12 -0800789}
790
Colin Cross54250902017-12-05 09:28:08 -0800791func TestJarGenrules(t *testing.T) {
792 ctx := testJava(t, `
793 java_library {
794 name: "foo",
795 srcs: ["a.java"],
796 }
797
798 java_genrule {
799 name: "jargen",
800 tool_files: ["b.java"],
801 cmd: "$(location b.java) $(in) $(out)",
802 out: ["jargen.jar"],
803 srcs: [":foo"],
804 }
805
806 java_library {
807 name: "bar",
808 static_libs: ["jargen"],
809 srcs: ["c.java"],
810 }
811
812 java_library {
813 name: "baz",
814 libs: ["jargen"],
815 srcs: ["c.java"],
816 }
817 `)
818
819 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
820 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
821 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
822 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
823 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
824
825 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
826 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
827 }
828
829 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
830 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
831 }
832
833 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
834 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
835 }
836
837 if len(barCombined.Inputs) != 2 ||
838 barCombined.Inputs[0].String() != bar.Output.String() ||
839 barCombined.Inputs[1].String() != jargen.Output.String() {
840 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
841 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
842 }
843}
844
Nan Zhang27e284d2018-02-09 21:03:53 +0000845func TestExcludeFileGroupInSrcs(t *testing.T) {
846 ctx := testJava(t, `
847 java_library {
848 name: "foo",
849 srcs: ["a.java", ":foo-srcs"],
850 exclude_srcs: ["a.java", ":foo-excludes"],
851 }
852
853 filegroup {
854 name: "foo-srcs",
855 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
856 }
857
858 filegroup {
859 name: "foo-excludes",
860 srcs: ["java-fg/a.java", "java-fg/b.java"],
861 }
862 `)
863
864 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
865
866 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
867 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
868 }
869}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900870
Paul Duffin52d398a2019-06-11 12:31:14 +0100871func TestJavaLibrary(t *testing.T) {
872 config := testConfig(nil)
873 ctx := testContext(config, "", map[string][]byte{
874 "libcore/Android.bp": []byte(`
875 java_library {
876 name: "core",
877 sdk_version: "none",
878 system_modules: "none",
879 }`),
880 })
881 run(t, ctx, config)
882}
883
Jiyong Parkc678ad32018-04-10 13:07:10 +0900884func TestJavaSdkLibrary(t *testing.T) {
885 ctx := testJava(t, `
886 droiddoc_template {
887 name: "droiddoc-templates-sdk",
888 path: ".",
889 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900890 java_sdk_library {
891 name: "foo",
892 srcs: ["a.java", "b.java"],
893 api_packages: ["foo"],
894 }
895 java_sdk_library {
896 name: "bar",
897 srcs: ["a.java", "b.java"],
898 api_packages: ["bar"],
899 }
900 java_library {
901 name: "baz",
902 srcs: ["c.java"],
903 libs: ["foo", "bar"],
904 sdk_version: "system_current",
905 }
Jiyong Park1be96912018-05-28 18:02:19 +0900906 java_library {
907 name: "qux",
908 srcs: ["c.java"],
909 libs: ["baz"],
910 sdk_version: "system_current",
911 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900912 `)
913
914 // check the existence of the internal modules
915 ctx.ModuleForTests("foo", "android_common")
916 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
917 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900918 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900919 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
920 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900921 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800922 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +0900923 ctx.ModuleForTests("foo.api.public.28", "")
924 ctx.ModuleForTests("foo.api.system.28", "")
925 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900926
927 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
928 // tests if baz is actually linked to the stubs lib
929 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
930 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
931 "foo.stubs.system.jar")
932 }
933 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +0900934 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900935 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +0900936 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900937 }
938 // test if baz is not linked to the system variant of foo
939 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
940 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
941 "foo.stubs.jar")
942 }
Jiyong Park1be96912018-05-28 18:02:19 +0900943
944 // test if baz has exported SDK lib names foo and bar to qux
945 qux := ctx.ModuleForTests("qux", "android_common")
946 if quxLib, ok := qux.Module().(*Library); ok {
947 sdkLibs := quxLib.ExportedSdkLibs()
948 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
949 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
950 }
951 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900952}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200953
954var compilerFlagsTestCases = []struct {
955 in string
956 out bool
957}{
958 {
959 in: "a",
960 out: false,
961 },
962 {
963 in: "-a",
964 out: true,
965 },
966 {
967 in: "-no-jdk",
968 out: false,
969 },
970 {
971 in: "-no-stdlib",
972 out: false,
973 },
974 {
975 in: "-kotlin-home",
976 out: false,
977 },
978 {
979 in: "-kotlin-home /some/path",
980 out: false,
981 },
982 {
983 in: "-include-runtime",
984 out: false,
985 },
986 {
987 in: "-Xintellij-plugin-root",
988 out: false,
989 },
990}
991
992type mockContext struct {
993 android.ModuleContext
994 result bool
995}
996
997func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
998 // CheckBadCompilerFlags calls this function when the flag should be rejected
999 ctx.result = false
1000}
1001
1002func TestCompilerFlags(t *testing.T) {
1003 for _, testCase := range compilerFlagsTestCases {
1004 ctx := &mockContext{result: true}
1005 CheckKotlincFlags(ctx, []string{testCase.in})
1006 if ctx.result != testCase.out {
1007 t.Errorf("incorrect output:")
1008 t.Errorf(" input: %#v", testCase.in)
1009 t.Errorf(" expected: %#v", testCase.out)
1010 t.Errorf(" got: %#v", ctx.result)
1011 }
1012 }
1013}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001014
1015// TODO(jungjw): Consider making this more robust by ignoring path order.
1016func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1017 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1018 flags := strings.Split(variables["javacFlags"], " ")
1019 got := ""
1020 for _, flag := range flags {
1021 keyEnd := strings.Index(flag, "=")
1022 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1023 got = flag[keyEnd+1:]
1024 break
1025 }
1026 }
1027 if expected != got {
1028 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1029 }
1030}
1031
1032func TestPatchModule(t *testing.T) {
1033 bp := `
1034 java_library {
1035 name: "foo",
1036 srcs: ["a.java"],
1037 }
1038
1039 java_library {
1040 name: "bar",
1041 srcs: ["b.java"],
Paul Duffina3d09862019-06-11 13:40:47 +01001042 sdk_version: "none",
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001043 system_modules: "none",
1044 patch_module: "java.base",
1045 }
1046
1047 java_library {
1048 name: "baz",
1049 srcs: ["c.java"],
1050 patch_module: "java.base",
1051 }
1052 `
1053
Pete Gillin0c2143e2019-05-02 15:32:11 +01001054 t.Run("Java language level 8", func(t *testing.T) {
1055 // Test default javac -source 1.8 -target 1.8
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001056 ctx := testJava(t, bp)
1057
1058 checkPatchModuleFlag(t, ctx, "foo", "")
1059 checkPatchModuleFlag(t, ctx, "bar", "")
1060 checkPatchModuleFlag(t, ctx, "baz", "")
1061 })
1062
Pete Gillin0c2143e2019-05-02 15:32:11 +01001063 t.Run("Java language level 9", func(t *testing.T) {
1064 // Test again with javac -source 9 -target 9
1065 config := testConfig(map[string]string{"EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9": "true"})
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001066 ctx := testContext(config, bp, nil)
1067 run(t, ctx, config)
1068
1069 checkPatchModuleFlag(t, ctx, "foo", "")
1070 expected := "java.base=.:" + buildDir
1071 checkPatchModuleFlag(t, ctx, "bar", expected)
1072 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
1073 checkPatchModuleFlag(t, ctx, "baz", expected)
1074 })
1075}