• 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 "event_handler_test_common.h"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 using namespace OHOS::AppExecFwk;
22 
23 class EventHandlerSetGetRemoveModuleTest : public testing::Test {
24 public:
25     static void SetUpTestCase(void);
26     static void TearDownTestCase(void);
27     void SetUp();
28     void TearDown();
29 };
30 
SetUpTestCase(void)31 void EventHandlerSetGetRemoveModuleTest::SetUpTestCase(void)
32 {}
33 
TearDownTestCase(void)34 void EventHandlerSetGetRemoveModuleTest::TearDownTestCase(void)
35 {}
36 
SetUp(void)37 void EventHandlerSetGetRemoveModuleTest::SetUp(void)
38 {
39     /**
40      * @tc.setup: Set the value of flags to the default.
41      */
42     CommonUtils::EventRunSet(false);
43     CommonUtils::TaskCalledSet(false);
44 }
45 
TearDown(void)46 void EventHandlerSetGetRemoveModuleTest::TearDown(void)
47 {}
48 
49 /**
50  * @tc.name: SetRunner001
51  * @tc.desc: Set the EventRunner for the EventHandler
52  * @tc.type: FUNC
53  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
54  */
55 HWTEST_F(EventHandlerSetGetRemoveModuleTest, SetRunner001, TestSize.Level1)
56 {
57     /**
58      * @tc.steps: step1. Set the event runner for current handler.
59      * @tc.expected: step1. Set successfully and the event can be send.
60      */
61     auto myRunner = EventRunner::Create(false);
62     auto handler = std::make_shared<MyEventHandler>(nullptr);
63     handler->SetEventRunner(myRunner);
64     bool result = handler->SendEvent(STOP_EVENT_ID);
65     myRunner->Run();
66     EXPECT_TRUE(result);
67 }
68 
69 /**
70  * @tc.name: SetRunner002
71  * @tc.desc: Set a null EventRunner for the EventHandler
72  * @tc.type: FUNC
73  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
74  */
75 HWTEST_F(EventHandlerSetGetRemoveModuleTest, SetRunner002, TestSize.Level1)
76 {
77     /**
78      * @tc.steps: step1. Set a null event runner for current handler.
79      * @tc.expected: step1. Set failed and the event can not be send.
80      */
81     auto myRunner = EventRunner::Create(false);
82     auto handler = std::make_shared<MyEventHandler>(myRunner);
83     handler->SetEventRunner(nullptr);
84     bool allowSend = handler->SendEvent(Random());
85     EXPECT_FALSE(allowSend);
86 }
87 
88 /**
89  * @tc.name: GetRunner001
90  * @tc.desc: Get the EventRunner of the EventHandler
91  * @tc.type: FUNC
92  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
93  */
94 HWTEST_F(EventHandlerSetGetRemoveModuleTest, GetRunner001, TestSize.Level1)
95 {
96     /**
97      * @tc.steps: step1. Get the event runner of current handler.
98      * @tc.expected: step1. The got runner has the same addr as created runner.
99      */
100     auto myRunner = EventRunner::Create(false);
101     auto handler = std::make_shared<MyEventHandler>(myRunner);
102     auto currentRunner = handler->GetEventRunner();
103     EXPECT_EQ(myRunner, currentRunner);
104 }
105 
106 /**
107  * @tc.name: GetRunner002
108  * @tc.desc: Get a null EventRunner of the EventHandler
109  * @tc.type: FUNC
110  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
111  */
112 HWTEST_F(EventHandlerSetGetRemoveModuleTest, GetRunner002, TestSize.Level1)
113 {
114     /**
115      * @tc.steps: step1. Get the event runner of handler which not loaded the runner.
116      * @tc.expected: step1. Return a nullptr.
117      */
118     auto handler = std::make_shared<MyEventHandler>(nullptr);
119     auto currentRunner = handler->GetEventRunner();
120     EXPECT_EQ(nullptr, currentRunner);
121 }
122 
123 /**
124  * @tc.name: DistributeEvent001
125  * @tc.desc: Distribute an event
126  * @tc.type: FUNC
127  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
128  */
129 HWTEST_F(EventHandlerSetGetRemoveModuleTest, DistributeEvent001, TestSize.Level1)
130 {
131     /**
132      * @tc.steps: step1. Create and distribute the event.
133      * @tc.expected: step1. Distribute successfully and the event handled.
134      */
135     auto event = InnerEvent::Get(RUN_EVENT_ID);
136     auto myRunner = EventRunner::Create(false);
137     auto handler = std::make_shared<MyEventHandler>(myRunner);
138 
139     handler->DistributeEvent(event);
140     bool result = CommonUtils::EventRunGet();
141     EXPECT_TRUE(result);
142 }
143 
144 /**
145  * @tc.name: DistributeEvent002
146  * @tc.desc: Distribute a task
147  * @tc.type: FUNC
148  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
149  */
150 HWTEST_F(EventHandlerSetGetRemoveModuleTest, DistributeEvent002, TestSize.Level1)
151 {
152     /**
153      * @tc.steps: step1. Create and distribute the task.
154      * @tc.expected: step1. Distribute successfully and the task executed.
155      */
__anon92f399180102() 156     auto f = []() { CommonUtils::TaskCalledSet(true); };
157     auto event = InnerEvent::Get(f);
158     auto myRunner = EventRunner::Create(false);
159     auto handler = std::make_shared<MyEventHandler>(myRunner);
160 
161     handler->DistributeEvent(event);
162     bool result = CommonUtils::TaskCalledGet();
163     EXPECT_TRUE(result);
164 }
165 
166 /**
167  * @tc.name: DistributeEvent003
168  * @tc.desc: Distribute a null event
169  * @tc.type: FUNC
170  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
171  */
172 HWTEST_F(EventHandlerSetGetRemoveModuleTest, DistributeEvent003, TestSize.Level1)
173 {
174     /**
175      * @tc.steps: step1. Distribute a null event.
176      * @tc.expected: step1. Distribute failed and the event not handled.
177      */
178     auto myRunner = EventRunner::Create(false);
179     auto handler = std::make_shared<MyEventHandler>(myRunner);
180     auto nullPtr = InnerEvent::Pointer(nullptr, nullptr);
181 
182     handler->DistributeEvent(nullPtr);
183     bool result = CommonUtils::EventRunGet();
184     EXPECT_FALSE(result);
185 }
186 
187 /**
188  * @tc.name: CurrentHandler001
189  * @tc.desc: Get current handler when handler running
190  * @tc.type: FUNC
191  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
192  */
193 HWTEST_F(EventHandlerSetGetRemoveModuleTest, CurrentHandler001, TestSize.Level1)
194 {
195     /**
196      * @tc.steps: step1. Get current handler when handler running.
197      * @tc.expected: step1. Get successfully and the same as created handler.
198      */
199     auto myRunner = EventRunner::Create();
200     auto handler = std::make_shared<MyEventHandler>(myRunner);
201     std::shared_ptr<EventHandler> myHandler;
__anon92f399180202() 202     auto f = [&myHandler, &handler]() { myHandler = handler->Current(); };
203 
204     handler->PostTask(f);
205     uint32_t sleepTime = 20000;
206     usleep(sleepTime);
207     EXPECT_EQ(handler, myHandler);
208 }
209 
210 /**
211  * @tc.name: CurrentHandler002
212  * @tc.desc: Get current handler when handler not running
213  * @tc.type: FUNC
214  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
215  */
216 HWTEST_F(EventHandlerSetGetRemoveModuleTest, CurrentHandler002, TestSize.Level1)
217 {
218     /**
219      * @tc.steps: step1. Get current handler when handler not running.
220      * @tc.expected: step1. Return a nullptr.
221      */
222     auto myRunner = EventRunner::Create();
223     auto handler = std::make_shared<MyEventHandler>(myRunner);
224     std::shared_ptr<EventHandler> myHandler = handler->Current();
225 
226     EXPECT_EQ(nullptr, myHandler);
227 }
228 
229 /**
230  * @tc.name: CurrentHandler003
231  * @tc.desc: Get current handler when running in different handler
232  * @tc.type: FUNC
233  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
234  */
235 HWTEST_F(EventHandlerSetGetRemoveModuleTest, CurrentHandler003, TestSize.Level1)
236 {
237     /**
238      * @tc.steps: step1. Get current handler when handler running in different handler.
239      * @tc.expected: step1. Get successfully and the same as created handler.
240      */
241     auto myRunner = EventRunner::Create(false);
242     auto handler = std::make_shared<MyEventHandler>(myRunner);
243     std::shared_ptr<EventHandler> myHandler;
__anon92f399180302() 244     auto f = [&handler, &myHandler]() {
245         auto inRunner = EventRunner::Create(false);
246         auto inHandler = std::make_shared<MyEventHandler>(inRunner);
247         auto fIn = [&inHandler]() { EXPECT_EQ(inHandler, inHandler->Current()); };
248         inHandler->PostTask(fIn);
249         inHandler->SendEvent(STOP_EVENT_ID);
250         inRunner->Run();
251         myHandler = handler->Current();
252     };
253 
254     handler->PostTask(f);
255     handler->SendEvent(STOP_EVENT_ID);
256     myRunner->Run();
257 }
258 
259 /**
260  * @tc.name: Remove001
261  * @tc.desc: Remove all events
262  * @tc.type: FUNC
263  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
264  */
265 HWTEST_F(EventHandlerSetGetRemoveModuleTest, Remove001, TestSize.Level1)
266 {
267     /**
268      * @tc.steps: step1. Send multi events to event queue.
269      */
270     int64_t delayTime = 0;
271     uint32_t taskCalledCount = 0;
272     uint32_t expectResult = 0;
__anon92f399180502() 273     auto f = [&taskCalledCount]() { taskCalledCount++; };
274     auto myRunner = EventRunner::Create(false);
275     auto handler = std::make_shared<MyEventHandler>(myRunner);
276 
277     for (uint32_t i = 0; i < 2; ++i) {
278         handler->PostTask(f, delayTime, EventQueue::Priority::HIGH);
279         delayTime++;
280     }
281 
282     /**
283      * @tc.steps: step2. Remove all events, and run the runner.
284      * @tc.expected: step2. Remove successfully and no event run.
285      */
286     handler->RemoveAllEvents();
287     int64_t param = 0;
288     int64_t offsetTime = 3;
289     handler->SendEvent(STOP_EVENT_ID, param, delayTime + offsetTime);
290     myRunner->Run();
291     EXPECT_EQ(expectResult, taskCalledCount);
292 }
293 
294 /**
295  * @tc.name: Remove002
296  * @tc.desc: Remove all events without runner
297  * @tc.type: FUNC
298  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
299  */
300 HWTEST_F(EventHandlerSetGetRemoveModuleTest, Remove002, TestSize.Level1)
301 {
302     /**
303      * @tc.steps: step1. Remove all events without runner.
304      * @tc.expected: step1. Remove failed.
305      */
306     auto handler = std::make_shared<MyEventHandler>(nullptr);
307     handler->RemoveAllEvents();
308     bool result = handler->SendEvent(RUN_EVENT_ID);
309     EXPECT_FALSE(result);
310 }
311 
312 /**
313  * @tc.name: Remove003
314  * @tc.desc: Remove an event by eventId
315  * @tc.type: FUNC
316  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
317  */
318 HWTEST_F(EventHandlerSetGetRemoveModuleTest, Remove003, TestSize.Level1)
319 {
320     /**
321      * @tc.steps: step1. Send event to event queue.
322      */
323     auto myRunner = EventRunner::Create(false);
324     auto handler = std::make_shared<MyEventHandler>(myRunner);
325     handler->SendEvent(RUN_EVENT_ID);
326 
327     /**
328      * @tc.steps: step2. Remove the event by eventId.
329      * @tc.expected: step2. Remove successfully and removed event not run.
330      */
331     handler->RemoveEvent(RUN_EVENT_ID);
332     handler->SendEvent(STOP_EVENT_ID);
333     myRunner->Run();
334     bool result = CommonUtils::EventRunGet();
335     EXPECT_FALSE(result);
336 }
337 
338 /**
339  * @tc.name: Remove004
340  * @tc.desc: Remove an event by eventId without runner
341  * @tc.type: FUNC
342  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
343  */
344 HWTEST_F(EventHandlerSetGetRemoveModuleTest, Remove004, TestSize.Level1)
345 {
346     /**
347      * @tc.steps: step1. Remove an event by eventId without runner.
348      * @tc.expected: step1. Remove failed.
349      */
350     auto handler = std::make_shared<MyEventHandler>(nullptr);
351     handler->RemoveEvent(Random());
352     bool result = handler->SendEvent(RUN_EVENT_ID);
353     EXPECT_FALSE(result);
354 }
355 
356 /**
357  * @tc.name: Remove005
358  * @tc.desc: Remove an event by eventId and param
359  * @tc.type: FUNC
360  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
361  */
362 HWTEST_F(EventHandlerSetGetRemoveModuleTest, Remove005, TestSize.Level1)
363 {
364     /**
365      * @tc.steps: step1. Send event to event queue.
366      */
367     int64_t param = 0;
368     auto myRunner = EventRunner::Create(false);
369     auto handler = std::make_shared<MyEventHandler>(myRunner);
370     handler->SendEvent(RUN_EVENT_ID, param, 0);
371 
372     /**
373      * @tc.steps: step2. Remove the event by eventId and param.
374      * @tc.expected: step2. Remove successfully and removed event not run.
375      */
376     handler->RemoveEvent(RUN_EVENT_ID, param);
377     handler->SendEvent(STOP_EVENT_ID);
378     myRunner->Run();
379     bool result = CommonUtils::EventRunGet();
380     EXPECT_FALSE(result);
381 }
382 
383 /**
384  * @tc.name: Remove006
385  * @tc.desc: Remove an event by eventId and param without runner
386  * @tc.type: FUNC
387  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
388  */
389 HWTEST_F(EventHandlerSetGetRemoveModuleTest, Remove006, TestSize.Level1)
390 {
391     /**
392      * @tc.steps: step1. Remove an event by eventId and param without runner.
393      * @tc.expected: step1. Remove failed.
394      */
395     int64_t param = 0;
396     auto handler = std::make_shared<MyEventHandler>(nullptr);
397     handler->RemoveEvent(Random(), param);
398     bool result = handler->SendEvent(RUN_EVENT_ID);
399     EXPECT_FALSE(result);
400 }
401 
402 /**
403  * @tc.name: Remove007
404  * @tc.desc: Remove a task by name
405  * @tc.type: FUNC
406  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
407  */
408 HWTEST_F(EventHandlerSetGetRemoveModuleTest, Remove007, TestSize.Level1)
409 {
410     /**
411      * @tc.steps: step1. Post a task to event queue.
412      */
413     string taskName = std::to_string(Random());
__anon92f399180602() 414     auto f = []() { CommonUtils::TaskCalledSet(true); };
415     auto myRunner = EventRunner::Create(false);
416     auto handler = std::make_shared<MyEventHandler>(myRunner);
417     handler->PostTask(f, taskName);
418 
419     /**
420      * @tc.steps: step2. Remove the task by name.
421      * @tc.expected: step2. Remove successfully and removed task not run.
422      */
423     handler->RemoveTask(taskName);
424     handler->SendEvent(STOP_EVENT_ID);
425     myRunner->Run();
426     bool result = CommonUtils::TaskCalledGet();
427     EXPECT_FALSE(result);
428 }
429 
430 /**
431  * @tc.name: Remove008
432  * @tc.desc: Remove a task by name without runner
433  * @tc.type: FUNC
434  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
435  */
436 HWTEST_F(EventHandlerSetGetRemoveModuleTest, Remove008, TestSize.Level1)
437 {
438     /**
439      * @tc.steps: step1. Remove a task by name without runner.
440      * @tc.expected: step1. Remove failed.
441      */
442     string taskName = std::to_string(Random());
443     auto handler = std::make_shared<MyEventHandler>(nullptr);
444     handler->RemoveTask(taskName);
445     bool result = handler->SendEvent(RUN_EVENT_ID);
446     EXPECT_FALSE(result);
447 }
448