blob: 6ad932329897bdcd925da36cae369f3bba36e208 [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"
Colin Cross2207f872021-03-24 12:39:08 -070019 "strings"
20 "testing"
21
22 "android/soong/android"
23)
24
25func TestDroidstubs(t *testing.T) {
26 ctx, _ := testJavaWithFS(t, `
27 droiddoc_exported_dir {
28 name: "droiddoc-templates-sdk",
29 path: ".",
30 }
31
32 droidstubs {
33 name: "bar-stubs",
34 srcs: ["bar-doc/a.java"],
35 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
36 api_levels_annotations_enabled: true,
37 }
38
39 droidstubs {
40 name: "bar-stubs-other",
41 srcs: ["bar-doc/a.java"],
42 high_mem: true,
43 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
44 api_levels_annotations_enabled: true,
45 api_levels_jar_filename: "android.other.jar",
46 }
47 `,
48 map[string][]byte{
49 "bar-doc/a.java": nil,
50 })
51 testcases := []struct {
52 moduleName string
53 expectedJarFilename string
54 high_mem bool
55 }{
56 {
57 moduleName: "bar-stubs",
58 expectedJarFilename: "android.jar",
59 high_mem: false,
60 },
61 {
62 moduleName: "bar-stubs-other",
63 expectedJarFilename: "android.other.jar",
64 high_mem: true,
65 },
66 }
67 for _, c := range testcases {
68 m := ctx.ModuleForTests(c.moduleName, "android_common")
69 metalava := m.Rule("metalava")
70 rp := metalava.RuleParams
71 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
72 if actual := rp.Command; !strings.Contains(actual, expected) {
73 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
74 }
75
76 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
77 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
78 }
79 }
80}
81
Colin Cross6aa5c402021-03-24 12:28:50 -070082func TestDroidstubsSandbox(t *testing.T) {
83 ctx, _ := testJavaWithFS(t, `
Colin Crossbc139922021-03-25 18:33:16 -070084 genrule {
85 name: "foo",
86 out: ["foo.txt"],
87 cmd: "touch $(out)",
88 }
89
Colin Cross6aa5c402021-03-24 12:28:50 -070090 droidstubs {
91 name: "bar-stubs",
92 srcs: ["bar-doc/a.java"],
93 sandbox: true,
Colin Crossbc139922021-03-25 18:33:16 -070094
95 args: "--reference $(location :foo)",
96 arg_files: [":foo"],
Colin Cross6aa5c402021-03-24 12:28:50 -070097 }
98 `,
99 map[string][]byte{
100 "bar-doc/a.java": nil,
101 })
102
103 m := ctx.ModuleForTests("bar-stubs", "android_common")
104 metalava := m.Rule("metalava")
105 if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
106 t.Errorf("Expected inputs %q, got %q", w, g)
107 }
Colin Crossbc139922021-03-25 18:33:16 -0700108
109 manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto"))
110 if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
111 t.Errorf("Expected command to contain %q, got %q", w, g)
112 }
Colin Cross6aa5c402021-03-24 12:28:50 -0700113}
114
Colin Cross2207f872021-03-24 12:39:08 -0700115func TestDroidstubsWithSystemModules(t *testing.T) {
116 ctx, _ := testJava(t, `
117 droidstubs {
118 name: "stubs-source-system-modules",
119 srcs: [
120 "bar-doc/a.java",
121 ],
122 sdk_version: "none",
123 system_modules: "source-system-modules",
124 }
125
126 java_library {
127 name: "source-jar",
128 srcs: [
129 "a.java",
130 ],
131 }
132
133 java_system_modules {
134 name: "source-system-modules",
135 libs: ["source-jar"],
136 }
137
138 droidstubs {
139 name: "stubs-prebuilt-system-modules",
140 srcs: [
141 "bar-doc/a.java",
142 ],
143 sdk_version: "none",
144 system_modules: "prebuilt-system-modules",
145 }
146
147 java_import {
148 name: "prebuilt-jar",
149 jars: ["a.jar"],
150 }
151
152 java_system_modules_import {
153 name: "prebuilt-system-modules",
154 libs: ["prebuilt-jar"],
155 }
156 `)
157
158 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
159
160 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
161}
162
163func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
164 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
165 var systemJars []string
166 for _, i := range metalavaRule.Implicits {
167 systemJars = append(systemJars, i.Base())
168 }
169 if len(systemJars) < 1 || systemJars[0] != systemJar {
170 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
171 }
172}