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 #include <copy/file_copy_manager.h>
16 #include <copy/trans_listener.h>
17 #include <gtest/gtest.h>
18 #include "dfs_error.h"
19
20 namespace OHOS::Storage::DistributedFile::Test {
21 using namespace OHOS::FileManagement;
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace std;
25
26 class TransListenerTest : public testing::Test {
27 public:
28 static void SetUpTestCase(void);
29 static void TearDownTestCase(void);
30 void SetUp();
31 void TearDown();
32
33 uint64_t process = 0;
34 uint64_t fileSize = 0;
35 using callBack = std::function<void(uint64_t processSize, uint64_t totalFileSize)>;
__anon58f8fbbb0102(uint64_t processSize, uint64_t totalFileSize) 36 callBack listener = [&](uint64_t processSize, uint64_t totalFileSize) {
37 process = processSize;
38 fileSize = totalFileSize;
39 };
40 };
41
SetUpTestCase(void)42 void TransListenerTest::SetUpTestCase(void)
43 {
44 GTEST_LOG_(INFO) << "SetUpTestCase";
45 }
46
TearDownTestCase(void)47 void TransListenerTest::TearDownTestCase(void)
48 {
49 GTEST_LOG_(INFO) << "TearDownTestCase";
50 }
51
SetUp(void)52 void TransListenerTest::SetUp(void)
53 {
54 GTEST_LOG_(INFO) << "SetUp";
55 }
56
TearDown(void)57 void TransListenerTest::TearDown(void)
58 {
59 GTEST_LOG_(INFO) << "TearDown";
60 }
61
62 /**
63 * @tc.name: TransListener_0001
64 * @tc.desc: The execution of the CopyTosandbox success.
65 * @tc.type: FUNC
66 * @tc.require: I7TDJK
67 */
68 HWTEST_F(TransListenerTest, TransListener_0001, TestSize.Level1)
69 {
70 GTEST_LOG_(INFO) << "TransListener_0001 Start";
71 string srcuri = "file://docs/storage/media/100/local/files/Docs/aa/";
72 string desturi = "file://docs/storage/media/100/local/files/Docs/aa1/";
73 auto ptr = std::make_shared<TransListener>(desturi, listener);
74 int32_t ret = ptr->CopyToSandBox(srcuri);
75 EXPECT_EQ(ret, E_OK);
76 GTEST_LOG_(INFO) << "TransListener_0001 End";
77 }
78
79 /**
80 * @tc.name: TransListener_0002
81 * @tc.desc: The execution of the Cancel success.
82 * @tc.type: FUNC
83 * @tc.require: I7TDJK
84 */
85 HWTEST_F(TransListenerTest, TransListener_0002, TestSize.Level1)
86 {
87 GTEST_LOG_(INFO) << "TransListener_0002 Start";
88 string srcuri = "file://docs/storage/media/100/local/files/Docs/aa/";
89 string desturi = "file://docs/storage/media/100/local/files/Docs/aa1/";
90 auto ptr = std::make_shared<TransListener>(desturi, listener);
91 int32_t ret = ptr->Cancel();
92 EXPECT_EQ(ret, E_SA_LOAD_FAILED);
93 GTEST_LOG_(INFO) << "TransListener_0002 End";
94 }
95
96 /**
97 * @tc.name: TransListener_0003
98 * @tc.desc: The execution of the OnFileReceive success.
99 * @tc.type: FUNC
100 * @tc.require: I7TDJK
101 */
102 HWTEST_F(TransListenerTest, TransListener_0003, TestSize.Level1)
103 {
104 GTEST_LOG_(INFO) << "TransListener_0003 Start";
105 string srcuri = "file://docs/storage/media/100/local/files/Docs/aa/";
106 string desturi = "file://docs/storage/media/100/local/files/Docs/aa1/";
107 auto ptr = std::make_shared<TransListener>(desturi, listener);
108 int32_t ret = ptr->OnFileReceive(process, fileSize);
109 EXPECT_EQ(ret, E_OK);
110 GTEST_LOG_(INFO) << "TransListener_0003 End";
111 }
112
113 /**
114 * @tc.name: TransListener_0004
115 * @tc.desc: The execution of the OnFinished success.
116 * @tc.type: FUNC
117 * @tc.require: I7TDJK
118 */
119 HWTEST_F(TransListenerTest, TransListener_0004, TestSize.Level1)
120 {
121 GTEST_LOG_(INFO) << "TransListener_0004 Start";
122 string sessionName = "test";
123 string desturi = "file://docs/storage/media/100/local/files/Docs/aa1/";
124 auto ptr = std::make_shared<TransListener>(desturi, listener);
125 int32_t ret = ptr->OnFinished(sessionName);
126 EXPECT_EQ(ret, E_OK);
127 GTEST_LOG_(INFO) << "TransListener_0004 End";
128 }
129
130 /**
131 * @tc.name: TransListener_0005
132 * @tc.desc: The execution of the OnFailed success.
133 * @tc.type: FUNC
134 * @tc.require: I7TDJK
135 */
136 HWTEST_F(TransListenerTest, TransListener_0005, TestSize.Level1)
137 {
138 GTEST_LOG_(INFO) << "TransListener_0005 Start";
139 string sessionName = "test";
140 int32_t errorCode = 0;
141 string desturi = "file://docs/storage/media/100/local/files/Docs/aa1/";
142 auto ptr = std::make_shared<TransListener>(desturi, listener);
143 int32_t ret = ptr->OnFailed(sessionName, errorCode);
144 EXPECT_EQ(ret, E_OK);
145 GTEST_LOG_(INFO) << "TransListener_0005 End";
146 }
147
148 }