blob: f8125fb8c1ce7b741f4e1343372d512a3790d559 [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,
Colin Cross3fbf2be2021-03-25 17:56:29 -070037 sandbox: false,
Colin Cross2207f872021-03-24 12:39:08 -070038 }
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",
Colin Cross3fbf2be2021-03-25 17:56:29 -070047 sandbox: false,
Colin Cross2207f872021-03-24 12:39:08 -070048 }
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")
71 metalava := m.Rule("metalava")
72 rp := metalava.RuleParams
73 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
74 if actual := rp.Command; !strings.Contains(actual, expected) {
75 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
76 }
77
78 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
79 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
80 }
81 }
82}
83
Colin Cross6aa5c402021-03-24 12:28:50 -070084func TestDroidstubsSandbox(t *testing.T) {
85 ctx, _ := testJavaWithFS(t, `
Colin Crossbc139922021-03-25 18:33:16 -070086 genrule {
87 name: "foo",
88 out: ["foo.txt"],
89 cmd: "touch $(out)",
90 }
91
Colin Cross6aa5c402021-03-24 12:28:50 -070092 droidstubs {
93 name: "bar-stubs",
94 srcs: ["bar-doc/a.java"],
95 sandbox: true,
Colin Crossbc139922021-03-25 18:33:16 -070096
97 args: "--reference $(location :foo)",
98 arg_files: [":foo"],
Colin Cross6aa5c402021-03-24 12:28:50 -070099 }
100 `,
101 map[string][]byte{
102 "bar-doc/a.java": nil,
103 })
104
105 m := ctx.ModuleForTests("bar-stubs", "android_common")
106 metalava := m.Rule("metalava")
107 if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
108 t.Errorf("Expected inputs %q, got %q", w, g)
109 }
Colin Crossbc139922021-03-25 18:33:16 -0700110
111 manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto"))
112 if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
113 t.Errorf("Expected command to contain %q, got %q", w, g)
114 }
Colin Cross6aa5c402021-03-24 12:28:50 -0700115}
116
Colin Cross2207f872021-03-24 12:39:08 -0700117func TestDroidstubsWithSystemModules(t *testing.T) {
118 ctx, _ := testJava(t, `
119 droidstubs {
120 name: "stubs-source-system-modules",
121 srcs: [
122 "bar-doc/a.java",
123 ],
124 sdk_version: "none",
125 system_modules: "source-system-modules",
126 }
127
128 java_library {
129 name: "source-jar",
130 srcs: [
131 "a.java",
132 ],
133 }
134
135 java_system_modules {
136 name: "source-system-modules",
137 libs: ["source-jar"],
138 }
139
140 droidstubs {
141 name: "stubs-prebuilt-system-modules",
142 srcs: [
143 "bar-doc/a.java",
144 ],
145 sdk_version: "none",
146 system_modules: "prebuilt-system-modules",
147 }
148
149 java_import {
150 name: "prebuilt-jar",
151 jars: ["a.jar"],
152 }
153
154 java_system_modules_import {
155 name: "prebuilt-system-modules",
156 libs: ["prebuilt-jar"],
157 }
158 `)
159
160 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
161
162 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
163}
164
165func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
166 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
167 var systemJars []string
168 for _, i := range metalavaRule.Implicits {
169 systemJars = append(systemJars, i.Base())
170 }
171 if len(systemJars) < 1 || systemJars[0] != systemJar {
172 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
173 }
174}