1 /*
2  * Copyright (c) 2023-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 "pasteboard_web_controller.h"
17 #include <gtest/gtest.h>
18 
19 using namespace testing;
20 using namespace testing::ext;
21 using namespace OHOS::MiscServices;
22 class WebControllerTest : public testing::Test {
23 public:
WebControllerTest()24     WebControllerTest() {};
~WebControllerTest()25     ~WebControllerTest() {};
26     static void SetUpTestCase(void);
27     static void TearDownTestCase(void);
28     void SetUp();
29     void TearDown();
30 };
31 
SetUpTestCase(void)32 void WebControllerTest::SetUpTestCase(void)
33 {
34 }
35 
TearDownTestCase(void)36 void WebControllerTest::TearDownTestCase(void)
37 {
38 }
39 
SetUp(void)40 void WebControllerTest::SetUp(void)
41 {
42 }
43 
TearDown(void)44 void WebControllerTest::TearDown(void)
45 {
46 }
47 
48 /**
49  * @tc.name: SplitHtmlTest_001.
50  * @tc.desc: Test did not use local image address HTML with SplitHtml.
51  * @tc.type: FUNC.
52  * @tc.require:
53  * @tc.author:
54  */
HWTEST_F(WebControllerTest, SplitHtmlTest_001, TestSize.Level1)55 HWTEST_F(WebControllerTest, SplitHtmlTest_001, TestSize.Level1)
56 {
57     auto webClipboardController = PasteboardWebController::GetInstance();
58     std::shared_ptr<std::string> html(
59         new std::string("<img data-ohos='clipboard' src='http://file1.jpg'><img data-ohos='clipboard' "
60                         "src='https://data/storage/el2/distributedfiles/202305301.png'>"));
61     auto pasteData = webClipboardController.SplitHtml(html);
62     EXPECT_EQ(pasteData->GetRecordCount(), 1);
63     std::vector<std::shared_ptr<PasteDataRecord>> pasteDataRecords = pasteData->AllRecords();
64     EXPECT_EQ(*(pasteDataRecords[0]->GetHtmlText()), *html);
65 }
66 
67 /**
68  * @tc.name: SplitHtmlTest_002.
69  * @tc.desc: Test contains a local image address HTML with SplitHtml.
70  * @tc.type: FUNC.
71  * @tc.require:
72  * @tc.author:
73  */
HWTEST_F(WebControllerTest, SplitHtmlTest_002, TestSize.Level1)74 HWTEST_F(WebControllerTest, SplitHtmlTest_002, TestSize.Level1)
75 {
76     auto webClipboardController = PasteboardWebController::GetInstance();
77     std::shared_ptr<std::string> html(
78         new std::string("<img data-ohos='clipboard' src='file:///file1.jpg'><img data-ohos='clipboard' "
79                         "src='https://data/storage/el2/distributedfiles/202305301.png'>"));
80     auto pasteData = webClipboardController.SplitHtml(html);
81     EXPECT_NE(pasteData, nullptr);
82     std::vector<std::shared_ptr<PasteDataRecord>> pasteDataRecords = pasteData->AllRecords();
83     EXPECT_FALSE(pasteDataRecords.empty());
84 }
85 
86 /**
87  * @tc.name: SplitHtmlTest_003.
88  * @tc.desc: Test contains multiple local image addresses HTML with SplitHtml.
89  * @tc.type: FUNC.
90  * @tc.require:
91  * @tc.author:
92  */
HWTEST_F(WebControllerTest, SplitHtmlTest_003, TestSize.Level1)93 HWTEST_F(WebControllerTest, SplitHtmlTest_003, TestSize.Level1)
94 {
95     auto webClipboardController = PasteboardWebController::GetInstance();
96     std::shared_ptr<std::string> html(
97         new std::string("<img data-ohos='clipboard' src='file:///file1.jpg'><img data-ohos='clipboard' "
98                         "src='file2.jpg'><img data-ohos='clipboard' "
99                         "src='https://data/storage/el2/distributedfiles/202305301.png'>"));
100     auto pasteData = webClipboardController.SplitHtml(html);
101     EXPECT_NE(pasteData, nullptr);
102     std::vector<std::shared_ptr<PasteDataRecord>> pasteDataRecords = pasteData->AllRecords();
103     EXPECT_FALSE(pasteDataRecords.empty());
104 }
105 
106 /**
107  * @tc.name: RebuildHtmlTest_004.
108  * @tc.desc: Test does not include local image address HTML with RebuildHtml.
109  * @tc.type: FUNC.
110  * @tc.require:
111  * @tc.author:
112  */
HWTEST_F(WebControllerTest, RebuildHtmlTest_004, TestSize.Level1)113 HWTEST_F(WebControllerTest, RebuildHtmlTest_004, TestSize.Level1)
114 {
115     auto webClipboardController = PasteboardWebController::GetInstance();
116     std::shared_ptr<std::string> html(
117         new std::string("<img data-ohos='clipboard' src='http://file1.jpg'><img data-ohos='clipboard' "
118                         "src='https://data/storage/el2/distributedfiles/202305301.png'>"));
119     auto pasteData = webClipboardController.SplitHtml(html);
120     EXPECT_EQ(pasteData->GetRecordCount(), 1);
121     std::shared_ptr<std::string> newHtml = webClipboardController.RebuildHtml(pasteData);
122     EXPECT_EQ(*newHtml, *html);
123 }
124 
125 /**
126  * @tc.name: RebuildHtmlTest_005.
127  * @tc.desc: Test contains a local image address HTML with RebuildHtml.
128  * @tc.type: FUNC.
129  * @tc.require:
130  * @tc.author:
131  */
HWTEST_F(WebControllerTest, RebuildHtmlTest_005, TestSize.Level1)132 HWTEST_F(WebControllerTest, RebuildHtmlTest_005, TestSize.Level1)
133 {
134     const int32_t splitRecordCount = 2;
135     const std::string uri = "file:///data/storage/el2/distributedfiles/temp.png";
136     auto webClipboardController = PasteboardWebController::GetInstance();
137     std::shared_ptr<std::string> html(
138         new std::string("<img data-ohos='clipboard' src='file:///file1.jpg'><img data-ohos='clipboard' "
139                         "src='https://data/storage/el2/distributedfiles/202305301.png'>"));
140     const char* execptHtml =
141         "<img data-ohos='clipboard' src='file:///data/storage/el2/distributedfiles/temp.png'><img "
142         "data-ohos='clipboard' "
143         "src='https://data/storage/el2/distributedfiles/202305301.png'>";
144     auto pasteData = webClipboardController.SplitHtml(html);
145     EXPECT_NE(pasteData, nullptr);
146     EXPECT_EQ(pasteData->GetRecordCount(), splitRecordCount);
147     std::shared_ptr<PasteData> newPasteData = std::make_shared<PasteData>();
148     std::vector<std::shared_ptr<PasteDataRecord>> pasteDataRecords = pasteData->AllRecords();
149     EXPECT_EQ(*(pasteDataRecords[pasteData->GetRecordCount() - 1]->GetHtmlText()), *html);
150 
151     newPasteData->AddHtmlRecord(*html);
152     for (auto i = 0; i < pasteData->GetRecordCount() - 1; i++) {
153         PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
154         auto newUri = std::make_shared<OHOS::Uri>(uri);
155         builder.SetUri(newUri);
156         builder.SetCustomData(pasteDataRecords[i]->GetCustomData());
157         auto record = builder.Build();
158         newPasteData->AddRecord(record);
159     }
160     EXPECT_EQ(newPasteData->GetRecordCount(), splitRecordCount);
161     std::shared_ptr<std::string> newHtml = webClipboardController.RebuildHtml(newPasteData);
162     EXPECT_EQ(newPasteData->GetRecordCount(), 1);
163     const char* newHtmlStr = newHtml.get()->c_str();
164     EXPECT_STREQ(newHtmlStr, execptHtml);
165 }
166 
167 /**
168  * @tc.name: RebuildHtmlTest_006.
169  * @tc.desc: Test contains a multiple image address HTML with RebuildHtml.
170  * @tc.type: FUNC.
171  * @tc.require:
172  * @tc.author:
173  */
HWTEST_F(WebControllerTest, RebuildHtmlTest_006, TestSize.Level1)174 HWTEST_F(WebControllerTest, RebuildHtmlTest_006, TestSize.Level1)
175 {
176     const int32_t splitRecordCount = 3;
177     const std::string uri = "file:///data/storage/el2/distributedfiles/temp.png";
178     auto webClipboardController = PasteboardWebController::GetInstance();
179     std::shared_ptr<std::string> html(
180         new std::string("<img data-ohos='clipboard' src='file:///file1.jpg'><img data-ohos='clipboard' "
181                         "src=\"file2.jpg\"><img data-ohos='clipboard' "
182                         "src='https://data/storage/el2/distributedfiles/202305301.png'>"));
183     const char* execptHtml =
184         "<img data-ohos='clipboard' src='file:///data/storage/el2/distributedfiles/temp.png'><img "
185         "data-ohos='clipboard' "
186         "src=\"file:///data/storage/el2/distributedfiles/temp.png\"><img data-ohos='clipboard' "
187         "src='https://data/storage/el2/distributedfiles/202305301.png'>";
188     auto pasteData = webClipboardController.SplitHtml(html);
189     EXPECT_NE(pasteData, nullptr);
190     EXPECT_EQ(pasteData->GetRecordCount(), splitRecordCount);
191     std::shared_ptr<PasteData> newPasteData = std::make_shared<PasteData>();
192     std::vector<std::shared_ptr<PasteDataRecord>> pasteDataRecords = pasteData->AllRecords();
193     EXPECT_EQ(*(pasteDataRecords[pasteData->GetRecordCount() - 1]->GetHtmlText()), *html);
194 
195     newPasteData->AddHtmlRecord(*html);
196     for (auto i = 0; i < pasteData->GetRecordCount() - 1; i++) {
197         PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
198         auto newUri = std::make_shared<OHOS::Uri>(uri);
199         builder.SetUri(newUri);
200         builder.SetCustomData(pasteDataRecords[i]->GetCustomData());
201         auto record = builder.Build();
202         newPasteData->AddRecord(record);
203     }
204     EXPECT_EQ(newPasteData->GetRecordCount(), splitRecordCount);
205     std::shared_ptr<std::string> newHtml = webClipboardController.RebuildHtml(newPasteData);
206     EXPECT_EQ(newPasteData->GetRecordCount(), 1);
207     const char* newHtmlStr = newHtml.get()->c_str();
208     EXPECT_STREQ(newHtmlStr, execptHtml);
209 }
210 
211 /**
212  * @tc.name: SplitHtmlTest_007.
213  * @tc.desc: Test contains invalid protocol image link HTML with SplitHtml.
214  * @tc.type: FUNC.
215  * @tc.require:
216  * @tc.author:
217  */
HWTEST_F(WebControllerTest, SplitHtmlTest_007, TestSize.Level1)218 HWTEST_F(WebControllerTest, SplitHtmlTest_007, TestSize.Level1)
219 {
220     auto webClipboardController = PasteboardWebController::GetInstance();
221     std::shared_ptr<std::string> html(
222         new std::string("<img data-ohos='clipboard' src='xxx://file1.jpg'><img data-ohos='clipboard' "
223                         "src='yyy://data/storage/el2/distributedfiles/202305301.png'>"));
224     auto pasteData = webClipboardController.SplitHtml(html);
225     EXPECT_EQ(pasteData->GetRecordCount(), 1);
226     std::vector<std::shared_ptr<PasteDataRecord>> pasteDataRecords = pasteData->AllRecords();
227     EXPECT_EQ(*(pasteDataRecords[0]->GetHtmlText()), *html);
228 }
229 
230 /**
231  * @tc.name: RebuildHtmlTest_008.
232  * @tc.desc: Test contains invalid protocol image link HTML with RebuildHtml.
233  * @tc.type: FUNC.
234  * @tc.require:
235  * @tc.author:
236  */
HWTEST_F(WebControllerTest, RebuildHtmlTest_008, TestSize.Level1)237 HWTEST_F(WebControllerTest, RebuildHtmlTest_008, TestSize.Level1)
238 {
239     auto webClipboardController = PasteboardWebController::GetInstance();
240     std::shared_ptr<std::string> html(
241         new std::string("<img data-ohos='clipboard' src='xxx://file1.jpg'><img data-ohos='clipboard' "
242                         "src='ttt://data/storage/el2/distributedfiles/202305301.png'>"));
243     auto pasteData = webClipboardController.SplitHtml(html);
244     EXPECT_EQ(pasteData->GetRecordCount(), 1);
245     std::shared_ptr<std::string> newHtml = webClipboardController.RebuildHtml(pasteData);
246     EXPECT_EQ(*newHtml, *html);
247 }
248 
249 /**
250  * @tc.name: RebuildHtmlTest_009.
251  * @tc.desc: Test contains a local image address HTML with RebuildHtml.
252  * @tc.type: FUNC.
253  * @tc.require:
254  * @tc.author:
255  */
HWTEST_F(WebControllerTest, RebuildHtmlTest_009, TestSize.Level1)256 HWTEST_F(WebControllerTest, RebuildHtmlTest_009, TestSize.Level1)
257 {
258     const int32_t splitRecordCount = 3;
259     const std::string uri = "file:///data/storage/el2/distributedfiles/temp.png";
260     auto webClipboardController = PasteboardWebController::GetInstance();
261     std::shared_ptr<std::string> html(
262         new std::string("<img src='file:///file1.jpg'><img src=\"file2.jpg\"><img "
263                         "src='https://data/storage/el2/distributedfiles/202305301.png'>"));
264     const char* execptHtml =
265         "<img src='file:///data/storage/el2/distributedfiles/temp.png'><img "
266         ""
267         "src=\"file:///data/storage/el2/distributedfiles/temp.png\"><img "
268         "src='https://data/storage/el2/distributedfiles/202305301.png'>";
269     auto pasteData = webClipboardController.SplitHtml(html);
270     EXPECT_NE(pasteData, nullptr);
271     EXPECT_EQ(pasteData->GetRecordCount(), splitRecordCount);
272     std::shared_ptr<PasteData> newPasteData = std::make_shared<PasteData>();
273     std::vector<std::shared_ptr<PasteDataRecord>> pasteDataRecords = pasteData->AllRecords();
274     EXPECT_EQ(*(pasteDataRecords[pasteData->GetRecordCount() - 1]->GetHtmlText()), *html);
275 
276     newPasteData->AddHtmlRecord(*html);
277     for (auto i = 0; i < pasteData->GetRecordCount() - 1; i++) {
278         PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
279         auto newUri = std::make_shared<OHOS::Uri>(uri);
280         builder.SetUri(newUri);
281         builder.SetCustomData(pasteDataRecords[i]->GetCustomData());
282         auto record = builder.Build();
283         newPasteData->AddRecord(record);
284     }
285     EXPECT_EQ(newPasteData->GetRecordCount(), splitRecordCount);
286     std::shared_ptr<std::string> newHtml = webClipboardController.RebuildHtml(newPasteData);
287     EXPECT_EQ(newPasteData->GetRecordCount(), 1);
288     const char* newHtmlStr = newHtml.get()->c_str();
289     EXPECT_STREQ(newHtmlStr, execptHtml);
290 }