1"""Copyright (C) 2022 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(":hidl_library.bzl", "HidlInfo", "hidl_library")
19load(":hidl_package_root.bzl", "hidl_package_root")
20
21SRC_NAME = "src.hal"
22DEP1_NAME = "dep1.hal"
23DEP2_NAME = "dep2.hal"
24DEP3_NAME = "dep3.hal"
25ROOT = "android.hardware"
26ROOT_INTERFACE_FILE_LABEL = "//hardware/interfaces:current.txt"
27ROOT_INTERFACE_FILE = "hardware/interfaces/current.txt"
28ROOT_INTERFACE_PATH = "hardware/interfaces"
29ROOT_ARGUMENT = "android.hardware:hardware/interfaces"
30ROOT1 = "android.system"
31ROOT1_INTERFACE_FILE_LABEL = "//system/hardware/interfaces:current.txt"
32ROOT1_INTERFACE_FILE = "system/hardware/interfaces/current.txt"
33ROOT1_INTERFACE_PATH = "system/hardware/interfaces"
34ROOT1_ARGUMENT = "android.system:system/hardware/interfaces"
35ROOT2 = "android.hidl"
36ROOT2_INTERFACE_FILE_LABEL = "//system/libhidl/transport:current.txt"
37ROOT2_INTERFACE_FILE = "system/libhidl/transport/current.txt"
38ROOT2_INTERFACE_PATH = "system/libhidl/transport"
39ROOT2_ARGUMENT = "android.hidl:system/libhidl/transport"
40
41def _setup_roots():
42    hidl_package_root(
43        name = ROOT,
44        current = ROOT_INTERFACE_FILE_LABEL,
45        path = ROOT_INTERFACE_PATH,
46    )
47
48    hidl_package_root(
49        name = ROOT1,
50        current = ROOT1_INTERFACE_FILE_LABEL,
51        path = ROOT1_INTERFACE_PATH,
52        tags = ["manual"],
53    )
54
55    hidl_package_root(
56        name = ROOT2,
57        current = ROOT2_INTERFACE_FILE_LABEL,
58        path = ROOT2_INTERFACE_PATH,
59        tags = ["manual"],
60    )
61
62def _hidl_info_simple_test_impl(ctx):
63    env = analysistest.begin(ctx)
64    target_under_test = analysistest.target_under_test(env)
65    package_root = paths.dirname(ctx.build_file_path)
66
67    asserts.equals(
68        env,
69        expected = [
70            paths.join(package_root, "src.hal"),
71        ],
72        actual = [
73            file.short_path
74            for file in target_under_test[HidlInfo].srcs.to_list()
75        ],
76    )
77
78    asserts.equals(
79        env,
80        expected = sorted([
81            paths.join(package_root, DEP1_NAME),
82            paths.join(package_root, DEP3_NAME),
83            paths.join(package_root, DEP2_NAME),
84            paths.join(package_root, SRC_NAME),
85        ]),
86        actual = sorted([
87            file.short_path
88            for file in target_under_test[HidlInfo].transitive_srcs.to_list()
89        ]),
90    )
91
92    asserts.equals(
93        env,
94        expected = sorted([
95            ROOT1_ARGUMENT,
96            ROOT2_ARGUMENT,
97            ROOT_ARGUMENT,
98        ]),
99        actual = sorted(target_under_test[HidlInfo].transitive_roots.to_list()),
100        msg = "arguments",
101    )
102
103    asserts.equals(
104        env,
105        expected = sorted([
106            ROOT1_INTERFACE_FILE,
107            ROOT2_INTERFACE_FILE,
108            ROOT_INTERFACE_FILE,
109        ]),
110        actual = sorted([
111            file.short_path
112            for file in target_under_test[HidlInfo].transitive_root_interface_files.to_list()
113        ]),
114        msg = "interface files",
115    )
116
117    return analysistest.end(env)
118
119hidl_info_simple_test = analysistest.make(
120    _hidl_info_simple_test_impl,
121)
122
123def _test_hidl_info_simple():
124    test_base_name = "hidl_info_simple"
125    test_name = test_base_name + "_test"
126    dep1 = test_base_name + "_dep1"
127    dep2 = test_base_name + "_dep2"
128    dep3 = test_base_name + "_dep3"
129
130    hidl_library(
131        name = test_base_name,
132        srcs = [SRC_NAME],
133        deps = [
134            ":" + dep1,
135            ":" + dep2,
136        ],
137        root = ROOT,
138        tags = ["manual"],
139    )
140    hidl_library(
141        name = dep1,
142        srcs = [DEP1_NAME],
143        root = ROOT1,
144        tags = ["manual"],
145    )
146    hidl_library(
147        name = dep2,
148        srcs = [DEP2_NAME],
149        deps = [
150            ":" + dep3,
151        ],
152        root = ROOT2,
153        tags = ["manual"],
154    )
155    hidl_library(
156        name = dep3,
157        srcs = [DEP3_NAME],
158        root = ROOT2,
159        tags = ["manual"],
160    )
161    hidl_info_simple_test(
162        name = test_name,
163        target_under_test = test_base_name,
164    )
165
166    return test_name
167
168def hidl_library_test_suite(name):
169    _setup_roots()
170    native.test_suite(
171        name = name,
172        tests = [
173            _test_hidl_info_simple(),
174        ],
175    )
176