• 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 <iostream>
17 #include <string>
18 #include <unistd.h>
19 
20 #include "dm_log.h"
21 #include "dm_constants.h"
22 #include "dm_anonymous.h"
23 #include "ipc_server_listener.h"
24 #include "device_manager_service_listener.h"
25 #include "discovery_service.h"
26 #include "UTTest_dm_discovery_manager.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
SetUp()30 void DmDiscoveryManagerTest::SetUp()
31 {
32 }
33 
TearDown()34 void DmDiscoveryManagerTest::TearDown()
35 {
36 }
37 
SetUpTestCase()38 void DmDiscoveryManagerTest::SetUpTestCase()
39 {
40 }
41 
TearDownTestCase()42 void DmDiscoveryManagerTest::TearDownTestCase()
43 {
44 }
45 
46 namespace {
47 std::shared_ptr<SoftbusConnector> softbusConnector_ = std::make_shared<SoftbusConnector>();
48 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
49 std::shared_ptr<DmDiscoveryManager> discoveryMgr_ = std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_);
50 
51 /**
52  * @tc.name: DmDiscoveryManager_001
53  * @tc.desc: Test whether the DmDiscoveryManager function can generate a new pointer
54  * @tc.type: FUNC
55  * @tc.require: AR000GHSJK
56  */
57 HWTEST_F(DmDiscoveryManagerTest, DmDiscoveryManager_001, testing::ext::TestSize.Level0)
58 {
59     std::shared_ptr<DmDiscoveryManager> Test = std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_);
60     ASSERT_NE(Test, nullptr);
61 }
62 
63 /**
64  * @tc.name: DmDiscoveryManager_002
65  * @tc.desc: Test whether the DmDiscoveryManager function can delete a new pointer
66  * @tc.type: FUNC
67  * @tc.require: AR000GHSJK
68  */
69 HWTEST_F(DmDiscoveryManagerTest, DmDiscoveryManager_002, testing::ext::TestSize.Level0)
70 {
71     std::shared_ptr<DmDiscoveryManager> Test = std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_);
72     Test.reset();
73     EXPECT_EQ(Test, nullptr);
74 }
75 
76 /**
77  * @tc.name:StartDeviceDiscovery_001
78  * @tc.desc: keeping pkgame unchanged, call StartDeviceDiscovery twice
79  *           so that its discoveryQueue is not empty and return DM_DISCOVERY_REPEATED
80  * @tc.type: FUNC
81  * @tc.require: AR000GHSJK
82  */
83 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_001, testing::ext::TestSize.Level0)
84 {
85     std::string pkgName = "com.ohos.helloworld";
86     DmSubscribeInfo subscribeInfo;
87     const std::string extra;
88     discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
89     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
90     EXPECT_EQ(ret, DM_DISCOVERY_REPEATED);
91     discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeInfo.subscribeId);
92 }
93 
94 /**
95  * @tc.name:StartDeviceDiscovery_002
96  * @tc.desc: pkgame changed, call StartDeviceDiscovery twice
97  *           so that its discoveryQueue is not empty and return DM_DISCOVERY_REPEATED
98  * @tc.type: FUNC
99  * @tc.require: AR000GHSJK
100  */
101 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_002, testing::ext::TestSize.Level0)
102 {
103     std::string pkgName = "com.ohos.helloworld";
104     std::string extra;
105     DmSubscribeInfo subscribeInfo;
106     discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
107     pkgName = "com.ohos.helloworld.new";
108     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
109     ASSERT_EQ(ret, DM_DISCOVERY_FAILED);
110     discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeInfo.subscribeId);
111 }
112 
113 /**
114  * @tc.name: OnDeviceFound_001
115  * @tc.desc: The OnDeviceFound function does the correct case and assigns pkgName
116  * @tc.type: FUNC
117  * @tc.require: AR000GHSJK
118  */
119 HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_001, testing::ext::TestSize.Level0)
120 {
121     std::string pkgName = "com.ohos.helloworld";
122     uint16_t aaa = 11;
123     DmDiscoveryContext context { pkgName, "121110", aaa };
124     discoveryMgr_->discoveryContextMap_[pkgName] = context;
125     sleep(1);
126     DmDeviceInfo info;
127     info.deviceId[0] = '\0';
128     info.deviceName[0] = '\0';
129     discoveryMgr_->OnDeviceFound(pkgName, info);
130     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq =
131         std::static_pointer_cast<IpcNotifyDeviceFoundReq>(listener_->ipcServerListener_.req_);
132     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
133     EXPECT_EQ(ret1, 1);
134     std ::string ret = pReq->GetPkgName();
135     EXPECT_EQ(ret, pkgName);
136 }
137 
138 /**
139  * @tc.name: OnDeviceFound_002
140  * @tc.desc: set pkgName  not null and discoveryContextMap_ null and return
141  * @tc.type: FUNC
142  * @tc.require: AR000GHSJK
143  */
144 HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_002, testing::ext::TestSize.Level0)
145 {
146     std::string pkgName = "com.ohos.helloworld";
147     DmDeviceInfo info;
148     discoveryMgr_->OnDeviceFound(pkgName, info);
149     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
150     EXPECT_EQ(ret1, 1);
151     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq =
152         std::static_pointer_cast<IpcNotifyDeviceFoundReq>(listener_->ipcServerListener_.req_);
153     std ::string ret = pReq->GetPkgName();
154     EXPECT_EQ(ret, pkgName);
155 }
156 
157 /**
158  * @tc.name: OnDiscoveryFailed_001
159  * @tc.desc: The OnDeviceFound function takes the wrong case and emptying pkgName
160  * @tc.type: FUNC
161  * @tc.require: AR000GHSJK
162  */
163 HWTEST_F(DmDiscoveryManagerTest, OnDiscoveryFailed_001, testing::ext::TestSize.Level0)
164 {
165     std::string pkgName = "com.ohos.helloworld";
166     int32_t subscribeId = 1;
167     int32_t failedReason = 3;
168     discoveryMgr_->OnDiscoveryFailed(pkgName, subscribeId, failedReason);
169     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
170         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
171     std ::string ret = pReq->GetPkgName();
172     EXPECT_EQ(ret, pkgName);
173 }
174 
175 /**
176  * @tc.name: OnDiscoveryFailed_002
177  * @tc.desc: The OnDeviceFound function takes the wrong case and emptying pkgName
178  * @tc.type: FUNC
179  * @tc.require: AR000GHSJK
180  */
181 HWTEST_F(DmDiscoveryManagerTest, OnDiscoveryFailed_002, testing::ext::TestSize.Level0)
182 {
183     std::string pkgName;
184     int32_t subscribeId = 1;
185     int32_t failedReason = 3;
186     discoveryMgr_->OnDiscoveryFailed(pkgName, subscribeId, failedReason);
187     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
188     EXPECT_EQ(ret1, 0);
189     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
190         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
191     std ::string ret = pReq->GetPkgName();
192     EXPECT_EQ(ret, pkgName);
193 }
194 
195 /**
196  * @tc.name: OnDiscoverySuccess_001
197  * @tc.desc: The OnDeviceFound function takes the wrong case and return pkgName
198  * @tc.type: FUNC
199  * @tc.require: AR000GHSJK
200  */
201 HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_001, testing::ext::TestSize.Level0)
202 {
203     std::string pkgName = "com.ohos.helloworld";
204     int32_t subscribeId = 1;
205     discoveryMgr_->OnDiscoverySuccess(pkgName, subscribeId);
206     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
207         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
208     std ::string ret = pReq->GetPkgName();
209     EXPECT_EQ(ret, pkgName);
210 }
211 
212 /**
213  * @tc.name: OnDiscoverySuccess_002
214  * @tc.desc: set pkgName null and return  discoveryContextMap_ null and return pkgName(null)
215  * @tc.type: FUNC
216  * @tc.require: AR000GHSJK
217  */
218 HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_002, testing::ext::TestSize.Level0)
219 {
220     std::string pkgName;
221     int32_t subscribeId = 1;
222     discoveryMgr_->OnDiscoverySuccess(pkgName, subscribeId);
223     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
224     EXPECT_EQ(ret1, 1);
225     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
226         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
227     std ::string ret = pReq->GetPkgName();
228     EXPECT_EQ(ret, pkgName);
229 }
230 } // namespace
231 } // namespace DistributedHardware
232 } // namespace OHOS
233