• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
17 #include <string>
18 #include <thread>
19 
20 #include "common_event_manager.h"
21 #include "ces_inner_error_code.h"
22 #include "common_event_subscriber.h"
23 #include "datetime_ex.h"
24 #include "event_log_wrapper.h"
25 #include "inner_event.h"
26 
27 #include <gtest/gtest.h>
28 
29 using namespace testing::ext;
30 using namespace OHOS::AppExecFwk;
31 
32 namespace OHOS {
33 namespace EventFwk {
34 
35 class CommonEventServicesSystemTest : public CommonEventSubscriber {
36 public:
37     explicit CommonEventServicesSystemTest(const CommonEventSubscribeInfo &subscribeInfo);
~CommonEventServicesSystemTest()38     virtual ~CommonEventServicesSystemTest() {};
39     virtual void OnReceiveEvent(const CommonEventData &data);
40 };
41 
CommonEventServicesSystemTest(const CommonEventSubscribeInfo & subscribeInfo)42 CommonEventServicesSystemTest::CommonEventServicesSystemTest(const CommonEventSubscribeInfo &subscribeInfo)
43     : CommonEventSubscriber(subscribeInfo)
44 {}
45 
GetNowSysTime()46 int64_t GetNowSysTime()
47 {
48     InnerEvent::TimePoint nowSys = InnerEvent::Clock::now();
49     auto epoch = nowSys.time_since_epoch();
50     auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
51     int64_t duration = value.count();
52 
53     return duration;
54 }
55 
OnReceiveEvent(const CommonEventData & data)56 void CommonEventServicesSystemTest::OnReceiveEvent(const CommonEventData &data)
57 {
58     GTEST_LOG_(INFO) << " ActsCesSpecificationTest:CommonEventServicesSystemTest:OnReceiveEvent \n";
59 }
60 
61 class ActsCesSpecificationTest : public testing::Test {
62 public:
63     static void SetUpTestCase();
64     static void TearDownTestCase();
65     void SetUp();
66     void TearDown();
67 };
SetUpTestCase()68 void ActsCesSpecificationTest::SetUpTestCase()
69 {}
70 
TearDownTestCase()71 void ActsCesSpecificationTest::TearDownTestCase()
72 {}
73 
SetUp()74 void ActsCesSpecificationTest::SetUp()
75 {}
76 
TearDown()77 void ActsCesSpecificationTest::TearDown()
78 {}
79 
80 /*
81  * @tc.number: SubscribeCommonEventExceedLimit_0100
82  * @tc.name: verify subscribe exceed limit
83  * @tc.desc: Failed to call SubscribeCommonEvent API due to the count of subscriber exceed limit
84  */
85 HWTEST_F(ActsCesSpecificationTest, SubscribeCommonEventExceedLimit_0100, TestSize.Level0)
86 {
87     MatchingSkills matchingSkills;
88     matchingSkills.AddEvent("test");
89     for (int i = 0; i < 200; i++) {
90         CommonEventSubscribeInfo subscribeInfo(matchingSkills);
91         auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
92         EXPECT_EQ(CommonEventManager::NewSubscribeCommonEvent(subscriberPtr), ERR_OK);
93     }
94     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
95     auto subscriberPtr = std::make_shared<CommonEventServicesSystemTest>(subscribeInfo);
96     EXPECT_EQ(CommonEventManager::NewSubscribeCommonEvent(subscriberPtr),
97         Notification::ERR_NOTIFICATION_CES_SUBSCRIBE_EXCEED_LIMIT);
98 }
99 
100 /*
101  * @tc.number: PublishCommonEventExceedLimit_0100
102  * @tc.name: verify publish exceed limit
103  * @tc.desc: Failed to call publish common event API due to publish sequency is too high
104  */
105 HWTEST_F(ActsCesSpecificationTest, PublishCommonEventExceedLimit_0100, TestSize.Level0)
106 {
107     Want wantTest;
108     wantTest.SetAction("eventAction");
109     CommonEventData commonEventData(wantTest);
110     CommonEventPublishInfo info;
111     int64_t start = GetNowSysTime();
112     for (int i = 0; i < 20; i++) {
113         EXPECT_EQ(CommonEventManager::NewPublishCommonEvent(commonEventData, info), ERR_OK);
114     }
115     int32_t result = CommonEventManager::NewPublishCommonEvent(commonEventData, info);
116     int64_t now = GetNowSysTime();
117     if (now - start <= 5) {
118         EXPECT_EQ(result, Notification::ERR_NOTIFICATION_CES_EVENT_FREQ_TOO_HIGH);
119     } else {
120         EXPECT_EQ(result, ERR_OK);
121     }
122 }
123 }  // namespace EventFwk
124 }  // namespace OHOS