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 <thread>
17 #include "dm_common_event_manager.h"
18 #include "dm_constants.h"
19 #include "hichain_connector.h"
20 #include "UTTest_dm_common_event_manager.h"
21 #include "matching_skills.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
SetUp()26 void DmCommonEventManagerTest::SetUp()
27 {
28 }
29
TearDown()30 void DmCommonEventManagerTest::TearDown()
31 {
32 }
33
SetUpTestCase()34 void DmCommonEventManagerTest::SetUpTestCase()
35 {
36 }
37
TearDownTestCase()38 void DmCommonEventManagerTest::TearDownTestCase()
39 {
40 }
41
42 namespace {
43 /**
44 * @tc.name: DmCommonEventManager::~DmCommonEventManager_001
45 * @tc.desc: Destructor of DmCommonEventManager
46 * @tc.type: FUNC
47 * @tc.require: AR000GHSJK
48 */
49 HWTEST_F(DmCommonEventManagerTest, DeleteDmCommonEventManager_001, testing::ext::TestSize.Level0)
50 {
51 auto commonEventManager = std::make_shared<DmCommonEventManager>();
52 }
53
54 /**
55 * @tc.name: DmCommonEventManager::SubscribeServiceEvent_001
56 * @tc.desc: call SubscribeServiceEvent(), pass parameters: strEvent and callbackNode.callback_
57 * @tc.type: FUNC
58 * @tc.require: AR000GHSJK
59 */
60 HWTEST_F(DmCommonEventManagerTest, SubscribeServiceEvent_001, testing::ext::TestSize.Level0)
61 {
62 std::string strEvent;
63 CommomEventCallback callback = nullptr;
64 auto commonEventManager = std::make_shared<DmCommonEventManager>();
65 bool result = commonEventManager->SubscribeServiceEvent(strEvent, callback);
66 EXPECT_EQ(false, result);
67 }
68
69 /**
70 * @tc.name: DmCommonEventManager::SubscribeServiceEvent_002
71 * @tc.desc: call SubscribeServiceEvent(), pass parameters: strEvent and callbackNode.callback_
72 * @tc.type: FUNC
73 * @tc.require: AR000GHSJK
74 */
75 HWTEST_F(DmCommonEventManagerTest, SubscribeServiceEvent_002, testing::ext::TestSize.Level0)
76 {
77 std::string strEvent = "test";
78 CommomEventCallback callback = nullptr;
79 auto commonEventManager = std::make_shared<DmCommonEventManager>();
80 bool result = commonEventManager->SubscribeServiceEvent(strEvent, callback);
81 EXPECT_EQ(false, result);
82 }
83
84 /**
85 * @tc.name: DmCommonEventManager::UnsubscribeServiceEvent_001
86 * @tc.desc: call UnsubscribeServiceEvent(), pass parameters: strEvent
87 * @tc.type: FUNC
88 * @tc.require: AR000GHSJK
89 */
90 HWTEST_F(DmCommonEventManagerTest, UnsubscribeServiceEvent_001, testing::ext::TestSize.Level0)
91 {
92 auto commonEventManager = std::make_shared<DmCommonEventManager>();
93 bool result = commonEventManager->UnsubscribeServiceEvent();
94 EXPECT_EQ(false, result);
95 }
96
97 /**
98 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility_001
99 * @tc.desc: call OnAddSystemAbility()
100 * @tc.type: FUNC
101 */
102 HWTEST_F(DmCommonEventManagerTest, OnAddSystemAbility_001, testing::ext::TestSize.Level0)
103 {
104 int32_t systemAbilityId = COMMON_EVENT_SERVICE_ID;
105 std::string deviceId;
106 auto commonEventManager = std::make_shared<DmCommonEventManager>();
107 auto systemAbilityStatusChangeListener =
108 std::make_shared<DmCommonEventManager::SystemAbilityStatusChangeListener>(commonEventManager->subscriber_);
109 systemAbilityStatusChangeListener->OnAddSystemAbility(systemAbilityId, deviceId);
110 }
111
112 /**
113 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility_001
114 * @tc.desc: call OnAddSystemAbility()
115 * @tc.type: FUNC
116 */
117 HWTEST_F(DmCommonEventManagerTest, OnAddSystemAbility_002, testing::ext::TestSize.Level0)
118 {
119 int32_t systemAbilityId = 0;
120 std::string deviceId;
121 auto commonEventManager = std::make_shared<DmCommonEventManager>();
122 auto systemAbilityStatusChangeListener =
123 std::make_shared<DmCommonEventManager::SystemAbilityStatusChangeListener>(commonEventManager->subscriber_);
124 systemAbilityStatusChangeListener->OnAddSystemAbility(systemAbilityId, deviceId);
125 }
126
127 /**
128 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility_001
129 * @tc.desc: call OnRemoveSystemAbility()
130 * @tc.type: FUNC
131 */
132 HWTEST_F(DmCommonEventManagerTest, OnRemoveSystemAbility_002, testing::ext::TestSize.Level0)
133 {
134 int32_t systemAbilityId = COMMON_EVENT_SERVICE_ID;
135 std::string deviceId;
136 auto commonEventManager = std::make_shared<DmCommonEventManager>();
137 auto systemAbilityStatusChangeListener =
138 std::make_shared<DmCommonEventManager::SystemAbilityStatusChangeListener>(commonEventManager->subscriber_);
139 systemAbilityStatusChangeListener->OnRemoveSystemAbility(systemAbilityId, deviceId);
140 }
141
142 /**
143 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnReceiveEvent_001
144 * @tc.desc: call OnReceiveEvent()
145 * @tc.type: FUNC
146 */
147 HWTEST_F(DmCommonEventManagerTest, OnReceiveEvent_001, testing::ext::TestSize.Level0)
148 {
149 AAFwk::Want want;
150 EventFwk::CommonEventData data;
151 want.SetAction("changeEvent");
152 data.SetWant(want);
153 data.SetCode(0);
154
155 std::string strEvent = "test";
156 CommomEventCallback callback = nullptr;
157 EventFwk::MatchingSkills matchingSkills;
158 matchingSkills.AddEvent(strEvent);
159 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
160
161 auto commonEventManager = std::make_shared<DmCommonEventManager>();
162 commonEventManager->subscriber_ = std::make_shared<DmEventSubscriber>(subscriberInfo, callback, strEvent);
163 commonEventManager->subscriber_->OnReceiveEvent(data);
164 }
165
166 /**
167 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnReceiveEvent_002
168 * @tc.desc: call OnReceiveEvent()
169 * @tc.type: FUNC
170 */
171 HWTEST_F(DmCommonEventManagerTest, OnReceiveEvent_002, testing::ext::TestSize.Level0)
172 {
173 AAFwk::Want want;
174 EventFwk::CommonEventData data;
175 want.SetAction("test");
176 data.SetWant(want);
177 data.SetCode(0);
178 CommomEventCallback callback = nullptr;
179 EventFwk::MatchingSkills matchingSkills;
180 std::string strEvent = "test";
181 matchingSkills.AddEvent(strEvent);
182 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
183
184 auto commonEventManager = std::make_shared<DmCommonEventManager>();
185 commonEventManager->subscriber_ = std::make_shared<DmEventSubscriber>(subscriberInfo, callback, strEvent);
186 commonEventManager->subscriber_->OnReceiveEvent(data);
187 }
188 } // namespace
189 } // namespace DistributedHardware
190 } // namespace OHOS
191