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 "event_handler_test_common.h"
17 #include "event_handler_utils.h"
18
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 using namespace OHOS::AppExecFwk;
23
24 namespace {
25 const uint32_t MAX_RETRY_COUNT = 2000;
26 const uint32_t SLEEP_TIME = 1000;
27
GetNowSysTime()28 int64_t GetNowSysTime()
29 {
30 InnerEvent::TimePoint nowSys = InnerEvent::Clock::now();
31 auto epoch = nowSys.time_since_epoch();
32 auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
33 int64_t duration = value.count();
34 return duration;
35 }
36
37 /**
38 * Function: Waiting for task run, the most waiting time is 1s.
39 * @param f Task callback.
40 * @param handler Event handler.
41 */
42 template<typename F>
WaitTaskCalled(const F & f,const std::shared_ptr<EventHandler> & handler)43 void WaitTaskCalled(const F &f, const std::shared_ptr<EventHandler> &handler)
44 {
45 int64_t nowTime = GetNowSysTime();
46 int64_t delayTime = 1;
47 uint32_t count = 0;
48 if (handler->PostTimingTask(f, nowTime + delayTime)) {
49 while (true) {
50 if (CommonUtils::EventRunGet()) {
51 break;
52 }
53 if (CommonUtils::TaskCalledGet()) {
54 break;
55 }
56 // If delay more than 1s, break
57 if (count >= MAX_RETRY_COUNT) {
58 break;
59 }
60 usleep(SLEEP_TIME);
61 count++;
62 }
63 }
64 }
65
66 /**
67 * Function: Send event with different shared pointer, and then check the processed result.
68 * @param smartPointerType One of smart pointer.
69 */
SendEventWithSmartPtr(SmartPointerType smartPointerType)70 void SendEventWithSmartPtr(SmartPointerType smartPointerType)
71 {
72 auto myRunner = EventRunner::Create(false);
73 auto handler = std::make_shared<MyEventHandler>(myRunner);
74 auto sharedPtr = std::make_shared<int>(1);
75 auto weakPtr = std::weak_ptr<int>(sharedPtr);
76 auto f = [](int *intPtr) { delete intPtr; };
77 auto uniquePtr = std::unique_ptr<int, void (*)(int *)>((new int(1)), f);
78
79 int64_t nowTime = GetNowSysTime();
80 int64_t delayTime = 10;
81 switch (smartPointerType) {
82 case SmartPointerType::SHARED_PTR: {
83 handler->SendTimingEvent(RUN_EVENT_ID, sharedPtr, nowTime + delayTime);
84 break;
85 }
86 case SmartPointerType::WEAK_PTR: {
87 handler->SendTimingEvent(RUN_EVENT_ID, weakPtr, nowTime + delayTime);
88 break;
89 }
90 case SmartPointerType::LVALUE_REFERENCE_UNIQUE_PTR: {
91 handler->SendTimingEvent(RUN_EVENT_ID, uniquePtr, nowTime + delayTime);
92 break;
93 }
94 case SmartPointerType::RVALUE_REFERENCE_UNIQUE_PTR: {
95 handler->SendTimingEvent(
96 RUN_EVENT_ID, std::unique_ptr<int, void (*)(int *)>((new int(1)), f), nowTime + delayTime);
97 break;
98 }
99 default:
100 break;
101 }
102 handler->SendTimingEvent(STOP_EVENT_ID, delayTime + delayTime + nowTime);
103 myRunner->Run();
104
105 bool runResult = CommonUtils::EventRunGet();
106 EXPECT_TRUE(runResult);
107 }
108 } // unnamed namespace
109
110 class EventHandlerSendTimingEventModuleTest : public testing::Test {
111 public:
112 static void SetUpTestCase(void);
113 static void TearDownTestCase(void);
114 void SetUp();
115 void TearDown();
116 };
117
SetUpTestCase(void)118 void EventHandlerSendTimingEventModuleTest::SetUpTestCase(void)
119 {}
120
TearDownTestCase(void)121 void EventHandlerSendTimingEventModuleTest::TearDownTestCase(void)
122 {}
123
SetUp(void)124 void EventHandlerSendTimingEventModuleTest::SetUp(void)
125 {
126 /**
127 * @tc.setup: Set the value of flags to the default.
128 */
129 CommonUtils::EventRunSet(false);
130 CommonUtils::EventRunCountReset();
131 CommonUtils::TaskCalledSet(false);
132 }
133
TearDown(void)134 void EventHandlerSendTimingEventModuleTest::TearDown(void)
135 {}
136
137 /**
138 * @tc.name: SendTiming001
139 * @tc.desc: Send null event
140 * @tc.type: FUNC
141 * @tc.require: SR000DPU9L AR000DPVJS
142 */
143 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming001, TestSize.Level1)
144 {
145 /**
146 * @tc.steps: step1. Send a null event.
147 * @tc.expected: step1. Send failed.
148 */
149 int64_t nowTime = GetNowSysTime();
150 int64_t delayTime = 2;
151 auto myRunner = EventRunner::Create(false);
152 auto handler = std::make_shared<MyEventHandler>(myRunner);
153 auto nullPtr = InnerEvent::Pointer(nullptr, nullptr);
154 bool result = handler->SendTimingEvent(nullPtr, nowTime + delayTime, EventQueue::Priority::LOW);
155 EXPECT_FALSE(result);
156 }
157
158 /**
159 * @tc.name: SendTiming002
160 * @tc.desc: Send r-value event with event and event id
161 * @tc.type: FUNC
162 * @tc.require: SR000DPU9L AR000DPVJS
163 */
164 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming002, TestSize.Level1)
165 {
166 /**
167 * @tc.steps: step1. Send r-value event with priority which not IDLE in a task.
168 * @tc.expected: step1. Send successfully, the event will be run.
169 */
170 int64_t nowTime = GetNowSysTime();
171 int64_t delayTime = 10;
172 auto event = InnerEvent::Get(RUN_EVENT_ID);
173 auto myRunner = EventRunner::Create(false);
174 auto handler = std::make_shared<MyEventHandler>(myRunner);
175 handler->SendTimingEvent(event, delayTime + nowTime);
176 handler->SendTimingEvent(STOP_EVENT_ID, delayTime * 2 + nowTime);
177 myRunner->Run();
178
179 uint32_t runResult = 1;
180 EXPECT_EQ(runResult, CommonUtils::EventRunCount());
181 }
182
183 /**
184 * @tc.name: SendTiming003
185 * @tc.desc: Send r-value event with event, event id and priority
186 * @tc.type: FUNC
187 * @tc.require: SR000DPU9L AR000DPVJS
188 */
189 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming003, TestSize.Level1)
190 {
191 /**
192 * @tc.steps: step1. Send r-value event with priority which not IDLE in a task.
193 * @tc.expected: step1. Send successfully, the event will be run.
194 */
195 int64_t nowTime = GetNowSysTime();
196 int64_t delayTime = 10;
197 auto myRunner = EventRunner::Create(false);
198 auto handler = std::make_shared<MyEventHandler>(myRunner);
199 handler->SendTimingEvent(RUN_EVENT_ID, delayTime + nowTime, EventQueue::Priority::LOW);
200 handler->SendTimingEvent(STOP_EVENT_ID, delayTime * 2 + nowTime);
201 myRunner->Run();
202
203 uint32_t runResult = 1;
204 EXPECT_EQ(runResult, CommonUtils::EventRunCount());
205 }
206
207 /**
208 * @tc.name: SendTiming004
209 * @tc.desc: Send r-value event with event, event id and param
210 * @tc.type: FUNC
211 * @tc.require: SR000DPU9L AR000DPVJS
212 */
213 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming004, TestSize.Level1)
214 {
215 /**
216 * @tc.steps: step1. Send r-value event with priority which not IDLE in a task.
217 * @tc.expected: step1. Send successfully, the event will be run.
218 */
219 int64_t nowTime = GetNowSysTime();
220 int64_t delayTime = 10;
221 auto event = InnerEvent::Get(RUN_EVENT_ID);
222 auto myRunner = EventRunner::Create(false);
223 auto handler = std::make_shared<MyEventHandler>(myRunner);
224 handler->SendTimingEvent(RUN_EVENT_ID, delayTime + nowTime, 1);
225 handler->SendTimingEvent(STOP_EVENT_ID, delayTime * 2 + nowTime);
226 myRunner->Run();
227
228 uint32_t runResult = 1;
229 EXPECT_EQ(runResult, CommonUtils::EventRunCount());
230 }
231 /**
232 * @tc.name: SendTiming005
233 * @tc.desc: Send event with eventId, shared_ptr object and priority which not IDLE
234 * @tc.type: FUNC
235 * @tc.require: SR000DPU9L AR000DPVJS
236 */
237 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming005, TestSize.Level1)
238 {
239 /**
240 * @tc.steps: step1. Send event in a task with eventId, shared_ptr object and priority which not IDLE.
241 * @tc.expected: step1. Send successfully, the event will be run.
242 */
243 SendEventWithSmartPtr(SmartPointerType::SHARED_PTR);
244 bool runResult = CommonUtils::EventRunGet();
245 EXPECT_TRUE(runResult);
246 }
247
248 /**
249 * @tc.name: SendTiming006
250 * @tc.desc: Send event with eventId, weak_ptr object and priority which not IDLE
251 * @tc.type: FUNC
252 * @tc.require: SR000DPU9L AR000DPVJS
253 */
254 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming006, TestSize.Level1)
255 {
256 /**
257 * @tc.steps: step1. Send event in a task with eventId, weak_ptr object and priority which not IDLE.
258 * @tc.expected: step1. Send successfully, the event will be run.
259 */
260 SendEventWithSmartPtr(SmartPointerType::WEAK_PTR);
261 bool runResult = CommonUtils::EventRunGet();
262 EXPECT_TRUE(runResult);
263 }
264
265 /**
266 * @tc.name: SendTiming007
267 * @tc.desc: Send event with eventId, unique_ptr object and priority which not IDLE
268 * @tc.type: FUNC
269 * @tc.require: SR000DPU9L AR000DPVJS
270 */
271 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming007, TestSize.Level1)
272 {
273 /**
274 * @tc.steps: step1. Send event in a task with eventId, unique_ptr object and priority which not IDLE.
275 * @tc.expected: step1. Send successfully, the event will be run.
276 */
277 SendEventWithSmartPtr(SmartPointerType::LVALUE_REFERENCE_UNIQUE_PTR);
278 bool runResult = CommonUtils::EventRunGet();
279 EXPECT_TRUE(runResult);
280 }
281
282 /**
283 * @tc.name: SendTiming008
284 * @tc.desc: Send event with eventId, rvalue reference unique_ptr object and priority which not IDLE
285 * @tc.type: FUNC
286 * @tc.require: SR000DPU9L AR000DPVJS
287 */
288 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming008, TestSize.Level1)
289 {
290 /**
291 * @tc.steps: step1. Send event in a task with eventId,
292 * rvalue reference unique_ptr object and priority which not IDLE.
293 * @tc.expected: step1. Send successfully, the event will be run.
294 */
295 SendEventWithSmartPtr(SmartPointerType::RVALUE_REFERENCE_UNIQUE_PTR);
296 bool runResult = CommonUtils::EventRunGet();
297 EXPECT_TRUE(runResult);
298 }
299
300 /**
301 * @tc.name: SendTiming009
302 * @tc.desc: Send r-value event with event, event id and param at past time
303 * @tc.type: FUNC
304 * @tc.require: SR000DPU9L AR000DPVJS
305 */
306 HWTEST_F(EventHandlerSendTimingEventModuleTest, SendTiming009, TestSize.Level1)
307 {
308 /**
309 * @tc.steps: step1. Send r-value event with priority which not IDLE in a task.
310 * @tc.expected: step1. Send successfully, the event will be run.
311 */
312 int64_t nowTime = GetNowSysTime();
313 int64_t delayTime = 10;
314 auto event = InnerEvent::Get(RUN_EVENT_ID);
315 auto myRunner = EventRunner::Create(false);
316 auto handler = std::make_shared<MyEventHandler>(myRunner);
317 handler->SendTimingEvent(RUN_EVENT_ID, nowTime - delayTime, 1);
318 handler->SendTimingEvent(STOP_EVENT_ID, delayTime + nowTime);
319 myRunner->Run();
320
321 uint32_t runResult = 1;
322 EXPECT_EQ(runResult, CommonUtils::EventRunCount());
323 }
324