xref: /aosp_15_r20/build/bazel/rules/apex/apex_aab_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2022 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
15load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
16load(":apex_aab.bzl", "apex_aab")
17load(":apex_test_helpers.bzl", "test_apex")
18
19def _apex_aab_test(ctx):
20    env = analysistest.begin(ctx)
21    target_under_test = analysistest.target_under_test(env)
22    asserts.true(
23        env,
24        len(target_under_test.files.to_list()) == len(ctx.attr.expected_paths),
25    )
26    for i in range(0, len(ctx.attr.expected_paths)):
27        asserts.equals(
28            env,
29            ctx.attr.expected_paths[i],
30            target_under_test.files.to_list()[i].short_path,
31        )
32    return analysistest.end(env)
33
34apex_aab_test = analysistest.make(
35    _apex_aab_test,
36    attrs = {
37        "expected_paths": attr.string_list(mandatory = True),
38    },
39)
40
41def _test_apex_aab_generates_aab():
42    name = "apex_aab_simple"
43    test_name = name + "_test"
44    apex_name = name + "_apex"
45
46    test_apex(name = apex_name)
47
48    apex_aab(
49        name = name,
50        mainline_module = apex_name,
51        tags = ["manual"],
52    )
53
54    apex_aab_test(
55        name = test_name,
56        target_under_test = name,
57        expected_paths = ["/".join([native.package_name(), apex_name, apex_name + ".aab"])],
58    )
59
60    return test_name
61
62def _apex_aab_output_group_test(ctx):
63    env = analysistest.begin(ctx)
64    target_under_test = analysistest.target_under_test(env)
65    actual_paths = sorted([
66        f.short_path
67        for f in target_under_test[OutputGroupInfo].apex_files.to_list()
68    ])
69    asserts.equals(
70        env,
71        sorted(ctx.attr.expected_paths),
72        sorted(actual_paths),
73    )
74    return analysistest.end(env)
75
76apex_aab_output_group_test = analysistest.make(
77    _apex_aab_output_group_test,
78    attrs = {"expected_paths": attr.string_list(mandatory = True)},
79)
80
81def _test_apex_aab_apex_files_output_group():
82    name = "apex_aab_apex_files"
83    test_name = name + "_test"
84    apex_name = name + "_apex"
85
86    test_apex(name = apex_name)
87
88    apex_aab(
89        name = name,
90        mainline_module = apex_name,
91        tags = ["manual"],
92    )
93
94    expected_paths = []
95    for arch in ["arm", "arm64", "x86", "x86_64", "arm64only", "x86_64only"]:
96        paths = [
97            "/".join([native.package_name(), "mainline_modules_" + arch, basename])
98            for basename in [
99                apex_name + ".apex",
100                apex_name + "-base.zip",
101                "java_apis_used_by_apex/" + apex_name + "_using.xml",
102                "ndk_apis_usedby_apex/" + apex_name + "_using.txt",
103                "ndk_apis_backedby_apex/" + apex_name + "_backing.txt",
104            ]
105        ]
106        expected_paths.extend(paths)
107
108    apex_aab_output_group_test(
109        name = test_name,
110        target_under_test = name,
111        expected_paths = expected_paths,
112    )
113
114    return test_name
115
116def _test_apex_aab_generates_aab_and_apks():
117    name = "apex_aab_apks"
118    test_name = name + "_test"
119    apex_name = name + "_apex"
120
121    test_apex(name = apex_name, package_name = "com.google.android." + apex_name)
122
123    apex_aab(
124        name = name,
125        mainline_module = apex_name,
126        dev_sign_bundle = "//build/make/tools/releasetools:sign_apex",
127        dev_keystore = "//build/bazel/rules/apex/testdata:dev-keystore",
128        tags = ["manual"],
129    )
130
131    apex_aab_test(
132        name = test_name,
133        target_under_test = name,
134        expected_paths = [
135            "/".join([native.package_name(), apex_name, apex_name + ".aab"]),
136            "/".join([native.package_name(), apex_name, apex_name + ".apks"]),
137            "/".join([native.package_name(), apex_name, apex_name + ".cert_info.txt"]),
138        ],
139    )
140
141    return test_name
142
143def apex_aab_test_suite(name):
144    native.test_suite(
145        name = name,
146        tests = [
147            _test_apex_aab_generates_aab(),
148            _test_apex_aab_apex_files_output_group(),
149            _test_apex_aab_generates_aab_and_apks(),
150        ],
151    )
152