1// 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 (
18	"reflect"
19	"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		manifest := m.Output("metalava.sbox.textproto")
70		sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest)
71		expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
72		if actual := String(sboxProto.Commands[0].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		metalava := m.Rule("metalava")
77		rp := metalava.RuleParams
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
84func TestDroidstubsSandbox(t *testing.T) {
85	ctx, _ := testJavaWithFS(t, `
86		genrule {
87			name: "foo",
88			out: ["foo.txt"],
89			cmd: "touch $(out)",
90		}
91
92		droidstubs {
93			name: "bar-stubs",
94			srcs: ["bar-doc/a.java"],
95
96			args: "--reference $(location :foo)",
97			arg_files: [":foo"],
98		}
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	}
109
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	}
114}
115
116func 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}
174