1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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  */
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 #include "bluetooth_a2dp_src.h"
18 #include "bluetooth_host.h"
19 #include "bluetooth_errorcode.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 using namespace std;
27 
28 constexpr int TIME = 2;
29 
30 class A2dpSourceObserverCommon : public A2dpSourceObserver {
31 public:
32     A2dpSourceObserverCommon() = default;
33     virtual ~A2dpSourceObserverCommon() = default;
34 
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)35     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) {}
OnConfigurationChanged(const BluetoothRemoteDevice & device,const A2dpCodecInfo & info,int error)36     virtual void OnConfigurationChanged(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info, int error){}
OnPlayingStatusChanged(const BluetoothRemoteDevice & device,int playingState,int error)37     virtual void OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error){}
38 
39 private:
40 };
41 
42 
43 static std::shared_ptr<A2dpSourceObserverCommon> observer_ = std::make_shared<A2dpSourceObserverCommon>();
44 static A2dpSource *profile_;
45 static BluetoothHost *host_;
46 
47 class A2dpSourceTest : public testing::Test {
48 public:
A2dpSourceTest()49     A2dpSourceTest()
50     {}
~A2dpSourceTest()51     ~A2dpSourceTest()
52     {}
53 
54     static void SetUpTestCase(void);
55     static void TearDownTestCase(void);
56     void SetUp();
57     void TearDown();
58 };
59 
60 
SetUpTestCase(void)61 void A2dpSourceTest::SetUpTestCase(void)
62 {
63 
64 }
TearDownTestCase(void)65 void A2dpSourceTest::TearDownTestCase(void)
66 {
67 
68 }
SetUp()69 void A2dpSourceTest::SetUp()
70 {
71     host_ = &BluetoothHost::GetDefaultHost();
72     host_->EnableBt();
73     host_->EnableBle();
74     sleep(TIME);
75 }
76 
TearDown()77 void A2dpSourceTest::TearDown()
78 {
79     host_->DisableBt();
80     host_->DisableBle();
81     host_ = nullptr;
82     sleep(TIME);
83 }
84 
85 /*
86  * @tc.number: A2dpSource001
87  * @tc.name: GetProfile
88  * @tc.desc: Get a2dp sink instance
89 */
90 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_GetProfile, TestSize.Level1)
91 {
92     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetProfile start";
93 
94     profile_ = A2dpSource::GetProfile();
95 
96     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetProfile end";
97 }
98 
99 /*
100  * @tc.number: A2dpSource002
101  * @tc.name: GetPlayingState
102  * @tc.desc: Get device playing state by address when peer device is on connected
103 */
104 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_GetPlayingState, TestSize.Level1)
105 {
106     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetPlayingState start";
107 
108     profile_ = A2dpSource::GetProfile();
109     BluetoothRemoteDevice device;
110     int state = profile_->GetPlayingState(device);
111     EXPECT_EQ(state, BT_SUCCESS);
112     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetPlayingState end";
113 }
114 
115 /*
116  * @tc.number: A2dpSource003
117  * @tc.name: RegisterObserver
118  * @tc.desc: Register callback function of framework
119 */
120 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_RegisterObserver, TestSize.Level1)
121 {
122     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_RegisterObserver start";
123 
124     profile_ = A2dpSource::GetProfile();
125     profile_->RegisterObserver(observer_);
126     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_RegisterObserver end";
127 }
128 
129 /*
130  * @tc.number: A2dpSource004
131  * @tc.name: DeregisterObserver
132  * @tc.desc: Deregister callback function of framework
133 */
134 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_DeregisterObserver, TestSize.Level1)
135 {
136     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_DeregisterObserver start";
137 
138     profile_ = A2dpSource::GetProfile();
139     profile_->DeregisterObserver(observer_);
140     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_DeregisterObserver end";
141 }
142 
143 /*
144  * @tc.number: A2dpSource005
145  * @tc.name: GetDeviceState
146  * @tc.desc: Get device connection state by address
147 */
148 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_GetDeviceState, TestSize.Level1)
149 {
150     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetDeviceState start";
151 
152     profile_ = A2dpSource::GetProfile();
153     BluetoothRemoteDevice device;
154     EXPECT_EQ(profile_->GetDeviceState(device), 0);
155 
156     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetDeviceState end";
157 }
158 
159 /*
160  * @tc.number: A2dpSource006
161  * @tc.name: GetDevicesByStates
162  * @tc.desc: Get devices by connection states
163 */
164 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_GetDevicesByStates, TestSize.Level1)
165 {
166     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetDevicesByStates start";
167 
168     profile_ = A2dpSource::GetProfile();
169     BluetoothRemoteDevice device;
170     vector<int> states = {static_cast<int>(BTConnectState::CONNECTED)};
171     vector<BluetoothRemoteDevice> devices = profile_->GetDevicesByStates(states);
172 
173     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetDevicesByStates end";
174 }
175 
176 /*
177  * @tc.number: A2dpSource007
178  * @tc.name: Connect
179  * @tc.desc: Connect to the peer bluetooth device
180 */
181 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_Connect, TestSize.Level1)
182 {
183     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_Connect start";
184 
185     profile_ = A2dpSource::GetProfile();
186     BluetoothRemoteDevice device;
187     int32_t ret = profile_->Connect(device);
188     EXPECT_NE(ret, BT_SUCCESS);
189     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_Connect end";
190 }
191 
192 /*
193  * @tc.number: A2dpSource008
194  * @tc.name: Disconnect
195  * @tc.desc: Disconnect with the peer bluetooth service
196 */
197 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_Disconnect, TestSize.Level1)
198 {
199     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_Disconnect start";
200 
201     profile_ = A2dpSource::GetProfile();
202     BluetoothRemoteDevice device;
203     int32_t ret = profile_->Disconnect(device);
204     EXPECT_NE(ret, BT_SUCCESS);
205     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_Disconnect end";
206 }
207 
208 /*
209  * @tc.number: A2dpSource009
210  * @tc.name: SetActiveSinkDevice
211  * @tc.desc: Set the active sink device for audio connection.
212 */
213 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_SetActiveSinkDevice, TestSize.Level1)
214 {
215     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_SetActiveSinkDevice start";
216 
217     profile_ = A2dpSource::GetProfile();
218     BluetoothRemoteDevice device;
219     EXPECT_EQ(profile_->SetActiveSinkDevice(device), -3);
220 
221     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_SetActiveSinkDevice end";
222 }
223 
224 /*
225  * @tc.number: A2dpSource010
226  * @tc.name: GetActiveSinkDevice
227  * @tc.desc: Get the active sink device for audio connection.
228 */
229 HWTEST_F(A2dpSourceTest, A2dpSource_UnitTest_GetActiveSinkDevice, TestSize.Level1)
230 {
231     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetActiveSinkDevice start";
232 
233     profile_ = A2dpSource::GetProfile();
234     BluetoothRemoteDevice device = profile_->GetActiveSinkDevice();
235 
236     GTEST_LOG_(INFO) << "A2dpSource_UnitTest_GetActiveSinkDevice end";
237 }
238 
239 /*
240  * @tc.number: A2dpSource011
241  * @tc.name: GetConnectStrategy
242  * @tc.desc: Get connect strategy by address
243 */
244 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_GetConnectStrategy, TestSize.Level1)
245 {
246     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_GetConnectStrategy start";
247 
248     profile_ = A2dpSource::GetProfile();
249     BluetoothRemoteDevice device;
250     EXPECT_EQ(profile_->GetConnectStrategy(device), 0);
251 
252     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_GetConnectStrategy end";
253 }
254 
255 /*
256  * @tc.number: A2dpSource012
257  * @tc.name: SetConnectStrategy
258  * @tc.desc: Set connect strategy by address
259 */
260 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_SetConnectStrategy, TestSize.Level1)
261 {
262     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SetConnectStrategy start";
263 
264     profile_ = A2dpSource::GetProfile();
265     BluetoothRemoteDevice device;
266     bool isOK = profile_->SetConnectStrategy(device,2);
267     EXPECT_EQ(isOK, false);
268 
269     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SetConnectStrategy end";
270 }
271 
272 /*
273  * @tc.number: A2dpSource013
274  * @tc.name: GetCodecStatus
275  * @tc.desc: Get codec status by address
276 */
277 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_GetCodecStatus, TestSize.Level1)
278 {
279     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_GetCodecStatus start";
280 
281     profile_ = A2dpSource::GetProfile();
282     BluetoothRemoteDevice device;
283     A2dpCodecStatus ret = profile_->GetCodecStatus(device);
284 
285     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_GetCodecStatus end";
286 }
287 
288 /*
289  * @tc.number: A2dpSource014
290  * @tc.name: SetCodecPreference
291  * @tc.desc: Set codec preference by address
292 */
293 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_SetCodecPreference, TestSize.Level1)
294 {
295     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SetCodecPreference start";
296 
297     profile_ = A2dpSource::GetProfile();
298     BluetoothRemoteDevice device;
299     A2dpCodecInfo info;
300     info.codecPriority = 0;
301     info.codecType = 1;
302     info.sampleRate = 2;
303     info.bitsPerSample = 3;
304     info.channelMode = 4;
305     info.codecSpecific1 = 5;
306     info.codecSpecific2 = 6;
307     info.codecSpecific3 = 7;
308     info.codecSpecific4 = 8;
309     EXPECT_EQ(profile_->SetCodecPreference(device, info), 0);
310 
311     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SetCodecPreference end";
312 }
313 
314 /*
315  * @tc.number: A2dpSource015
316  * @tc.name: SwitchOptionalCodecs
317  * @tc.desc: Switch optional codecs by address
318 */
319 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_SwitchOptionalCodecs, TestSize.Level1)
320 {
321     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SwitchOptionalCodecs start";
322 
323     profile_ = A2dpSource::GetProfile();
324     BluetoothRemoteDevice device;
325     bool isEnable = true;
326     profile_->SwitchOptionalCodecs(device, isEnable);
327 
328     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SwitchOptionalCodecs end";
329 }
330 
331 /*
332  * @tc.number: A2dpSource016
333  * @tc.name: GetOptionalCodecsSupportState
334  * @tc.desc: Get optional codecs support state by address
335 */
336 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_GetOptionalCodecsSupportState, TestSize.Level1)
337 {
338     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_GetOptionalCodecsSupportState start";
339 
340     profile_ = A2dpSource::GetProfile();
341     BluetoothRemoteDevice device;
342     EXPECT_EQ(profile_->GetOptionalCodecsSupportState(device), 0);
343 
344     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_GetOptionalCodecsSupportState end";
345 }
346 
347 /*
348  * @tc.number: A2dpSource017
349  * @tc.name: StartPlaying
350  * @tc.desc: Start playing
351 */
352 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_StartPlaying, TestSize.Level1)
353 {
354     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_StartPlaying start";
355 
356     profile_ = A2dpSource::GetProfile();
357     BluetoothRemoteDevice device;
358     EXPECT_EQ(profile_->StartPlaying(device), 0);
359 
360     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_StartPlaying end";
361 }
362 
363 /*
364  * @tc.number: A2dpSource018
365  * @tc.name: SuspendPlaying
366  * @tc.desc: Suspend playing
367 */
368 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_SuspendPlaying, TestSize.Level1)
369 {
370     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SuspendPlaying start";
371 
372     profile_ = A2dpSource::GetProfile();
373     BluetoothRemoteDevice device;
374     EXPECT_EQ(profile_->SuspendPlaying(device), 0);
375 
376     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SuspendPlaying end";
377 }
378 
379 /*
380  * @tc.number: A2dpSource019
381  * @tc.name: StopPlaying
382  * @tc.desc: Stop playing
383 */
384 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_StopPlaying, TestSize.Level1)
385 {
386     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_StopPlaying start";
387 
388     profile_ = A2dpSource::GetProfile();
389     BluetoothRemoteDevice device;
390     EXPECT_EQ(profile_->StopPlaying(device), 0);
391 
392     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_StopPlaying end";
393 }
394 
395 /*
396  * @tc.number: A2dpSource020
397  * @tc.name: SetAudioConfigure
398  * @tc.desc: Set audio configure
399 */
400 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_SetAudioConfigure, TestSize.Level1)
401 {
402     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SetAudioConfigure start";
403 
404     profile_ = A2dpSource::GetProfile();
405     BluetoothRemoteDevice device;
406     profile_->SetAudioConfigure(device, 1, 2, 3);
407 
408     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_SetAudioConfigure end";
409 }
410 
411 /*
412  * @tc.number: A2dpSource021
413  * @tc.name: WriteFrame
414  * @tc.desc: Write frame data to a2dp
415 */
416 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_WriteFrame, TestSize.Level1)
417 {
418     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_WriteFrame start";
419 
420     uint8_t str[4] = {'1', '2', '3', '4'};
421     profile_ = A2dpSource::GetProfile();
422     profile_->WriteFrame(str, 4);
423 
424     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_WriteFrame end";
425 }
426 
427 /*
428  * @tc.number: A2dpSource022
429  * @tc.name: GetRenderPosition
430  * @tc.desc: Get delayValue, sendDataSize and timeStamp from a2dp
431 */
432 HWTEST_F(A2dpSourceTest, A2dpSourceTest_UnitTest_GetRenderPosition, TestSize.Level1)
433 {
434     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_GetRenderPosition start";
435 
436     uint16_t delayValue = 0;
437     uint16_t sendDataSize = 0;
438     uint32_t timeStamp = 0;
439     profile_ = A2dpSource::GetProfile();
440     profile_->GetRenderPosition(delayValue, sendDataSize, timeStamp);
441     GTEST_LOG_(INFO) << "delayValue =" << delayValue << ",sendDataSize =" << sendDataSize <<
442         ",timeStamp =" << timeStamp;
443 
444     GTEST_LOG_(INFO) << "A2dpSourceTest_UnitTest_GetRenderPosition end";
445 }
446 }  // namespace Bluetooth
447 }  // namespace OHOS