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