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_pan.h"
18 #include "bluetooth_remote_device.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Bluetooth {
25 using namespace std;
26 
27 class PanObserverTest : public PanObserver {
28 public:
29     PanObserverTest() = default;
30     virtual ~PanObserverTest() = default;
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)31     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) {}
32 
33 private:
34 };
35 
36 
37 static Bluetooth::Pan *profile_;
38 static std::shared_ptr<PanObserverTest> panObserverTest = std::make_shared<PanObserverTest>();
39 
40 class PanTest : public testing::Test {
41 public:
42     PanTest();
43     ~PanTest();
44 
45     static void SetUpTestCase(void);
46     static void TearDownTestCase(void);
47     void SetUp();
48     void TearDown();
49     bool CompareDevice(std::vector<BluetoothRemoteDevice> bluetoothRemoteDeviceByState,
50                        std::vector<std::string> bluetoothRemoteDeviceAddr);
51 
52 private:
53 };
54 
PanTest()55 PanTest::PanTest()
56 {
57 }
58 
~PanTest()59 PanTest::~PanTest()
60 {
61 }
62 
SetUpTestCase(void)63 void PanTest::SetUpTestCase(void)
64 {
65 }
TearDownTestCase(void)66 void PanTest::TearDownTestCase(void)
67 {
68 }
SetUp()69 void PanTest::SetUp()
70 {
71 }
TearDown()72 void PanTest::TearDown()
73 {
74 }
CompareDevice(std::vector<BluetoothRemoteDevice> bluetoothRemoteDeviceByState,std::vector<std::string> bluetoothRemoteDeviceAddr)75 bool PanTest::CompareDevice(std::vector<BluetoothRemoteDevice> bluetoothRemoteDeviceByState,
76                             std::vector<std::string> bluetoothRemoteDeviceAddr)
77 {
78     if (bluetoothRemoteDeviceByState.size() != bluetoothRemoteDeviceAddr.size()) {
79         return false;
80     }
81     for (int i = 0; i < bluetoothRemoteDeviceByState.size(); i++) {
82         if (bluetoothRemoteDeviceByState[i].GetDeviceAddr().compare(bluetoothRemoteDeviceAddr[i]) != 0) {
83             return false;
84         }
85     }
86     return true;
87 }
88 
89 /*
90  * @tc.number: PAN_ModuleTest_GetDevicesByStates_00100
91  * @tc.name: GetDevicesByStates
92  * @tc.desc:
93  */
94 HWTEST_F(PanTest, PAN_ModuleTest_GetDevicesByStates_00100, TestSize.Level1)
95 {
96     GTEST_LOG_(INFO) << "PAN_ModuleTest_GetDevicesByStates_00100 start";
97 
98     GTEST_LOG_(INFO) << "GetDevicesByStates function test";
99 
100     profile_ = Pan::GetProfile();
101     std::vector<int> states;
102     std::vector<std::string> bluetoothRemoteDeviceAddr;
103     std::vector<BluetoothRemoteDevice> devices;
104     profile_->GetDevicesByStates(states, devices);
105     EXPECT_TRUE(CompareDevice(devices, bluetoothRemoteDeviceAddr));
106 
107     GTEST_LOG_(INFO) << "PAN_ModuleTest_GetDevicesByStates_00100 end";
108 }
109 
110 /*
111  * @tc.number: PAN_ModuleTest_GetDeviceState_00100
112  * @tc.name: GetDeviceState
113  * @tc.desc:
114  */
115 HWTEST_F(PanTest, PAN_ModuleTest_GetDeviceState_00100, TestSize.Level1)
116 {
117     GTEST_LOG_(INFO) << "PAN_ModuleTest_GetDeviceState_00100 start";
118 
119     GTEST_LOG_(INFO) << "GetDeviceState function test";
120 
121     int sucess = static_cast<int>(BTConnectState::DISCONNECTED);
122     BluetoothRemoteDevice device;
123     profile_ = Pan::GetProfile();
124     int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
125     profile_->GetDeviceState(device, state);
126     EXPECT_EQ(sucess, state);
127 
128     GTEST_LOG_(INFO) << "PAN_ModuleTest_GetDeviceState_00100 end";
129 }
130 
131 /*
132  * @tc.number: PAN_ModuleTest_Disconnect_00100
133  * @tc.name: Disconnect
134  * @tc.desc:
135  */
136 HWTEST_F(PanTest, PAN_ModuleTest_Disconnect_00100, TestSize.Level1)
137 {
138     GTEST_LOG_(INFO) << "PAN_ModuleTest_Disconnect_00100 start";
139 
140     GTEST_LOG_(INFO) << "Disconnect function test";
141 
142     BluetoothRemoteDevice device;
143     profile_ = Pan::GetProfile();
144     EXPECT_TRUE(profile_->Disconnect(device));
145 
146     GTEST_LOG_(INFO) << "PAN_ModuleTest_Disconnect_00100 end";
147 }
148 
149 /*
150  * @tc.number: PAN_ModuleTest_RegisterObserver_00100
151  * @tc.name: RegisterObserver
152  * @tc.desc:
153  */
154 HWTEST_F(PanTest, PAN_ModuleTest_RegisterObserver_00100, TestSize.Level1)
155 {
156     GTEST_LOG_(INFO) << "PAN_ModuleTest_RegisterObserver_00100 start";
157 
158     GTEST_LOG_(INFO) << "RegisterObserver function test";
159 
160     profile_ = Pan::GetProfile();
161     profile_->RegisterObserver(panObserverTest);
162 
163     GTEST_LOG_(INFO) << "PAN_ModuleTest_RegisterObserver_00100 end";
164 }
165 
166 /*
167  * @tc.number: PAN_ModuleTest_DeregisterObserver_00100
168  * @tc.name: DeregisterObserver
169  * @tc.desc:
170  */
171 HWTEST_F(PanTest, PAN_ModuleTest_DeregisterObserver_00100, TestSize.Level1)
172 {
173     GTEST_LOG_(INFO) << "PAN_ModuleTest_DeregisterObserver_00100 start";
174 
175     GTEST_LOG_(INFO) << "DeregisterObserver function test";
176 
177     profile_ = Pan::GetProfile();
178     profile_->DeregisterObserver(panObserverTest);
179 
180     GTEST_LOG_(INFO) << "PAN_ModuleTest_DeregisterObserver_00100 end";
181 }
182 
183 /*
184  * @tc.number: PAN_ModuleTest_OnConnectionStateChanged_00100
185  * @tc.name: OnConnectionStateChanged
186  * @tc.desc:
187  */
188 HWTEST_F(PanTest, PAN_ModuleTest_OnConnectionStateChanged_00100, TestSize.Level1)
189 {
190     GTEST_LOG_(INFO) << "PAN_ModuleTest_OnConnectionStateChanged_00100 start";
191 
192     GTEST_LOG_(INFO) << "OnConnectionStateChanged function test";
193 
194     BluetoothRemoteDevice device;
195     int state = static_cast<int>(BTConnectState::DISCONNECTED);
196     panObserverTest->OnConnectionStateChanged(device, state, 0);
197 
198     GTEST_LOG_(INFO) << "PAN_ModuleTest_OnConnectionStateChanged_00100 end";
199 }
200 
201 /*
202  * @tc.number: PAN_ModuleTest_SetTethering_00100
203  * @tc.name: SetTethering
204  * @tc.desc:
205  */
206 HWTEST_F(PanTest, PAN_ModuleTest_SetTethering_00100, TestSize.Level1)
207 {
208     GTEST_LOG_(INFO) << "PAN_ModuleTest_SetTethering_00100 start";
209 
210     GTEST_LOG_(INFO) << "SetTethering function test";
211 
212     bool value = false;
213     profile_ = Pan::GetProfile();
214     EXPECT_TRUE(profile_->SetTethering(value));
215 
216     GTEST_LOG_(INFO) << "PAN_ModuleTest_SetTethering_00100 end";
217 }
218 
219 /*
220  * @tc.number: PAN_ModuleTest_IsTetheringOn_00100
221  * @tc.name: IsTetheringOn
222  * @tc.desc:
223  */
224 HWTEST_F(PanTest, PAN_ModuleTest_IsTetheringOn_00100, TestSize.Level1)
225 {
226     GTEST_LOG_(INFO) << "PAN_ModuleTest_IsTetheringOn_00100 start";
227 
228     GTEST_LOG_(INFO) << "IsTetheringOn function test";
229 
230     profile_ = Pan::GetProfile();
231     bool result = false;
232     profile_->IsTetheringOn(result);
233     EXPECT_FALSE(result);
234 
235     GTEST_LOG_(INFO) << "PAN_ModuleTest_IsTetheringOn_00100 end";
236 }
237 }  // namespace Bluetooth
238 }  // namespace OHOS