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 <gtest/gtest.h>
17 #include <memory>
18
19 #include "cloud_download_callback_manager.h"
20 #include "data_handler_mock.h"
21 #include "dfs_error.h"
22 #include "iservice_registry.h"
23
24 namespace OHOS {
25 namespace FileManagement::CloudSync {
26 namespace Test {
27 using namespace testing::ext;
28 using namespace testing;
29 using namespace std;
30
31 class CloudDownloadCallbackManagerTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
39 class MockICloudDownloadCallback : public ICloudDownloadCallback {
40 public:
OnDownloadProcess(DownloadProgressObj & progress)41 void OnDownloadProcess(DownloadProgressObj& progress) {return;}
AsObject()42 sptr<IRemoteObject> AsObject() {return nullptr;}
43 };
44
SetUpTestCase(void)45 void CloudDownloadCallbackManagerTest::SetUpTestCase(void)
46 {
47 std::cout << "SetUpTestCase" << std::endl;
48 }
49
TearDownTestCase(void)50 void CloudDownloadCallbackManagerTest::TearDownTestCase(void)
51 {
52 std::cout << "TearDownTestCase" << std::endl;
53 }
54
SetUp(void)55 void CloudDownloadCallbackManagerTest::SetUp(void)
56 {
57 std::cout << "SetUp" << std::endl;
58 }
59
TearDown(void)60 void CloudDownloadCallbackManagerTest::TearDown(void)
61 {
62 std::cout << "TearDown" << std::endl;
63 }
64
65 /**
66 * @tc.name: StartDonwloadTest
67 * @tc.desc: Verify the StartDonwloadTest function.
68 * @tc.type: FUNC
69 * @tc.require: I6H5MH
70 */
71 HWTEST_F(CloudDownloadCallbackManagerTest, StartDonwloadTest, TestSize.Level1)
72 {
73 GTEST_LOG_(INFO) << "StartDonwloadTest Start";
74 try {
75 auto cloudDownloadCallbackManager =std::make_shared<CloudDownloadCallbackManager>();
76 std::string path = "data/";
77 int32_t userId = 1;
78 cloudDownloadCallbackManager->StartDonwload(path, userId);
79 EXPECT_TRUE(true);
80 } catch (...) {
81 EXPECT_TRUE(false);
82 GTEST_LOG_(INFO) << " StartDonwloadTest FAILED";
83 }
84 GTEST_LOG_(INFO) << "StartDonwloadTest End";
85 }
86
87 /**
88 * @tc.name: StopDonwloadTest
89 * @tc.desc: Verify the StopDonwloadTest function.
90 * @tc.type: FUNC
91 * @tc.require: I6H5MH
92 */
93 HWTEST_F(CloudDownloadCallbackManagerTest, StopDonwloadTest, TestSize.Level1)
94 {
95 GTEST_LOG_(INFO) << "StopDonwloadTest Start";
96 try {
97 CloudDownloadCallbackManager cloudDownloadCallbackManager;
98 std::string path = "data/";
99 int32_t userId = 1;
100 cloudDownloadCallbackManager.StopDonwload(path, userId);
101
102 DownloadProgressObj downloadProgressObj;
103 cloudDownloadCallbackManager.downloads_.insert({path, downloadProgressObj});
104 cloudDownloadCallbackManager.StopDonwload(path, userId);
105 EXPECT_TRUE(true);
106 } catch (...) {
107 EXPECT_TRUE(false);
108 GTEST_LOG_(INFO) << " StopDonwloadTest FAILED";
109 }
110 GTEST_LOG_(INFO) << "StopDonwloadTest End";
111 }
112
113 /**
114 * @tc.name: RegisterCallbackTest
115 * @tc.desc: Verify the RegisterCallbackTest function.
116 * @tc.type: FUNC
117 * @tc.require: I6H5MH
118 */
119 HWTEST_F(CloudDownloadCallbackManagerTest, RegisterCallbackTest, TestSize.Level1)
120 {
121 GTEST_LOG_(INFO) << "RegisterCallbackTest Start";
122 try {
123 CloudDownloadCallbackManager cloudDownloadCallbackManager;
124 int32_t userId = 1;
125 sptr<MockICloudDownloadCallback> callback = sptr(new MockICloudDownloadCallback());
126 cloudDownloadCallbackManager.RegisterCallback(userId, callback);
127 EXPECT_TRUE(true);
128 } catch (...) {
129 EXPECT_TRUE(false);
130 GTEST_LOG_(INFO) << " RegisterCallbackTest FAILED";
131 }
132 GTEST_LOG_(INFO) << "RegisterCallbackTest End";
133 }
134
135 /**
136 * @tc.name: UnregisterCallbackTest
137 * @tc.desc: Verify the UnregisterCallbackTest function.
138 * @tc.type: FUNC
139 * @tc.require: I6H5MH
140 */
141 HWTEST_F(CloudDownloadCallbackManagerTest, UnregisterCallbackTest, TestSize.Level1)
142 {
143 GTEST_LOG_(INFO) << "UnregisterCallbackTest Start";
144 try {
145 CloudDownloadCallbackManager cloudDownloadCallbackManager;
146 int32_t userId = 1;
147 cloudDownloadCallbackManager.UnregisterCallback(userId);
148 EXPECT_TRUE(true);
149 } catch (...) {
150 EXPECT_TRUE(false);
151 GTEST_LOG_(INFO) << " UnregisterCallbackTest FAILED";
152 }
153 GTEST_LOG_(INFO) << "UnregisterCallbackTest End";
154 }
155
156 /**
157 * @tc.name: OnDownloadedResultTest
158 * @tc.desc: Verify the OnDownloadedResultTest function.
159 * @tc.type: FUNC
160 * @tc.require: I6H5MH
161 */
162 HWTEST_F(CloudDownloadCallbackManagerTest, OnDownloadedResultTest, TestSize.Level1)
163 {
164 GTEST_LOG_(INFO) << "OnDownloadedResultTest Start";
165 try {
166 CloudDownloadCallbackManager cloudDownloadCallbackManager;
167
168 DriveKit::DKAppBundleName bundleName = "bundleName";
169 DriveKit::DKContainerName containerName = "containerName";
170 int32_t userId = 0;
171 auto driveKit = std::make_shared<DriveKit::DriveKitNative>(userId);
172 auto container = std::make_shared<DriveKit::DKContainer>(bundleName, containerName, driveKit);
173 DriveKit::DKDatabaseScope scope;
174
175 std::string path = "data/";
176 std::vector<DriveKit::DKDownloadAsset> assetsToDownload;
177 auto handler = std::make_shared<DataHandlerMock>();
178 auto context = std::make_shared<DriveKit::DKContext>();
179 auto database = std::make_shared<DriveKit::DKDatabase>(container, scope);
180 std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> results;
181 DriveKit::DKError err;
182 DownloadProgressObj downloadProgressObj;
183 cloudDownloadCallbackManager.downloads_.insert({path, downloadProgressObj});
184 cloudDownloadCallbackManager.OnDownloadedResult(path, assetsToDownload, handler, context, database, results,
185 err);
186 EXPECT_TRUE(true);
187 } catch (...) {
188 EXPECT_TRUE(false);
189 GTEST_LOG_(INFO) << " OnDownloadedResultTest FAILED";
190 }
191 GTEST_LOG_(INFO) << "OnDownloadedResultTest End";
192 }
193
194 /**
195 * @tc.name: OnDownloadProcessTest
196 * @tc.desc: Verify the OnDownloadProcessTest function.
197 * @tc.type: FUNC
198 * @tc.require: I6H5MH
199 */
200 HWTEST_F(CloudDownloadCallbackManagerTest, OnDownloadProcessTest, TestSize.Level1)
201 {
202 GTEST_LOG_(INFO) << "OnDownloadProcessTest Start";
203 try {
204 CloudDownloadCallbackManager cloudDownloadCallbackManager;
205 std::string path = "data";
206 auto context = std::make_shared<DriveKit::DKContext>();
207 DriveKit::DKDownloadAsset asset;
208 DriveKit::TotalSize totalSize = 1024;
209 DriveKit::DownloadSize downloadSize = 1024;
210 DownloadProgressObj downloadProgressObj;
211 cloudDownloadCallbackManager.downloads_.insert({path, downloadProgressObj});
212 cloudDownloadCallbackManager.OnDownloadProcess(path, context, asset, totalSize, downloadSize);
213 EXPECT_TRUE(true);
214 } catch (...) {
215 EXPECT_TRUE(false);
216 GTEST_LOG_(INFO) << " OnDownloadProcessTest FAILED";
217 }
218 GTEST_LOG_(INFO) << "OnDownloadProcessTest End";
219 }
220 } // namespace Test
221 } // namespace FileManagement::CloudSync {
222 } // namespace OHOS
223