1"""Copyright (C) 2023 The Android Open Source Project
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7     http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14"""
15
16load("@bazel_skylib//lib:paths.bzl", "paths")
17load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
18load(":cc_stub_library.bzl", "cc_stub_gen")
19
20_SYMBOL_FILE = "foo.map.txt"
21
22def _get_actual_api_surface_attrs(package, all_args):
23    # These are injected after api map and before symbol_file
24    index_api_map = all_args.index("../soong_injection/api_levels/api_levels.json")
25    index_symbol_file = all_args.index(paths.join(package, _SYMBOL_FILE))
26    return all_args[index_api_map + 1:index_symbol_file]
27
28def _cc_stub_gen_test_impl(ctx):
29    env = analysistest.begin(ctx)
30    actions = analysistest.target_actions(env)
31    asserts.equals(
32        env,
33        len(actions),
34        1,
35        "expected a single cc stubgen action",
36    )
37
38    asserts.equals(
39        env,
40        ctx.attr.expected_api_surface_attrs,
41        _get_actual_api_surface_attrs(ctx.label.package, actions[0].argv),
42    )
43
44    return analysistest.end(env)
45
46cc_stub_gen_test = analysistest.make(
47    _cc_stub_gen_test_impl,
48    attrs = {
49        "expected_api_surface_attrs": attr.string_list(),
50    },
51)
52
53def _test_cc_stub_gen_modulelibapi_with_no_ndk():
54    test_name = "cc_stub_gen_modulelibapi_with_no_ndk"
55    target_under_test_name = test_name + "_target"
56
57    cc_stub_gen(
58        name = target_under_test_name,
59        symbol_file = _SYMBOL_FILE,
60        version = "current",
61        api_surface = "module-libapi",
62        source_library_label = "//:empty",
63        tags = ["manual"],
64    )
65
66    cc_stub_gen_test(
67        name = test_name,
68        target_under_test = target_under_test_name,
69        expected_api_surface_attrs = ["--systemapi", "--apex", "--no-ndk"],
70    )
71
72    return test_name
73
74# This was created from a Soong cc_library with a non-empty stubs.symbol_file
75# There also exists a sibling ndk_library
76def _test_cc_stub_gen_modulelibapi_with_ndk():
77    test_name = "cc_stub_gen_modulelibapi_with_ndk"
78    target_under_test_name = test_name + "_target"
79
80    cc_stub_gen(
81        name = target_under_test_name,
82        symbol_file = _SYMBOL_FILE,
83        version = "current",
84        api_surface = "module-libapi",
85        source_library_label = "//:empty",
86        included_in_ndk = True,
87        tags = ["manual"],
88    )
89
90    cc_stub_gen_test(
91        name = test_name,
92        target_under_test = target_under_test_name,
93        expected_api_surface_attrs = ["--systemapi", "--apex"],
94    )
95
96    return test_name
97
98def _test_cc_stub_gen_publicapi():
99    test_name = "cc_stub_gen_publicapi"
100    target_under_test_name = test_name + "_target"
101
102    cc_stub_gen(
103        name = target_under_test_name,
104        symbol_file = _SYMBOL_FILE,
105        version = "current",
106        api_surface = "publicapi",
107        source_library_label = "//:empty",
108        included_in_ndk = True,
109        tags = ["manual"],
110    )
111
112    cc_stub_gen_test(
113        name = test_name,
114        target_under_test = target_under_test_name,
115        expected_api_surface_attrs = [],
116    )
117
118    return test_name
119
120def cc_stub_library_test_suite(name):
121    native.test_suite(
122        name = name,
123        tests = [
124            _test_cc_stub_gen_modulelibapi_with_no_ndk(),
125            _test_cc_stub_gen_modulelibapi_with_ndk(),
126            _test_cc_stub_gen_publicapi(),
127        ],
128    )
129