summaryrefslogtreecommitdiffstats
path: root/sw/qa/extras/txtexport
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 05:54:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 05:54:39 +0000
commit267c6f2ac71f92999e969232431ba04678e7437e (patch)
tree358c9467650e1d0a1d7227a21dac2e3d08b622b2 /sw/qa/extras/txtexport
parentInitial commit. (diff)
downloadlibreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz
libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sw/qa/extras/txtexport')
-rw-r--r--sw/qa/extras/txtexport/data/UTF16LEBOMCRLF.txtbin0 -> 18 bytes
-rw-r--r--sw/qa/extras/txtexport/data/UTF16LECRLF.txtbin0 -> 16 bytes
-rw-r--r--sw/qa/extras/txtexport/data/UTF8BOMCRLF.txt2
-rw-r--r--sw/qa/extras/txtexport/data/UTF8CRLF.txt2
-rw-r--r--sw/qa/extras/txtexport/data/bullets.odtbin0 -> 10379 bytes
-rw-r--r--sw/qa/extras/txtexport/txtexport.cxx144
6 files changed, 148 insertions, 0 deletions
diff --git a/sw/qa/extras/txtexport/data/UTF16LEBOMCRLF.txt b/sw/qa/extras/txtexport/data/UTF16LEBOMCRLF.txt
new file mode 100644
index 0000000000..be232521ea
--- /dev/null
+++ b/sw/qa/extras/txtexport/data/UTF16LEBOMCRLF.txt
Binary files differ
diff --git a/sw/qa/extras/txtexport/data/UTF16LECRLF.txt b/sw/qa/extras/txtexport/data/UTF16LECRLF.txt
new file mode 100644
index 0000000000..b74e964113
--- /dev/null
+++ b/sw/qa/extras/txtexport/data/UTF16LECRLF.txt
Binary files differ
diff --git a/sw/qa/extras/txtexport/data/UTF8BOMCRLF.txt b/sw/qa/extras/txtexport/data/UTF8BOMCRLF.txt
new file mode 100644
index 0000000000..eed02bb9ff
--- /dev/null
+++ b/sw/qa/extras/txtexport/data/UTF8BOMCRLF.txt
@@ -0,0 +1,2 @@
+フー
+バー
diff --git a/sw/qa/extras/txtexport/data/UTF8CRLF.txt b/sw/qa/extras/txtexport/data/UTF8CRLF.txt
new file mode 100644
index 0000000000..62d4d44677
--- /dev/null
+++ b/sw/qa/extras/txtexport/data/UTF8CRLF.txt
@@ -0,0 +1,2 @@
+フー
+バー
diff --git a/sw/qa/extras/txtexport/data/bullets.odt b/sw/qa/extras/txtexport/data/bullets.odt
new file mode 100644
index 0000000000..43e0c2123f
--- /dev/null
+++ b/sw/qa/extras/txtexport/data/bullets.odt
Binary files differ
diff --git a/sw/qa/extras/txtexport/txtexport.cxx b/sw/qa/extras/txtexport/txtexport.cxx
new file mode 100644
index 0000000000..3d3154f34c
--- /dev/null
+++ b/sw/qa/extras/txtexport/txtexport.cxx
@@ -0,0 +1,144 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <swmodeltestbase.hxx>
+
+#include <com/sun/star/frame/XStorable.hpp>
+#include <com/sun/star/text/XTextDocument.hpp>
+
+#include <osl/thread.hxx>
+#include <comphelper/propertyvalue.hxx>
+
+#include <formatlinebreak.hxx>
+
+class TxtExportTest : public SwModelTestBase
+{
+public:
+ TxtExportTest()
+ : SwModelTestBase("/sw/qa/extras/txtexport/data/", "Text")
+ {
+ }
+
+protected:
+ template <class T> std::vector<T> readMemoryStream()
+ {
+ SvMemoryStream aMemoryStream;
+ SvFileStream aStream(maTempFile.GetURL(), StreamMode::READ);
+ aStream.ReadStream(aMemoryStream);
+ const T* pData = static_cast<const T*>(aMemoryStream.GetData());
+ sal_uInt64 size = aMemoryStream.GetSize();
+ CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), size % sizeof(T));
+ return std::vector<T>(pData, pData + size / sizeof(T));
+ }
+
+ OString readExportedFile()
+ {
+ std::vector<char> aMemStream = readMemoryStream<char>();
+
+ int offset = 0;
+ if (aMemStream.size() > 2 && aMemStream[0] == '\xEF' && aMemStream[1] == '\xBB'
+ && aMemStream[2] == '\xBF')
+ offset = 3;
+
+ return OString(aMemStream.data() + offset, aMemStream.size() - offset);
+ }
+};
+
+#define DECLARE_TXTEXPORT_TEST(TestName, filename) \
+ DECLARE_SW_EXPORT_TEST(TestName, filename, nullptr, TxtExportTest)
+
+DECLARE_TXTEXPORT_TEST(testBullets, "bullets.odt")
+{
+ OString aData = readExportedFile();
+
+ OUString aString = OStringToOUString(
+ "1 Heading 1" SAL_NEWLINE_STRING "1.A Heading 2" SAL_NEWLINE_STRING
+ "Paragraph" SAL_NEWLINE_STRING "" SAL_NEWLINE_STRING
+ " \xe2\x80\xa2 First bullet" SAL_NEWLINE_STRING
+ " \xe2\x80\xa2 Second bullet" SAL_NEWLINE_STRING
+ " \xe2\x97\xa6 Sub-second bullet" SAL_NEWLINE_STRING
+ " Third bullet, but deleted" SAL_NEWLINE_STRING
+ " \xe2\x80\xa2 Fourth bullet" SAL_NEWLINE_STRING "" SAL_NEWLINE_STRING
+ "Numbering" SAL_NEWLINE_STRING "" SAL_NEWLINE_STRING " 1. First" SAL_NEWLINE_STRING
+ " 2. Second" SAL_NEWLINE_STRING " 1. Second-first" SAL_NEWLINE_STRING
+ " Third, but deleted" SAL_NEWLINE_STRING " 3. Actual third" SAL_NEWLINE_STRING
+ "" SAL_NEWLINE_STRING "Paragraph after numbering" SAL_NEWLINE_STRING
+ "Next paragraph" SAL_NEWLINE_STRING "Final paragraph" SAL_NEWLINE_STRING,
+ RTL_TEXTENCODING_UTF8);
+
+ // To get the stuff back in the system's encoding
+ OString aExpected(OUStringToOString(aString, osl_getThreadTextEncoding()));
+
+ CPPUNIT_ASSERT_EQUAL(aExpected, aData);
+}
+
+DECLARE_TXTEXPORT_TEST(testTdf120574_utf8bom, "UTF8BOMCRLF.txt")
+{
+ std::vector<char> aMemStream = readMemoryStream<char>();
+ OString aData(std::string_view(aMemStream.data(), aMemStream.size()));
+ CPPUNIT_ASSERT_EQUAL(u8"\uFEFFフー\r\nバー\r\n"_ostr, aData);
+}
+
+DECLARE_TXTEXPORT_TEST(testTdf120574_utf16lebom, "UTF16LEBOMCRLF.txt")
+{
+ std::vector<sal_Unicode> aMemStream = readMemoryStream<sal_Unicode>();
+ OUString aData(aMemStream.data(), aMemStream.size());
+ CPPUNIT_ASSERT_EQUAL(u"\uFEFFフー\r\nバー\r\n"_ustr, aData);
+}
+
+DECLARE_TXTEXPORT_TEST(testTdf142669_utf8, "UTF8CRLF.txt")
+{
+ std::vector<char> aMemStream = readMemoryStream<char>();
+ OString aData(std::string_view(aMemStream.data(), aMemStream.size()));
+ CPPUNIT_ASSERT_EQUAL(u8"フー\r\nバー\r\n"_ostr, aData);
+}
+
+DECLARE_TXTEXPORT_TEST(testTdf142669_utf16le, "UTF16LECRLF.txt")
+{
+ std::vector<sal_Unicode> aMemStream = readMemoryStream<sal_Unicode>();
+ OUString aData(aMemStream.data(), aMemStream.size());
+ CPPUNIT_ASSERT_EQUAL(u"フー\r\nバー\r\n"_ustr, aData);
+}
+
+CPPUNIT_TEST_FIXTURE(TxtExportTest, testClearingBreakExport)
+{
+ // Given a document with a clearing break:
+ createSwDoc();
+ uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
+ uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
+ uno::Reference<text::XTextContent> xLineBreak(
+ xMSF->createInstance("com.sun.star.text.LineBreak"), uno::UNO_QUERY);
+ uno::Reference<beans::XPropertySet> xLineBreakProps(xLineBreak, uno::UNO_QUERY);
+ auto eClear = static_cast<sal_Int16>(SwLineBreakClear::ALL);
+ xLineBreakProps->setPropertyValue("Clear", uno::Any(eClear));
+ uno::Reference<text::XText> xText = xTextDocument->getText();
+ uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
+ xText->insertString(xCursor, "foo", /*bAbsorb=*/false);
+ xText->insertTextContent(xCursor, xLineBreak, /*bAbsorb=*/false);
+ xText->insertString(xCursor, "bar", /*bAbsorb=*/false);
+
+ // When exporting to plain text:
+ uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+ uno::Sequence<beans::PropertyValue> aStoreProps = {
+ comphelper::makePropertyValue("FilterName", OUString("Text")),
+ };
+ xStorable->storeToURL(maTempFile.GetURL(), aStoreProps);
+
+ // Then make sure that the newline is not lost:
+ OString aActual = readExportedFile();
+ // Without the accompanying fix in place, this test would have failed with:
+ // - Expected: foo\nbar
+ // - Actual : foobar
+ // i.e. the clearing break was not downgraded to a plain line break.
+ CPPUNIT_ASSERT_EQUAL("foo\nbar" SAL_NEWLINE_STRING ""_ostr, aActual);
+}
+
+CPPUNIT_PLUGIN_IMPLEMENT();
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */