1 /*
2  * Copyright (C) 2024-2024 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 #define private   public
16 #define protected public
17 #include "task_manager.h"
18 
19 #include "actions/action_wait.h"
20 #include "tasks/task_ams.h"
21 #include "tasks/task_inner.h"
22 #undef private
23 #undef protected
24 
25 #include <chrono>
26 #include <gtest/gtest.h>
27 #include <memory>
28 #include <thread>
29 
30 #include "global.h"
31 
32 namespace OHOS {
33 namespace MiscServices {
34 
35 using namespace testing::ext;
36 
37 class TaskManagerTest : public testing::Test {
38 public:
39     void SetUp() override
40     {
41         IMSA_HILOGI("TaskManagerTest::SetUp");
42         mgr = std::make_unique<TaskManager>();
43     }
44 
45     void TearDown() override
46     {
47         IMSA_HILOGI("TaskManagerTest::TearDown");
48         mgr.reset();
49     }
50 
51 public:
52     std::unique_ptr<TaskManager> mgr;
53 };
54 
55 /**
56  * @tc.name: PostTask_001
57  * @tc.desc: Post a null task
58  * @tc.type: FUNC
59  * @tc.require:
60  */
HWTEST_F(TaskManagerTest, PostTask_001, TestSize.Level0)61 HWTEST_F(TaskManagerTest, PostTask_001, TestSize.Level0)
62 {
63     IMSA_HILOGI("TaskManagerTest PostTask_001 START");
64     std::shared_ptr<Task> task { nullptr };
65     auto ret = mgr->PostTask(task);
66     EXPECT_EQ(ret, 0);
67 }
68 
69 /**
70  * @tc.name: PostTask_002
71  * @tc.desc: Post an AmsInit task
72  * @tc.type: FUNC
73  * @tc.require:
74  */
HWTEST_F(TaskManagerTest, PostTask_002, TestSize.Level0)75 HWTEST_F(TaskManagerTest, PostTask_002, TestSize.Level0)
76 {
77     IMSA_HILOGI("TaskManagerTest PostTask_002 START");
78     std::shared_ptr<Task> task = std::make_shared<TaskAmsInit>();
79     auto ret = mgr->PostTask(task);
80     EXPECT_EQ(ret, task->GetSeqId());
81 
82     std::this_thread::sleep_for(std::chrono::seconds(1));
83     EXPECT_EQ(mgr->amsTasks_.size(), 0);
84     EXPECT_EQ(mgr->curTask_, task);
85     EXPECT_EQ(mgr->curTask_->GetSeqId(), ret);
86 }
87 
88 /**
89  * @tc.name: Complete_001
90  * @tc.desc: Call TaskManager::Complete with seqId
91  * @tc.type: FUNC
92  * @tc.require:
93  */
HWTEST_F(TaskManagerTest, Complete_001, TestSize.Level0)94 HWTEST_F(TaskManagerTest, Complete_001, TestSize.Level0)
95 {
96     IMSA_HILOGI("TaskManagerTest Complete_001 START");
97 
98     EXPECT_EQ(mgr->curTask_, nullptr);
99     auto seqId = Task::GetNextSeqId();
100     mgr->Complete(seqId);
101     std::this_thread::sleep_for(std::chrono::seconds(1));
102     EXPECT_EQ(mgr->innerTasks_.size(), 1);
103     EXPECT_EQ(mgr->innerTasks_.front()->GetSeqId(), seqId);
104 }
105 
106 /**
107  * @tc.name: Pend_001
108  * @tc.desc: Pend a NULL action
109  * @tc.type: FUNC
110  * @tc.require:
111  */
HWTEST_F(TaskManagerTest, Pend_001, TestSize.Level0)112 HWTEST_F(TaskManagerTest, Pend_001, TestSize.Level0)
113 {
114     IMSA_HILOGI("TaskManagerTest Pend_001 START");
115     std::unique_ptr<ActionWait> action { nullptr };
116     auto ret = mgr->Pend(std::move(action));
117     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
118 }
119 
120 /**
121  * @tc.name: Pend_002
122  * @tc.desc: Pend a action while curTask_ is NULL or not running
123  * @tc.type: FUNC
124  * @tc.require:
125  */
HWTEST_F(TaskManagerTest, Pend_002, TestSize.Level0)126 HWTEST_F(TaskManagerTest, Pend_002, TestSize.Level0)
127 {
128     IMSA_HILOGI("TaskManagerTest Pend_002 START");
129     EXPECT_EQ(mgr->curTask_, nullptr);
130 
131     std::unique_ptr<ActionWait> action = std::make_unique<ActionWait>(Task::GetNextSeqId(), 1000);
132     auto ret = mgr->Pend(std::move(action));
133     EXPECT_EQ(ret, ErrorCode::ERROR_TASK_MANAGER_PEND_FAILED);
134 
135     mgr->curTask_ = std::make_shared<TaskAmsInit>();
136     action = std::make_unique<ActionWait>(0, 1000);
137     ret = mgr->Pend(std::move(action));
138     EXPECT_EQ(ret, ErrorCode::ERROR_TASK_MANAGER_PEND_FAILED);
139 }
140 
141 /**
142  * @tc.name: Pend_003
143  * @tc.desc: Pend action success
144  * @tc.type: FUNC
145  * @tc.require:
146  */
HWTEST_F(TaskManagerTest, Pend_003, TestSize.Level0)147 HWTEST_F(TaskManagerTest, Pend_003, TestSize.Level0)
148 {
149     IMSA_HILOGI("TaskManagerTest Pend_003 START");
150     EXPECT_EQ(mgr->curTask_, nullptr);
151 
152     mgr->curTask_ = std::make_shared<TaskAmsInit>();
153     mgr->curTask_->state_ = RUNNING_STATE_RUNNING;
154 
155     auto action = std::make_unique<ActionWait>(Task::GetNextSeqId(), 1000);
156     auto ret = mgr->Pend(std::move(action));
157     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
158     EXPECT_EQ(mgr->curTask_->pendingActions_.size(), 1);
159 }
160 
161 /**
162  * @tc.name: WaitExec_001
163  * @tc.desc: Pend action success
164  * @tc.type: FUNC
165  * @tc.require:
166  */
HWTEST_F(TaskManagerTest, WaitExec_001, TestSize.Level0)167 HWTEST_F(TaskManagerTest, WaitExec_001, TestSize.Level0)
168 {
169     IMSA_HILOGI("TaskManagerTest WaitExec_001 START");
170     EXPECT_EQ(mgr->curTask_, nullptr);
171 
172     auto seqId = Task::GetNextSeqId();
173     bool flag = false;
174     auto func = [&flag] {
175         flag = true;
176     };
177 
178     auto ret = mgr->WaitExec(seqId, 1000, func);
179     EXPECT_EQ(ret, ErrorCode::ERROR_TASK_MANAGER_PEND_FAILED);
180 }
181 
182 /**
183  * @tc.name: WaitExec_002
184  * @tc.desc: Pend action success
185  * @tc.type: FUNC
186  * @tc.require:
187  */
HWTEST_F(TaskManagerTest, WaitExec_002, TestSize.Level0)188 HWTEST_F(TaskManagerTest, WaitExec_002, TestSize.Level0)
189 {
190     IMSA_HILOGI("TaskManagerTest WaitExec_002 START");
191     EXPECT_EQ(mgr->curTask_, nullptr);
192 
193     auto task = std::make_shared<TaskAmsInit>();
194     task->state_ = RUNNING_STATE_RUNNING;
195     mgr->curTask_ = task;
196 
197     EXPECT_EQ(mgr->curTask_->pendingActions_.size(), 0);
198 
199     auto seqId = Task::GetNextSeqId();
200     bool flag = false;
201     auto func = [&flag] {
202         flag = true;
203     };
204     auto ret = mgr->WaitExec(seqId, 1000, func);
205     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
206     EXPECT_EQ(mgr->curTask_->pendingActions_.size(), 2);
207 }
208 
209 /**
210  * @tc.name: OnNewTask_001
211  * @tc.desc: OnNewTask got empty task
212  * @tc.type: FUNC
213  * @tc.require:
214  */
HWTEST_F(TaskManagerTest, OnNewTask_001, TestSize.Level0)215 HWTEST_F(TaskManagerTest, OnNewTask_001, TestSize.Level0)
216 {
217     IMSA_HILOGI("TaskManagerTest OnNewTask_001 START");
218 
219     std::shared_ptr<Task> task { nullptr };
220     mgr->OnNewTask(task);
221     EXPECT_EQ(mgr->amsTasks_.size(), 0);
222     EXPECT_EQ(mgr->imaTasks_.size(), 0);
223     EXPECT_EQ(mgr->imsaTasks_.size(), 0);
224     EXPECT_EQ(mgr->innerTasks_.size(), 0);
225 }
226 
227 /**
228  * @tc.name: OnNewTask_002
229  * @tc.desc: OnNewTask got a valid task
230  * @tc.type: FUNC
231  * @tc.require:
232  */
HWTEST_F(TaskManagerTest, OnNewTask_002, TestSize.Level0)233 HWTEST_F(TaskManagerTest, OnNewTask_002, TestSize.Level0)
234 {
235     IMSA_HILOGI("TaskManagerTest OnNewTask_002 START");
236 
237     EXPECT_EQ(mgr->curTask_, nullptr);
238     EXPECT_EQ(mgr->amsTasks_.size(), 0);
239     EXPECT_EQ(mgr->imaTasks_.size(), 0);
240     EXPECT_EQ(mgr->imsaTasks_.size(), 0);
241     EXPECT_EQ(mgr->innerTasks_.size(), 0);
242 
243     std::shared_ptr<Task> task = std::make_shared<TaskAmsInit>();
244     mgr->OnNewTask(task);
245 
246     EXPECT_EQ(mgr->amsTasks_.size(), 0);
247     EXPECT_EQ(mgr->curTask_, task);
248 }
249 
250 /**
251  * @tc.name: ProcessNextInnerTask_001
252  * @tc.desc: ProcessNextInnerTask while curTask_ is NULL
253  * @tc.type: FUNC
254  * @tc.require:
255  */
HWTEST_F(TaskManagerTest, ProcessNextInnerTask_001, TestSize.Level0)256 HWTEST_F(TaskManagerTest, ProcessNextInnerTask_001, TestSize.Level0)
257 {
258     IMSA_HILOGI("TaskManagerTest ProcessNextInnerTask_001 START");
259 
260     mgr->innerTasks_ = {
261         std::make_shared<TaskResume>(Task::GetNextSeqId()),
262         std::make_shared<TaskResume>(Task::GetNextSeqId()),
263         std::make_shared<TaskResume>(Task::GetNextSeqId()),
264     };
265 
266     mgr->ProcessNextInnerTask();
267     EXPECT_EQ(mgr->innerTasks_.size(), 3);
268 }
269 
270 /**
271  * @tc.name: ProcessNextInnerTask_002
272  * @tc.desc: ProcessNextInnerTask resume success
273  * @tc.type: FUNC
274  * @tc.require:
275  */
HWTEST_F(TaskManagerTest, ProcessNextInnerTask_002, TestSize.Level0)276 HWTEST_F(TaskManagerTest, ProcessNextInnerTask_002, TestSize.Level0)
277 {
278     IMSA_HILOGI("TaskManagerTest ProcessNextInnerTask_002 START");
279 
280     auto task = std::make_shared<TaskAmsInit>();
281     mgr->OnNewTask(task);
282     EXPECT_EQ(mgr->curTask_, task);
283     EXPECT_EQ(mgr->curTask_->state_, RUNNING_STATE_PAUSED);
284 
285     mgr->innerTasks_ = {
286         std::make_shared<TaskResume>(task->GetSeqId()),
287     };
288     mgr->ProcessNextInnerTask();
289     EXPECT_EQ(mgr->curTask_, nullptr);
290     EXPECT_EQ(task->GetState(), RUNNING_STATE_COMPLETED);
291     EXPECT_EQ(mgr->innerTasks_.size(), 0);
292 }
293 
294 /**
295  * @tc.name: ProcessNextAmsTask_001
296  * @tc.desc: ProcessNextAmsTask while curTask_ is not NULL
297  * @tc.type: FUNC
298  * @tc.require:
299  */
HWTEST_F(TaskManagerTest, ProcessNextAmsTask_001, TestSize.Level0)300 HWTEST_F(TaskManagerTest, ProcessNextAmsTask_001, TestSize.Level0)
301 {
302     IMSA_HILOGI("TaskManagerTest ProcessNextAmsTask_001 START");
303 
304     mgr->curTask_ = std::make_shared<TaskResume>(Task::GetNextSeqId());
305     auto task = std::make_shared<TaskAmsInit>();
306     mgr->amsTasks_.push_back(task);
307     mgr->ProcessNextAmsTask();
308     EXPECT_EQ(mgr->curTask_, task);
309     EXPECT_EQ(mgr->curTask_->GetState(), RUNNING_STATE_PAUSED);
310 }
311 
312 /**
313  * @tc.name: ProcessNextAmsTask_002
314  * @tc.desc: ProcessNextAmsTask while curTask_ is not NULL
315  * @tc.type: FUNC
316  * @tc.require:
317  */
HWTEST_F(TaskManagerTest, ProcessNextAmsTask_002, TestSize.Level0)318 HWTEST_F(TaskManagerTest, ProcessNextAmsTask_002, TestSize.Level0)
319 {
320     IMSA_HILOGI("TaskManagerTest ProcessNextAmsTask_002 START");
321     EXPECT_EQ(mgr->curTask_, nullptr);
322     auto task = std::make_shared<TaskAmsInit>();
323     mgr->amsTasks_.push_back(task);
324     mgr->ProcessNextAmsTask();
325     EXPECT_EQ(mgr->curTask_, task);
326     EXPECT_EQ(mgr->curTask_->GetState(), RUNNING_STATE_PAUSED);
327 }
328 
329 } // namespace MiscServices
330 } // namespace OHOS