• 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 #define LOG_TAG "ObjectAssetLoaderTest"
17 
18 #include "object_asset_loader.h"
19 
20 #include <gtest/gtest.h>
21 
22 #include "executor_pool.h"
23 #include "object_common.h"
24 #include "snapshot/machine_status.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::DistributedObject;
28 using namespace OHOS::DistributedData;
29 namespace OHOS::Test {
30 
31 class ObjectAssetLoaderTest : public testing::Test {
32 public:
33     void SetUp();
34     void TearDown();
35 
36 protected:
37     Asset asset_;
38     std::string uri_;
39     int32_t userId_ = 1;
40     std::string bundleName_ = "test_bundleName_1";
41     std::string deviceId_ = "test_deviceId__1";
42 };
43 
SetUp()44 void ObjectAssetLoaderTest::SetUp()
45 {
46     uri_ = "file:://com.example.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
47     Asset asset{
48         .name = "test_name",
49         .uri = uri_,
50         .modifyTime = "modifyTime",
51         .size = "size",
52         .hash = "modifyTime_size",
53     };
54     asset_ = asset;
55 }
56 
TearDown()57 void ObjectAssetLoaderTest::TearDown() {}
58 
59 /**
60 * @tc.name: UploadTest001
61 * @tc.desc: Transfer test.
62 * @tc.type: FUNC
63 * @tc.require:
64 * @tc.author: wangbin
65 */
66 HWTEST_F(ObjectAssetLoaderTest, UploadTest001, TestSize.Level0)
67 {
68     auto &assetLoader = ObjectAssetLoader::GetInstance();
69     auto result = assetLoader.Transfer(userId_, bundleName_, deviceId_, asset_);
70     ASSERT_EQ(result, false);
71 }
72 
73 /**
74 * @tc.name: TransferAssetsAsync001
75 * @tc.desc: TransferAssetsAsync test.
76 * @tc.type: FUNC
77 * @tc.require:
78 * @tc.author: wangbin
79 */
80 HWTEST_F(ObjectAssetLoaderTest, TransferAssetsAsync001, TestSize.Level0)
81 {
82     auto &assetLoader = ObjectAssetLoader::GetInstance();
__anon61cd533d0102(bool success) 83     std::function<void(bool)> lambdaFunc = [](bool success) {
84         if (success) {}
85     };
86     std::vector<Asset> assets{ asset_ };
87     ASSERT_EQ(assetLoader.executors_, nullptr);
88     assetLoader.TransferAssetsAsync(userId_, bundleName_, deviceId_, assets, lambdaFunc);
89 }
90 
91 /**
92 * @tc.name: TransferAssetsAsync002
93 * @tc.desc: TransferAssetsAsync test.
94 * @tc.type: FUNC
95 * @tc.require:
96 * @tc.author: wangbin
97 */
98 HWTEST_F(ObjectAssetLoaderTest, TransferAssetsAsync002, TestSize.Level0)
99 {
100     auto &assetLoader = ObjectAssetLoader::GetInstance();
__anon61cd533d0202(bool success) 101     std::function<void(bool)> lambdaFunc = [](bool success) {
102         if (success) {}
103     };
104     std::vector<Asset> assets{ asset_ };
105     std::shared_ptr<ExecutorPool> executors = std::make_shared<ExecutorPool>(5, 3);
106     ASSERT_NE(executors, nullptr);
107     assetLoader.SetThreadPool(executors);
108     ASSERT_NE(assetLoader.executors_, nullptr);
109     assetLoader.TransferAssetsAsync(userId_, bundleName_, deviceId_, assets, lambdaFunc);
110 }
111 
112 /**
113 * @tc.name: FinishTask001
114 * @tc.desc: FinishTask test.
115 * @tc.type: FUNC
116 * @tc.require:
117 * @tc.author: wangbin
118 */
119 HWTEST_F(ObjectAssetLoaderTest, FinishTask001, TestSize.Level0)
120 {
121     auto &assetLoader = ObjectAssetLoader::GetInstance();
122     TransferTask task;
123     task.downloadAssets.insert(uri_);
124     assetLoader.FinishTask(asset_.uri, true);
125     ASSERT_TRUE(assetLoader.tasks_.Empty());
126     assetLoader.FinishTask(asset_.uri, false);
127     ASSERT_TRUE(assetLoader.tasks_.Empty());
128 }
129 
130 /**
131 * @tc.name: IsDownloading001
132 * @tc.desc: IsDownloading test.
133 * @tc.type: FUNC
134 * @tc.require:
135 * @tc.author: wangbin
136 */
137 HWTEST_F(ObjectAssetLoaderTest, IsDownloading001, TestSize.Level0)
138 {
139     auto &assetLoader = ObjectAssetLoader::GetInstance();
140     assetLoader.downloading_.InsertOrAssign(asset_.uri, asset_.hash);
141     auto result = assetLoader.IsDownloading(asset_);
142     ASSERT_EQ(result, true);
143     assetLoader.downloading_.Erase(asset_.uri);
144     result = assetLoader.IsDownloading(asset_);
145     ASSERT_EQ(result, false);
146 }
147 
148 /**
149 * @tc.name: IsDownloaded001
150 * @tc.desc: IsDownloaded test.
151 * @tc.type: FUNC
152 * @tc.require:
153 * @tc.author: wangbin
154 */
155 HWTEST_F(ObjectAssetLoaderTest, IsDownloaded001, TestSize.Level0)
156 {
157     auto &assetLoader = ObjectAssetLoader::GetInstance();
158     auto result = assetLoader.IsDownloaded(asset_);
159     ASSERT_EQ(result, false);
160     assetLoader.downloaded_.Insert(asset_.uri, "modifyTime_size");
161     result = assetLoader.IsDownloaded(asset_);
162     ASSERT_EQ(result, true);
163 }
164 
165 /**
166 * @tc.name: UpdateDownloaded001
167 * @tc.desc: UpdateDownloaded test.
168 * @tc.type: FUNC
169 * @tc.require:
170 * @tc.author: wangbin
171 */
172 HWTEST_F(ObjectAssetLoaderTest, UpdateDownloaded001, TestSize.Level0)
173 {
174     auto &assetLoader = ObjectAssetLoader::GetInstance();
175     while (!assetLoader.assetQueue_.empty()) {
176         assetLoader.assetQueue_.pop();
177     }
178     assetLoader.UpdateDownloaded(asset_);
179     auto [success, hash] = assetLoader.downloaded_.Find(asset_.uri);
180     ASSERT_TRUE(success);
181     EXPECT_EQ(hash, asset_.hash);
182 }
183 
184 /**
185 * @tc.name: UpdateDownloaded002
186 * @tc.desc: UpdateDownloaded test.
187 * @tc.type: FUNC
188 * @tc.require:
189 * @tc.author: wangbin
190 */
191 HWTEST_F(ObjectAssetLoaderTest, UpdateDownloaded002, TestSize.Level0)
192 {
193     auto &assetLoader = ObjectAssetLoader::GetInstance();
194     while (!assetLoader.assetQueue_.empty()) {
195         assetLoader.assetQueue_.pop();
196     }
197     for (int i = 0; i <= assetLoader.LAST_DOWNLOAD_ASSET_SIZE; i++) {
198         assetLoader.assetQueue_.push(asset_.uri);
199     }
200     assetLoader.UpdateDownloaded(asset_);
201     EXPECT_NE(assetLoader.assetQueue_.size(), ObjectAssetLoader::LAST_DOWNLOAD_ASSET_SIZE);
202     EXPECT_EQ(assetLoader.assetQueue_.size(), ObjectAssetLoader::LAST_DOWNLOAD_ASSET_SIZE + 1);
203     auto [success, hash] = assetLoader.downloaded_.Find(asset_.uri);
204     EXPECT_EQ(success, false);
205     EXPECT_EQ(hash, "");
206 }
207 
208 /**
209 * @tc.name: PushAsset001
210 * @tc.desc: PushAsset test.
211 * @tc.type: FUNC
212 */
213 HWTEST_F(ObjectAssetLoaderTest, PushAsset001, TestSize.Level0)
214 {
215     auto &assetLoader = ObjectAssetLoader::GetInstance();
216     sptr<AssetObj> assetObj = new AssetObj();
217     assetObj->dstBundleName_ = bundleName_;
218     assetObj->srcBundleName_ = bundleName_;
219     assetObj->dstNetworkId_ = "1";
220     assetObj->sessionId_ = "123";
221 
222     sptr<ObjectAssetsSendListener> sendCallback = new ObjectAssetsSendListener();
223     ASSERT_NE(sendCallback, nullptr);
224     int32_t ret = assetLoader.PushAsset(userId_, assetObj, sendCallback);
225     EXPECT_NE(ret, DistributedObject::OBJECT_SUCCESS);
226 }
227 
228 /**
229 * @tc.name: PushAsset002
230 * @tc.desc: PushAsset test.
231 * @tc.type: FUNC
232 */
233 HWTEST_F(ObjectAssetLoaderTest, PushAsset002, TestSize.Level0)
234 {
235     auto &assetLoader = ObjectAssetLoader::GetInstance();
236     int32_t ret = assetLoader.PushAsset(userId_, nullptr, nullptr);
237     EXPECT_EQ(ret, DistributedObject::OBJECT_INNER_ERROR);
238 }
239 
240 /**
241 * @tc.name: OnSendResult001
242 * @tc.desc: OnSendResult test.
243 * @tc.type: FUNC
244 */
245 HWTEST_F(ObjectAssetLoaderTest, OnSendResult001, TestSize.Level1)
246 {
247     sptr<AssetObj> assetObj = nullptr;
248 
249     int32_t result = -1;
250     sptr<ObjectAssetsSendListener> sendCallback = new ObjectAssetsSendListener();
251     int32_t ret = sendCallback->OnSendResult(assetObj, result);
252     EXPECT_EQ(ret, result);
253 
254     assetObj = new AssetObj();
255     assetObj->dstBundleName_ = bundleName_;
256     assetObj->srcBundleName_ = bundleName_;
257     assetObj->dstNetworkId_ = "1";
258     assetObj->sessionId_ = "123";
259 
260     ret = sendCallback->OnSendResult(assetObj, result);
261     EXPECT_EQ(ret, result);
262 
263     result = 0;
264     ret = sendCallback->OnSendResult(assetObj, result);
265     EXPECT_EQ(ret, result);
266 }
267 } // namespace OHOS::Test
268