blob: c6db9790189eac36b65ad65b64c5ced7927dabf3 [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, `
84 droidstubs {
85 name: "bar-stubs",
86 srcs: ["bar-doc/a.java"],
87 sandbox: true,
88 }
89 `,
90 map[string][]byte{
91 "bar-doc/a.java": nil,
92 })
93
94 m := ctx.ModuleForTests("bar-stubs", "android_common")
95 metalava := m.Rule("metalava")
96 if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
97 t.Errorf("Expected inputs %q, got %q", w, g)
98 }
99}
100
Colin Cross2207f872021-03-24 12:39:08 -0700101func TestDroidstubsWithSystemModules(t *testing.T) {
102 ctx, _ := testJava(t, `
103 droidstubs {
104 name: "stubs-source-system-modules",
105 srcs: [
106 "bar-doc/a.java",
107 ],
108 sdk_version: "none",
109 system_modules: "source-system-modules",
110 }
111
112 java_library {
113 name: "source-jar",
114 srcs: [
115 "a.java",
116 ],
117 }
118
119 java_system_modules {
120 name: "source-system-modules",
121 libs: ["source-jar"],
122 }
123
124 droidstubs {
125 name: "stubs-prebuilt-system-modules",
126 srcs: [
127 "bar-doc/a.java",
128 ],
129 sdk_version: "none",
130 system_modules: "prebuilt-system-modules",
131 }
132
133 java_import {
134 name: "prebuilt-jar",
135 jars: ["a.jar"],
136 }
137
138 java_system_modules_import {
139 name: "prebuilt-system-modules",
140 libs: ["prebuilt-jar"],
141 }
142 `)
143
144 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
145
146 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
147}
148
149func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
150 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
151 var systemJars []string
152 for _, i := range metalavaRule.Implicits {
153 systemJars = append(systemJars, i.Base())
154 }
155 if len(systemJars) < 1 || systemJars[0] != systemJar {
156 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
157 }
158}