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