1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "audio_hal_interface/hal_version_manager.h"
18 
19 #include <gtest/gtest.h>
20 
21 using bluetooth::audio::BluetoothAudioHalTransport;
22 using bluetooth::audio::BluetoothAudioHalVersion;
23 using bluetooth::audio::HalVersionManager;
24 
25 class BluetoothAudioHalVersionTest : public ::testing::Test {};
26 
TEST_F(BluetoothAudioHalVersionTest,versionOperatorEqual)27 TEST_F(BluetoothAudioHalVersionTest, versionOperatorEqual) {
28   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0 ==
29               BluetoothAudioHalVersion::VERSION_2_0);
30   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 ==
31               BluetoothAudioHalVersion::VERSION_2_1);
32   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 ==
33               BluetoothAudioHalVersion::VERSION_AIDL_V1);
34   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 ==
35               BluetoothAudioHalVersion::VERSION_AIDL_V2);
36   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 ==
37               BluetoothAudioHalVersion::VERSION_AIDL_V3);
38   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4 ==
39               BluetoothAudioHalVersion::VERSION_AIDL_V4);
40 }
41 
TEST_F(BluetoothAudioHalVersionTest,versionOperatorLessOrEqual)42 TEST_F(BluetoothAudioHalVersionTest, versionOperatorLessOrEqual) {
43   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0 <
44               BluetoothAudioHalVersion::VERSION_2_1);
45   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0 <=
46               BluetoothAudioHalVersion::VERSION_2_1);
47 
48   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 <
49               BluetoothAudioHalVersion::VERSION_AIDL_V1);
50   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 <=
51               BluetoothAudioHalVersion::VERSION_AIDL_V1);
52 
53   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 <
54               BluetoothAudioHalVersion::VERSION_AIDL_V2);
55   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 <=
56               BluetoothAudioHalVersion::VERSION_AIDL_V2);
57 
58   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 <
59               BluetoothAudioHalVersion::VERSION_AIDL_V3);
60   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 <=
61               BluetoothAudioHalVersion::VERSION_AIDL_V3);
62 
63   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 <
64               BluetoothAudioHalVersion::VERSION_AIDL_V4);
65   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 <=
66               BluetoothAudioHalVersion::VERSION_AIDL_V4);
67 
68   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1 <
69                BluetoothAudioHalVersion::VERSION_2_0);
70   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1 <=
71                BluetoothAudioHalVersion::VERSION_2_0);
72 
73   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1 <
74                BluetoothAudioHalVersion::VERSION_2_1);
75   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1 <=
76                BluetoothAudioHalVersion::VERSION_2_1);
77 
78   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2 <
79                BluetoothAudioHalVersion::VERSION_AIDL_V1);
80   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2 <=
81                BluetoothAudioHalVersion::VERSION_AIDL_V1);
82 
83   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3 <
84                BluetoothAudioHalVersion::VERSION_AIDL_V2);
85   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3 <=
86                BluetoothAudioHalVersion::VERSION_AIDL_V2);
87 
88   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V4 <
89                BluetoothAudioHalVersion::VERSION_AIDL_V3);
90   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V4 <=
91                BluetoothAudioHalVersion::VERSION_AIDL_V3);
92 }
93 
TEST_F(BluetoothAudioHalVersionTest,versionOperatorGreaterOrEqual)94 TEST_F(BluetoothAudioHalVersionTest, versionOperatorGreaterOrEqual) {
95   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 >
96               BluetoothAudioHalVersion::VERSION_2_0);
97   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 >=
98               BluetoothAudioHalVersion::VERSION_2_0);
99 
100   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 >
101               BluetoothAudioHalVersion::VERSION_2_1);
102   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 >=
103               BluetoothAudioHalVersion::VERSION_2_1);
104 
105   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 >
106               BluetoothAudioHalVersion::VERSION_AIDL_V1);
107   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 >=
108               BluetoothAudioHalVersion::VERSION_AIDL_V1);
109 
110   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 >
111               BluetoothAudioHalVersion::VERSION_AIDL_V2);
112   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 >=
113               BluetoothAudioHalVersion::VERSION_AIDL_V2);
114 
115   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4 >
116               BluetoothAudioHalVersion::VERSION_AIDL_V3);
117   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4 >=
118               BluetoothAudioHalVersion::VERSION_AIDL_V3);
119 
120   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_0 >
121                BluetoothAudioHalVersion::VERSION_2_1);
122   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_0 >=
123                BluetoothAudioHalVersion::VERSION_2_1);
124 
125   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1 >
126                BluetoothAudioHalVersion::VERSION_AIDL_V1);
127   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1 >=
128                BluetoothAudioHalVersion::VERSION_AIDL_V1);
129 
130   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1 >
131                BluetoothAudioHalVersion::VERSION_AIDL_V2);
132   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1 >=
133                BluetoothAudioHalVersion::VERSION_AIDL_V2);
134 
135   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2 >
136                BluetoothAudioHalVersion::VERSION_AIDL_V3);
137   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2 >=
138                BluetoothAudioHalVersion::VERSION_AIDL_V3);
139 
140   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3 >
141                BluetoothAudioHalVersion::VERSION_AIDL_V4);
142   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3 >=
143                BluetoothAudioHalVersion::VERSION_AIDL_V4);
144 }
145 
TEST_F(BluetoothAudioHalVersionTest,HIDL_VERSION_2_0)146 TEST_F(BluetoothAudioHalVersionTest, HIDL_VERSION_2_0) {
147   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_2_0,
148             BluetoothAudioHalVersion(BluetoothAudioHalTransport::HIDL, 2, 0));
149 
150   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_2_0.getTransport(),
151             BluetoothAudioHalTransport::HIDL);
152   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0.isHIDL());
153   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_0.isAIDL());
154 
155   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0.toString().find(
156                   "transport: HIDL") != std::string::npos);
157   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0.toString().find(
158                   "major: 2") != std::string::npos);
159   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0.toString().find(
160                   "minor: 0") != std::string::npos);
161 }
162 
TEST_F(BluetoothAudioHalVersionTest,HIDL_VERSION_2_1)163 TEST_F(BluetoothAudioHalVersionTest, HIDL_VERSION_2_1) {
164   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_2_1,
165             BluetoothAudioHalVersion(BluetoothAudioHalTransport::HIDL, 2, 1));
166 
167   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_2_1.getTransport(),
168             BluetoothAudioHalTransport::HIDL);
169   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1.isHIDL());
170   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1.isAIDL());
171 
172   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1.toString().find(
173                   "transport: HIDL") != std::string::npos);
174   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1.toString().find(
175                   "major: 2") != std::string::npos);
176   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1.toString().find(
177                   "minor: 1") != std::string::npos);
178 }
179 
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_V1)180 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_V1) {
181   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V1,
182             BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 1, 0));
183 
184   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V1.getTransport(),
185             BluetoothAudioHalTransport::AIDL);
186   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1.isHIDL());
187   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1.isAIDL());
188 
189   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1.toString().find(
190                   "transport: AIDL") != std::string::npos);
191   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1.toString().find(
192                   "major: 1") != std::string::npos);
193   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1.toString().find(
194                   "minor: 0") != std::string::npos);
195 }
196 
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_V2)197 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_V2) {
198   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V2,
199             BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 2, 0));
200 
201   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V2.getTransport(),
202             BluetoothAudioHalTransport::AIDL);
203   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2.isHIDL());
204   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2.isAIDL());
205 
206   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2.toString().find(
207                   "transport: AIDL") != std::string::npos);
208   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2.toString().find(
209                   "major: 2") != std::string::npos);
210   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2.toString().find(
211                   "minor: 0") != std::string::npos);
212 }
213 
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_V3)214 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_V3) {
215   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V3,
216             BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 3, 0));
217 
218   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V3.getTransport(),
219             BluetoothAudioHalTransport::AIDL);
220   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3.isHIDL());
221   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3.isAIDL());
222 
223   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3.toString().find(
224                   "transport: AIDL") != std::string::npos);
225   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3.toString().find(
226                   "major: 3") != std::string::npos);
227   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3.toString().find(
228                   "minor: 0") != std::string::npos);
229 }
230 
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_V4)231 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_V4) {
232   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V4,
233             BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 4, 0));
234 
235   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V4.getTransport(),
236             BluetoothAudioHalTransport::AIDL);
237   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V4.isHIDL());
238   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4.isAIDL());
239 
240   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4.toString().find(
241                   "transport: AIDL") != std::string::npos);
242   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4.toString().find(
243                   "major: 4") != std::string::npos);
244   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4.toString().find(
245                   "minor: 0") != std::string::npos);
246 }
247 
248 /**
249  * An example of future AIDL version (next one will be V5), we check that next
250  * AIDL version will be larger than existing AIDL versions
251  */
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_Vx)252 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_Vx) {
253   EXPECT_TRUE(BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 5, 0) >
254               BluetoothAudioHalVersion::VERSION_AIDL_V4);
255   EXPECT_FALSE(BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 5, 0)
256                    .isHIDL());
257   EXPECT_TRUE(BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 5, 0)
258                   .isAIDL());
259 }
260