• 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 EventHandlerPostTaskModuleTest : 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 EventHandlerPostTaskModuleTest::SetUpTestCase(void)
32 {}
33 
TearDownTestCase(void)34 void EventHandlerPostTaskModuleTest::TearDownTestCase(void)
35 {}
36 
SetUp(void)37 void EventHandlerPostTaskModuleTest::SetUp(void)
38 {
39     /**
40      * @tc.setup: Set the value of test flags to the default.
41      */
42     CommonUtils::TaskCalledSet(false);
43 }
44 
TearDown(void)45 void EventHandlerPostTaskModuleTest::TearDown(void)
46 {}
47 
48 /**
49  * @tc.name: Post001
50  * @tc.desc: Post a task with callback, name, delayTime and priority
51  * @tc.type: FUNC
52  * @tc.require: SR000BTOPD SR000BTOPJ
53  */
54 HWTEST_F(EventHandlerPostTaskModuleTest, Post001, TestSize.Level1)
55 {
56     /**
57      * @tc.steps: step1. Post a task with callback, name, delayTime and priority.
58      * @tc.expected: step1. Post successfully and the task handled.
59      */
60     int64_t delayTime = 1;
61     string taskName = std::to_string(Random());
__anond04304590102() 62     auto f = []() { CommonUtils::TaskCalledSet(true); };
63     auto myRunner = EventRunner::Create(false);
64     auto handler = std::make_shared<MyEventHandler>(myRunner);
65 
66     bool postResult = handler->PostTask(f, taskName, delayTime, EventQueue::Priority::LOW);
67     EXPECT_TRUE(postResult);
68     int64_t param = 0;
69     handler->SendEvent(STOP_EVENT_ID, param, delayTime + 1);
70     myRunner->Run();
71     bool runResult = CommonUtils::TaskCalledGet();
72     EXPECT_TRUE(runResult);
73 }
74 
75 /**
76  * @tc.name: Post002
77  * @tc.desc: Post a task with callback, delayTime and priority
78  * @tc.type: FUNC
79  * @tc.require: SR000BTOPM AR000CQ2AE
80  */
81 HWTEST_F(EventHandlerPostTaskModuleTest, Post002, TestSize.Level1)
82 {
83     /**
84      * @tc.steps: step1. Post a task with callback, delayTime and priority.
85      * @tc.expected: step1. Post successfully and the task handled.
86      */
87     int64_t delayTime = 0;
__anond04304590202() 88     auto f = []() { CommonUtils::TaskCalledSet(true); };
89     auto myRunner = EventRunner::Create(false);
90     auto handler = std::make_shared<MyEventHandler>(myRunner);
91 
92     bool postResult = handler->PostTask(f, delayTime, EventQueue::Priority::HIGH);
93     EXPECT_TRUE(postResult);
94     handler->SendEvent(STOP_EVENT_ID);
95     myRunner->Run();
96     bool runResult = CommonUtils::TaskCalledGet();
97     EXPECT_TRUE(runResult);
98 }
99 
100 /**
101  * @tc.name: Post003
102  * @tc.desc: Post an immediate task with callback and name
103  * @tc.type: FUNC
104  * @tc.require: AR000CQ2A8 SR000CQ2AH
105  */
106 HWTEST_F(EventHandlerPostTaskModuleTest, Post003, TestSize.Level1)
107 {
108     /**
109      * @tc.steps: step1. Post an immediate task with callback and name.
110      * @tc.expected: step1. Post successfully and the task handled.
111      */
112     string taskName = std::to_string(Random());
__anond04304590302() 113     auto f = []() { CommonUtils::TaskCalledSet(true); };
114     auto myRunner = EventRunner::Create(false);
115     auto handler = std::make_shared<MyEventHandler>(myRunner);
116 
117     bool postResult = handler->PostImmediateTask(f, taskName);
118     EXPECT_TRUE(postResult);
119     handler->SendEvent(STOP_EVENT_ID);
120     myRunner->Run();
121     bool runResult = CommonUtils::TaskCalledGet();
122     EXPECT_TRUE(runResult);
123 }
124 
125 /**
126  * @tc.name: Post004
127  * @tc.desc: Post a high priority with callback, name and delayTime
128  * @tc.type: FUNC
129  * @tc.require: AR000CQ2A9 SR000CQ2AH
130  */
131 HWTEST_F(EventHandlerPostTaskModuleTest, Post004, TestSize.Level1)
132 {
133     /**
134      * @tc.steps: step1. Post a high priority with callback, name and delayTime.
135      * @tc.expected: step1. Post successfully and the task handled.
136      */
137     int64_t delayTime = 0;
138     string taskName = std::to_string(Random());
__anond04304590402() 139     auto f = []() { CommonUtils::TaskCalledSet(true); };
140     auto myRunner = EventRunner::Create(false);
141     auto handler = std::make_shared<MyEventHandler>(myRunner);
142 
143     bool postResult = handler->PostHighPriorityTask(f, taskName, delayTime);
144     EXPECT_TRUE(postResult);
145     handler->SendEvent(STOP_EVENT_ID);
146     myRunner->Run();
147     bool runResult = CommonUtils::TaskCalledGet();
148     EXPECT_TRUE(runResult);
149 }
150 
151 /**
152  * @tc.name: Post005
153  * @tc.desc: Post a high priority with callback and delayTime
154  * @tc.type: FUNC
155  * @tc.require: AR000CQ2AA SR000CQ2AH
156  */
157 HWTEST_F(EventHandlerPostTaskModuleTest, Post005, TestSize.Level1)
158 {
159     /**
160      * @tc.steps: step1. Post a high priority with callback and delayTime.
161      * @tc.expected: step1. Post successfully and the task handled.
162      */
163     int64_t delayTime = 0;
__anond04304590502() 164     auto f = []() { CommonUtils::TaskCalledSet(true); };
165     auto myRunner = EventRunner::Create(false);
166     auto handler = std::make_shared<MyEventHandler>(myRunner);
167 
168     bool postResult = handler->PostHighPriorityTask(f, delayTime);
169     EXPECT_TRUE(postResult);
170     handler->SendEvent(STOP_EVENT_ID);
171     myRunner->Run();
172     bool runResult = CommonUtils::TaskCalledGet();
173     EXPECT_TRUE(runResult);
174 }
175 
176 /**
177  * @tc.name: Post006
178  * @tc.desc: Post a idle priority with callback, name and delayTime
179  * @tc.type: FUNC
180  * @tc.require: AR000CQ2AB SR000BTOPJ
181  */
182 HWTEST_F(EventHandlerPostTaskModuleTest, Post006, TestSize.Level1)
183 {
184     /**
185      * @tc.steps: step1. Post a idle priority with callback, name and delayTime.
186      * @tc.expected: step1. Post successfully and the task handled.
187      */
188     int64_t delayTime = 0;
189     string taskName = std::to_string(Random());
__anond04304590602() 190     auto f = []() { CommonUtils::TaskCalledSet(true); };
191     auto myRunner = EventRunner::Create(false);
192     auto handler = std::make_shared<MyEventHandler>(myRunner);
193 
194     bool postResult = handler->PostIdleTask(f, taskName, delayTime);
195     EXPECT_TRUE(postResult);
__anond04304590702() 196     auto fStop = [&myRunner]() { myRunner->Stop(); };
197     handler->PostIdleTask(fStop, delayTime + 1);
198     int64_t param = 0;
199     int64_t offsetTime = 2;
200     handler->SendEvent(RUN_EVENT_ID, param, delayTime + offsetTime);
201     myRunner->Run();
202     bool runResult = CommonUtils::TaskCalledGet();
203     EXPECT_TRUE(runResult);
204 }
205 
206 /**
207  * @tc.name: Post007
208  * @tc.desc: Post a idle priority with callback and delayTime
209  * @tc.type: FUNC
210  * @tc.require: AR000CQ2AC SR000BTOPJ
211  */
212 HWTEST_F(EventHandlerPostTaskModuleTest, Post007, TestSize.Level1)
213 {
214     /**
215      * @tc.steps: step1. Post a idle priority with callback and delayTime.
216      * @tc.expected: step1. Post successfully and the task handled.
217      */
218     int64_t delayTime = 0;
219     int64_t param = 0;
__anond04304590802() 220     auto f = []() { CommonUtils::TaskCalledSet(true); };
221     auto myRunner = EventRunner::Create(false);
222     auto handler = std::make_shared<MyEventHandler>(myRunner);
223 
224     bool postResult = handler->PostIdleTask(f, delayTime);
225     EXPECT_TRUE(postResult);
__anond04304590902() 226     auto fStop = [&myRunner]() { myRunner->Stop(); };
227     handler->PostIdleTask(fStop, delayTime + 1);
228     int64_t offsetTime = 2;
229     handler->SendEvent(RUN_EVENT_ID, param, delayTime + offsetTime);
230     myRunner->Run();
231     bool runResult = CommonUtils::TaskCalledGet();
232     EXPECT_TRUE(runResult);
233 }
234 
235 /**
236  * @tc.name: Post008
237  * @tc.desc: Post a task at front with callback, name, delayTime and priority
238  * @tc.type: FUNC
239  * @tc.require: SR000BTOPD SR000BTOPJ
240  */
241 HWTEST_F(EventHandlerPostTaskModuleTest, Post008, TestSize.Level1)
242 {
243     /**
244      * @tc.steps: step1. Post a task with callback, name, delayTime and priority.
245      * @tc.expected: step1. Post successfully and the task handled.
246      */
247     auto myRunner = EventRunner::Create(false);
248     auto handler = std::make_shared<MyEventHandler>(myRunner);
249     // insert at end
250     string taskName = std::to_string(Random());
__anond04304590a02() 251     auto f1 = []() { CommonUtils::CommonUtils::TaskCalledSet(true); };
252     bool postResult = handler->PostTask(f1, taskName, 0, EventQueue::Priority::LOW);
253     EXPECT_TRUE(postResult);
254     // insert at front
255     taskName = std::to_string(Random());
__anond04304590b02() 256     auto f2 = []() { CommonUtils::CommonUtils::TaskCalledSet(false); };
257     postResult = handler->PostTaskAtFront(f2, taskName, EventQueue::Priority::LOW);
258     EXPECT_TRUE(postResult);
259     int64_t param = 0;
260     handler->SendEvent(STOP_EVENT_ID, param, 1);
261     myRunner->Run();
262     bool runResult = CommonUtils::TaskCalledGet();
263     EXPECT_TRUE(runResult);
264 }
265 
266 /**
267  * @tc.name: Post009
268  * @tc.desc: Post a task at front with callback, name, delayTime and priority
269  * @tc.type: FUNC
270  * @tc.require: SR000BTOPD SR000BTOPJ
271  */
272 HWTEST_F(EventHandlerPostTaskModuleTest, Post009, TestSize.Level1)
273 {
274     /**
275      * @tc.steps: step1. Post a task with callback, name, delayTime and priority.
276      * @tc.expected: step1. Post successfully and the task handled.
277      */
278     auto myRunner = EventRunner::Create(false);
279     auto handler = std::make_shared<MyEventHandler>(myRunner);
280     // insert at front
281     string taskName = std::to_string(Random());
__anond04304590c02() 282     auto f1 = []() { CommonUtils::CommonUtils::TaskCalledSet(false); };
283     bool postResult = handler->PostTaskAtFront(f1, taskName, EventQueue::Priority::LOW);
284     EXPECT_TRUE(postResult);
285     // insert at front
286     taskName = std::to_string(Random());
__anond04304590d02() 287     auto f2 = []() { CommonUtils::CommonUtils::TaskCalledSet(true); };
288     postResult = handler->PostTaskAtFront(f2, taskName, EventQueue::Priority::LOW);
289     EXPECT_TRUE(postResult);
290     int64_t param = 0;
291     handler->SendEvent(STOP_EVENT_ID, param, 1);
292     myRunner->Run();
293     bool runResult = CommonUtils::TaskCalledGet();
294     EXPECT_FALSE(runResult);
295 }
296 
297 /**
298  * @tc.name: Post009
299  * @tc.desc: Post a task at no wait runner
300  * @tc.type: FUNC
301  * @tc.require: SR20240112414855
302  */
303 HWTEST_F(EventHandlerPostTaskModuleTest, Post010, TestSize.Level1)
304 {
305     /**
306      * @tc.steps: step1. Post a task with callback, name, delayTime and priority in a no wait runner.
307      * @tc.expected: step1. Post successfully and the task handled.
308      */
309     auto myRunner = EventRunner::CreateNoWait("NowaitRunner001");
310     auto handler = std::make_shared<MyEventHandler>(myRunner);
311     // insert at front
312     string taskName = std::to_string(Random());
__anond04304590e02() 313     auto f1 = []() { CommonUtils::CommonUtils::TaskCalledSet(false); };
314     bool postResult = handler->PostTask(f1, taskName, 0, EventQueue::Priority::LOW);
315     EXPECT_TRUE(postResult);
316     // insert at front
317     taskName = std::to_string(Random());
__anond04304590f02() 318     auto f2 = []() { CommonUtils::CommonUtils::TaskCalledSet(true); };
319     postResult = handler->PostTask(f2, taskName, 1000, EventQueue::Priority::LOW);
320     EXPECT_TRUE(postResult);
321     myRunner->Run();
322     bool runResult = CommonUtils::TaskCalledGet();
323     EXPECT_FALSE(runResult);
324 }
325