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