• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "data_handler_mock.h"
17 #include "dfs_error.h"
18 #include "rdb_helper.h"
19 #include "rdb_open_callback.h"
20 #include "rdb_store_config.h"
21 #include "result_set_mock.h"
22 #include "sync_rule/cloud_status.h"
23 #include "task.h"
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 #include <memory>
27 
28 namespace OHOS::FileManagement::CloudSync::Test {
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace std;
32 const int USER_ID = 100;
33 const std::string BUND_NAME = "com.ohos.photos";
34 const std::string TABLE_NAME = "test";
CallBack()35 void CallBack()
36 {
37     return;
38 }
39 
Action(std::shared_ptr<TaskContext> context)40 void Action(std::shared_ptr<TaskContext> context)
41 {
42     return;
43 }
44 
CommitFuncSuccess(std::shared_ptr<TaskRunner> runner,std::shared_ptr<Task> task)45 int32_t CommitFuncSuccess(std::shared_ptr<TaskRunner> runner, std::shared_ptr<Task> task)
46 {
47     return 0;
48 }
49 
CommitFuncFail(std::shared_ptr<TaskRunner> runner,std::shared_ptr<Task> task)50 int32_t CommitFuncFail(std::shared_ptr<TaskRunner> runner, std::shared_ptr<Task> task)
51 {
52     return 1;
53 }
54 
55 class TaskTest : public testing::Test {
56 public:
57     static void SetUpTestCase(void);
58     static void TearDownTestCase(void);
59     void SetUp();
60     void TearDown();
61 };
62 
SetUpTestCase(void)63 void TaskTest::SetUpTestCase(void)
64 {
65     GTEST_LOG_(INFO) << "SetUpTestCase";
66 }
67 
TearDownTestCase(void)68 void TaskTest::TearDownTestCase(void)
69 {
70     GTEST_LOG_(INFO) << "TearDownTestCase";
71 }
72 
SetUp(void)73 void TaskTest::SetUp(void)
74 {
75     GTEST_LOG_(INFO) << "SetUp";
76 }
77 
TearDown(void)78 void TaskTest::TearDown(void)
79 {
80     GTEST_LOG_(INFO) << "TearDown";
81 }
82 
83 /**
84  * @tc.name: AddTask
85  * @tc.desc: Verify the AddTask function
86  * @tc.type: FUNC
87  * @tc.require: I6JPKG
88  */
89 HWTEST_F(TaskTest, AddTask, TestSize.Level1)
90 {
91     GTEST_LOG_(INFO) << "AddTask Begin";
92     try {
93         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
94         auto context = std::make_shared<TaskContext>(handler);
95         TaskAction action = Action;
96         auto task = make_shared<Task>(context, action);
97         std::function<void()> callBack = CallBack;
98         TaskRunner taskRunner(callBack);
99         taskRunner.stopFlag_ = true;
100         auto ret = taskRunner.AddTask(task);
101         EXPECT_EQ(E_STOP, ret);
102         taskRunner.stopFlag_ = false;
103         ret = taskRunner.AddTask(task);
104         EXPECT_EQ(E_OK, ret);
105     } catch (...) {
106         EXPECT_TRUE(false);
107         GTEST_LOG_(INFO) << "AddTask ERROR";
108     }
109 
110     GTEST_LOG_(INFO) << "AddTask End";
111 }
112 
113 /**
114  * @tc.name: StartTask001
115  * @tc.desc: Verify the StartTask001 function
116  * @tc.type: FUNC
117  * @tc.require: I6JPKG
118  */
119 HWTEST_F(TaskTest, StartTask001, TestSize.Level1)
120 {
121     GTEST_LOG_(INFO) << "StartTask001 Begin";
122     try {
123         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
124         auto context = std::make_shared<TaskContext>(handler);
125         TaskAction action = Action;
126         auto task = make_shared<Task>(context, action);
127         std::function<void()> callBack = CallBack;
128         auto taskRunner = make_shared<TaskRunner>(callBack);
129         taskRunner->stopFlag_ = false;
130         taskRunner->SetCommitFunc(CommitFuncSuccess);
131         auto ret = taskRunner->StartTask(task, action);
132         EXPECT_EQ(E_OK, ret);
133     } catch (...) {
134         EXPECT_TRUE(false);
135         GTEST_LOG_(INFO) << " StartTask001 ERROR";
136     }
137 
138     GTEST_LOG_(INFO) << "StartTask001 End";
139 }
140 
141 /**
142  * @tc.name: StartTask002
143  * @tc.desc: Verify the StartTask002 function
144  * @tc.type: FUNC
145  * @tc.require: I6JPKG
146  */
147 HWTEST_F(TaskTest, StartTask002, TestSize.Level1)
148 {
149     GTEST_LOG_(INFO) << "StartTask002 Begin";
150     try {
151         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
152         auto context = std::make_shared<TaskContext>(handler);
153         TaskAction action = Action;
154         auto task = make_shared<Task>(context, action);
155         std::function<void()> callBack = CallBack;
156         auto taskRunner = make_shared<TaskRunner>(callBack);
157         taskRunner->stopFlag_ = false;
158         taskRunner->SetCommitFunc(CommitFuncFail);
159         auto ret = taskRunner->StartTask(task, action);
160         EXPECT_EQ(1, ret);
161     } catch (...) {
162         EXPECT_TRUE(false);
163         GTEST_LOG_(INFO) << " StartTask002 ERROR";
164     }
165 
166     GTEST_LOG_(INFO) << "StartTask002 End";
167 }
168 
169 /**
170  * @tc.name: CommitTask001
171  * @tc.desc: Verify the CommitTask001 function
172  * @tc.type: FUNC
173  * @tc.require: I6JPKG
174  */
175 HWTEST_F(TaskTest, CommitTask001, TestSize.Level1)
176 {
177     GTEST_LOG_(INFO) << "CommitTask001 Begin";
178     try {
179         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
180         auto context = std::make_shared<TaskContext>(handler);
181         TaskAction action = Action;
182         auto task = make_shared<Task>(context, action);
183         std::function<void()> callBack = CallBack;
184         auto taskRunner = make_shared<TaskRunner>(callBack);
185         taskRunner->stopFlag_ = true;
186         taskRunner->AddTask(task);
187         auto ret = taskRunner->CommitTask(task);
188         EXPECT_EQ(E_STOP, ret);
189     } catch (...) {
190         EXPECT_TRUE(false);
191         GTEST_LOG_(INFO) << " CommitTask001 ERROR";
192     }
193 
194     GTEST_LOG_(INFO) << "CommitTask001 End";
195 }
196 
197 /**
198  * @tc.name: CommitTask002
199  * @tc.desc: Verify the CommitTask002 function
200  * @tc.type: FUNC
201  * @tc.require: I6JPKG
202  */
203 HWTEST_F(TaskTest, CommitTask002, TestSize.Level1)
204 {
205     GTEST_LOG_(INFO) << "CommitTask002 Begin";
206     try {
207         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
208         auto context = std::make_shared<TaskContext>(handler);
209         TaskAction action = Action;
210         auto task = make_shared<Task>(context, action);
211         std::function<void()> callBack = CallBack;
212         auto taskRunner = make_shared<TaskRunner>(callBack);
213         taskRunner->stopFlag_ = false;
214         taskRunner->AddTask(task);
215         taskRunner->SetCommitFunc(CommitFuncFail);
216         auto ret = taskRunner->CommitTask(task);
217         EXPECT_EQ(1, ret);
218     } catch (...) {
219         EXPECT_TRUE(false);
220         GTEST_LOG_(INFO) << " CommitTask002 ERROR";
221     }
222 
223     GTEST_LOG_(INFO) << "CommitTask002 End";
224 }
225 
226 /**
227  * @tc.name: CommitTask003
228  * @tc.desc: Verify the CommitTask003 function
229  * @tc.type: FUNC
230  * @tc.require: I6JPKG
231  */
232 HWTEST_F(TaskTest, CommitTask003, TestSize.Level1)
233 {
234     GTEST_LOG_(INFO) << "CommitTask003 Begin";
235     try {
236         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
237         auto context = std::make_shared<TaskContext>(handler);
238         TaskAction action = Action;
239         auto task = make_shared<Task>(context, action);
240         std::function<void()> callBack = CallBack;
241         auto taskRunner = make_shared<TaskRunner>(callBack);
242         taskRunner->stopFlag_ = false;
243         taskRunner->AddTask(task);
244         taskRunner->SetCommitFunc(CommitFuncSuccess);
245         auto ret = taskRunner->CommitTask(task);
246         EXPECT_EQ(E_OK, ret);
247     } catch (...) {
248         EXPECT_TRUE(false);
249         GTEST_LOG_(INFO) << " CommitTask003 ERROR";
250     }
251 
252     GTEST_LOG_(INFO) << "CommitTask003 End";
253 }
254 
255 /**
256  * @tc.name: CompleteTask
257  * @tc.desc: Verify the CompleteTask function
258  * @tc.type: FUNC
259  * @tc.require: I6JPKG
260  */
261 HWTEST_F(TaskTest, CompleteTask, TestSize.Level1)
262 {
263     GTEST_LOG_(INFO) << "CompleteTask Begin";
264     try {
265         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
266         auto context = std::make_shared<TaskContext>(handler);
267         TaskAction action = Action;
268         auto task = make_shared<Task>(context, action);
269         task->SetId(0);
270 
271         std::function<void()> callBack = CallBack;
272         TaskRunner taskRunner(callBack);
273 
274         taskRunner.stopFlag_ = true;
275         taskRunner.CompleteTask(0);
276         EXPECT_TRUE(true);
277         taskRunner.stopFlag_ = false;
278         taskRunner.CompleteTask(0);
279         EXPECT_TRUE(true);
280         taskRunner.taskList_.push_back(task);
281         taskRunner.CompleteTask(0);
282         EXPECT_TRUE(true);
283         taskRunner.CompleteTask(1);
284         EXPECT_TRUE(true);
285     } catch (...) {
286         EXPECT_TRUE(false);
287         GTEST_LOG_(INFO) << " CompleteTask ERROR";
288     }
289 
290     GTEST_LOG_(INFO) << "CompleteTask End";
291 }
292 
293 /**
294  * @tc.name: StopAndWaitFor
295  * @tc.desc: Verify the StopAndWaitFor function
296  * @tc.type: FUNC
297  * @tc.require: I6JPKG
298  */
299 HWTEST_F(TaskTest, StopAndWaitFor, TestSize.Level1)
300 {
301     GTEST_LOG_(INFO) << "StopAndWaitFor Begin";
302     try {
303         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
304         auto context = std::make_shared<TaskContext>(handler);
305         TaskAction action = Action;
306         auto task = make_shared<Task>(context, action);
307 
308         std::function<void()> callBack = CallBack;
309         TaskRunner taskRunner(callBack);
310         auto ret = taskRunner.StopAndWaitFor();
311         EXPECT_TRUE(ret);
312         taskRunner.taskList_.push_back(task);
313         ret = taskRunner.StopAndWaitFor();
314         EXPECT_TRUE(ret == false);
315     } catch (...) {
316         EXPECT_TRUE(false);
317         GTEST_LOG_(INFO) << " StopAndWaitFor ERROR";
318     }
319 
320     GTEST_LOG_(INFO) << "StopAndWaitFor End";
321 }
322 
323 /**
324  * @tc.name: Reset
325  * @tc.desc: Verify the Reset function
326  * @tc.type: FUNC
327  * @tc.require: I6JPKG
328  */
329 HWTEST_F(TaskTest, Reset, TestSize.Level1)
330 {
331     GTEST_LOG_(INFO) << "Reset Begin";
332     try {
333         std::function<void()> callBack = CallBack;
334         TaskRunner taskRunner(callBack);
335         taskRunner.Reset();
336         EXPECT_TRUE(true);
337     } catch (...) {
338         EXPECT_TRUE(false);
339         GTEST_LOG_(INFO) << " Reset ERROR";
340     }
341 
342     GTEST_LOG_(INFO) << "Reset End";
343 }
344 
345 /**
346  * @tc.name: CommitDummyTask
347  * @tc.desc: Verify the CommitDummyTask function
348  * @tc.type: FUNC
349  * @tc.require: I6JPKG
350  */
351 HWTEST_F(TaskTest, CommitDummyTask, TestSize.Level1)
352 {
353     GTEST_LOG_(INFO) << "CommitDummyTask Begin";
354     try {
355         std::function<void()> callBack = CallBack;
356         TaskRunner taskRunner(callBack);
357         taskRunner.CommitDummyTask();
358         EXPECT_TRUE(true);
359     } catch (...) {
360         EXPECT_TRUE(false);
361         GTEST_LOG_(INFO) << " CommitDummyTask ERROR";
362     }
363 
364     GTEST_LOG_(INFO) << "CommitDummyTask End";
365 }
366 
367 /**
368  * @tc.name: CompleteDummyTask
369  * @tc.desc: Verify the CompleteDummyTask function
370  * @tc.type: FUNC
371  * @tc.require: I6JPKG
372  */
373 HWTEST_F(TaskTest, CompleteDummyTask, TestSize.Level1)
374 {
375     GTEST_LOG_(INFO) << "CompleteDummyTask Begin";
376     try {
377         std::function<void()> callBack = CallBack;
378         TaskRunner taskRunner(callBack);
379         taskRunner.CompleteDummyTask();
380         EXPECT_TRUE(true);
381     } catch (...) {
382         EXPECT_TRUE(false);
383         GTEST_LOG_(INFO) << " CompleteDummyTask ERROR";
384     }
385 
386     GTEST_LOG_(INFO) << "CompleteDummyTask End";
387 }
388 
389 /**
390  * @tc.name: AllocRunner
391  * @tc.desc: Verify the AllocRunner function
392  * @tc.type: FUNC
393  * @tc.require: I6JPKG
394  */
395 HWTEST_F(TaskTest, AllocRunner, TestSize.Level1)
396 {
397     GTEST_LOG_(INFO) << "AllocRunner Begin";
398     try {
399         TaskManager taskManager;
400         int32_t userId = 0;
401         std::string bundleName = "bundleName";
402         function<void()> callBack = CallBack;
403         auto taskRunner = taskManager.AllocRunner(userId, bundleName, callBack);
404         EXPECT_TRUE(taskRunner != nullptr);
405     } catch (...) {
406         EXPECT_TRUE(false);
407         GTEST_LOG_(INFO) << " AllocRunner ERROR";
408     }
409 
410     GTEST_LOG_(INFO) << "AllocRunner End";
411 }
412 
413 /**
414  * @tc.name: ReleaseRunner
415  * @tc.desc: Verify the ReleaseRunner function
416  * @tc.type: FUNC
417  * @tc.require: I6JPKG
418  */
419 HWTEST_F(TaskTest, ReleaseRunner, TestSize.Level1)
420 {
421     GTEST_LOG_(INFO) << "ReleaseRunner Begin";
422     try {
423         TaskManager taskManager;
424         int32_t userId = 0;
425         std::string bundleName = "bundleName";
426         taskManager.ReleaseRunner(userId, bundleName);
427         EXPECT_TRUE(true);
428     } catch (...) {
429         EXPECT_TRUE(false);
430         GTEST_LOG_(INFO) << " ReleaseRunner ERROR";
431     }
432 
433     GTEST_LOG_(INFO) << "ReleaseRunner End";
434 }
435 
436 /**
437  * @tc.name: GetRunner
438  * @tc.desc: Verify the GetRunner function
439  * @tc.type: FUNC
440  * @tc.require: I6JPKG
441  */
442 HWTEST_F(TaskTest, GetRunner, TestSize.Level1)
443 {
444     GTEST_LOG_(INFO) << "GetRunner Begin";
445     try {
446         TaskManager taskManager;
447         int32_t userId = 0;
448         std::string bundleName = "bundleName";
449         auto taskRunner = taskManager.GetRunner(userId, bundleName);
450         EXPECT_TRUE(taskRunner == nullptr);
451 
452         std::string key = "key";
453         std::function<void()> callBack = CallBack;
454         auto value = make_shared<TaskRunner>(callBack);
455         taskManager.map_.insert({key, value});
456         taskRunner = taskManager.GetRunner(userId, bundleName);
457         EXPECT_TRUE(taskRunner == nullptr);
458     } catch (...) {
459         EXPECT_TRUE(false);
460         GTEST_LOG_(INFO) << " GetRunner ERROR";
461     }
462 
463     GTEST_LOG_(INFO) << "GetRunner End";
464 }
465 
466 /**
467  * @tc.name: InitRunner
468  * @tc.desc: Verify the InitRunner function
469  * @tc.type: FUNC
470  * @tc.require: I6JPKG
471  */
472 HWTEST_F(TaskTest, InitRunner, TestSize.Level1)
473 {
474     GTEST_LOG_(INFO) << "InitRunner Begin";
475     try {
476         TaskManager taskManager;
477         std::function<void()> callBack = CallBack;
478         TaskRunner taskRunner(callBack);
479         taskManager.InitRunner(taskRunner);
480         EXPECT_TRUE(true);
481     } catch (...) {
482         EXPECT_TRUE(false);
483         GTEST_LOG_(INFO) << " InitRunner ERROR";
484     }
485 
486     GTEST_LOG_(INFO) << "InitRunner End";
487 }
488 
489 /**
490  * @tc.name: TaskManager_CommitTask
491  * @tc.desc: Verify the TaskManager_CommitTask function
492  * @tc.type: FUNC
493  * @tc.require: I6JPKG
494  */
495 HWTEST_F(TaskTest, TaskManager_CommitTask, TestSize.Level1)
496 {
497     GTEST_LOG_(INFO) << "TaskManager_CommitTask Begin";
498     try {
499         auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
500         auto context = std::make_shared<TaskContext>(handler);
501         TaskAction action = Action;
502         auto task = make_shared<Task>(context, action);
503         std::function<void()> callBack = CallBack;
504         auto taskRunner = make_shared<TaskRunner>(callBack);
505         TaskManager taskManager;
506         taskManager.CommitTask(taskRunner, task);
507         EXPECT_TRUE(true);
508     } catch (...) {
509         EXPECT_TRUE(false);
510         GTEST_LOG_(INFO) << " TaskManager_CommitTask ERROR";
511     }
512 
513     GTEST_LOG_(INFO) << "TaskManager_CommitTask End";
514 }
515 
516 /**
517  * @tc.name: GetKey
518  * @tc.desc: Verify the GetKey function
519  * @tc.type: FUNC
520  * @tc.require: I6JPKG
521  */
522 HWTEST_F(TaskTest, GetKey, TestSize.Level1)
523 {
524     GTEST_LOG_(INFO) << "GetKey Begin";
525     try {
526         TaskManager taskManager;
527         int32_t userId = 0;
528         string bundleName = "bundleName";
529         auto ret = taskManager.GetKey(userId, bundleName);
530         EXPECT_EQ(ret, "0bundleName");
531     } catch (...) {
532         EXPECT_TRUE(false);
533         GTEST_LOG_(INFO) << " GetKey ERROR";
534     }
535 
536     GTEST_LOG_(INFO) << "GetKey End";
537 }
538 } // namespace OHOS::FileManagement::CloudSync::Test
539