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 == BluetoothAudioHalVersion::VERSION_2_0);
29   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 == BluetoothAudioHalVersion::VERSION_2_1);
30   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 ==
31               BluetoothAudioHalVersion::VERSION_AIDL_V1);
32   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 ==
33               BluetoothAudioHalVersion::VERSION_AIDL_V2);
34   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 ==
35               BluetoothAudioHalVersion::VERSION_AIDL_V3);
36   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4 ==
37               BluetoothAudioHalVersion::VERSION_AIDL_V4);
38 }
39 
TEST_F(BluetoothAudioHalVersionTest,versionOperatorLessOrEqual)40 TEST_F(BluetoothAudioHalVersionTest, versionOperatorLessOrEqual) {
41   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0 < BluetoothAudioHalVersion::VERSION_2_1);
42   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0 <= BluetoothAudioHalVersion::VERSION_2_1);
43 
44   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 < BluetoothAudioHalVersion::VERSION_AIDL_V1);
45   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 <= BluetoothAudioHalVersion::VERSION_AIDL_V1);
46 
47   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 <
48               BluetoothAudioHalVersion::VERSION_AIDL_V2);
49   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 <=
50               BluetoothAudioHalVersion::VERSION_AIDL_V2);
51 
52   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 <
53               BluetoothAudioHalVersion::VERSION_AIDL_V3);
54   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 <=
55               BluetoothAudioHalVersion::VERSION_AIDL_V3);
56 
57   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 <
58               BluetoothAudioHalVersion::VERSION_AIDL_V4);
59   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 <=
60               BluetoothAudioHalVersion::VERSION_AIDL_V4);
61 
62   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1 < BluetoothAudioHalVersion::VERSION_2_0);
63   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1 <= BluetoothAudioHalVersion::VERSION_2_0);
64 
65   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1 < BluetoothAudioHalVersion::VERSION_2_1);
66   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1 <= BluetoothAudioHalVersion::VERSION_2_1);
67 
68   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2 <
69                BluetoothAudioHalVersion::VERSION_AIDL_V1);
70   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2 <=
71                BluetoothAudioHalVersion::VERSION_AIDL_V1);
72 
73   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3 <
74                BluetoothAudioHalVersion::VERSION_AIDL_V2);
75   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3 <=
76                BluetoothAudioHalVersion::VERSION_AIDL_V2);
77 
78   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V4 <
79                BluetoothAudioHalVersion::VERSION_AIDL_V3);
80   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V4 <=
81                BluetoothAudioHalVersion::VERSION_AIDL_V3);
82 }
83 
TEST_F(BluetoothAudioHalVersionTest,versionOperatorGreaterOrEqual)84 TEST_F(BluetoothAudioHalVersionTest, versionOperatorGreaterOrEqual) {
85   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 > BluetoothAudioHalVersion::VERSION_2_0);
86   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1 >= BluetoothAudioHalVersion::VERSION_2_0);
87 
88   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 > BluetoothAudioHalVersion::VERSION_2_1);
89   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1 >= BluetoothAudioHalVersion::VERSION_2_1);
90 
91   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 >
92               BluetoothAudioHalVersion::VERSION_AIDL_V1);
93   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2 >=
94               BluetoothAudioHalVersion::VERSION_AIDL_V1);
95 
96   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 >
97               BluetoothAudioHalVersion::VERSION_AIDL_V2);
98   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3 >=
99               BluetoothAudioHalVersion::VERSION_AIDL_V2);
100 
101   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4 >
102               BluetoothAudioHalVersion::VERSION_AIDL_V3);
103   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4 >=
104               BluetoothAudioHalVersion::VERSION_AIDL_V3);
105 
106   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_0 > BluetoothAudioHalVersion::VERSION_2_1);
107   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_0 >= BluetoothAudioHalVersion::VERSION_2_1);
108 
109   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1 > BluetoothAudioHalVersion::VERSION_AIDL_V1);
110   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1 >= BluetoothAudioHalVersion::VERSION_AIDL_V1);
111 
112   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1 >
113                BluetoothAudioHalVersion::VERSION_AIDL_V2);
114   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1 >=
115                BluetoothAudioHalVersion::VERSION_AIDL_V2);
116 
117   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2 >
118                BluetoothAudioHalVersion::VERSION_AIDL_V3);
119   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2 >=
120                BluetoothAudioHalVersion::VERSION_AIDL_V3);
121 
122   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3 >
123                BluetoothAudioHalVersion::VERSION_AIDL_V4);
124   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3 >=
125                BluetoothAudioHalVersion::VERSION_AIDL_V4);
126 }
127 
TEST_F(BluetoothAudioHalVersionTest,HIDL_VERSION_2_0)128 TEST_F(BluetoothAudioHalVersionTest, HIDL_VERSION_2_0) {
129   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_2_0,
130             BluetoothAudioHalVersion(BluetoothAudioHalTransport::HIDL, 2, 0));
131 
132   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_2_0.getTransport(), BluetoothAudioHalTransport::HIDL);
133   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0.isHIDL());
134   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_0.isAIDL());
135 
136   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0.toString().find("transport: HIDL") !=
137               std::string::npos);
138   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0.toString().find("major: 2") !=
139               std::string::npos);
140   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_0.toString().find("minor: 0") !=
141               std::string::npos);
142 }
143 
TEST_F(BluetoothAudioHalVersionTest,HIDL_VERSION_2_1)144 TEST_F(BluetoothAudioHalVersionTest, HIDL_VERSION_2_1) {
145   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_2_1,
146             BluetoothAudioHalVersion(BluetoothAudioHalTransport::HIDL, 2, 1));
147 
148   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_2_1.getTransport(), BluetoothAudioHalTransport::HIDL);
149   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1.isHIDL());
150   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_2_1.isAIDL());
151 
152   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1.toString().find("transport: HIDL") !=
153               std::string::npos);
154   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1.toString().find("major: 2") !=
155               std::string::npos);
156   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_2_1.toString().find("minor: 1") !=
157               std::string::npos);
158 }
159 
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_V1)160 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_V1) {
161   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V1,
162             BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 1, 0));
163 
164   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V1.getTransport(),
165             BluetoothAudioHalTransport::AIDL);
166   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V1.isHIDL());
167   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1.isAIDL());
168 
169   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1.toString().find("transport: AIDL") !=
170               std::string::npos);
171   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1.toString().find("major: 1") !=
172               std::string::npos);
173   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V1.toString().find("minor: 0") !=
174               std::string::npos);
175 }
176 
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_V2)177 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_V2) {
178   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V2,
179             BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 2, 0));
180 
181   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V2.getTransport(),
182             BluetoothAudioHalTransport::AIDL);
183   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V2.isHIDL());
184   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2.isAIDL());
185 
186   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2.toString().find("transport: AIDL") !=
187               std::string::npos);
188   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2.toString().find("major: 2") !=
189               std::string::npos);
190   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V2.toString().find("minor: 0") !=
191               std::string::npos);
192 }
193 
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_V3)194 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_V3) {
195   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V3,
196             BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 3, 0));
197 
198   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V3.getTransport(),
199             BluetoothAudioHalTransport::AIDL);
200   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V3.isHIDL());
201   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3.isAIDL());
202 
203   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3.toString().find("transport: AIDL") !=
204               std::string::npos);
205   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3.toString().find("major: 3") !=
206               std::string::npos);
207   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V3.toString().find("minor: 0") !=
208               std::string::npos);
209 }
210 
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_V4)211 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_V4) {
212   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V4,
213             BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 4, 0));
214 
215   EXPECT_EQ(BluetoothAudioHalVersion::VERSION_AIDL_V4.getTransport(),
216             BluetoothAudioHalTransport::AIDL);
217   EXPECT_FALSE(BluetoothAudioHalVersion::VERSION_AIDL_V4.isHIDL());
218   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4.isAIDL());
219 
220   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4.toString().find("transport: AIDL") !=
221               std::string::npos);
222   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4.toString().find("major: 4") !=
223               std::string::npos);
224   EXPECT_TRUE(BluetoothAudioHalVersion::VERSION_AIDL_V4.toString().find("minor: 0") !=
225               std::string::npos);
226 }
227 
228 /**
229  * An example of future AIDL version (next one will be V5), we check that next
230  * AIDL version will be larger than existing AIDL versions
231  */
TEST_F(BluetoothAudioHalVersionTest,AIDL_VERSIONS_Vx)232 TEST_F(BluetoothAudioHalVersionTest, AIDL_VERSIONS_Vx) {
233   EXPECT_TRUE(BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 5, 0) >
234               BluetoothAudioHalVersion::VERSION_AIDL_V4);
235   EXPECT_FALSE(BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 5, 0).isHIDL());
236   EXPECT_TRUE(BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 5, 0).isAIDL());
237 }
238