• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <cstring>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #define private public
21 #include "pasteboard_client_adapter_impl.h"
22 #undef private
23 
24 #include "paste_data.h"
25 #include "paste_data_record.h"
26 #include "pasteboard_client_adapter.h"
27 #include "ohos_adapter_helper.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace OHOS::MiscServices;
33 using namespace OHOS::Media;
34 
35 namespace OHOS::NWeb {
36 namespace {
37 const int RESULT_OK = 0;
38 const bool TRUE_OK = true;
39 const std::string g_mimeType = "data";
40 std::shared_ptr<std::string> g_htmlText;
41 std::shared_ptr<std::string> g_plainText;
42 std::shared_ptr<PasteDataRecordAdapterImpl> g_paster;
43 std::shared_ptr<PasteDataRecordAdapterImpl> g_pasternull;
44 std::shared_ptr<PasteDataRecordAdapterImpl> g_datarecord;
45 std::shared_ptr<PasteDataRecordAdapterImpl> g_datarecordnull;
46 std::shared_ptr<PasteDataAdapterImpl> g_dataAdapter;
47 std::shared_ptr<PasteDataAdapterImpl> g_dataAdapterNull;
48 } // namespace
49 
50 class NWebPasteboardAdapterTest : public testing::Test {
51 public:
52     static void SetUpTestCase(void);
53     static void TearDownTestCase(void);
54     void SetUp();
55     void TearDown();
56 };
57 
SetUpTestCase(void)58 void NWebPasteboardAdapterTest::SetUpTestCase(void)
59 {
60     int result = 0;
61     g_dataAdapterNull = std::make_shared<PasteDataAdapterImpl>();
62     if (g_dataAdapterNull == nullptr) {
63         result = -1;
64     }
65     EXPECT_EQ(RESULT_OK, result);
66     g_dataAdapterNull->data_ = nullptr;
67 
68     std::shared_ptr<PasteDataRecord> record = std::make_shared<PasteDataRecord>();
69     if (record == nullptr) {
70         result = -1;
71     }
72     EXPECT_EQ(RESULT_OK, result);
73     g_pasternull = std::make_shared<PasteDataRecordAdapterImpl>(record);
74     if (g_pasternull == nullptr) {
75         result = -1;
76     }
77     EXPECT_EQ(RESULT_OK, result);
78     g_pasternull->record_ = nullptr;
79 
80     std::string mimeType = "pixelMap";
81     g_datarecordnull = std::make_shared<PasteDataRecordAdapterImpl>(mimeType);
82     if (g_datarecordnull == nullptr) {
83         result = -1;
84     }
85     EXPECT_EQ(RESULT_OK, result);
86     g_datarecordnull->builder_ = nullptr;
87     g_datarecordnull->record_ = nullptr;
88 
89     result = 0;
90     g_datarecord = std::make_shared<PasteDataRecordAdapterImpl>(mimeType);
91     if (g_datarecord == nullptr) {
92         result = -1;
93     }
94     EXPECT_EQ(RESULT_OK, result);
95 }
96 
TearDownTestCase(void)97 void NWebPasteboardAdapterTest::TearDownTestCase(void)
98 {}
99 
SetUp(void)100 void NWebPasteboardAdapterTest::SetUp(void)
101 {}
102 
TearDown(void)103 void NWebPasteboardAdapterTest::TearDown(void)
104 {}
105 
106 class MockPasteData : public PasteData {
107 public:
108     MOCK_METHOD1(GetRecordAt, std::shared_ptr<PasteDataRecord>(std::size_t));
109     MOCK_METHOD0(GetRecordCount, std::size_t());
110     MOCK_METHOD1(Encode, bool(std::vector<uint8_t> &));
111     MOCK_METHOD1(Decode, bool(const std::vector<uint8_t> &));
112 };
113 
114 class MockPasteDataRecord : public PasteDataRecord {
115 public:
116     MOCK_METHOD0(GetPixelMap, std::shared_ptr<PixelMap>());
117     MOCK_METHOD1(Encode, bool(std::vector<uint8_t> &));
118     MOCK_METHOD1(Decode, bool(const std::vector<uint8_t> &));
119 };
120 
121 class MockPasteboardObserver : public PasteboardObserverAdapter {
122 public:
123     MockPasteboardObserver() = default;
OnPasteboardChanged()124     void OnPasteboardChanged() override {}
125 };
126 
127 /**
128  * @tc.name: NWebPasteboardAdapter_PasteDataRecordAdapterImpl_001.
129  * @tc.desc: Test the PasteDataRecordAdapterImpl.
130  * @tc.type: FUNC
131  * @tc.require:issueI5O4BN
132  */
133 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_PasteDataRecordAdapterImpl_001, TestSize.Level1)
134 {
135     int result = 0;
136     std::shared_ptr<PasteDataRecord> record = std::make_shared<PasteDataRecord>();
137     if (record == nullptr) {
138         result = -1;
139     }
140     EXPECT_EQ(RESULT_OK, result);
141     std::shared_ptr<PasteDataRecordAdapterImpl> pasterimpl = std::make_shared<PasteDataRecordAdapterImpl>(record);
142     if (pasterimpl == nullptr) {
143         result = -1;
144     }
145     EXPECT_EQ(RESULT_OK, result);
146 }
147 
148 /**
149  * @tc.name: NWebPasteboardAdapter_PasteDataRecordAdapterImpl_002.
150  * @tc.desc: Test the PasteDataRecordAdapterImpl.
151  * @tc.type: FUNC
152  * @tc.require:issueI5O4BN
153  */
154 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_PasteDataRecordAdapterImpl_002, TestSize.Level1)
155 {
156     int result = 0;
157     g_htmlText = std::make_shared<std::string>("htmlText");
158     if (g_htmlText == nullptr) {
159         result = -1;
160     }
161     EXPECT_EQ(RESULT_OK, result);
162     g_plainText = std::make_shared<std::string>("plainText");
163     if (g_plainText == nullptr) {
164         result = -1;
165     }
166     EXPECT_EQ(RESULT_OK, result);
167     g_paster = std::make_shared<PasteDataRecordAdapterImpl>(g_mimeType, g_htmlText, g_plainText);
168     if (g_paster == nullptr) {
169         result = -1;
170     }
171     EXPECT_EQ(RESULT_OK, result);
172 }
173 
174 /**
175  * @tc.name: NWebPasteboardAdapter_NewRecord_003.
176  * @tc.desc: Test the NewRecord.
177  * @tc.type: FUNC
178  * @tc.require:issueI5O4BN
179  */
180 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_NewRecord_003, TestSize.Level1)
181 {
182     int result = 0;
183     std::shared_ptr<PasteDataRecordAdapter> record =
184         PasteDataRecordAdapter::NewRecord(g_mimeType, g_htmlText, g_plainText);
185     if (record == nullptr) {
186         result = -1;
187     }
188     EXPECT_EQ(RESULT_OK, result);
189 }
190 
191 /**
192  * @tc.name: NWebPasteboardAdapter_GetMimeType_004.
193  * @tc.desc: Test the GetMimeType.
194  * @tc.type: FUNC
195  * @tc.require:issueI5O4BN
196  */
197 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetMimeType_004, TestSize.Level1)
198 {
199     int ret = 0;
200     std::string mimeType = g_paster->GetMimeType();
201     if (mimeType.empty()) {
202         ret = -1;
203     }
204     EXPECT_EQ(RESULT_OK, ret);
205     mimeType = g_pasternull->GetMimeType();
206     if (mimeType.empty()) {
207         ret = -1;
208     }
209     EXPECT_NE(RESULT_OK, ret);
210 }
211 
212 /**
213  * @tc.name: NWebPasteboardAdapter_GetHtmlText_005.
214  * @tc.desc: Test the GetHtmlText.
215  * @tc.type: FUNC
216  * @tc.require:issueI5O4BN
217  */
218 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetHtmlText_005, TestSize.Level1)
219 {
220     int result = 0;
221     std::shared_ptr<std::string> htmlText = g_paster->GetHtmlText();
222     if (htmlText == nullptr) {
223         result = -1;
224     }
225     EXPECT_EQ(RESULT_OK, result);
226     std::shared_ptr<std::string> html = g_pasternull->GetHtmlText();
227     if (html == nullptr) {
228         result = -1;
229     }
230     EXPECT_NE(RESULT_OK, result);
231 }
232 
233 /**
234  * @tc.name: NWebPasteboardAdapter_GetPlainText_006.
235  * @tc.desc: Test the GetPlainText.
236  * @tc.type: FUNC
237  * @tc.require:issueI5O4BN
238  */
239 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetPlainText_006, TestSize.Level1)
240 {
241     int result = 0;
242     std::shared_ptr<std::string> plainText = g_paster->GetPlainText();
243     if (plainText == nullptr) {
244         result = -1;
245     }
246     EXPECT_EQ(RESULT_OK, result);
247     std::shared_ptr<std::string> plain = g_pasternull->GetPlainText();
248     if (plain == nullptr) {
249         result = -1;
250     }
251     EXPECT_NE(RESULT_OK, result);
252 }
253 
254 /**
255  * @tc.name: NWebPasteboardAdapter_GetRecord_007.
256  * @tc.desc: Test the GetRecord.
257  * @tc.type: FUNC
258  * @tc.require:issueI5O4BN
259  */
260 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetRecord_007, TestSize.Level1)
261 {
262     int result = 0;
263     std::shared_ptr<PasteDataRecord> record = g_paster->GetRecord();
264     if (record == nullptr) {
265         result = -1;
266     }
267     EXPECT_EQ(RESULT_OK, result);
268 }
269 
270 /**
271  * @tc.name: NWebPasteboardAdapter_PasteDataAdapterImpl_008.
272  * @tc.desc: Test the PasteDataAdapterImpl.
273  * @tc.type: FUNC
274  * @tc.require:issueI5O4BN
275  */
276 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_PasteDataAdapterImpl_008, TestSize.Level1)
277 {
278     int result = 0;
279     g_dataAdapter = std::make_shared<PasteDataAdapterImpl>();
280     if (g_dataAdapter == nullptr) {
281         result = -1;
282     }
283     EXPECT_EQ(RESULT_OK, result);
284 }
285 
286 /**
287  * @tc.name: NWebPasteboardAdapter_PasteDataAdapterImpl_009.
288  * @tc.desc: Test the PasteDataAdapterImpl.
289  * @tc.type: FUNC
290  * @tc.require:issueI5O4BB
291  */
292 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_PasteDataAdapterImpl_009, TestSize.Level1)
293 {
294     int result = 0;
295     std::shared_ptr<PasteData> data = std::make_shared<PasteData>();
296     if (data == nullptr) {
297         result = -1;
298     }
299     EXPECT_EQ(RESULT_OK, result);
300     std::shared_ptr<PasteDataAdapterImpl> dataAdapter = std::make_shared<PasteDataAdapterImpl>(data);
301     if (dataAdapter == nullptr) {
302         result = -1;
303     }
304     EXPECT_EQ(RESULT_OK, result);
305 }
306 
307 /**
308  * @tc.name: NWebPasteboardAdapter_AddHtmlRecord_010.
309  * @tc.desc: Test the AddHtmlRecord.
310  * @tc.type: FUNC
311  * @tc.require:issueI5O4BB
312  */
313 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_AddHtmlRecord_010, TestSize.Level1)
314 {
315     std::string htmlName = "test";
316     EXPECT_NE(g_dataAdapter, nullptr);
317     g_dataAdapter->AddHtmlRecord(htmlName);
318     g_dataAdapterNull->AddHtmlRecord(htmlName);
319 }
320 
321 /**
322  * @tc.name: NWebPasteboardAdapter_AddTextRecord_011.
323  * @tc.desc: Test the AddTextRecord.
324  * @tc.type: FUNC
325  * @tc.require:issueI5O4BB
326  */
327 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_AddTextRecord_011, TestSize.Level1)
328 {
329     std::string htmlName = "test";
330     EXPECT_NE(g_dataAdapter, nullptr);
331     g_dataAdapter->AddTextRecord(htmlName);
332     g_dataAdapterNull->AddTextRecord(htmlName);
333 }
334 
335 /**
336  * @tc.name: NWebPasteboardAdapter_GetMimeTypes_012.
337  * @tc.desc: Test the GetMimeTypes.
338  * @tc.type: FUNC
339  * @tc.require:issueI5O4BB
340  */
341 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetMimeTypes_012, TestSize.Level1)
342 {
343     int result = 0;
344     std::vector<std::string> mimeTypes = g_dataAdapter->GetMimeTypes();
345     if (mimeTypes.empty()) {
346         result = -1;
347     }
348     EXPECT_EQ(RESULT_OK, result);
349     std::vector<std::string> types = g_dataAdapterNull->GetMimeTypes();
350     if (types.empty()) {
351         result = -1;
352     }
353     EXPECT_NE(RESULT_OK, result);
354 }
355 
356 /**
357  * @tc.name: NWebPasteboardAdapter_GetPrimaryHtml_013.
358  * @tc.desc: Test the GetPrimaryHtml.
359  * @tc.type: FUNC
360  * @tc.require:issueI5O4BB
361  */
362 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetPrimaryHtml_013, TestSize.Level1)
363 {
364     int result = 0;
365     std::shared_ptr<std::string> primaryHtml = g_dataAdapter->GetPrimaryHtml();
366     if (primaryHtml == nullptr) {
367         result = -1;
368     }
369     EXPECT_EQ(RESULT_OK, result);
370     std::shared_ptr<std::string> primary = g_dataAdapterNull->GetPrimaryHtml();
371     if (primary == nullptr) {
372         result = -1;
373     }
374     EXPECT_NE(RESULT_OK, result);
375 }
376 
377 /**
378  * @tc.name: NWebPasteboardAdapter_GetPrimaryText_014.
379  * @tc.desc: Test the GetPrimaryText.
380  * @tc.type: FUNC
381  * @tc.require:issueI5O4BB
382  */
383 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetPrimaryText_014, TestSize.Level1)
384 {
385     int result = 0;
386     std::size_t index = 0;
387     std::shared_ptr<std::string> primaryText = g_dataAdapter->GetPrimaryText();
388     if (primaryText == nullptr) {
389         result = -1;
390     }
391     EXPECT_EQ(RESULT_OK, result);
392     result = 0;
393     std::shared_ptr<PasteDataRecordAdapter> record = g_dataAdapter->GetRecordAt(index);
394     if (record == nullptr) {
395         result = -1;
396     }
397     EXPECT_EQ(RESULT_OK, result);
398     result = 0;
399     PasteRecordList recordList = g_dataAdapter->AllRecords();
400     if (recordList.empty()) {
401         result = -1;
402     }
403     EXPECT_EQ(RESULT_OK, result);
404     result = 0;
405     std::shared_ptr<std::string> primary = g_dataAdapterNull->GetPrimaryText();
406     if (primary == nullptr) {
407         result = -1;
408     }
409     EXPECT_NE(RESULT_OK, result);
410 }
411 
412 /**
413  * @tc.name: NWebPasteboardAdapter_GetPrimaryMimeType_015.
414  * @tc.desc: Test the GetPrimaryMimeType.
415  * @tc.type: FUNC
416  * @tc.require:issueI5O4BB
417  */
418 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetPrimaryMimeType_015, TestSize.Level1)
419 {
420     int result = 0;
421     std::shared_ptr<std::string> primaryMimeType = g_dataAdapter->GetPrimaryMimeType();
422     if (primaryMimeType == nullptr) {
423         result = -1;
424     }
425     EXPECT_EQ(RESULT_OK, result);
426     std::shared_ptr<std::string> primary = g_dataAdapterNull->GetPrimaryMimeType();
427     if (primary == nullptr) {
428         result = -1;
429     }
430     EXPECT_NE(RESULT_OK, result);
431 }
432 
433 /**
434  * @tc.name: NWebPasteboardAdapter_GetRecordAt_016.
435  * @tc.desc: Test the GetRecordAt.
436  * @tc.type: FUNC
437  * @tc.require:issueI5O4BB
438  */
439 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetRecordAt_016, TestSize.Level1)
440 {
441     int result = 0;
442     std::size_t index = 1;
443     std::shared_ptr<PasteDataRecordAdapter> record = g_dataAdapterNull->GetRecordAt(index);
444     if (record == nullptr) {
445         result = -1;
446     }
447     EXPECT_NE(RESULT_OK, result);
448 }
449 
450 /**
451  * @tc.name: NWebPasteboardAdapter_GetRecordAt_017.
452  * @tc.desc: Test the GetRecordAt.
453  * @tc.type: FUNC
454  * @tc.require:issueI5O4B5
455  */
456 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetRecordAt_017, TestSize.Level1)
457 {
458     int result = 0;
459     std::size_t index = 0;
460     MockPasteData *mock1 = new MockPasteData();
461     g_dataAdapterNull->data_.reset((PasteData *)mock1);
462     std::shared_ptr<PasteDataRecordAdapter> str = g_dataAdapterNull->GetRecordAt(index);
463     if (str == nullptr) {
464         result = -1;
465     }
466     EXPECT_NE(RESULT_OK, result);
467     g_dataAdapterNull->data_ = nullptr;
468 }
469 
470 /**
471  * @tc.name: NWebPasteboardAdapter_GetRecordCount_018.
472  * @tc.desc: Test the GetRecordCount.
473  * @tc.type: FUNC
474  * @tc.require:issueI5O4B5
475  */
476 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetRecordCount_018, TestSize.Level1)
477 {
478     int result = 0;
479     std::size_t record = g_dataAdapterNull->GetRecordCount();
480     if (record != 0) {
481         result = -1;
482     }
483     EXPECT_EQ(RESULT_OK, result);
484     result = 0;
485     MockPasteData *mock2 = new MockPasteData();
486     g_dataAdapterNull->data_.reset((PasteData *)mock2);
487     std::size_t count = g_dataAdapterNull->GetRecordCount();
488     EXPECT_EQ(RESULT_OK, count);
489     g_dataAdapterNull->data_ = nullptr;
490 }
491 
492 /**
493  * @tc.name: NWebPasteboardAdapter_AllRecords_019.
494  * @tc.desc: Test the AllRecords.
495  * @tc.type: FUNC
496  * @tc.require:issueI5O4B5
497  */
498 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_AllRecords_019, TestSize.Level1)
499 {
500     int result = 0;
501     PasteRecordList record = g_dataAdapterNull->AllRecords();
502     if (record.empty()) {
503         result = -1;
504     }
505     EXPECT_NE(RESULT_OK, result);
506     result = 0;
507     MockPasteData *mock = new MockPasteData();
508     g_dataAdapterNull->data_.reset((PasteData *)mock);
509     PasteRecordList str = g_dataAdapterNull->AllRecords();
510     if (str.empty()) {
511         result = -1;
512     }
513     EXPECT_NE(RESULT_OK, result);
514 }
515 
516 /**
517  * @tc.name: NWebPasteboardAdapter_SetPasteData_020.
518  * @tc.desc: Test the SetPasteData.
519  * @tc.type: FUNC
520  * @tc.require:issueI5O4B5
521  */
522 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_SetPasteData_020, TestSize.Level1)
523 {
524     PasteRecordList data;
525     std::shared_ptr<PasteDataRecordAdapter> record = PasteDataRecordAdapter::NewRecord("text/html");
526     EXPECT_NE(record, nullptr);
527     std::shared_ptr<std::string> pasteData = std::make_shared<std::string>("test");
528     EXPECT_NE(pasteData, nullptr);
529     record->SetHtmlText(pasteData);
530     data.push_back(record);
531     PasteBoardClientAdapterImpl::GetInstance().SetPasteData(data);
532     PasteBoardClientAdapterImpl::GetInstance().SetPasteData(data, CopyOptionMode::NONE);
533 }
534 
535 /**
536  * @tc.name: NWebPasteboardAdapter_GetPasteData_021.
537  * @tc.desc: Test the GetPasteData.
538  * @tc.type: FUNC
539  * @tc.require:issueI5O4B5
540  */
541 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetPasteData_021, TestSize.Level1)
542 {
543     PasteBoardClientAdapterImpl::GetInstance().Clear();
544     PasteRecordList data;
545     bool result = PasteBoardClientAdapterImpl::GetInstance().GetPasteData(data);
546     EXPECT_EQ(false, result);
547 }
548 
549 /**
550  * @tc.name: NWebPasteboardAdapter_HasPasteData_022.
551  * @tc.desc: Test the HasPasteData.
552  * @tc.type: FUNC
553  * @tc.require:issueI5O4B5
554  */
555 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_HasPasteData_022, TestSize.Level1)
556 {
557     bool result = PasteBoardClientAdapterImpl::GetInstance().HasPasteData();
558     EXPECT_EQ(false, result);
559 }
560 
561 /**
562  * @tc.name: NWebPasteboardAdapter_PasteDataRecordAdapterImpl_023.
563  * @tc.desc: Test the PasteDataRecordAdapterImpl.
564  * @tc.type: FUNC
565  * @tc.require:issueI5O4B5
566  */
567 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_PasteDataRecordAdapterImpl_023, TestSize.Level1)
568 {
569     int result = 0;
570     std::string mimeType = "test";
571     std::shared_ptr<PasteDataRecordAdapterImpl> datarecord = std::make_shared<PasteDataRecordAdapterImpl>(mimeType);
572     if (datarecord == nullptr) {
573         result = -1;
574     }
575     EXPECT_EQ(RESULT_OK, result);
576 }
577 
578 /**
579  * @tc.name: NWebPasteboardAdapter_NewRecord_024.
580  * @tc.desc: Test the NewRecord.
581  * @tc.type: FUNC
582  * @tc.require:issueI5O4B5
583  */
584 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_NewRecord_024, TestSize.Level1)
585 {
586     int result = 0;
587     std::string mimeType = "test";
588     std::shared_ptr<PasteDataRecordAdapter> record = PasteDataRecordAdapter::NewRecord(mimeType);
589     if (record == nullptr) {
590         result = -1;
591     }
592     EXPECT_EQ(RESULT_OK, result);
593 }
594 
595 /**
596  * @tc.name: NWebPasteboardAdapter_SetHtmlText_025.
597  * @tc.desc: Test the SetHtmlText.
598  * @tc.type: FUNC
599  * @tc.require:issueI5O4B5
600  */
601 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_SetHtmlText_025, TestSize.Level1)
602 {
603     int result = 0;
604     std::shared_ptr<std::string> htmlText = std::make_shared<std::string>("test");
605     if (htmlText == nullptr) {
606         result = -1;
607     }
608     EXPECT_EQ(RESULT_OK, result);
609     bool reset = g_datarecordnull->SetHtmlText(htmlText);
610     EXPECT_NE(TRUE_OK, reset);
611     reset = g_datarecord->SetHtmlText(htmlText);
612     EXPECT_EQ(TRUE_OK, reset);
613 }
614 
615 /**
616  * @tc.name: NWebPasteboardAdapter_SetPlainText_026.
617  * @tc.desc: Test the SetPlainText.
618  * @tc.type: FUNC
619  * @tc.require:issueI5O4B5
620  */
621 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_SetPlainText_026, TestSize.Level1)
622 {
623     int result = 0;
624     std::shared_ptr<std::string> plainText = std::make_shared<std::string>("test");
625     if (plainText == nullptr) {
626         result = -1;
627     }
628     EXPECT_EQ(RESULT_OK, result);
629     bool reset = g_datarecordnull->SetPlainText(plainText);
630     EXPECT_NE(TRUE_OK, reset);
631     reset = g_datarecord->SetPlainText(plainText);
632     EXPECT_EQ(TRUE_OK, reset);
633 }
634 
635 /**
636  * @tc.name: NWebPasteboardAdapter_ImageToClipboardAlphaType_027.
637  * @tc.desc: Test the ImageToClipboardAlphaType.
638  * @tc.type: FUNC
639  * @tc.require:issueI5O4B5
640  */
641 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_ImageToClipboardAlphaType_027, TestSize.Level1)
642 {
643     Media::ImageInfo imgInfo;
644     imgInfo.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
645     ClipBoardImageAlphaType result = g_datarecord->ImageToClipboardAlphaType(imgInfo);
646     EXPECT_EQ(result, ClipBoardImageAlphaType::ALPHA_TYPE_UNKNOWN);
647 
648     imgInfo.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
649     result = g_datarecord->ImageToClipboardAlphaType(imgInfo);
650     EXPECT_EQ(result, ClipBoardImageAlphaType::ALPHA_TYPE_OPAQUE);
651 
652     imgInfo.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
653     result = g_datarecord->ImageToClipboardAlphaType(imgInfo);
654     EXPECT_EQ(result, ClipBoardImageAlphaType::ALPHA_TYPE_PREMULTIPLIED);
655 
656     imgInfo.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
657     result = g_datarecord->ImageToClipboardAlphaType(imgInfo);
658     EXPECT_EQ(result, ClipBoardImageAlphaType::ALPHA_TYPE_UNKNOWN);
659 }
660 
661 /**
662  * @tc.name: NWebPasteboardAdapter_ImageToClipboardColorType_028.
663  * @tc.desc: Test the ImageToClipboardColorType.
664  * @tc.type: FUNC
665  * @tc.require:issueI5O4AZ
666  */
667 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_ImageToClipboardColorType_028, TestSize.Level1)
668 {
669     Media::ImageInfo imgInfo;
670     imgInfo.pixelFormat = Media::PixelFormat::RGBA_8888;
671     ClipBoardImageColorType result = g_datarecord->ImageToClipboardColorType(imgInfo);
672     EXPECT_EQ(result, ClipBoardImageColorType::COLOR_TYPE_RGBA_8888);
673 
674     imgInfo.pixelFormat = Media::PixelFormat::BGRA_8888;
675     result = g_datarecord->ImageToClipboardColorType(imgInfo);
676     EXPECT_EQ(result, ClipBoardImageColorType::COLOR_TYPE_BGRA_8888);
677 
678     imgInfo.pixelFormat = Media::PixelFormat::RGBA_F16;
679     result = g_datarecord->ImageToClipboardColorType(imgInfo);
680     EXPECT_EQ(result, ClipBoardImageColorType::COLOR_TYPE_UNKNOWN);
681 }
682 
683 /**
684  * @tc.name: NWebPasteboardAdapter_ClipboardToImageAlphaType_029.
685  * @tc.desc: Test the ClipboardToImageAlphaType.
686  * @tc.type: FUNC
687  * @tc.require:issueI5O4AZ
688  */
689 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_ClipboardToImageAlphaType_029, TestSize.Level1)
690 {
691     ClipBoardImageAlphaType alphaType = ClipBoardImageAlphaType::ALPHA_TYPE_UNKNOWN;
692     Media::AlphaType result = g_datarecord->ClipboardToImageAlphaType(alphaType);
693     EXPECT_EQ(result, Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN);
694 
695     alphaType = ClipBoardImageAlphaType::ALPHA_TYPE_OPAQUE;
696     result = g_datarecord->ClipboardToImageAlphaType(alphaType);
697     EXPECT_EQ(result, Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE);
698 
699     alphaType = ClipBoardImageAlphaType::ALPHA_TYPE_PREMULTIPLIED;
700     result = g_datarecord->ClipboardToImageAlphaType(alphaType);
701     EXPECT_EQ(result, Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL);
702 
703     alphaType = ClipBoardImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED;
704     result = g_datarecord->ClipboardToImageAlphaType(alphaType);
705     EXPECT_EQ(result, Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN);
706 }
707 
708 /**
709  * @tc.name: NWebPasteboardAdapter_ClipboardToImageColorType_030.
710  * @tc.desc: Test the ClipboardToImageColorType.
711  * @tc.type: FUNC
712  * @tc.require:issueI5O4AZ
713  */
714 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_ClipboardToImageColorType_030, TestSize.Level1)
715 {
716     ClipBoardImageColorType colorType = ClipBoardImageColorType::COLOR_TYPE_RGBA_8888;
717     Media::PixelFormat result = g_datarecord->ClipboardToImageColorType(colorType);
718     EXPECT_EQ(result, Media::PixelFormat::RGBA_8888);
719 
720     colorType = ClipBoardImageColorType::COLOR_TYPE_BGRA_8888;
721     result = g_datarecord->ClipboardToImageColorType(colorType);
722     EXPECT_EQ(result, Media::PixelFormat::BGRA_8888);
723 
724     colorType = ClipBoardImageColorType::COLOR_TYPE_UNKNOWN;
725     result = g_datarecord->ClipboardToImageColorType(colorType);
726     EXPECT_EQ(result, Media::PixelFormat::UNKNOWN);
727 }
728 
729 /**
730  * @tc.name: NWebPasteboardAdapter_SetImgData_031.
731  * @tc.desc: Test the SetImgData.
732  * @tc.type: FUNC
733  * @tc.require:issueI5O4AZ
734  */
735 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_SetImgData_031, TestSize.Level1)
736 {
737     uint32_t storage[][5] = {
738         {0xCA, 0xDA, 0xCA, 0xC9, 0xA3},
739         {0xAC, 0xA8, 0x89, 0x47, 0x87},
740         {0x4B, 0x25, 0x25, 0x25, 0x46},
741         {0x90, 0x1, 0x25, 0x41, 0x33},
742         {0x75, 0x55, 0x44, 0x20, 0x00},
743     };
744     ClipBoardImageData *image = new ClipBoardImageData;
745     image->colorType = ClipBoardImageColorType::COLOR_TYPE_BGRA_8888;
746     image->alphaType = ClipBoardImageAlphaType::ALPHA_TYPE_UNKNOWN;
747     image->data = storage[0];
748     image->dataSize = sizeof(storage);
749     image->rowBytes = 5;
750     image->width = 5;
751     image->height = 5;
752     std::shared_ptr<ClipBoardImageData> imageData(image);
753     bool reset = g_datarecord->SetImgData(imageData);
754     EXPECT_EQ(TRUE_OK, reset);
755     reset = g_datarecordnull->SetImgData(imageData);
756     EXPECT_NE(TRUE_OK, reset);
757     imageData->dataSize = 1;
758     reset = g_datarecordnull->SetImgData(imageData);
759     EXPECT_NE(TRUE_OK, reset);
760     imageData = nullptr;
761     reset = g_datarecordnull->SetImgData(imageData);
762     EXPECT_NE(TRUE_OK, reset);
763 }
764 
765 /**
766  * @tc.name: NWebPasteboardAdapter_GetImgData_032.
767  * @tc.desc: Test the GetImgData.
768  * @tc.type: FUNC
769  * @tc.require:issueI5O4AZ
770  */
771 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetImgData_032, TestSize.Level1)
772 {
773     ClipBoardImageData image;
774     bool reset = g_datarecordnull->GetImgData(image);
775     EXPECT_NE(TRUE_OK, reset);
776     reset = g_paster->GetImgData(image);
777     EXPECT_NE(TRUE_OK, reset);
778 }
779 
780 /**
781  * @tc.name: NWebPasteboardAdapter_GetImgData_033.
782  * @tc.desc: Test the GetImgData.
783  * @tc.type: FUNC
784  * @tc.require:issueI5O4AZ
785  */
786 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetImgData_033, TestSize.Level1)
787 {
788     ClipBoardImageData image;
789     bool reset = g_datarecord->GetImgData(image);
790     EXPECT_EQ(TRUE_OK, reset);
791 }
792 
793 /**
794  * @tc.name: NWebPasteboardAdapter_Clear_034.
795  * @tc.desc: Test the Clear.
796  * @tc.type: FUNC
797  * @tc.require:issueI5O4AZ
798  */
799 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_Clear_034, TestSize.Level1)
800 {
801     uint32_t bufferSize = 20;
802     EXPECT_NE(g_datarecord, nullptr);
803     if (g_datarecord->imgBuffer_ == nullptr) {
804         g_datarecord->imgBuffer_ = static_cast<uint8_t *>(calloc(static_cast<size_t>(bufferSize), sizeof(uint8_t)));
805     }
806     g_datarecord->Clear();
807 }
808 
809 /**
810  * @tc.name: NWebPasteboardAdapter_Clear_035.
811  * @tc.desc: Test the Clear.
812  * @tc.type: FUNC
813  * @tc.require:issueI5O4AZ
814  */
815 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_Clear_035, TestSize.Level1)
816 {
817     PasteRecordList data;
818     data.clear();
819     EXPECT_EQ(data.size(), 0);
820     PasteBoardClientAdapterImpl::GetInstance().Clear();
821     PasteBoardClientAdapterImpl::GetInstance().SetPasteData(data);
822     PasteBoardClientAdapterImpl::GetInstance().Clear();
823 }
824 
825 /**
826  * @tc.name: NWebPasteboardAdapter_SetUri_036.
827  * @tc.desc: Test the SetUri.
828  * @tc.type: FUNC
829  * @tc.require:issueI5QA3D
830  */
831 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_SetUri_036, TestSize.Level1)
832 {
833     int result = 0;
834     std::string emptyTestUri = "";
835     std::string testUri = "/data/test/path";
836     EXPECT_EQ(RESULT_OK, result);
837     bool reset = g_datarecordnull->SetUri(testUri);
838     EXPECT_NE(TRUE_OK, reset);
839     reset = g_datarecord->SetUri(emptyTestUri);
840     EXPECT_NE(TRUE_OK, reset);
841     reset = g_datarecord->SetUri(testUri);
842     EXPECT_EQ(TRUE_OK, reset);
843 }
844 
845 /**
846  * @tc.name: NWebPasteboardAdapter_SetCustomData_037.
847  * @tc.desc: Test the SetCustomData.
848  * @tc.type: FUNC
849  * @tc.require:issueI5QA3D
850  */
851 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_SetCustomData_037, TestSize.Level1)
852 {
853     int result = 0;
854     PasteCustomData emptyTestData;
855     std::string format = "format";
856     vector<uint8_t> data = {0, 1, 2};
857     PasteCustomData testData;
858     testData.insert(std::make_pair(format, data));
859     EXPECT_EQ(RESULT_OK, result);
860     bool reset = g_datarecordnull->SetCustomData(testData);
861     EXPECT_NE(TRUE_OK, reset);
862     reset = g_datarecord->SetCustomData(emptyTestData);
863     EXPECT_NE(TRUE_OK, reset);
864     reset = g_datarecord->SetCustomData(testData);
865     EXPECT_EQ(TRUE_OK, reset);
866 }
867 
868 /**
869  * @tc.name: NWebPasteboardAdapter_GetUri_038.
870  * @tc.desc: Test the GetUri.
871  * @tc.type: FUNC
872  * @tc.require:issueI5O4BN
873  */
874 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetUri_038, TestSize.Level1)
875 {
876     int result = 0;
877     g_datarecord->SetUri("/data/test/path");
878     std::shared_ptr<std::string> uri = g_datarecord->GetUri();
879     if (uri == nullptr) {
880         result = -1;
881     }
882     EXPECT_EQ(RESULT_OK, result);
883     g_datarecord->record_ = g_datarecord->builder_->SetUri(nullptr).Build();
884     uri = g_datarecord->GetUri();
885     if (uri == nullptr) {
886         result = -1;
887     }
888     EXPECT_NE(RESULT_OK, result);
889     uri = g_pasternull->GetUri();
890     if (uri == nullptr) {
891         result = -1;
892     }
893     EXPECT_NE(RESULT_OK, result);
894 }
895 
896 /**
897  * @tc.name: NWebPasteboardAdapter_GetCustomData_039.
898  * @tc.desc: Test the GetCustomData.
899  * @tc.type: FUNC
900  * @tc.require:issueI5O4BN
901  */
902 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_GetCustomData_039, TestSize.Level1)
903 {
904     int result = 0;
905     std::string format = "format";
906     vector<uint8_t> data = {0, 1, 2};
907     PasteCustomData testData;
908     g_datarecord->SetCustomData(testData);
909     std::shared_ptr<PasteCustomData> customData = g_datarecord->GetCustomData();
910     if (customData == nullptr) {
911         result = -1;
912     }
913     EXPECT_EQ(RESULT_OK, result);
914     g_datarecord->record_ = g_datarecord->builder_->SetCustomData(nullptr).Build();
915     customData = g_datarecord->GetCustomData();
916     if (customData == nullptr) {
917         result = -1;
918     }
919     EXPECT_NE(RESULT_OK, result);
920     customData = g_pasternull->GetCustomData();
921     if (customData == nullptr) {
922         result = -1;
923     }
924     EXPECT_NE(RESULT_OK, result);
925 }
926 
927 /**
928  * @tc.name: NWebPasteboardAdapter_OpenRemoteUri_040.
929  * @tc.desc: Test the OpenRemoteUri.
930  * @tc.type: FUNC
931  * @tc.require:issueI5O4BN
932  */
933 HWTEST_F(NWebPasteboardAdapterTest, NWebPasteboardAdapter_OpenRemoteUri_040, TestSize.Level1)
934 {
935     std::string testUri = "datashare:////#fdFromBinder=155";
936     int32_t fd = PasteBoardClientAdapterImpl::GetInstance().OpenRemoteUri(testUri);
937     EXPECT_EQ(155, fd);
938     std::string testInvalidUri = "www.example.com";
939     fd = PasteBoardClientAdapterImpl::GetInstance().OpenRemoteUri(testInvalidUri);
940     EXPECT_EQ(-1, fd);
941 }
942 
943 /**
944  * @tc.name: PasteBoardClientAdapterImpl_GetTokenId_041.
945  * @tc.desc: Test the GetTokenId.
946  * @tc.type: FUNC
947  * @tc.require:issueI5O4BN
948  */
949 HWTEST_F(NWebPasteboardAdapterTest, PasteBoardClientAdapterImpl_GetTokenId_041, TestSize.Level1)
950 {
951     PasteBoardClientAdapterImpl::GetInstance().Clear();
952     EXPECT_EQ(0, PasteBoardClientAdapterImpl::GetInstance().GetTokenId());
953 }
954 
955 /**
956  * @tc.name: PasteBoardClientAdapterImpl_IsLocalPaste_042.
957  * @tc.desc: Test the IsLocalPaste.
958  * @tc.type: FUNC
959  * @tc.require:issueI5O4BN
960  */
961 HWTEST_F(NWebPasteboardAdapterTest, PasteBoardClientAdapterImpl_IsLocalPaste_042, TestSize.Level1)
962 {
963     PasteBoardClientAdapterImpl::GetInstance().Clear();
964     EXPECT_EQ(false, PasteBoardClientAdapterImpl::GetInstance().IsLocalPaste());
965 }
966 
967 /**
968  * @tc.name: PasteboardObserverAdapter_OnPasteboardChanged_043.
969  * @tc.desc: Test the CreatePasteboardObserver.
970  * @tc.type: FUNC
971  * @tc.require:issueI5O4BN
972  */
973 HWTEST_F(NWebPasteboardAdapterTest, PasteboardObserverAdapter_OnPasteboardChanged_043, TestSize.Level1)
974 {
975     std::shared_ptr<PasteboardObserverAdapter> observer =
976         std::make_shared<MockPasteboardObserver>();
977     EXPECT_NE(observer, nullptr);
978     PasteboardObserverAdapterImpl observerImpl(observer);
979     observerImpl.OnPasteboardChanged();
980     observerImpl.observer_ = nullptr;
981     observerImpl.OnPasteboardChanged();
982 }
983 
984 /**
985  * @tc.name: PasteBoardClientAdapterImpl_AddPasteboardChangedObserver_044.
986  * @tc.desc: Test the AddPasteboardChangedObserver.
987  * @tc.type: FUNC
988  * @tc.require:issueI5O4BN
989  */
990 HWTEST_F(NWebPasteboardAdapterTest, PasteBoardClientAdapterImpl_AddPasteboardChangedObserver_044, TestSize.Level1)
991 {
992     std::shared_ptr<PasteboardObserverAdapter> observer =
993         std::make_shared<MockPasteboardObserver>();
994     std::shared_ptr<PasteboardObserverAdapter> observerInvalid =
995         std::make_shared<MockPasteboardObserver>();
996     PasteBoardClientAdapterImpl::GetInstance().AddPasteboardChangedObserver(observer);
997     PasteBoardClientAdapterImpl::GetInstance().AddPasteboardChangedObserver(nullptr);
998     EXPECT_EQ(1, PasteBoardClientAdapterImpl::GetInstance().reg_.size());
999     PasteBoardClientAdapterImpl::GetInstance().RemovePasteboardChangedObserver(observer);
1000     EXPECT_EQ(0, PasteBoardClientAdapterImpl::GetInstance().reg_.size());
1001     PasteBoardClientAdapterImpl::GetInstance().RemovePasteboardChangedObserver(observerInvalid);
1002     EXPECT_EQ(0, PasteBoardClientAdapterImpl::GetInstance().reg_.size());
1003     PasteBoardClientAdapterImpl::GetInstance().RemovePasteboardChangedObserver(nullptr);
1004     EXPECT_EQ(0, PasteBoardClientAdapterImpl::GetInstance().reg_.size());
1005 
1006     MiscServices::ShareOption option =
1007         PasteBoardClientAdapterImpl::GetInstance().TransitionCopyOption(CopyOptionMode::IN_APP);
1008     EXPECT_EQ(option, MiscServices::ShareOption::InApp);
1009     option = PasteBoardClientAdapterImpl::GetInstance().TransitionCopyOption(CopyOptionMode::LOCAL_DEVICE);
1010     EXPECT_EQ(option, MiscServices::ShareOption::LocalDevice);
1011     option = PasteBoardClientAdapterImpl::GetInstance().TransitionCopyOption(CopyOptionMode::CROSS_DEVICE);
1012     EXPECT_EQ(option, MiscServices::ShareOption::CrossDevice);
1013     option = PasteBoardClientAdapterImpl::GetInstance().TransitionCopyOption(CopyOptionMode::NONE);
1014     EXPECT_EQ(option, MiscServices::ShareOption::CrossDevice);
1015 }
1016 }