1 /*
2 * Copyright (c) 2021-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 <chrono>
17 #include <iostream>
18 #include <string>
19 #include <thread>
20
21 #include "common_event_manager.h"
22 #define private public
23 #define protected public
24 #include "common_event_manager_service.h"
25 #undef private
26 #undef protected
27 #include "common_event_subscriber.h"
28 #include "common_event_support.h"
29 #include "datetime_ex.h"
30 #include "event_log_wrapper.h"
31 #include "nativetoken_kit.h"
32 #include "token_setproc.h"
33 #include "accesstoken_kit.h"
34
35 #include <gtest/gtest.h>
36
37 using namespace testing::ext;
38
39 namespace OHOS {
40 namespace EventFwk {
41 namespace {
42 std::mutex mtx_;
43 std::mutex mtx2_;
44 const time_t TIME_OUT_SECONDS_LIMIT = 5;
45 const time_t TIME_OUT_SECONDS_TWO = 2;
46 const time_t TIME_OUT_SECONDS_ = 3;
47 const time_t TIME_OUT_SECONDS_TEN = 10;
48 const time_t TIME_OUT_SECONDS_TWENTY = 20;
49 const size_t SUBSCRIBER_MAX_SIZE = 200;
50 const size_t SUBSCRIBER_MAX_SIZE_PLUS = 201;
51
52 const std::string COMPARE_STR = "cesComparesStrForCase";
53 const std::string COMPARE_STR_FALSE = "cesComparesStrForCaseFalse";
54
55 const int32_t g_CODE_COMPARE1 = 1;
56 const int32_t g_CODE_COMPARE2 = 2;
57 const int32_t g_CODE_COMPARE3 = 200;
58 const int32_t PRIORITY_HIGH = 80;
59 const int32_t PRIORITY_LOW = 20;
60 } // namespace
61
62 class CommonEventServicesSystemTest : public CommonEventSubscriber {
63 public:
64 explicit CommonEventServicesSystemTest(const CommonEventSubscribeInfo &subscribeInfo);
~CommonEventServicesSystemTest()65 virtual ~CommonEventServicesSystemTest() {};
66 virtual void OnReceiveEvent(const CommonEventData &data);
67 };
68
CommonEventServicesSystemTest(const CommonEventSubscribeInfo & subscribeInfo)69 CommonEventServicesSystemTest::CommonEventServicesSystemTest(const CommonEventSubscribeInfo &subscribeInfo)
70 : CommonEventSubscriber(subscribeInfo)
71 {}
72
OnReceiveEvent(const CommonEventData & data)73 void CommonEventServicesSystemTest::OnReceiveEvent(const CommonEventData &data)
74 {
75 GTEST_LOG_(INFO) << " cesSystemTest:CommonEventServicesSystemTest:OnReceiveEvent \n";
76 std::string action = data.GetWant().GetAction();
77 if (action == COMPARE_STR_FALSE) {
78 EXPECT_TRUE(data.GetCode() == g_CODE_COMPARE3);
79 }
80 mtx_.unlock();
81 }
82
83 class CommonEventServicesSystemTestSubscriber : public CommonEventSubscriber {
84 public:
85 explicit CommonEventServicesSystemTestSubscriber(const CommonEventSubscribeInfo &subscribeInfo);
~CommonEventServicesSystemTestSubscriber()86 virtual ~CommonEventServicesSystemTestSubscriber() {};
87 virtual void OnReceiveEvent(const CommonEventData &data);
88 };
89
CommonEventServicesSystemTestSubscriber(const CommonEventSubscribeInfo & subscribeInfo)90 CommonEventServicesSystemTestSubscriber::CommonEventServicesSystemTestSubscriber(
91 const CommonEventSubscribeInfo &subscribeInfo)
92 : CommonEventSubscriber(subscribeInfo)
93 {}
94
OnReceiveEvent(const CommonEventData & data)95 void CommonEventServicesSystemTestSubscriber::OnReceiveEvent(const CommonEventData &data)
96 {
97 GTEST_LOG_(INFO) << " cesSystemTest:CommonEventServicesSystemTestSubscriber:OnReceiveEvent \n";
98 std::string action = data.GetWant().GetAction();
99 if (action == COMPARE_STR) {
100 EXPECT_TRUE(data.GetCode() == g_CODE_COMPARE1);
101 } else if (action == COMPARE_STR_FALSE) {
102 EXPECT_TRUE(data.GetCode() == g_CODE_COMPARE2);
103 }
104 mtx2_.unlock();
105 }
106
107 class CESPublishOrderTimeOutOne : public CommonEventSubscriber {
108 public:
109 explicit CESPublishOrderTimeOutOne(const CommonEventSubscribeInfo &subscribeInfo);
~CESPublishOrderTimeOutOne()110 virtual ~CESPublishOrderTimeOutOne() {};
111 virtual void OnReceiveEvent(const CommonEventData &data);
112 };
113
CESPublishOrderTimeOutOne(const CommonEventSubscribeInfo & subscribeInfo)114 CESPublishOrderTimeOutOne::CESPublishOrderTimeOutOne(const CommonEventSubscribeInfo &subscribeInfo)
115 : CommonEventSubscriber(subscribeInfo)
116 {}
117
OnReceiveEvent(const CommonEventData & data)118 void CESPublishOrderTimeOutOne::OnReceiveEvent(const CommonEventData &data)
119 {
120 std::this_thread::sleep_for(std::chrono::seconds(TIME_OUT_SECONDS_TEN));
121 }
122
123 class CESPublishOrderTimeOutTwo : public CommonEventSubscriber {
124 public:
125 explicit CESPublishOrderTimeOutTwo(const CommonEventSubscribeInfo &subscribeInfo);
~CESPublishOrderTimeOutTwo()126 virtual ~CESPublishOrderTimeOutTwo() {};
127 virtual void OnReceiveEvent(const CommonEventData &data);
128 };
129
CESPublishOrderTimeOutTwo(const CommonEventSubscribeInfo & subscribeInfo)130 CESPublishOrderTimeOutTwo::CESPublishOrderTimeOutTwo(const CommonEventSubscribeInfo &subscribeInfo)
131 : CommonEventSubscriber(subscribeInfo)
132 {}
133
OnReceiveEvent(const CommonEventData & data)134 void CESPublishOrderTimeOutTwo::OnReceiveEvent(const CommonEventData &data)
135 {
136 GTEST_LOG_(INFO) << " cesSystemTest:CESPublishOrderTimeOutTwo:OnReceiveEvent \n";
137 EXPECT_TRUE(data.GetCode() == GetCode());
138 mtx_.unlock();
139 }
140
141 class cesSystemTest : public testing::Test {
142 public:
143 static void SetUpTestCase();
144 static void TearDownTestCase();
145 void SetUp();
146 void TearDown();
147
148 bool PublishCommonEventTest(const std::string &eventName);
149 };
150
SetUpTestCase()151 void cesSystemTest::SetUpTestCase()
152 {
153 uint64_t tokenId;
154 const char **perms = new const char *[4];
155 perms[0] = "ohos.permission.COMMONEVEVT_STICKY"; // system_core
156 perms[1] = "ohos.permission.LISTEN_BUNDLE_CHANGE"; // system_core
157 perms[2] = "ohos.permission.MANAGE_LOCAL_ACCOUNTS"; // system_core
158 perms[3] = "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"; // system_core
159 NativeTokenInfoParams infoInstance = {
160 .dcapsNum = 0,
161 .permsNum = 4,
162 .aclsNum = 0,
163 .dcaps = nullptr,
164 .perms = perms,
165 .acls = nullptr,
166 .aplStr = "system_basic",
167 };
168
169 infoInstance.processName = "SetUpTestCase";
170 tokenId = GetAccessTokenId(&infoInstance);
171 SetSelfTokenID(tokenId);
172 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
173 delete[] perms;
174 }
175
TearDownTestCase()176 void cesSystemTest::TearDownTestCase()
177 {}
178
SetUp()179 void cesSystemTest::SetUp()
180 {}
181
TearDown()182 void cesSystemTest::TearDown()
183 {}
184
185 /*
186 * @tc.number: CES_SubscriptionEvent_0100
187 * @tc.name: SubscribeCommonEvent
188 * @tc.desc: Verify the function when the input string is normal
189 */
190 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0100, Function | MediumTest | Level1)
191 {
192 std::string eventName = "TESTEVENT_SUBSCRIBER";
193 MatchingSkills matchingSkills;
194 matchingSkills.AddEvent(eventName);
195 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
196 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
197
198 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
199 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
200 }
201
202 /*
203 * @tc.number: CES_SubscriptionEvent_0200
204 * @tc.name: SubscribeCommonEvent
205 * @tc.desc: Verify the function when the input string is number
206 */
207 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0200, Function | MediumTest | Level1)
208 {
209 std::string eventName = "1";
210 MatchingSkills matchingSkills;
211 matchingSkills.AddEvent(eventName);
212 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
213 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
214
215 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
216 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
217 }
218
219 /*
220 * @tc.number: CES_SubscriptionEvent_0300
221 * @tc.name: SubscribeCommonEvent
222 * @tc.desc: Verify the function when the input string is empty
223 */
224 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0300, Function | MediumTest | Level1)
225 {
226 std::string eventName = "";
227 MatchingSkills matchingSkills;
228 matchingSkills.AddEvent(eventName);
229 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
230 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
231
232 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
233 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
234 }
235
236 /*
237 * @tc.number: CES_SubscriptionEvent_0400
238 * @tc.name: SubscribeCommonEvent
239 * @tc.desc: Verify the function when the input string is quotes
240 */
241 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0400, Function | MediumTest | Level1)
242 {
243 std::string eventName = ".............";
244 MatchingSkills matchingSkills;
245 matchingSkills.AddEvent(eventName);
246 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
247 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
248
249 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
250 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
251 }
252
253 /*
254 * @tc.number: CES_SubscriptionEvent_0500
255 * @tc.name: SubscribeCommonEvent
256 * @tc.desc: Verify the function when the input string is normal
257 */
258 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0500, Function | MediumTest | Level1)
259 {
260 std::string eventName = "HELLO\0\0\0WORLD";
261 MatchingSkills matchingSkills;
262 matchingSkills.AddEvent(eventName);
263 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
264 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
265
266 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
267 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
268 }
269
270 /*
271 * @tc.number: CES_SubscriptionEvent_0600
272 * @tc.name: SubscribeCommonEvent
273 * @tc.desc: Verify that the ordered common event was subsribered successfully
274 */
275 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0600, Function | MediumTest | Level1)
276 {
277 std::string eventName = "TESTEVENT_SUBSCRIBER_SETPRIORITY";
278 MatchingSkills matchingSkills;
279 matchingSkills.AddEvent(eventName);
280 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
281 subscribeInfo.SetPriority(100);
282 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
283
284 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
285 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
286 }
287
288 /*
289 * @tc.number: CES_SubscriptionEvent_0700
290 * @tc.name: UnsubscribeCommonEvent
291 * @tc.desc: Verify the function when the input string is normal
292 */
293 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0700, Function | MediumTest | Level1)
294 {
295 std::string eventName = "TESTEVENT_UNSUBSCRIBE_SETPRIORITY";
296 MatchingSkills matchingSkills;
297 matchingSkills.AddEvent(eventName);
298 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
299 subscribeInfo.SetPriority(100);
300 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
301
302 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
303 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
304 }
305
306 /*
307 * @tc.number: CES_SubscriptionEvent_0800
308 * @tc.name: SubscribeCommonEvent
309 * @tc.desc: Verify the same input string three times
310 */
311 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0800, Function | MediumTest | Level1)
312 {
313 std::string eventName = "TESTEVENT1";
314 MatchingSkills matchingSkills;
315 matchingSkills.AddEvent(eventName);
316 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
317
318 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
319 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
320
321 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
322 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
323
324 auto subscriberPtr3 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
325 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr3), true);
326
327 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
328 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
329 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr3), true);
330 }
331
332 /*
333 * @tc.number: CES_SubscriptionEvent_0900
334 * @tc.name: SubscribeCommonEvent
335 * @tc.desc: Verify the same input empty string three times
336 */
337 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_0900, Function | MediumTest | Level1)
338 {
339 std::string eventName = "";
340 MatchingSkills matchingSkills;
341 matchingSkills.AddEvent(eventName);
342 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
343
344 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
345 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
346
347 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
348 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
349
350 auto subscriberPtr3 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
351 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr3), true);
352
353 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
354 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
355 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr3), true);
356 }
357
358 /*
359 * @tc.number: CES_SubscriptionEvent_1000
360 * @tc.name: SubscribeCommonEvent
361 * @tc.desc: Verify the normal input string three times
362 */
363 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1000, Function | MediumTest | Level1)
364 {
365 std::string eventName = "TESTEVENT3";
366 MatchingSkills matchingSkills;
367 matchingSkills.AddEvent(eventName);
368 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
369
370 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
371 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
372
373 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
374 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
375
376 auto subscriberPtr3 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
377 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr3), true);
378
379 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
380 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
381 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr3), true);
382 }
383
384 /*
385 * @tc.number: CES_SubscriptionEvent_1100
386 * @tc.name: SubscribeCommonEvent
387 * @tc.desc: Verify the normal input string two times
388 */
389 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1100, Function | MediumTest | Level1)
390 {
391 std::string eventName1 = "TEST1";
392 std::string eventName2 = "TEST2";
393
394 MatchingSkills matchingSkills1;
395 matchingSkills1.AddEvent(eventName1);
396 CommonEventSubscribeInfo subscribeInfo1(matchingSkills1);
397 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo1);
398 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
399
400 MatchingSkills matchingSkills2;
401 matchingSkills2.AddEvent(eventName2);
402 CommonEventSubscribeInfo subscribeInfo2(matchingSkills2);
403 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo2);
404 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
405
406 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
407 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
408 }
409
410 /*
411 * @tc.number: CES_SubscriptionEvent_1200
412 * @tc.name: SubscribeCommonEvent
413 * @tc.desc: Verify the normal, number and empty input string
414 */
415 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1200, Function | MediumTest | Level1)
416 {
417 std::string eventName1 = "TESTEVENT1";
418 std::string eventName2 = "1";
419 std::string eventName3 = "";
420
421 MatchingSkills matchingSkills1;
422 matchingSkills1.AddEvent(eventName1);
423 CommonEventSubscribeInfo subscribeInfo1(matchingSkills1);
424 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo1);
425 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
426
427 MatchingSkills matchingSkills2;
428 matchingSkills2.AddEvent(eventName2);
429 CommonEventSubscribeInfo subscribeInfo2(matchingSkills2);
430 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo2);
431 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
432
433 MatchingSkills matchingSkills3;
434 matchingSkills3.AddEvent(eventName3);
435 CommonEventSubscribeInfo subscribeInfo3(matchingSkills3);
436 auto subscriberPtr3 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo3);
437 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr3), true);
438
439 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
440 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
441 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr3), true);
442 }
443
444 /*
445 * @tc.number: CES_SubscriptionEvent_1300
446 * @tc.name: UnSubscribeCommonEvent
447 * @tc.desc: Verify the function when the input string is normal
448 */
449 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1300, Function | MediumTest | Level1)
450 {
451 std::string eventName = "TESTEVENT_UNSUBSCRIBE";
452
453 MatchingSkills matchingSkills;
454 matchingSkills.AddEvent(eventName);
455 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
456 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
457
458 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
459 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
460 }
461
462 /*
463 * @tc.number: CES_SubscriptionEvent_1400
464 * @tc.name: UnSubscribeCommonEvent
465 * @tc.desc: Verify the function when the input string is number
466 */
467 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1400, Function | MediumTest | Level1)
468 {
469 std::string eventName = "2";
470
471 MatchingSkills matchingSkills;
472 matchingSkills.AddEvent(eventName);
473 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
474 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
475
476 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
477 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
478 }
479
480 /*
481 * @tc.number: CES_SubscriptionEvent_1500
482 * @tc.name: UnSubscribeCommonEvent
483 * @tc.desc: Verify the function when the input string is empty
484 */
485 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1500, Function | MediumTest | Level1)
486 {
487 std::string eventName = "";
488
489 MatchingSkills matchingSkills;
490 matchingSkills.AddEvent(eventName);
491 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
492 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
493
494 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
495 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
496 }
497
498 /*
499 * @tc.number: CES_SubscriptionEvent_1600
500 * @tc.name: UnSubscribeCommonEvent
501 * @tc.desc: Verify the function when the input string is quotes
502 */
503 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1600, Function | MediumTest | Level1)
504 {
505 std::string eventName = "..................";
506
507 MatchingSkills matchingSkills;
508 matchingSkills.AddEvent(eventName);
509 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
510 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
511
512 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
513 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
514 }
515
516 /*
517 * @tc.number: CES_SubscriptionEvent_1700
518 * @tc.name: UnSubscribeCommonEvent
519 * @tc.desc: Verify the function when the input string with \0
520 */
521 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1700, Function | MediumTest | Level1)
522 {
523 std::string eventName = "HELLO\0\0\0WORLD";
524
525 MatchingSkills matchingSkills;
526 matchingSkills.AddEvent(eventName);
527 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
528 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
529
530 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
531 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
532 }
533
534 /*
535 * @tc.number: CES_SubscriptionEvent_1800
536 * @tc.name: UnSubscribeCommonEvent
537 * @tc.desc: Verify the normal input string three times
538 */
539 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1800, Function | MediumTest | Level1)
540 {
541 std::string eventName1 = "TESTEVENT4";
542 std::string eventName2 = "TESTEVENT5";
543 std::string eventName3 = "TESTEVENT6";
544
545 MatchingSkills matchingSkills1;
546 matchingSkills1.AddEvent(eventName1);
547 CommonEventSubscribeInfo subscribeInfo1(matchingSkills1);
548 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo1);
549 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
550 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
551
552 MatchingSkills matchingSkills2;
553 matchingSkills2.AddEvent(eventName2);
554 CommonEventSubscribeInfo subscribeInfo2(matchingSkills2);
555 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo2);
556 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
557 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
558
559 MatchingSkills matchingSkills3;
560 matchingSkills3.AddEvent(eventName3);
561 CommonEventSubscribeInfo subscribeInfo3(matchingSkills3);
562 auto subscriberPtr3 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo3);
563 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr3), true);
564 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr3), true);
565 }
566
567 /*
568 * @tc.number: CES_SubscriptionEvent_1900
569 * @tc.name: UnSubscribeCommonEvent
570 * @tc.desc: Verify the normal input string two times
571 */
572 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_1900, Function | MediumTest | Level1)
573 {
574 std::string eventName1 = "TEST3";
575 std::string eventName2 = "TEST4";
576
577 MatchingSkills matchingSkills1;
578 matchingSkills1.AddEvent(eventName1);
579 CommonEventSubscribeInfo subscribeInfo1(matchingSkills1);
580 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo1);
581 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
582 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
583
584 MatchingSkills matchingSkills2;
585 matchingSkills2.AddEvent(eventName2);
586 CommonEventSubscribeInfo subscribeInfo2(matchingSkills2);
587 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo2);
588 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
589 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
590 }
591
592 /*
593 * @tc.number: CES_SubscriptionEvent_2000
594 * @tc.name: UnSubscribeCommonEvent
595 * @tc.desc: Verify the normal, number and empty input string
596 */
597 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_2000, Function | MediumTest | Level1)
598 {
599 std::string eventName1 = "TESTEVENT7";
600 std::string eventName2 = "3";
601 std::string eventName3 = "";
602
603 MatchingSkills matchingSkills1;
604 matchingSkills1.AddEvent(eventName1);
605 CommonEventSubscribeInfo subscribeInfo1(matchingSkills1);
606 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo1);
607 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
608 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
609
610 MatchingSkills matchingSkills2;
611 matchingSkills2.AddEvent(eventName2);
612 CommonEventSubscribeInfo subscribeInfo2(matchingSkills2);
613 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo2);
614 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
615 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
616
617 MatchingSkills matchingSkills3;
618 matchingSkills3.AddEvent(eventName3);
619 CommonEventSubscribeInfo subscribeInfo3(matchingSkills3);
620 auto subscriberPtr3 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo3);
621 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr3), true);
622 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr3), true);
623 }
624
625 /*
626 * @tc.number: CES_SubscriptionEvent_2100
627 * @tc.name: UnSubscribeCommonEvent
628 * @tc.desc: Verify the same input string three times
629 */
630 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_2100, Function | MediumTest | Level1)
631 {
632 std::string eventName = "TESTEVENT4";
633
634 MatchingSkills matchingSkills;
635 matchingSkills.AddEvent(eventName);
636 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
637
638 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
639 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
640 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
641
642 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
643 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
644 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
645
646 auto subscriberPtr3 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
647 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr3), true);
648 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr3), true);
649 }
650
651 /*
652 * @tc.number: CES_SubscriptionEvent_2200
653 * @tc.name: UnSubscribeCommonEvent
654 * @tc.desc: Verify the same input empty string three times
655 */
656 HWTEST_F(cesSystemTest, CES_SubscriptionEvent_2200, Function | MediumTest | Level1)
657 {
658 std::string eventName = "";
659
660 MatchingSkills matchingSkills;
661 matchingSkills.AddEvent(eventName);
662 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
663
664 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
665 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
666 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
667
668 auto subscriberPtr2 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
669 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr2), true);
670 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr2), true);
671
672 auto subscriberPtr3 = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
673 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr3), true);
674 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr3), true);
675 }
676
677 /*
678 * @tc.number: CES_SendEvent_0100
679 * @tc.name: PublishCommonEvent
680 * @tc.desc: Verify the function when only set action
681 */
682 HWTEST_F(cesSystemTest, CES_SendEvent_0100, Function | MediumTest | Level1)
683 {
684 struct tm startTime = {0};
685 std::string eventName = "TESTEVENT_PUBLISH_ACTION";
686
687 Want wantTest;
688 wantTest.SetAction(eventName);
689 CommonEventData commonEventData(wantTest);
690
691 MatchingSkills matchingSkills;
692 matchingSkills.AddEvent(eventName);
693 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
694 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
695
696 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
697 mtx_.lock();
698 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
699
700 struct tm doingTime = {0};
701 int64_t seconds = 0;
702 while (!mtx_.try_lock()) {
703 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
704 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
705 if (seconds >= TIME_OUT_SECONDS_TWO) {
706 break;
707 }
708 }
709 mtx_.unlock();
710 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
711 }
712
713 /*
714 * @tc.number: CES_SendEvent_0200
715 * @tc.name: PublishCommonEvent
716 * @tc.desc: Verify the function when add entity
717 */
718 HWTEST_F(cesSystemTest, CES_SendEvent_0200, Function | MediumTest | Level1)
719 {
720 struct tm startTime = {0};
721 std::string eventName = "TESTEVENT_PUBLISH_ENTITY";
722 std::string entity = "ADDENTITY";
723
724 Want wantTest;
725 wantTest.SetAction(eventName);
726 wantTest.AddEntity(entity);
727 CommonEventData commonEventData(wantTest);
728
729 MatchingSkills matchingSkills;
730 matchingSkills.AddEvent(eventName);
731 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
732 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
733
734 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
735 mtx_.lock();
736 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
737 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData), true);
738
739 struct tm doingTime = {0};
740 int64_t seconds = 0;
741 while (!mtx_.try_lock()) {
742 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
743 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
744 if (seconds >= TIME_OUT_SECONDS_TWO) {
745 break;
746 }
747 }
748 mtx_.unlock();
749 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
750 }
751
752 /*
753 * @tc.number: CES_SendEvent_0300
754 * @tc.name: PublishCommonEvent
755 * @tc.desc: Verify the function when set scheme
756 */
757 HWTEST_F(cesSystemTest, CES_SendEvent_0300, Function | MediumTest | Level1)
758 {
759 struct tm startTime = {0};
760 std::string eventName = "TESTEVENT_PUBLISH_SCHEME";
761 std::string scheme = "SETSCHEME";
762
763 Want wantTest;
764 wantTest.SetAction(eventName);
765 CommonEventData commonEventData(wantTest);
766
767 MatchingSkills matchingSkills;
768 matchingSkills.AddEvent(eventName);
769 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
770 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
771
772 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
773 mtx_.lock();
774 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
775 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData), true);
776
777 struct tm doingTime = {0};
778 int64_t seconds = 0;
779 while (!mtx_.try_lock()) {
780 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
781 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
782 if (seconds >= TIME_OUT_SECONDS_TWO) {
783 break;
784 }
785 }
786 mtx_.unlock();
787 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
788 }
789
790 /*
791 * @tc.number: CES_SendEvent_0400
792 * @tc.name: PublishCommonEvent
793 * @tc.desc: Verify the function when set scheme and add entity
794 */
795 HWTEST_F(cesSystemTest, CES_SendEvent_0400, Function | MediumTest | Level1)
796 {
797 struct tm startTime = {0};
798 std::string eventName = "TESTEVENT_PUBLISH_SCHEME_ENTITY";
799 std::string scheme = "SETSCHEME";
800 std::string entity = "ADDENTITY";
801
802 Want wantTest;
803 wantTest.SetAction(eventName);
804 wantTest.AddEntity(entity);
805 CommonEventData commonEventData(wantTest);
806
807 MatchingSkills matchingSkills;
808 matchingSkills.AddEvent(eventName);
809 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
810 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
811
812 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
813 mtx_.lock();
814 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
815 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData), true);
816
817 struct tm doingTime = {0};
818 int64_t seconds = 0;
819 while (!mtx_.try_lock()) {
820 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
821 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
822 if (seconds >= TIME_OUT_SECONDS_TWO) {
823 break;
824 }
825 }
826 mtx_.unlock();
827 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
828 }
829
830 /*
831 * @tc.number: CES_SendEvent_0500
832 * @tc.name: SubscribeCommonEvent
833 * @tc.desc: Verify set action with sticky is false
834 */
835 HWTEST_F(cesSystemTest, CES_SendEvent_0500, Function | MediumTest | Level1)
836 {
837 struct tm startTime = {0};
838 std::string eventName = "TESTEVENT_PUBLISH_ACTION_INFO_FALSE";
839
840 Want wantTest;
841 wantTest.SetAction(eventName);
842 CommonEventData commonEventData(wantTest);
843
844 MatchingSkills matchingSkills;
845 matchingSkills.AddEvent(eventName);
846 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
847 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
848
849 CommonEventPublishInfo publishInfo;
850 publishInfo.SetSticky(false);
851
852 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
853 mtx_.lock();
854 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
855 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
856
857 struct tm doingTime = {0};
858 int64_t seconds = 0;
859 while (!mtx_.try_lock()) {
860 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
861 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
862 if (seconds >= TIME_OUT_SECONDS_TWO) {
863 break;
864 }
865 }
866 mtx_.unlock();
867 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
868 }
869
870 /*
871 * @tc.number: CES_SendEvent_0600
872 * @tc.name: PublishCommonEvent
873 * @tc.desc: Verify add entity with sticky is true
874 */
875 HWTEST_F(cesSystemTest, CES_SendEvent_0600, Function | MediumTest | Level1)
876 {
877 std::string eventName = "TESTEVENT_PUBLISH_ACTION_ENTITY_INFO_TRUE";
878 std::string entity = "ADDENTITY";
879
880 Want wantTest;
881 wantTest.SetAction(eventName);
882 wantTest.AddEntity(entity);
883 CommonEventData commonEventData(wantTest);
884
885 MatchingSkills matchingSkills;
886 matchingSkills.AddEvent(eventName);
887 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
888 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
889
890 CommonEventPublishInfo publishInfo;
891 publishInfo.SetSticky(true);
892
893 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
894 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
895 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
896 }
897
898 /*
899 * @tc.number: CES_SendEvent_0700
900 * @tc.name: PublishCommonEvent
901 * @tc.desc: Verify add entity with sticky is false
902 */
903 HWTEST_F(cesSystemTest, CES_SendEvent_0700, Function | MediumTest | Level1)
904 {
905 struct tm startTime = {0};
906 std::string eventName = "TESTEVENT_PUBLISH_ACTION_ENTITY_INFO_FALSE";
907 std::string entity = "ADDENTITY";
908
909 Want wantTest;
910 wantTest.SetAction(eventName);
911 wantTest.AddEntity(entity);
912 CommonEventData commonEventData(wantTest);
913
914 MatchingSkills matchingSkills;
915 matchingSkills.AddEvent(eventName);
916 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
917 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
918
919 CommonEventPublishInfo publishInfo;
920 publishInfo.SetSticky(false);
921
922 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
923 mtx_.lock();
924 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
925 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
926
927 struct tm doingTime = {0};
928 int64_t seconds = 0;
929 while (!mtx_.try_lock()) {
930 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
931 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
932 if (seconds >= TIME_OUT_SECONDS_TWO) {
933 break;
934 }
935 }
936 mtx_.unlock();
937 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
938 }
939
940 /*
941 * @tc.number: CES_SendEvent_0800
942 * @tc.name: PublishCommonEvent
943 * @tc.desc: Verify set action with sticky is true
944 */
945 HWTEST_F(cesSystemTest, CES_SendEvent_0800, Function | MediumTest | Level1)
946 {
947 std::string eventName = "TESTEVENT_PUBLISH_ACTION_INFO_TRUE";
948
949 Want wantTest;
950 wantTest.SetAction(eventName);
951 CommonEventData commonEventData(wantTest);
952
953 MatchingSkills matchingSkills;
954 matchingSkills.AddEvent(eventName);
955 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
956 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
957
958 CommonEventPublishInfo publishInfo;
959 publishInfo.SetSticky(true);
960
961 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
962 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
963 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
964 }
965
966 /*
967 * @tc.number: CES_SendEvent_0900
968 * @tc.name: PublishCommonEvent
969 * @tc.desc: Verify set scheme with sticky is true
970 */
971 HWTEST_F(cesSystemTest, CES_SendEvent_0900, Function | MediumTest | Level1)
972 {
973 std::string eventName = "TESTEVENT_PUBLISH_ACTION_SCHEME_INFO_TRUE";
974 std::string scheme = "SETSCHEME";
975
976 Want wantTest;
977 wantTest.SetAction(eventName);
978 CommonEventData commonEventData(wantTest);
979
980 MatchingSkills matchingSkills;
981 matchingSkills.AddEvent(eventName);
982 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
983 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
984
985 CommonEventPublishInfo publishInfo;
986 publishInfo.SetSticky(true);
987
988 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
989 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
990 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
991 }
992
993 /*
994 * @tc.number: CES_SendEvent_1000
995 * @tc.name: PublishCommonEvent
996 * @tc.desc: Verify set scheme with sticky is false
997 */
998 HWTEST_F(cesSystemTest, CES_SendEvent_1000, Function | MediumTest | Level1)
999 {
1000 struct tm startTime = {0};
1001 std::string eventName = "TESTEVENT_PUBLISH_ACTION_SCHEME_INFO_FALSE";
1002 std::string scheme = "SETSCHEME";
1003
1004 Want wantTest;
1005 wantTest.SetAction(eventName);
1006 CommonEventData commonEventData(wantTest);
1007
1008 MatchingSkills matchingSkills;
1009 matchingSkills.AddEvent(eventName);
1010 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1011 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1012
1013 CommonEventPublishInfo publishInfo;
1014 publishInfo.SetSticky(false);
1015
1016 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1017 mtx_.lock();
1018 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1019 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1020
1021 struct tm doingTime = {0};
1022 int64_t seconds = 0;
1023 while (!mtx_.try_lock()) {
1024 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1025 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1026 if (seconds >= TIME_OUT_SECONDS_TWO) {
1027 break;
1028 }
1029 }
1030 mtx_.unlock();
1031 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1032 }
1033
1034 /*
1035 * @tc.number: CES_SendEvent_1100
1036 * @tc.name: PublishCommonEvent
1037 * @tc.desc: Verify set scheme and add entity with sticky is true
1038 */
1039 HWTEST_F(cesSystemTest, CES_SendEvent_1100, Function | MediumTest | Level1)
1040 {
1041 std::string eventName = "TESTEVENT_PUBLISH_ACTION_ENTITY_SCHEME_INFO_TRUE";
1042 std::string entity = "ADDENTITY";
1043 std::string scheme = "SETSCHEME";
1044
1045 Want wantTest;
1046 wantTest.SetAction(eventName);
1047 wantTest.AddEntity(entity);
1048 CommonEventData commonEventData(wantTest);
1049
1050 MatchingSkills matchingSkills;
1051 matchingSkills.AddEvent(eventName);
1052 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1053 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1054
1055 CommonEventPublishInfo publishInfo;
1056 publishInfo.SetSticky(true);
1057
1058 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1059 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1060 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1061 }
1062
1063 /*
1064 * @tc.number: CES_SendEvent_1200
1065 * @tc.name: PublishCommonEvent
1066 * @tc.desc: Verify set scheme and add entity with sticky is false
1067 */
1068 HWTEST_F(cesSystemTest, CES_SendEvent_1200, Function | MediumTest | Level1)
1069 {
1070 struct tm startTime = {0};
1071 std::string eventName = "TESTEVENT_PUBLISH_ACTION_ENTITY_SCHEME_INFO_FALSE";
1072 std::string entity = "ADDENTITY";
1073 std::string scheme = "SETSCHEME";
1074
1075 Want wantTest;
1076 wantTest.SetAction(eventName);
1077 wantTest.AddEntity(entity);
1078 CommonEventData commonEventData(wantTest);
1079
1080 MatchingSkills matchingSkills;
1081 matchingSkills.AddEvent(eventName);
1082 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1083 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1084
1085 CommonEventPublishInfo publishInfo;
1086 publishInfo.SetSticky(false);
1087
1088 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1089 mtx_.lock();
1090 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1091 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1092
1093 struct tm doingTime = {0};
1094 int64_t seconds = 0;
1095 while (!mtx_.try_lock()) {
1096 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1097 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1098 if (seconds >= TIME_OUT_SECONDS_TWO) {
1099 break;
1100 }
1101 }
1102 mtx_.unlock();
1103 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1104 }
1105
1106 /*
1107 * @tc.number: CES_SendEventSetViscosity_0100
1108 * @tc.name: SetSticky
1109 * @tc.desc: Verify set action with sticky is false
1110 */
1111 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0100, Function | MediumTest | Level1)
1112 {
1113 struct tm startTime = {0};
1114 std::string eventName = "TESTEVENT_STICHY_ACTION_INFO_FALSE";
1115
1116 Want wantTest;
1117 wantTest.SetAction(eventName);
1118 CommonEventData commonEventData(wantTest);
1119
1120 MatchingSkills matchingSkills;
1121 matchingSkills.AddEvent(eventName);
1122 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1123 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1124
1125 CommonEventPublishInfo publishInfo;
1126 publishInfo.SetSticky(false);
1127
1128 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1129 mtx_.lock();
1130 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1131 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1132
1133 struct tm doingTime = {0};
1134 int64_t seconds = 0;
1135 while (!mtx_.try_lock()) {
1136 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1137 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1138 if (seconds >= TIME_OUT_SECONDS_TWO) {
1139 break;
1140 }
1141 }
1142 mtx_.unlock();
1143 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1144 }
1145
1146 /*
1147 * @tc.number: CES_SendEventSetViscosity_0200
1148 * @tc.name: SetSticky
1149 * @tc.desc: Verify add entity with sticky is true
1150 */
1151 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0200, Function | MediumTest | Level1)
1152 {
1153 std::string eventName = "TESTEVENT_STICKY_ACTION_ENTITY_INFO_TRUE";
1154 std::string entity = "ADDENTITY";
1155
1156 Want wantTest;
1157 wantTest.SetAction(eventName);
1158 wantTest.AddEntity(entity);
1159 CommonEventData commonEventData(wantTest);
1160
1161 MatchingSkills matchingSkills;
1162 matchingSkills.AddEvent(eventName);
1163 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1164 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1165
1166 CommonEventPublishInfo publishInfo;
1167 publishInfo.SetSticky(true);
1168
1169 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1170 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1171 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1172 }
1173
1174 /*
1175 * @tc.number: CES_SendEventSetViscosity_0300
1176 * @tc.name: SetSticky
1177 * @tc.desc: Verify add entity with sticky is false
1178 */
1179 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0300, Function | MediumTest | Level1)
1180 {
1181 struct tm startTime = {0};
1182 std::string eventName = "TESTEVENT_STICKY_ACTION_ENTITY_INFO_FALSE";
1183 std::string entity = "ADDENTITY";
1184
1185 Want wantTest;
1186 wantTest.SetAction(eventName);
1187 wantTest.AddEntity(entity);
1188 CommonEventData commonEventData(wantTest);
1189
1190 MatchingSkills matchingSkills;
1191 matchingSkills.AddEvent(eventName);
1192 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1193 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1194
1195 CommonEventPublishInfo publishInfo;
1196 publishInfo.SetSticky(false);
1197
1198 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1199 mtx_.lock();
1200 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1201 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1202
1203 struct tm doingTime = {0};
1204 int64_t seconds = 0;
1205 while (!mtx_.try_lock()) {
1206 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1207 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1208 if (seconds >= TIME_OUT_SECONDS_TWO) {
1209 break;
1210 }
1211 }
1212 mtx_.unlock();
1213 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1214 }
1215
1216 /*
1217 * @tc.number: CES_SendEventSetViscosity_0400
1218 * @tc.name: SetSticky
1219 * @tc.desc: Verify set action with sticky is true
1220 */
1221 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0400, Function | MediumTest | Level1)
1222 {
1223 std::string eventName = "TESTEVENT_STICKY_ACTION_INFO_TRUE";
1224
1225 Want wantTest;
1226 wantTest.SetAction(eventName);
1227 CommonEventData commonEventData(wantTest);
1228
1229 MatchingSkills matchingSkills;
1230 matchingSkills.AddEvent(eventName);
1231 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1232 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1233
1234 CommonEventPublishInfo publishInfo;
1235 publishInfo.SetSticky(true);
1236
1237 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1238 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1239 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1240 }
1241
1242 /*
1243 * @tc.number: CES_SendEventSetViscosity_0500
1244 * @tc.name: SetSticky
1245 * @tc.desc: Verify set scheme with sticky is true
1246 */
1247 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0500, Function | MediumTest | Level1)
1248 {
1249 std::string eventName = "TESTEVENT_STICKY_ACTION_SCHEME_INFO_TRUE";
1250 std::string scheme = "SETSCHEME";
1251
1252 Want wantTest;
1253 wantTest.SetAction(eventName);
1254 CommonEventData commonEventData(wantTest);
1255
1256 MatchingSkills matchingSkills;
1257 matchingSkills.AddEvent(eventName);
1258 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1259 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1260
1261 CommonEventPublishInfo publishInfo;
1262 publishInfo.SetSticky(true);
1263
1264 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1265 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1266 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1267 }
1268
1269 /*
1270 * @tc.number: CES_SendEventSetViscosity_0600
1271 * @tc.name: SetSticky
1272 * @tc.desc: Verify set scheme with sticky is false
1273 */
1274 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0600, Function | MediumTest | Level1)
1275 {
1276 struct tm startTime = {0};
1277 std::string eventName = "TESTEVENT_STICKY_ACTION_SCHEME_INFO_FALSE";
1278 std::string scheme = "SETSCHEME";
1279
1280 Want wantTest;
1281 wantTest.SetAction(eventName);
1282 CommonEventData commonEventData(wantTest);
1283
1284 MatchingSkills matchingSkills;
1285 matchingSkills.AddEvent(eventName);
1286 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1287 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1288
1289 CommonEventPublishInfo publishInfo;
1290 publishInfo.SetSticky(false);
1291
1292 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1293 mtx_.lock();
1294 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1295 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1296
1297 struct tm doingTime = {0};
1298 int64_t seconds = 0;
1299 while (!mtx_.try_lock()) {
1300 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1301 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1302 if (seconds >= TIME_OUT_SECONDS_TWO) {
1303 break;
1304 }
1305 }
1306 mtx_.unlock();
1307 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1308 }
1309
1310 /*
1311 * @tc.number: CES_SendEventSetViscosity_0700
1312 * @tc.name: SetSticky
1313 * @tc.desc: Verify set scheme and add entity with sticky is true
1314 */
1315 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0700, Function | MediumTest | Level1)
1316 {
1317 std::string eventName = "TESTEVENT_STICKY_ACTION_ENTITY_SCHEME_INFO_TRUE";
1318 std::string entity = "ADDENTITY";
1319 std::string scheme = "SETSCHEME";
1320
1321 Want wantTest;
1322 wantTest.SetAction(eventName);
1323 wantTest.AddEntity(entity);
1324 CommonEventData commonEventData(wantTest);
1325
1326 MatchingSkills matchingSkills;
1327 matchingSkills.AddEvent(eventName);
1328 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1329 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1330
1331 CommonEventPublishInfo publishInfo;
1332 publishInfo.SetSticky(true);
1333
1334 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1335 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1336 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1337 }
1338
1339 /*
1340 * @tc.number: CES_SendEventSetViscosity_0800
1341 * @tc.name:SetSticky
1342 * @tc.desc: Verify set scheme and add entity with sticky is false
1343 */
1344 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0800, Function | MediumTest | Level1)
1345 {
1346 struct tm startTime = {0};
1347 std::string eventName = "TESTEVENT_STICKY_ACTION_ENTITY_SCHEME_INFO_FALSE";
1348 std::string entity = "ADDENTITY";
1349 std::string scheme = "SETSCHEME";
1350
1351 Want wantTest;
1352 wantTest.SetAction(eventName);
1353 wantTest.AddEntity(entity);
1354 CommonEventData commonEventData(wantTest);
1355
1356 MatchingSkills matchingSkills;
1357 matchingSkills.AddEvent(eventName);
1358 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1359 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1360
1361 CommonEventPublishInfo publishInfo;
1362 publishInfo.SetSticky(false);
1363
1364 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1365 mtx_.lock();
1366 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1367 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1368
1369 struct tm doingTime = {0};
1370 int64_t seconds = 0;
1371 while (!mtx_.try_lock()) {
1372 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1373 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1374 if (seconds >= TIME_OUT_SECONDS_TWO) {
1375 break;
1376 }
1377 }
1378 mtx_.unlock();
1379 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1380 }
1381
1382 /*
1383 * @tc.number: CES_SendEventSetViscosity_0900
1384 * @tc.name: GetStickyCommonEvent
1385 * @tc.desc: publish common event set sticky to true and verify the action of stickyData
1386 */
1387 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_0900, Function | MediumTest | Level1)
1388 {
1389 std::string eventName = "TESTEVENT_GETSTICKY_";
1390 std::string eventActionStr = "TESTEVENT_GETSTICKY_Str";
1391
1392 Want wantTest;
1393 wantTest.SetAction(eventName);
1394 CommonEventData commonEventData(wantTest);
1395
1396 MatchingSkills matchingSkills;
1397 matchingSkills.AddEvent(eventName);
1398 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1399 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1400
1401 CommonEventPublishInfo publishInfo;
1402 publishInfo.SetSticky(true);
1403
1404 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1405 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1406
1407 CommonEventData stickyData;
1408 CommonEventManager::GetStickyCommonEvent(eventName, stickyData);
1409 EXPECT_FALSE(eventActionStr == stickyData.GetWant().GetAction());
1410 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1411 }
1412
1413 /*
1414 * @tc.number: CES_SendEventSetViscosity_1000
1415 * @tc.name: set sticky and get another action
1416 * @tc.desc: publish common event set sticky to true and verify the action of stickyData
1417 */
1418 HWTEST_F(cesSystemTest, CES_SendEventSetViscosity_1000, TestSize.Level2)
1419 {
1420 std::string eventName = "TESTEVENT_GETSTICKY_FALSE";
1421 std::string actionTest = "CHECKTESTACTION";
1422
1423 Want wantTest;
1424 wantTest.SetAction(eventName);
1425 CommonEventData commonEventData(wantTest);
1426
1427 MatchingSkills matchingSkills;
1428 matchingSkills.AddEvent(eventName);
1429 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1430 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1431
1432 CommonEventPublishInfo publishInfo;
1433 publishInfo.SetSticky(true);
1434
1435 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1436 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1437
1438 CommonEventData stickyData;
1439 EXPECT_EQ(CommonEventManager::GetStickyCommonEvent(actionTest, stickyData), false);
1440 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1441 }
1442
1443 /*
1444 * @tc.number: CES_ReceiveEvent_0100
1445 * @tc.name:OnReceiveEvent
1446 * @tc.desc: Verify the function when only set action
1447 */
1448 HWTEST_F(cesSystemTest, CES_ReceiveEvent_0100, Function | MediumTest | Level1)
1449 {
1450 std::string eventName = "TESTEVENT_RECEIVE_ACTIONReceiveEvent_0100";
1451
1452 Want wantTest;
1453 wantTest.SetAction(eventName);
1454 CommonEventData commonEventData(wantTest);
1455
1456 MatchingSkills matchingSkills;
1457 matchingSkills.AddEvent(eventName);
1458 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1459 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1460
1461 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1462 mtx_.lock();
1463 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData), true);
1464
1465 struct tm startTime = {0};
1466 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1467 struct tm doingTime = {0};
1468 int64_t seconds = 0;
1469 while (!mtx_.try_lock()) {
1470 // get current time and compare it with the start time
1471 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1472 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1473 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1474 break;
1475 }
1476 }
1477 // expect the subscriber could receive the event within 5 seconds.
1478 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
1479 mtx_.unlock();
1480 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1481 }
1482
1483 /*
1484 * @tc.number: CES_ReceiveEvent_0200
1485 * @tc.name: OnReceiveEvent
1486 * @tc.desc: Verify the function when add entity
1487 */
1488 HWTEST_F(cesSystemTest, CES_ReceiveEvent_0200, Function | MediumTest | Level1)
1489 {
1490 bool result = true;
1491 std::string eventName = "TESTEVENT_RECEIVE_ENTITYReceiveEvent_0200";
1492 std::string entity = "ADDENTITY";
1493
1494 Want wantTest;
1495 wantTest.SetAction(eventName);
1496 wantTest.AddEntity(entity);
1497 CommonEventData commonEventData(wantTest);
1498
1499 MatchingSkills matchingSkills;
1500 matchingSkills.AddEvent(eventName);
1501 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1502 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1503
1504 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1505 mtx_.lock();
1506 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData), true);
1507
1508 struct tm startTime = {0};
1509 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1510 struct tm doingTime = {0};
1511 int64_t seconds = 0;
1512 while (!mtx_.try_lock()) {
1513 // get current time and compare it with the start time
1514 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1515 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1516 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1517 result = false;
1518 break;
1519 }
1520 }
1521 // The publisher sets the Entity, the receiver must set it, otherwise the receiver will not receive the information
1522 EXPECT_FALSE(result);
1523 mtx_.unlock();
1524 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1525 }
1526
1527 /*
1528 * @tc.number: CES_ReceiveEvent_0300
1529 * @tc.name: OnReceiveEvent
1530 * @tc.desc: Verify set action with sticky is false
1531 */
1532 HWTEST_F(cesSystemTest, CES_ReceiveEvent_0300, Function | MediumTest | Level1)
1533 {
1534 std::string eventName = "TESTEVENT_RECEIVE_ACTION_INFO_FALSE";
1535
1536 Want wantTest;
1537 wantTest.SetAction(eventName);
1538 CommonEventData commonEventData(wantTest);
1539
1540 MatchingSkills matchingSkills;
1541 matchingSkills.AddEvent(eventName);
1542 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1543 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1544
1545 CommonEventPublishInfo publishInfo;
1546 publishInfo.SetSticky(false);
1547
1548 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1549 mtx_.lock();
1550 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1551
1552 struct tm startTime = {0};
1553 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1554 struct tm doingTime = {0};
1555 int64_t seconds = 0;
1556 while (!mtx_.try_lock()) {
1557 // get current time and compare it with the start time
1558 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1559 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1560 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1561 break;
1562 }
1563 }
1564 // expect the subscriber could receive the event within 5 seconds.
1565 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
1566 mtx_.unlock();
1567 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1568 }
1569
1570 /*
1571 * @tc.number: CES_ReceiveEvent_0400
1572 * @tc.name: OnReceiveEvent
1573 * @tc.desc: Verify set action with sticky is true
1574 */
1575 HWTEST_F(cesSystemTest, CES_ReceiveEvent_0400, Function | MediumTest | Level1)
1576 {
1577 bool result = true;
1578 std::string eventName = "TESTEVENT_RECEIVE_ACTION_INFO_TRUEReceiveEvent_0400";
1579
1580 Want wantTest;
1581 wantTest.SetAction(eventName);
1582 CommonEventData commonEventData(wantTest);
1583
1584 MatchingSkills matchingSkills;
1585 matchingSkills.AddEvent(eventName);
1586 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1587 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1588
1589 CommonEventPublishInfo publishInfo;
1590 publishInfo.SetSticky(true);
1591
1592 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1593 mtx_.lock();
1594 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1595
1596 struct tm startTime = {0};
1597 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1598 struct tm doingTime = {0};
1599 int64_t seconds = 0;
1600 while (!mtx_.try_lock()) {
1601 // get current time and compare it with the start time
1602 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1603 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1604 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1605 result = false;
1606 break;
1607 }
1608 }
1609 // expect the subscriber could receive the event within 5 seconds.
1610 EXPECT_TRUE(result);
1611 mtx_.unlock();
1612 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1613 }
1614
1615 /*
1616 * @tc.number: CES_ReceiveEvent_0500
1617 * @tc.name: OnReceiveEvent
1618 * @tc.desc: Verify add entity with sticky is false
1619 */
1620 HWTEST_F(cesSystemTest, CES_ReceiveEvent_0500, Function | MediumTest | Level1)
1621 {
1622 bool result = true;
1623 std::string eventName = "TESTEVENT_RECEIVE_ENTITY_INFO_FALSE";
1624 std::string entity = "ADDENTITY";
1625
1626 Want wantTest;
1627 wantTest.SetAction(eventName);
1628 wantTest.AddEntity(entity);
1629 CommonEventData commonEventData(wantTest);
1630
1631 MatchingSkills matchingSkills;
1632 matchingSkills.AddEvent(eventName);
1633 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1634 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1635
1636 CommonEventPublishInfo publishInfo;
1637 publishInfo.SetSticky(false);
1638
1639 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1640 mtx_.lock();
1641 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1642
1643 struct tm startTime = {0};
1644 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1645 struct tm doingTime = {0};
1646 int64_t seconds = 0;
1647 while (!mtx_.try_lock()) {
1648 // get current time and compare it with the start time
1649 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1650 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1651 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1652 result = false;
1653 break;
1654 }
1655 }
1656 // The publisher sets the Entity, the receiver must set it, otherwise the receiver will not receive the information
1657 EXPECT_FALSE(result);
1658 mtx_.unlock();
1659 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1660 }
1661
1662 /*
1663 * @tc.number: CES_ReceiveEvent_0600
1664 * @tc.name:OnReceiveEvent
1665 * @tc.desc: Verify add entity with sticky is true
1666 */
1667 HWTEST_F(cesSystemTest, CES_ReceiveEvent_0600, Function | MediumTest | Level1)
1668 {
1669 bool result = true;
1670 std::string eventName = "testEventReceiveEntityInfoFalse";
1671 std::string entity = "addEntity";
1672
1673 Want wantTest;
1674 wantTest.SetAction(eventName);
1675 wantTest.AddEntity(entity);
1676 CommonEventData commonEventData(wantTest);
1677
1678 MatchingSkills matchingSkills;
1679 matchingSkills.AddEvent(eventName);
1680 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1681 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1682
1683 CommonEventPublishInfo publishInfo;
1684 publishInfo.SetSticky(true);
1685
1686 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1687 mtx_.lock();
1688 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1689
1690 struct tm startTime = {0};
1691 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1692 struct tm doingTime = {0};
1693 int64_t seconds = 0;
1694 while (!mtx_.try_lock()) {
1695 // get current time and compare it with the start time
1696 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1697 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1698 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1699 result = false;
1700 break;
1701 }
1702 }
1703 // expect the subscriber could receive the event within 5 seconds.
1704 EXPECT_FALSE(result);
1705 mtx_.unlock();
1706 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1707 }
1708
1709 /*
1710 * @tc.number: CES_SubscriptionEventTheme_0100
1711 * @tc.name: AddEvent
1712 * @tc.desc: Verify add an event Theme
1713 */
1714 HWTEST_F(cesSystemTest, CES_SubscriptionEventTheme_0100, Function | MediumTest | Level1)
1715 {
1716 std::string eventName = "TESTADDTHEME";
1717 MatchingSkills matchingSkills;
1718 matchingSkills.AddEvent(eventName);
1719 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1720 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1721
1722 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1723 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1724 }
1725
1726 /*
1727 * @tc.number: CES_SubscriptionEventTheme_0200
1728 * @tc.name: AddEvent
1729 * @tc.desc: Verify add multiple enent themes
1730 */
1731 HWTEST_F(cesSystemTest, CES_SubscriptionEventTheme_0200, Function | MediumTest | Level1)
1732 {
1733 std::string eventName1 = "TESTADDTHEME1";
1734 std::string eventName2 = "TESTADDTHEME2";
1735
1736 MatchingSkills matchingSkills;
1737 matchingSkills.AddEvent(eventName1);
1738 matchingSkills.AddEvent(eventName2);
1739 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1740 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1741
1742 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1743 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1744 }
1745
1746 /*
1747 * @tc.number: CES_SubscriptionEventTheme_0300
1748 * @tc.name: MatchEvent
1749 * @tc.desc: Verify march an event theme
1750 */
1751 HWTEST_F(cesSystemTest, CES_SubscriptionEventTheme_0300, Function | MediumTest | Level1)
1752 {
1753 std::string eventName = "TESTEVENT_MATCHEVENTTEST";
1754
1755 Want wantTest;
1756 wantTest.SetAction(eventName);
1757 MatchingSkills matchingSkills;
1758 matchingSkills.AddEvent(eventName);
1759
1760 EXPECT_EQ(matchingSkills.Match(wantTest), true);
1761 }
1762
1763 /*
1764 * @tc.number: CES_SubscriptionEventTheme_0400
1765 * @tc.name: MatchEvent
1766 * @tc.desc: Verify march other event theme
1767 */
1768 HWTEST_F(cesSystemTest, CES_SubscriptionEventTheme_0400, Function | MediumTest | Level1)
1769 {
1770 std::string eventName = "TESTMATCHEVENTTOPICAL";
1771 std::string eventNameCompare = "TESTMATCHEVENTTOPICAL_COMPARE";
1772
1773 Want wantTest;
1774 wantTest.SetAction(eventNameCompare);
1775 MatchingSkills matchingSkills;
1776 matchingSkills.AddEvent(eventName);
1777
1778 EXPECT_EQ(matchingSkills.Match(wantTest), false);
1779 }
1780
1781 /*
1782 * @tc.number: CES_SendEvent_1300
1783 * @tc.name: PublishCommonEvent
1784 * @tc.desc: The publisher can send normally, but does not have permission
1785 * to send system events and cannot receive published system events
1786 * This test case has been covered in the module test, and the system test
1787 * cannot simulate the non-subsystem scenario
1788 */
1789 HWTEST_F(cesSystemTest, CES_SendEvent_1300, Function | MediumTest | Level1)
1790 {
1791 GTEST_LOG_(INFO) << "start CES_SendEvent_1300";
1792 bool sysResult = false;
1793 std::string eventName = CommonEventSupport::COMMON_EVENT_ABILITY_ADDED;
1794
1795 Want wantTest;
1796 wantTest.SetAction(eventName);
1797 CommonEventData commonEventData(wantTest);
1798
1799 MatchingSkills matchingSkills;
1800 matchingSkills.AddEvent(eventName);
1801 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1802 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1803
1804 CommonEventPublishInfo publishInfo;
1805
1806 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1807 mtx_.lock();
1808 // The publisher can send normally, but does not have permission to send system events
1809 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1810
1811 struct tm startTime = {0};
1812 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1813 struct tm doingTime = {0};
1814 int64_t seconds = 0;
1815
1816 while (!mtx_.try_lock()) {
1817 // get current time and compare it with the start time
1818 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1819 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1820 if (seconds >= TIME_OUT_SECONDS_) {
1821 sysResult = true;
1822 break;
1823 }
1824 }
1825 // Unable to receive published system events, failed to send system events
1826 EXPECT_FALSE(sysResult);
1827 mtx_.unlock();
1828 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1829 GTEST_LOG_(INFO) << "end CES_SendEvent_1300";
1830 }
1831
1832 /*
1833 * @tc.number: CES_SendEvent_1400
1834 * @tc.name: PublishCommonEvent
1835 * @tc.desc: The publisher can send normally, but does not have permission
1836 * to send system events and cannot receive published system events
1837 * This test case has been covered in the module test, and the system test
1838 * cannot simulate the non-subsystem scenario
1839 */
1840 HWTEST_F(cesSystemTest, CES_SendEvent_1400, Function | MediumTest | Level1)
1841 {
1842 bool sysResult = false;
1843 std::string eventName = CommonEventSupport::COMMON_EVENT_ABILITY_REMOVED;
1844
1845 Want wantTest;
1846 wantTest.SetAction(eventName);
1847 CommonEventData commonEventData(wantTest);
1848
1849 MatchingSkills matchingSkills;
1850 matchingSkills.AddEvent(eventName);
1851 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1852 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1853
1854 CommonEventPublishInfo publishInfo;
1855
1856 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1857 mtx_.lock();
1858 // The publisher can send normally, but does not have permission to send system events
1859 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1860
1861 struct tm startTime = {0};
1862 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1863 struct tm doingTime = {0};
1864 int64_t seconds = 0;
1865
1866 while (!mtx_.try_lock()) {
1867 // get current time and compare it with the start time
1868 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1869 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1870 if (seconds >= TIME_OUT_SECONDS_) {
1871 sysResult = true;
1872 break;
1873 }
1874 }
1875 // Unable to receive published system events, failed to send system events
1876 EXPECT_FALSE(sysResult);
1877 mtx_.unlock();
1878 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1879 }
1880
1881 /*
1882 * @tc.number: CES_SendEvent_1500
1883 * @tc.name: PublishCommonEvent
1884 * @tc.desc: The publisher can send normally, but does not have permission
1885 * to send system events and cannot receive published system events
1886 * This test case has been covered in the module test, and the system test
1887 * cannot simulate the non-subsystem scenario
1888 */
1889 HWTEST_F(cesSystemTest, CES_SendEvent_1500, Function | MediumTest | Level1)
1890 {
1891 bool sysResult = false;
1892 std::string eventName = CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED;
1893
1894 Want wantTest;
1895 wantTest.SetAction(eventName);
1896 CommonEventData commonEventData(wantTest);
1897
1898 MatchingSkills matchingSkills;
1899 matchingSkills.AddEvent(eventName);
1900 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1901 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1902
1903 CommonEventPublishInfo publishInfo;
1904
1905 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1906 mtx_.lock();
1907 // The publisher can send normally, but does not have permission to send system events
1908 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1909
1910 struct tm startTime = {0};
1911 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1912 struct tm doingTime = {0};
1913 int64_t seconds = 0;
1914
1915 while (!mtx_.try_lock()) {
1916 // get current time and compare it with the start time
1917 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1918 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1919 if (seconds >= TIME_OUT_SECONDS_) {
1920 sysResult = true;
1921 break;
1922 }
1923 }
1924 // Unable to receive published system events, failed to send system events
1925 EXPECT_FALSE(sysResult);
1926 mtx_.unlock();
1927 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1928 }
1929
1930 /*
1931 * @tc.number: CES_SendEvent_1600
1932 * @tc.name: PublishCommonEvent
1933 * @tc.desc: publisher cannot receive published system events
1934 * This test case has been covered in the module test, and the system test
1935 * cannot simulate the non-subsystem scenario
1936 */
1937 HWTEST_F(cesSystemTest, CES_SendEvent_1600, Function | MediumTest | Level1)
1938 {
1939 bool result = true;
1940 std::string eventName = CommonEventSupport::COMMON_EVENT_ACCOUNT_DELETED;
1941
1942 Want wantTest;
1943 wantTest.SetAction(eventName);
1944 CommonEventData commonEventData(wantTest);
1945
1946 MatchingSkills matchingSkills;
1947 matchingSkills.AddEvent(eventName);
1948 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1949 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1950
1951 CommonEventPublishInfo publishInfo;
1952
1953 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
1954 mtx_.lock();
1955 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
1956
1957 struct tm startTime = {0};
1958 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1959 struct tm doingTime = {0};
1960 int64_t seconds = 0;
1961 while (!mtx_.try_lock()) {
1962 // get current time and compare it with the start time
1963 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1964 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1965 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1966 result = false;
1967 break;
1968 }
1969 }
1970 // System events published by ordinary publishers, the publication fails, and the receiver cannot receive it
1971 EXPECT_TRUE(result);
1972 mtx_.unlock();
1973 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
1974 }
1975
1976 /*
1977 * @tc.number: CES_SendEvent_1700
1978 * @tc.name: PublishCommonEvent
1979 * @tc.desc: Both subscribers subscribe to the event, after the event is published, both the subscribers can receive the
1980 * event
1981 */
1982 HWTEST_F(cesSystemTest, CES_SendEvent_1700, Function | MediumTest | Level1)
1983 {
1984 struct tm startTime = {0};
1985 std::string eventName = COMPARE_STR;
1986
1987 Want wantTest;
1988 wantTest.SetAction(eventName);
1989 CommonEventData commonEventData(wantTest);
1990 commonEventData.SetCode(g_CODE_COMPARE1);
1991
1992 MatchingSkills matchingSkills;
1993 matchingSkills.AddEvent(eventName);
1994 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1995 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
1996 auto subscriberPtr1 = std::make_shared<CommonEventServicesSystemTestSubscriber>(subscribeInfo);
1997
1998 CommonEventPublishInfo publishInfo;
1999
2000 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2001 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr1), true);
2002 mtx_.lock();
2003 mtx2_.lock();
2004 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
2005
2006 struct tm doingTime = {0};
2007 int64_t seconds = 0;
2008 while (!mtx_.try_lock()) {
2009 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2010 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2011 if (seconds >= TIME_OUT_SECONDS_TWO) {
2012 break;
2013 }
2014 }
2015 while (!mtx2_.try_lock()) {
2016 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2017 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2018 if (seconds >= TIME_OUT_SECONDS_TWO) {
2019 break;
2020 }
2021 }
2022
2023 mtx_.unlock();
2024 mtx2_.unlock();
2025 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2026 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr1), true);
2027 }
2028
2029 /*
2030 * @tc.number: CES_SendEvent_1800
2031 * @tc.name: PublishCommonEvent
2032 * @tc.desc: One subscriber subscribe to the event and another subscriber does not subscribe to the event,
2033 * after the event is published, subscribed subscriber can receive the event and unsubscribed
2034 * subscribe can not receive the event
2035 */
2036 HWTEST_F(cesSystemTest, CES_SendEvent_1800, Function | MediumTest | Level1)
2037 {
2038 struct tm startTime = {0};
2039 std::string eventName = COMPARE_STR_FALSE;
2040
2041 Want wantTest;
2042 wantTest.SetAction(eventName);
2043 CommonEventData commonEventData(wantTest);
2044 commonEventData.SetCode(g_CODE_COMPARE3);
2045
2046 MatchingSkills matchingSkills;
2047 matchingSkills.AddEvent(eventName);
2048 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2049 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2050
2051 CommonEventPublishInfo publishInfo;
2052
2053 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2054 mtx_.lock();
2055 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2056 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
2057
2058 struct tm doingTime = {0};
2059 int64_t seconds = 0;
2060 while (!mtx_.try_lock()) {
2061 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2062 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2063 if (seconds >= TIME_OUT_SECONDS_TWO) {
2064 break;
2065 }
2066 }
2067 mtx_.unlock();
2068 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2069 }
2070
2071 /*
2072 * @tc.number: CES_SetEventAuthority_0100
2073 * @tc.name: SetPermission
2074 * @tc.desc: Set permission for common event subscribers and verify successfully subscribe to common events
2075 */
2076 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0100, Function | MediumTest | Level1)
2077 {
2078 std::string eventName = "TESTEVENT_SUBSCRIBER_PERMISSION";
2079 std::string permissin = "ohos.permission.MANAGE_LOCAL_ACCOUNTS";
2080
2081 MatchingSkills matchingSkills;
2082 matchingSkills.AddEvent(eventName);
2083 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2084 subscribeInfo.SetPermission(permissin);
2085 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2086
2087 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2088 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2089 }
2090
2091 /*
2092 * @tc.number: CES_SetEventAuthority_0200
2093 * @tc.name:Set permission and priority for common event subscribers
2094 * @tc.desc: Set permission and priority for common event subscribers and verify successfully subscribe to
2095 * common events
2096 */
2097 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0200, Function | MediumTest | Level1)
2098 {
2099 std::string eventName = "TESTEVENT_SUBSCRIBER_PERMISSION_PRIORITY";
2100 std::string permissin = "PERMISSION";
2101
2102 MatchingSkills matchingSkills;
2103 matchingSkills.AddEvent(eventName);
2104 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2105 subscribeInfo.SetPermission(permissin);
2106 subscribeInfo.SetPriority(1);
2107 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2108
2109 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2110 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2111 }
2112
2113 /*
2114 * @tc.number: CES_SetEventAuthority_0300
2115 * @tc.name: SetPermission SetPriority and SetDeviceId
2116 * @tc.desc: Set permission and priority and DeviceId for common event subscribers and verify successfully
2117 * subscribe to common events
2118 */
2119 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0300, Function | MediumTest | Level1)
2120 {
2121 std::string eventName = "TESTEVENT_SUBSCRIBER_PERMISSION_PRIORITY_D";
2122 std::string permissin = "PERMISSION";
2123 std::string deviceId = "deviceId";
2124
2125 MatchingSkills matchingSkills;
2126 matchingSkills.AddEvent(eventName);
2127 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2128 subscribeInfo.SetPermission(permissin);
2129 subscribeInfo.SetPriority(1);
2130 subscribeInfo.SetDeviceId(deviceId);
2131 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2132
2133 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2134 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2135 }
2136
2137 /*
2138 * @tc.number: CES_SetEventAuthority_0400
2139 * @tc.name: SetPermission
2140 * @tc.desc: Set permission for common event subscribers and verify successfully Unsubscribe to common events
2141 */
2142 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0400, Function | MediumTest | Level1)
2143 {
2144 std::string eventName = "TESTEVENT_UNSUBSCRIBER_PERMISSION";
2145 std::string permissin = "PERMISSION";
2146
2147 MatchingSkills matchingSkills;
2148 matchingSkills.AddEvent(eventName);
2149 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2150 subscribeInfo.SetPermission(permissin);
2151 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2152
2153 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2154 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2155 }
2156
2157 /*
2158 * @tc.number: CES_SetEventAuthority_0500
2159 * @tc.name: SetPermission and SetPriority
2160 * @tc.desc: Set permission and priority for common event subscribers and verify successfully Unsubscribe to
2161 * common events
2162 */
2163 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0500, Function | MediumTest | Level1)
2164 {
2165 std::string eventName = "TESTEVENT_UNSUBSCRIBER_PERMISSION_PRIORITY";
2166 std::string permissin = "PERMISSION";
2167
2168 MatchingSkills matchingSkills;
2169 matchingSkills.AddEvent(eventName);
2170 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2171 subscribeInfo.SetPermission(permissin);
2172 subscribeInfo.SetPriority(1);
2173 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2174
2175 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2176 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2177 }
2178
2179 /*
2180 * @tc.number: CES_SetEventAuthority_0600
2181 * @tc.name: SetPermission SetPriority and SetDeviceId
2182 * @tc.desc: Set permission and priority and DeviceId for common event subscribers and verify successfully
2183 * Unsubscribe to common events
2184 */
2185 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0600, Function | MediumTest | Level1)
2186 {
2187 std::string eventName = "TESTEVENT_UNSUBSCRIBER_PERMISSION_PRIORITY_D";
2188 std::string permissin = "PERMISSION";
2189 std::string deviceId = "deviceId";
2190
2191 MatchingSkills matchingSkills;
2192 matchingSkills.AddEvent(eventName);
2193 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2194 subscribeInfo.SetPermission(permissin);
2195 subscribeInfo.SetPriority(1);
2196 subscribeInfo.SetDeviceId(deviceId);
2197 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2198
2199 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2200 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2201 }
2202
2203 /*
2204 * @tc.number: CES_SetEventAuthority_0700
2205 * @tc.name: SetSubscriberPermissions
2206 * @tc.desc: Set permission for common event subscribers and verify successfully publish common events
2207 */
2208 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0700, Function | MediumTest | Level1)
2209 {
2210 struct tm startTime = {0};
2211 std::string eventName = "TESTEVENT_PUBLISH_ACTION_PERMISSION";
2212 std::string permissin = "PERMISSION";
2213
2214 Want wantTest;
2215 wantTest.SetAction(eventName);
2216 CommonEventData commonEventData(wantTest);
2217
2218 MatchingSkills matchingSkills;
2219 matchingSkills.AddEvent(eventName);
2220 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2221 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2222
2223 std::vector<std::string> permissins;
2224 permissins.emplace_back(permissin);
2225 CommonEventPublishInfo publishInfo;
2226 publishInfo.SetSubscriberPermissions(permissins);
2227
2228 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2229 mtx_.lock();
2230 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2231 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
2232
2233 struct tm doingTime = {0};
2234 int64_t seconds = 0;
2235 while (!mtx_.try_lock()) {
2236 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2237 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2238 if (seconds >= TIME_OUT_SECONDS_TWO) {
2239 break;
2240 }
2241 }
2242 mtx_.unlock();
2243 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2244 }
2245
2246 /*
2247 * @tc.number: CES_SetEventAuthority_0800
2248 * @tc.name: SetSubscriberPermissions
2249 * @tc.desc: Set permission for common event subscribers and verify successfully receive common events
2250 */
2251 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0800, Function | MediumTest | Level1)
2252 {
2253 bool result = false;
2254 std::string eventName = "TESTEVENT_PUBLISH_ACTION_PERMISSION_R";
2255 std::string permissin = "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS";
2256
2257 Want wantTest;
2258 wantTest.SetAction(eventName);
2259 CommonEventData commonEventData(wantTest);
2260
2261 MatchingSkills matchingSkills;
2262 matchingSkills.AddEvent(eventName);
2263 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2264 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2265
2266 std::vector<std::string> permissins;
2267 permissins.emplace_back(permissin);
2268 CommonEventPublishInfo publishInfo;
2269 publishInfo.SetSubscriberPermissions(permissins);
2270
2271 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2272 mtx_.lock();
2273 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
2274
2275 struct tm startTime = {0};
2276 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2277 struct tm doingTime = {0};
2278 int64_t seconds = 0;
2279 while (!mtx_.try_lock()) {
2280 // get current time and compare it with the start time
2281 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2282 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2283 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
2284 result = true;
2285 break;
2286 }
2287 }
2288 EXPECT_FALSE(result);
2289 mtx_.unlock();
2290 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2291 }
2292
2293 /*
2294 * @tc.number: CES_SetEventAuthority_0900
2295 * @tc.name: SetThreadMode
2296 * @tc.desc: Set ThreadMode for common event subscribers and verify successfully subscribe to common events
2297 */
2298 HWTEST_F(cesSystemTest, CES_SetEventAuthority_0900, Function | MediumTest | Level1)
2299 {
2300 std::string eventName = "TESTEVENT_SUBSCRIBER_SETTHREADMODE";
2301
2302 MatchingSkills matchingSkills;
2303 matchingSkills.AddEvent(eventName);
2304 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2305 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER);
2306 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2307
2308 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2309 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2310 }
2311
2312 /*
2313 * @tc.number: CES_SetEventAuthority_1000
2314 * @tc.name: SetThreadMode
2315 * @tc.desc: Set ThreadMode for common event subscribers and verify successfully Unsubscribe to common events
2316 */
2317 HWTEST_F(cesSystemTest, CES_SetEventAuthority_1000, Function | MediumTest | Level1)
2318 {
2319 std::string eventName = "TESTEVENT_UNSUBSCRIBER_SETTHREADMODE";
2320
2321 MatchingSkills matchingSkills;
2322 matchingSkills.AddEvent(eventName);
2323 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2324 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER);
2325 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2326
2327 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2328 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2329 }
2330
2331 /*
2332 * @tc.number: CES_SetEventAuthority_1100
2333 * @tc.name: SetThreadMode
2334 * @tc.desc: Set ThreadMode for common event subscribers and verify successfully publish to common events
2335 */
2336 HWTEST_F(cesSystemTest, CES_SetEventAuthority_1100, Function | MediumTest | Level1)
2337 {
2338 struct tm startTime = {0};
2339 std::string eventName = "TESTEVENT_PUBLISH_ACTION_SETTHREADMODE";
2340
2341 Want wantTest;
2342 wantTest.SetAction(eventName);
2343 CommonEventData commonEventData(wantTest);
2344
2345 MatchingSkills matchingSkills;
2346 matchingSkills.AddEvent(eventName);
2347 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2348 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER);
2349 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2350
2351 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2352 mtx_.lock();
2353 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2354 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData), true);
2355
2356 struct tm doingTime = {0};
2357 int64_t seconds = 0;
2358 while (!mtx_.try_lock()) {
2359 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2360 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2361 if (seconds >= TIME_OUT_SECONDS_TWO) {
2362 break;
2363 }
2364 }
2365 mtx_.unlock();
2366 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2367 }
2368
2369 /*
2370 * @tc.number: CES_SetEventAuthority_1200
2371 * @tc.name: SetThreadMode
2372 * @tc.desc: Set ThreadMode for common event subscribers and verify successfully receive to common events
2373 */
2374 HWTEST_F(cesSystemTest, CES_SetEventAuthority_1200, Function | MediumTest | Level1)
2375 {
2376 std::string eventName = "TESTEVENT_PUBLISH_ACTION_SETHANDLER_HANDLER_STCES";
2377
2378 Want wantTest;
2379 wantTest.SetAction(eventName);
2380 CommonEventData commonEventData(wantTest);
2381
2382 MatchingSkills matchingSkills;
2383 matchingSkills.AddEvent(eventName);
2384 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2385 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::POST);
2386 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2387
2388 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2389 mtx_.lock();
2390 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData), true);
2391
2392 struct tm startTime = {0};
2393 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2394 struct tm doingTime = {0};
2395 int64_t seconds = 0;
2396 while (!mtx_.try_lock()) {
2397 // get current time and compare it with the start time
2398 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2399 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2400 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
2401 break;
2402 }
2403 }
2404 // expect the subscriber could receive the event within 5 seconds.
2405 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
2406 mtx_.unlock();
2407 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2408 }
2409
2410 /*
2411 * @tc.number: CES_VerifyMatchingSkills_0100
2412 * @tc.name: GetEvent
2413 * @tc.desc: check to get the added event
2414 */
2415 HWTEST_F(cesSystemTest, CES_VerifyMatchingSkills_0100, Function | MediumTest | Level1)
2416 {
2417 std::string eventName = "TESTEVENT_GETMATCHINGSKILLS";
2418
2419 MatchingSkills matchingSkills;
2420 matchingSkills.AddEvent(eventName);
2421 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2422 MatchingSkills testMatching = subscribeInfo.GetMatchingSkills();
2423
2424 EXPECT_EQ(testMatching.GetEvent(0), eventName);
2425 }
2426
2427 /*
2428 * @tc.number: CES_VerifyMatchingSkills_0200
2429 * @tc.name: GetEntity
2430 * @tc.desc: check to get the added entity
2431 */
2432 HWTEST_F(cesSystemTest, CES_VerifyMatchingSkills_0200, Function | MediumTest | Level1)
2433 {
2434 std::string eventName = "TESTEVENT_ADDENTITY_GETENTITY";
2435 std::string entity = "entity";
2436
2437 MatchingSkills matchingSkills;
2438 matchingSkills.AddEvent(eventName);
2439 matchingSkills.AddEntity(entity);
2440
2441 EXPECT_EQ(matchingSkills.GetEntity(0), entity);
2442 }
2443
2444 /*
2445 * @tc.number: CES_VerifyMatchingSkills_0300
2446 * @tc.name: HasEntity
2447 * @tc.desc: verify that entity is in MatchingSkills
2448 */
2449 HWTEST_F(cesSystemTest, CES_VerifyMatchingSkills_0300, Function | MediumTest | Level1)
2450 {
2451 std::string eventName = "TESTEVENT_ADDENTITY_HASENTITY";
2452 std::string entity = "entity";
2453
2454 MatchingSkills matchingSkills;
2455 matchingSkills.AddEvent(eventName);
2456 matchingSkills.AddEntity(entity);
2457
2458 EXPECT_TRUE(matchingSkills.HasEntity(entity));
2459 }
2460
2461 /*
2462 * @tc.number: CES_VerifyMatchingSkills_0400
2463 * @tc.name: RemoveEntity
2464 * @tc.desc: verify that the entity was successfully removed
2465 */
2466 HWTEST_F(cesSystemTest, CES_VerifyMatchingSkills_0400, Function | MediumTest | Level1)
2467 {
2468 std::string eventName = "TESTEVENT_ADDENTITY_REMOVEENTITY";
2469 std::string entity = "entity";
2470
2471 MatchingSkills matchingSkills;
2472 matchingSkills.AddEvent(eventName);
2473 matchingSkills.AddEntity(entity);
2474 matchingSkills.RemoveEntity(entity);
2475
2476 EXPECT_FALSE(matchingSkills.HasEntity(entity));
2477 }
2478
2479 /*
2480 * @tc.number: CES_VerifyMatchingSkills_0500
2481 * @tc.name: CountEntities
2482 * @tc.desc: verify that count correct number of entities
2483 */
2484 HWTEST_F(cesSystemTest, CES_VerifyMatchingSkills_0500, Function | MediumTest | Level1)
2485 {
2486 std::string eventName = "TESTEVENT_ADDENTITY_ENTITYCOUNT";
2487 std::string entity = "entity";
2488
2489 MatchingSkills matchingSkills;
2490 matchingSkills.AddEvent(eventName);
2491 matchingSkills.AddEntity(entity);
2492
2493 EXPECT_TRUE((matchingSkills.CountEntities() >= 1));
2494 }
2495
2496 /*
2497 * @tc.number: CES_SubscriberMaxSize_0200
2498 * @tc.name: SubscribeCommonEvent
2499 * @tc.desc: Verify that the maximum number of subscriptions for a single third-party application is 200
2500 */
2501 HWTEST_F(cesSystemTest, CES_SubscriberMaxSize_0100, Function | MediumTest | Level1)
2502 {
2503 std::string eventName = "TESTEVENT_SUBSCRIBER";
2504
2505 MatchingSkills matchingSkills;
2506 matchingSkills.AddEvent(eventName);
2507 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2508 std::vector<std::shared_ptr<CommonEventServicesSystemTest>> subscriberPtrs;
2509
2510 for (size_t i = 0; i < SUBSCRIBER_MAX_SIZE; i++) {
2511 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2512 subscriberPtrs.push_back(subscriberPtr);
2513 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtrs.at(i)), true);
2514 }
2515 for (size_t i = 0; i < SUBSCRIBER_MAX_SIZE; i++) {
2516 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtrs.at(i)), true);
2517 }
2518 }
2519
2520 /*
2521 * @tc.number: CES_SubscriberMaxSize_0200
2522 * @tc.name: SubscribeCommonEvent
2523 * @tc.desc: Verify that the maximum number of subscriptions for a single third-party application is 200 and the 201st
2524 * subscription failed
2525 */
2526 HWTEST_F(cesSystemTest, CES_SubscriberMaxSize_0200, Function | MediumTest | Level1)
2527 {
2528 std::string eventName = "TESTEVENT_SUBSCRIBER_PLUS";
2529
2530 MatchingSkills matchingSkills;
2531 matchingSkills.AddEvent(eventName);
2532 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2533 std::vector<std::shared_ptr<CommonEventServicesSystemTest>> subscriberPtrs;
2534
2535 for (size_t i = 0; i < SUBSCRIBER_MAX_SIZE_PLUS - 1; i++) {
2536 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2537 subscriberPtrs.push_back(subscriberPtr);
2538 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtrs.at(i)), true);
2539 }
2540
2541 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2542 EXPECT_FALSE(CommonEventManager::SubscribeCommonEvent(subscriberPtr));
2543
2544 for (size_t i = 0; i < SUBSCRIBER_MAX_SIZE_PLUS - 1; i++) {
2545 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtrs.at(i)), true);
2546 }
2547 }
2548
2549 /*
2550 * @tc.number: CES_PublishOrderTimeOut_0100
2551 * @tc.name: OnReceiveEvent
2552 * @tc.desc: Verify allowing a receiver to run for ten seconds before giving up on it
2553 */
2554 HWTEST_F(cesSystemTest, CES_PublishOrderTimeOut_0100, Function | MediumTest | Level1)
2555 {
2556 struct tm startTime = {0};
2557 std::string eventNameTimeOut = "TESTEVENT_PUBLISHORDER_TIMEOUT";
2558
2559 Want wantTimeOut;
2560 wantTimeOut.SetAction(eventNameTimeOut);
2561 CommonEventData commonEventDataTimeOut(wantTimeOut);
2562
2563 CommonEventPublishInfo publishInfoTimeOut;
2564 publishInfoTimeOut.SetOrdered(true);
2565
2566 MatchingSkills matchingSkills;
2567 matchingSkills.AddEvent(eventNameTimeOut);
2568
2569 CommonEventSubscribeInfo subscribeInfoOne(matchingSkills);
2570 subscribeInfoOne.SetPriority(PRIORITY_HIGH);
2571 auto subscriberTimeOnePtr = std::make_shared<CESPublishOrderTimeOutOne>(subscribeInfoOne);
2572 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberTimeOnePtr));
2573
2574 CommonEventSubscribeInfo subscribeInfoTwo(matchingSkills);
2575 subscribeInfoTwo.SetPriority(PRIORITY_LOW);
2576 auto subscriberTimeTwoPtr = std::make_shared<CESPublishOrderTimeOutTwo>(subscribeInfoTwo);
2577 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberTimeTwoPtr));
2578
2579 mtx_.lock();
2580 EXPECT_TRUE(CommonEventManager::PublishCommonEvent(commonEventDataTimeOut, publishInfoTimeOut));
2581
2582 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2583 struct tm doingTime = {0};
2584 int64_t seconds = 0;
2585 while (!mtx_.try_lock()) {
2586 // get current time and compare it with the start time
2587 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2588 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2589 if (seconds >= TIME_OUT_SECONDS_TWENTY) {
2590 break;
2591 }
2592 }
2593 // expect the subscriber could receive the event within 11 seconds.
2594 std::cout << "seconds: " << seconds << std::endl;
2595 EXPECT_GE(seconds, TIME_OUT_SECONDS_TEN);
2596
2597 mtx_.unlock();
2598 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberTimeOnePtr), true);
2599 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberTimeTwoPtr), true);
2600 }
2601
2602 /*
2603 * @tc.number: CES_PublishOrderTimeOut_0200
2604 * @tc.name: OnReceiveEvent
2605 * @tc.desc: Set the last subscriber and Verify allowing a receiver to run for ten seconds before giving up on it
2606 */
2607 HWTEST_F(cesSystemTest, CES_PublishOrderTimeOut_0200, Function | MediumTest | Level1)
2608 {
2609 struct tm startTime = {0};
2610 std::string eventNameTimeOut = "TESTEVENT_PUBLISHORDER_TIMEOUT_SUBSCRIBER";
2611
2612 MatchingSkills matchingSkills;
2613 matchingSkills.AddEvent(eventNameTimeOut);
2614
2615 Want wantTimeOut;
2616 wantTimeOut.SetAction(eventNameTimeOut);
2617 CommonEventData commonEventDataTimeOut(wantTimeOut);
2618
2619 CommonEventPublishInfo publishInfoTimeOut;
2620 publishInfoTimeOut.SetOrdered(true);
2621
2622 CommonEventSubscribeInfo subscribeInfoOne(matchingSkills);
2623 subscribeInfoOne.SetPriority(PRIORITY_HIGH);
2624 auto subscriberTimeOnePtr = std::make_shared<CESPublishOrderTimeOutOne>(subscribeInfoOne);
2625 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberTimeOnePtr));
2626
2627 CommonEventSubscribeInfo subscribeInfoTwo(matchingSkills);
2628 subscribeInfoTwo.SetPriority(PRIORITY_LOW);
2629 auto subscriberTimeTwoPtr = std::make_shared<CESPublishOrderTimeOutTwo>(subscribeInfoTwo);
2630 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberTimeTwoPtr));
2631
2632 mtx_.lock();
2633 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2634 EXPECT_TRUE(
2635 CommonEventManager::PublishCommonEvent(commonEventDataTimeOut, publishInfoTimeOut, subscriberTimeTwoPtr));
2636 struct tm doingTime = {0};
2637 int64_t seconds = 0;
2638 while (!mtx_.try_lock()) {
2639 // get current time and compare it with the start time
2640 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2641 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2642 if (seconds >= TIME_OUT_SECONDS_TWENTY) {
2643 break;
2644 }
2645 }
2646 // expect the subscriber could receive the event within 11 seconds.
2647 std::cout << "seconds: " << seconds << std::endl;
2648 EXPECT_GE(seconds, TIME_OUT_SECONDS_TEN);
2649 mtx_.unlock();
2650 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberTimeOnePtr), true);
2651 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberTimeTwoPtr), true);
2652 }
2653
2654 /*
2655 * @tc.number: CES_PublishOrderTimeOut_0300
2656 * @tc.name: OnReceiveEvent
2657 * @tc.desc: Without setting priority for subscribers and Verify allowing a receiver to run for ten seconds before
2658 * giving up on it
2659 */
2660 HWTEST_F(cesSystemTest, CES_PublishOrderTimeOut_0300, Function | MediumTest | Level1)
2661 {
2662 struct tm startTime = {0};
2663 std::string eventNameTimeOut = "TESTEVENT_PUBLISHORDER_TIMEOUT_NOPRIORITY";
2664
2665 MatchingSkills matchingSkills;
2666 matchingSkills.AddEvent(eventNameTimeOut);
2667
2668 Want wantTimeOut;
2669 wantTimeOut.SetAction(eventNameTimeOut);
2670 CommonEventData commonEventDataTimeOut(wantTimeOut);
2671
2672 CommonEventPublishInfo publishInfoTimeOut;
2673 publishInfoTimeOut.SetOrdered(true);
2674
2675 CommonEventSubscribeInfo subscribeInfoOne(matchingSkills);
2676 auto subscriberTimeOnePtr = std::make_shared<CESPublishOrderTimeOutOne>(subscribeInfoOne);
2677 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberTimeOnePtr));
2678
2679 CommonEventSubscribeInfo subscribeInfoTwo(matchingSkills);
2680 auto subscriberTimeTwoPtr = std::make_shared<CESPublishOrderTimeOutTwo>(subscribeInfoTwo);
2681 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberTimeTwoPtr));
2682
2683 mtx_.lock();
2684 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2685 EXPECT_TRUE(CommonEventManager::PublishCommonEvent(commonEventDataTimeOut, publishInfoTimeOut));
2686 struct tm doingTime = {0};
2687 int64_t seconds = 0;
2688 while (!mtx_.try_lock()) {
2689 // get current time and compare it with the start time
2690 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2691 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2692 if (seconds >= TIME_OUT_SECONDS_TWENTY) {
2693 break;
2694 }
2695 }
2696 // expect the subscriber could receive the event within 11 seconds.
2697 std::cout << "seconds: " << seconds << std::endl;
2698 EXPECT_GE(seconds, TIME_OUT_SECONDS_TEN);
2699 mtx_.unlock();
2700 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberTimeOnePtr), true);
2701 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberTimeTwoPtr), true);
2702 }
2703
2704 /*
2705 * @tc.number: CES_PublishOrderTimeOut_0400
2706 * @tc.name: OnReceiveEvent
2707 * @tc.desc: Set the last subscriber and Without setting priority for subscribers, Verify allowing a receiver to run
2708 * for ten seconds before giving up on it
2709 */
2710 HWTEST_F(cesSystemTest, CES_PublishOrderTimeOut_0400, Function | MediumTest | Level1)
2711 {
2712 struct tm startTime = {0};
2713 std::string eventNameTimeOut = "TESTEVENT_PUBLISHORDER_TIMEOUT_SUBSCRIBER_NOPRIORITY";
2714
2715 MatchingSkills matchingSkills;
2716 matchingSkills.AddEvent(eventNameTimeOut);
2717
2718 Want wantTimeOut;
2719 wantTimeOut.SetAction(eventNameTimeOut);
2720 CommonEventData commonEventDataTimeOut(wantTimeOut);
2721
2722 CommonEventPublishInfo publishInfoTimeOut;
2723 publishInfoTimeOut.SetOrdered(true);
2724
2725 CommonEventSubscribeInfo subscribeInfoOne(matchingSkills);
2726 auto subscriberTimeOnePtr = std::make_shared<CESPublishOrderTimeOutOne>(subscribeInfoOne);
2727 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberTimeOnePtr));
2728
2729 CommonEventSubscribeInfo subscribeInfoTwo(matchingSkills);
2730 auto subscriberTimeTwoPtr = std::make_shared<CESPublishOrderTimeOutTwo>(subscribeInfoTwo);
2731 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberTimeTwoPtr));
2732
2733 mtx_.lock();
2734 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2735 EXPECT_TRUE(
2736 CommonEventManager::PublishCommonEvent(commonEventDataTimeOut, publishInfoTimeOut, subscriberTimeTwoPtr));
2737 struct tm doingTime = {0};
2738 int64_t seconds = 0;
2739 while (!mtx_.try_lock()) {
2740 // get current time and compare it with the start time
2741 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2742 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2743 if (seconds >= TIME_OUT_SECONDS_TWENTY) {
2744 break;
2745 }
2746 }
2747 // expect the subscriber could receive the event within 11 seconds.
2748 std::cout << "seconds: " << seconds << std::endl;
2749 EXPECT_GE(seconds, TIME_OUT_SECONDS_TEN);
2750
2751 mtx_.unlock();
2752 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberTimeOnePtr), true);
2753 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberTimeTwoPtr), true);
2754 }
2755
2756 /*
2757 * @tc.number: CES_PermissionAndOrDefault_0100
2758 * @tc.name: OnReceiveEvent
2759 * @tc.desc: Verify system applications handle system public events with permissions
2760 */
2761 HWTEST_F(cesSystemTest, CES_PermissionAndOrDefault_0100, Function | MediumTest | Level1)
2762 {
2763 std::string eventAndN = "COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_DISCOVERED";
2764 std::string eventDefaultN = "COMMON_EVENT_USER_SWITCHED";
2765
2766 Want wantAnd;
2767 wantAnd.SetAction(eventAndN);
2768 CommonEventData commonEventDataAnd(wantAnd);
2769 CommonEventPublishInfo publishInfoAnd;
2770 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventDataAnd, publishInfoAnd), true);
2771
2772 Want wantDefaultN;
2773 wantDefaultN.SetAction(eventDefaultN);
2774 CommonEventData commonEventDataDefaultN(wantDefaultN);
2775 CommonEventPublishInfo publishInfoDefaultN;
2776 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventDataDefaultN, publishInfoDefaultN), true);
2777 }
2778
2779 /*
2780 * @tc.number: CES_RemoveStickyCommonEvent_0100
2781 * @tc.name: RemoveStickyCommonEvent
2782 * @tc.desc: Verify remove sticky common event
2783 */
2784 HWTEST_F(cesSystemTest, CES_RemoveStickyCommonEvent_0100, Function | MediumTest | Level1)
2785 {
2786 bool result = true;
2787 std::string eventName = "TESTEVENT_REMOVE_STICKY_COMMON_EVENT";
2788
2789 Want wantTest;
2790 wantTest.SetAction(eventName);
2791 CommonEventData commonEventData(wantTest);
2792
2793 MatchingSkills matchingSkills;
2794 matchingSkills.AddEvent(eventName);
2795 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2796 auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
2797
2798 CommonEventPublishInfo publishInfo;
2799 publishInfo.SetSticky(true);
2800 EXPECT_EQ(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo), true);
2801
2802 std::this_thread::sleep_for(std::chrono::seconds(TIME_OUT_SECONDS_));
2803 EXPECT_EQ(CommonEventManager::RemoveStickyCommonEvent(eventName), OHOS::ERR_OK);
2804 EXPECT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true);
2805 mtx_.lock();
2806
2807 struct tm startTime = {0};
2808 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
2809 struct tm doingTime = {0};
2810 int64_t seconds = 0;
2811 while (!mtx_.try_lock()) {
2812 // get current time and compare it with the start time
2813 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
2814 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
2815 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
2816 result = false;
2817 break;
2818 }
2819 }
2820 // expect the subscriber can't receive the event within 5 seconds.
2821 EXPECT_FALSE(result);
2822 mtx_.unlock();
2823 EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
2824 }
2825 } // namespace EventFwk
2826 } // namespace OHOS