blob: 10d99f3a54fbc79e4f9706456f2ffaafbc91b9d4 [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
24 "android/soong/android"
25)
26
27func TestDroidstubs(t *testing.T) {
28 ctx, _ := testJavaWithFS(t, `
29 droiddoc_exported_dir {
30 name: "droiddoc-templates-sdk",
31 path: ".",
32 }
33
34 droidstubs {
35 name: "bar-stubs",
36 srcs: ["bar-doc/a.java"],
37 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
38 api_levels_annotations_enabled: true,
39 }
40
41 droidstubs {
42 name: "bar-stubs-other",
43 srcs: ["bar-doc/a.java"],
44 high_mem: true,
45 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
46 api_levels_annotations_enabled: true,
47 api_levels_jar_filename: "android.other.jar",
48 }
49 `,
50 map[string][]byte{
51 "bar-doc/a.java": nil,
52 })
53 testcases := []struct {
54 moduleName string
55 expectedJarFilename string
56 high_mem bool
57 }{
58 {
59 moduleName: "bar-stubs",
60 expectedJarFilename: "android.jar",
61 high_mem: false,
62 },
63 {
64 moduleName: "bar-stubs-other",
65 expectedJarFilename: "android.other.jar",
66 high_mem: true,
67 },
68 }
69 for _, c := range testcases {
70 m := ctx.ModuleForTests(c.moduleName, "android_common")
Colin Cross8095c292021-03-30 16:40:48 -070071 manifest := m.Output("metalava.sbox.textproto")
72 sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest)
Colin Cross2207f872021-03-24 12:39:08 -070073 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Colin Cross8095c292021-03-30 16:40:48 -070074 if actual := String(sboxProto.Commands[0].Command); !strings.Contains(actual, expected) {
Colin Cross2207f872021-03-24 12:39:08 -070075 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
76 }
77
Colin Cross8095c292021-03-30 16:40:48 -070078 metalava := m.Rule("metalava")
79 rp := metalava.RuleParams
Colin Cross2207f872021-03-24 12:39:08 -070080 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
81 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
82 }
83 }
84}
85
Pedro Loureirocc203502021-10-04 17:24:00 +000086// runs a test for droidstubs with a customizable sdkType argument and returns
87// the list of jar patterns that is passed as `--android-jar-pattern`
88func getAndroidJarPatternsForDroidstubs(t *testing.T, sdkType string) []string {
89 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
satayev783195c2021-06-23 21:49:57 +010090 droiddoc_exported_dir {
91 name: "some-exported-dir",
92 path: "somedir",
93 }
94
95 droiddoc_exported_dir {
96 name: "some-other-exported-dir",
97 path: "someotherdir",
98 }
99
100 droidstubs {
101 name: "foo-stubs",
102 srcs: ["foo-doc/a.java"],
103 api_levels_annotations_dirs: [
104 "some-exported-dir",
105 "some-other-exported-dir",
106 ],
107 api_levels_annotations_enabled: true,
Pedro Loureirocc203502021-10-04 17:24:00 +0000108 api_levels_sdk_type: "%s",
satayev783195c2021-06-23 21:49:57 +0100109 }
Pedro Loureirocc203502021-10-04 17:24:00 +0000110 `, sdkType),
satayev783195c2021-06-23 21:49:57 +0100111 map[string][]byte{
112 "foo-doc/a.java": nil,
113 })
114
115 m := ctx.ModuleForTests("foo-stubs", "android_common")
116 manifest := m.Output("metalava.sbox.textproto")
117 cmd := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command)
118 r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`)
Pedro Loureirocc203502021-10-04 17:24:00 +0000119 return r.FindAllString(cmd, -1)
120}
121
122func TestPublicDroidstubs(t *testing.T) {
123 patterns := getAndroidJarPatternsForDroidstubs(t, "public")
124
125 android.AssertArrayString(t, "order of patterns", []string{
126 "--android-jar-pattern somedir/%/public/android.jar",
127 "--android-jar-pattern someotherdir/%/public/android.jar",
128 }, patterns)
129}
130
131func TestSystemDroidstubs(t *testing.T) {
132 patterns := getAndroidJarPatternsForDroidstubs(t, "system")
133
satayev783195c2021-06-23 21:49:57 +0100134 android.AssertArrayString(t, "order of patterns", []string{
135 "--android-jar-pattern somedir/%/system/android.jar",
136 "--android-jar-pattern someotherdir/%/system/android.jar",
137 "--android-jar-pattern somedir/%/public/android.jar",
138 "--android-jar-pattern someotherdir/%/public/android.jar",
Pedro Loureirocc203502021-10-04 17:24:00 +0000139 }, patterns)
140}
141
142func TestModuleLibDroidstubs(t *testing.T) {
143 patterns := getAndroidJarPatternsForDroidstubs(t, "module-lib")
144
145 android.AssertArrayString(t, "order of patterns", []string{
146 "--android-jar-pattern somedir/%/module-lib/android.jar",
147 "--android-jar-pattern someotherdir/%/module-lib/android.jar",
148 "--android-jar-pattern somedir/%/system/android.jar",
149 "--android-jar-pattern someotherdir/%/system/android.jar",
150 "--android-jar-pattern somedir/%/public/android.jar",
151 "--android-jar-pattern someotherdir/%/public/android.jar",
152 }, patterns)
satayev783195c2021-06-23 21:49:57 +0100153}
154
Colin Cross6aa5c402021-03-24 12:28:50 -0700155func TestDroidstubsSandbox(t *testing.T) {
156 ctx, _ := testJavaWithFS(t, `
Colin Crossbc139922021-03-25 18:33:16 -0700157 genrule {
158 name: "foo",
159 out: ["foo.txt"],
160 cmd: "touch $(out)",
161 }
162
Colin Cross6aa5c402021-03-24 12:28:50 -0700163 droidstubs {
164 name: "bar-stubs",
165 srcs: ["bar-doc/a.java"],
Colin Crossbc139922021-03-25 18:33:16 -0700166
167 args: "--reference $(location :foo)",
168 arg_files: [":foo"],
Colin Cross6aa5c402021-03-24 12:28:50 -0700169 }
170 `,
171 map[string][]byte{
172 "bar-doc/a.java": nil,
173 })
174
175 m := ctx.ModuleForTests("bar-stubs", "android_common")
176 metalava := m.Rule("metalava")
177 if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
178 t.Errorf("Expected inputs %q, got %q", w, g)
179 }
Colin Crossbc139922021-03-25 18:33:16 -0700180
181 manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto"))
182 if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
183 t.Errorf("Expected command to contain %q, got %q", w, g)
184 }
Colin Cross6aa5c402021-03-24 12:28:50 -0700185}
186
Colin Cross2207f872021-03-24 12:39:08 -0700187func TestDroidstubsWithSystemModules(t *testing.T) {
188 ctx, _ := testJava(t, `
189 droidstubs {
190 name: "stubs-source-system-modules",
191 srcs: [
192 "bar-doc/a.java",
193 ],
194 sdk_version: "none",
195 system_modules: "source-system-modules",
196 }
197
198 java_library {
199 name: "source-jar",
200 srcs: [
201 "a.java",
202 ],
203 }
204
205 java_system_modules {
206 name: "source-system-modules",
207 libs: ["source-jar"],
208 }
209
210 droidstubs {
211 name: "stubs-prebuilt-system-modules",
212 srcs: [
213 "bar-doc/a.java",
214 ],
215 sdk_version: "none",
216 system_modules: "prebuilt-system-modules",
217 }
218
219 java_import {
220 name: "prebuilt-jar",
221 jars: ["a.jar"],
222 }
223
224 java_system_modules_import {
225 name: "prebuilt-system-modules",
226 libs: ["prebuilt-jar"],
227 }
228 `)
229
230 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
231
232 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
233}
234
Colin Cross2207f872021-03-24 12:39:08 -0700235func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
236 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
237 var systemJars []string
238 for _, i := range metalavaRule.Implicits {
239 systemJars = append(systemJars, i.Base())
240 }
241 if len(systemJars) < 1 || systemJars[0] != systemJar {
242 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
243 }
244}