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 #include <gtest/gtest.h>
20 #include "snapshot/machine_status.h"
21 #include "executor_pool.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS::DistributedObject;
25 using namespace OHOS::DistributedData;
26 namespace OHOS::Test {
27 
28 class ObjectAssetLoaderTest : public testing::Test {
29 public:
30     void SetUp();
31     void TearDown();
32 
33 protected:
34     Asset asset_;
35     std::string uri_;
36     int32_t userId_ = 1;
37     std::string bundleName_ = "test_bundleName_1";
38     std::string deviceId_ = "test_deviceId__1";
39 };
40 
SetUp()41 void ObjectAssetLoaderTest::SetUp()
42 {
43     uri_ = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
44     Asset asset{
45         .name = "test_name",
46         .uri = uri_,
47         .modifyTime = "modifyTime",
48         .size = "size",
49         .hash = "modifyTime_size",
50     };
51     asset_ = asset;
52 }
53 
TearDown()54 void ObjectAssetLoaderTest::TearDown() {}
55 
56 /**
57 * @tc.name: UploadTest001
58 * @tc.desc: Transfer test.
59 * @tc.type: FUNC
60 * @tc.require:
61 * @tc.author: wangbin
62 */
63 HWTEST_F(ObjectAssetLoaderTest, UploadTest001, TestSize.Level0)
64 {
65     auto assetLoader = ObjectAssetLoader::GetInstance();
66     auto result = assetLoader->Transfer(userId_, bundleName_, deviceId_, asset_);
67     ASSERT_EQ(result, false);
68 }
69 
70 /**
71 * @tc.name: TransferAssetsAsync001
72 * @tc.desc: TransferAssetsAsync test.
73 * @tc.type: FUNC
74 * @tc.require:
75 * @tc.author: wangbin
76 */
77 HWTEST_F(ObjectAssetLoaderTest, TransferAssetsAsync001, TestSize.Level0)
78 {
79     auto assetLoader = ObjectAssetLoader::GetInstance();
__anonc13185930102(bool success) 80     std::function<void(bool)> lambdaFunc = [](bool success) {
81         if (success) {}
82     };
83     std::vector<Asset> assets{ asset_ };
84     ASSERT_EQ(assetLoader->executors_, nullptr);
85     assetLoader->TransferAssetsAsync(userId_, bundleName_, deviceId_, assets, lambdaFunc);
86 }
87 
88 /**
89 * @tc.name: TransferAssetsAsync002
90 * @tc.desc: TransferAssetsAsync test.
91 * @tc.type: FUNC
92 * @tc.require:
93 * @tc.author: wangbin
94 */
95 HWTEST_F(ObjectAssetLoaderTest, TransferAssetsAsync002, TestSize.Level0)
96 {
97     auto assetLoader = ObjectAssetLoader::GetInstance();
__anonc13185930202(bool success) 98     std::function<void(bool)> lambdaFunc = [](bool success) {
99         if (success) {}
100     };
101     std::vector<Asset> assets{ asset_ };
102     std::shared_ptr<ExecutorPool> executors = std::make_shared<ExecutorPool>(5, 3);
103     ASSERT_NE(executors, nullptr);
104     assetLoader->SetThreadPool(executors);
105     ASSERT_NE(assetLoader->executors_, nullptr);
106     assetLoader->TransferAssetsAsync(userId_, bundleName_, deviceId_, assets, lambdaFunc);
107 }
108 
109 /**
110 * @tc.name: FinishTask001
111 * @tc.desc: FinishTask test.
112 * @tc.type: FUNC
113 * @tc.require:
114 * @tc.author: wangbin
115 */
116 HWTEST_F(ObjectAssetLoaderTest, FinishTask001, TestSize.Level0)
117 {
118     auto assetLoader = ObjectAssetLoader::GetInstance();
119     ASSERT_NE(assetLoader, nullptr);
120     TransferTask task;
121     task.downloadAssets.insert(uri_);
122     assetLoader->FinishTask(asset_.uri, true);
123     ASSERT_TRUE(assetLoader->tasks_.Empty());
124     assetLoader->FinishTask(asset_.uri, false);
125     ASSERT_TRUE(assetLoader->tasks_.Empty());
126 }
127 
128 /**
129 * @tc.name: IsDownloading001
130 * @tc.desc: IsDownloading test.
131 * @tc.type: FUNC
132 * @tc.require:
133 * @tc.author: wangbin
134 */
135 HWTEST_F(ObjectAssetLoaderTest, IsDownloading001, TestSize.Level0)
136 {
137     auto assetLoader = ObjectAssetLoader::GetInstance();
138     assetLoader->downloading_.InsertOrAssign(asset_.uri, asset_.hash);
139     auto result = assetLoader->IsDownloading(asset_);
140     ASSERT_EQ(result, true);
141     assetLoader->downloading_.Erase(asset_.uri);
142     result = assetLoader->IsDownloading(asset_);
143     ASSERT_EQ(result, false);
144 }
145 
146 /**
147 * @tc.name: IsDownloaded001
148 * @tc.desc: IsDownloaded test.
149 * @tc.type: FUNC
150 * @tc.require:
151 * @tc.author: wangbin
152 */
153 HWTEST_F(ObjectAssetLoaderTest, IsDownloaded001, TestSize.Level0)
154 {
155     auto assetLoader = ObjectAssetLoader::GetInstance();
156     auto result = assetLoader->IsDownloaded(asset_);
157     ASSERT_EQ(result, false);
158     assetLoader->downloaded_.Insert(asset_.uri, "modifyTime_size");
159     result = assetLoader->IsDownloaded(asset_);
160     ASSERT_EQ(result, true);
161 }
162 
163 /**
164 * @tc.name: UpdateDownloaded001
165 * @tc.desc: UpdateDownloaded test.
166 * @tc.type: FUNC
167 * @tc.require:
168 * @tc.author: wangbin
169 */
170 HWTEST_F(ObjectAssetLoaderTest, UpdateDownloaded001, TestSize.Level0)
171 {
172     auto assetLoader = ObjectAssetLoader::GetInstance();
173     ASSERT_NE(assetLoader, nullptr);
174     while (!assetLoader->assetQueue_.empty()) {
175         assetLoader->assetQueue_.pop();
176     }
177     assetLoader->UpdateDownloaded(asset_);
178     auto [success, hash] = assetLoader->downloaded_.Find(asset_.uri);
179     ASSERT_TRUE(success);
180     EXPECT_EQ(hash, asset_.hash);
181 }
182 
183 /**
184 * @tc.name: UpdateDownloaded002
185 * @tc.desc: UpdateDownloaded test.
186 * @tc.type: FUNC
187 * @tc.require:
188 * @tc.author: wangbin
189 */
190 HWTEST_F(ObjectAssetLoaderTest, UpdateDownloaded002, TestSize.Level0)
191 {
192     auto assetLoader = ObjectAssetLoader::GetInstance();
193     ASSERT_NE(assetLoader, nullptr);
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 } // namespace OHOS::Test
208