• 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 namespace {
24 const uint32_t MAX_RETRY_COUNT = 1000;
25 const uint32_t SLEEP_TIME = 1000;
26 
27 /**
28  * Function: Waiting for task run, the most waiting time is 1s.
29  * @param f Task callback.
30  * @param handler Event handler.
31  */
32 template<typename F>
WaitTaskCalled(const F & f,const std::shared_ptr<EventHandler> & handler)33 void WaitTaskCalled(const F &f, const std::shared_ptr<EventHandler> &handler)
34 {
35     uint32_t count = 0;
36     if (handler->PostTask(f)) {
37         while (true) {
38             if (CommonUtils::EventRunGet()) {
39                 break;
40             }
41             if (CommonUtils::TaskCalledGet()) {
42                 break;
43             }
44             // If delay more than 1s, break
45             if (count >= MAX_RETRY_COUNT) {
46                 break;
47             }
48 
49             usleep(SLEEP_TIME);
50             count++;
51         }
52     }
53 }
54 
55 /**
56  * Function: Send event with different shared pointer, and then check the processed result.
57  * @param smartPointerType One of smart pointer.
58  */
SendEventWithSmartPtr(SmartPointerType smartPointerType)59 void SendEventWithSmartPtr(SmartPointerType smartPointerType)
60 {
61     auto myRunner = EventRunner::Create();
62     auto handler = std::make_shared<MyEventHandler>(myRunner);
63     auto sharedPtr = std::make_shared<int>(1);
64     auto weakPtr = std::weak_ptr<int>(sharedPtr);
65     auto f = [](int *intPtr) { delete intPtr; };
66     auto uniquePtr = std::unique_ptr<int, void (*)(int *)>((new int(1)), f);
67 
68     int32_t result = 0;
69     auto task = [&handler, &result, &sharedPtr, &weakPtr, &uniquePtr, &f, smartPointerType]() {
70         switch (smartPointerType) {
71             case SmartPointerType::SHARED_PTR: {
72                 result = handler->SendSyncEvent(RUN_EVENT_ID, sharedPtr);
73                 break;
74             }
75             case SmartPointerType::WEAK_PTR: {
76                 result = handler->SendSyncEvent(RUN_EVENT_ID, weakPtr);
77                 break;
78             }
79             case SmartPointerType::LVALUE_REFERENCE_UNIQUE_PTR: {
80                 result = handler->SendSyncEvent(RUN_EVENT_ID, uniquePtr);
81                 break;
82             }
83             case SmartPointerType::RVALUE_REFERENCE_UNIQUE_PTR: {
84                 result = handler->SendSyncEvent(RUN_EVENT_ID, std::unique_ptr<int, void (*)(int *)>((new int(1)), f));
85                 break;
86             }
87             default:
88                 break;
89         }
90     };
91 
92     WaitTaskCalled(task, handler);
93 
94     uint32_t count = 0;
95     while (true) {
96         if (result || (count >= MAX_RETRY_COUNT)) {
97             break;
98         }
99         usleep(SLEEP_TIME);
100         count++;
101     }
102 
103     EXPECT_EQ(1, result);
104 }
105 }  // unnamed namespace
106 
107 class EventHandlerSendSyncEventModuleTest : public testing::Test {
108 public:
109     static void SetUpTestCase(void);
110     static void TearDownTestCase(void);
111     void SetUp();
112     void TearDown();
113 };
114 
SetUpTestCase(void)115 void EventHandlerSendSyncEventModuleTest::SetUpTestCase(void)
116 {}
117 
TearDownTestCase(void)118 void EventHandlerSendSyncEventModuleTest::TearDownTestCase(void)
119 {}
120 
SetUp(void)121 void EventHandlerSendSyncEventModuleTest::SetUp(void)
122 {
123     /**
124      * @tc.setup: Set the value of flags to the default.
125      */
126     CommonUtils::EventRunSet(false);
127     CommonUtils::TaskCalledSet(false);
128 }
129 
TearDown(void)130 void EventHandlerSendSyncEventModuleTest::TearDown(void)
131 {}
132 
133 /**
134  * @tc.name: SendSync001
135  * @tc.desc: Send null event
136  * @tc.type: FUNC
137  * @tc.require: AR000CQ2AF SR000BTOPM
138  */
139 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync001, TestSize.Level1)
140 {
141     /**
142      * @tc.steps: step1. Send a null event.
143      * @tc.expected: step1. Send failed.
144      */
145     auto myRunner = EventRunner::Create(false);
146     auto handler = std::make_shared<MyEventHandler>(myRunner);
147     auto nullPtr = InnerEvent::Pointer(nullptr, nullptr);
148     bool result = handler->SendSyncEvent(nullPtr, EventQueue::Priority::LOW);
149     EXPECT_FALSE(result);
150 }
151 
152 /**
153  * @tc.name: SendSync002
154  * @tc.desc: Send event with IDLE priority
155  * @tc.type: FUNC
156  * @tc.require: SR000CQ2A7 SR000BTOPM
157  */
158 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync002, TestSize.Level1)
159 {
160     /**
161      * @tc.steps: step1. Send an event with IDLE priority.
162      * @tc.expected: step1. Send failed, and event will not be run.
163      */
164     auto event = InnerEvent::Get(RUN_EVENT_ID);
165     auto myRunner = EventRunner::Create(false);
166     auto handler = std::make_shared<MyEventHandler>(myRunner);
167     bool result = handler->SendSyncEvent(event, EventQueue::Priority::IDLE);
168     EXPECT_FALSE(result);
169 
170     bool runResult = CommonUtils::EventRunGet();
171     EXPECT_FALSE(runResult);
172 }
173 
174 /**
175  * @tc.name: SendSync003
176  * @tc.desc: Send non-null event with priority which not IDLE
177  * @tc.type: FUNC
178  * @tc.require: SR000BTOPJ SR000BTOPM
179  */
180 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync003, TestSize.Level1)
181 {
182     /**
183      * @tc.steps: step1. Send a non-idle priority event in a task.
184      * @tc.expected: step1. Send successfully, and event will be run.
185      */
186     auto event = InnerEvent::Get(RUN_EVENT_ID);
187     auto myRunner = EventRunner::Create();
188     auto handler = std::make_shared<MyEventHandler>(myRunner);
189 
__anon7c1e0d860402() 190     auto f = [&handler, &event]() {
191         bool result = handler->SendSyncEvent(event, EventQueue::Priority::IMMEDIATE);
192         EXPECT_TRUE(result);
193     };
194     WaitTaskCalled(f, handler);
195 
196     bool runResult = CommonUtils::EventRunGet();
197     EXPECT_TRUE(runResult);
198 }
199 
200 /**
201  * @tc.name: SendSync004
202  * @tc.desc: Send non-null event with priority which not IDLE in new runner
203  * @tc.type: FUNC
204  * @tc.require: SR000CQ2AQ SR000BTOPM
205  */
206 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync004, TestSize.Level1)
207 {
208     /**
209      * @tc.steps: step1. Send an event with priority which not IDLE in new runner.
210      * @tc.expected: step1. Send successfully, and event will be run.
211      */
212     auto event = InnerEvent::Get(RUN_EVENT_ID);
213     auto myRunner = EventRunner::Create();
214     auto handler = std::make_shared<MyEventHandler>(myRunner);
215     bool result = handler->SendSyncEvent(event, EventQueue::Priority::HIGH);
216     EXPECT_TRUE(result);
217 
218     bool runResult = CommonUtils::EventRunGet();
219     EXPECT_TRUE(runResult);
220 }
221 
222 /**
223  * @tc.name: SendSync005
224  * @tc.desc: Send r-value event with priority which not IDLE
225  * @tc.type: FUNC
226  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
227  */
228 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync005, TestSize.Level1)
229 {
230     /**
231      * @tc.steps: step1. Send r-value event with priority which not IDLE in a task.
232      * @tc.expected: step1. Send successfully, the event will be run.
233      */
234     auto event = InnerEvent::Get(RUN_EVENT_ID);
235     auto myRunner = EventRunner::Create();
236     auto handler = std::make_shared<MyEventHandler>(myRunner);
237 
__anon7c1e0d860502() 238     auto f = [&handler]() {
239         bool rValueResult = handler->SendSyncEvent(InnerEvent::Get(RUN_EVENT_ID), EventQueue::Priority::LOW);
240         EXPECT_TRUE(rValueResult);
241     };
242     WaitTaskCalled(f, handler);
243 
244     bool runResult = CommonUtils::EventRunGet();
245     EXPECT_TRUE(runResult);
246 }
247 
248 /**
249  * @tc.name: SendSync006
250  * @tc.desc: Send event with eventId, param and priority which not IDLE
251  * @tc.type: FUNC
252  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
253  */
254 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync006, TestSize.Level1)
255 {
256     /**
257      * @tc.steps: step1. Send event in a task with eventId, param and priority which not IDLE.
258      * @tc.expected: step1. Send successfully, the event will be run.
259      */
260     auto myRunner = EventRunner::Create();
261     auto handler = std::make_shared<MyEventHandler>(myRunner);
262 
__anon7c1e0d860602() 263     auto f = [&handler]() {
264         bool result = handler->SendSyncEvent(RUN_EVENT_ID, 0);
265         EXPECT_TRUE(result);
266     };
267     WaitTaskCalled(f, handler);
268 
269     bool runResult = CommonUtils::EventRunGet();
270     EXPECT_TRUE(runResult);
271 }
272 
273 /**
274  * @tc.name: SendSync007
275  * @tc.desc: Send event with eventId, shared_ptr object and priority which not IDLE
276  * @tc.type: FUNC
277  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
278  */
279 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync007, TestSize.Level1)
280 {
281     /**
282      * @tc.steps: step1. Send event in a task with eventId, shared_ptr object and priority which not IDLE.
283      * @tc.expected: step1. Send successfully, the event will be run.
284      */
285     SendEventWithSmartPtr(SmartPointerType::SHARED_PTR);
286     bool runResult = CommonUtils::EventRunGet();
287     EXPECT_TRUE(runResult);
288 }
289 
290 /**
291  * @tc.name: SendSync008
292  * @tc.desc: Send event with eventId, weak_ptr object and priority which not IDLE
293  * @tc.type: FUNC
294  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
295  */
296 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync008, TestSize.Level1)
297 {
298     /**
299      * @tc.steps: step1. Send event in a task with eventId, weak_ptr object and priority which not IDLE.
300      * @tc.expected: step1. Send successfully, the event will be run.
301      */
302     SendEventWithSmartPtr(SmartPointerType::WEAK_PTR);
303     bool runResult = CommonUtils::EventRunGet();
304     EXPECT_TRUE(runResult);
305 }
306 
307 /**
308  * @tc.name: SendSync009
309  * @tc.desc: Send event with eventId, unique_ptr object and priority which not IDLE
310  * @tc.type: FUNC
311  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
312  */
313 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync009, TestSize.Level1)
314 {
315     /**
316      * @tc.steps: step1. Send event in a task with eventId, unique_ptr object and priority which not IDLE.
317      * @tc.expected: step1. Send successfully, the event will be run.
318      */
319     SendEventWithSmartPtr(SmartPointerType::LVALUE_REFERENCE_UNIQUE_PTR);
320     bool runResult = CommonUtils::EventRunGet();
321     EXPECT_TRUE(runResult);
322 }
323 
324 /**
325  * @tc.name: SendSync010
326  * @tc.desc: Send event with eventId, rvalue reference unique_ptr object and priority which not IDLE
327  * @tc.type: FUNC
328  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
329  */
330 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync010, TestSize.Level1)
331 {
332     /**
333      * @tc.steps: step1. Send event in a task with eventId,
334      *                   rvalue reference unique_ptr object and priority which not IDLE.
335      * @tc.expected: step1. Send successfully, the event will be run.
336      */
337     SendEventWithSmartPtr(SmartPointerType::RVALUE_REFERENCE_UNIQUE_PTR);
338     bool runResult = CommonUtils::EventRunGet();
339     EXPECT_TRUE(runResult);
340 }
341 
342 /**
343  * @tc.name: SendSync011
344  * @tc.desc: Post task with callback, taskName and priority
345  * @tc.type: FUNC
346  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
347  */
348 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync011, TestSize.Level1)
349 {
350     /**
351      * @tc.steps: step1. Post task in another task with callback, taskName and priority.
352      * @tc.expected: step1. Post successfully, the task will be run.
353      */
354     string taskName = "myTask";
__anon7c1e0d860702() 355     auto myTask = []() { CommonUtils::TaskCalledSet(true); };
356     auto myRunner = EventRunner::Create();
357     auto handler = std::make_shared<MyEventHandler>(myRunner);
__anon7c1e0d860802() 358     auto f = [&handler, &myTask, &taskName]() {
359         bool result = handler->PostSyncTask(myTask, taskName);
360         EXPECT_TRUE(result);
361     };
362     WaitTaskCalled(f, handler);
363 
364     bool runResult = CommonUtils::TaskCalledGet();
365     EXPECT_TRUE(runResult);
366 }
367 
368 /**
369  * @tc.name: SendSync012
370  * @tc.desc: Post task with callback and priority
371  * @tc.type: FUNC
372  * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
373  */
374 HWTEST_F(EventHandlerSendSyncEventModuleTest, SendSync012, TestSize.Level1)
375 {
376     /**
377      * @tc.steps: step1. Post task in another task with callback and priority.
378      * @tc.expected: step1. Post successfully, the task will be run.
379      */
__anon7c1e0d860902() 380     auto myTask = []() { CommonUtils::TaskCalledSet(true); };
381     auto myRunner = EventRunner::Create();
382     auto handler = std::make_shared<MyEventHandler>(myRunner);
__anon7c1e0d860a02() 383     auto f = [&handler, &myTask]() {
384         bool result = handler->PostSyncTask(myTask, EventQueue::Priority::HIGH);
385         EXPECT_TRUE(result);
386     };
387     WaitTaskCalled(f, handler);
388 
389     bool runResult = CommonUtils::TaskCalledGet();
390     EXPECT_TRUE(runResult);
391 }