• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cloud_sync_asset_manager_impl.h"
17 #include "service_proxy.h"
18 #include "service_proxy_mock.h"
19 #include "dfs_error.h"
20 #include "utils_log.h"
21 
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 
25 namespace OHOS {
26 namespace FileManagement::CloudSync {
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace std;
30 
31 class CloudSyncAssetManagerImplTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
35     void SetUp();
36     void TearDown();
37     static inline std::shared_ptr<MockServiceProxy> proxy_ = nullptr;
38     static inline sptr<CloudSyncServiceMock> serviceProxy_ = nullptr;
39 };
40 
SetUpTestCase(void)41 void CloudSyncAssetManagerImplTest::SetUpTestCase(void)
42 {
43     proxy_ = std::make_shared<MockServiceProxy>();
44     IserviceProxy::proxy_ = proxy_;
45     serviceProxy_ = sptr(new CloudSyncServiceMock());
46     CloudSyncAssetManagerImpl::GetInstance().isFirstCall_.test_and_set();
47     std::cout << "SetUpTestCase" << std::endl;
48 }
49 
TearDownTestCase(void)50 void CloudSyncAssetManagerImplTest::TearDownTestCase(void)
51 {
52     IserviceProxy::proxy_ = nullptr;
53     proxy_ = nullptr;
54     serviceProxy_ = nullptr;
55     std::cout << "TearDownTestCase" << std::endl;
56 }
57 
SetUp(void)58 void CloudSyncAssetManagerImplTest::SetUp(void)
59 {
60     std::cout << "SetUp" << std::endl;
61 }
62 
TearDown(void)63 void CloudSyncAssetManagerImplTest::TearDown(void)
64 {
65     std::cout << "TearDown" << std::endl;
66 }
67 
68 /*
69  * @tc.name: SetDeathRecipientTest
70  * @tc.desc: Verify the SetDeathRecipient function.
71  * @tc.type: FUNC
72  * @tc.require: I6H5MH
73  */
74 HWTEST_F(CloudSyncAssetManagerImplTest, SetDeathRecipientTest, TestSize.Level1)
75 {
76     GTEST_LOG_(INFO) << "SetDeathRecipientTest Start";
77     try {
78         CloudSyncAssetManagerImpl::GetInstance().SetDeathRecipient(serviceProxy_->AsObject());
79         EXPECT_TRUE(true);
80     } catch (...) {
81         EXPECT_TRUE(false);
82         GTEST_LOG_(INFO) << " SetDeathRecipientTest FAILED";
83     }
84     GTEST_LOG_(INFO) << "SetDeathRecipientTest End";
85 }
86 
87 /*
88  * @tc.name: UploadAssetTest
89  * @tc.desc: Verify the UploadAsset function.
90  * @tc.type: FUNC
91  * @tc.require: I6H5MH
92  */
93 HWTEST_F(CloudSyncAssetManagerImplTest, UploadAssetTest, TestSize.Level1)
94 {
95     GTEST_LOG_(INFO) << "UploadAssetTest Start";
96     try {
97         int32_t userId = 100;
98         std::string request = "";
99         std::string result = "";
100         EXPECT_CALL(*proxy_, GetInstance()).WillOnce(Return(serviceProxy_));
101 
102         int32_t res = CloudSyncAssetManagerImpl::GetInstance().UploadAsset(userId, request, result);
103         EXPECT_EQ(res, E_OK);
104     } catch (...) {
105         EXPECT_TRUE(false);
106         GTEST_LOG_(INFO) << "UploadAsseteTest FAILED";
107     }
108     GTEST_LOG_(INFO) << "UploadAssetTest End";
109 }
110 
111 /*
112  * @tc.name: DownloadFileTest001
113  * @tc.desc: Verify the DownloadFile function.
114  * @tc.type: FUNC
115  * @tc.require: I6H5MH
116  */
117 HWTEST_F(CloudSyncAssetManagerImplTest, DownloadFileTest001, TestSize.Level1)
118 {
119     GTEST_LOG_(INFO) << "DownloadFileTest Start";
120     try {
121         int32_t userId = 100;
122         std::string bundleName = "com.ohos.photos";
123         AssetInfo assetInfo;
124         EXPECT_CALL(*proxy_, GetInstance()).WillOnce(Return(serviceProxy_));
125 
126         int32_t res = CloudSyncAssetManagerImpl::GetInstance().DownloadFile(userId, bundleName, assetInfo);
127         EXPECT_EQ(res, E_OK);
128     } catch (...) {
129         EXPECT_TRUE(false);
130         GTEST_LOG_(INFO) << "DownloadFileTest FAILED";
131     }
132     GTEST_LOG_(INFO) << "DownloadFileTest End";
133 }
134 
135 /*
136  * @tc.name: DownloadFilesTest001
137  * @tc.desc: Verify the DownloadFiles function.
138  * @tc.type: FUNC
139  * @tc.require: I6H5MH
140  */
141 HWTEST_F(CloudSyncAssetManagerImplTest, DownloadFilesTest001, TestSize.Level1)
142 {
143     GTEST_LOG_(INFO) << "DownloadFilesTest001 Start";
144     try {
145         int32_t userId = 100;
146         std::string bundleName = "com.ohos.photos";
147         std::vector<AssetInfo> assetInfo;
148         std::vector<bool> assetResultMap;
149         int32_t connectTime = 1;
150         EXPECT_CALL(*proxy_, GetInstance()).WillOnce(Return(serviceProxy_));
151 
152         int32_t res = CloudSyncAssetManagerImpl::GetInstance().DownloadFiles(userId,
153                                                     bundleName, assetInfo, assetResultMap, connectTime);
154         EXPECT_EQ(res, E_OK);
155     } catch (...) {
156         EXPECT_TRUE(false);
157         GTEST_LOG_(INFO) << "DownloadFilesTest001 FAILED";
158     }
159     GTEST_LOG_(INFO) << "DownloadFilesTest001 End";
160 }
161 
162 /*
163  * @tc.name: DownloadFilesTest002
164  * @tc.desc: Verify the DownloadFiles function.
165  * @tc.type: FUNC
166  * @tc.require: I6H5MH
167  */
168 HWTEST_F(CloudSyncAssetManagerImplTest, DownloadFilesTest002, TestSize.Level1)
169 {
170     GTEST_LOG_(INFO) << "DownloadFilesTest002 Start";
171     try {
172         int32_t userId = 100;
173         std::string bundleName = "com.ohos.photos";
174         std::vector<AssetInfo> assetInfo;
175         std::vector<bool> assetResultMap;
176         int32_t connectTime = 1;
177         EXPECT_CALL(*proxy_, GetInstance()).WillOnce(Return(nullptr));
178 
179         int32_t res = CloudSyncAssetManagerImpl::GetInstance().DownloadFiles(userId,
180                                                     bundleName, assetInfo, assetResultMap, connectTime);
181         EXPECT_EQ(res, E_SA_LOAD_FAILED);
182     } catch (...) {
183         EXPECT_TRUE(false);
184         GTEST_LOG_(INFO) << "DownloadFilesTest002 FAILED";
185     }
186     GTEST_LOG_(INFO) << "DownloadFilesTest002 End";
187 }
188 
189 /*
190  * @tc.name: DeleteAssetTest
191  * @tc.desc: Verify the DeleteAsset function.
192  * @tc.type: FUNC
193  * @tc.require: I6H5MH
194  */
195 HWTEST_F(CloudSyncAssetManagerImplTest, DeleteAssetTest, TestSize.Level1)
196 {
197     GTEST_LOG_(INFO) << "DeleteAssetTest Start";
198     try {
199         int32_t userId = 100;
200         std::string uri = "uri";
201         EXPECT_CALL(*proxy_, GetInstance()).WillOnce(Return(serviceProxy_));
202 
203         int32_t res = CloudSyncAssetManagerImpl::GetInstance().DeleteAsset(userId, uri);
204         EXPECT_EQ(res, E_OK);
205     } catch (...) {
206         EXPECT_TRUE(false);
207         GTEST_LOG_(INFO) << "DeleteAssetTest FAILED";
208     }
209     GTEST_LOG_(INFO) << "DeleteAssetTest End";
210 }
211 
212 } // namespace FileManagement::CloudSync
213 } // namespace OHOS