blob: d450c1a805f66f0cd32461d3bc6f8186842b719a [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 (
18 "strings"
19 "testing"
20
21 "android/soong/android"
22)
23
24func TestDroidstubs(t *testing.T) {
25 ctx, _ := testJavaWithFS(t, `
26 droiddoc_exported_dir {
27 name: "droiddoc-templates-sdk",
28 path: ".",
29 }
30
31 droidstubs {
32 name: "bar-stubs",
33 srcs: ["bar-doc/a.java"],
34 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
35 api_levels_annotations_enabled: true,
36 }
37
38 droidstubs {
39 name: "bar-stubs-other",
40 srcs: ["bar-doc/a.java"],
41 high_mem: true,
42 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
43 api_levels_annotations_enabled: true,
44 api_levels_jar_filename: "android.other.jar",
45 }
46 `,
47 map[string][]byte{
48 "bar-doc/a.java": nil,
49 })
50 testcases := []struct {
51 moduleName string
52 expectedJarFilename string
53 high_mem bool
54 }{
55 {
56 moduleName: "bar-stubs",
57 expectedJarFilename: "android.jar",
58 high_mem: false,
59 },
60 {
61 moduleName: "bar-stubs-other",
62 expectedJarFilename: "android.other.jar",
63 high_mem: true,
64 },
65 }
66 for _, c := range testcases {
67 m := ctx.ModuleForTests(c.moduleName, "android_common")
68 metalava := m.Rule("metalava")
69 rp := metalava.RuleParams
70 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
71 if actual := rp.Command; !strings.Contains(actual, expected) {
72 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
73 }
74
75 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
76 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
77 }
78 }
79}
80
81func TestDroidstubsWithSystemModules(t *testing.T) {
82 ctx, _ := testJava(t, `
83 droidstubs {
84 name: "stubs-source-system-modules",
85 srcs: [
86 "bar-doc/a.java",
87 ],
88 sdk_version: "none",
89 system_modules: "source-system-modules",
90 }
91
92 java_library {
93 name: "source-jar",
94 srcs: [
95 "a.java",
96 ],
97 }
98
99 java_system_modules {
100 name: "source-system-modules",
101 libs: ["source-jar"],
102 }
103
104 droidstubs {
105 name: "stubs-prebuilt-system-modules",
106 srcs: [
107 "bar-doc/a.java",
108 ],
109 sdk_version: "none",
110 system_modules: "prebuilt-system-modules",
111 }
112
113 java_import {
114 name: "prebuilt-jar",
115 jars: ["a.jar"],
116 }
117
118 java_system_modules_import {
119 name: "prebuilt-system-modules",
120 libs: ["prebuilt-jar"],
121 }
122 `)
123
124 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
125
126 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
127}
128
129func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
130 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
131 var systemJars []string
132 for _, i := range metalavaRule.Implicits {
133 systemJars = append(systemJars, i.Base())
134 }
135 if len(systemJars) < 1 || systemJars[0] != systemJar {
136 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
137 }
138}