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 "snapshot/machine_status.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::DistributedObject;
27 using namespace OHOS::DistributedData;
28 namespace OHOS::Test {
29
30 class ObjectAssetLoaderTest : public testing::Test {
31 public:
32 void SetUp();
33 void TearDown();
34
35 protected:
36 Asset asset_;
37 std::string uri_;
38 int32_t userId_ = 1;
39 std::string bundleName_ = "test_bundleName_1";
40 std::string deviceId_ = "test_deviceId__1";
41 };
42
SetUp()43 void ObjectAssetLoaderTest::SetUp()
44 {
45 uri_ = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
46 Asset asset{
47 .name = "test_name",
48 .uri = uri_,
49 .modifyTime = "modifyTime",
50 .size = "size",
51 .hash = "modifyTime_size",
52 };
53 asset_ = asset;
54 }
55
TearDown()56 void ObjectAssetLoaderTest::TearDown() {}
57
58 /**
59 * @tc.name: UploadTest001
60 * @tc.desc: Transfer test.
61 * @tc.type: FUNC
62 * @tc.require:
63 * @tc.author: wangbin
64 */
HWTEST_F(ObjectAssetLoaderTest, UploadTest001, TestSize.Level0)65 HWTEST_F(ObjectAssetLoaderTest, UploadTest001, TestSize.Level0)
66 {
67 auto assetLoader = ObjectAssetLoader::GetInstance();
68 auto result = assetLoader->Transfer(userId_, bundleName_, deviceId_, asset_);
69 ASSERT_EQ(result, false);
70 }
71
72 /**
73 * @tc.name: TransferAssetsAsync001
74 * @tc.desc: TransferAssetsAsync test.
75 * @tc.type: FUNC
76 * @tc.require:
77 * @tc.author: wangbin
78 */
HWTEST_F(ObjectAssetLoaderTest, TransferAssetsAsync001, TestSize.Level0)79 HWTEST_F(ObjectAssetLoaderTest, TransferAssetsAsync001, TestSize.Level0)
80 {
81 auto assetLoader = ObjectAssetLoader::GetInstance();
82 std::function<void(bool)> lambdaFunc = [](bool success) {
83 if (success) {}
84 };
85 std::vector<Asset> assets{ asset_ };
86 ASSERT_EQ(assetLoader->executors_, nullptr);
87 assetLoader->TransferAssetsAsync(userId_, bundleName_, deviceId_, assets, lambdaFunc);
88 }
89
90 /**
91 * @tc.name: TransferAssetsAsync002
92 * @tc.desc: TransferAssetsAsync test.
93 * @tc.type: FUNC
94 * @tc.require:
95 * @tc.author: wangbin
96 */
HWTEST_F(ObjectAssetLoaderTest, TransferAssetsAsync002, TestSize.Level0)97 HWTEST_F(ObjectAssetLoaderTest, TransferAssetsAsync002, TestSize.Level0)
98 {
99 auto assetLoader = ObjectAssetLoader::GetInstance();
100 std::function<void(bool)> lambdaFunc = [](bool success) {
101 if (success) {}
102 };
103 std::vector<Asset> assets{ asset_ };
104 std::shared_ptr<ExecutorPool> executors = std::make_shared<ExecutorPool>(5, 3);
105 ASSERT_NE(executors, nullptr);
106 assetLoader->SetThreadPool(executors);
107 ASSERT_NE(assetLoader->executors_, nullptr);
108 assetLoader->TransferAssetsAsync(userId_, bundleName_, deviceId_, assets, lambdaFunc);
109 }
110
111 /**
112 * @tc.name: FinishTask001
113 * @tc.desc: FinishTask test.
114 * @tc.type: FUNC
115 * @tc.require:
116 * @tc.author: wangbin
117 */
HWTEST_F(ObjectAssetLoaderTest, FinishTask001, TestSize.Level0)118 HWTEST_F(ObjectAssetLoaderTest, FinishTask001, TestSize.Level0)
119 {
120 auto assetLoader = ObjectAssetLoader::GetInstance();
121 ASSERT_NE(assetLoader, nullptr);
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 */
HWTEST_F(ObjectAssetLoaderTest, IsDownloading001, TestSize.Level0)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 */
HWTEST_F(ObjectAssetLoaderTest, IsDownloaded001, TestSize.Level0)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 */
HWTEST_F(ObjectAssetLoaderTest, UpdateDownloaded001, TestSize.Level0)172 HWTEST_F(ObjectAssetLoaderTest, UpdateDownloaded001, TestSize.Level0)
173 {
174 auto assetLoader = ObjectAssetLoader::GetInstance();
175 ASSERT_NE(assetLoader, nullptr);
176 while (!assetLoader->assetQueue_.empty()) {
177 assetLoader->assetQueue_.pop();
178 }
179 assetLoader->UpdateDownloaded(asset_);
180 auto [success, hash] = assetLoader->downloaded_.Find(asset_.uri);
181 ASSERT_TRUE(success);
182 EXPECT_EQ(hash, asset_.hash);
183 }
184
185 /**
186 * @tc.name: UpdateDownloaded002
187 * @tc.desc: UpdateDownloaded test.
188 * @tc.type: FUNC
189 * @tc.require:
190 * @tc.author: wangbin
191 */
HWTEST_F(ObjectAssetLoaderTest, UpdateDownloaded002, TestSize.Level0)192 HWTEST_F(ObjectAssetLoaderTest, UpdateDownloaded002, TestSize.Level0)
193 {
194 auto assetLoader = ObjectAssetLoader::GetInstance();
195 ASSERT_NE(assetLoader, nullptr);
196 while (!assetLoader->assetQueue_.empty()) {
197 assetLoader->assetQueue_.pop();
198 }
199 for (int i = 0; i <= assetLoader->LAST_DOWNLOAD_ASSET_SIZE; i++) {
200 assetLoader->assetQueue_.push(asset_.uri);
201 }
202 assetLoader->UpdateDownloaded(asset_);
203 EXPECT_NE(assetLoader->assetQueue_.size(), ObjectAssetLoader::LAST_DOWNLOAD_ASSET_SIZE);
204 EXPECT_EQ(assetLoader->assetQueue_.size(), ObjectAssetLoader::LAST_DOWNLOAD_ASSET_SIZE + 1);
205 auto [success, hash] = assetLoader->downloaded_.Find(asset_.uri);
206 EXPECT_EQ(success, false);
207 EXPECT_EQ(hash, "");
208 }
209 } // namespace OHOS::Test
210