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
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "bluetooth_gatt_client.h"
20 #include "bluetooth_gatt_descriptor.h"
21 #include "bluetooth_host.h"
22 #include "uuid.h"
23 #include "vector"
24
25 using namespace testing;
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace Bluetooth {
29 class GattClientCallbackTest : public GattClientCallback {
30 public:
GattClientCallbackTest()31 GattClientCallbackTest()
32 {}
~GattClientCallbackTest()33 ~GattClientCallbackTest()
34 {}
35
OnConnectionStateChanged(int connectionState,int ret)36 void OnConnectionStateChanged(int connectionState, int ret)
37 {
38 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnConnectionStateChanged called";
39 }
40
OnCharacteristicChanged(const GattCharacteristic & characteristic)41 void OnCharacteristicChanged(const GattCharacteristic &characteristic)
42 {
43 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnCharacteristicChanged called";
44 }
45
OnCharacteristicReadResult(const GattCharacteristic & characteristic,int ret)46 void OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret)
47 {
48 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnCharacteristicReadResult called";
49 }
50
OnCharacteristicWriteResult(const GattCharacteristic & characteristic,int ret)51 void OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret)
52 {
53 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnCharacteristicWriteResult called";
54 }
55
OnDescriptorReadResult(const GattDescriptor & descriptor,int ret)56 void OnDescriptorReadResult(const GattDescriptor &descriptor, int ret)
57 {
58 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnDescriptorReadResult called";
59 }
60
OnDescriptorWriteResult(const GattDescriptor & descriptor,int ret)61 void OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret)
62 {
63 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnDescriptorWriteResult called";
64 }
65
OnMtuUpdate(int mtu,int ret)66 void OnMtuUpdate(int mtu, int ret)
67 {
68 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnMtuUpdate called";
69 }
70
OnServicesDiscovered(int status)71 void OnServicesDiscovered(int status)
72 {
73 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnServicesDiscovered called";
74 }
75
OnConnectionParameterChanged(int interval,int latency,int timeout,int status)76 void OnConnectionParameterChanged(int interval, int latency, int timeout, int status)
77 {
78 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnConnectionParameterChanged called";
79 }
80
OnSetNotifyCharacteristic(int status)81 void OnSetNotifyCharacteristic(int status)
82 {
83 GTEST_LOG_(INFO) << "GattClientCallbackTest::OnSetNotifyCharacteristic called";
84 }
85 };
86
87 class GattClientTest : public testing::Test {
88 public:
GattClientTest()89 GattClientTest()
90 {}
~GattClientTest()91 ~GattClientTest()
92 {}
93
94 int tempData_ = 0;
95 static void SetUpTestCase(void);
96 static void TearDownTestCase(void);
97 void SetUp();
98 void TearDown();
99 };
100
SetUpTestCase(void)101 void GattClientTest::SetUpTestCase(void)
102 {}
TearDownTestCase(void)103 void GattClientTest::TearDownTestCase(void)
104 {}
SetUp()105 void GattClientTest::SetUp()
106 {
107 tempData_ = 0;
108 }
109
TearDown()110 void GattClientTest::TearDown()
111 {}
112
113 /*
114 * @tc.number: GattClient001
115 * @tc.name: GattClient
116 */
117 HWTEST_F(GattClientTest, GattClient_UnitTest_GattClient, TestSize.Level1)
118 {
119 GTEST_LOG_(INFO) << "GattClient_UnitTest_GattClient start";
120 BluetoothRemoteDevice device;
121 GattClient client(device);
122 GTEST_LOG_(INFO) << "GattClient_UnitTest_GattClient end";
123 }
124
125 /*
126 * @tc.number: GattClient002
127 * @tc.name: Connect
128 */
129 HWTEST_F(GattClientTest, GattClient_UnitTest_Connect, TestSize.Level1)
130 {
131 GTEST_LOG_(INFO) << "GattClient_UnitTest_Connect start";
132 BluetoothRemoteDevice device;
133 GattClient client(device);
134 GattClientCallbackTest callback_;
135 bool isAutoConnect = true;
136 int transport = 1;
137 int result = client.Connect(callback_, isAutoConnect, transport);
138 EXPECT_EQ(result, -18);
139 GTEST_LOG_(INFO) << "GattClient_UnitTest_Connect end";
140 }
141
142 /*
143 * @tc.number: GattClient003
144 * @tc.name: Disconnect
145 */
146 HWTEST_F(GattClientTest, GattClient_UnitTest_Disconnect, TestSize.Level1)
147 {
148 GTEST_LOG_(INFO) << "GattClient_UnitTest_Disconnect start";
149 BluetoothRemoteDevice device;
150 GattClient client(device);
151 GattClientCallbackTest callback_;
152 bool isAutoConnect = true;
153 int transport = 1;
154 client.Connect(callback_, isAutoConnect, transport);
155 int result = client.Disconnect();
156 EXPECT_EQ(result, -18);
157 GTEST_LOG_(INFO) << "GattClient_UnitTest_Disconnect end";
158 }
159
160 /*
161 * @tc.number: GattClient004
162 * @tc.name: DiscoverServices
163 */
164 HWTEST_F(GattClientTest, GattClient_UnitTest_DiscoverServices, TestSize.Level1)
165 {
166 GTEST_LOG_(INFO) << "GattClient_UnitTest_DiscoverServices start";
167 BluetoothRemoteDevice device;
168 GattClient client(device);
169 GattClientCallbackTest callback_;
170 bool isAutoConnect = true;
171 int transport = 1;
172 client.Connect(callback_, isAutoConnect, transport);
173 int result = client.DiscoverServices();
174 EXPECT_EQ(result, -18);
175 GTEST_LOG_(INFO) << "GattClient_UnitTest_DiscoverServices end";
176 }
177
178 /*
179 * @tc.number: GattClient005
180 * @tc.name: GetService_1
181 */
182 HWTEST_F(GattClientTest, GattClient_UnitTest_GetService_1, TestSize.Level1)
183 {
184 GTEST_LOG_(INFO) << "GattClient_UnitTest_GetService_1 start";
185 BluetoothRemoteDevice device;
186 GattClient client(device);
187 GattClientCallbackTest callback_;
188 bool isAutoConnect = true;
189 int transport = 1;
190 bool ret = false;
191 client.Connect(callback_, isAutoConnect, transport);
192 UUID id = UUID::RandomUUID();
193 client.GetService(id);
194 if (client.GetService(id) != std::nullopt) {
195 ret = true;
196 }
197 EXPECT_EQ(ret, false);
198 GTEST_LOG_(INFO) << "GattClient_UnitTest_GetService_1 end";
199 }
200
201 /*
202 * @tc.number: GattClient006
203 * @tc.name: GetService_2
204 */
205 HWTEST_F(GattClientTest, GattClient_UnitTest_GetService_2, TestSize.Level1)
206 {
207 GTEST_LOG_(INFO) << "GattClient_UnitTest_GetService_2 start";
208 BluetoothRemoteDevice device;
209 GattClient client(device);
210 GattClientCallbackTest callback_;
211 bool isAutoConnect = true;
212 int transport = 1;
213 client.Connect(callback_, isAutoConnect, transport);
214 EXPECT_EQ((int)client.GetService().size(), 0);
215 GTEST_LOG_(INFO) << "GattClient_UnitTest_GetService_2 end";
216 }
217
218 /*
219 * @tc.number: GattClient007
220 * @tc.name: ReadCharacteristic
221 */
222 HWTEST_F(GattClientTest, GattClient_UnitTest_ReadCharacteristic, TestSize.Level1)
223 {
224 GTEST_LOG_(INFO) << "GattClient_UnitTest_ReadCharacteristic start";
225 BluetoothRemoteDevice device;
226 GattClient client(device);
227 GattClientCallbackTest callback_;
228 bool isAutoConnect = true;
229 int transport = 1;
230 client.Connect(callback_, isAutoConnect, transport);
231 UUID uuid_ = UUID::RandomUUID();
232 int permissions = 17;
233 int properties = 37;
234 GattCharacteristic characteristic = GattCharacteristic(uuid_, permissions, properties);
235 int result = client.ReadCharacteristic(characteristic);
236 EXPECT_EQ(result, -18);
237 GTEST_LOG_(INFO) << "GattClient_UnitTest_ReadCharacteristic end";
238 }
239
240 /*
241 * @tc.number: GattClient008
242 * @tc.name: ReadDescriptor
243 */
244 HWTEST_F(GattClientTest, GattClient_UnitTest_ReadDescriptor, TestSize.Level1)
245 {
246 GTEST_LOG_(INFO) << "GattClient_UnitTest_ReadDescriptor start";
247 BluetoothRemoteDevice device;
248 GattClient client(device);
249 GattClientCallbackTest callback_;
250 bool isAutoConnect = true;
251 int transport = 1;
252 client.Connect(callback_, isAutoConnect, transport);
253 UUID uuid_ = UUID::RandomUUID();
254 int permissions = 17;
255 GattDescriptor descriptor = GattDescriptor(uuid_, permissions);
256 int result = client.ReadDescriptor(descriptor);
257 EXPECT_EQ(result, -18);
258 GTEST_LOG_(INFO) << "GattClient_UnitTest_ReadDescriptor end";
259 }
260
261 /*
262 * @tc.number: GattClient009
263 * @tc.name: RequestBleMtuSize
264 */
265 HWTEST_F(GattClientTest, GattClient_UnitTest_RequestBleMtuSize, TestSize.Level1)
266 {
267 GTEST_LOG_(INFO) << "GattClient_UnitTest_RequestBleMtuSize start";
268 BluetoothRemoteDevice device;
269 GattClient client(device);
270 GattClientCallbackTest callback_;
271 bool isAutoConnect = true;
272 int transport = 1;
273 int result = client.Connect(callback_, isAutoConnect, transport);
274 int mtu = 17;
275 result = client.RequestBleMtuSize(mtu);
276 EXPECT_EQ(result, -18);
277 GTEST_LOG_(INFO) << "GattClient_UnitTest_RequestBleMtuSize end";
278 }
279
280 /*
281 * @tc.number: GattClient010
282 * @tc.name: SetNotifyCharacteristic
283 */
284 HWTEST_F(GattClientTest, GattClient_UnitTest_SetNotifyCharacteristic, TestSize.Level1)
285 {
286 GTEST_LOG_(INFO) << "GattClient_UnitTest_SetNotifyCharacteristic start";
287 BluetoothRemoteDevice device;
288 GattClient client(device);
289 GattClientCallbackTest callback_;
290 bool isAutoConnect = true;
291 int transport = 1;
292 client.Connect(callback_, isAutoConnect, transport);
293 UUID uuid_ = UUID::RandomUUID();
294 int permissions = 17;
295 int properties = 37;
296 GattCharacteristic characteristic = GattCharacteristic(uuid_, permissions, properties);
297 bool enable = true;
298 int result = client.SetNotifyCharacteristic(characteristic, enable);
299 EXPECT_EQ(result, -18);
300 GTEST_LOG_(INFO) << "GattClient_UnitTest_SetNotifyCharacteristic end";
301 }
302
303 /*
304 * @tc.number: GattClient011
305 * @tc.name: WriteCharacteristic
306 */
307 HWTEST_F(GattClientTest, GattClient_UnitTest_WriteCharacteristic, TestSize.Level1)
308 {
309 GTEST_LOG_(INFO) << "GattClient_UnitTest_WriteCharacteristic start";
310 BluetoothRemoteDevice device;
311 GattClient client(device);
312 GattClientCallbackTest callback_;
313 bool isAutoConnect = true;
314 int transport = 1;
315 client.Connect(callback_, isAutoConnect, transport);
316 UUID uuid_ = UUID::RandomUUID();
317 int permissions = 17;
318 int properties = 37;
319 GattCharacteristic characteristic = GattCharacteristic(uuid_, permissions, properties);
320 int result = client.WriteCharacteristic(characteristic);
321 EXPECT_EQ(result, -18);
322 GTEST_LOG_(INFO) << "GattClient_UnitTest_WriteCharacteristic end";
323 }
324
325 /*
326 * @tc.number: GattClient012
327 * @tc.name: WriteDescriptor
328 */
329 HWTEST_F(GattClientTest, GattClient_UnitTest_WriteDescriptor, TestSize.Level1)
330 {
331 GTEST_LOG_(INFO) << "GattClient_UnitTest_WriteDescriptor start";
332 BluetoothRemoteDevice device;
333 GattClient client(device);
334 GattClientCallbackTest callback_;
335 bool isAutoConnect = true;
336 int transport = 1;
337 client.Connect(callback_, isAutoConnect, transport);
338 UUID uuid_ = UUID::RandomUUID();
339 int permissions = 17;
340 GattDescriptor descriptor = GattDescriptor(uuid_, permissions);
341 int result = client.WriteDescriptor(descriptor);
342 EXPECT_EQ(result, -18);
343 GTEST_LOG_(INFO) << "GattClient_UnitTest_WriteDescriptor end";
344 }
345
346 /*
347 * @tc.number: GattClient013
348 * @tc.name: RequestConnectionPriority
349 */
350 HWTEST_F(GattClientTest, GattClient_UnitTest_RequestConnectionPriority, TestSize.Level1)
351 {
352 GTEST_LOG_(INFO) << "GattClient_UnitTest_RequestConnectionPriority start";
353 BluetoothRemoteDevice device;
354 GattClient client(device);
355 GattClientCallbackTest callback_;
356 bool isAutoConnect = true;
357 int transport = 1;
358 client.Connect(callback_, isAutoConnect, transport);
359 int connPriority = 2;
360 int result = client.RequestConnectionPriority(connPriority);
361 EXPECT_EQ(result, -18);
362 GTEST_LOG_(INFO) << "GattClient_UnitTest_RequestConnectionPriority end";
363 }
364
365 } // namespace Bluetooth
366 } // namespace OHOS
367