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
16 #include <gtest/gtest.h>
17 #include <thread>
18
19 #include "device_manager.h"
20 #include "distributeddb_tools_unit_test.h"
21 #include "kv_virtual_device.h"
22 #include "log_print.h"
23 #include "parcel.h"
24 #include "sync_types.h"
25 #include "virtual_communicator.h"
26 #include "virtual_communicator_aggregator.h"
27 #include "virtual_single_ver_sync_db_Interface.h"
28
29 using namespace testing::ext;
30 using namespace DistributedDB;
31 using namespace std;
32
33 namespace {
34 const std::string DEVICE_B = "deviceB";
35 const std::string DEVICE_C = "deviceC";
36 VirtualCommunicatorAggregator* g_communicatorAggregator = nullptr;
37 VirtualCommunicator* g_virtualCommunicator = nullptr;
38 KvVirtualDevice *g_deviceB = nullptr;
39 KvVirtualDevice *g_deviceC = nullptr;
40 DeviceManager *g_deviceManager = nullptr;
41 #ifndef OMIT_MULTI_VER
42 const int WAIT_TIME = 1000;
43 #endif
44
45 class DistributedDBSyncerDeviceManagerTest : public testing::Test {
46 public:
47 static void SetUpTestCase(void);
48 static void TearDownTestCase(void);
49 void SetUp();
50 void TearDown();
51 };
52
SetUpTestCase(void)53 void DistributedDBSyncerDeviceManagerTest::SetUpTestCase(void)
54 {
55 /**
56 * @tc.setup: Virtual Communicator.
57 */
58 g_communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator();
59 ASSERT_TRUE(g_communicatorAggregator != nullptr);
60 RuntimeContext::GetInstance()->SetCommunicatorAggregator(g_communicatorAggregator);
61 int errCode;
62 g_virtualCommunicator = static_cast<VirtualCommunicator *>(g_communicatorAggregator->AllocCommunicator(0, errCode));
63 ASSERT_TRUE(g_virtualCommunicator != nullptr);
64 }
65
TearDownTestCase(void)66 void DistributedDBSyncerDeviceManagerTest::TearDownTestCase(void)
67 {
68 /**
69 * @tc.setup: Release Virtual CommunicatorAggregator.
70 */
71 RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
72 g_communicatorAggregator = nullptr;
73 }
74
SetUp(void)75 void DistributedDBSyncerDeviceManagerTest::SetUp(void)
76 {
77 DistributedDBUnitTest::DistributedDBToolsUnitTest::PrintTestCaseInfo();
78 /**
79 * @tc.setup: Init a DeviceManager and DeviceB, C
80 */
81 g_deviceManager = new (std::nothrow) DeviceManager;
82 ASSERT_TRUE(g_deviceManager != nullptr);
83 g_deviceManager->Initialize(g_virtualCommunicator, nullptr, nullptr);
84 g_virtualCommunicator->RegOnConnectCallback(
85 std::bind(&DeviceManager::OnDeviceConnectCallback, g_deviceManager,
86 std::placeholders::_1, std::placeholders::_2), nullptr);
87
88 g_deviceB = new (std::nothrow) KvVirtualDevice(DEVICE_B);
89 ASSERT_TRUE(g_deviceB != nullptr);
90 VirtualSingleVerSyncDBInterface *syncInterfaceB = new (std::nothrow) VirtualSingleVerSyncDBInterface();
91 ASSERT_TRUE(syncInterfaceB != nullptr);
92 ASSERT_EQ(g_deviceB->Initialize(g_communicatorAggregator, syncInterfaceB), E_OK);
93
94 g_deviceC = new (std::nothrow) KvVirtualDevice(DEVICE_C);
95 ASSERT_TRUE(g_deviceC != nullptr);
96 VirtualSingleVerSyncDBInterface *syncInterfaceC = new (std::nothrow) VirtualSingleVerSyncDBInterface();
97 ASSERT_TRUE(syncInterfaceC != nullptr);
98 ASSERT_EQ(g_deviceC->Initialize(g_communicatorAggregator, syncInterfaceC), E_OK);
99 }
100
TearDown(void)101 void DistributedDBSyncerDeviceManagerTest::TearDown(void)
102 {
103 /**
104 * @tc.setup: Release a DeviceManager and DeviceB, C
105 */
106 if (g_deviceManager != nullptr) {
107 g_virtualCommunicator->RegOnConnectCallback(nullptr, nullptr);
108 delete g_deviceManager;
109 g_deviceManager = nullptr;
110 }
111 if (g_deviceB != nullptr) {
112 delete g_deviceB;
113 g_deviceB = nullptr;
114 }
115 if (g_deviceC != nullptr) {
116 delete g_deviceC;
117 g_deviceC = nullptr;
118 }
119 }
120
121 /**
122 * @tc.name: Online Callback 001
123 * @tc.desc: Test DeviceManager device online callback function.
124 * @tc.type: FUNC
125 * @tc.require: AR000CKRTD AR000CQE0E
126 * @tc.author: xushaohua
127 */
128 HWTEST_F(DistributedDBSyncerDeviceManagerTest, OnlineCallback001, TestSize.Level0)
129 {
130 bool onlineCalled = false;
131
132 /**
133 * @tc.steps: step1. set device online callback
134 */
__anon77278ea40202(const std::string &targetDev) 135 g_deviceManager->RegDeviceOnLineCallBack([&onlineCalled](const std::string &targetDev) {
136 LOGD("DeviceManageTest online called, dev %s", targetDev.c_str());
137 if (targetDev == g_deviceB->GetDeviceId()) {
138 LOGD("DEVICE TEST CALL ONLINE CALLBACK");
139 onlineCalled = true;
140 }
141 });
142
143 /**
144 * @tc.steps: step2. deviceB online
145 * @tc.expected: step2, the online callback should be called.
146 */
147 g_communicatorAggregator->OnlineDevice(g_deviceB->GetDeviceId());
148 EXPECT_TRUE(onlineCalled);
149 }
150
151 /**
152 * @tc.name: Offline Callback 001
153 * @tc.desc: Test DeviceManager device offline callback function.
154 * @tc.type: FUNC
155 * @tc.require: AR000CKRTD AR000CQE0E
156 * @tc.author: xushaohua
157 */
158 HWTEST_F(DistributedDBSyncerDeviceManagerTest, OfflineCallback001, TestSize.Level0)
159 {
160 bool offlineCalled = false;
161 g_communicatorAggregator->OnlineDevice(g_deviceB->GetDeviceId());
162
163 /**
164 * @tc.steps: step1. set device offline callback
165 */
__anon77278ea40302(const std::string &targetDev) 166 g_deviceManager->RegDeviceOffLineCallBack([&offlineCalled](const std::string &targetDev) {
167 LOGD("DeviceManageTest offline called, dev %s", targetDev.c_str());
168 if (targetDev == g_deviceB->GetDeviceId()) {
169 offlineCalled = true;
170 }
171 });
172
173 /**
174 * @tc.steps: step2. deviceB offline
175 * @tc.expected: step2, the offline callback should be called.
176 */
177 g_communicatorAggregator->OfflineDevice(g_deviceB->GetDeviceId());
178 EXPECT_TRUE(offlineCalled);
179 }
180
181 /**
182 * @tc.name: Get Devices 001
183 * @tc.desc: Test DeviceManager GetDevices function.
184 * @tc.type: FUNC
185 * @tc.require: AR000CKRTD AR000CQE0E
186 * @tc.author: xushaohua
187 */
188 HWTEST_F(DistributedDBSyncerDeviceManagerTest, GetDevices001, TestSize.Level0)
189 {
190 std::vector<std::string> deviceList;
191
192 /**
193 * @tc.steps: step1. call GetDevices
194 * @tc.expected: step1, GetDevices return deviceB,C
195 */
196 g_deviceManager->GetOnlineDevices(deviceList);
197 ASSERT_TRUE(deviceList.size() == 2);
198 EXPECT_TRUE(deviceList[0] == g_deviceB->GetDeviceId());
199 EXPECT_TRUE(deviceList[1] == g_deviceC->GetDeviceId());
200 g_communicatorAggregator->OfflineDevice(g_deviceC->GetDeviceId());
201
202 /**
203 * @tc.steps: step2. deiceC offline and call GetDevices
204 * @tc.expected: step2, GetDevices return deviceB
205 */
206 g_deviceManager->GetOnlineDevices(deviceList);
207 ASSERT_TRUE(deviceList.size() == 1);
208 EXPECT_TRUE(deviceList[0] == g_deviceB->GetDeviceId());
209 }
210
211 #ifndef OMIT_MULTI_VER
212 /**
213 * @tc.name: Send BroadCast 001
214 * @tc.desc: Test DeviceManager SendBroadCast function.
215 * @tc.type: FUNC
216 * @tc.require: AR000CKRTD AR000CQE0E
217 * @tc.author: xushaohua
218 */
219 HWTEST_F(DistributedDBSyncerDeviceManagerTest, SendBroadCast001, TestSize.Level1)
220 {
221 bool deviceBReviced = false;
222 bool deviceCReviced = false;
223
224 /**
225 * @tc.steps: step1. deviceB, C set OnRemoteDataChanged callback
226 */
__anon77278ea40402(const std::string &deviceId)227 g_deviceB->OnRemoteDataChanged([&deviceBReviced](const std::string &deviceId){
228 deviceBReviced = true;
229 });
__anon77278ea40502(const std::string &deviceId)230 g_deviceC->OnRemoteDataChanged([&deviceCReviced](const std::string &deviceId){
231 deviceCReviced = true;
232 });
233
234 /**
235 * @tc.steps: step2. call SendBroadCast.
236 * @tc.expected: step2, deviceB,C OnRemoteDataChanged should be called
237 */
238 int errCode = g_deviceManager->SendBroadCast(LOCAL_DATA_CHANGED);
239 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
240 ASSERT_TRUE(errCode == E_OK);
241 EXPECT_TRUE(deviceBReviced);
242 EXPECT_TRUE(deviceCReviced);
243 }
244 #endif // OMIT_MULTI_VER
245 }