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_mse.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 MapServerObserverCommon : public MapServerObserver {
29 public:
30     MapServerObserverCommon() = default;
31     virtual ~MapServerObserverCommon() = default;
32 
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)33     void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) {}
OnPermission(const BluetoothRemoteDevice & device)34     void OnPermission(const BluetoothRemoteDevice &device) {}
35 
36 private:
37 };
38 
39 static MapServerObserverCommon observer_;
40 static MapServer *profile_;
41 
42 class MapServerTest : public testing::Test {
43 public:
MapServerTest()44     MapServerTest()
45     {}
~MapServerTest()46     ~MapServerTest()
47     {}
48     static void SetUpTestCase(void);
49     static void TearDownTestCase(void);
50     void SetUp();
51     void TearDown();
52     BluetoothHost *host_;
53 };
54 
55 
SetUpTestCase(void)56 void MapServerTest::SetUpTestCase(void) {}
TearDownTestCase(void)57 void MapServerTest::TearDownTestCase(void) {}
58 
SetUp()59 void MapServerTest::SetUp()
60 {
61     host_ = &BluetoothHost::GetDefaultHost();
62     host_->EnableBt();
63     host_->EnableBle();
64     sleep(TIME);
65 }
66 
TearDown()67 void MapServerTest::TearDown()
68 {
69     host_->DisableBt();
70     host_->DisableBle();
71     host_ = nullptr;
72 }
73 /*
74  * @tc.number: MapServerUnit001
75  * @tc.name: Disconnect
76  * @tc.desc: Disconnect bluetooth connection service.
77 */
78 HWTEST_F(MapServerTest, MapServer_UnitTest_Disconnect, TestSize.Level1)
79 {
80     GTEST_LOG_(INFO) << "MapServer_UnitTest_Disconnect start";
81 
82     profile_ = MapServer::GetProfile();
83     BluetoothRemoteDevice device;
84     bool isOK = profile_->Disconnect(device);
85     EXPECT_EQ(isOK, true);
86     GTEST_LOG_(INFO) << "MapServer_UnitTest_Disconnect end";
87 }
88 
89 /*
90  * @tc.number: MapServerUnit002
91  * @tc.name: RegisterObserver
92  * @tc.desc: Register the callback of map server.
93 */
94 HWTEST_F(MapServerTest, MapServer_UnitTest_RegisterObserver, TestSize.Level1)
95 {
96     GTEST_LOG_(INFO) << "MapServer_UnitTest_RegisterObserver start";
97     profile_ = MapServer::GetProfile();
98     profile_->RegisterObserver(observer_);
99     GTEST_LOG_(INFO) << "MapServer_UnitTest_RegisterObserver end";
100 }
101 
102 /*
103  * @tc.number: MapServerUnit003
104  * @tc.name: DeregisterObserver
105  * @tc.desc: Unregister the callback of map server.
106 */
107 HWTEST_F(MapServerTest, MapServer_UnitTest_DeregisterObserver, TestSize.Level1)
108 {
109     GTEST_LOG_(INFO) << "MapServer_UnitTest_DeregisterObserver start";
110     profile_ = MapServer::GetProfile();
111     profile_->DeregisterObserver(observer_);
112     GTEST_LOG_(INFO) << "MapServer_UnitTest_DeregisterObserver end";
113 }
114 
115 /*
116  * @tc.number: MapServerUnit004
117  * @tc.name: GetState
118  * @tc.desc: Get the connection status of the server.
119 */
120 HWTEST_F(MapServerTest, MapServer_UnitTest_GetState, TestSize.Level1)
121 {
122     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetState start";
123     profile_ = MapServer::GetProfile();
124     int ret = profile_->GetState();
125     EXPECT_EQ(ret, 0);
126     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetState end";
127 }
128 
129 /*
130  * @tc.number: MapServerUnit005
131  * @tc.name: IsConnected
132  * @tc.desc: Get whether bluetooth device is connected.
133 */
134 HWTEST_F(MapServerTest, MapServer_UnitTest_IsConnected, TestSize.Level1)
135 {
136     GTEST_LOG_(INFO) << "MapServer_UnitTest_IsConnected start";
137     profile_ = MapServer::GetProfile();
138     BluetoothRemoteDevice device;
139     bool isOk = profile_->IsConnected(device);
140     EXPECT_EQ(isOk, false);
141     GTEST_LOG_(INFO) << "MapServer_UnitTest_IsConnected end";
142 }
143 
144 /*
145  * @tc.number: MapServerUnit006
146  * @tc.name: GetConnectedDevices
147  * @tc.desc: Get the list of connected bluetooth devices.
148 */
149 HWTEST_F(MapServerTest, MapServer_UnitTest_GetConnectedDevices, TestSize.Level1)
150 {
151     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetConnectedDevices start";
152     profile_ = MapServer::GetProfile();
153     vector<BluetoothRemoteDevice> devices = profile_->GetConnectedDevices();
154     int vectorSize = devices.size();
155     EXPECT_EQ(vectorSize, 0);
156     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetConnectedDevices end";
157 }
158 
159 /*
160  * @tc.number: MapServerUnit007
161  * @tc.name: GetDevicesByStates
162  * @tc.desc: Get the device list through the connection status.
163 */
164 HWTEST_F(MapServerTest, MapServer_UnitTest_GetDevicesByStates, TestSize.Level1)
165 {
166     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetDevicesByStates start";
167     profile_ = MapServer::GetProfile();
168     vector<int> states = {static_cast<int>(BTConnectState::CONNECTED)};
169     vector<BluetoothRemoteDevice> devices = profile_->GetDevicesByStates(states);
170     int vectorSize = devices.size();
171     EXPECT_EQ(vectorSize, 0);
172     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetDevicesByStates end";
173 }
174 
175 /*
176  * @tc.number: MapServerUnit008
177  * @tc.name: GetConnectionState
178  * @tc.desc: Get the connection status of the specified device.
179 */
180 HWTEST_F(MapServerTest, MapServer_UnitTest_GetConnectionState, TestSize.Level1)
181 {
182     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetConnectionState start";
183     profile_ = MapServer::GetProfile();
184     BluetoothRemoteDevice device;
185     int state = profile_->GetConnectionState(device);
186     EXPECT_EQ(state, -1);
187     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetConnectionState end";
188 }
189 
190 /*
191  * @tc.number: MapServerUnit009
192  * @tc.name: SetConnectionStrategy
193  * @tc.desc: Set the connection policy of the specified device.
194 */
195 HWTEST_F(MapServerTest, MapServer_UnitTest_SetConnectionStrategy, TestSize.Level1)
196 {
197     GTEST_LOG_(INFO) << "MapServer_UnitTest_SetConnectionStrategy start";
198     profile_ = MapServer::GetProfile();
199     BluetoothRemoteDevice device;
200     int strategy = 0;
201     bool isOk = profile_->SetConnectionStrategy(device, strategy);
202     EXPECT_EQ(isOk, false);
203     GTEST_LOG_(INFO) << "MapServer_UnitTest_SetConnectionStrategy end";
204 }
205 
206 /*
207  * @tc.number: MapServerUnit010
208  * @tc.name: GetConnectionStrategy
209  * @tc.desc: Get the connection policy of the specified device.
210 */
211 HWTEST_F(MapServerTest, MapServer_UnitTest_GetConnectionStrategy, TestSize.Level1)
212 {
213     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetConnectionStrategy start";
214     profile_ = MapServer::GetProfile();
215     BluetoothRemoteDevice device;
216     int ret = profile_->GetConnectionStrategy(device);
217     EXPECT_EQ(ret, 0);
218     GTEST_LOG_(INFO) << "MapServer_UnitTest_GetConnectionStrategy end";
219 }
220 
221 /*
222  * @tc.number: MapServerUnit011
223  * @tc.name: GrantPermission
224  * @tc.desc: Set whether to authorize the connection.
225 */
226 HWTEST_F(MapServerTest, MapServer_UnitTest_GrantPermission, TestSize.Level1)
227 {
228     GTEST_LOG_(INFO) << "MapServer_UnitTest_GrantPermission start";
229     profile_ = MapServer::GetProfile();
230     BluetoothRemoteDevice device;
231     bool allow = true;
232     bool save = false;
233     profile_->GrantPermission(device, allow, save);
234     GTEST_LOG_(INFO) << "MapServer_UnitTest_GrantPermission end";
235 }
236 }  // namespace Bluetooth
237 }  // namespace OHOS