• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2023 The Android Open Source Project
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"""Analysis tests for android_binary_aosp_internal."""
16
17load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
18load("//build/bazel/rules/android/android_binary_aosp_internal:rule.bzl", "android_binary_aosp_internal")
19load("//build/bazel/rules/cc:cc_library_shared.bzl", "cc_library_shared")
20load("//build/bazel/rules/cc:cc_stub_library.bzl", "cc_stub_library_shared")
21
22def _android_binary_aosp_internal_providers_test_impl(ctx):
23    """Basic analysis test that checks android_binary_aosp_internal returns AndroidBinaryNativeLibsInfo.
24
25    Also checks that the provider contains a list of expected .so files.
26
27    Args:
28        ctx: The analysis test context
29
30    Returns:
31        The analysis test result
32    """
33    env = analysistest.begin(ctx)
34    target = analysistest.target_under_test(env)
35
36    asserts.true(
37        env,
38        AndroidBinaryNativeLibsInfo in target,
39        "AndroidBinaryNativeLibsInfo provider not in the built target.",
40    )
41
42    returned_filename_list = []
43    for lib_depset in target[AndroidBinaryNativeLibsInfo].native_libs.values():
44        returned_filename_list.extend([lib.basename for lib in lib_depset.to_list()])
45    returned_filename_list = sorted(returned_filename_list)
46    expected_filename_list = sorted(ctx.attr.expected_so_files)
47
48    # Check that the expected list of filenames is exactly the same as the provider's list of filenames.
49    asserts.equals(
50        env,
51        expected_filename_list,
52        returned_filename_list,
53    )
54
55    return analysistest.end(env)
56
57android_binary_aosp_internal_providers_test = analysistest.make(
58    _android_binary_aosp_internal_providers_test_impl,
59    attrs = dict(
60        expected_so_files = attr.string_list(),
61    ),
62)
63
64def _test_contains_expected_providers_and_files(name):
65    dummy_app_name = name + "_dummy_app_DO_NOT_USE"
66
67    fake_cc_library_shared_with_map_txt = name + "_fake_cc_library_with_map_txt"
68    cc_library_shared(
69        name = fake_cc_library_shared_with_map_txt,
70        stubs_symbol_file = "fake.map.txt",
71        tags = ["manual"],
72    )
73    fake_cc_stub_library_shared = name + "_fake_cc_stub_library"
74    cc_stub_library_shared(
75        name = fake_cc_stub_library_shared,
76        stubs_symbol_file = "fake.map.txt",
77        source_library_label = ":" + fake_cc_library_shared_with_map_txt,
78        version = "42",
79        export_includes = [],
80        soname = "libfoo.so",
81        deps = [],
82        target_compatible_with = [],
83        features = [],
84        tags = ["manual"],
85        api_surface = "module-libapi",
86    )
87    fake_cc_library_shared = name + "_fake_cc_library_shared"
88    cc_library_shared(
89        name = fake_cc_library_shared,
90        dynamic_deps = [
91            ":" + fake_cc_library_shared_with_map_txt,
92            ":" + fake_cc_stub_library_shared,
93        ],
94        tags = ["manual"],
95    )
96
97    android_binary_aosp_internal(
98        name = dummy_app_name,
99        manifest = "AndroidManifest.xml",
100        deps = [
101            ":" + fake_cc_library_shared,
102        ],
103        srcs = ["fake.java"],
104        tags = ["manual"],
105        sdk_version = "current",
106    )
107
108    android_binary_aosp_internal_providers_test(
109        name = name,
110        target_under_test = ":" + dummy_app_name,
111        # Only expect libc++ and the non-stub cc libraries.
112        expected_so_files = [
113            "libc++.so",
114            fake_cc_library_shared + ".so",
115            fake_cc_library_shared_with_map_txt + ".so",
116        ],
117    )
118    return name
119
120def android_binary_aosp_internal_test_suite(name):
121    native.test_suite(
122        name = name,
123        tests = [
124            _test_contains_expected_providers_and_files(name + "_test_contains_expected_providers_and_files"),
125        ],
126    )
127