• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #include <gtest/gtest.h>
17 
18 #include "cloud_sync_common.h"
19 #include "dfs_error.h"
20 #include "cycle_task.h"
21 #include "cycle_task_runner.h"
22 #include "tasks/database_backup_task.h"
23 #include "tasks/optimize_storage_task.h"
24 #include "tasks/periodic_check_task.h"
25 #include "tasks/save_subscription_task.h"
26 #include "tasks/report_statistics_task.h"
27 #include "utils_log.h"
28 
29 namespace OHOS {
30 namespace FileManagement::CloudSync {
31 namespace Test {
32 using namespace testing::ext;
33 using namespace std;
34 
35 std::shared_ptr<CloudFile::DataSyncManager> g_dataSyncManagerPtr_;
36 
37 class SubCycleTask : public CycleTask {
38 public:
SubCycleTask(std::string taskName,std::set<std::string> bundleNames,int32_t intervalTime,std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)39     SubCycleTask(std::string taskName,
40                  std::set<std::string> bundleNames,
41                  int32_t intervalTime,
42                  std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
43         :CycleTask(taskName, bundleNames, intervalTime, dataSyncManager)
44     {}
RunTaskForBundle(int32_t userId,std::string bundleName)45     int32_t RunTaskForBundle(int32_t userId, std::string bundleName) override
46     {
47         return 0;
48     }
49 };
50 
51 class CloudSyncServiceCycleTaskTest : public testing::Test {
52 public:
53     static void SetUpTestCase(void);
54     static void TearDownTestCase(void);
55     void SetUp();
56     void TearDown();
57 };
58 
SetUpTestCase(void)59 void CloudSyncServiceCycleTaskTest::SetUpTestCase(void)
60 {
61     std::cout << "SetUpTestCase" << std::endl;
62     if (g_dataSyncManagerPtr_ == nullptr) {
63         g_dataSyncManagerPtr_ = make_shared<CloudFile::DataSyncManager>();
64     }
65 }
66 
TearDownTestCase(void)67 void CloudSyncServiceCycleTaskTest::TearDownTestCase(void)
68 {
69     std::cout << "TearDownTestCase" << std::endl;
70 }
71 
SetUp(void)72 void CloudSyncServiceCycleTaskTest::SetUp(void)
73 {
74     std::cout << "SetUp" << std::endl;
75 }
76 
TearDown(void)77 void CloudSyncServiceCycleTaskTest::TearDown(void)
78 {
79     std::cout << "TearDown" << std::endl;
80 }
81 
82 /**
83  * @tc.name: RunTaskForBundleTest001
84  * @tc.desc: Verify the RunTaskForBundle function
85  * @tc.type: FUNC
86  * @tc.require: IB3SWZ
87  */
88 HWTEST_F(CloudSyncServiceCycleTaskTest, RunTaskForBundleTest001, TestSize.Level1)
89 {
90     GTEST_LOG_(INFO) << "RunTaskForBundleTest001 start";
91     try {
92         string bundleName = "com.ohos.photos";
93         int32_t userId = 100;
94         EXPECT_NE(g_dataSyncManagerPtr_, nullptr);
95         shared_ptr<DatabaseBackupTask> task = make_shared<DatabaseBackupTask>(g_dataSyncManagerPtr_);
96         int32_t ret = task->RunTaskForBundle(userId, bundleName);
97         EXPECT_NE(ret, 0);
98     } catch (...) {
99         EXPECT_TRUE(false);
100         GTEST_LOG_(INFO) << "RunTaskForBundleTest001 FAILED";
101     }
102     GTEST_LOG_(INFO) << "RunTaskForBundleTest001 end";
103 }
104 
105 /**
106  * @tc.name: RunTaskForBundleTest002
107  * @tc.desc: Verify the RunTaskForBundle function
108  * @tc.type: FUNC
109  * @tc.require: IB3SWZ
110  */
111 HWTEST_F(CloudSyncServiceCycleTaskTest, RunTaskForBundleTest002, TestSize.Level1)
112 {
113     GTEST_LOG_(INFO) << "RunTaskForBundleTest002 start";
114     try {
115         string bundleName = "com.ohos.photos";
116         int32_t userId = 100;
117         EXPECT_NE(g_dataSyncManagerPtr_, nullptr);
118         shared_ptr<OptimizeStorageTask> task = make_shared<OptimizeStorageTask>(g_dataSyncManagerPtr_);
119         int32_t ret = task->RunTaskForBundle(userId, bundleName);
120         EXPECT_NE(ret, 0);
121     } catch (...) {
122         EXPECT_TRUE(false);
123         GTEST_LOG_(INFO) << "RunTaskForBundleTest002 FAILED";
124     }
125     GTEST_LOG_(INFO) << "RunTaskForBundleTest002 end";
126 }
127 
128 /**
129  * @tc.name: RunTaskForBundleTest003
130  * @tc.desc: Verify the RunTaskForBundle function
131  * @tc.type: FUNC
132  * @tc.require: IB3SWZ
133  */
134 HWTEST_F(CloudSyncServiceCycleTaskTest, RunTaskForBundleTest003, TestSize.Level1)
135 {
136     GTEST_LOG_(INFO) << "RunTaskForBundleTest003 start";
137     try {
138         string bundleName = "com.ohos.photos";
139         int32_t userId = 100;
140         EXPECT_NE(g_dataSyncManagerPtr_, nullptr);
141         shared_ptr<PeriodicCheckTask> task = make_shared<PeriodicCheckTask>(g_dataSyncManagerPtr_);
142         int32_t ret = task->RunTaskForBundle(userId, bundleName);
143         EXPECT_NE(ret, 0);
144     } catch (...) {
145         EXPECT_TRUE(false);
146         GTEST_LOG_(INFO) << "RunTaskForBundleTest003 FAILED";
147     }
148     GTEST_LOG_(INFO) << "RunTaskForBundleTest003 end";
149 }
150 
151 /**
152  * @tc.name: RunTaskForBundleTest004
153  * @tc.desc: Verify the RunTaskForBundle function
154  * @tc.type: FUNC
155  * @tc.require: IB3SWZ
156  */
157 HWTEST_F(CloudSyncServiceCycleTaskTest, RunTaskForBundleTest004, TestSize.Level1)
158 {
159     GTEST_LOG_(INFO) << "RunTaskForBundleTest004 start";
160     try {
161         string bundleName = "com.ohos.photos";
162         int32_t userId = 100;
163         EXPECT_NE(g_dataSyncManagerPtr_, nullptr);
164         shared_ptr<ReportStatisticsTask> task = make_shared<ReportStatisticsTask>(g_dataSyncManagerPtr_);
165         int32_t ret = task->RunTaskForBundle(userId, bundleName);
166         EXPECT_EQ(ret, 0);
167     } catch (...) {
168         EXPECT_TRUE(false);
169         GTEST_LOG_(INFO) << "RunTaskForBundleTest004 FAILED";
170     }
171     GTEST_LOG_(INFO) << "RunTaskForBundleTest004 end";
172 }
173 
174 /**
175  * @tc.name: RunTaskForBundleTest005
176  * @tc.desc: Verify the RunTaskForBundle function
177  * @tc.type: FUNC
178  * @tc.require: IB3SWZ
179  */
180 HWTEST_F(CloudSyncServiceCycleTaskTest, RunTaskForBundleTest005, TestSize.Level1)
181 {
182     GTEST_LOG_(INFO) << "RunTaskForBundleTest005 start";
183     try {
184         string bundleName = "com.ohos.photos";
185         int32_t userId = 100;
186         EXPECT_NE(g_dataSyncManagerPtr_, nullptr);
187         shared_ptr<SaveSubscriptionTask> task = make_shared<SaveSubscriptionTask>(g_dataSyncManagerPtr_);
188         int32_t ret = task->RunTaskForBundle(userId, bundleName);
189         EXPECT_NE(ret, 0);
190     } catch (...) {
191         EXPECT_TRUE(false);
192         GTEST_LOG_(INFO) << "RunTaskForBundleTest005 FAILED";
193     }
194     GTEST_LOG_(INFO) << "RunTaskForBundleTest005 end";
195 }
196 
197 /**
198  * @tc.name: InitTasksTest001
199  * @tc.desc: Verify the InitTasks function
200  * @tc.type: FUNC
201  * @tc.require: IB3SWZ
202  */
203 HWTEST_F(CloudSyncServiceCycleTaskTest, InitTasksTest001, TestSize.Level1)
204 {
205     GTEST_LOG_(INFO) << "InitTasksTest001 start";
206     try {
207         EXPECT_NE(g_dataSyncManagerPtr_, nullptr);
208         shared_ptr<CycleTaskRunner> taskRunner = make_shared<CycleTaskRunner>(g_dataSyncManagerPtr_);
209         taskRunner->InitTasks();
210     } catch (...) {
211         EXPECT_TRUE(false);
212         GTEST_LOG_(INFO) << "InitTasksTest001 FAILED";
213     }
214     GTEST_LOG_(INFO) << "InitTasksTest001 end";
215 }
216 
217 /**
218  * @tc.name: StartTaskTest001
219  * @tc.desc: Verify the StartTask function
220  * @tc.type: FUNC
221  * @tc.require: IB3SWZ
222  */
223 HWTEST_F(CloudSyncServiceCycleTaskTest, StartTaskTest001, TestSize.Level1)
224 {
225     GTEST_LOG_(INFO) << "StartTaskTest001 start";
226     try {
227         EXPECT_NE(g_dataSyncManagerPtr_, nullptr);
228         shared_ptr<CycleTaskRunner> taskRunner = make_shared<CycleTaskRunner>(g_dataSyncManagerPtr_);
229         GTEST_LOG_(INFO) << "StartTaskTest001 userId_:" << taskRunner->userId_;
230         taskRunner->StartTask();
231     } catch (...) {
232         EXPECT_TRUE(false);
233         GTEST_LOG_(INFO) << "StartTaskTest001 FAILED";
234     }
235     GTEST_LOG_(INFO) << "StartTaskTest001 end";
236 }
237 
238 /**
239  * @tc.name: SetRunableBundleNamesTest001
240  * @tc.desc: Verify the SetRunableBundleNames function
241  * @tc.type: FUNC
242  * @tc.require: IB3SWZ
243  */
244 HWTEST_F(CloudSyncServiceCycleTaskTest, SetRunableBundleNamesTest001, TestSize.Level1)
245 {
246     GTEST_LOG_(INFO) << "SetRunableBundleNamesTest001 start";
247     try {
248         EXPECT_NE(g_dataSyncManagerPtr_, nullptr);
249         shared_ptr<CycleTaskRunner> taskRunner = make_shared<CycleTaskRunner>(g_dataSyncManagerPtr_);
250         taskRunner->SetRunableBundleNames();
251     } catch (...) {
252         EXPECT_TRUE(false);
253         GTEST_LOG_(INFO) << "SetRunableBundleNamesTest001 FAILED";
254     }
255     GTEST_LOG_(INFO) << "SetRunableBundleNamesTest001 end";
256 }
257 
258 /*
259   * @tc.name: CycleTaskRunnerTest
260   * @tc.desc: Verify the CycleTaskRunner function.
261   * @tc.type: FUNC
262   * @tc.require: I6H5MH
263   */
264 HWTEST_F(CloudSyncServiceCycleTaskTest, CycleTaskRunnerTest001, TestSize.Level1)
265 {
266     GTEST_LOG_(INFO) << "CycleTaskRunnerTest001 start";
267     try {
268         shared_ptr<CloudFile::DataSyncManager> dataSyncManager = nullptr;
269         CycleTaskRunner cycleTaskRunner(dataSyncManager);
270         auto res = cycleTaskRunner.dataSyncManager_;
271         EXPECT_EQ(res, nullptr);
272     } catch (...) {
273         EXPECT_TRUE(false);
274         GTEST_LOG_(INFO) << "CycleTaskRunnerTest001 FAILED";
275     }
276     GTEST_LOG_(INFO) << "CycleTaskRunnerTest001 end";
277 }
278 
279  /*
280   * @tc.name: SetLastRunTimeTest001
281   * @tc.desc: Verify the SetLastRunTime function.
282   * @tc.type: FUNC
283   * @tc.require: I6H5MH
284   */
285 HWTEST_F(CloudSyncServiceCycleTaskTest, SetLastRunTimeTest001, TestSize.Level1)
286 {
287     GTEST_LOG_(INFO) << "SetLastRunTimeTest001 start";
288     try {
289         std::string taskName;
290         std::set<std::string> bundleNames;
291         int32_t intervalTime = 0;
292         std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager = nullptr;
293 
294         CycleTask *sycTask = new SubCycleTask(taskName, bundleNames, intervalTime, dataSyncManager);
295 
296         std::time_t time = std::time(nullptr);
297         sycTask->SetLastRunTime(time);
298         EXPECT_EQ(sycTask->cloudPrefImpl_, nullptr);
299         delete sycTask;
300     } catch (...) {
301         EXPECT_TRUE(false);
302         GTEST_LOG_(INFO) << "SetLastRunTimeTest001 FAILED";
303     }
304     GTEST_LOG_(INFO) << "SetLastRunTimeTest001 end";
305 }
306 
307  /*
308   * @tc.name: SetLastRunTimeTest002
309   * @tc.desc: Verify the SetLastRunTime function.
310   * @tc.type: FUNC
311   * @tc.require: I6H5MH
312   */
313 HWTEST_F(CloudSyncServiceCycleTaskTest, SetLastRunTimeTest002, TestSize.Level1)
314 {
315     GTEST_LOG_(INFO) << "SetLastRunTimeTest002 start";
316     try {
317         std::string taskName;
318         std::set<std::string> bundleNames;
319         int32_t intervalTime = 0;
320         std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager = nullptr;
321 
322         CycleTask *sycTask = new SubCycleTask(taskName, bundleNames, intervalTime, dataSyncManager);
323 
324         sycTask->cloudPrefImpl_ = std::make_unique<CloudPrefImpl>("");
325         std::time_t time = std::time(nullptr);
326         sycTask->SetLastRunTime(time);
327         EXPECT_NE(sycTask->cloudPrefImpl_, nullptr);
328         delete sycTask;
329     } catch (...) {
330         EXPECT_TRUE(false);
331         GTEST_LOG_(INFO) << "SetLastRunTimeTest002 FAILED";
332     }
333     GTEST_LOG_(INFO) << "SetLastRunTimeTest002 end";
334 }
335 
336  /*
337   * @tc.name: GetLastRunTimeTest001
338   * @tc.desc: Verify the GetLastRunTime function.
339   * @tc.type: FUNC
340   * @tc.require: I6H5MH
341   */
342 HWTEST_F(CloudSyncServiceCycleTaskTest, GetLastRunTimeTest001, TestSize.Level1)
343 {
344     GTEST_LOG_(INFO) << "GetLastRunTimeTest001 start";
345     try {
346         std::string taskName;
347         std::set<std::string> bundleNames;
348         int32_t intervalTime = 0;
349         std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager = nullptr;
350 
351         CycleTask *sycTask = new SubCycleTask(taskName, bundleNames, intervalTime, dataSyncManager);
352 
353         std::time_t time = std::time(nullptr);
354         sycTask->GetLastRunTime(time);
355         EXPECT_EQ(sycTask->cloudPrefImpl_, nullptr);
356         delete sycTask;
357     } catch (...) {
358         EXPECT_TRUE(false);
359         GTEST_LOG_(INFO) << "GetLastRunTimeTest001 FAILED";
360     }
361     GTEST_LOG_(INFO) << "GetLastRunTimeTest001 end";
362 }
363 
364  /*
365   * @tc.name: GetLastRunTimeTest002
366   * @tc.desc: Verify the GetLastRunTime function.
367   * @tc.type: FUNC
368   * @tc.require: I6H5MH
369   */
370 HWTEST_F(CloudSyncServiceCycleTaskTest, GetLastRunTimeTest002, TestSize.Level1)
371 {
372     GTEST_LOG_(INFO) << "GetLastRunTimeTest002 start";
373     try {
374         std::string taskName;
375         std::set<std::string> bundleNames;
376         int32_t intervalTime = 0;
377         std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager = nullptr;
378 
379         CycleTask *sycTask = new SubCycleTask(taskName, bundleNames, intervalTime, dataSyncManager);
380 
381         sycTask->cloudPrefImpl_ = std::make_unique<CloudPrefImpl>("");
382         std::time_t time = std::time(nullptr);
383         sycTask->GetLastRunTime(time);
384         EXPECT_NE(sycTask->cloudPrefImpl_, nullptr);
385         delete sycTask;
386     } catch (...) {
387         EXPECT_TRUE(false);
388         GTEST_LOG_(INFO) << "GetLastRunTimeTest002 FAILED";
389     }
390     GTEST_LOG_(INFO) << "GetLastRunTimeTest002 end";
391 }
392 
393  /*
394   * @tc.name: IsEligibleToRunTest001
395   * @tc.desc: Verify the IsEligibleToRun function.
396   * @tc.type: FUNC
397   * @tc.require: I6H5MH
398   */
399 HWTEST_F(CloudSyncServiceCycleTaskTest, IsEligibleToRunTest001, TestSize.Level1)
400 {
401     GTEST_LOG_(INFO) << "IsEligibleToRunTest001 start";
402     try {
403         std::string taskName;
404         std::set<std::string> bundleNames;
405         int32_t intervalTime = 0;
406         std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager = nullptr;
407         CycleTask *sycTask = new SubCycleTask(taskName, bundleNames, intervalTime, dataSyncManager);
408 
409         std::time_t currentTime = std::time(nullptr);
410         std::string bundleName;
411         bool res = sycTask->IsEligibleToRun(currentTime, bundleName);
412         EXPECT_EQ(res, true);
413         delete sycTask;
414     } catch (...) {
415         EXPECT_TRUE(false);
416         GTEST_LOG_(INFO) << "IsEligibleToRunTest001 FAILED";
417     }
418     GTEST_LOG_(INFO) << "IsEligibleToRunTest001 end";
419 }
420 
421  /*
422   * @tc.name: IsEligibleToRunTest002
423   * @tc.desc: Verify the IsEligibleToRun function.
424   * @tc.type: FUNC
425   * @tc.require: I6H5MH
426   */
427 HWTEST_F(CloudSyncServiceCycleTaskTest, IsEligibleToRunTest002, TestSize.Level1)
428 {
429     GTEST_LOG_(INFO) << "IsEligibleToRunTest002 start";
430     try {
431         std::string taskName;
432         std::set<std::string> bundleNames = {"test"};
433         int32_t intervalTime = 0;
434         std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager = nullptr;
435         CycleTask *sycTask = new SubCycleTask(taskName, bundleNames, intervalTime, dataSyncManager);
436 
437         std::time_t currentTime = std::time(nullptr);
438         std::string bundleName = "";
439         bool res = sycTask->IsEligibleToRun(currentTime, bundleName);
440         EXPECT_EQ(res, false);
441         delete sycTask;
442     } catch (...) {
443         EXPECT_TRUE(false);
444         GTEST_LOG_(INFO) << "IsEligibleToRunTest002 FAILED";
445     }
446     GTEST_LOG_(INFO) << "IsEligibleToRunTest002 end";
447 }
448 
449  /*
450   * @tc.name: RunTaskTest001
451   * @tc.desc: Verify the RunTask function.
452   * @tc.type: FUNC
453   * @tc.require: I6H5MH
454   */
455 HWTEST_F(CloudSyncServiceCycleTaskTest, RunTaskTest001, TestSize.Level1)
456 {
457     GTEST_LOG_(INFO) << "RunTaskTest001 start";
458     try {
459         std::string taskName;
460         std::set<std::string> bundleNames = {"test"};
461         int32_t intervalTime = 0;
462         std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager = nullptr;
463         CycleTask *sycTask = new SubCycleTask(taskName, bundleNames, intervalTime, dataSyncManager);
464 
465         int32_t userId = 0;
466         sycTask->RunTask(userId);
467         EXPECT_EQ(sycTask->runnableBundleNames_, nullptr);
468         delete sycTask;
469     } catch (...) {
470         EXPECT_TRUE(false);
471         GTEST_LOG_(INFO) << "RunTaskTest001 FAILED";
472     }
473     GTEST_LOG_(INFO) << "RunTaskTest001 end";
474 }
475 }
476 }
477 }