1 /*
2  * Copyright (c) 2023 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 #include "event_base_test.h"
16 
17 #include <chrono>
18 #include <map>
19 
20 #include "event_loop.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
GetCurrentMillis()25 inline uint64_t GetCurrentMillis()
26 {
27     auto now = std::chrono::system_clock::now();
28     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
29     return millisecs.count();
30 }
31 }
SetUpTestCase()32 void EventBaseTest::SetUpTestCase()
33 {
34 }
35 
TearDownTestCase()36 void EventBaseTest::TearDownTestCase()
37 {
38 }
39 
SetUp()40 void EventBaseTest::SetUp()
41 {
42 }
43 
TearDown()44 void EventBaseTest::TearDown()
45 {
46 }
47 
OnEvent(std::shared_ptr<OHOS::HiviewDFX::Event> & event)48 bool TestEventHandler::OnEvent(std::shared_ptr<OHOS::HiviewDFX::Event>& event)
49 {
50     if (event == nullptr) {
51         return false;
52     }
53     return true;
54 }
55 
OnUnorderedEvent(const Event & event)56 void TestEventListener::OnUnorderedEvent(const Event& event)
57 {
58     ASSERT_TRUE(event.messageType_ == Event::MessageType::NONE);
59 }
60 
GetListenerName()61 std::string TestEventListener::GetListenerName()
62 {
63     return "TestEventListener";
64 }
65 
66 /**
67  * @tc.name: EventLoopTest001
68  * @tc.desc: Test the api of EventLoop.
69  * @tc.type: FUNC
70  * @tc.require: issueI7PF2N
71  */
72 HWTEST_F(EventBaseTest, EventLoopTest001, testing::ext::TestSize.Level0)
73 {
74     auto now = GetCurrentMillis();
75     LoopEvent event1 = LoopEvent::CreateLoopEvent(now);
76     LoopEvent event2 = LoopEvent::CreateLoopEvent(now + 200); // 200ms later
77     ASSERT_TRUE(event2 < event1);
78 }
79 
80 /**
81  * @tc.name: EventHanderTest001
82  * @tc.desc: Test the api of EventHander.
83  * @tc.type: FUNC
84  * @tc.require: issueI7PF2N
85  */
86 HWTEST_F(EventBaseTest, EventHanderTest001, testing::ext::TestSize.Level0)
87 {
88     TestEventHandler handler;
89     EventHandler& handlerRef = handler;
90     std::shared_ptr<Event> event = std::make_shared<Event>("");
91     ASSERT_TRUE(handlerRef.CanProcessEvent(event));
92     ASSERT_TRUE(handlerRef.IsInterestedPipelineEvent(event));
93     ASSERT_TRUE(handlerRef.CanProcessMoreEvents());
94     ASSERT_TRUE(handlerRef.GetHandlerInfo() == "");
95 }
96 
97 /**
98  * @tc.name: EventTest001
99  * @tc.desc: Test the api of Event.
100  * @tc.type: FUNC
101  * @tc.require: issueI7PF2N
102  */
103 HWTEST_F(EventBaseTest, EventTest001, testing::ext::TestSize.Level0)
104 {
105     Event event("");
106     ASSERT_TRUE(event.OnContinue());
107     ASSERT_TRUE(!event.hasFinish_);
108     ASSERT_TRUE(event.OnFinish());
109     ASSERT_TRUE(event.hasFinish_);
110     event.OnRepack();
111     event.OnPending();
112     ASSERT_TRUE(event.GetPendingProcessorSize() == 0); // 0 is default size.
113     std::map<std::string, std::string> kvSet{{"key1", "value1"}};
114     event.SetKeyValuePairs(kvSet);
115     std::map<std::string, std::string> kvGet = event.GetKeyValuePairs();
116     ASSERT_TRUE(kvGet.size() == 1 && kvGet["key1"] == "value1");
117     int32_t i32v = 188; // a test value;
118     event.SetValue("", i32v);
119     event.SetValue("I32_KEY", i32v);
120     int32_t ret = event.GetIntValue("UNKNOWN_KEY");
121     ASSERT_EQ(ret, -1); // unknown value
122     ret = event.GetIntValue("I32_KEY");
123     ASSERT_EQ(ret, i32v);
124 }
125 
126 /**
127  * @tc.name: EventListenerTest001
128  * @tc.desc: Test the api of EventListener.
129  * @tc.type: FUNC
130  * @tc.require: issueI7PF2N
131  */
132 HWTEST_F(EventBaseTest, EventListenerTest001, testing::ext::TestSize.Level0)
133 {
134     TestEventListener listener;
135     EventListener& listenerRef = listener;
136     Event event("");
137     std::map<std::string, DomainRule> domainRulesMap;
138     listenerRef.AddListenerInfo(0, domainRulesMap); // 0 is random type.
139     ASSERT_TRUE(!listenerRef.OnOrderedEvent(event));
140     ASSERT_TRUE(listenerRef.GetListenerName() == "TestEventListener");
141 }
142 } // namespace HiviewDFX
143 } // namespace OHOS
144