1 /*
2 * Copyright (c) 2025 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_advertise_manager.h"
17 #include "dm_constants.h"
18
19 namespace OHOS {
20 namespace DistributedHardware {
21 namespace {
22 constexpr int32_t VALUABLE_PUBLISH_ID = 10;
23 constexpr int32_t DEFAULT_META_TYPE = 10;
24 constexpr int32_t DEFAULT_DISC_MODE = 20;
25 constexpr int32_t DEFAULT_DISC_FREQ = 30;
26 const std::string PKG_NAME = "pkgName";
27 const std::string DEFAULT_CUSTOM_DATA = "CUSTOM_DATA";
28 } // namespace
SetUp()29 void AdvertiseManagerTest::SetUp()
30 {}
31
TearDown()32 void AdvertiseManagerTest::TearDown()
33 {}
34
SetUpTestCase()35 void AdvertiseManagerTest::SetUpTestCase()
36 {}
37
TearDownTestCase()38 void AdvertiseManagerTest::TearDownTestCase()
39 {}
40
41 HWTEST_F(AdvertiseManagerTest, StartAdvertising_001, testing::ext::TestSize.Level0)
42 {
43 std::string stopTime = std::to_string(100);
44 auto softbusListener = std::make_shared<SoftbusListener>();
45 auto advertiseManager = std::make_shared<AdvertiseManager>(softbusListener);
46
47 std::map<std::string, std::string> advertiseParam;
48 advertiseParam[PARAM_KEY_DISC_CAPABILITY] = DM_CAPABILITY_APPROACH;
49 advertiseParam[PARAM_KEY_CUSTOM_DATA] = DEFAULT_CUSTOM_DATA;
50 advertiseParam[PARAM_KEY_AUTO_STOP_ADVERTISE] = stopTime;
51 advertiseParam[PARAM_KEY_META_TYPE] = std::to_string(DEFAULT_META_TYPE);
52 advertiseParam[PARAM_KEY_PUBLISH_ID] = std::to_string(VALUABLE_PUBLISH_ID);
53 advertiseParam[PARAM_KEY_DISC_MODE] = std::to_string(DEFAULT_DISC_MODE);
54 advertiseParam[PARAM_KEY_DISC_FREQ] = std::to_string(DEFAULT_DISC_FREQ);
55 auto ret = advertiseManager->StartAdvertising(PKG_NAME, advertiseParam);
56 EXPECT_EQ(ret, DM_OK);
57 }
58
59 HWTEST_F(AdvertiseManagerTest, StartAdvertising_002, testing::ext::TestSize.Level0)
60 {
61 std::string stopTime = std::to_string(-5);
62 auto softbusListener = std::make_shared<SoftbusListener>();
63 auto advertiseManager = std::make_shared<AdvertiseManager>(softbusListener);
64
65 std::map<std::string, std::string> advertiseParam;
66 advertiseParam[PARAM_KEY_DISC_CAPABILITY] = DM_CAPABILITY_APPROACH;
67 advertiseParam[PARAM_KEY_CUSTOM_DATA] = DEFAULT_CUSTOM_DATA;
68 advertiseParam[PARAM_KEY_AUTO_STOP_ADVERTISE] = stopTime;
69 advertiseParam[PARAM_KEY_META_TYPE] = std::to_string(DEFAULT_META_TYPE);
70 advertiseParam[PARAM_KEY_PUBLISH_ID] = std::to_string(VALUABLE_PUBLISH_ID);
71 advertiseParam[PARAM_KEY_DISC_MODE] = std::to_string(DEFAULT_DISC_MODE);
72 advertiseParam[PARAM_KEY_DISC_FREQ] = std::to_string(DEFAULT_DISC_FREQ);
73 auto ret = advertiseManager->StartAdvertising(PKG_NAME, advertiseParam);
74 EXPECT_EQ(ret, DM_OK);
75 }
76
77 HWTEST_F(AdvertiseManagerTest, StartAdvertising_003, testing::ext::TestSize.Level2)
78 {
79 std::string emptyPackageName;
80 std::string stopTime = std::to_string(100);
81 auto softbusListener = std::make_shared<SoftbusListener>();
82 auto advertiseManager = std::make_shared<AdvertiseManager>(softbusListener);
83
84 std::map<std::string, std::string> advertiseParam;
85 advertiseParam[PARAM_KEY_DISC_CAPABILITY] = DM_CAPABILITY_APPROACH;
86 advertiseParam[PARAM_KEY_CUSTOM_DATA] = DEFAULT_CUSTOM_DATA;
87 advertiseParam[PARAM_KEY_AUTO_STOP_ADVERTISE] = stopTime;
88 advertiseParam[PARAM_KEY_META_TYPE] = std::to_string(DEFAULT_META_TYPE);
89 advertiseParam[PARAM_KEY_PUBLISH_ID] = std::to_string(VALUABLE_PUBLISH_ID);
90 advertiseParam[PARAM_KEY_DISC_MODE] = std::to_string(DEFAULT_DISC_MODE);
91 advertiseParam[PARAM_KEY_DISC_FREQ] = std::to_string(DEFAULT_DISC_FREQ);
92 auto ret = advertiseManager->StartAdvertising(emptyPackageName, advertiseParam);
93 EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
94 }
95
96 HWTEST_F(AdvertiseManagerTest, HandleAutoStopAdvertise_001, testing::ext::TestSize.Level0)
97 {
98 auto softbusListener = std::make_shared<SoftbusListener>();
99 auto advertiseManager = std::make_shared<AdvertiseManager>(softbusListener);
100 std::string timerName = "timer";
101 advertiseManager->HandleAutoStopAdvertise(timerName, PKG_NAME, VALUABLE_PUBLISH_ID);
102 ASSERT_TRUE(advertiseManager->softbusListener_ != nullptr);
103 }
104
105 HWTEST_F(AdvertiseManagerTest, StopAdvertising_001, testing::ext::TestSize.Level2)
106 {
107 std::string emptyPackageName;
108 auto softbusListener = std::make_shared<SoftbusListener>();
109 auto advertiseManager = std::make_shared<AdvertiseManager>(softbusListener);
110 auto ret = advertiseManager->StopAdvertising(emptyPackageName, VALUABLE_PUBLISH_ID);
111 EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
112 }
113
114 HWTEST_F(AdvertiseManagerTest, StopAdvertising_002, testing::ext::TestSize.Level0)
115 {
116 auto softbusListener = std::make_shared<SoftbusListener>();
117 auto advertiseManager = std::make_shared<AdvertiseManager>(softbusListener);
118 auto innerPublishId = advertiseManager->GenInnerPublishId(PKG_NAME, VALUABLE_PUBLISH_ID);
119 auto ret = advertiseManager->StopAdvertising(PKG_NAME, VALUABLE_PUBLISH_ID);
120 EXPECT_EQ(ret, DM_OK);
121 }
122 } // namespace DistributedHardware
123 } // namespace OHOS
124