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 #define LOG_TAG "CopyFileTest"
16
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <string>
20
21 #include "logger.h"
22 #include "udmf_copy_file.h"
23
24 using namespace testing::ext;
25 using namespace OHOS::UDMF;
26 using namespace OHOS;
27 namespace OHOS::Test {
28
29 class CopyFileTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35 };
36
SetUpTestCase()37 void CopyFileTest::SetUpTestCase()
38 {
39 }
40
TearDownTestCase()41 void CopyFileTest::TearDownTestCase()
42 {
43 }
44
SetUp()45 void CopyFileTest::SetUp()
46 {
47 }
48
TearDown()49 void CopyFileTest::TearDown()
50 {
51 }
52
53 /**
54 * @tc.name: GetInstance
55 * @tc.desc: GetInstance Should Return Same Instance
56 * @tc.type: FUNC
57 */
58 HWTEST_F(CopyFileTest, GetInstance, TestSize.Level1)
59 {
60 auto& instance1 = UdmfCopyFile::GetInstance();
61 auto& instance2 = UdmfCopyFile::GetInstance();
62 ASSERT_EQ(&instance1, &instance2);
63 }
64
65 /**
66 * @tc.name: Copy001
67 * @tc.desc: Copy Should Return Canceled When ProgressQueue Is Cancel
68 * @tc.type: FUNC
69 */
70 HWTEST_F(CopyFileTest, Copy001, TestSize.Level1)
71 {
72 auto asyncHelper = std::make_unique<AsyncHelper>();
73 asyncHelper->progressQueue.Cancel();
74 ASSERT_TRUE(asyncHelper->progressQueue.IsCancel());
75
76 auto result = UdmfCopyFile::GetInstance().Copy(asyncHelper);
77 ASSERT_EQ(result, E_COPY_CANCELED);
78 }
79
80 /**
81 * @tc.name: Copy002
82 * @tc.desc: normal copy test
83 * @tc.type: FUNC
84 */
85 HWTEST_F(CopyFileTest, Copy002, TestSize.Level1)
86 {
87 std::string srcUri = "test_src.txt";
88 std::string destUri = "test_dest.txt";
89 auto record = std::make_shared<UnifiedRecord>();
90 auto asyncHelper = std::make_unique<AsyncHelper>();
91 UdmfCopyFile::CopyContext context(asyncHelper);
92
93 bool result = UdmfCopyFile::GetInstance().CopyFile(srcUri, destUri, record, context);
94 EXPECT_TRUE(result);
95 EXPECT_EQ(context.status, E_OK);
96 }
97
98 /**
99 * @tc.name: ConstructDestUri
100 * @tc.desc: Construct DestUri Should Correctly Construct Destination Path
101 * @tc.type: FUNC
102 */
103 HWTEST_F(CopyFileTest, ConstructDestUri, TestSize.Level1)
104 {
105 std::string destUri = "/destination";
106 std::string srcUri = "/source/file.txt";
107 auto result = UdmfCopyFile::GetInstance().ConstructDestUri(destUri, srcUri);
108 ASSERT_EQ(result, "/destination/file.txt");
109 }
110
111 /**
112 * @tc.name: GetFileName001
113 * @tc.desc: GetFileName Should Handle Remote Files Correctly
114 * @tc.type: FUNC
115 */
116 HWTEST_F(CopyFileTest, GetFileName001, TestSize.Level1)
117 {
118 std::string remoteFile = "file://data/storage/el2/distributedfiles/103.png";
119 auto result = UdmfCopyFile::GetInstance().GetFileName(remoteFile);
120 ASSERT_EQ(result, "103.png");
121 }
122
123 /**
124 * @tc.name: GetFileName002
125 * @tc.desc: GetFileName Should Handle Remote Files Correctly
126 * @tc.type: FUNC
127 */
128 HWTEST_F(CopyFileTest, GetFileName002, TestSize.Level1)
129 {
130 std::string remoteFile = "file://data/storage/el2/distributedfiles/103.png?networkid=xxx";
131 auto result = UdmfCopyFile::GetInstance().GetFileName(remoteFile);
132 ASSERT_EQ(result, "103.png");
133 }
134
135 /**
136 * @tc.name: HandleRecord
137 * @tc.desc: HandleRecord Should Add To Processed Data For Non FileType
138 * @tc.type: FUNC
139 */
140 HWTEST_F(CopyFileTest, HandleRecord, TestSize.Level1)
141 {
142 auto record = std::make_shared<UnifiedRecord>();
143 auto helper = std::make_unique<AsyncHelper>();
144 UdmfCopyFile::CopyContext context(helper);
145 bool result = UdmfCopyFile::GetInstance().HandleRecord(record, context);
146 ASSERT_TRUE(result);
147 ASSERT_EQ(context.processedData->GetRecords().size(), 1);
148 }
149
150 } // OHOS::Test