blob: 60d0bea5d166c34fa88a5fc797a22fdefcfce3b9 [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 (
Colin Cross6aa5c402021-03-24 12:28:50 -070018 "reflect"
satayev783195c2021-06-23 21:49:57 +010019 "regexp"
Colin Cross2207f872021-03-24 12:39:08 -070020 "strings"
21 "testing"
22
23 "android/soong/android"
24)
25
26func TestDroidstubs(t *testing.T) {
27 ctx, _ := testJavaWithFS(t, `
28 droiddoc_exported_dir {
29 name: "droiddoc-templates-sdk",
30 path: ".",
31 }
32
33 droidstubs {
34 name: "bar-stubs",
35 srcs: ["bar-doc/a.java"],
36 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
37 api_levels_annotations_enabled: true,
38 }
39
40 droidstubs {
41 name: "bar-stubs-other",
42 srcs: ["bar-doc/a.java"],
43 high_mem: true,
44 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
45 api_levels_annotations_enabled: true,
46 api_levels_jar_filename: "android.other.jar",
47 }
48 `,
49 map[string][]byte{
50 "bar-doc/a.java": nil,
51 })
52 testcases := []struct {
53 moduleName string
54 expectedJarFilename string
55 high_mem bool
56 }{
57 {
58 moduleName: "bar-stubs",
59 expectedJarFilename: "android.jar",
60 high_mem: false,
61 },
62 {
63 moduleName: "bar-stubs-other",
64 expectedJarFilename: "android.other.jar",
65 high_mem: true,
66 },
67 }
68 for _, c := range testcases {
69 m := ctx.ModuleForTests(c.moduleName, "android_common")
Colin Cross8095c292021-03-30 16:40:48 -070070 manifest := m.Output("metalava.sbox.textproto")
71 sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest)
Colin Cross2207f872021-03-24 12:39:08 -070072 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Colin Cross8095c292021-03-30 16:40:48 -070073 if actual := String(sboxProto.Commands[0].Command); !strings.Contains(actual, expected) {
Colin Cross2207f872021-03-24 12:39:08 -070074 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
75 }
76
Colin Cross8095c292021-03-30 16:40:48 -070077 metalava := m.Rule("metalava")
78 rp := metalava.RuleParams
Colin Cross2207f872021-03-24 12:39:08 -070079 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
80 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
81 }
82 }
83}
84
satayev783195c2021-06-23 21:49:57 +010085func TestSystemDroidstubs(t *testing.T) {
86 ctx, _ := testJavaWithFS(t, `
87 droiddoc_exported_dir {
88 name: "some-exported-dir",
89 path: "somedir",
90 }
91
92 droiddoc_exported_dir {
93 name: "some-other-exported-dir",
94 path: "someotherdir",
95 }
96
97 droidstubs {
98 name: "foo-stubs",
99 srcs: ["foo-doc/a.java"],
100 api_levels_annotations_dirs: [
101 "some-exported-dir",
102 "some-other-exported-dir",
103 ],
104 api_levels_annotations_enabled: true,
105 api_levels_sdk_type: "system",
106 }
107 `,
108 map[string][]byte{
109 "foo-doc/a.java": nil,
110 })
111
112 m := ctx.ModuleForTests("foo-stubs", "android_common")
113 manifest := m.Output("metalava.sbox.textproto")
114 cmd := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command)
115 r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`)
116 matches := r.FindAllString(cmd, -1)
117 android.AssertArrayString(t, "order of patterns", []string{
118 "--android-jar-pattern somedir/%/system/android.jar",
119 "--android-jar-pattern someotherdir/%/system/android.jar",
120 "--android-jar-pattern somedir/%/public/android.jar",
121 "--android-jar-pattern someotherdir/%/public/android.jar",
122 }, matches)
123}
124
Colin Cross6aa5c402021-03-24 12:28:50 -0700125func TestDroidstubsSandbox(t *testing.T) {
126 ctx, _ := testJavaWithFS(t, `
Colin Crossbc139922021-03-25 18:33:16 -0700127 genrule {
128 name: "foo",
129 out: ["foo.txt"],
130 cmd: "touch $(out)",
131 }
132
Colin Cross6aa5c402021-03-24 12:28:50 -0700133 droidstubs {
134 name: "bar-stubs",
135 srcs: ["bar-doc/a.java"],
Colin Crossbc139922021-03-25 18:33:16 -0700136
137 args: "--reference $(location :foo)",
138 arg_files: [":foo"],
Colin Cross6aa5c402021-03-24 12:28:50 -0700139 }
140 `,
141 map[string][]byte{
142 "bar-doc/a.java": nil,
143 })
144
145 m := ctx.ModuleForTests("bar-stubs", "android_common")
146 metalava := m.Rule("metalava")
147 if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
148 t.Errorf("Expected inputs %q, got %q", w, g)
149 }
Colin Crossbc139922021-03-25 18:33:16 -0700150
151 manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto"))
152 if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
153 t.Errorf("Expected command to contain %q, got %q", w, g)
154 }
Colin Cross6aa5c402021-03-24 12:28:50 -0700155}
156
Colin Cross2207f872021-03-24 12:39:08 -0700157func TestDroidstubsWithSystemModules(t *testing.T) {
158 ctx, _ := testJava(t, `
159 droidstubs {
160 name: "stubs-source-system-modules",
161 srcs: [
162 "bar-doc/a.java",
163 ],
164 sdk_version: "none",
165 system_modules: "source-system-modules",
166 }
167
168 java_library {
169 name: "source-jar",
170 srcs: [
171 "a.java",
172 ],
173 }
174
175 java_system_modules {
176 name: "source-system-modules",
177 libs: ["source-jar"],
178 }
179
180 droidstubs {
181 name: "stubs-prebuilt-system-modules",
182 srcs: [
183 "bar-doc/a.java",
184 ],
185 sdk_version: "none",
186 system_modules: "prebuilt-system-modules",
187 }
188
189 java_import {
190 name: "prebuilt-jar",
191 jars: ["a.jar"],
192 }
193
194 java_system_modules_import {
195 name: "prebuilt-system-modules",
196 libs: ["prebuilt-jar"],
197 }
198 `)
199
200 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
201
202 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
203}
204
205func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
206 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
207 var systemJars []string
208 for _, i := range metalavaRule.Implicits {
209 systemJars = append(systemJars, i.Base())
210 }
211 if len(systemJars) < 1 || systemJars[0] != systemJar {
212 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
213 }
214}