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