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 <gtest/gtest.h>
17 
18 #include "event_handler.h"
19 #include "event_queue.h"
20 #include "event_runner.h"
21 #include "inner_event.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AppExecFwk;
26 
27 namespace {
28 const uint32_t EVENT_ID = 0;
29 const std::string THREAD_NAME_TEST1 = "threadNameTest1";
30 const std::string THREAD_NAME_TEST2 = " ";
31 const std::string THREAD_NAME_TEST3 = "@#¥#3243adsafdf_中文";
32 const std::string THREAD_NAME_TEST4 = std::to_string(0);
33 const std::string THREAD_NAME_TEST5 = std::to_string(0) + "@#¥#3243adsafdf_中文";
34 }  // namespace
35 
36 /**
37  * test for task information.
38  */
TestTaskInfo1()39 static void TestTaskInfo1()
40 {
41     string taskName(THREAD_NAME_TEST1);
42     bool callbackCalled = false;
43     auto f = [&callbackCalled]() { callbackCalled = true; };
44     auto event = InnerEvent::Get(f, taskName);
45     auto getName = event->GetTaskName();
46     EXPECT_EQ(taskName, getName);
47     // execute callback function, check whether the callback function is the one we set
48     (event->GetTaskCallback())();
49     // drop event, execute destructor function
50     EXPECT_TRUE(callbackCalled);
51 }
52 
TestTaskInfo2()53 static void TestTaskInfo2()
54 {
55     string taskName(THREAD_NAME_TEST2);
56     bool callbackCalled = false;
57     auto f = [&callbackCalled]() { callbackCalled = true; };
58     auto event = InnerEvent::Get(f, taskName);
59     auto getName = event->GetTaskName();
60     EXPECT_EQ(taskName, getName);
61     // execute callback function, check whether the callback function is the one we set
62     (event->GetTaskCallback())();
63     // drop event, execute destructor function
64     EXPECT_TRUE(callbackCalled);
65 }
66 
TestTaskInfo3()67 static void TestTaskInfo3()
68 {
69     string taskName(THREAD_NAME_TEST3);
70     bool callbackCalled = false;
71     auto f = [&callbackCalled]() { callbackCalled = true; };
72     auto event = InnerEvent::Get(f, taskName);
73     auto getName = event->GetTaskName();
74     EXPECT_EQ(taskName, getName);
75     // execute callback function, check whether the callback function is the one we set
76     (event->GetTaskCallback())();
77     // drop event, execute destructor function
78     EXPECT_TRUE(callbackCalled);
79 }
80 
TestTaskInfo4()81 static void TestTaskInfo4()
82 {
83     string taskName(THREAD_NAME_TEST4);
84     bool callbackCalled = false;
85     auto f = [&callbackCalled]() { callbackCalled = true; };
86     auto event = InnerEvent::Get(f, taskName);
87     auto getName = event->GetTaskName();
88     EXPECT_EQ(taskName, getName);
89     // execute callback function, check whether the callback function is the one we set
90     (event->GetTaskCallback())();
91     // drop event, execute destructor function
92     EXPECT_TRUE(callbackCalled);
93 }
94 
TestTaskInfo5()95 static void TestTaskInfo5()
96 {
97     string taskName(THREAD_NAME_TEST5);
98     bool callbackCalled = false;
99     auto f = [&callbackCalled]() { callbackCalled = true; };
100     auto event = InnerEvent::Get(f, taskName);
101     auto getName = event->GetTaskName();
102     EXPECT_EQ(taskName, getName);
103     // execute callback function, check whether the callback function is the one we set
104     (event->GetTaskCallback())();
105     // drop event, execute destructor function
106     EXPECT_TRUE(callbackCalled);
107 }
108 
109 class EmsInnerEventSystemTest : public testing::Test {
110 public:
111     static void SetUpTestCase(void);
112     static void TearDownTestCase(void);
113     void SetUp();
114     void TearDown();
115 };
116 
SetUpTestCase(void)117 void EmsInnerEventSystemTest::SetUpTestCase(void)
118 {}
119 
TearDownTestCase(void)120 void EmsInnerEventSystemTest::TearDownTestCase(void)
121 {}
122 
SetUp(void)123 void EmsInnerEventSystemTest::SetUp(void)
124 {}
125 
TearDown(void)126 void EmsInnerEventSystemTest::TearDown(void)
127 {}
128 
129 /*
130  * @tc.name: GetHandleTime001
131  * @tc.desc: set event handleTime and get event handleTime then compare,
132  *           check whether the callback function is the one we set
133  * @tc.type: FUNC
134  */
135 HWTEST_F(EmsInnerEventSystemTest, GetHandleTime001, TestSize.Level1)
136 {
137     auto event = InnerEvent::Get(EVENT_ID);
138     InnerEvent::TimePoint now = InnerEvent::Clock::now();
139     event->SetHandleTime(now);
140     auto handleTime = event->GetHandleTime();
141     EXPECT_EQ(now, handleTime);
142 }
143 
144 /*
145  * @tc.name: GetHandleTime002
146  * @tc.desc: set event handleTime and get event handleTime then compare
147  * @tc.type: FUNC
148  */
149 HWTEST_F(EmsInnerEventSystemTest, GetHandleTime002, TestSize.Level1)
150 {
151     auto event = InnerEvent::Get(EVENT_ID);
152     InnerEvent::TimePoint now = InnerEvent::TimePoint::max();
153     event->SetHandleTime(now);
154     auto handleTime = event->GetHandleTime();
155     EXPECT_EQ(now, handleTime);
156 }
157 
158 /*
159  * @tc.name: GetHandleTime003
160  * @tc.desc: set event handleTime and get event handleTime then compare,
161  *           check whether the callback function is the one we set
162  * @tc.type: FUNC
163  */
164 HWTEST_F(EmsInnerEventSystemTest, GetHandleTime003, TestSize.Level1)
165 {
166     auto event = InnerEvent::Get(EVENT_ID);
167     InnerEvent::TimePoint now = InnerEvent::Clock::time_point();
168     event->SetHandleTime(now);
169     auto handleTime = event->GetHandleTime();
170     EXPECT_EQ(now, handleTime);
171 }
172 
173 /*
174  * @tc.name: GetHandleTime001
175  * @tc.desc: set event handleTime and get event handleTime then compare
176  * @tc.type: FUNC
177  */
178 HWTEST_F(EmsInnerEventSystemTest, GetHandleTime004, TestSize.Level1)
179 {
180     auto event = InnerEvent::Get(EVENT_ID);
181     InnerEvent::TimePoint now = InnerEvent::TimePoint::min();
182     event->SetHandleTime(now);
183     auto handleTime = event->GetHandleTime();
184     EXPECT_EQ(now, handleTime);
185 }
186 
187 /*
188  * @tc.name: GetHandleTime005
189  * @tc.desc: set event handleTime and get event handleTime then compare
190  * @tc.type: FUNC
191  */
192 HWTEST_F(EmsInnerEventSystemTest, GetHandleTime005, TestSize.Level1)
193 {
194     auto event = InnerEvent::Get(EVENT_ID);
195     InnerEvent::TimePoint now;
196     event->SetHandleTime(now);
197     auto handleTime = event->GetHandleTime();
198     EXPECT_EQ(now, handleTime);
199 }
200 
201 /*
202  * @tc.name: GetTaskCallback
203  * @tc.desc: set event callback taskName1 get event callback taskName1 and then compare
204  * @tc.type: FUNC
205  */
206 HWTEST_F(EmsInnerEventSystemTest, GetTaskCallback001, TestSize.Level1)
207 {
208     TestTaskInfo1();
209 }
210 
211 /*
212  * @tc.name: GetEventInfo002
213  * @tc.desc: set event callback taskName2 and get event callback taskName2 then compare, check whether the callback
214  *           function is the one we set
215  * @tc.type: FUNC
216  */
217 HWTEST_F(EmsInnerEventSystemTest, GetTaskCallback002, TestSize.Level1)
218 {
219     TestTaskInfo2();
220 }
221 
222 /*
223  * @tc.name: GetEventInfo003
224  * @tc.desc: set event callback taskName3 and get event callback taskName3 then compare
225  * @tc.type: FUNC
226  */
227 HWTEST_F(EmsInnerEventSystemTest, GetTaskCallback003, TestSize.Level1)
228 {
229     TestTaskInfo3();
230 }
231 
232 /*
233  * @tc.name: GetEventInfo004
234  * @tc.desc: set event callback taskName4 and get event callback taskName4 then compare, check whether the callback
235  *           function is the one we set
236  * @tc.type: FUNC
237  */
238 HWTEST_F(EmsInnerEventSystemTest, GetTaskCallback004, TestSize.Level1)
239 {
240     TestTaskInfo4();
241 }
242 
243 /*
244  * @tc.name: GetEventInfo005
245  * @tc.desc: set event callback taskName5 and get event callback taskName5 then compare, check whether the callback
246  *           function is the one we set
247  * @tc.type: FUNC
248  */
249 HWTEST_F(EmsInnerEventSystemTest, GetTaskCallback005, TestSize.Level1)
250 {
251     TestTaskInfo5();
252 }