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_snk.h"
18 #include "bluetooth_host.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Bluetooth {
25 using namespace std;
26 
27 constexpr int TIME = 4;
28 
29 class A2dpSinkObserverCommon : public A2dpSinkObserver {
30 public:
31     A2dpSinkObserverCommon() = default;
32     virtual ~A2dpSinkObserverCommon() = default;
33 
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)34     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) {}
35 
36 private:
37 };
38 
39 
40 static std::shared_ptr<A2dpSinkObserverCommon> observer_ = std::make_shared<A2dpSinkObserverCommon>();
41 static A2dpSink *profile_;
42 static BluetoothHost *host_;
43 
44 
45 class A2dpSinkTest : public testing::Test {
46 public:
A2dpSinkTest()47     A2dpSinkTest()
48     {}
~A2dpSinkTest()49     ~A2dpSinkTest()
50     {}
51 
52     static void SetUpTestCase(void);
53     static void TearDownTestCase(void);
54     void SetUp();
55     void TearDown();
56 
57 };
58 
59 
SetUpTestCase(void)60 void A2dpSinkTest::SetUpTestCase(void)
61 {
62 
63 }
TearDownTestCase(void)64 void A2dpSinkTest::TearDownTestCase(void)
65 {
66 
67 }
SetUp()68 void A2dpSinkTest::SetUp()
69 {
70     host_ = &BluetoothHost::GetDefaultHost();
71     host_->EnableBt();
72     host_->EnableBle();
73     sleep(TIME);
74 }
75 
TearDown()76 void A2dpSinkTest::TearDown()
77 {
78     host_->DisableBt();
79     host_->DisableBle();
80     host_ = nullptr;
81     sleep(TIME);
82 }
83 
84 /*
85  * @tc.number: A2dpSink001
86  * @tc.name: GetProfile
87  * @tc.desc: Get a2dp sink instance
88 */
89 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_GetProfile, TestSize.Level1)
90 {
91     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetProfile start";
92 
93     profile_ = A2dpSink::GetProfile();
94 
95     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetProfile end";
96 }
97 
98 /*
99  * @tc.number: A2dpSink002
100  * @tc.name: GetPlayingState
101  * @tc.desc: Get device playing state by address when peer device is on connected
102 */
103 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_GetPlayingState, TestSize.Level1)
104 {
105     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetPlayingState start";
106 
107     profile_ = A2dpSink::GetProfile();
108     BluetoothRemoteDevice device;
109     int state = profile_->GetPlayingState(device);
110     EXPECT_EQ(state, BT_SUCCESS);
111     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetPlayingState end";
112 }
113 
114 /*
115  * @tc.number: A2dpSink003
116  * @tc.name: Connect
117  * @tc.desc: Connect to the peer bluetooth device
118 */
119 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_Connect, TestSize.Level1)
120 {
121     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_Connect start";
122 
123     profile_ = A2dpSink::GetProfile();
124     BluetoothRemoteDevice device;
125     bool isOK = profile_->Connect(device);
126     EXPECT_EQ(isOK, true);
127     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_Connect end";
128 }
129 
130 /*
131  * @tc.number: A2dpSink004
132  * @tc.name: Disconnect
133  * @tc.desc: Disconnect with the peer bluetooth service
134 */
135 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_Disconnect, TestSize.Level1)
136 {
137     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_Disconnect start";
138 
139     profile_ = A2dpSink::GetProfile();
140     BluetoothRemoteDevice device;
141     bool isOK = profile_->Disconnect(device);
142     EXPECT_EQ(isOK, true);
143     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_Disconnect end";
144 }
145 
146 /*
147  * @tc.number: A2dpSink005
148  * @tc.name: RegisterObserver
149  * @tc.desc: Register callback function of framework
150 */
151 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_RegisterObserver, TestSize.Level1)
152 {
153     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_RegisterObserver start";
154 
155     profile_ = A2dpSink::GetProfile();
156     profile_->RegisterObserver(observer_);
157     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_RegisterObserver end";
158 }
159 
160 /*
161  * @tc.number: A2dpSink006
162  * @tc.name: DeregisterObserver
163  * @tc.desc: Deregister callback function of framework
164 */
165 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_DeregisterObserver, TestSize.Level1)
166 {
167     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_DeregisterObserver start";
168 
169     profile_ = A2dpSink::GetProfile();
170     profile_->DeregisterObserver(observer_);
171     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_DeregisterObserver end";
172 }
173 
174 /*
175  * @tc.number: HandsFreeUnit007
176  * @tc.name: GetDeviceState
177  * @tc.desc: Get device connection state by address
178 */
179 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_GetDeviceState, TestSize.Level1)
180 {
181     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetDeviceState start";
182 
183     profile_ = A2dpSink::GetProfile();
184     BluetoothRemoteDevice device;
185     EXPECT_EQ(profile_->GetDeviceState(device), 0);
186 
187     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetDeviceState end";
188 }
189 
190 /*
191  * @tc.number: HandsFreeUnit008
192  * @tc.name: GetDevicesByStates
193  * @tc.desc: Get devices by connection states
194 */
195 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_GetDevicesByStates, TestSize.Level1)
196 {
197     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetDevicesByStates start";
198 
199     profile_ = A2dpSink::GetProfile();
200     BluetoothRemoteDevice device;
201     vector<int> states = {static_cast<int>(BTConnectState::CONNECTED)};
202     vector<BluetoothRemoteDevice> devices = profile_->GetDevicesByStates(states);
203 
204     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetDevicesByStates end";
205 }
206 
207 /*
208  * @tc.number: HandsFreeUnit009
209  * @tc.name: GetConnectStrategy
210  * @tc.desc: Get connect strategy by address
211 */
212 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_GetConnectStrategy, TestSize.Level1)
213 {
214     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetConnectStrategy start";
215 
216     profile_ = A2dpSink::GetProfile();
217     BluetoothRemoteDevice device;
218     EXPECT_EQ(profile_->GetConnectStrategy(device), 0);
219 
220     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_GetConnectStrategy end";
221 }
222 
223 /*
224  * @tc.number: HandsFreeUnit010
225  * @tc.name: SetConnectStrategy
226  * @tc.desc: Set connect strategy by address
227 */
228 HWTEST_F(A2dpSinkTest, A2dpSink_UnitTest_SetConnectStrategy, TestSize.Level1)
229 {
230     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_SetConnectStrategy start";
231 
232     profile_ = A2dpSink::GetProfile();
233     BluetoothRemoteDevice device;
234     bool isOK = profile_->SetConnectStrategy(device,2);
235     EXPECT_EQ(isOK, false);
236 
237     GTEST_LOG_(INFO) << "A2dpSink_UnitTest_SetConnectStrategy end";
238 }
239 }  // namespace Bluetooth
240 }  // namespace OHOS