123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- // Copyright 2021 Google Inc. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package java
- import (
- "fmt"
- "reflect"
- "regexp"
- "strings"
- "testing"
- "android/soong/android"
- )
- func TestDroidstubs(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- droiddoc_exported_dir {
- name: "droiddoc-templates-sdk",
- path: ".",
- }
- droidstubs {
- name: "bar-stubs",
- srcs: ["bar-doc/a.java"],
- api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
- api_levels_annotations_enabled: true,
- }
- droidstubs {
- name: "bar-stubs-other",
- srcs: ["bar-doc/a.java"],
- high_mem: true,
- api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
- api_levels_annotations_enabled: true,
- api_levels_jar_filename: "android.other.jar",
- }
- droidstubs {
- name: "stubs-applying-api-versions",
- srcs: ["bar-doc/a.java"],
- api_levels_module: "bar-stubs-other",
- }
- `,
- map[string][]byte{
- "bar-doc/a.java": nil,
- })
- testcases := []struct {
- moduleName string
- expectedJarFilename string
- generate_xml bool
- high_mem bool
- }{
- {
- moduleName: "bar-stubs",
- generate_xml: true,
- expectedJarFilename: "android.jar",
- high_mem: false,
- },
- {
- moduleName: "bar-stubs-other",
- generate_xml: true,
- expectedJarFilename: "android.other.jar",
- high_mem: true,
- },
- {
- moduleName: "stubs-applying-api-versions",
- generate_xml: false,
- },
- }
- for _, c := range testcases {
- m := ctx.ModuleForTests(c.moduleName, "android_common")
- manifest := m.Output("metalava.sbox.textproto")
- sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest)
- cmdline := String(sboxProto.Commands[0].Command)
- android.AssertStringContainsEquals(t, "api-versions generation flag", cmdline, "--generate-api-levels", c.generate_xml)
- if c.expectedJarFilename != "" {
- expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
- if !strings.Contains(cmdline, expected) {
- t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, cmdline)
- }
- }
- metalava := m.Rule("metalava")
- rp := metalava.RuleParams
- if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
- t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
- }
- }
- }
- // runs a test for droidstubs with a customizable sdkType argument and returns
- // the list of jar patterns that is passed as `--android-jar-pattern`
- func getAndroidJarPatternsForDroidstubs(t *testing.T, sdkType string) []string {
- ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
- droiddoc_exported_dir {
- name: "some-exported-dir",
- path: "somedir",
- }
- droiddoc_exported_dir {
- name: "some-other-exported-dir",
- path: "someotherdir",
- }
- droidstubs {
- name: "foo-stubs",
- srcs: ["foo-doc/a.java"],
- api_levels_annotations_dirs: [
- "some-exported-dir",
- "some-other-exported-dir",
- ],
- api_levels_annotations_enabled: true,
- api_levels_sdk_type: "%s",
- }
- `, sdkType),
- map[string][]byte{
- "foo-doc/a.java": nil,
- })
- m := ctx.ModuleForTests("foo-stubs", "android_common")
- manifest := m.Output("metalava.sbox.textproto")
- cmd := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command)
- r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`)
- return r.FindAllString(cmd, -1)
- }
- func TestPublicDroidstubs(t *testing.T) {
- patterns := getAndroidJarPatternsForDroidstubs(t, "public")
- android.AssertArrayString(t, "order of patterns", []string{
- "--android-jar-pattern somedir/%/public/android.jar",
- "--android-jar-pattern someotherdir/%/public/android.jar",
- }, patterns)
- }
- func TestSystemDroidstubs(t *testing.T) {
- patterns := getAndroidJarPatternsForDroidstubs(t, "system")
- android.AssertArrayString(t, "order of patterns", []string{
- "--android-jar-pattern somedir/%/system/android.jar",
- "--android-jar-pattern someotherdir/%/system/android.jar",
- "--android-jar-pattern somedir/%/public/android.jar",
- "--android-jar-pattern someotherdir/%/public/android.jar",
- }, patterns)
- }
- func TestModuleLibDroidstubs(t *testing.T) {
- patterns := getAndroidJarPatternsForDroidstubs(t, "module-lib")
- android.AssertArrayString(t, "order of patterns", []string{
- "--android-jar-pattern somedir/%/module-lib/android.jar",
- "--android-jar-pattern someotherdir/%/module-lib/android.jar",
- "--android-jar-pattern somedir/%/system/android.jar",
- "--android-jar-pattern someotherdir/%/system/android.jar",
- "--android-jar-pattern somedir/%/public/android.jar",
- "--android-jar-pattern someotherdir/%/public/android.jar",
- }, patterns)
- }
- func TestSystemServerDroidstubs(t *testing.T) {
- patterns := getAndroidJarPatternsForDroidstubs(t, "system-server")
- android.AssertArrayString(t, "order of patterns", []string{
- "--android-jar-pattern somedir/%/system-server/android.jar",
- "--android-jar-pattern someotherdir/%/system-server/android.jar",
- "--android-jar-pattern somedir/%/module-lib/android.jar",
- "--android-jar-pattern someotherdir/%/module-lib/android.jar",
- "--android-jar-pattern somedir/%/system/android.jar",
- "--android-jar-pattern someotherdir/%/system/android.jar",
- "--android-jar-pattern somedir/%/public/android.jar",
- "--android-jar-pattern someotherdir/%/public/android.jar",
- }, patterns)
- }
- func TestDroidstubsSandbox(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- genrule {
- name: "foo",
- out: ["foo.txt"],
- cmd: "touch $(out)",
- }
- droidstubs {
- name: "bar-stubs",
- srcs: ["bar-doc/a.java"],
- args: "--reference $(location :foo)",
- arg_files: [":foo"],
- }
- `,
- map[string][]byte{
- "bar-doc/a.java": nil,
- })
- m := ctx.ModuleForTests("bar-stubs", "android_common")
- metalava := m.Rule("metalava")
- if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
- t.Errorf("Expected inputs %q, got %q", w, g)
- }
- manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto"))
- if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
- t.Errorf("Expected command to contain %q, got %q", w, g)
- }
- }
- func TestDroidstubsWithSystemModules(t *testing.T) {
- ctx, _ := testJava(t, `
- droidstubs {
- name: "stubs-source-system-modules",
- srcs: [
- "bar-doc/a.java",
- ],
- sdk_version: "none",
- system_modules: "source-system-modules",
- }
- java_library {
- name: "source-jar",
- srcs: [
- "a.java",
- ],
- }
- java_system_modules {
- name: "source-system-modules",
- libs: ["source-jar"],
- }
- droidstubs {
- name: "stubs-prebuilt-system-modules",
- srcs: [
- "bar-doc/a.java",
- ],
- sdk_version: "none",
- system_modules: "prebuilt-system-modules",
- }
- java_import {
- name: "prebuilt-jar",
- jars: ["a.jar"],
- }
- java_system_modules_import {
- name: "prebuilt-system-modules",
- libs: ["prebuilt-jar"],
- }
- `)
- checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
- checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
- }
- func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
- metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
- var systemJars []string
- for _, i := range metalavaRule.Implicits {
- systemJars = append(systemJars, i.Base())
- }
- if len(systemJars) < 1 || systemJars[0] != systemJar {
- t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
- }
- }
- func TestDroidstubsWithSdkExtensions(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- droiddoc_exported_dir {
- name: "sdk-dir",
- path: "sdk",
- }
- droidstubs {
- name: "baz-stubs",
- api_levels_annotations_dirs: ["sdk-dir"],
- api_levels_annotations_enabled: true,
- extensions_info_file: ":info-file",
- }
- filegroup {
- name: "info-file",
- srcs: ["sdk/extensions/info.txt"],
- }
- `,
- map[string][]byte{
- "sdk/extensions/1/public/some-mainline-module-stubs.jar": nil,
- "sdk/extensions/info.txt": nil,
- })
- m := ctx.ModuleForTests("baz-stubs", "android_common")
- manifest := m.Output("metalava.sbox.textproto")
- cmdline := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command)
- android.AssertStringDoesContain(t, "sdk-extensions-root present", cmdline, "--sdk-extensions-root sdk/extensions")
- android.AssertStringDoesContain(t, "sdk-extensions-info present", cmdline, "--sdk-extensions-info sdk/extensions/info.txt")
- }
- func TestApiSurfaceFromDroidStubsName(t *testing.T) {
- testCases := []struct {
- desc string
- name string
- expectedApiSurface string
- }{
- {
- desc: "Default is publicapi",
- name: "mydroidstubs",
- expectedApiSurface: "publicapi",
- },
- {
- desc: "name contains system substring",
- name: "mydroidstubs.system.suffix",
- expectedApiSurface: "systemapi",
- },
- {
- desc: "name contains system_server substring",
- name: "mydroidstubs.system_server.suffix",
- expectedApiSurface: "system-serverapi",
- },
- {
- desc: "name contains module_lib substring",
- name: "mydroidstubs.module_lib.suffix",
- expectedApiSurface: "module-libapi",
- },
- {
- desc: "name contains test substring",
- name: "mydroidstubs.test.suffix",
- expectedApiSurface: "testapi",
- },
- {
- desc: "name contains intra.core substring",
- name: "mydroidstubs.intra.core.suffix",
- expectedApiSurface: "intracoreapi",
- },
- }
- for _, tc := range testCases {
- android.AssertStringEquals(t, tc.desc, tc.expectedApiSurface, bazelApiSurfaceName(tc.name))
- }
- }
- func TestDroidStubsApiContributionGeneration(t *testing.T) {
- ctx, _ := testJavaWithFS(t, `
- droidstubs {
- name: "foo",
- srcs: ["A/a.java"],
- api_surface: "public",
- check_api: {
- current: {
- api_file: "A/current.txt",
- removed_api_file: "A/removed.txt",
- }
- }
- }
- `,
- map[string][]byte{
- "A/a.java": nil,
- "A/current.txt": nil,
- "A/removed.txt": nil,
- },
- )
- ctx.ModuleForTests("foo.api.contribution", "")
- }
- func TestGeneratedApiContributionVisibilityTest(t *testing.T) {
- library_bp := `
- java_api_library {
- name: "bar",
- api_surface: "public",
- api_contributions: ["foo.api.contribution"],
- }
- `
- ctx, _ := testJavaWithFS(t, `
- droidstubs {
- name: "foo",
- srcs: ["A/a.java"],
- api_surface: "public",
- check_api: {
- current: {
- api_file: "A/current.txt",
- removed_api_file: "A/removed.txt",
- }
- },
- visibility: ["//a"],
- }
- `,
- map[string][]byte{
- "a/a.java": nil,
- "a/current.txt": nil,
- "a/removed.txt": nil,
- "b/Android.bp": []byte(library_bp),
- },
- )
- ctx.ModuleForTests("bar", "android_common")
- }
|