1 /*
2  * Copyright (C) 2021 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_pbap_client.h"
18 #include "bluetooth_host.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 using namespace std;
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 constexpr int TIME = 2;
27 
28 class PbapClientObserverCommon : public PbapClientObserver {
29 public:
30     PbapClientObserverCommon() = default;
31     virtual ~PbapClientObserverCommon() = default;
32 
OnServiceConnectionStateChanged(const BluetoothRemoteDevice & device,int state)33     void OnServiceConnectionStateChanged(const BluetoothRemoteDevice &device, int state) {}
OnServicePasswordRequired(const BluetoothRemoteDevice & device,const std::vector<uint8_t> & description,uint8_t charset,bool fullAccess=true)34     void OnServicePasswordRequired(const BluetoothRemoteDevice &device,
35         const std::vector<uint8_t> &description, uint8_t charset, bool fullAccess = true) {}
OnActionCompleted(const BluetoothRemoteDevice & device,int respCode,int actionType,const PbapPhoneBookData & result)36     void OnActionCompleted(
37         const BluetoothRemoteDevice &device, int respCode, int actionType, const PbapPhoneBookData &result) {}
38 
39 private:
40 };
41 
42 static PbapClientObserverCommon observer_;
43 static PbapClient *profile_;
44 
45 
46 class PbapClientTest : public testing::Test {
47 public:
PbapClientTest()48     PbapClientTest()
49     {}
~PbapClientTest()50     ~PbapClientTest()
51     {}
52     static void SetUpTestCase(void);
53     static void TearDownTestCase(void);
54     void SetUp();
55     void TearDown();
56     BluetoothHost *host_;
57 };
58 
59 
SetUpTestCase(void)60 void PbapClientTest::SetUpTestCase(void) {}
TearDownTestCase(void)61 void PbapClientTest::TearDownTestCase(void) {}
62 
SetUp()63 void PbapClientTest::SetUp()
64 {
65     host_ = &BluetoothHost::GetDefaultHost();
66     host_->EnableBt();
67     host_->EnableBle();
68     sleep(TIME);
69 }
70 
TearDown()71 void PbapClientTest::TearDown()
72 {
73     host_->DisableBt();
74     host_->DisableBle();
75     host_ = nullptr;
76     sleep(TIME);
77 }
78 
79 /*
80  * @tc.number: PbapClientUnit001
81  * @tc.name: Connect
82  * @tc.desc: Initiate the establishment of a service level connection to remote pbapclient device.
83 */
84 HWTEST_F(PbapClientTest, PbapClient_UnitTest_Connect, TestSize.Level1)
85 {
86     GTEST_LOG_(INFO) << "PbapClient_UnitTest_Connect start";
87 
88     profile_ = PbapClient::GetProfile();
89     BluetoothRemoteDevice device;
90     bool isOK = profile_->Connect(device);
91     EXPECT_EQ(isOK, true);
92     GTEST_LOG_(INFO) << "PbapClient_UnitTest_Connect end";
93 }
94 
95 /*
96  * @tc.number: PbapClientUnit002
97  * @tc.name: Disconnect
98  * @tc.desc: Release the connection from pbapclient device.
99 */
100 HWTEST_F(PbapClientTest, PbapClient_UnitTest_Disconnect, TestSize.Level1)
101 {
102     GTEST_LOG_(INFO) << "PbapClient_UnitTest_Disconnect start";
103 
104     profile_ = PbapClient::GetProfile();
105     BluetoothRemoteDevice device;
106     bool isOK = profile_->Disconnect(device);
107     EXPECT_EQ(isOK, true);
108     GTEST_LOG_(INFO) << "PbapClient_UnitTest_Disconnect end";
109 }
110 
111 /*
112  * @tc.number: PbapClientUnit003
113  * @tc.name: RegisterObserver
114  * @tc.desc: Register PbapClient observer instance.
115 */
116 HWTEST_F(PbapClientTest, PbapClient_UnitTest_RegisterObserver, TestSize.Level1)
117 {
118     GTEST_LOG_(INFO) << "PbapClient_UnitTest_RegisterObserver start";
119 
120     profile_ = PbapClient::GetProfile();
121     profile_->RegisterObserver(&observer_);
122     GTEST_LOG_(INFO) << "PbapClient_UnitTest_RegisterObserver end";
123 }
124 
125 /*
126  * @tc.number: PbapClientUnit004
127  * @tc.name: DeregisterObserver
128  * @tc.desc: Deregister PbapClient observer instance.
129 */
130 HWTEST_F(PbapClientTest, PbapClient_UnitTest_DeregisterObserver, TestSize.Level1)
131 {
132     GTEST_LOG_(INFO) << "PbapClient_UnitTest_DeregisterObserver start";
133 
134     profile_ = PbapClient::GetProfile();
135     profile_->DeregisterObserver(&observer_);
136     GTEST_LOG_(INFO) << "PbapClient_UnitTest_DeregisterObserver end";
137 }
138 
139 /*
140  * @tc.number: PbapClientUnit005
141  * @tc.name: GetConnectedDevices
142  * @tc.desc: Get remote PbapClient device list which are in the connected state.
143 */
144 HWTEST_F(PbapClientTest, PbapClient_UnitTest_GetConnectedDevices, TestSize.Level1)
145 {
146     GTEST_LOG_(INFO) << "PbapClient_UnitTest_GetConnectedDevices start";
147     profile_ = PbapClient::GetProfile();
148     vector<BluetoothRemoteDevice> devices = profile_->GetConnectedDevices();
149     int vectorSize = devices.size();
150     EXPECT_EQ(vectorSize, 0);
151     GTEST_LOG_(INFO) << "PbapClient_UnitTest_GetConnectedDevices end";
152 }
153 
154 /*
155  * @tc.number: PbapClientUnit006
156  * @tc.name: GetDevicesByStates
157  * @tc.desc: Get remote PbapClient device list which are in the specified states.
158 */
159 HWTEST_F(PbapClientTest, PbapClient_UnitTest_GetDevicesByStates, TestSize.Level1)
160 {
161     GTEST_LOG_(INFO) << "PbapClient_UnitTest_GetDevicesByStates start";
162     profile_ = PbapClient::GetProfile();
163     vector<int> states = {static_cast<int>(BTConnectState::CONNECTED)};
164     vector<BluetoothRemoteDevice> devices = profile_->GetDevicesByStates(states);
165     int vectorSize = devices.size();
166     EXPECT_EQ(vectorSize, 0);
167     GTEST_LOG_(INFO) << "PbapClient_UnitTest_GetDevicesByStates end";
168 }
169 
170 /*
171  * @tc.number: PbapClientUnit007
172  * @tc.name: GetDeviceState
173  * @tc.desc: Get remote PbapClient device state which are in the specified states.
174 */
175 HWTEST_F(PbapClientTest, PbapClient_UnitTest_GetDeviceState, TestSize.Level1)
176 {
177     GTEST_LOG_(INFO) << "PbapClient_UnitTest_GetDeviceState start";
178 
179     profile_ = PbapClient::GetProfile();
180     BluetoothRemoteDevice device;
181     int state = profile_->GetDeviceState(device);
182     EXPECT_EQ(state, -1);
183     GTEST_LOG_(INFO) << "PbapClient_UnitTest_GetDevicesByStates end";
184 }
185 
186 /*
187  * @tc.number: PbapClientUnit008
188  * @tc.name: SetConnectionStrategy
189  * @tc.desc: set the strategy with the specified remote device
190 */
191 HWTEST_F(PbapClientTest, PbapClient_UnitTest_SetConnectionStrategy, TestSize.Level1)
192 {
193     GTEST_LOG_(INFO) << "PbapClient_UnitTest_SetConnectionStrategy start";
194     profile_ = PbapClient::GetProfile();
195     BluetoothRemoteDevice device;
196     int strategy = 0;
197     bool isOk = profile_->SetConnectionStrategy(device, strategy);
198     EXPECT_EQ(isOk, false);
199     GTEST_LOG_(INFO) << "PbapClient_UnitTest_SetConnectionStrategy end";
200 }
201 
202 /*
203  * @tc.number: PbapClientUnit009
204  * @tc.name: GetConnectionStrategy
205  * @tc.desc: get the strategy with the specified remote device
206 */
207 HWTEST_F(PbapClientTest, PbapClient_UnitTest_GetConnectionStrategy, TestSize.Level1)
208 {
209     GTEST_LOG_(INFO) << "PbapClient_UnitTest_GetConnectionStrategy start";
210     profile_ = PbapClient::GetProfile();
211     BluetoothRemoteDevice device;
212     int state = profile_->GetConnectionStrategy(device);
213     EXPECT_EQ(state, -1);
214     GTEST_LOG_(INFO) << "PbapClient_UnitTest_GetConnectionStrategy end";
215 }
216 
217 /*
218  * @tc.number: PbapClientUnit010
219  * @tc.name: SetDevicePassword
220  * @tc.desc: Set device's password. please call after OnServicePasswordRequired event.
221 */
222 HWTEST_F(PbapClientTest, PbapClient_UnitTest_SetDevicePassword, TestSize.Level1)
223 {
224     GTEST_LOG_(INFO) << "PbapClient_UnitTest_SetDevicePassword start";
225     profile_ = PbapClient::GetProfile();
226     BluetoothRemoteDevice device;
227     string password = "123";
228     string userId = "123";
229     int state = profile_->SetDevicePassword(device, password, userId);
230     EXPECT_EQ(state, -1);
231     GTEST_LOG_(INFO) << "PbapClient_UnitTest_SetDevicePassword end";
232 }
233 
234 /*
235  * @tc.number: PbapClientUnit011
236  * @tc.name: PullPhoneBook
237  * @tc.desc: Pull phone book from remote device after connected.
238 */
239 HWTEST_F(PbapClientTest, PbapClient_UnitTest_PullPhoneBook, TestSize.Level1)
240 {
241     GTEST_LOG_(INFO) << "PbapClient_UnitTest_PullPhoneBook start";
242     profile_ = PbapClient::GetProfile();
243     BluetoothRemoteDevice device;
244     PbapPullPhoneBookParam param;
245     int state = profile_->PullPhoneBook(device, param);
246     EXPECT_EQ(state, -1);
247     GTEST_LOG_(INFO) << "PbapClient_UnitTest_PullPhoneBook end";
248 }
249 
250 /*
251  * @tc.number: PbapClientUnit012
252  * @tc.name: SetPhoneBook
253  * @tc.desc: Set phone book from remote device after connected.
254 */
255 HWTEST_F(PbapClientTest, PbapClient_UnitTest_SetPhoneBook, TestSize.Level1)
256 {
257     GTEST_LOG_(INFO) << "PbapClient_UnitTest_SetPhoneBook start";
258     profile_ = PbapClient::GetProfile();
259     BluetoothRemoteDevice device;
260     std::u16string name = {'a', 'b', 'c', 0};
261     int flag = 0;
262     int state = profile_->SetPhoneBook(device, name, flag);
263     EXPECT_EQ(state, -1);
264     GTEST_LOG_(INFO) << "PbapClient_UnitTest_SetPhoneBook end";
265 }
266 
267 /*
268  * @tc.number: PbapClientUnit013
269  * @tc.name: PullvCardListing
270  * @tc.desc: Pull vCard listing from remote device after connected.
271 */
272 HWTEST_F(PbapClientTest, PbapClient_UnitTest_PullvCardListing, TestSize.Level1)
273 {
274     GTEST_LOG_(INFO) << "PbapClient_UnitTest_PullvCardListing start";
275     profile_ = PbapClient::GetProfile();
276     BluetoothRemoteDevice device;
277     PbapPullvCardListingParam param;
278     int state = profile_->PullvCardListing(device, param);
279     EXPECT_EQ(state, -1);
280     GTEST_LOG_(INFO) << "PbapClient_UnitTest_PullvCardListing end";
281 }
282 
283 /*
284  * @tc.number: PbapClientUnit014
285  * @tc.name: PullvCardEntry
286  * @tc.desc: Pull vCard entry from remote device after connected.
287 */
288 HWTEST_F(PbapClientTest, PbapClient_UnitTest_PullvCardEntry, TestSize.Level1)
289 {
290     GTEST_LOG_(INFO) << "PbapClient_UnitTest_PullvCardEntry start";
291     profile_ = PbapClient::GetProfile();
292     BluetoothRemoteDevice device;
293     PbapPullvCardEntryParam param;
294     int state = profile_->PullvCardEntry(device, param);
295     EXPECT_EQ(state, -1);
296     GTEST_LOG_(INFO) << "PbapClient_UnitTest_PullvCardEntry end";
297 }
298 
299 /*
300  * @tc.number: PbapClientUnit015
301  * @tc.name: IsDownloading
302  * @tc.desc: Check if local device is downloading phonebook from remote device.
303 */
304 HWTEST_F(PbapClientTest, PbapClient_UnitTest_IsDownloading, TestSize.Level1)
305 {
306     GTEST_LOG_(INFO) << "PbapClient_UnitTest_IsDownloading start";
307     profile_ = PbapClient::GetProfile();
308     BluetoothRemoteDevice device;
309     bool isOk = profile_->IsDownloading(device);
310     EXPECT_EQ(isOk, false);
311     GTEST_LOG_(INFO) << "PbapClient_UnitTest_IsDownloading end";
312 }
313 
314 /*
315  * @tc.number: PbapClientUnit015
316  * @tc.name: AbortDownloading
317  * @tc.desc: Abort downloading phonebook from remote device.
318 */
319 HWTEST_F(PbapClientTest, PbapClient_UnitTest_AbortDownloading, TestSize.Level1)
320 {
321     GTEST_LOG_(INFO) << "PbapClient_UnitTest_AbortDownloading start";
322     profile_ = PbapClient::GetProfile();
323     BluetoothRemoteDevice device;
324     int state = profile_->AbortDownloading(device);
325     EXPECT_EQ(state, -1);
326     GTEST_LOG_(INFO) << "PbapClient_UnitTest_AbortDownloading end";
327 }
328 }  // namespace Bluetooth
329 }  // namespace OHOS