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_map_mce.h"
18#include "bluetooth_host.h"
19#include "bluetooth_def.h"
20
21using namespace testing;
22using namespace testing::ext;
23using namespace std;
24
25namespace OHOS {
26namespace Bluetooth {
27constexpr int TIME = 2;
28
29class MapClientObserverCommon : public MapClientObserver{
30public:
31    MapClientObserverCommon() = default;
32    virtual ~MapClientObserverCommon() = default;
33
34    void OnMapActionCompleted(
35        const BluetoothRemoteDevice &device, const MapAction &action, MapExecuteStatus status) {}
36
37    void OnMapEventReported(const BluetoothRemoteDevice &device, const MapEventReport &report) {}
38
39    void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state) {}
40
41    void OnBmessageCompleted(
42        const BluetoothRemoteDevice &deviceAddress, const MapBMessage &bmsg, MapExecuteStatus status) {}
43
44    void OnMessagesListingCompleted(
45        const BluetoothRemoteDevice &deviceAddress, const MessagesListing &listing, MapExecuteStatus status) {}
46
47    void OnConversationListingCompleted(
48        const BluetoothRemoteDevice &deviceAddress, const ConversationListing &listing, MapExecuteStatus status) {}
49
50private:
51};
52
53static MapClientObserverCommon observer_;
54static MapClient *profile_;
55
56class MapClientTest : public testing::Test {
57public:
58    MapClientTest()
59    {}
60    ~MapClientTest()
61    {}
62
63    static void SetUpTestCase(void);
64    static void TearDownTestCase(void);
65    void SetUp();
66    void TearDown();
67    BluetoothHost *host_;
68};
69
70
71void MapClientTest::SetUpTestCase(void)
72{}
73void MapClientTest::TearDownTestCase(void)
74{}
75void MapClientTest::SetUp()
76{
77    host_ = &BluetoothHost::GetDefaultHost();
78    host_->EnableBt();
79    host_->EnableBle();
80    sleep(TIME);
81}
82
83void MapClientTest::TearDown()
84{
85    host_->DisableBt();
86    host_->DisableBle();
87    host_ = nullptr;
88}
89
90
91
92/*
93 * @tc.number: MapClient001
94 * @tc.name: GetProfile
95 * @tc.desc: Get the Profile object.
96*/
97HWTEST_F(MapClientTest, Avrcp_UnitTest_GetProfile, TestSize.Level1)
98{
99    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetProfile start";
100
101    profile_ = MapClient::GetProfile();
102
103    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetProfile end";
104}
105
106
107/*
108 * @tc.number: MapClient002
109 * @tc.name: RegisterObserver
110 * @tc.desc: Register Observer.
111*/
112HWTEST_F(MapClientTest, Avrcp_UnitTest_RegisterObserver, TestSize.Level1)
113{
114    GTEST_LOG_(INFO) << "Avrcp_UnitTest_RegisterObserver start";
115
116    profile_ = MapClient::GetProfile();
117    profile_->RegisterObserver(observer_);
118    sleep(TIME);
119    GTEST_LOG_(INFO) << "Avrcp_UnitTest_RegisterObserver end";
120}
121
122
123/*
124 * @tc.number: MapClient003
125 * @tc.name: DeregisterObserver
126 * @tc.desc: Deregister Observer.
127*/
128HWTEST_F(MapClientTest, Avrcp_UnitTest_DeregisterObserver, TestSize.Level1)
129{
130    GTEST_LOG_(INFO) << "Avrcp_UnitTest_DeregisterObserver start";
131
132    profile_ = MapClient::GetProfile();
133    profile_->DeregisterObserver(observer_);
134    sleep(TIME);
135    GTEST_LOG_(INFO) << "Avrcp_UnitTest_DeregisterObserver end";
136}
137
138
139/*
140 * @tc.number: MapClient004
141 * @tc.name: Connect
142 * @tc.desc: Connect to map server.
143*/
144HWTEST_F(MapClientTest, Avrcp_UnitTest_Connect, TestSize.Level1)
145{
146    GTEST_LOG_(INFO) << "Avrcp_UnitTest_Connect start";
147
148    profile_ = MapClient::GetProfile();
149    BluetoothRemoteDevice device;
150    bool ret = profile_->Connect(device);
151    sleep(TIME);
152    EXPECT_EQ(ret, true);
153
154    GTEST_LOG_(INFO) << "Avrcp_UnitTest_Connect end";
155}
156
157
158/*
159 * @tc.number: MapClient005
160 * @tc.name: Disconnect
161 * @tc.desc: disconnect from map server.
162*/
163HWTEST_F(MapClientTest, Avrcp_UnitTest_Disconnect, TestSize.Level1)
164{
165    GTEST_LOG_(INFO) << "Avrcp_UnitTest_Disconnect start";
166
167    profile_ = MapClient::GetProfile();
168    BluetoothRemoteDevice device;
169    bool ret = profile_->Disconnect(device);
170    sleep(TIME);
171    EXPECT_EQ(ret, true);
172
173    GTEST_LOG_(INFO) << "Avrcp_UnitTest_Disconnect end";
174}
175
176
177/*
178 * @tc.number: MapClient006
179 * @tc.name: IsConnected
180 * @tc.desc: display connect status.
181*/
182HWTEST_F(MapClientTest, Avrcp_UnitTest_IsConnected, TestSize.Level1)
183{
184    GTEST_LOG_(INFO) << "Avrcp_UnitTest_IsConnected start";
185
186    profile_ = MapClient::GetProfile();
187    BluetoothRemoteDevice device;
188    bool ret = profile_->IsConnected(device);
189    sleep(TIME);
190    EXPECT_EQ(ret, true);
191
192    GTEST_LOG_(INFO) << "Avrcp_UnitTest_IsConnected end";
193}
194
195
196/*
197 * @tc.number: MapClient007
198 * @tc.name: GetConnectedDevices
199 * @tc.desc: Get the Connected Devices object.
200*/
201HWTEST_F(MapClientTest, Avrcp_UnitTest_GetConnectedDevices, TestSize.Level1)
202{
203    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConnectedDevices start";
204
205    profile_ = MapClient::GetProfile();
206    vector<BluetoothRemoteDevice> devices = profile_->GetConnectedDevices();
207    sleep(TIME);
208
209
210    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConnectedDevices end";
211}
212
213
214/*
215 * @tc.number: MapClient008
216 * @tc.name: GetDevicesByStates
217 * @tc.desc: Get the device list through the connection status.
218*/
219HWTEST_F(MapClientTest, Avrcp_UnitTest_GetDevicesByStates, TestSize.Level1)
220{
221    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetDevicesByStates start";
222
223    profile_ = MapClient::GetProfile();
224
225    vector<int> statusList = {static_cast<int>(BTConnectState::CONNECTED)};
226    vector<BluetoothRemoteDevice> devices = profile_->GetDevicesByStates(statusList);
227    sleep(TIME);
228
229
230    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetDevicesByStates end";
231}
232
233
234/*
235 * @tc.number: MapClient009
236 * @tc.name: GetConnectionState
237 * @tc.desc: Get the Connection State object.
238*/
239HWTEST_F(MapClientTest, Avrcp_UnitTest_GetConnectionState, TestSize.Level1)
240{
241    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConnectionState start";
242
243    profile_ = MapClient::GetProfile();
244    BluetoothRemoteDevice device;
245    int ret = profile_->GetConnectionState(device);
246    sleep(TIME);
247    EXPECT_EQ(ret, 0);
248
249    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConnectionState end";
250}
251
252
253/*
254 * @tc.number: MapClient010
255 * @tc.name: SetConnectionStrategy
256 * @tc.desc: Set the connection policy of the specified device.
257*/
258HWTEST_F(MapClientTest, Avrcp_UnitTest_SetConnectionStrategy, TestSize.Level1)
259{
260    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetConnectionStrategy start";
261
262    profile_ = MapClient::GetProfile();
263    BluetoothRemoteDevice device;
264    bool ret = profile_->SetConnectionStrategy(device, 0);
265    sleep(TIME);
266    EXPECT_EQ(ret, true);
267
268    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetConnectionStrategy end";
269}
270
271
272/*
273 * @tc.number: MapClient011
274 * @tc.name: GetConnectionStrategy
275 * @tc.desc: Get the Connection Strategy object.
276*/
277HWTEST_F(MapClientTest, Avrcp_UnitTest_GetConnectionStrategy, TestSize.Level1)
278{
279    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConnectionStrategy start";
280
281    profile_ = MapClient::GetProfile();
282    BluetoothRemoteDevice device;
283    int ret = profile_->GetConnectionStrategy(device);
284    sleep(TIME);
285    EXPECT_EQ(ret, 0);
286
287    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConnectionStrategy end";
288}
289
290
291/*
292 * @tc.number: MapClient012
293 * @tc.name: GetUnreadMessages
294 * @tc.desc: Get the Unread Messages object.
295*/
296HWTEST_F(MapClientTest, Avrcp_UnitTest_GetUnreadMessages, TestSize.Level1)
297{
298    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetUnreadMessages start";
299
300    profile_ = MapClient::GetProfile();
301    BluetoothRemoteDevice device;
302    MapMessageType type = MapMessageType::SMS_GSM;
303    bool ret = profile_->GetUnreadMessages(device, type, 3);
304    sleep(TIME);
305    EXPECT_EQ(ret, true);
306
307    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetUnreadMessages end";
308}
309
310
311/*
312 * @tc.number: MapClient013
313 * @tc.name: GetSupportedFeatures
314 * @tc.desc: Get the Supported Features object.
315*/
316HWTEST_F(MapClientTest, Avrcp_UnitTest_GetSupportedFeatures, TestSize.Level1)
317{
318    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetSupportedFeatures start";
319
320    profile_ = MapClient::GetProfile();
321    BluetoothRemoteDevice device;
322    int ret = profile_->GetSupportedFeatures(device);
323    sleep(TIME);
324    EXPECT_EQ(ret, 0);
325
326    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetSupportedFeatures end";
327}
328
329
330/*
331 * @tc.number: MapClient014
332 * @tc.name: SendMessage
333 * @tc.desc: Send Message.
334*/
335HWTEST_F(MapClientTest, Avrcp_UnitTest_SendMessage, TestSize.Level1)
336{
337    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SendMessage start";
338
339    profile_ = MapClient::GetProfile();
340    BluetoothRemoteDevice device;
341    MapSendMessageParameters message;
342    int ret = profile_->SendMessage(device, message);
343    sleep(TIME);
344    EXPECT_EQ(ret, 0);
345
346    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SendMessage end";
347}
348
349
350/*
351 * @tc.number: MapClient015
352 * @tc.name: SetNotificationFilter
353 * @tc.desc: Set the Notification Filter object.
354*/
355HWTEST_F(MapClientTest, Avrcp_UnitTest_SetNotificationFilter, TestSize.Level1)
356{
357    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetNotificationFilter start";
358
359    profile_ = MapClient::GetProfile();
360    BluetoothRemoteDevice device;
361    int mask = MAP_NOTIFICATION_FILTER_MASK_NEW_MESSAGE;
362    int ret = profile_->SetNotificationFilter(device, mask);
363    sleep(TIME);
364    EXPECT_EQ(ret, 0);
365
366    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetNotificationFilter end";
367}
368
369
370/*
371 * @tc.number: MapClient016
372 * @tc.name: GetMessagesListing
373 * @tc.desc: Get the Messages Listing object.
374*/
375HWTEST_F(MapClientTest, Avrcp_UnitTest_GetMessagesListing, TestSize.Level1)
376{
377    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetMessagesListing start";
378
379    profile_ = MapClient::GetProfile();
380    BluetoothRemoteDevice device;
381    GetMessagesListingParameters para;
382    int ret = profile_->GetMessagesListing(device, para);
383    sleep(TIME);
384    EXPECT_EQ(ret, 0);
385
386    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetMessagesListing end";
387}
388
389
390/*
391 * @tc.number: MapClient017
392 * @tc.name: GetMessage
393 * @tc.desc: Get the Message object.
394*/
395HWTEST_F(MapClientTest, Avrcp_UnitTest_GetMessage, TestSize.Level1)
396{
397    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetMessage start";
398
399    profile_ = MapClient::GetProfile();
400    BluetoothRemoteDevice device;
401
402    GetMessageParameters para;
403    MapMessageType type = MapMessageType::SMS_GSM;
404    std::u16string msgHandle = {'a', 'b', 'c'};
405    int ret = profile_->GetMessage(device, type, msgHandle, para);
406    sleep(TIME);
407    EXPECT_EQ(ret, 0);
408
409    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetMessage end";
410}
411
412
413/*
414 * @tc.number: MapClient018
415 * @tc.name: UpdateInbox
416 * @tc.desc: Update Inbox.
417*/
418HWTEST_F(MapClientTest, Avrcp_UnitTest_UpdateInbox, TestSize.Level1)
419{
420    GTEST_LOG_(INFO) << "Avrcp_UnitTest_UpdateInbox start";
421
422    profile_ = MapClient::GetProfile();
423    BluetoothRemoteDevice device;
424
425    MapMessageType type = MapMessageType::SMS_GSM;
426    int ret = profile_->UpdateInbox(device, type);
427    sleep(TIME);
428    EXPECT_EQ(ret, 0);
429
430    GTEST_LOG_(INFO) << "Avrcp_UnitTest_UpdateInbox end";
431}
432
433
434/*
435 * @tc.number: MapClient019
436 * @tc.name: GetConversationListing
437 * @tc.desc: Get the Conversation Listing object.
438*/
439HWTEST_F(MapClientTest, Avrcp_UnitTest_GetConversationListing, TestSize.Level1)
440{
441    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConversationListing start";
442
443    profile_ = MapClient::GetProfile();
444    BluetoothRemoteDevice device;
445
446    GetConversationListingParameters para;
447    int ret = profile_->GetConversationListing(device, para);
448    sleep(TIME);
449    EXPECT_EQ(ret, 0);
450
451    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetConversationListing end";
452}
453
454
455/*
456 * @tc.number: MapClient020
457 * @tc.name: SetMessageStatus
458 * @tc.desc: Set the Message Status object.
459*/
460HWTEST_F(MapClientTest, Avrcp_UnitTest_SetMessageStatus, TestSize.Level1)
461{
462    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetMessageStatus start";
463
464    profile_ = MapClient::GetProfile();
465    BluetoothRemoteDevice device;
466
467    MapMessageType type = MapMessageType::SMS_GSM;
468    MapSetMessageStatus msgStatus;
469    int ret = profile_->SetMessageStatus(device, type, msgStatus);
470    sleep(TIME);
471    EXPECT_EQ(ret, 0);
472
473    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetMessageStatus end";
474}
475
476
477/*
478 * @tc.number: MapClient021
479 * @tc.name: SetOwnerStatus
480 * @tc.desc: Set the Owner Status object.
481*/
482HWTEST_F(MapClientTest, Avrcp_UnitTest_SetOwnerStatus, TestSize.Level1)
483{
484    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetOwnerStatus start";
485
486    profile_ = MapClient::GetProfile();
487    BluetoothRemoteDevice device;
488
489    SetOwnerStatusParameters para;
490    int ret = profile_->SetOwnerStatus(device, para);
491    sleep(TIME);
492    EXPECT_EQ(ret, 0);
493
494    GTEST_LOG_(INFO) << "Avrcp_UnitTest_SetOwnerStatus end";
495}
496
497
498/*
499 * @tc.number: MapClient022
500 * @tc.name: GetOwnerStatus
501 * @tc.desc: Get the Owner Status object.
502*/
503HWTEST_F(MapClientTest, Avrcp_UnitTest_GetOwnerStatus, TestSize.Level1)
504{
505    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetOwnerStatus start";
506
507    profile_ = MapClient::GetProfile();
508    BluetoothRemoteDevice device;
509
510    std::string conversationId = "GetOwnerStatus test";
511    int ret = profile_->GetOwnerStatus(device, conversationId);
512    sleep(TIME);
513    EXPECT_EQ(ret, 0);
514
515    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetOwnerStatus end";
516}
517
518
519/*
520 * @tc.number: MapClient023
521 * @tc.name: GetMasInstanceInfo
522 * @tc.desc: Get the Mas Instance Info object.
523*/
524HWTEST_F(MapClientTest, Avrcp_UnitTest_GetMasInstanceInfo, TestSize.Level1)
525{
526    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetMasInstanceInfo start";
527
528    profile_ = MapClient::GetProfile();
529    BluetoothRemoteDevice device;
530
531    profile_->GetMasInstanceInfo(device);
532    sleep(TIME);
533
534    GTEST_LOG_(INFO) << "Avrcp_UnitTest_GetMasInstanceInfo end";
535}
536
537
538}  // namespace Bluetooth
539}  // namespace OHOS