1 /*
2  * Copyright (C) 2022 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 <gtest/gtest.h>
17 #include "bluetooth_opp.h"
18 #include "bluetooth_remote_device.h"
19 #include "bluetooth_host.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 using namespace std;
27 
28 class OppObserverTest : public OppObserver {
29 public:
30     OppObserverTest() = default;
31     virtual ~OppObserverTest() = default;
OnReceiveIncomingFileChanged(const BluetoothOppTransferInformation & transferInformation)32     virtual void OnReceiveIncomingFileChanged(const BluetoothOppTransferInformation &transferInformation) {}
OnTransferStateChanged(const BluetoothOppTransferInformation & transferInformation)33     virtual void OnTransferStateChanged(const BluetoothOppTransferInformation &transferInformation) {}
34 
35 private:
36 };
37 
38 
39 static Bluetooth::Opp  *profile_;
40 static std::shared_ptr<OppObserverTest> g_oppObserverTest = std::make_shared<OppObserverTest>();
41 
42 class OppTest : public testing::Test {
43 public:
44     OppTest();
45     ~OppTest();
46 
47     static void SetUpTestCase(void);
48     static void TearDownTestCase(void);
49     void SetUp();
50     void TearDown();
51     bool CompareDevice(std::vector<BluetoothRemoteDevice> bluetoothRemoteDeviceByState,
52         std::vector<std::string> bluetoothRemoteDeviceAddr);
53     bool CompareTransferInformation(const BluetoothOppTransferInformation &information,
54         const BluetoothOppTransferInformation &transferInformation);
55 
56 private:
57 };
58 
OppTest()59 OppTest::OppTest()
60 {
61 }
62 
~OppTest()63 OppTest::~OppTest()
64 {
65 }
66 
SetUpTestCase(void)67 void OppTest::SetUpTestCase(void)
68 {
69 }
TearDownTestCase(void)70 void OppTest::TearDownTestCase(void)
71 {
72 }
SetUp()73 void OppTest::SetUp()
74 {
75 }
TearDown()76 void OppTest::TearDown()
77 {
78 }
79 
CompareDevice(std::vector<BluetoothRemoteDevice> bluetoothRemoteDeviceByState,std::vector<std::string> bluetoothRemoteDeviceAddr)80 bool OppTest::CompareDevice(std::vector<BluetoothRemoteDevice> bluetoothRemoteDeviceByState,
81     std::vector<std::string> bluetoothRemoteDeviceAddr)
82 {
83     if (bluetoothRemoteDeviceByState.size() != bluetoothRemoteDeviceAddr.size()) {
84         return false;
85     }
86     for (int i = 0; i < bluetoothRemoteDeviceByState.size(); i++) {
87         if (bluetoothRemoteDeviceByState[i].GetDeviceAddr().compare(bluetoothRemoteDeviceAddr[i]) != 0) {
88             return false;
89         }
90     }
91     return true;
92 }
93 
CompareTransferInformation(const BluetoothOppTransferInformation & information,const BluetoothOppTransferInformation & transferInformation)94 bool OppTest::CompareTransferInformation(const BluetoothOppTransferInformation &information,
95     const BluetoothOppTransferInformation &transferInformation)
96 {
97     if (&information == &transferInformation) {
98         return true;
99     }
100     if ((information.GetFileName() != transferInformation.GetFileName()) ||
101         (information.GetFilePath() != transferInformation.GetFilePath()) ||
102         (information.GetMimeType() != transferInformation.GetMimeType()) ||
103         (information.GetDeviceName() != transferInformation.GetDeviceName()) ||
104         (information.GetDeviceAddress() != transferInformation.GetDeviceAddress()) ||
105         (information.GetTimeStamp() != transferInformation.GetTimeStamp()) ||
106         (information.GetCurrentBytes() != transferInformation.GetCurrentBytes()) ||
107         (information.GetTotalBytes() != transferInformation.GetTotalBytes()) ||
108         (information.GetFailedReason() != transferInformation.GetFailedReason()) ||
109         (information.GetDirection() != transferInformation.GetDirection()) ||
110         (information.GetStatus() != transferInformation.GetStatus()) ||
111         (information.GetId() != transferInformation.GetId())) {
112         return false;
113     }
114     return true;
115 }
116 
117 /*
118  * @tc.number: OPP_ModuleTest_GetDevicesByStates_00100
119  * @tc.name: GetDevicesByStates
120  * @tc.desc:
121  */
122 HWTEST_F(OppTest, OPP_ModuleTest_GetDevicesByStates_00100, TestSize.Level1)
123 {
124     GTEST_LOG_(INFO) << "OPP_ModuleTest_GetDevicesByStates_00100 start";
125 
126     GTEST_LOG_(INFO) << "GetDevicesByStates function test";
127 
128     profile_ = Opp::GetProfile();
129     std::vector<int> states;
130     std::vector<std::string> bluetoothRemoteDeviceAddr;
131     std::vector<BluetoothRemoteDevice> bluetoothRemoteDeviceByState = profile_->GetDevicesByStates(states);
132     EXPECT_TRUE(CompareDevice(bluetoothRemoteDeviceByState, bluetoothRemoteDeviceAddr));
133 
134     GTEST_LOG_(INFO) << "OPP_ModuleTest_GetDevicesByStates_00100 end";
135 }
136 
137 
138 /*
139  * @tc.number: OPP_ModuleTest_GetDeviceState_00100
140  * @tc.name: GetDeviceState
141  * @tc.desc:
142  */
143 HWTEST_F(OppTest, OPP_ModuleTest_GetDeviceState_00100, TestSize.Level1)
144 {
145     GTEST_LOG_(INFO) << "OPP_ModuleTest_GetDeviceState_00100 start";
146 
147     GTEST_LOG_(INFO) << "GetDeviceState function test";
148 
149     int sucess = static_cast<int>(BTConnectState::DISCONNECTED);
150     BluetoothRemoteDevice device;
151     profile_ = Opp::GetProfile();
152     EXPECT_EQ(sucess, profile_->GetDeviceState(device));
153 
154     GTEST_LOG_(INFO) << "OPP_ModuleTest_GetDeviceState_00100 end";
155 }
156 
157 /*
158  * @tc.number: OPP_ModuleTest_SendFile_00100
159  * @tc.name: SendFile
160  * @tc.desc:
161  */
162 HWTEST_F(OppTest, OPP_ModuleTest_SendFile_00100, TestSize.Level1)
163 {
164     GTEST_LOG_(INFO) << "OPP_ModuleTest_SendFile_00100 start";
165 
166     GTEST_LOG_(INFO) << "SendFile function test";
167 
168     std::string device;
169     std::vector<std::string>filePaths;
170     std::vector<std::string>mimeTypes;
171     profile_ = Opp::GetProfile();
172     EXPECT_FALSE(profile_->SendFile(device, filePaths, mimeTypes));
173 
174     GTEST_LOG_(INFO) << "OPP_ModuleTest_SendFile_00100 end";
175 }
176 
177 /*
178  * @tc.number: OPP_ModuleTest_SetIncomingFileConfirmation_00100
179  * @tc.name: SetIncomingFileConfirmation
180  * @tc.desc:
181  */
182 HWTEST_F(OppTest, OPP_ModuleTest_SetIncomingFileConfirmation_00100, TestSize.Level1)
183 {
184     GTEST_LOG_(INFO) << "OPP_ModuleTest_SetIncomingFileConfirmation_00100 start";
185 
186     GTEST_LOG_(INFO) << "SetIncomingFileConfirmation function test";
187 
188     bool accept = true;
189     profile_ = Opp::GetProfile();
190     EXPECT_FALSE(profile_->SetIncomingFileConfirmation(accept));
191 
192     GTEST_LOG_(INFO) << "OPP_ModuleTest_SetIncomingFileConfirmation_00100 end";
193 }
194 
195 /*
196  * @tc.number: OPP_ModuleTest_GetCurrentTransferInformation_00100
197  * @tc.name: GetCurrentTransferInformation
198  * @tc.desc:
199  */
200 HWTEST_F(OppTest, OPP_ModuleTest_GetCurrentTransferInformation_00100, TestSize.Level1)
201 {
202     GTEST_LOG_(INFO) << "OPP_ModuleTest_GetCurrentTransferInformation_00100 start";
203 
204     GTEST_LOG_(INFO) << "GetCurrentTransferInformation function test";
205 
206     profile_ = Opp::GetProfile();
207     BluetoothOppTransferInformation Infermation;
208     BluetoothOppTransferInformation transferInfermation = profile_->GetCurrentTransferInformation();
209     EXPECT_TRUE(CompareTransferInformation(Infermation, transferInfermation));
210 
211     GTEST_LOG_(INFO) << "OPP_ModuleTest_GetCurrentTransferInformation_00100 end";
212 }
213 
214 /*
215  * @tc.number: OPP_ModuleTest_CancelTransfer_00100
216  * @tc.name: CancelTransfer
217  * @tc.desc:
218  */
219 HWTEST_F(OppTest, OPP_ModuleTest_CancelTransfer_00100, TestSize.Level1)
220 {
221     GTEST_LOG_(INFO) << "OPP_ModuleTest_CancelTransfer_00100 start";
222 
223     GTEST_LOG_(INFO) << "CancelTransfer function test";
224 
225     profile_ = Opp::GetProfile();
226     EXPECT_FALSE(profile_->CancelTransfer());
227 
228     GTEST_LOG_(INFO) << "OPP_ModuleTest_CancelTransfer_00100 end";
229 }
230 
231 /*
232  * @tc.number: OPP_ModuleTest_RegisterObserver_00100
233  * @tc.name: RegisterObserver
234  * @tc.desc:
235  */
236 HWTEST_F(OppTest, OPP_ModuleTest_RegisterObserver_00100, TestSize.Level1)
237 {
238     GTEST_LOG_(INFO) << "OPP_ModuleTest_RegisterObserver_00100 start";
239 
240     GTEST_LOG_(INFO) << "RegisterObserver function test";
241 
242     profile_ = Opp::GetProfile();
243     profile_->RegisterObserver(g_oppObserverTest);
244 
245     GTEST_LOG_(INFO) << "OPP_ModuleTest_RegisterObserver_00100 end";
246 }
247 
248 /*
249  * @tc.number: OPP_ModuleTest_DeregisterObserver_00100
250  * @tc.name: DeregisterObserver
251  * @tc.desc:
252  */
253 HWTEST_F(OppTest, OPP_ModuleTest_DeregisterObserver_00100, TestSize.Level1)
254 {
255     GTEST_LOG_(INFO) << "OPP_ModuleTest_DeregisterObserver_00100 start";
256 
257     GTEST_LOG_(INFO) << "DeregisterObserver function test";
258 
259     profile_ = Opp::GetProfile();
260     profile_->DeregisterObserver(g_oppObserverTest);
261 
262     GTEST_LOG_(INFO) << "OPP_ModuleTest_DeregisterObserver_00100 end";
263 }
264 
265 /*
266  * @tc.number: OPP_ModuleTest_OnReceiveIncomingFileChanged_00100
267  * @tc.name: OnReceiveIncomingFileChanged
268  * @tc.desc:
269  */
270 HWTEST_F(OppTest, OPP_ModuleTest_OnReceiveIncomingFileChanged_00100, TestSize.Level1)
271 {
272     GTEST_LOG_(INFO) << "OPP_ModuleTest_OnReceiveIncomingFileChanged_00100 start";
273 
274     GTEST_LOG_(INFO) << "OnReceiveIncomingFileChanged function test";
275 
276     BluetoothOppTransferInformation trasnferInformation;
277     g_oppObserverTest->OnReceiveIncomingFileChanged(trasnferInformation);
278 
279     GTEST_LOG_(INFO) << "OPP_ModuleTest_OnReceiveIncomingFileChanged_00100 end";
280 }
281 
282 /*
283  * @tc.number: OPP_ModuleTest_OnTransferStateChanged_00100
284  * @tc.name: OnTransferStateChanged
285  * @tc.desc:
286  */
287 HWTEST_F(OppTest, OPP_ModuleTest_OnTransferStateChanged_00100, TestSize.Level1)
288 {
289     GTEST_LOG_(INFO) << "OPP_ModuleTest_OnTransferStateChanged_00100 start";
290 
291     GTEST_LOG_(INFO) << "OnTransferStateChanged function test";
292 
293     BluetoothOppTransferInformation trasnferInformation;
294     g_oppObserverTest->OnTransferStateChanged(trasnferInformation);
295 
296     GTEST_LOG_(INFO) << "OPP_ModuleTest_OnTransferStateChanged_00100 end";
297 }
298 }  // namespace Bluetooth
299 }  // namespace OHOS
300