1 /*
2 * Copyright (c) 2023 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 <functional>
17 #include <gtest/gtest.h>
18
19 #include "work_queue.h"
20 #include "work_status.h"
21 #include "work_scheduler_service.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace WorkScheduler {
27
28 class WorkQueueTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
TearDownTestCase()31 static void TearDownTestCase() {}
SetUp()32 void SetUp() {}
TearDown()33 void TearDown() {}
34 static std::shared_ptr<WorkQueue> workQueue_;
35 };
36
37 std::shared_ptr<WorkQueue> WorkQueueTest::workQueue_ = nullptr;
38
SetUpTestCase()39 void WorkQueueTest::SetUpTestCase()
40 {
41 workQueue_ = std::make_shared<WorkQueue>();
42 }
43
44 /**
45 * @tc.name: ParseCondition_001
46 * @tc.desc: Test WorkQueue ParseCondition.
47 * @tc.type: FUNC
48 * @tc.require: I8JBRY
49 */
HWTEST_F(WorkQueueTest, ParseCondition_001, TestSize.Level1)50 HWTEST_F(WorkQueueTest, ParseCondition_001, TestSize.Level1)
51 {
52 WorkCondition::Type type = WorkCondition::Type::NETWORK;
53 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(0, 0, false, "");
54 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
55 EXPECT_TRUE(ret->enumVal == WorkCondition::Network::NETWORK_TYPE_ANY);
56 }
57
58 /**
59 * @tc.name: ParseCondition_002
60 * @tc.desc: Test WorkQueue ParseCondition.
61 * @tc.type: FUNC
62 * @tc.require: I8JBRY
63 */
HWTEST_F(WorkQueueTest, ParseCondition_002, TestSize.Level1)64 HWTEST_F(WorkQueueTest, ParseCondition_002, TestSize.Level1)
65 {
66 WorkCondition::Type type = WorkCondition::Type::BATTERY_STATUS;
67 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(0, 0, false, "");
68 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
69 EXPECT_TRUE(ret->enumVal == WorkCondition::BatteryStatus::BATTERY_STATUS_LOW);
70 }
71
72 /**
73 * @tc.name: ParseCondition_003
74 * @tc.desc: Test WorkQueue ParseCondition.
75 * @tc.type: FUNC
76 * @tc.require: I8JBRY
77 */
HWTEST_F(WorkQueueTest, ParseCondition_003, TestSize.Level1)78 HWTEST_F(WorkQueueTest, ParseCondition_003, TestSize.Level1)
79 {
80 WorkCondition::Type type = WorkCondition::Type::STORAGE;
81 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(0, 0, false, "");
82 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
83 EXPECT_TRUE(ret->enumVal == WorkCondition::Storage::STORAGE_LEVEL_LOW);
84 }
85
86 /**
87 * @tc.name: ParseCondition_004
88 * @tc.desc: Test WorkQueue ParseCondition.
89 * @tc.type: FUNC
90 * @tc.require: I8JBRY
91 */
HWTEST_F(WorkQueueTest, ParseCondition_004, TestSize.Level1)92 HWTEST_F(WorkQueueTest, ParseCondition_004, TestSize.Level1)
93 {
94 WorkCondition::Type type = WorkCondition::Type::CHARGER;
95 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(0, 0, false, "");
96 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
97 EXPECT_TRUE(ret->enumVal == WorkCondition::Charger::CHARGING_PLUGGED_ANY);
98 }
99
100 /**
101 * @tc.name: ParseCondition_005
102 * @tc.desc: Test WorkQueue ParseCondition.
103 * @tc.type: FUNC
104 * @tc.require: I8JBRY
105 */
HWTEST_F(WorkQueueTest, ParseCondition_005, TestSize.Level1)106 HWTEST_F(WorkQueueTest, ParseCondition_005, TestSize.Level1)
107 {
108 WorkCondition::Type type = WorkCondition::Type::BATTERY_LEVEL;
109 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(81, 0, false, "");
110 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
111 EXPECT_TRUE(ret->intVal == 81);
112 }
113
114 /**
115 * @tc.name: ParseCondition_006
116 * @tc.desc: Test WorkQueue ParseCondition.
117 * @tc.type: FUNC
118 * @tc.require: I8JBRY
119 */
HWTEST_F(WorkQueueTest, ParseCondition_006, TestSize.Level1)120 HWTEST_F(WorkQueueTest, ParseCondition_006, TestSize.Level1)
121 {
122 WorkCondition::Type type = WorkCondition::Type::TIMER;
123 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(0, 0, false, "");
124 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
125 EXPECT_TRUE(ret->intVal == 0);
126 }
127
128 /**
129 * @tc.name: ParseCondition_007
130 * @tc.desc: Test WorkQueue ParseCondition.
131 * @tc.type: FUNC
132 * @tc.require: I8JBRY
133 */
HWTEST_F(WorkQueueTest, ParseCondition_007, TestSize.Level1)134 HWTEST_F(WorkQueueTest, ParseCondition_007, TestSize.Level1)
135 {
136 WorkCondition::Type type = WorkCondition::Type::GROUP;
137 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(0, 0, false, "");
138 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
139 EXPECT_TRUE(ret->enumVal == 0);
140 }
141
142 /**
143 * @tc.name: ParseCondition_008
144 * @tc.desc: Test WorkQueue ParseCondition.
145 * @tc.type: FUNC
146 * @tc.require: I8JBRY
147 */
HWTEST_F(WorkQueueTest, ParseCondition_008, TestSize.Level1)148 HWTEST_F(WorkQueueTest, ParseCondition_008, TestSize.Level1)
149 {
150 WorkCondition::Type type = WorkCondition::Type::DEEP_IDLE;
151 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(0, 0, false, "");
152 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
153 EXPECT_FALSE(ret->boolVal);
154 }
155
156 /**
157 * @tc.name: ParseCondition_009
158 * @tc.desc: Test WorkQueue ParseCondition.
159 * @tc.type: FUNC
160 * @tc.require: I8JBRY
161 */
HWTEST_F(WorkQueueTest, ParseCondition_009, TestSize.Level1)162 HWTEST_F(WorkQueueTest, ParseCondition_009, TestSize.Level1)
163 {
164 WorkCondition::Type type = WorkCondition::Type::UNKNOWN;
165 std::shared_ptr<DetectorValue> value = std::make_shared<DetectorValue>(0, 0, false, "");
166 std::shared_ptr<Condition> ret = workQueue_->ParseCondition(type, value);
167 EXPECT_FALSE(ret->boolVal);
168 }
169
170 /**
171 * @tc.name: Push_001
172 * @tc.desc: Test WorkQueue Push.
173 * @tc.type: FUNC
174 * @tc.require: I8JBRY
175 */
HWTEST_F(WorkQueueTest, Push_001, TestSize.Level1)176 HWTEST_F(WorkQueueTest, Push_001, TestSize.Level1)
177 {
178 workQueue_->ClearAll();
179 vector<std::shared_ptr<WorkStatus>> workVector;
180 for (int i = 0; i < 10; i++) {
181 auto workInfo_ = WorkInfo();
182 workInfo_.SetWorkId(i);
183 std::string bundleName = "com.example.workStatus";
184 std::string abilityName = "workStatusAbility";
185 workInfo_.SetElement(bundleName, abilityName);
186 auto workStatus = std::make_shared<WorkStatus>(workInfo_, i);
187 workVector.push_back(workStatus);
188 }
189 std::shared_ptr<vector<std::shared_ptr<WorkStatus>>> workStatusVector =
190 std::make_shared<vector<std::shared_ptr<WorkStatus>>>(workVector);
191 workQueue_->Push(workStatusVector);
192 EXPECT_TRUE(workQueue_->GetSize() == 10);
193 }
194
195 /**
196 * @tc.name: Push_002
197 * @tc.desc: Test WorkQueue Push.
198 * @tc.type: FUNC
199 * @tc.require: I8JBRY
200 */
HWTEST_F(WorkQueueTest, Push_002, TestSize.Level1)201 HWTEST_F(WorkQueueTest, Push_002, TestSize.Level1)
202 {
203 workQueue_->ClearAll();
204 auto workInfo_ = WorkInfo();
205 workInfo_.SetWorkId(1);
206 std::string bundleName = "com.example.workStatus";
207 std::string abilityName = "workStatusAbility";
208 workInfo_.SetElement(bundleName, abilityName);
209 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
210 workQueue_->Push(workStatus);
211 EXPECT_TRUE(workQueue_->GetSize() == 1);
212 }
213
214 /**
215 * @tc.name: Remove_001
216 * @tc.desc: Test WorkQueue Remove.
217 * @tc.type: FUNC
218 * @tc.require: I8JBRY
219 */
HWTEST_F(WorkQueueTest, Remove_001, TestSize.Level1)220 HWTEST_F(WorkQueueTest, Remove_001, TestSize.Level1)
221 {
222 workQueue_->ClearAll();
223 auto workInfo_ = WorkInfo();
224 workInfo_.SetWorkId(1);
225 std::string bundleName = "com.example.workStatus";
226 std::string abilityName = "workStatusAbility";
227 workInfo_.SetElement(bundleName, abilityName);
228 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
229 workQueue_->Push(workStatus);
230 workQueue_->Remove(workStatus);
231 EXPECT_TRUE(workQueue_->GetSize() == 0);
232 }
233
234 /**
235 * @tc.name: Contains_001
236 * @tc.desc: Test WorkQueue Contains.
237 * @tc.type: FUNC
238 * @tc.require: I8JBRY
239 */
HWTEST_F(WorkQueueTest, Contains_001, TestSize.Level1)240 HWTEST_F(WorkQueueTest, Contains_001, TestSize.Level1)
241 {
242 workQueue_->ClearAll();
243 auto workInfo_ = WorkInfo();
244 workInfo_.SetWorkId(1);
245 std::string bundleName = "com.example.workStatus";
246 std::string abilityName = "workStatusAbility";
247 workInfo_.SetElement(bundleName, abilityName);
248 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
249 workQueue_->Push(workStatus);
250 bool ret = workQueue_->Contains(std::make_shared<std::string>(workStatus->workId_));
251 EXPECT_TRUE(ret);
252 }
253
254 /**
255 * @tc.name: Contains_002
256 * @tc.desc: Test WorkQueue Contains.
257 * @tc.type: FUNC
258 * @tc.require: I8JBRY
259 */
HWTEST_F(WorkQueueTest, Contains_002, TestSize.Level1)260 HWTEST_F(WorkQueueTest, Contains_002, TestSize.Level1)
261 {
262 workQueue_->ClearAll();
263 auto workInfo_ = WorkInfo();
264 workInfo_.SetWorkId(1);
265 std::string bundleName = "com.example.workStatus";
266 std::string abilityName = "workStatusAbility";
267 workInfo_.SetElement(bundleName, abilityName);
268 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
269 workQueue_->Push(workStatus);
270 bool ret = workQueue_->Contains(std::make_shared<std::string>("u1_2"));
271 EXPECT_FALSE(ret);
272 }
273
274 /**
275 * @tc.name: Find_001
276 * @tc.desc: Test WorkQueue Find.
277 * @tc.type: FUNC
278 * @tc.require: I8JBRY
279 */
HWTEST_F(WorkQueueTest, Find_001, TestSize.Level1)280 HWTEST_F(WorkQueueTest, Find_001, TestSize.Level1)
281 {
282 workQueue_->ClearAll();
283 auto workInfo_ = WorkInfo();
284 workInfo_.SetWorkId(1);
285 std::string bundleName = "com.example.workStatus";
286 std::string abilityName = "workStatusAbility";
287 workInfo_.SetElement(bundleName, abilityName);
288 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
289 workQueue_->Push(workStatus);
290 bool ret = workQueue_->Contains(std::make_shared<std::string>(workStatus->workId_));
291 EXPECT_TRUE(ret);
292 }
293
294 /**
295 * @tc.name: Find_002
296 * @tc.desc: Test WorkQueue Find.
297 * @tc.type: FUNC
298 * @tc.require: I8JBRY
299 */
HWTEST_F(WorkQueueTest, Find_002, TestSize.Level1)300 HWTEST_F(WorkQueueTest, Find_002, TestSize.Level1)
301 {
302 workQueue_->ClearAll();
303 auto workInfo_ = WorkInfo();
304 workInfo_.SetWorkId(1);
305 std::string bundleName = "com.example.workStatus";
306 std::string abilityName = "workStatusAbility";
307 workInfo_.SetElement(bundleName, abilityName);
308 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
309 workQueue_->Push(workStatus);
310 bool ret = workQueue_->Contains(std::make_shared<std::string>("u1_2"));
311 EXPECT_FALSE(ret);
312 }
313
314 /**
315 * @tc.name: GetWorkToRunByPriority_001
316 * @tc.desc: Test WorkQueue GetWorkToRunByPriority.
317 * @tc.type: FUNC
318 * @tc.require: I8JBRY
319 */
HWTEST_F(WorkQueueTest, GetWorkToRunByPriority_001, TestSize.Level1)320 HWTEST_F(WorkQueueTest, GetWorkToRunByPriority_001, TestSize.Level1)
321 {
322 workQueue_->ClearAll();
323 auto workInfo_ = WorkInfo();
324 workInfo_.SetWorkId(1);
325 std::string bundleName = "com.example.workStatus";
326 std::string abilityName = "workStatusAbility";
327 workInfo_.SetElement(bundleName, abilityName);
328 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
329 workStatus->MarkStatus(WorkStatus::Status::CONDITION_READY);
330 workQueue_->Push(workStatus);
331 auto ret = workQueue_->GetWorkToRunByPriority();
332 EXPECT_TRUE(ret != nullptr);
333 }
334
335 /**
336 * @tc.name: CancelWork_001
337 * @tc.desc: Test WorkQueue CancelWork.
338 * @tc.type: FUNC
339 * @tc.require: I8JBRY
340 */
HWTEST_F(WorkQueueTest, CancelWork_001, TestSize.Level1)341 HWTEST_F(WorkQueueTest, CancelWork_001, TestSize.Level1)
342 {
343 workQueue_->ClearAll();
344 auto workInfo_ = WorkInfo();
345 workInfo_.SetWorkId(1);
346 std::string bundleName = "com.example.workStatus";
347 std::string abilityName = "workStatusAbility";
348 workInfo_.SetElement(bundleName, abilityName);
349 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
350 workStatus->MarkStatus(WorkStatus::Status::CONDITION_READY);
351 workQueue_->Push(workStatus);
352 auto ret = workQueue_->CancelWork(workStatus);
353 EXPECT_TRUE(ret);
354 }
355
356 /**
357 * @tc.name: GetWorkList_001
358 * @tc.desc: Test WorkQueue GetWorkList.
359 * @tc.type: FUNC
360 * @tc.require: I8JBRY
361 */
HWTEST_F(WorkQueueTest, GetWorkList_001, TestSize.Level1)362 HWTEST_F(WorkQueueTest, GetWorkList_001, TestSize.Level1)
363 {
364 workQueue_->ClearAll();
365 auto workInfo_ = WorkInfo();
366 workInfo_.SetWorkId(1);
367 std::string bundleName = "com.example.workStatus";
368 std::string abilityName = "workStatusAbility";
369 workInfo_.SetElement(bundleName, abilityName);
370 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
371 workStatus->MarkStatus(WorkStatus::Status::CONDITION_READY);
372 workQueue_->Push(workStatus);
373 std::list<std::shared_ptr<WorkStatus>> ret = workQueue_->GetWorkList();
374 EXPECT_TRUE(ret.size() == 1);
375 }
376
377 /**
378 * @tc.name: RemoveUnReady_001
379 * @tc.desc: Test WorkQueue RemoveUnReady.
380 * @tc.type: FUNC
381 * @tc.require: I8JBRY
382 */
HWTEST_F(WorkQueueTest, RemoveUnReady_001, TestSize.Level1)383 HWTEST_F(WorkQueueTest, RemoveUnReady_001, TestSize.Level1)
384 {
385 workQueue_->ClearAll();
386 auto workInfo_ = WorkInfo();
387 workInfo_.SetWorkId(1);
388 std::string bundleName = "com.example.workStatus";
389 std::string abilityName = "workStatusAbility";
390 workInfo_.SetElement(bundleName, abilityName);
391 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
392 workStatus->MarkStatus(WorkStatus::Status::WAIT_CONDITION);
393 workQueue_->Push(workStatus);
394 workQueue_->RemoveUnReady();
395 EXPECT_TRUE(workQueue_->GetWorkList().size() == 0);
396 }
397
398 /**
399 * @tc.name: GetRunningCount_001
400 * @tc.desc: Test WorkQueue GetRunningCount.
401 * @tc.type: FUNC
402 * @tc.require: I8JBRY
403 */
HWTEST_F(WorkQueueTest, GetRunningCount_001, TestSize.Level1)404 HWTEST_F(WorkQueueTest, GetRunningCount_001, TestSize.Level1)
405 {
406 workQueue_->ClearAll();
407 auto workInfo_ = WorkInfo();
408 workInfo_.SetWorkId(1);
409 std::string bundleName = "com.example.workStatus";
410 std::string abilityName = "workStatusAbility";
411 workInfo_.SetElement(bundleName, abilityName);
412 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
413 workStatus->MarkStatus(WorkStatus::Status::RUNNING);
414 workQueue_->Push(workStatus);
415 EXPECT_TRUE(workQueue_->GetRunningCount() == 1);
416 }
417
418 /**
419 * @tc.name: GetRunningWorks_001
420 * @tc.desc: Test WorkQueue GetRunningWorks.
421 * @tc.type: FUNC
422 * @tc.require: I8JBRY
423 */
HWTEST_F(WorkQueueTest, GetRunningWorks_001, TestSize.Level1)424 HWTEST_F(WorkQueueTest, GetRunningWorks_001, TestSize.Level1)
425 {
426 workQueue_->ClearAll();
427 auto workInfo_ = WorkInfo();
428 workInfo_.SetWorkId(1);
429 std::string bundleName = "com.example.workStatus";
430 std::string abilityName = "workStatusAbility";
431 workInfo_.SetElement(bundleName, abilityName);
432 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
433 workStatus->MarkStatus(WorkStatus::Status::RUNNING);
434 workQueue_->Push(workStatus);
435 EXPECT_TRUE(workQueue_->GetRunningWorks().size() == 1);
436 }
437
438 /**
439 * @tc.name: GetDeepIdleWorks_001
440 * @tc.desc: Test WorkQueue GetDeepIdleWorks.
441 * @tc.type: FUNC
442 * @tc.require: I8JBRY
443 */
HWTEST_F(WorkQueueTest, GetDeepIdleWorks_001, TestSize.Level1)444 HWTEST_F(WorkQueueTest, GetDeepIdleWorks_001, TestSize.Level1)
445 {
446 workQueue_->ClearAll();
447 auto workInfo_ = WorkInfo();
448 workInfo_.SetWorkId(1);
449 std::string bundleName = "com.example.workStatus";
450 std::string abilityName = "workStatusAbility";
451 workInfo_.SetElement(bundleName, abilityName);
452 workInfo_.RequestDeepIdle(true);
453 auto workStatus = std::make_shared<WorkStatus>(workInfo_, 1);
454 workStatus->MarkStatus(WorkStatus::Status::RUNNING);
455 workQueue_->Push(workStatus);
456 EXPECT_TRUE(workQueue_->GetDeepIdleWorks().size() == 1);
457 }
458 }
459 }