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 auto ret = taskRunner.AddTask(task);
100 EXPECT_EQ(E_OK, ret);
101 } catch (...) {
102 EXPECT_TRUE(false);
103 GTEST_LOG_(INFO) << "AddTask ERROR";
104 }
105
106 GTEST_LOG_(INFO) << "AddTask End";
107 }
108
109 /**
110 * @tc.name: StartTask001
111 * @tc.desc: Verify the StartTask001 function
112 * @tc.type: FUNC
113 * @tc.require: I6JPKG
114 */
115 HWTEST_F(TaskTest, StartTask001, TestSize.Level1)
116 {
117 GTEST_LOG_(INFO) << "StartTask001 Begin";
118 try {
119 auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
120 auto context = std::make_shared<TaskContext>(handler);
121 TaskAction action = Action;
122 auto task = make_shared<Task>(context, action);
123 std::function<void()> callBack = CallBack;
124 auto taskRunner = make_shared<TaskRunner>(callBack);
125 taskRunner->stopFlag_ = make_shared<bool>(false);
126 taskRunner->SetCommitFunc(CommitFuncSuccess);
127 auto ret = taskRunner->StartTask(task, action);
128 EXPECT_EQ(E_OK, ret);
129 } catch (...) {
130 EXPECT_TRUE(false);
131 GTEST_LOG_(INFO) << " StartTask001 ERROR";
132 }
133
134 GTEST_LOG_(INFO) << "StartTask001 End";
135 }
136
137 /**
138 * @tc.name: StartTask002
139 * @tc.desc: Verify the StartTask002 function
140 * @tc.type: FUNC
141 * @tc.require: I6JPKG
142 */
143 HWTEST_F(TaskTest, StartTask002, TestSize.Level1)
144 {
145 GTEST_LOG_(INFO) << "StartTask002 Begin";
146 try {
147 auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
148 auto context = std::make_shared<TaskContext>(handler);
149 TaskAction action = Action;
150 auto task = make_shared<Task>(context, action);
151 std::function<void()> callBack = CallBack;
152 auto taskRunner = make_shared<TaskRunner>(callBack);
153 taskRunner->stopFlag_ = make_shared<bool>(false);
154 taskRunner->SetCommitFunc(CommitFuncFail);
155 auto ret = taskRunner->StartTask(task, action);
156 EXPECT_EQ(1, ret);
157 } catch (...) {
158 EXPECT_TRUE(false);
159 GTEST_LOG_(INFO) << " StartTask002 ERROR";
160 }
161
162 GTEST_LOG_(INFO) << "StartTask002 End";
163 }
164
165 /**
166 * @tc.name: CommitTask001
167 * @tc.desc: Verify the CommitTask001 function
168 * @tc.type: FUNC
169 * @tc.require: I6JPKG
170 */
171 HWTEST_F(TaskTest, CommitTask001, TestSize.Level1)
172 {
173 GTEST_LOG_(INFO) << "CommitTask001 Begin";
174 try {
175 auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
176 auto context = std::make_shared<TaskContext>(handler);
177 TaskAction action = Action;
178 auto task = make_shared<Task>(context, action);
179 std::function<void()> callBack = CallBack;
180 auto taskRunner = make_shared<TaskRunner>(callBack);
181 taskRunner->stopFlag_ = make_shared<bool>(false);
182 taskRunner->AddTask(task);
183 taskRunner->SetCommitFunc(CommitFuncFail);
184 auto ret = taskRunner->CommitTask(task);
185 EXPECT_EQ(1, ret);
186 } catch (...) {
187 EXPECT_TRUE(false);
188 GTEST_LOG_(INFO) << " CommitTask001 ERROR";
189 }
190
191 GTEST_LOG_(INFO) << "CommitTask001 End";
192 }
193
194 /**
195 * @tc.name: CommitTask002
196 * @tc.desc: Verify the CommitTask002 function
197 * @tc.type: FUNC
198 * @tc.require: I6JPKG
199 */
200 HWTEST_F(TaskTest, CommitTask002, TestSize.Level1)
201 {
202 GTEST_LOG_(INFO) << "CommitTask002 Begin";
203 try {
204 auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
205 auto context = std::make_shared<TaskContext>(handler);
206 TaskAction action = Action;
207 auto task = make_shared<Task>(context, action);
208 std::function<void()> callBack = CallBack;
209 auto taskRunner = make_shared<TaskRunner>(callBack);
210 taskRunner->stopFlag_ = make_shared<bool>(false);
211 taskRunner->AddTask(task);
212 taskRunner->SetCommitFunc(CommitFuncSuccess);
213 auto ret = taskRunner->CommitTask(task);
214 EXPECT_EQ(E_OK, ret);
215 } catch (...) {
216 EXPECT_TRUE(false);
217 GTEST_LOG_(INFO) << " CommitTask002 ERROR";
218 }
219
220 GTEST_LOG_(INFO) << "CommitTask002 End";
221 }
222
223 /**
224 * @tc.name: CompleteTask
225 * @tc.desc: Verify the CompleteTask function
226 * @tc.type: FUNC
227 * @tc.require: I6JPKG
228 */
229 HWTEST_F(TaskTest, CompleteTask, TestSize.Level1)
230 {
231 GTEST_LOG_(INFO) << "CompleteTask Begin";
232 try {
233 auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
234 auto context = std::make_shared<TaskContext>(handler);
235 TaskAction action = Action;
236 auto task = make_shared<Task>(context, action);
237 task->SetId(0);
238
239 std::function<void()> callBack = CallBack;
240 TaskRunner taskRunner(callBack);
241 taskRunner.stopFlag_ = make_shared<bool>(true);
242 taskRunner.CompleteTask(0);
243 EXPECT_TRUE(true);
244 *taskRunner.stopFlag_ = false;
245 taskRunner.CompleteTask(0);
246 EXPECT_TRUE(true);
247 taskRunner.taskList_.push_back(task);
248 taskRunner.CompleteTask(0);
249 EXPECT_TRUE(true);
250 taskRunner.CompleteTask(1);
251 EXPECT_TRUE(true);
252 } catch (...) {
253 EXPECT_TRUE(false);
254 GTEST_LOG_(INFO) << " CompleteTask ERROR";
255 }
256
257 GTEST_LOG_(INFO) << "CompleteTask End";
258 }
259
260 /**
261 * @tc.name: ReleaseTask
262 * @tc.desc: Verify the ReleaseTask function
263 * @tc.type: FUNC
264 * @tc.require: I6JPKG
265 */
266 HWTEST_F(TaskTest, ReleaseTask, TestSize.Level1)
267 {
268 GTEST_LOG_(INFO) << "ReleaseTask Begin";
269 try {
270 auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
271 auto context = std::make_shared<TaskContext>(handler);
272 TaskAction action = Action;
273 auto task = make_shared<Task>(context, action);
274
275 std::function<void()> callBack = CallBack;
276 TaskRunner taskRunner(callBack);
277 taskRunner.taskList_.push_back(task);
278 auto ret = taskRunner.ReleaseTask();
279 EXPECT_TRUE(ret);
280 } catch (...) {
281 EXPECT_TRUE(false);
282 GTEST_LOG_(INFO) << " ReleaseTask ERROR";
283 }
284
285 GTEST_LOG_(INFO) << "ReleaseTask End";
286 }
287
288 /**
289 * @tc.name: Reset
290 * @tc.desc: Verify the Reset function
291 * @tc.type: FUNC
292 * @tc.require: I6JPKG
293 */
294 HWTEST_F(TaskTest, Reset, TestSize.Level1)
295 {
296 GTEST_LOG_(INFO) << "Reset Begin";
297 try {
298 std::function<void()> callBack = CallBack;
299 TaskRunner taskRunner(callBack);
300 taskRunner.Reset();
301 EXPECT_TRUE(true);
302 } catch (...) {
303 EXPECT_TRUE(false);
304 GTEST_LOG_(INFO) << " Reset ERROR";
305 }
306
307 GTEST_LOG_(INFO) << "Reset End";
308 }
309
310 /**
311 * @tc.name: CommitDummyTask
312 * @tc.desc: Verify the CommitDummyTask function
313 * @tc.type: FUNC
314 * @tc.require: I6JPKG
315 */
316 HWTEST_F(TaskTest, CommitDummyTask, TestSize.Level1)
317 {
318 GTEST_LOG_(INFO) << "CommitDummyTask Begin";
319 try {
320 std::function<void()> callBack = CallBack;
321 TaskRunner taskRunner(callBack);
322 taskRunner.CommitDummyTask();
323 EXPECT_TRUE(true);
324 } catch (...) {
325 EXPECT_TRUE(false);
326 GTEST_LOG_(INFO) << " CommitDummyTask ERROR";
327 }
328
329 GTEST_LOG_(INFO) << "CommitDummyTask End";
330 }
331
332 /**
333 * @tc.name: CompleteDummyTask
334 * @tc.desc: Verify the CompleteDummyTask function
335 * @tc.type: FUNC
336 * @tc.require: I6JPKG
337 */
338 HWTEST_F(TaskTest, CompleteDummyTask, TestSize.Level1)
339 {
340 GTEST_LOG_(INFO) << "CompleteDummyTask Begin";
341 try {
342 std::function<void()> callBack = CallBack;
343 TaskRunner taskRunner(callBack);
344 taskRunner.stopFlag_ = make_shared<bool>(false);
345 taskRunner.CompleteDummyTask();
346 EXPECT_TRUE(true);
347 } catch (...) {
348 EXPECT_TRUE(false);
349 GTEST_LOG_(INFO) << " CompleteDummyTask ERROR";
350 }
351
352 GTEST_LOG_(INFO) << "CompleteDummyTask End";
353 }
354
355 /**
356 * @tc.name: AllocRunner
357 * @tc.desc: Verify the AllocRunner function
358 * @tc.type: FUNC
359 * @tc.require: I6JPKG
360 */
361 HWTEST_F(TaskTest, AllocRunner, TestSize.Level1)
362 {
363 GTEST_LOG_(INFO) << "AllocRunner Begin";
364 try {
365 TaskManager taskManager;
366 int32_t userId = 0;
367 std::string bundleName = "bundleName";
368 function<void()> callBack = CallBack;
369 auto taskRunner = taskManager.AllocRunner(userId, bundleName, callBack);
370 EXPECT_TRUE(taskRunner != nullptr);
371 } catch (...) {
372 EXPECT_TRUE(false);
373 GTEST_LOG_(INFO) << " AllocRunner ERROR";
374 }
375
376 GTEST_LOG_(INFO) << "AllocRunner End";
377 }
378
379 /**
380 * @tc.name: ReleaseRunner
381 * @tc.desc: Verify the ReleaseRunner function
382 * @tc.type: FUNC
383 * @tc.require: I6JPKG
384 */
385 HWTEST_F(TaskTest, ReleaseRunner, TestSize.Level1)
386 {
387 GTEST_LOG_(INFO) << "ReleaseRunner Begin";
388 try {
389 TaskManager taskManager;
390 int32_t userId = 0;
391 std::string bundleName = "bundleName";
392 taskManager.ReleaseRunner(userId, bundleName);
393 EXPECT_TRUE(true);
394 } catch (...) {
395 EXPECT_TRUE(false);
396 GTEST_LOG_(INFO) << " ReleaseRunner ERROR";
397 }
398
399 GTEST_LOG_(INFO) << "ReleaseRunner End";
400 }
401
402 /**
403 * @tc.name: GetRunner
404 * @tc.desc: Verify the GetRunner function
405 * @tc.type: FUNC
406 * @tc.require: I6JPKG
407 */
408 HWTEST_F(TaskTest, GetRunner, TestSize.Level1)
409 {
410 GTEST_LOG_(INFO) << "GetRunner Begin";
411 try {
412 TaskManager taskManager;
413 int32_t userId = 0;
414 std::string bundleName = "bundleName";
415 auto taskRunner = taskManager.GetRunner(userId, bundleName);
416 EXPECT_TRUE(taskRunner == nullptr);
417
418 std::string key = "key";
419 std::function<void()> callBack = CallBack;
420 auto value = make_shared<TaskRunner>(callBack);
421 taskManager.map_.insert({key, value});
422 taskRunner = taskManager.GetRunner(userId, bundleName);
423 EXPECT_TRUE(taskRunner == nullptr);
424 } catch (...) {
425 EXPECT_TRUE(false);
426 GTEST_LOG_(INFO) << " GetRunner ERROR";
427 }
428
429 GTEST_LOG_(INFO) << "GetRunner End";
430 }
431
432 /**
433 * @tc.name: InitRunner
434 * @tc.desc: Verify the InitRunner function
435 * @tc.type: FUNC
436 * @tc.require: I6JPKG
437 */
438 HWTEST_F(TaskTest, InitRunner, TestSize.Level1)
439 {
440 GTEST_LOG_(INFO) << "InitRunner Begin";
441 try {
442 TaskManager taskManager;
443 std::function<void()> callBack = CallBack;
444 TaskRunner taskRunner(callBack);
445 taskManager.InitRunner(taskRunner);
446 EXPECT_TRUE(true);
447 } catch (...) {
448 EXPECT_TRUE(false);
449 GTEST_LOG_(INFO) << " InitRunner ERROR";
450 }
451
452 GTEST_LOG_(INFO) << "InitRunner End";
453 }
454
455 /**
456 * @tc.name: TaskManager_CommitTask
457 * @tc.desc: Verify the TaskManager_CommitTask function
458 * @tc.type: FUNC
459 * @tc.require: I6JPKG
460 */
461 HWTEST_F(TaskTest, TaskManager_CommitTask, TestSize.Level1)
462 {
463 GTEST_LOG_(INFO) << "TaskManager_CommitTask Begin";
464 try {
465 auto handler = std::make_shared<DataHandlerMock>(USER_ID, BUND_NAME, TABLE_NAME);
466 auto context = std::make_shared<TaskContext>(handler);
467 TaskAction action = Action;
468 auto task = make_shared<Task>(context, action);
469 std::function<void()> callBack = CallBack;
470 auto taskRunner = make_shared<TaskRunner>(callBack);
471 TaskManager taskManager;
472 taskManager.CommitTask(taskRunner, task);
473 EXPECT_TRUE(true);
474 } catch (...) {
475 EXPECT_TRUE(false);
476 GTEST_LOG_(INFO) << " TaskManager_CommitTask ERROR";
477 }
478
479 GTEST_LOG_(INFO) << "TaskManager_CommitTask End";
480 }
481
482 /**
483 * @tc.name: GetKey
484 * @tc.desc: Verify the GetKey function
485 * @tc.type: FUNC
486 * @tc.require: I6JPKG
487 */
488 HWTEST_F(TaskTest, GetKey, TestSize.Level1)
489 {
490 GTEST_LOG_(INFO) << "GetKey Begin";
491 try {
492 TaskManager taskManager;
493 int32_t userId = 0;
494 string bundleName = "bundleName";
495 auto ret = taskManager.GetKey(userId, bundleName);
496 EXPECT_EQ(ret, "0bundleName");
497 } catch (...) {
498 EXPECT_TRUE(false);
499 GTEST_LOG_(INFO) << " GetKey ERROR";
500 }
501
502 GTEST_LOG_(INFO) << "GetKey End";
503 }
504 } // namespace OHOS::FileManagement::CloudSync::Test
505