Fix: NDK Support Get Plain Content From UdsHtml

Signed-off-by: zhusiyuan <zhusiyuan2@huawei.com>
This commit is contained in:
zhusiyuan 2024-11-21 14:47:31 +08:00
parent 75ad2bcc9e
commit ea52ce7c1d
3 changed files with 132 additions and 2 deletions

View File

@ -203,6 +203,8 @@ std::shared_ptr<std::string> PasteDataEntry::ConvertToPlainText() const
auto object = std::get<std::shared_ptr<Object>>(entry);
if (utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::PLAIN_TEXT)) {
object->GetValue(UDMF::CONTENT, res);
} else if (utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::HTML)) {
object->GetValue(UDMF::PLAIN_CONTENT, res);
} else {
object->GetValue(UDMF::URL, res);
}

View File

@ -621,6 +621,9 @@ std::shared_ptr<EntryValue> PasteDataRecord::GetUDMFValue()
if (htmlText_ != nullptr) {
object->value_[UDMF::HTML_CONTENT] = *htmlText_;
}
if (plainText_ != nullptr) {
object->value_[UDMF::PLAIN_CONTENT] = *plainText_;
}
} else if (mimeType_ == MIMETYPE_PIXELMAP) {
object->value_[UDMF::UNIFORM_DATA_TYPE] = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_PIXEL_MAP);
if (pixelMap_ != nullptr) {
@ -703,6 +706,7 @@ void PasteDataRecord::AddEntry(const std::string &utdType, std::shared_ptr<Paste
pixelMap_ = value->ConvertToPixelMap();
} else if (mimeType_ == MIMETYPE_TEXT_HTML) {
htmlText_ = value->ConvertToHtml();
plainText_ = value->ConvertToPlainText();
} else if (mimeType_ == MIMETYPE_TEXT_PLAIN) {
plainText_ = value->ConvertToPlainText();
} else if (mimeType_ == MIMETYPE_TEXT_URI) {

View File

@ -35,7 +35,8 @@ using namespace testing::ext;
using namespace OHOS::Security::AccessToken;
using namespace OHOS::MiscServices;
namespace OHOS::Test {
namespace OHOS {
namespace Test {
class PasteboardCapiTest : public testing::Test {
public:
static void SetUpTestCase(void);
@ -872,4 +873,127 @@ HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData007, TestSize.Level1)
OH_Pasteboard_Destroy(pasteboard);
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData007 end");
}
/**
* @tc.name: OH_Pasteboard_GetDataWithMultiAttributes001
* @tc.desc: should get html & text when set html & text with https uri without tag
* @tc.type: FUNC
*/
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetDataWithMultiAttributes001, TestSize.Level1)
{
const char *htmlContent = "<p>Hello world!<img src=\"https://storage/local/files/Images/hello.png\"/></p>";
const char *plainContent = "Hello world!";
OH_UdsHtml *uHtml = OH_UdsHtml_Create();
OH_UdsHtml_SetContent(uHtml, htmlContent);
OH_UdsHtml_SetPlainContent(uHtml, plainContent);
OH_UdmfRecord *uRecord = OH_UdmfRecord_Create();
OH_UdmfRecord_AddHtml(uRecord, uHtml);
OH_UdsHtml_Destroy(uHtml);
uHtml = nullptr;
OH_UdmfData *uData = OH_UdmfData_Create();
OH_UdmfData_AddRecord(uData, uRecord);
OH_Pasteboard *pasteboard = OH_Pasteboard_Create();
int ret = OH_Pasteboard_SetData(pasteboard, uData);
OH_UdmfRecord_Destroy(uRecord);
OH_UdmfData_Destroy(uData);
uData = nullptr;
uRecord = nullptr;
EXPECT_EQ(ret, ERR_OK);
ret = -1;
uData = OH_Pasteboard_GetData(pasteboard, &ret);
OH_Pasteboard_Destroy(pasteboard);
pasteboard = nullptr;
EXPECT_EQ(ret, ERR_OK);
EXPECT_NE(uData, nullptr);
unsigned int count = 0;
OH_UdmfRecord **records = OH_UdmfData_GetRecords(uData, &count);
EXPECT_EQ(count, 1);
EXPECT_NE(records, nullptr);
if (count == 1 && records != nullptr) {
uHtml = OH_UdsHtml_Create();
OH_UdmfRecord_GetHtml(records[0], uHtml);
const char *htmlText = OH_UdsHtml_GetContent(uHtml);
const char *plainText = OH_UdsHtml_GetPlainContent(uHtml);
EXPECT_STREQ(htmlText, htmlContent);
EXPECT_STREQ(plainText, plainContent);
OH_UdsHtml_Destroy(uHtml);
uHtml = nullptr;
}
OH_UdmfData_Destroy(uData);
uData = nullptr;
}
/**
* @tc.name: OH_Pasteboard_GetDataWithMultiAttributes002
* @tc.desc: should get html & text when set html & text with https uri and tag
* @tc.type: FUNC
*/
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetDataWithMultiAttributes002, TestSize.Level1)
{
const char *htmlContent = "<p>Hello world!<img src=\"https://storage/local/files/Images/hello.png\"/></p>";
const char *plainContent = "Hello world!";
OH_UdsHtml *uHtml = OH_UdsHtml_Create();
OH_UdsHtml_SetContent(uHtml, htmlContent);
OH_UdsHtml_SetPlainContent(uHtml, plainContent);
OH_UdmfRecord *uRecord = OH_UdmfRecord_Create();
OH_UdmfRecord_AddHtml(uRecord, uHtml);
OH_UdsHtml_Destroy(uHtml);
uHtml = nullptr;
OH_UdmfData *uData = OH_UdmfData_Create();
OH_UdmfData_AddRecord(uData, uRecord);
OH_UdmfProperty *uProp = OH_UdmfProperty_Create(uData);
int ret = OH_UdmfProperty_SetTag(uProp, PasteData::WEBVIEW_PASTEDATA_TAG.c_str()); // set webview tag
EXPECT_EQ(ret, ERR_OK);
OH_Pasteboard *pasteboard = OH_Pasteboard_Create();
ret = OH_Pasteboard_SetData(pasteboard, uData);
OH_UdmfRecord_Destroy(uRecord);
OH_UdmfData_Destroy(uData);
uData = nullptr;
uRecord = nullptr;
EXPECT_EQ(ret, ERR_OK);
ret = -1;
uData = OH_Pasteboard_GetData(pasteboard, &ret);
OH_Pasteboard_Destroy(pasteboard);
pasteboard = nullptr;
EXPECT_EQ(ret, ERR_OK);
EXPECT_NE(uData, nullptr);
unsigned int count = 0;
OH_UdmfRecord **records = OH_UdmfData_GetRecords(uData, &count);
EXPECT_EQ(count, 1);
EXPECT_NE(records, nullptr);
if (count == 1 && records != nullptr) {
uHtml = OH_UdsHtml_Create();
OH_UdmfRecord_GetHtml(records[0], uHtml);
const char *htmlText = OH_UdsHtml_GetContent(uHtml);
const char *plainText = OH_UdsHtml_GetPlainContent(uHtml);
EXPECT_STREQ(htmlText, htmlContent);
EXPECT_STREQ(plainText, plainContent);
OH_UdsHtml_Destroy(uHtml);
uHtml = nullptr;
}
OH_UdmfData_Destroy(uData);
uData = nullptr;
}
} // namespace Test
} // namespace OHOS