From adb203bc05e3e36173cbd46b9951f79821a81799 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 11:27:54 +0200 Subject: Merging upstream version 4:24.2.1. Signed-off-by: Daniel Baumann --- editeng/qa/unit/core-test.cxx | 92 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'editeng/qa') diff --git a/editeng/qa/unit/core-test.cxx b/editeng/qa/unit/core-test.cxx index b5320f90e4..8d2a6c11d4 100644 --- a/editeng/qa/unit/core-test.cxx +++ b/editeng/qa/unit/core-test.cxx @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -79,6 +80,12 @@ public: /// Test Copy/Paste using Legacy Format void testCopyPaste(); + /// Test Paste using HTML + void testHTMLPaste(); + + /// Test Paste using an HTML fragment + void testHTMLFragmentPaste(); + /// Test Copy/Paste with selective selection over multiple paragraphs void testMultiParaSelCopyPaste(); @@ -125,6 +132,8 @@ public: CPPUNIT_TEST(testAutocorrect); CPPUNIT_TEST(testHyperlinkCopyPaste); CPPUNIT_TEST(testCopyPaste); + CPPUNIT_TEST(testHTMLPaste); + CPPUNIT_TEST(testHTMLFragmentPaste); CPPUNIT_TEST(testMultiParaSelCopyPaste); CPPUNIT_TEST(testTabsCopyPaste); CPPUNIT_TEST(testHyperlinkSearch); @@ -708,6 +717,89 @@ void Test::testCopyPaste() CPPUNIT_ASSERT_EQUAL( OUString(aText + aText), rDoc.GetParaAsString(sal_Int32(0)) ); } +/// XTransferable implementation that provides simple HTML content. +class TestHTMLTransferable : public cppu::WeakImplHelper +{ + OString m_aHTML; +public: + TestHTMLTransferable(const OString& rHTML); + uno::Any SAL_CALL getTransferData(const datatransfer::DataFlavor& rFlavor) override; + uno::Sequence SAL_CALL getTransferDataFlavors() override; + sal_Bool SAL_CALL isDataFlavorSupported(const datatransfer::DataFlavor& rFlavor) override; +}; + +TestHTMLTransferable::TestHTMLTransferable(const OString& rHTML) + : m_aHTML(rHTML) +{ +} + +uno::Any TestHTMLTransferable::getTransferData(const datatransfer::DataFlavor& rFlavor) +{ + if (rFlavor.MimeType != "text/html") + { + return {}; + } + + uno::Any aRet; + SvMemoryStream aStream; + aStream.WriteOString(m_aHTML); + aRet <<= uno::Sequence(static_cast(aStream.GetData()), aStream.GetSize()); + return aRet; +} + +uno::Sequence TestHTMLTransferable::getTransferDataFlavors() +{ + datatransfer::DataFlavor aFlavor; + aFlavor.DataType = cppu::UnoType>::get(); + aFlavor.MimeType = "text/html"; + aFlavor.HumanPresentableName = aFlavor.MimeType; + return { aFlavor }; +} + +sal_Bool TestHTMLTransferable::isDataFlavorSupported(const datatransfer::DataFlavor& rFlavor) +{ + return rFlavor.MimeType == "text/html" + && rFlavor.DataType == cppu::UnoType>::get(); +} + +void Test::testHTMLPaste() +{ + // Given an empty editeng document: + EditEngine aEditEngine(mpItemPool.get()); + EditDoc &rDoc = aEditEngine.GetEditDoc(); + OString aHTML("\ntest"_ostr); + uno::Reference< datatransfer::XTransferable > xData(new TestHTMLTransferable(aHTML)); + + // When trying to paste HTML: + aEditEngine.InsertText(xData, OUString(), rDoc.GetEndPaM(), true); + + // Then make sure the text gets pasted: + // Without the accompanying fix in place, this test would have failed with: + // - Expected: test + // - Actual : + // i.e. RTF and plain text paste worked, but not HTML. + CPPUNIT_ASSERT_EQUAL(OUString("test"), rDoc.GetParaAsString(static_cast(0))); +} + +void Test::testHTMLFragmentPaste() +{ + // Given an empty editeng document: + EditEngine aEditEngine(mpItemPool.get()); + EditDoc &rDoc = aEditEngine.GetEditDoc(); + OString aHTML("abc"_ostr); + uno::Reference< datatransfer::XTransferable > xData(new TestHTMLTransferable(aHTML)); + + // When trying to paste an HTML fragment: + aEditEngine.InsertText(xData, OUString(), rDoc.GetEndPaM(), true); + + // Then make sure the text gets pasted: + // Without the accompanying fix in place, this test would have failed with: + // - Expected: abc + // - Actual : + // i.e. a HTML fragment without a proper header was ignored on paste. + CPPUNIT_ASSERT_EQUAL(OUString("abc"), rDoc.GetParaAsString(static_cast(0))); +} + void Test::testMultiParaSelCopyPaste() { // Create EditEngine's instance -- cgit v1.2.3