blob: 82ebba7b17f0b808b77c5b9e9725d2e237fcda3e [file] [log] [blame]
Colin Cross2207f872021-03-24 12:39:08 -07001// Copyright 2021 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 (
Pedro Loureirocc203502021-10-04 17:24:00 +000018 "fmt"
Colin Cross6aa5c402021-03-24 12:28:50 -070019 "reflect"
satayev783195c2021-06-23 21:49:57 +010020 "regexp"
Colin Cross2207f872021-03-24 12:39:08 -070021 "strings"
22 "testing"
23
Keyife310a92022-01-07 15:42:18 +080024 "github.com/google/blueprint"
25
Colin Cross2207f872021-03-24 12:39:08 -070026 "android/soong/android"
27)
28
29func TestDroidstubs(t *testing.T) {
30 ctx, _ := testJavaWithFS(t, `
31 droiddoc_exported_dir {
32 name: "droiddoc-templates-sdk",
33 path: ".",
34 }
35
36 droidstubs {
37 name: "bar-stubs",
38 srcs: ["bar-doc/a.java"],
39 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
40 api_levels_annotations_enabled: true,
41 }
42
43 droidstubs {
44 name: "bar-stubs-other",
45 srcs: ["bar-doc/a.java"],
46 high_mem: true,
47 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
48 api_levels_annotations_enabled: true,
49 api_levels_jar_filename: "android.other.jar",
50 }
51 `,
52 map[string][]byte{
53 "bar-doc/a.java": nil,
54 })
55 testcases := []struct {
56 moduleName string
57 expectedJarFilename string
58 high_mem bool
59 }{
60 {
61 moduleName: "bar-stubs",
62 expectedJarFilename: "android.jar",
63 high_mem: false,
64 },
65 {
66 moduleName: "bar-stubs-other",
67 expectedJarFilename: "android.other.jar",
68 high_mem: true,
69 },
70 }
71 for _, c := range testcases {
72 m := ctx.ModuleForTests(c.moduleName, "android_common")
Colin Cross8095c292021-03-30 16:40:48 -070073 manifest := m.Output("metalava.sbox.textproto")
74 sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest)
Colin Cross2207f872021-03-24 12:39:08 -070075 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Colin Cross8095c292021-03-30 16:40:48 -070076 if actual := String(sboxProto.Commands[0].Command); !strings.Contains(actual, expected) {
Colin Cross2207f872021-03-24 12:39:08 -070077 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
78 }
79
Colin Cross8095c292021-03-30 16:40:48 -070080 metalava := m.Rule("metalava")
81 rp := metalava.RuleParams
Colin Cross2207f872021-03-24 12:39:08 -070082 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
83 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
84 }
85 }
86}
87
Pedro Loureirocc203502021-10-04 17:24:00 +000088// runs a test for droidstubs with a customizable sdkType argument and returns
89// the list of jar patterns that is passed as `--android-jar-pattern`
90func getAndroidJarPatternsForDroidstubs(t *testing.T, sdkType string) []string {
91 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
satayev783195c2021-06-23 21:49:57 +010092 droiddoc_exported_dir {
93 name: "some-exported-dir",
94 path: "somedir",
95 }
96
97 droiddoc_exported_dir {
98 name: "some-other-exported-dir",
99 path: "someotherdir",
100 }
101
102 droidstubs {
103 name: "foo-stubs",
104 srcs: ["foo-doc/a.java"],
105 api_levels_annotations_dirs: [
106 "some-exported-dir",
107 "some-other-exported-dir",
108 ],
109 api_levels_annotations_enabled: true,
Pedro Loureirocc203502021-10-04 17:24:00 +0000110 api_levels_sdk_type: "%s",
satayev783195c2021-06-23 21:49:57 +0100111 }
Pedro Loureirocc203502021-10-04 17:24:00 +0000112 `, sdkType),
satayev783195c2021-06-23 21:49:57 +0100113 map[string][]byte{
114 "foo-doc/a.java": nil,
115 })
116
117 m := ctx.ModuleForTests("foo-stubs", "android_common")
118 manifest := m.Output("metalava.sbox.textproto")
119 cmd := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command)
120 r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`)
Pedro Loureirocc203502021-10-04 17:24:00 +0000121 return r.FindAllString(cmd, -1)
122}
123
124func TestPublicDroidstubs(t *testing.T) {
125 patterns := getAndroidJarPatternsForDroidstubs(t, "public")
126
127 android.AssertArrayString(t, "order of patterns", []string{
128 "--android-jar-pattern somedir/%/public/android.jar",
129 "--android-jar-pattern someotherdir/%/public/android.jar",
130 }, patterns)
131}
132
133func TestSystemDroidstubs(t *testing.T) {
134 patterns := getAndroidJarPatternsForDroidstubs(t, "system")
135
satayev783195c2021-06-23 21:49:57 +0100136 android.AssertArrayString(t, "order of patterns", []string{
137 "--android-jar-pattern somedir/%/system/android.jar",
138 "--android-jar-pattern someotherdir/%/system/android.jar",
139 "--android-jar-pattern somedir/%/public/android.jar",
140 "--android-jar-pattern someotherdir/%/public/android.jar",
Pedro Loureirocc203502021-10-04 17:24:00 +0000141 }, patterns)
142}
143
144func TestModuleLibDroidstubs(t *testing.T) {
145 patterns := getAndroidJarPatternsForDroidstubs(t, "module-lib")
146
147 android.AssertArrayString(t, "order of patterns", []string{
148 "--android-jar-pattern somedir/%/module-lib/android.jar",
149 "--android-jar-pattern someotherdir/%/module-lib/android.jar",
150 "--android-jar-pattern somedir/%/system/android.jar",
151 "--android-jar-pattern someotherdir/%/system/android.jar",
152 "--android-jar-pattern somedir/%/public/android.jar",
153 "--android-jar-pattern someotherdir/%/public/android.jar",
154 }, patterns)
satayev783195c2021-06-23 21:49:57 +0100155}
156
Colin Cross6aa5c402021-03-24 12:28:50 -0700157func TestDroidstubsSandbox(t *testing.T) {
158 ctx, _ := testJavaWithFS(t, `
Colin Crossbc139922021-03-25 18:33:16 -0700159 genrule {
160 name: "foo",
161 out: ["foo.txt"],
162 cmd: "touch $(out)",
163 }
164
Colin Cross6aa5c402021-03-24 12:28:50 -0700165 droidstubs {
166 name: "bar-stubs",
167 srcs: ["bar-doc/a.java"],
Colin Crossbc139922021-03-25 18:33:16 -0700168
169 args: "--reference $(location :foo)",
170 arg_files: [":foo"],
Colin Cross6aa5c402021-03-24 12:28:50 -0700171 }
172 `,
173 map[string][]byte{
174 "bar-doc/a.java": nil,
175 })
176
177 m := ctx.ModuleForTests("bar-stubs", "android_common")
178 metalava := m.Rule("metalava")
179 if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
180 t.Errorf("Expected inputs %q, got %q", w, g)
181 }
Colin Crossbc139922021-03-25 18:33:16 -0700182
183 manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto"))
184 if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
185 t.Errorf("Expected command to contain %q, got %q", w, g)
186 }
Colin Cross6aa5c402021-03-24 12:28:50 -0700187}
188
Colin Cross2207f872021-03-24 12:39:08 -0700189func TestDroidstubsWithSystemModules(t *testing.T) {
190 ctx, _ := testJava(t, `
191 droidstubs {
192 name: "stubs-source-system-modules",
193 srcs: [
194 "bar-doc/a.java",
195 ],
196 sdk_version: "none",
197 system_modules: "source-system-modules",
198 }
199
200 java_library {
201 name: "source-jar",
202 srcs: [
203 "a.java",
204 ],
205 }
206
207 java_system_modules {
208 name: "source-system-modules",
209 libs: ["source-jar"],
210 }
211
212 droidstubs {
213 name: "stubs-prebuilt-system-modules",
214 srcs: [
215 "bar-doc/a.java",
216 ],
217 sdk_version: "none",
218 system_modules: "prebuilt-system-modules",
219 }
220
221 java_import {
222 name: "prebuilt-jar",
223 jars: ["a.jar"],
224 }
225
226 java_system_modules_import {
227 name: "prebuilt-system-modules",
228 libs: ["prebuilt-jar"],
229 }
230 `)
231
232 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
233
234 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
235}
236
Keyife310a92022-01-07 15:42:18 +0800237func TestAddJSONData(t *testing.T) {
238 prebuiltStubsSources := PrebuiltStubsSources{}
239 prebuiltStubsSources.jsonDataActions = []blueprint.JSONDataAction{
240 blueprint.JSONDataAction{
241 Inputs: []string{},
242 Outputs: []string{},
243 },
244 }
245 jsonData := map[string]interface{}{}
246 prebuiltStubsSources.AddJSONData(&jsonData)
Liz Kammer9525e712022-01-05 13:46:24 -0500247 expectedOut := []map[string]interface{}{
Keyife310a92022-01-07 15:42:18 +0800248 map[string]interface{}{
Liz Kammer9525e712022-01-05 13:46:24 -0500249 "Inputs": []string{},
250 "Outputs": []string{},
251 },
252 }
253 if !reflect.DeepEqual(jsonData["Actions"], expectedOut) {
254 t.Errorf("The JSON action data %#v isn't as expected %#v.", jsonData["Actions"], expectedOut)
Keyife310a92022-01-07 15:42:18 +0800255 }
256}
257
Colin Cross2207f872021-03-24 12:39:08 -0700258func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
259 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
260 var systemJars []string
261 for _, i := range metalavaRule.Implicits {
262 systemJars = append(systemJars, i.Base())
263 }
264 if len(systemJars) < 1 || systemJars[0] != systemJar {
265 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
266 }
267}