Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 6aa5c40 | 2021-03-24 12:28:50 -0700 | [diff] [blame] | 19 | "reflect" |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 20 | "regexp" |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | "testing" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | func TestDroidstubs(t *testing.T) { |
| 28 | ctx, _ := testJavaWithFS(t, ` |
| 29 | droiddoc_exported_dir { |
| 30 | name: "droiddoc-templates-sdk", |
| 31 | path: ".", |
| 32 | } |
| 33 | |
| 34 | droidstubs { |
| 35 | name: "bar-stubs", |
| 36 | srcs: ["bar-doc/a.java"], |
| 37 | api_levels_annotations_dirs: ["droiddoc-templates-sdk"], |
| 38 | api_levels_annotations_enabled: true, |
| 39 | } |
| 40 | |
| 41 | droidstubs { |
| 42 | name: "bar-stubs-other", |
| 43 | srcs: ["bar-doc/a.java"], |
| 44 | high_mem: true, |
| 45 | api_levels_annotations_dirs: ["droiddoc-templates-sdk"], |
| 46 | api_levels_annotations_enabled: true, |
| 47 | api_levels_jar_filename: "android.other.jar", |
| 48 | } |
| 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") |
Colin Cross | 8095c29 | 2021-03-30 16:40:48 -0700 | [diff] [blame] | 71 | manifest := m.Output("metalava.sbox.textproto") |
| 72 | sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 73 | expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename |
Colin Cross | 8095c29 | 2021-03-30 16:40:48 -0700 | [diff] [blame] | 74 | if actual := String(sboxProto.Commands[0].Command); !strings.Contains(actual, expected) { |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 75 | t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual) |
| 76 | } |
| 77 | |
Colin Cross | 8095c29 | 2021-03-30 16:40:48 -0700 | [diff] [blame] | 78 | metalava := m.Rule("metalava") |
| 79 | rp := metalava.RuleParams |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 80 | if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem { |
| 81 | t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 86 | // runs a test for droidstubs with a customizable sdkType argument and returns |
| 87 | // the list of jar patterns that is passed as `--android-jar-pattern` |
| 88 | func getAndroidJarPatternsForDroidstubs(t *testing.T, sdkType string) []string { |
| 89 | ctx, _ := testJavaWithFS(t, fmt.Sprintf(` |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 90 | droiddoc_exported_dir { |
| 91 | name: "some-exported-dir", |
| 92 | path: "somedir", |
| 93 | } |
| 94 | |
| 95 | droiddoc_exported_dir { |
| 96 | name: "some-other-exported-dir", |
| 97 | path: "someotherdir", |
| 98 | } |
| 99 | |
| 100 | droidstubs { |
| 101 | name: "foo-stubs", |
| 102 | srcs: ["foo-doc/a.java"], |
| 103 | api_levels_annotations_dirs: [ |
| 104 | "some-exported-dir", |
| 105 | "some-other-exported-dir", |
| 106 | ], |
| 107 | api_levels_annotations_enabled: true, |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 108 | api_levels_sdk_type: "%s", |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 109 | } |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 110 | `, sdkType), |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 111 | map[string][]byte{ |
| 112 | "foo-doc/a.java": nil, |
| 113 | }) |
| 114 | |
| 115 | m := ctx.ModuleForTests("foo-stubs", "android_common") |
| 116 | manifest := m.Output("metalava.sbox.textproto") |
| 117 | cmd := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command) |
| 118 | r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`) |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 119 | return r.FindAllString(cmd, -1) |
| 120 | } |
| 121 | |
| 122 | func TestPublicDroidstubs(t *testing.T) { |
| 123 | patterns := getAndroidJarPatternsForDroidstubs(t, "public") |
| 124 | |
| 125 | android.AssertArrayString(t, "order of patterns", []string{ |
| 126 | "--android-jar-pattern somedir/%/public/android.jar", |
| 127 | "--android-jar-pattern someotherdir/%/public/android.jar", |
| 128 | }, patterns) |
| 129 | } |
| 130 | |
| 131 | func TestSystemDroidstubs(t *testing.T) { |
| 132 | patterns := getAndroidJarPatternsForDroidstubs(t, "system") |
| 133 | |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 134 | android.AssertArrayString(t, "order of patterns", []string{ |
| 135 | "--android-jar-pattern somedir/%/system/android.jar", |
| 136 | "--android-jar-pattern someotherdir/%/system/android.jar", |
| 137 | "--android-jar-pattern somedir/%/public/android.jar", |
| 138 | "--android-jar-pattern someotherdir/%/public/android.jar", |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 139 | }, patterns) |
| 140 | } |
| 141 | |
| 142 | func TestModuleLibDroidstubs(t *testing.T) { |
| 143 | patterns := getAndroidJarPatternsForDroidstubs(t, "module-lib") |
| 144 | |
| 145 | android.AssertArrayString(t, "order of patterns", []string{ |
| 146 | "--android-jar-pattern somedir/%/module-lib/android.jar", |
| 147 | "--android-jar-pattern someotherdir/%/module-lib/android.jar", |
| 148 | "--android-jar-pattern somedir/%/system/android.jar", |
| 149 | "--android-jar-pattern someotherdir/%/system/android.jar", |
| 150 | "--android-jar-pattern somedir/%/public/android.jar", |
| 151 | "--android-jar-pattern someotherdir/%/public/android.jar", |
| 152 | }, patterns) |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Colin Cross | 6aa5c40 | 2021-03-24 12:28:50 -0700 | [diff] [blame] | 155 | func TestDroidstubsSandbox(t *testing.T) { |
| 156 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | bc13992 | 2021-03-25 18:33:16 -0700 | [diff] [blame] | 157 | genrule { |
| 158 | name: "foo", |
| 159 | out: ["foo.txt"], |
| 160 | cmd: "touch $(out)", |
| 161 | } |
| 162 | |
Colin Cross | 6aa5c40 | 2021-03-24 12:28:50 -0700 | [diff] [blame] | 163 | droidstubs { |
| 164 | name: "bar-stubs", |
| 165 | srcs: ["bar-doc/a.java"], |
Colin Cross | bc13992 | 2021-03-25 18:33:16 -0700 | [diff] [blame] | 166 | |
| 167 | args: "--reference $(location :foo)", |
| 168 | arg_files: [":foo"], |
Colin Cross | 6aa5c40 | 2021-03-24 12:28:50 -0700 | [diff] [blame] | 169 | } |
| 170 | `, |
| 171 | map[string][]byte{ |
| 172 | "bar-doc/a.java": nil, |
| 173 | }) |
| 174 | |
| 175 | m := ctx.ModuleForTests("bar-stubs", "android_common") |
| 176 | metalava := m.Rule("metalava") |
| 177 | if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) { |
| 178 | t.Errorf("Expected inputs %q, got %q", w, g) |
| 179 | } |
Colin Cross | bc13992 | 2021-03-25 18:33:16 -0700 | [diff] [blame] | 180 | |
| 181 | manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto")) |
| 182 | if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) { |
| 183 | t.Errorf("Expected command to contain %q, got %q", w, g) |
| 184 | } |
Colin Cross | 6aa5c40 | 2021-03-24 12:28:50 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 187 | func TestDroidstubsWithSystemModules(t *testing.T) { |
| 188 | ctx, _ := testJava(t, ` |
| 189 | droidstubs { |
| 190 | name: "stubs-source-system-modules", |
| 191 | srcs: [ |
| 192 | "bar-doc/a.java", |
| 193 | ], |
| 194 | sdk_version: "none", |
| 195 | system_modules: "source-system-modules", |
| 196 | } |
| 197 | |
| 198 | java_library { |
| 199 | name: "source-jar", |
| 200 | srcs: [ |
| 201 | "a.java", |
| 202 | ], |
| 203 | } |
| 204 | |
| 205 | java_system_modules { |
| 206 | name: "source-system-modules", |
| 207 | libs: ["source-jar"], |
| 208 | } |
| 209 | |
| 210 | droidstubs { |
| 211 | name: "stubs-prebuilt-system-modules", |
| 212 | srcs: [ |
| 213 | "bar-doc/a.java", |
| 214 | ], |
| 215 | sdk_version: "none", |
| 216 | system_modules: "prebuilt-system-modules", |
| 217 | } |
| 218 | |
| 219 | java_import { |
| 220 | name: "prebuilt-jar", |
| 221 | jars: ["a.jar"], |
| 222 | } |
| 223 | |
| 224 | java_system_modules_import { |
| 225 | name: "prebuilt-system-modules", |
| 226 | libs: ["prebuilt-jar"], |
| 227 | } |
| 228 | `) |
| 229 | |
| 230 | checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar") |
| 231 | |
| 232 | checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar") |
| 233 | } |
| 234 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 235 | func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) { |
| 236 | metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava") |
| 237 | var systemJars []string |
| 238 | for _, i := range metalavaRule.Implicits { |
| 239 | systemJars = append(systemJars, i.Base()) |
| 240 | } |
| 241 | if len(systemJars) < 1 || systemJars[0] != systemJar { |
| 242 | t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars) |
| 243 | } |
| 244 | } |