blob: 66178732ab624bce08269a15866aeb026fc57007 [file] [log] [blame]
Colin Cross9bb9bfb2022-03-17 11:12:32 -07001// Copyright 2022 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 (
18 "testing"
19
20 "android/soong/android"
21)
22
23func TestR8(t *testing.T) {
24 result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, `
25 android_app {
26 name: "app",
27 srcs: ["foo.java"],
28 libs: ["lib"],
29 static_libs: ["static_lib"],
30 platform_apis: true,
31 }
32
Jared Duke40d731a2022-09-20 15:32:14 -070033 android_app {
34 name: "stable_app",
35 srcs: ["foo.java"],
36 sdk_version: "current",
37 min_sdk_version: "31",
38 }
39
40 android_app {
41 name: "core_platform_app",
42 srcs: ["foo.java"],
43 sdk_version: "core_platform",
44 }
45
Colin Cross9bb9bfb2022-03-17 11:12:32 -070046 java_library {
47 name: "lib",
48 srcs: ["foo.java"],
49 }
50
51 java_library {
52 name: "static_lib",
53 srcs: ["foo.java"],
54 }
55 `)
56
57 app := result.ModuleForTests("app", "android_common")
Jared Duke40d731a2022-09-20 15:32:14 -070058 stableApp := result.ModuleForTests("stable_app", "android_common")
59 corePlatformApp := result.ModuleForTests("core_platform_app", "android_common")
Colin Cross9bb9bfb2022-03-17 11:12:32 -070060 lib := result.ModuleForTests("lib", "android_common")
61 staticLib := result.ModuleForTests("static_lib", "android_common")
62
63 appJavac := app.Rule("javac")
64 appR8 := app.Rule("r8")
Jared Duke40d731a2022-09-20 15:32:14 -070065 stableAppR8 := stableApp.Rule("r8")
66 corePlatformAppR8 := corePlatformApp.Rule("r8")
Colin Cross9bb9bfb2022-03-17 11:12:32 -070067 libHeader := lib.Output("turbine-combined/lib.jar").Output
68 staticLibHeader := staticLib.Output("turbine-combined/static_lib.jar").Output
69
70 android.AssertStringDoesContain(t, "expected lib header jar in app javac classpath",
71 appJavac.Args["classpath"], libHeader.String())
72 android.AssertStringDoesContain(t, "expected static_lib header jar in app javac classpath",
73 appJavac.Args["classpath"], staticLibHeader.String())
74
75 android.AssertStringDoesContain(t, "expected lib header jar in app r8 classpath",
76 appR8.Args["r8Flags"], libHeader.String())
77 android.AssertStringDoesNotContain(t, "expected no static_lib header jar in app javac classpath",
78 appR8.Args["r8Flags"], staticLibHeader.String())
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +090079 android.AssertStringDoesContain(t, "expected -ignorewarnings in app r8 flags",
80 appR8.Args["r8Flags"], "-ignorewarnings")
Jared Duke40d731a2022-09-20 15:32:14 -070081 android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags",
82 appR8.Args["r8Flags"], "--android-platform-build")
83 android.AssertStringDoesNotContain(t, "expected no --android-platform-build in stable_app r8 flags",
84 stableAppR8.Args["r8Flags"], "--android-platform-build")
85 android.AssertStringDoesContain(t, "expected --android-platform-build in core_platform_app r8 flags",
86 corePlatformAppR8.Args["r8Flags"], "--android-platform-build")
Remi NGUYEN VANbdad3142022-08-04 13:19:03 +090087}
88
89func TestR8Flags(t *testing.T) {
90 result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, `
91 android_app {
92 name: "app",
93 srcs: ["foo.java"],
94 platform_apis: true,
95 optimize: {
96 shrink: false,
97 optimize: false,
98 obfuscate: false,
99 ignore_warnings: false,
100 },
101 }
102 `)
103
104 app := result.ModuleForTests("app", "android_common")
105 appR8 := app.Rule("r8")
106 android.AssertStringDoesContain(t, "expected -dontshrink in app r8 flags",
107 appR8.Args["r8Flags"], "-dontshrink")
108 android.AssertStringDoesContain(t, "expected -dontoptimize in app r8 flags",
109 appR8.Args["r8Flags"], "-dontoptimize")
110 android.AssertStringDoesContain(t, "expected -dontobfuscate in app r8 flags",
111 appR8.Args["r8Flags"], "-dontobfuscate")
112 android.AssertStringDoesNotContain(t, "expected no -ignorewarnings in app r8 flags",
113 appR8.Args["r8Flags"], "-ignorewarnings")
Jared Duke40d731a2022-09-20 15:32:14 -0700114 android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags",
115 appR8.Args["r8Flags"], "--android-platform-build")
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700116}
117
118func TestD8(t *testing.T) {
119 result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, `
120 java_library {
121 name: "foo",
122 srcs: ["foo.java"],
123 libs: ["lib"],
124 static_libs: ["static_lib"],
125 installable: true,
126 }
127
128 java_library {
129 name: "lib",
130 srcs: ["foo.java"],
131 }
132
133 java_library {
134 name: "static_lib",
135 srcs: ["foo.java"],
136 }
137 `)
138
139 foo := result.ModuleForTests("foo", "android_common")
140 lib := result.ModuleForTests("lib", "android_common")
141 staticLib := result.ModuleForTests("static_lib", "android_common")
142
143 fooJavac := foo.Rule("javac")
144 fooD8 := foo.Rule("d8")
145 libHeader := lib.Output("turbine-combined/lib.jar").Output
146 staticLibHeader := staticLib.Output("turbine-combined/static_lib.jar").Output
147
148 android.AssertStringDoesContain(t, "expected lib header jar in foo javac classpath",
149 fooJavac.Args["classpath"], libHeader.String())
150 android.AssertStringDoesContain(t, "expected static_lib header jar in foo javac classpath",
151 fooJavac.Args["classpath"], staticLibHeader.String())
152
153 android.AssertStringDoesContain(t, "expected lib header jar in foo d8 classpath",
154 fooD8.Args["d8Flags"], libHeader.String())
155 android.AssertStringDoesNotContain(t, "expected no static_lib header jar in foo javac classpath",
156 fooD8.Args["d8Flags"], staticLibHeader.String())
157}