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