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());
__anonc7dbd2770102() 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;
__anonc7dbd2770202() 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());
__anonc7dbd2770302() 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());
__anonc7dbd2770402() 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;
__anonc7dbd2770502() 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());
__anonc7dbd2770602() 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);
__anonc7dbd2770702() 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;
__anonc7dbd2770802() 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);
__anonc7dbd2770902() 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