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::SubscribeServiceEvent_001
45 * @tc.desc: call SubscribeServiceEvent(), pass parameters: strEvent and callbackNode.callback_
46 * @tc.type: FUNC
47 * @tc.require: AR000GHSJK
48 */
49 HWTEST_F(DmCommonEventManagerTest, SubscribeServiceEvent_001, testing::ext::TestSize.Level0)
50 {
51 std::vector<std::string> strEvent;
52 CommomEventCallback callback = nullptr;
53 auto commonEventManager = std::make_shared<DmCommonEventManager>();
54 bool result = commonEventManager->SubscribeServiceEvent(strEvent, callback);
55 EXPECT_EQ(false, result);
56 }
57
58 /**
59 * @tc.name: DmCommonEventManager::SubscribeServiceEvent_002
60 * @tc.desc: call SubscribeServiceEvent(), pass parameters: strEvent and callbackNode.callback_
61 * @tc.type: FUNC
62 * @tc.require: AR000GHSJK
63 */
64 HWTEST_F(DmCommonEventManagerTest, SubscribeServiceEvent_002, testing::ext::TestSize.Level0)
65 {
66 std::string strEvent = "test";
67 std::vector<std::string> strEventVec;
68 strEventVec.push_back(strEvent);
69 CommomEventCallback callback = nullptr;
70 auto commonEventManager = std::make_shared<DmCommonEventManager>();
71 bool result = commonEventManager->SubscribeServiceEvent(strEventVec, callback);
72 EXPECT_EQ(false, result);
73 }
74
75 /**
76 * @tc.name: DmCommonEventManager::UnsubscribeServiceEvent_001
77 * @tc.desc: call UnsubscribeServiceEvent(), pass parameters: strEvent
78 * @tc.type: FUNC
79 * @tc.require: AR000GHSJK
80 */
81 HWTEST_F(DmCommonEventManagerTest, UnsubscribeServiceEvent_001, testing::ext::TestSize.Level0)
82 {
83 auto commonEventManager = std::make_shared<DmCommonEventManager>();
84 bool result = commonEventManager->UnsubscribeServiceEvent();
85 EXPECT_EQ(false, result);
86 }
87
88 /**
89 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility_001
90 * @tc.desc: call OnAddSystemAbility()
91 * @tc.type: FUNC
92 */
93 HWTEST_F(DmCommonEventManagerTest, OnAddSystemAbility_001, testing::ext::TestSize.Level0)
94 {
95 int32_t systemAbilityId = COMMON_EVENT_SERVICE_ID;
96 std::string deviceId;
97 auto commonEventManager = std::make_shared<DmCommonEventManager>();
98 auto systemAbilityStatusChangeListener =
99 std::make_shared<DmCommonEventManager::SystemAbilityStatusChangeListener>(commonEventManager->subscriber_);
100 systemAbilityStatusChangeListener->OnAddSystemAbility(systemAbilityId, deviceId);
101 EXPECT_EQ(systemAbilityStatusChangeListener->changeSubscriber_, nullptr);
102 }
103
104 /**
105 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility_002
106 * @tc.desc: call OnAddSystemAbility()
107 * @tc.type: FUNC
108 */
109 HWTEST_F(DmCommonEventManagerTest, OnAddSystemAbility_002, testing::ext::TestSize.Level0)
110 {
111 int32_t systemAbilityId = 0;
112 std::string deviceId;
113 auto commonEventManager = std::make_shared<DmCommonEventManager>();
114 auto systemAbilityStatusChangeListener =
115 std::make_shared<DmCommonEventManager::SystemAbilityStatusChangeListener>(commonEventManager->subscriber_);
116 systemAbilityStatusChangeListener->OnAddSystemAbility(systemAbilityId, deviceId);
117 EXPECT_NE(systemAbilityId, COMMON_EVENT_SERVICE_ID);
118 }
119
120 /**
121 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility_003
122 * @tc.desc: call OnAddSystemAbility()
123 * @tc.type: FUNC
124 */
125 HWTEST_F(DmCommonEventManagerTest, OnAddSystemAbility_003, testing::ext::TestSize.Level0)
126 {
127 int32_t systemAbilityId = COMMON_EVENT_SERVICE_ID;
128 std::string deviceId;
129 CommonEventSubscribeInfo subscribeInfo;
130 CommomEventCallback callback = nullptr;
131 std::string strEvent = "test";
132 std::vector<std::string> strEventVec;
133 strEventVec.push_back(strEvent);
134 auto commonEventManager = std::make_shared<DmCommonEventManager>();
135 commonEventManager->subscriber_ = std::make_shared<DmEventSubscriber>(subscribeInfo, callback, strEventVec);
136 auto systemAbilityStatusChangeListener =
137 std::make_shared<DmCommonEventManager::SystemAbilityStatusChangeListener>(commonEventManager->subscriber_);
138 systemAbilityStatusChangeListener->OnAddSystemAbility(systemAbilityId, deviceId);
139 std::vector<std::string> event = systemAbilityStatusChangeListener->changeSubscriber_->GetSubscriberEventNameVec();
140 EXPECT_EQ(event, strEventVec);
141 }
142
143 /**
144 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility_001
145 * @tc.desc: call OnRemoveSystemAbility()
146 * @tc.type: FUNC
147 */
148 HWTEST_F(DmCommonEventManagerTest, OnRemoveSystemAbility_001, testing::ext::TestSize.Level0)
149 {
150 int32_t systemAbilityId = COMMON_EVENT_SERVICE_ID;
151 std::string deviceId;
152 auto commonEventManager = std::make_shared<DmCommonEventManager>();
153 auto systemAbilityStatusChangeListener =
154 std::make_shared<DmCommonEventManager::SystemAbilityStatusChangeListener>(commonEventManager->subscriber_);
155 systemAbilityStatusChangeListener->OnRemoveSystemAbility(systemAbilityId, deviceId);
156 EXPECT_EQ(systemAbilityStatusChangeListener->changeSubscriber_, nullptr);
157 }
158
159 /**
160 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnReceiveEvent_001
161 * @tc.desc: call OnReceiveEvent()
162 * @tc.type: FUNC
163 */
164 HWTEST_F(DmCommonEventManagerTest, OnReceiveEvent_001, testing::ext::TestSize.Level0)
165 {
166 AAFwk::Want want;
167 EventFwk::CommonEventData data;
168 want.SetAction("changeEvent");
169 data.SetWant(want);
170 data.SetCode(0);
171 std::vector<std::string> changeEventVec;
172 changeEventVec.push_back("changeEvent");
173 std::string strEvent = "test";
174 std::vector<std::string> strEventVec;
175 strEventVec.push_back(strEvent);
176 CommomEventCallback callback = nullptr;
177 EventFwk::MatchingSkills matchingSkills;
178 matchingSkills.AddEvent(strEvent);
179 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
180
181 auto commonEventManager = std::make_shared<DmCommonEventManager>();
182 commonEventManager->subscriber_ = std::make_shared<DmEventSubscriber>(subscriberInfo, callback, strEventVec);
183 commonEventManager->subscriber_->OnReceiveEvent(data);
184 EXPECT_NE(commonEventManager->subscriber_->GetSubscriberEventNameVec(), changeEventVec);
185 }
186
187 /**
188 * @tc.name: DmCommonEventManager::SystemAbilityStatusChangeListener::OnReceiveEvent_002
189 * @tc.desc: call OnReceiveEvent()
190 * @tc.type: FUNC
191 */
192 HWTEST_F(DmCommonEventManagerTest, OnReceiveEvent_002, testing::ext::TestSize.Level0)
193 {
194 AAFwk::Want want;
195 EventFwk::CommonEventData data;
196 want.SetAction("test");
197 data.SetWant(want);
198 data.SetCode(0);
199 CommomEventCallback callback = nullptr;
200 EventFwk::MatchingSkills matchingSkills;
201 std::string strEvent = "test";
202 std::vector<std::string> strEventVec;
203 strEventVec.push_back(strEvent);
204 matchingSkills.AddEvent(strEvent);
205 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
206
207 auto commonEventManager = std::make_shared<DmCommonEventManager>();
208 commonEventManager->subscriber_ = std::make_shared<DmEventSubscriber>(subscriberInfo, callback, strEventVec);
209 commonEventManager->subscriber_->OnReceiveEvent(data);
210 EXPECT_EQ(commonEventManager->subscriber_->GetSubscriberEventNameVec(), strEventVec);
211 }
212 } // namespace
213 } // namespace DistributedHardware
214 } // namespace OHOS
215