• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
18 #include "folder.h"
19 #include "pasteboard_client.h"
20 #include "pasteboard_copy.h"
21 #include "pasteboard_error.h"
22 #include "pasteboard_hilog.h"
23 #include "unified_data.h"
24 
25 namespace OHOS::MiscServices {
26 using namespace testing::ext;
27 using namespace testing;
28 using UnifiedData = UDMF::UnifiedData;
29 
30 class PasteboardCopyTest : public testing::Test {
31 public:
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36     UnifiedData InitFileData();
37 
38 protected:
39     Details details_;
40     std::string uri_;
41 };
42 
SetUpTestCase(void)43 void PasteboardCopyTest::SetUpTestCase(void) { }
44 
TearDownTestCase(void)45 void PasteboardCopyTest::TearDownTestCase(void) { }
46 
SetUp(void)47 void PasteboardCopyTest::SetUp(void) { }
48 
TearDown(void)49 void PasteboardCopyTest::TearDown(void) { }
50 
InitFileData()51 UnifiedData PasteboardCopyTest::InitFileData()
52 {
53     UnifiedData data;
54     auto typeStr = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::FILE);
55     uri_ = "file://uri";
56     std::shared_ptr<UDMF::File> fileRecord = std::make_shared<UDMF::File>(uri_);
57     fileRecord->SetDetails(details_);
58     data.AddRecord(fileRecord);
59     return data;
60 }
61 
62 /**
63  * @tc.name: IsDirectoryTest
64  * @tc.desc: Test the path is a directory
65  * @tc.type: FUNC
66  * @tc.require:
67  */
68 HWTEST_F(PasteboardCopyTest, IsDirectoryTest, TestSize.Level0)
69 {
70     std::string directoryPath = "/invalid/path";
71     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
72 
73     bool result = pasteBoardCopyFile.IsDirectory(directoryPath);
74     EXPECT_FALSE(result);
75 }
76 
77 /**
78  * @tc.name: IsFileTest
79  * @tc.desc: Test the path is a file
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(PasteboardCopyTest, IsFileTest, TestSize.Level0)
84 {
85     std::string filePath = "/invalid/path";
86     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
87 
88     bool result = pasteBoardCopyFile.IsFile(filePath);
89     EXPECT_FALSE(result);
90 }
91 
92 /**
93  * @tc.name: OnProgressNotifyTest
94  * @tc.desc: Test progress notify
95  * @tc.type: FUNC
96  * @tc.require:
97  */
98 HWTEST_F(PasteboardCopyTest, OnProgressNotifyTest, TestSize.Level0)
99 {
100     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
101     pasteBoardCopyFile.OnProgressNotify(nullptr);
102 
103     std::shared_ptr<GetDataParams> params = std::make_shared<GetDataParams>();
104     params->info = nullptr;
105     pasteBoardCopyFile.OnProgressNotify(params);
106 
107     params->info = new ProgressInfo();
108     params->info->percentage = 101;
109     pasteBoardCopyFile.OnProgressNotify(params);
110     EXPECT_EQ(params->info->percentage, 100);
111     delete params->info;
112     params->info = nullptr;
113 }
114 
115 /**
116  * @tc.name: GetRealPathTest
117  * @tc.desc: Test get real path
118  * @tc.type: FUNC
119  * @tc.require:
120  */
121 HWTEST_F(PasteboardCopyTest, GetRealPathTest, TestSize.Level0)
122 {
123     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
124 
125     std::string path1 = "/home/user/./test";
126     std::string expected1 = "/home/user/test";
127     std::string realPath1 = pasteBoardCopyFile.GetRealPath(path1);
128     EXPECT_EQ(realPath1, expected1);
129 
130     std::string path2 = "/home/user/../test";
131     std::string expected2 = "/home/test";
132     std::string realPath2 = pasteBoardCopyFile.GetRealPath(path2);
133     EXPECT_EQ(realPath2, expected2);
134 
135     std::string path3 = "/home/user/test";
136     std::string expected3 = "/home/user/test";
137     std::string realPath3 = pasteBoardCopyFile.GetRealPath(path3);
138     EXPECT_EQ(realPath3, expected3);
139 
140     std::string path4 = "";
141     std::string expected4 = "";
142     std::string realPath4 = pasteBoardCopyFile.GetRealPath(path4);
143     EXPECT_EQ(realPath4, expected4);
144 }
145 
146 /**
147  * @tc.name: IsRemoteUriTest
148  * @tc.desc: Test the uri is remote
149  * @tc.type: FUNC
150  * @tc.require:
151  */
152 HWTEST_F(PasteboardCopyTest, IsRemoteUriTest, TestSize.Level0)
153 {
154     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
155     std::string uri = "http://www.example.com";
156     bool isRemoteUri = pasteBoardCopyFile.IsRemoteUri(uri);
157     EXPECT_FALSE(isRemoteUri);
158 }
159 
160 /**
161  * @tc.name: CheckCopyParamTest
162  * @tc.desc: Test check copy param
163  * @tc.type: FUNC
164  * @tc.require:
165  */
166 HWTEST_F(PasteboardCopyTest, CheckCopyParamTest, TestSize.Level0)
167 {
168     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
169     PasteData pasteData;
170     std::shared_ptr<GetDataParams> params = nullptr;
171     int32_t result = pasteBoardCopyFile.CheckCopyParam(pasteData, params);
172     EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR));
173 
174     params = std::make_shared<GetDataParams>();
175     result = pasteBoardCopyFile.CheckCopyParam(pasteData, params);
176     EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR));
177 
178     std::string plainText = "plain text";
179     auto data = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
180     ASSERT_TRUE(data != nullptr);
181 
182     int32_t ret = PasteboardClient::GetInstance()->SetPasteData(*data);
183     ASSERT_TRUE(ret == static_cast<int32_t>(PasteboardError::E_OK));
184 
185     bool has = PasteboardClient::GetInstance()->HasPasteData();
186     ASSERT_TRUE(has == true);
187 
188     ret = PasteboardClient::GetInstance()->GetPasteData(pasteData);
189     ASSERT_TRUE(ret == static_cast<int32_t>(PasteboardError::E_OK));
190 
191     result = pasteBoardCopyFile.CheckCopyParam(pasteData, params);
192     EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::E_OK));
193 }
194 
195 /**
196  * @tc.name: HandleProgressTest
197  * @tc.desc: Test handle progress
198  * @tc.type: FUNC
199  * @tc.require:
200  */
201 HWTEST_F(PasteboardCopyTest, HandleProgressTest, TestSize.Level0)
202 {
203     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
204     std::shared_ptr<CopyInfo> copyInfo = std::make_shared<CopyInfo>();
205     CopyInfo info = *copyInfo;
206     std::shared_ptr<GetDataParams> params = nullptr;
207     pasteBoardCopyFile.HandleProgress(1, info, 100, 1000, params);
208 
209     params = std::make_shared<GetDataParams>();
210     params->info = nullptr;
211     pasteBoardCopyFile.HandleProgress(1, info, 100, 1000, params);
212 
213     params->info = new ProgressInfo();
214     pasteBoardCopyFile.HandleProgress(0, info, 100, 1000, params);
215     pasteBoardCopyFile.HandleProgress(1, info, 100, 0, params);
216 
217     pasteBoardCopyFile.HandleProgress(1, info, 100, 1000, params);
218 
219     auto data = InitFileData();
220     PasteboardClient::GetInstance()->SetUnifiedData(data);
221 
222     UDMF::UnifiedData newData;
223     PasteboardClient::GetInstance()->GetUnifiedData(newData);
224     ASSERT_EQ(1, newData.GetRecords().size());
225 
226     delete params->info;
227     params->info = nullptr;
228 }
229 
230 /**
231  * @tc.name: InitCopyInfoTest
232  * @tc.desc: Test init copy info
233  * @tc.type: FUNC
234  * @tc.require:
235  */
236 HWTEST_F(PasteboardCopyTest, InitCopyInfoTest, TestSize.Level0)
237 {
238     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
239     std::string srcUri = "file:///source/path";
240     std::string destUri = "file:///destination/path";
241     std::shared_ptr<GetDataParams> dataParams = std::make_shared<GetDataParams>();
242     dataParams->destUri = destUri;
243     std::shared_ptr<CopyInfo> copyInfo = std::make_shared<CopyInfo>();
244     int32_t index = 0;
245 
246     int32_t result = pasteBoardCopyFile.InitCopyInfo(srcUri, dataParams, copyInfo, index);
247     EXPECT_EQ(result, ENOMEM);
248 }
249 
250 /**
251  * @tc.name: CopyFileDataTest001
252  * @tc.desc: Test copy file data
253  * @tc.type: FUNC
254  * @tc.require:
255  */
256 HWTEST_F(PasteboardCopyTest, CopyFileDataTest001, TestSize.Level0)
257 {
258     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
259     PasteData pasteData;
260     std::string destUri = "file:///destination/path";
261     std::shared_ptr<GetDataParams> dataParams = std::make_shared<GetDataParams>();
262     dataParams->destUri = destUri;
263 
264     int32_t result = pasteBoardCopyFile.CopyFileData(pasteData, dataParams);
265     EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::E_OK));
266 }
267 
268 /**
269  * @tc.name: CopyFileDataTest002
270  * @tc.desc: Test copy file data
271  * @tc.type: FUNC
272  * @tc.require:
273  */
274 HWTEST_F(PasteboardCopyTest, CopyFileDataTest002, TestSize.Level0)
275 {
276     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
277     PasteData pasteData;
278     OHOS::Uri uri("/");
279     pasteData.AddUriRecord(uri);
280     std::string destUri = "file:///destination/path";
281     std::shared_ptr<GetDataParams> dataParams = std::make_shared<GetDataParams>();
282     dataParams->destUri = destUri;
283     EXPECT_NE(dataParams, nullptr);
284 
285     int32_t result = pasteBoardCopyFile.CopyFileData(pasteData, dataParams);
286 }
287 
288 /**
289  * @tc.name: CopyFileDataTest003
290  * @tc.desc: Test copy file data
291  * @tc.type: FUNC
292  * @tc.require:
293  */
294 HWTEST_F(PasteboardCopyTest, CopyFileDataTest003, TestSize.Level0)
295 {
296     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
297     PasteData pasteData;
298     pasteData.AddTextRecord("hello");
299     std::shared_ptr<GetDataParams> dataParams = std::make_shared<GetDataParams>();
300 
301     int32_t result = pasteBoardCopyFile.CopyFileData(pasteData, dataParams);
302     EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::E_OK));
303 }
304 
305 /**
306  * @tc.name: CopyPasteDataTest001
307  * @tc.desc: Test copy paste data
308  * @tc.type: FUNC
309  * @tc.require:
310  */
311 HWTEST_F(PasteboardCopyTest, CopyPasteDataTest001, TestSize.Level0)
312 {
313     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
314     PasteData pasteData;
315     pasteData.AddTextRecord("hello");
316     std::shared_ptr<GetDataParams> dataParams = nullptr;
317 
318     int32_t result = pasteBoardCopyFile.CopyPasteData(pasteData, dataParams);
319     EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR));
320 }
321 
322 /**
323  * @tc.name: CopyPasteDataTest002
324  * @tc.desc: Test copy paste data
325  * @tc.type: FUNC
326  * @tc.require:
327  */
328 HWTEST_F(PasteboardCopyTest, CopyPasteDataTest002, TestSize.Level0)
329 {
330     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
331     PasteData pasteData;
332     pasteData.AddTextRecord("hello");
333     std::shared_ptr<GetDataParams> dataParams = std::make_shared<GetDataParams>();
334     dataParams->fileConflictOption = FILE_OVERWRITE;
335     dataParams->progressIndicator = DEFAULT_PROGRESS_INDICATOR;
336     dataParams->info = new ProgressInfo();
337     EXPECT_NE(dataParams, nullptr);
338 
339     int32_t result = pasteBoardCopyFile.CopyPasteData(pasteData, dataParams);
340     delete dataParams->info;
341     dataParams->info = nullptr;
342 }
343 
344 /**
345  * @tc.name: ShouldKeepRecordTest001
346  * @tc.desc: Test should keep record
347  * @tc.type: FUNC
348  * @tc.require:
349  */
350 HWTEST_F(PasteboardCopyTest, ShouldKeepRecordTest001, TestSize.Level0)
351 {
352     PasteBoardCopyFile &pasteBoardCopyFile = PasteBoardCopyFile::GetInstance();
353     int32_t ret = static_cast<int32_t>(PasteboardError::E_OK);
354     std::string destUri = "file:///destination/path";
355     std::shared_ptr<PasteDataRecord> record = nullptr;
356     int32_t result = pasteBoardCopyFile.ShouldKeepRecord(ret, destUri, record);
357     EXPECT_FALSE(result);
358 }
359 } // namespace OHOS::MiscServices