1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci* you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci* You may obtain a copy of the License at
6bc03f14fSopenharmony_ci*
7bc03f14fSopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci*
9bc03f14fSopenharmony_ci* Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci* See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci* limitations under the License.
14bc03f14fSopenharmony_ci*/
15bc03f14fSopenharmony_ci
16bc03f14fSopenharmony_ci#include <gtest/gtest.h>
17bc03f14fSopenharmony_ci
18bc03f14fSopenharmony_ci#include "pasteboard_client.h"
19bc03f14fSopenharmony_ci#include "pasteboard_hilog.h"
20bc03f14fSopenharmony_ci#include "tlv_object.h"
21bc03f14fSopenharmony_ci
22bc03f14fSopenharmony_cinamespace OHOS::MiscServices {
23bc03f14fSopenharmony_ciusing namespace testing::ext;
24bc03f14fSopenharmony_ciusing namespace OHOS::AAFwk;
25bc03f14fSopenharmony_ciclass TLVObjectTest : public testing::Test {
26bc03f14fSopenharmony_cipublic:
27bc03f14fSopenharmony_ci    static void SetUpTestCase(void);
28bc03f14fSopenharmony_ci    static void TearDownTestCase(void);
29bc03f14fSopenharmony_ci    void SetUp();
30bc03f14fSopenharmony_ci    void TearDown();
31bc03f14fSopenharmony_ci
32bc03f14fSopenharmony_ci    static std::shared_ptr<PasteDataRecord> GenRecord(std::uint32_t index);
33bc03f14fSopenharmony_ci};
34bc03f14fSopenharmony_ci
35bc03f14fSopenharmony_civoid TLVObjectTest::SetUpTestCase(void) {}
36bc03f14fSopenharmony_ci
37bc03f14fSopenharmony_civoid TLVObjectTest::TearDownTestCase(void) {}
38bc03f14fSopenharmony_ci
39bc03f14fSopenharmony_civoid TLVObjectTest::SetUp(void) {}
40bc03f14fSopenharmony_ci
41bc03f14fSopenharmony_civoid TLVObjectTest::TearDown(void) {}
42bc03f14fSopenharmony_ci
43bc03f14fSopenharmony_cistd::shared_ptr<PasteDataRecord> TLVObjectTest::GenRecord(std::uint32_t index)
44bc03f14fSopenharmony_ci{
45bc03f14fSopenharmony_ci    std::string indexStr = "";
46bc03f14fSopenharmony_ci    auto plainText = std::make_shared<std::string>("hello" + indexStr);
47bc03f14fSopenharmony_ci    auto htmlText = std::make_shared<std::string>("<span>hello" + indexStr + "</span>");
48bc03f14fSopenharmony_ci    auto uri = std::make_shared<OHOS::Uri>("dataability://hello" + indexStr + ".txt");
49bc03f14fSopenharmony_ci    std::shared_ptr<Want> want = std::make_shared<Want>();
50bc03f14fSopenharmony_ci    std::string key = "id";
51bc03f14fSopenharmony_ci    int32_t id = 456;
52bc03f14fSopenharmony_ci    want->SetParam(key, id);
53bc03f14fSopenharmony_ci
54bc03f14fSopenharmony_ci    PasteDataRecord::Builder builder(MIMETYPE_TEXT_HTML);
55bc03f14fSopenharmony_ci    auto record1 = builder.SetPlainText(plainText).SetHtmlText(htmlText).SetUri(uri).SetWant(want).Build();
56bc03f14fSopenharmony_ci    return record1;
57bc03f14fSopenharmony_ci}
58bc03f14fSopenharmony_ci
59bc03f14fSopenharmony_ci/**
60bc03f14fSopenharmony_ci* @tc.name: TLVOjbectTest001
61bc03f14fSopenharmony_ci* @tc.desc: test tlv coder.
62bc03f14fSopenharmony_ci* @tc.type: FUNC
63bc03f14fSopenharmony_ci* @tc.require:AR000H5I1D
64bc03f14fSopenharmony_ci* @tc.author: baoyayong
65bc03f14fSopenharmony_ci*/
66bc03f14fSopenharmony_ciHWTEST_F(TLVObjectTest, TLVOjbectTest001, TestSize.Level0)
67bc03f14fSopenharmony_ci{
68bc03f14fSopenharmony_ci    PasteData pasteData1;
69bc03f14fSopenharmony_ci    for (uint32_t i = 0; i < 10; ++i) {
70bc03f14fSopenharmony_ci        pasteData1.AddRecord(TLVObjectTest::GenRecord(i));
71bc03f14fSopenharmony_ci    }
72bc03f14fSopenharmony_ci
73bc03f14fSopenharmony_ci    std::vector<uint8_t> buffer;
74bc03f14fSopenharmony_ci    auto ret = pasteData1.Encode(buffer);
75bc03f14fSopenharmony_ci    ASSERT_TRUE(ret);
76bc03f14fSopenharmony_ci    ASSERT_EQ(buffer.size(), pasteData1.Count());
77bc03f14fSopenharmony_ci
78bc03f14fSopenharmony_ci    PasteData pasteData2;
79bc03f14fSopenharmony_ci    ret = pasteData2.Decode(buffer);
80bc03f14fSopenharmony_ci    EXPECT_TRUE(ret);
81bc03f14fSopenharmony_ci    EXPECT_EQ(pasteData2.GetRecordCount(), pasteData1.GetRecordCount());
82bc03f14fSopenharmony_ci
83bc03f14fSopenharmony_ci    for (uint32_t i = 0; i < 10; ++i) {
84bc03f14fSopenharmony_ci        auto record2 = pasteData2.GetRecordAt(i);
85bc03f14fSopenharmony_ci        auto record1 = pasteData1.GetRecordAt(i);
86bc03f14fSopenharmony_ci        EXPECT_EQ(*(record2->GetHtmlText()), *(record1->GetHtmlText()));
87bc03f14fSopenharmony_ci        EXPECT_EQ(*(record2->GetPlainText()), *(record1->GetPlainText()));
88bc03f14fSopenharmony_ci        EXPECT_TRUE(record2->GetUri()->ToString() == record1->GetUri()->ToString());
89bc03f14fSopenharmony_ci        EXPECT_EQ(record2->GetWant()->OperationEquals(*(record1->GetWant())), true);
90bc03f14fSopenharmony_ci    }
91bc03f14fSopenharmony_ci}
92bc03f14fSopenharmony_ci
93bc03f14fSopenharmony_ci/**
94bc03f14fSopenharmony_ci* @tc.name: TLVOjbectTest002
95bc03f14fSopenharmony_ci* @tc.desc: test tlv coder.
96bc03f14fSopenharmony_ci* @tc.type: FUNC
97bc03f14fSopenharmony_ci* @tc.require:AR000H5I1D
98bc03f14fSopenharmony_ci* @tc.author: baoyayong
99bc03f14fSopenharmony_ci*/
100bc03f14fSopenharmony_ciHWTEST_F(TLVObjectTest, TLVOjbectTest002, TestSize.Level0)
101bc03f14fSopenharmony_ci{
102bc03f14fSopenharmony_ci    std::shared_ptr<Want> want = std::make_shared<Want>();
103bc03f14fSopenharmony_ci    std::string key = "id";
104bc03f14fSopenharmony_ci    int32_t id = 456;
105bc03f14fSopenharmony_ci    Want wantIn = want->SetParam(key, id);
106bc03f14fSopenharmony_ci    auto pasteData1 = PasteboardClient::GetInstance()->CreateWantData(std::make_shared<Want>(wantIn));
107bc03f14fSopenharmony_ci
108bc03f14fSopenharmony_ci    std::vector<uint8_t> buffer;
109bc03f14fSopenharmony_ci    auto ret = pasteData1->Encode(buffer);
110bc03f14fSopenharmony_ci    ASSERT_TRUE(ret);
111bc03f14fSopenharmony_ci    ASSERT_EQ(buffer.size(), pasteData1->Count());
112bc03f14fSopenharmony_ci
113bc03f14fSopenharmony_ci    PasteData pasteData2;
114bc03f14fSopenharmony_ci    ret = pasteData2.Decode(buffer);
115bc03f14fSopenharmony_ci    EXPECT_TRUE(ret);
116bc03f14fSopenharmony_ci    EXPECT_EQ(pasteData2.GetRecordCount(), pasteData1->GetRecordCount());
117bc03f14fSopenharmony_ci
118bc03f14fSopenharmony_ci    auto record2 = pasteData2.GetRecordAt(0);
119bc03f14fSopenharmony_ci    auto record1 = pasteData1->GetRecordAt(0);
120bc03f14fSopenharmony_ci    EXPECT_EQ(record2->GetWant()->OperationEquals(*(record1->GetWant())), true);
121bc03f14fSopenharmony_ci}
122bc03f14fSopenharmony_ci
123bc03f14fSopenharmony_ci/**
124bc03f14fSopenharmony_ci* @tc.name: TLVOjbectTest003
125bc03f14fSopenharmony_ci* @tc.desc: test tlv coder map.
126bc03f14fSopenharmony_ci* @tc.type: FUNC
127bc03f14fSopenharmony_ci* @tc.require:AR000H5I1D
128bc03f14fSopenharmony_ci* @tc.author: baoyayong
129bc03f14fSopenharmony_ci*/
130bc03f14fSopenharmony_ciHWTEST_F(TLVObjectTest, TLVOjbectTest003, TestSize.Level0)
131bc03f14fSopenharmony_ci{
132bc03f14fSopenharmony_ci    std::string plainText = "plain text";
133bc03f14fSopenharmony_ci    auto pasteData1 = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
134bc03f14fSopenharmony_ci    std::vector<uint8_t> arrayBuffer(46);
135bc03f14fSopenharmony_ci    arrayBuffer = { 2, 7, 6, 8, 9 };
136bc03f14fSopenharmony_ci    std::string mimeType1 = "image/jpg";
137bc03f14fSopenharmony_ci    std::shared_ptr<MineCustomData> customData = std::make_shared<MineCustomData>();
138bc03f14fSopenharmony_ci    customData->AddItemData(mimeType1, arrayBuffer);
139bc03f14fSopenharmony_ci    PasteDataRecord::Builder builder(MIMETYPE_TEXT_PLAIN);
140bc03f14fSopenharmony_ci    std::shared_ptr<PasteDataRecord> pasteDataRecord = builder.SetCustomData(customData).Build();
141bc03f14fSopenharmony_ci    pasteData1->AddRecord(pasteDataRecord);
142bc03f14fSopenharmony_ci
143bc03f14fSopenharmony_ci    std::vector<uint8_t> buffer;
144bc03f14fSopenharmony_ci    auto ret = pasteData1->Encode(buffer);
145bc03f14fSopenharmony_ci    ASSERT_TRUE(ret);
146bc03f14fSopenharmony_ci    ASSERT_EQ(buffer.size(), pasteData1->Count());
147bc03f14fSopenharmony_ci
148bc03f14fSopenharmony_ci    PasteData pasteData2;
149bc03f14fSopenharmony_ci    ret = pasteData2.Decode(buffer);
150bc03f14fSopenharmony_ci    EXPECT_TRUE(ret);
151bc03f14fSopenharmony_ci    EXPECT_EQ(pasteData2.GetRecordCount(), pasteData1->GetRecordCount());
152bc03f14fSopenharmony_ci    EXPECT_EQ(pasteData2.GetRecordCount(), pasteData1->GetRecordCount());
153bc03f14fSopenharmony_ci
154bc03f14fSopenharmony_ci    auto record1 = pasteData1->GetRecordAt(0);
155bc03f14fSopenharmony_ci    auto record2 = pasteData2.GetRecordAt(0);
156bc03f14fSopenharmony_ci    ASSERT_TRUE(record2 != nullptr);
157bc03f14fSopenharmony_ci    auto custom2 = record2->GetCustomData();
158bc03f14fSopenharmony_ci    auto custom1 = record1->GetCustomData();
159bc03f14fSopenharmony_ci    ASSERT_TRUE(custom1 != nullptr && custom2 != nullptr);
160bc03f14fSopenharmony_ci    EXPECT_EQ(custom2->GetItemData().size(), custom1->GetItemData().size());
161bc03f14fSopenharmony_ci}
162bc03f14fSopenharmony_ci} // namespace OHOS::MiscServices