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 <cstdlib>
19 #include <vector>
20 
21 #include "event_handler.h"
22 #include "event_queue.h"
23 #include "event_queue_base.h"
24 #include "event_runner.h"
25 #include "inner_event.h"
26 #define private public
27 #include "native_implement_eventhandler.h"
28 #undef private
29 
30 using namespace testing::ext;
31 using namespace OHOS::AppExecFwk;
32 namespace {
33 const size_t MAX_POOL_SIZE = 64;
34 }
35 
36 typedef void (*FileFDCallback)(int32_t filedescriptor);
37 
38 struct FileDescriptorCallbacks {
39     FileFDCallback readableCallback_;
40     FileFDCallback writableCallback_;
41     FileFDCallback shutdownCallback_;
42     FileFDCallback exceptionCallback_;
43 };
44 
45 /**
46  * test for task information.
47  */
TestTaskInfo()48 static void TestTaskInfo()
49 {
50     string taskName("taskName");
51     bool callbackCalled = false;
52     auto f = [&callbackCalled]() { callbackCalled = true; };
53     auto event = InnerEvent::Get(f, taskName);
54     auto getName = event->GetTaskName();
55     EXPECT_EQ(taskName, getName);
56     // execute callback function, check whether the callback function is the one we set
57     (event->GetTaskCallback())();
58     // drop event, execute destructor function
59     EXPECT_TRUE(callbackCalled);
60 }
61 
62 /**
63  * Deleter of event shared pointer.
64  */
Deleter(uint32_t * object)65 inline static void Deleter(uint32_t *object)
66 {
67     if (object == nullptr) {
68         return;
69     }
70     delete object;
71     object = nullptr;
72 }
73 
74 class LibEventHandlerEventTest : public testing::Test {
75 public:
76     static void SetUpTestCase(void);
77     static void TearDownTestCase(void);
78     void SetUp();
79     void TearDown();
80 };
81 
SetUpTestCase(void)82 void LibEventHandlerEventTest::SetUpTestCase(void)
83 {}
84 
TearDownTestCase(void)85 void LibEventHandlerEventTest::TearDownTestCase(void)
86 {}
87 
SetUp(void)88 void LibEventHandlerEventTest::SetUp(void)
89 {}
90 
TearDown(void)91 void LibEventHandlerEventTest::TearDown(void)
92 {}
93 
94 /*
95  * @tc.name: GetEvent001
96  * @tc.desc: get event from pool, set id and param
97  * @tc.type: FUNC
98  */
99 HWTEST_F(LibEventHandlerEventTest, GetEvent001, TestSize.Level1)
100 {
101     /**
102      * @tc.steps: step1. get event with event id and param, then get event id and param from event.
103      * @tc.expected: step1. the event id and param is the same as we set.
104      */
105     uint32_t eventId = 0;
106     int64_t eventParam = 0;
107     auto event = InnerEvent::Get(eventId, eventParam);
108     auto id = event->GetInnerEventId();
109     auto param = event->GetParam();
110     EXPECT_EQ(eventId, id);
111     EXPECT_EQ(eventParam, param);
112 }
113 
114 /*
115  * @tc.name: GetEvent002
116  * @tc.desc: get event from pool , set id param and shared pointer object
117  * @tc.type: FUNC
118  */
119 HWTEST_F(LibEventHandlerEventTest, GetEvent002, TestSize.Level1)
120 {
121     /**
122      * @tc.steps: step1. get event with event id, object, and param, then get event id, object and param from event.
123      * @tc.expected: step1. the event id, object and param is the same as we set.
124      */
125     uint32_t eventId = 0;
126     int64_t eventParam = 0;
127     auto object = std::make_shared<uint32_t>();
128     auto event = InnerEvent::Get(eventId, object, eventParam);
129     auto id = event->GetInnerEventId();
130     auto param = event->GetParam();
131     auto sharedObject = event->GetSharedObject<uint32_t>();
132     EXPECT_EQ(eventId, id);
133     EXPECT_EQ(eventParam, param);
134     EXPECT_EQ(object, sharedObject);
135 }
136 
137 /*
138  * @tc.name: GetEvent003
139  * @tc.desc: get event from pool , set id param and weak pointer object
140  * @tc.type: FUNC
141  */
142 HWTEST_F(LibEventHandlerEventTest, GetEvent003, TestSize.Level1)
143 {
144     /**
145      * @tc.steps: step1. get event with event id, weak pointer object, and param, then get event id,
146      *            shared object and param from event.
147      * @tc.expected: step1. the event id, object and param is the same as we set.
148      */
149     uint32_t eventId = 0;
150     int64_t eventParam = 0;
151     auto object = std::make_shared<uint32_t>();
152     std::weak_ptr<uint32_t> weakObject = object;
153     auto event = InnerEvent::Get(eventId, weakObject, eventParam);
154     auto id = event->GetInnerEventId();
155     auto param = event->GetParam();
156     auto sharedObject = event->GetSharedObject<uint32_t>();
157     auto weakToSharedObject = weakObject.lock();
158     EXPECT_EQ(eventId, id);
159     EXPECT_EQ(eventParam, param);
160     EXPECT_EQ(weakToSharedObject, sharedObject);
161 }
162 
163 /*
164  * @tc.name: GetEvent004
165  * @tc.desc: get event from pool , set id param and rvalue unique_ptr object
166  * @tc.type: FUNC
167  */
168 HWTEST_F(LibEventHandlerEventTest, GetEvent004, TestSize.Level1)
169 {
170     /**
171      * @tc.steps: step1. get event with event id, rvalue unique_ptr object, and param. then get event id,
172      *            unique object and param from event.
173      * @tc.expected: step1. the event id, object and param is the same as we set.
174      */
175     uint32_t eventId = 0;
176     int64_t eventParam = 0;
177     uint32_t number = 1;
178     std::unique_ptr<uint32_t> object = std::make_unique<uint32_t>(number);
179     auto event = InnerEvent::Get(eventId, object, eventParam);
180     auto id = event->GetInnerEventId();
181     auto param = event->GetParam();
182     auto uniqueNumber = *(event->GetUniqueObject<uint32_t>());
183     EXPECT_EQ(eventId, id);
184     EXPECT_EQ(eventParam, param);
185     EXPECT_EQ(number, uniqueNumber);
186 }
187 
188 /*
189  * @tc.name: GetEvent005
190  * @tc.desc: get event from pool , set id param and lvalue unique_ptr object
191  * @tc.type: FUNC
192  */
193 HWTEST_F(LibEventHandlerEventTest, GetEvent005, TestSize.Level1)
194 {
195     /**
196      * @tc.steps: step1. get event with event id, lvalue unique_ptr object, and param. then get event id,
197      *            unique object and param from event.
198      * @tc.expected: step1. the event id, object and param is the same as we set.
199      */
200     uint32_t eventId = 0;
201     int64_t eventParam = 0;
202     uint32_t number = 1;
203     std::unique_ptr<uint32_t> object = std::make_unique<uint32_t>(number);
204     auto event = InnerEvent::Get(eventId, object, eventParam);
205     auto id = event->GetInnerEventId();
206     auto param = event->GetParam();
207     auto uniqueNumber = *(event->GetUniqueObject<uint32_t>());
208     EXPECT_EQ(eventId, id);
209     EXPECT_EQ(eventParam, param);
210     EXPECT_EQ(number, uniqueNumber);
211 }
212 
213 /*
214  * @tc.name: GetEvent006
215  * @tc.desc: get event from pool set task name and callback
216  * @tc.type: FUNC
217  */
218 HWTEST_F(LibEventHandlerEventTest, GetEvent006, TestSize.Level1)
219 {
220     /**
221      * @tc.steps: step1. get event with task and taskname, then get taskname from task and execute task,
222      *            check whether the taskname and executed task is the same as we set.
223      * @tc.expected: step1. the taskname and executed task is the same as we set.
224      */
225     TestTaskInfo();
226 }
227 
228 /*
229  * @tc.name: GetEvent007
230  * @tc.desc: Get Unique pointer of saved unique_ptr<T, D> object
231  * @tc.type: FUNC
232  */
233 HWTEST_F(LibEventHandlerEventTest, GetEvent007, TestSize.Level1)
234 {
235     /**
236      * @tc.steps: step1. get event with event id, param, and unique_ptr<T, D> type object. then get event id,
237      *            unique_ptr<T, D> type unique object and param from event.
238      * @tc.expected: step1. the event id, object and param is the same as we set.
239      */
240     using deleter = void (*)(uint32_t *);
241     uint32_t eventId = 0;
242     int64_t eventParam = 0;
243     uint32_t number = 1;
244     std::unique_ptr<uint32_t, deleter> object(new uint32_t(number), Deleter);
245     auto event = InnerEvent::Get(eventId, object, eventParam);
246     auto id = event->GetInnerEventId();
247     auto param = event->GetParam();
248     auto uniqueNumber = *(event->GetUniqueObject<uint32_t, deleter>());
249     EXPECT_EQ(eventId, id);
250     EXPECT_EQ(eventParam, param);
251     EXPECT_EQ(number, uniqueNumber);
252 }
253 
254 /*
255  * @tc.name: GetEventInfo001
256  * @tc.desc: set event owner and get event owner then compare
257  * @tc.type: FUNC
258  */
259 HWTEST_F(LibEventHandlerEventTest, GetEventInfo001, TestSize.Level1)
260 {
261     /**
262      * @tc.steps: step1. init handler, get event with event id. then set the handler as the event owner
263      *            and get event owner.
264      * @tc.expected: step1. the event owner is the same as we set.
265      */
266     uint32_t eventId = 0;
267     auto handler = std::make_shared<EventHandler>();
268     auto event = InnerEvent::Get(eventId);
269     event->SetOwner(handler);
270     auto owner = event->GetOwner();
271     EXPECT_EQ(handler, owner);
272 }
273 
274 /*
275  * @tc.name: GetEventInfo002
276  * @tc.desc: set event sendTime and get event sendTime then compare
277  * @tc.type: FUNC
278  */
279 HWTEST_F(LibEventHandlerEventTest, GetEventInfo002, TestSize.Level1)
280 {
281     /**
282      * @tc.steps: step1. get event with event id. then set the send time of the event, get send time of the event.
283      * @tc.expected: step1. the event send time is the same as we set.
284      */
285     uint32_t eventId = 0;
286     auto event = InnerEvent::Get(eventId);
287     InnerEvent::TimePoint now = InnerEvent::Clock::now();
288     event->SetSendTime(now);
289     auto sendTime = event->GetSendTime();
290     EXPECT_EQ(now, sendTime);
291 }
292 
293 /*
294  * @tc.name: GetEventInfo003
295  * @tc.desc: set event handleTime and get event handleTime then compare
296  * @tc.type: FUNC
297  */
298 HWTEST_F(LibEventHandlerEventTest, GetEventInfo003, TestSize.Level1)
299 {
300     /**
301      * @tc.steps: step1. get event with event id. then set the handle time of the event, get handle time of the event.
302      * @tc.expected: step1. the event handle time is the same as we set.
303      */
304     uint32_t eventId = 0;
305     auto event = InnerEvent::Get(eventId);
306     InnerEvent::TimePoint now = InnerEvent::Clock::now();
307     event->SetHandleTime(now);
308     auto handleTime = event->GetHandleTime();
309     EXPECT_EQ(now, handleTime);
310 }
311 
312 /*
313  * @tc.name: GetEventInfo004
314  * @tc.desc: set event param and get event param then compare
315  * @tc.type: FUNC
316  */
317 HWTEST_F(LibEventHandlerEventTest, GetEventInfo004, TestSize.Level1)
318 {
319     /**
320      * @tc.steps: step1. get event with event id and param. then get event param of the event.
321      * @tc.expected: step1. the event param is the same as we set.
322      */
323     uint32_t eventId = 0;
324     int64_t eventParam = 0;
325     auto event = InnerEvent::Get(eventId, eventParam);
326     auto param = event->GetParam();
327     EXPECT_EQ(eventParam, param);
328 }
329 
330 /*
331  * @tc.name: GetEventInfo005
332  * @tc.desc: set event callback taskName and and get event callback taskName then compare
333  * @tc.type: FUNC
334  */
335 HWTEST_F(LibEventHandlerEventTest, GetEventInfo005, TestSize.Level1)
336 {
337     /**
338      * @tc.steps: step1. get event with task and taskname, then get taskname from task and execute task,
339      *            check whether the taskname and executed task is the same as we set.
340      * @tc.expected: step1. the taskname and executed task is the same as we set.
341      */
342     TestTaskInfo();
343 }
344 
345 /*
346  * @tc.name: GetEventInfo006
347  * @tc.desc: judge whether the event has a task if it takes a task return true
348  * @tc.type: FUNC
349  */
350 HWTEST_F(LibEventHandlerEventTest, GetEventInfo006, TestSize.Level1)
351 {
352     /**
353      * @tc.steps: step1. get event with task and taskname, then use HasTask() to check if the event has a task.
354      * @tc.expected: step1. the event we get from event pool has a task.
355      */
356     string taskName("taskName");
__anone653a2330302() 357     auto f = []() {};
358     auto event = InnerEvent::Get(f, taskName);
359     auto whetherHasTask = event->HasTask();
360     EXPECT_TRUE(whetherHasTask);
361 }
362 
363 /*
364  * @tc.name: DrainPool001
365  * @tc.desc: get event from pool when the pool is full, then get event from new memory area
366  * @tc.type: FUNC
367  */
368 HWTEST_F(LibEventHandlerEventTest, DrainPool001, TestSize.Level1)
369 {
370     /**
371      * @tc.steps: step1. Drain the event pool, make sure event pool is empty
372      */
373     std::vector<InnerEvent::Pointer> drainPool;
374     int64_t param = 0;
375     uint32_t eventId = 1;
376     for (size_t i = 0; i < MAX_POOL_SIZE; ++i) {
377         drainPool.push_back(InnerEvent::Get(eventId));
378         ++eventId;
379     }
380 
381     ++eventId;
382     auto event = InnerEvent::Get(eventId, param);
383 
384     /**
385      * @tc.steps: step2. clear all the event we get from pool, make sure the pool is full.
386      */
387     drainPool.clear();
388 
389     /**
390      * @tc.steps: step3. get the event address of the event we get after the event pool is empty,
391      *            then reset all the event we get from pool before and get event, compare the two events address.
392      * @tc.expected: step3. the two event address are not the same.
393      */
394     auto firstAddr = event.get();
395     event.reset(nullptr);
396 
__anone653a2330402() 397     auto f = []() {};
398     event = InnerEvent::Get(f);
399     auto secondAddr = event.get();
400     event.reset(nullptr);
401 
402     EXPECT_NE(firstAddr, secondAddr);
403 }
404 
405 /*
406  * @tc.name: DrainPool002
407  * @tc.desc: get event from pool when the pool is not full, then get event from new memory area
408  * @tc.type: FUNC
409  */
410 HWTEST_F(LibEventHandlerEventTest, DrainPool002, TestSize.Level1)
411 {
412     /**
413      * @tc.steps: step1. Drain the event pool, make sure event pool is empty
414      */
415     std::vector<InnerEvent::Pointer> drainPool;
416     auto param = 0;
417     uint32_t eventId = 1;
418     for (size_t i = 0; i < MAX_POOL_SIZE; ++i) {
419         drainPool.push_back(InnerEvent::Get(eventId));
420         ++eventId;
421     }
422 
423     /**
424      * @tc.steps: step2. get event from pool and get the address of the event.
425      */
426     ++eventId;
427     auto event = InnerEvent::Get(eventId, param);
428     auto firstAddr = event.get();
429     event.reset(nullptr);
430 
431     /**
432      * @tc.steps: step3. get another event  from event pool, get the address of the event.
433      */
__anone653a2330502() 434     auto f = []() {};
435     event = InnerEvent::Get(f);
436     auto secondAddr = event.get();
437     event.reset(nullptr);
438 
439     /**
440      * @tc.steps: step4. reset the event pool
441      */
442     for (size_t i = 0; i < MAX_POOL_SIZE; ++i) {
443         drainPool.back().reset(nullptr);
444     }
445 
446     /**
447      * @tc.steps: step5. compare the two event addresses.
448      * @tc.expected: step3. the two event addresses are the same.
449      */
450     EXPECT_NE(firstAddr, secondAddr);
451 }
452 
453 /*
454  * @tc.name: Dump001
455  * @tc.desc: Invoke Dump and GetCurrentEventQueue interface verify whether it is normal
456  * @tc.type: FUNC
457  */
458 HWTEST_F(LibEventHandlerEventTest, Dump001, TestSize.Level1)
459 {
460     uint32_t eventId = 0;
461     int64_t eventParam = 0;
462     std::string runnerInfo = "aa";
463     auto runner = EventRunner::Create(true);
464     runner->DumpRunnerInfo(runnerInfo);
465     EventQueueBase queue;
466     queue.DumpQueueInfo(runnerInfo);
467     auto event = InnerEvent::Get(eventId, eventParam);
468     std::string result = event->Dump();
469     EXPECT_EQ("Event { No handler }\n", result);
470     EXPECT_EQ(runner->GetCurrentEventQueue(), nullptr);
471 }
472 
473 /*
474  * @tc.name: EventRunnerNativeImplement001
475  * @tc.desc: Invoke GetEventRunnerNativeObj and CreateEventRunnerNativeObj interface verify whether it is normal
476  * @tc.type: FUNC
477  */
478 HWTEST_F(LibEventHandlerEventTest, EventRunnerNativeImplement001, TestSize.Level1)
479 {
480     EventRunnerNativeImplement eventRunnerNativeImplement(true);
481     EXPECT_NE(eventRunnerNativeImplement.GetEventRunnerNativeObj(), nullptr);
482     EXPECT_NE(eventRunnerNativeImplement.CreateEventRunnerNativeObj(), nullptr);
483 }
484 
485 /*
486  * @tc.name: EventRunnerNativeImplement002
487  * @tc.desc: Invoke RunEventRunnerNativeObj and StopEventRunnerNativeObj interface verify whether it is normal
488  * @tc.type: FUNC
489  */
490 HWTEST_F(LibEventHandlerEventTest, EventRunnerNativeImplement002, TestSize.Level1)
491 {
492     EventRunnerNativeImplement eventRunnerNativeImplement(true);
493     ErrCode runEventRunnerNativeObj = eventRunnerNativeImplement.RunEventRunnerNativeObj();
494     EXPECT_EQ(OHOS::AppExecFwk::EVENT_HANDLER_ERR_NO_EVENT_RUNNER, runEventRunnerNativeObj);
495     ErrCode stopEventRunnerNativeObj = eventRunnerNativeImplement.StopEventRunnerNativeObj();
496     EXPECT_EQ(OHOS::AppExecFwk::EVENT_HANDLER_ERR_NO_EVENT_RUNNER, stopEventRunnerNativeObj);
497 }
498 
499 /*
500  * @tc.name: EventRunnerNativeImplement003
501  * @tc.desc: InvokeRemoveFileDescriptorListener interface verify whether it is normal
502  * @tc.type: FUNC
503  */
504 HWTEST_F(LibEventHandlerEventTest, EventRunnerNativeImplement003, TestSize.Level1)
505 {
506     std::shared_ptr<EventRunnerNativeImplement> eventRunnerNativeImplement =
507         std::make_shared<EventRunnerNativeImplement>(true);
508     EXPECT_NE(eventRunnerNativeImplement, nullptr);
509     eventRunnerNativeImplement->eventRunner_ = EventRunner::Create(false);
510     int32_t fileDescriptor = 1;
511     eventRunnerNativeImplement->RemoveFileDescriptorListener(fileDescriptor);
512 }
513 
514 /*
515  * @tc.name: EventRunnerNativeImplement004
516  * @tc.desc: AddFileDescriptorListener interface verify whether it is normal
517  * @tc.type: FUNC
518  */
519 HWTEST_F(LibEventHandlerEventTest, EventRunnerNativeImplement004, TestSize.Level1)
520 {
521     std::shared_ptr<EventRunnerNativeImplement> eventRunnerNativeImplement =
522         std::make_shared<EventRunnerNativeImplement>(true);
523     EXPECT_NE(eventRunnerNativeImplement, nullptr);
524     eventRunnerNativeImplement->eventRunner_ = EventRunner::Create(false);
525     int32_t fileDescriptor = 1;
526     uint32_t events = 2;
527     struct FileDescriptorCallbacks P1;
528     FileDescriptorCallbacks *fdCallbacks = &P1;
529     eventRunnerNativeImplement->AddFileDescriptorListener(fileDescriptor, events, fdCallbacks);
530 }
531