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 "UTTest_dm_publish_manager.h"
17
18 #include "dm_log.h"
19 #include "dm_constants.h"
20 #include "dm_anonymous.h"
21 #include "ipc_server_listener.h"
22 #include "device_manager_service_listener.h"
23 #include "softbus_bus_center.h"
24 #include "device_manager_service_listener.h"
25 #include "softbus_error_code.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
SetUp()29 void DmPublishManagerTest::SetUp()
30 {
31 }
32
TearDown()33 void DmPublishManagerTest::TearDown()
34 {
35 }
36
SetUpTestCase()37 void DmPublishManagerTest::SetUpTestCase()
38 {
39 }
40
TearDownTestCase()41 void DmPublishManagerTest::TearDownTestCase()
42 {
43 }
44
45 namespace {
46 std::shared_ptr<SoftbusConnector> softbusConnector_ = std::make_shared<SoftbusConnector>();
47 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
48 std::shared_ptr<DmPublishManager> publishMgr_ = std::make_shared<DmPublishManager>(softbusConnector_, listener_);
49
CheckSoftbusRes(int32_t ret)50 bool CheckSoftbusRes(int32_t ret)
51 {
52 return ret == SOFTBUS_NETWORK_NOT_INIT || ret == SOFTBUS_NETWORK_LOOPER_ERR || ret == SOFTBUS_IPC_ERR;
53 }
54 /**
55 * @tc.name: DmPublishManager_001
56 * @tc.desc: Test whether the DmPublishManager function can generate a new pointer
57 * @tc.type: FUNC
58 * @tc.require: I5N1K3
59 */
60 HWTEST_F(DmPublishManagerTest, DmPublishManager_001, testing::ext::TestSize.Level0)
61 {
62 std::shared_ptr<DmPublishManager> Test = std::make_shared<DmPublishManager>(softbusConnector_, listener_);
63 ASSERT_NE(Test, nullptr);
64 }
65
66 /**
67 * @tc.name: DmPublishManager_002
68 * @tc.desc: Test whether the DmPublishManager function can delete a new pointer
69 * @tc.type: FUNC
70 * @tc.require: I5N1K3
71 */
72 HWTEST_F(DmPublishManagerTest, DmPublishManager_002, testing::ext::TestSize.Level0)
73 {
74 std::shared_ptr<DmPublishManager> Test = std::make_shared<DmPublishManager>(softbusConnector_, listener_);
75 Test.reset();
76 EXPECT_EQ(Test, nullptr);
77 }
78
79 /**
80 * @tc.name: PublishDeviceDiscovery_001
81 * @tc.desc: keeping pkgame unchanged, call PublishDeviceDiscovery twice
82 * so that its PublishQueue is not empty and return ERR_DM_PUBLISH_REPEATED
83 * @tc.type: FUNC
84 * @tc.require: I5N1K3
85 */
86 HWTEST_F(DmPublishManagerTest, PublishDeviceDiscovery_001, testing::ext::TestSize.Level0)
87 {
88 std::string pkgName = "com.ohos.helloworld";
89 DmPublishInfo publishInfo;
90 publishInfo.publishId = 9;
91 publishInfo.mode = DM_DISCOVER_MODE_PASSIVE;
92 publishInfo.freq = DM_HIGH;
93 publishInfo.ranging = 1;
94 publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
95 int32_t ret = publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
96 EXPECT_EQ(ret, ERR_DM_PUBLISH_REPEATED);
97 publishMgr_->UnPublishDeviceDiscovery(pkgName, publishInfo.publishId);
98 }
99
100 /**
101 * @tc.name: PublishDeviceDiscovery_002
102 * @tc.desc: pkgame changed, call PublishDeviceDiscovery twice
103 * so that its discoveryQueue is not empty and return ERR_DM_PUBLISH_REPEATED
104 * @tc.type: FUNC
105 * @tc.require: I5N1K3
106 */
107 HWTEST_F(DmPublishManagerTest, PublishDeviceDiscovery_002, testing::ext::TestSize.Level0)
108 {
109 std::string pkgName = "com.ohos.helloworld";
110 DmPublishInfo publishInfo;
111 publishInfo.publishId = 9;
112 publishInfo.mode = DM_DISCOVER_MODE_PASSIVE;
113 publishInfo.freq = DM_FREQ_BUTT;
114 publishInfo.ranging = 1;
115 publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
116 pkgName = "com.ohos.helloworld.new";
117 int32_t ret = publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
118 ASSERT_TRUE(CheckSoftbusRes(ret));
119 publishMgr_->UnPublishDeviceDiscovery(pkgName, publishInfo.publishId);
120 }
121
122 /**
123 * @tc.name: OnPublishResult_001
124 * @tc.desc: The OnPublishFailed function takes the wrong case and emptying pkgName
125 * @tc.type: FUNC
126 * @tc.require: I5N1K3
127 */
128 HWTEST_F(DmPublishManagerTest, OnPublishResult_001, testing::ext::TestSize.Level0)
129 {
130 std::string pkgName = "com.ohos.helloworld";
131 int32_t publishId = 1;
132 int32_t failedReason = 3;
133 publishMgr_->OnPublishResult(pkgName, publishId, failedReason);
134 std::shared_ptr<IpcNotifyPublishResultReq> pReq =
135 std::static_pointer_cast<IpcNotifyPublishResultReq>(listener_->ipcServerListener_.req_);
136 std::string ret = pReq->GetPkgName();
137 EXPECT_EQ(ret, pkgName);
138 }
139
140 /**
141 * @tc.name: OnPublishResult_002
142 * @tc.desc: The OnPublishSuccess function takes the wrong case and return pkgName
143 * @tc.type: FUNC
144 * @tc.require: I5N1K3
145 */
146 HWTEST_F(DmPublishManagerTest, OnPublishResult_002, testing::ext::TestSize.Level0)
147 {
148 std::string pkgName;
149 int32_t publishId = 1;
150 publishMgr_->OnPublishResult(pkgName, publishId, 0);
151 std::shared_ptr<IpcNotifyPublishResultReq> pReq =
152 std::static_pointer_cast<IpcNotifyPublishResultReq>(listener_->ipcServerListener_.req_);
153 std::string ret = pReq->GetPkgName();
154 EXPECT_NE(ret, pkgName);
155 }
156 } // namespace
157 } // namespace DistributedHardware
158 } // namespace OHOS
159