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_server.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 PbapObserverCommon : public PbapObserver {
29 public:
30 PbapObserverCommon() = default;
31 virtual ~PbapObserverCommon() = default;
32
OnServiceConnectionStateChanged(const BluetoothRemoteDevice & device,int state)33 void OnServiceConnectionStateChanged(const BluetoothRemoteDevice &device, int state) {}
OnServicePermission(const BluetoothRemoteDevice & device)34 void OnServicePermission(const BluetoothRemoteDevice &device) {}
OnServicePasswordRequired(const BluetoothRemoteDevice & device,const std::vector<uint8_t> & description,uint8_t charset,bool fullAccess=true)35 void OnServicePasswordRequired(const BluetoothRemoteDevice &device,
36 const std::vector<uint8_t> &description, uint8_t charset, bool fullAccess = true) {}
37
38 private:
39 };
40
41 static PbapObserverCommon observer_;
42 static PbapServer *profile_;
43
44 class PbapServerTest : public testing::Test {
45 public:
PbapServerTest()46 PbapServerTest()
47 {}
~PbapServerTest()48 ~PbapServerTest()
49 {}
50 static void SetUpTestCase(void);
51 static void TearDownTestCase(void);
52 void SetUp();
53 void TearDown();
54 BluetoothHost *host_;
55 };
56
57
SetUpTestCase(void)58 void PbapServerTest::SetUpTestCase(void) {}
TearDownTestCase(void)59 void PbapServerTest::TearDownTestCase(void) {}
60
SetUp()61 void PbapServerTest::SetUp()
62 {
63 host_ = &BluetoothHost::GetDefaultHost();
64 host_->EnableBt();
65 host_->EnableBle();
66 sleep(TIME);
67 }
68
TearDown()69 void PbapServerTest::TearDown()
70 {
71 host_->DisableBt();
72 host_->DisableBle();
73 host_ = nullptr;
74 }
75
76 /*
77 * @tc.number: PbapServerUnit001
78 * @tc.name: Disconnect
79 * @tc.desc: Release the connection from pbapserver device.
80 */
81 HWTEST_F(PbapServerTest, PbapServer_UnitTest_Disconnect, TestSize.Level1)
82 {
83 GTEST_LOG_(INFO) << "PbapServer_UnitTest_Disconnect start";
84
85 profile_ = PbapServer::GetProfile();
86 BluetoothRemoteDevice device;
87 bool isOK = profile_->Disconnect(device);
88 EXPECT_EQ(isOK, true);
89 GTEST_LOG_(INFO) << "PbapServer_UnitTest_Disconnect end";
90 }
91
92 /*
93 * @tc.number: PbapServerUnit002
94 * @tc.name: RegisterObserver
95 * @tc.desc: Register PbapServer observer instance.
96 */
97 HWTEST_F(PbapServerTest, PbapServer_UnitTest_RegisterObserver, TestSize.Level1)
98 {
99 GTEST_LOG_(INFO) << "PbapServer_UnitTest_RegisterObserver start";
100 profile_ = PbapServer::GetProfile();
101 profile_->RegisterObserver(&observer_);
102 GTEST_LOG_(INFO) << "PbapServer_UnitTest_RegisterObserver end";
103 }
104
105 /*
106 * @tc.number: PbapServerUnit003
107 * @tc.name: DeregisterObserver
108 * @tc.desc: Deregister PbapServer observer instance.
109 */
110 HWTEST_F(PbapServerTest, PbapServer_UnitTest_DeregisterObserver, TestSize.Level1)
111 {
112 GTEST_LOG_(INFO) << "PbapServer_UnitTest_DeregisterObserver start";
113 profile_ = PbapServer::GetProfile();
114 profile_->DeregisterObserver(&observer_);
115 GTEST_LOG_(INFO) << "PbapServer_UnitTest_DeregisterObserver end";
116 }
117
118 /*
119 * @tc.number: PbapServerUnit004
120 * @tc.name: GetConnectedDevices
121 * @tc.desc: Get remote PbapServer device list which are in the connected state.
122 */
123 HWTEST_F(PbapServerTest, PbapServer_UnitTest_GetConnectedDevices, TestSize.Level1)
124 {
125 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GetConnectedDevices start";
126 profile_ = PbapServer::GetProfile();
127 vector<BluetoothRemoteDevice> devices = profile_->GetConnectedDevices();
128 int vectorSize = devices.size();
129 EXPECT_EQ(vectorSize, 0);
130 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GetConnectedDevices end";
131 }
132
133 /*
134 * @tc.number: PbapServerUnit005
135 * @tc.name: GetDevicesByStates
136 * @tc.desc: Get remote PbapServer device list which are in the specified states.
137 */
138 HWTEST_F(PbapServerTest, PbapServer_UnitTest_GetDevicesByStates, TestSize.Level1)
139 {
140 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GetDevicesByStates start";
141 profile_ = PbapServer::GetProfile();
142 vector<int> states = {static_cast<int>(BTConnectState::CONNECTED)};
143 vector<BluetoothRemoteDevice> devices = profile_->GetDevicesByStates(states);
144 int vectorSize = devices.size();
145 EXPECT_EQ(vectorSize, 0);
146 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GetDevicesByStates end";
147 }
148
149 /*
150 * @tc.number: PbapServerUnit006
151 * @tc.name: GetDeviceState
152 * @tc.desc: get the state with the specified remote device
153 */
154 HWTEST_F(PbapServerTest, PbapServer_UnitTest_GetDeviceState, TestSize.Level1)
155 {
156 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GetDeviceState start";
157 profile_ = PbapServer::GetProfile();
158 BluetoothRemoteDevice device;
159 int state = profile_->GetDeviceState(device);
160 EXPECT_EQ(state, -1);
161 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GetDeviceState end";
162 }
163
164 /*
165 * @tc.number: PbapServerUnit007
166 * @tc.name: SetConnectionStrategy
167 * @tc.desc: Set the connection policy of the specified device.
168 */
169 HWTEST_F(PbapServerTest, PbapServer_UnitTest_SetConnectionStrategy, TestSize.Level1)
170 {
171 GTEST_LOG_(INFO) << "PbapServer_UnitTest_SetConnectionStrategy start";
172 profile_ = PbapServer::GetProfile();
173 BluetoothRemoteDevice device;
174 int strategy = 0;
175 bool isOk = profile_->SetConnectionStrategy(device, strategy);
176 EXPECT_EQ(isOk, false);
177 GTEST_LOG_(INFO) << "PbapServer_UnitTest_SetConnectionStrategy end";
178 }
179
180 /*
181 * @tc.number: PbapServerUnit008
182 * @tc.name: GetConnectionStrategy
183 * @tc.desc: Get the connection policy of the specified device.
184 */
185 HWTEST_F(PbapServerTest, PbapServer_UnitTest_GetConnectionStrategy, TestSize.Level1)
186 {
187 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GetConnectionStrategy start";
188 profile_ = PbapServer::GetProfile();
189 BluetoothRemoteDevice device;
190 int ret = profile_->GetConnectionStrategy(device);
191 EXPECT_EQ(ret, 0);
192 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GetConnectionStrategy end";
193 }
194
195 /*
196 * @tc.number: PbapServerUnit009
197 * @tc.name: GrantPermission
198 * @tc.desc: Set whether to authorize the connection.
199 */
200 HWTEST_F(PbapServerTest, PbapServer_UnitTest_GrantPermission, TestSize.Level1)
201 {
202 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GrantPermission start";
203 profile_ = PbapServer::GetProfile();
204 BluetoothRemoteDevice device;
205 bool allow = true;
206 bool save = false;
207 profile_->GrantPermission(device, allow, save);
208 GTEST_LOG_(INFO) << "PbapServer_UnitTest_GrantPermission end";
209 }
210
211 /*
212 * @tc.number: PbapServerUnit010
213 * @tc.name: SetDevicePassword
214 * @tc.desc: Set device's password. please call after OnServicePasswordRequired event.
215 */
216 HWTEST_F(PbapServerTest, PbapServer_UnitTest_SetDevicePassword, TestSize.Level1)
217 {
218 GTEST_LOG_(INFO) << "PbapServer_UnitTest_SetDevicePassword start";
219 profile_ = PbapServer::GetProfile();
220 BluetoothRemoteDevice device;
221 string password = "123";
222 string userId = "456";
223 int state = profile_->SetDevicePassword(device, password, userId);
224 EXPECT_EQ(state, -1);
225 GTEST_LOG_(INFO) << "PbapServer_UnitTest_SetDevicePassword end";
226 }
227 } // namespace Bluetooth
228 } // namespace OHOS