blob: db664c15e82cfc64a1cb3dc50dc223c2320900d9 [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")
Colin Cross8095c292021-03-30 16:40:48 -070069 manifest := m.Output("metalava.sbox.textproto")
70 sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest)
Colin Cross2207f872021-03-24 12:39:08 -070071 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Colin Cross8095c292021-03-30 16:40:48 -070072 if actual := String(sboxProto.Commands[0].Command); !strings.Contains(actual, expected) {
Colin Cross2207f872021-03-24 12:39:08 -070073 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
74 }
75
Colin Cross8095c292021-03-30 16:40:48 -070076 metalava := m.Rule("metalava")
77 rp := metalava.RuleParams
Colin Cross2207f872021-03-24 12:39:08 -070078 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"],
Colin Crossbc139922021-03-25 18:33:16 -070095
96 args: "--reference $(location :foo)",
97 arg_files: [":foo"],
Colin Cross6aa5c402021-03-24 12:28:50 -070098 }
99 `,
100 map[string][]byte{
101 "bar-doc/a.java": nil,
102 })
103
104 m := ctx.ModuleForTests("bar-stubs", "android_common")
105 metalava := m.Rule("metalava")
106 if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
107 t.Errorf("Expected inputs %q, got %q", w, g)
108 }
Colin Crossbc139922021-03-25 18:33:16 -0700109
110 manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto"))
111 if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
112 t.Errorf("Expected command to contain %q, got %q", w, g)
113 }
Colin Cross6aa5c402021-03-24 12:28:50 -0700114}
115
Colin Cross2207f872021-03-24 12:39:08 -0700116func TestDroidstubsWithSystemModules(t *testing.T) {
117 ctx, _ := testJava(t, `
118 droidstubs {
119 name: "stubs-source-system-modules",
120 srcs: [
121 "bar-doc/a.java",
122 ],
123 sdk_version: "none",
124 system_modules: "source-system-modules",
125 }
126
127 java_library {
128 name: "source-jar",
129 srcs: [
130 "a.java",
131 ],
132 }
133
134 java_system_modules {
135 name: "source-system-modules",
136 libs: ["source-jar"],
137 }
138
139 droidstubs {
140 name: "stubs-prebuilt-system-modules",
141 srcs: [
142 "bar-doc/a.java",
143 ],
144 sdk_version: "none",
145 system_modules: "prebuilt-system-modules",
146 }
147
148 java_import {
149 name: "prebuilt-jar",
150 jars: ["a.jar"],
151 }
152
153 java_system_modules_import {
154 name: "prebuilt-system-modules",
155 libs: ["prebuilt-jar"],
156 }
157 `)
158
159 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
160
161 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
162}
163
164func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
165 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
166 var systemJars []string
167 for _, i := range metalavaRule.Implicits {
168 systemJars = append(systemJars, i.Base())
169 }
170 if len(systemJars) < 1 || systemJars[0] != systemJar {
171 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
172 }
173}