1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <swmodeltestbase.hxx>
12 #include <com/sun/star/frame/XStorable.hpp>
13 #include <com/sun/star/text/XTextDocument.hpp>
15 #include <osl/thread.hxx>
16 #include <comphelper/propertyvalue.hxx>
18 #include <formatlinebreak.hxx>
20 class TxtExportTest
: public SwModelTestBase
24 : SwModelTestBase("/sw/qa/extras/txtexport/data/", "Text")
29 template <class T
> std::vector
<T
> readMemoryStream()
31 SvMemoryStream aMemoryStream
;
32 SvFileStream
aStream(maTempFile
.GetURL(), StreamMode::READ
);
33 aStream
.ReadStream(aMemoryStream
);
34 const T
* pData
= static_cast<const T
*>(aMemoryStream
.GetData());
35 sal_uInt64 size
= aMemoryStream
.GetSize();
36 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), size
% sizeof(T
));
37 return std::vector
<T
>(pData
, pData
+ size
/ sizeof(T
));
40 OString
readExportedFile()
42 std::vector
<char> aMemStream
= readMemoryStream
<char>();
45 if (aMemStream
.size() > 2 && aMemStream
[0] == '\xEF' && aMemStream
[1] == '\xBB'
46 && aMemStream
[2] == '\xBF')
49 return OString(aMemStream
.data() + offset
, aMemStream
.size() - offset
);
53 #define DECLARE_TXTEXPORT_TEST(TestName, filename) \
54 DECLARE_SW_EXPORT_TEST(TestName, filename, nullptr, TxtExportTest)
56 DECLARE_TXTEXPORT_TEST(testBullets
, "bullets.odt")
58 OString aData
= readExportedFile();
60 OUString aString
= OStringToOUString(
61 "1 Heading 1" SAL_NEWLINE_STRING
"1.A Heading 2" SAL_NEWLINE_STRING
62 "Paragraph" SAL_NEWLINE_STRING
"" SAL_NEWLINE_STRING
63 " \xe2\x80\xa2 First bullet" SAL_NEWLINE_STRING
64 " \xe2\x80\xa2 Second bullet" SAL_NEWLINE_STRING
65 " \xe2\x97\xa6 Sub-second bullet" SAL_NEWLINE_STRING
66 " Third bullet, but deleted" SAL_NEWLINE_STRING
67 " \xe2\x80\xa2 Fourth bullet" SAL_NEWLINE_STRING
"" SAL_NEWLINE_STRING
68 "Numbering" SAL_NEWLINE_STRING
"" SAL_NEWLINE_STRING
" 1. First" SAL_NEWLINE_STRING
69 " 2. Second" SAL_NEWLINE_STRING
" 1. Second-first" SAL_NEWLINE_STRING
70 " Third, but deleted" SAL_NEWLINE_STRING
" 3. Actual third" SAL_NEWLINE_STRING
71 "" SAL_NEWLINE_STRING
"Paragraph after numbering" SAL_NEWLINE_STRING
72 "Next paragraph" SAL_NEWLINE_STRING
"Final paragraph" SAL_NEWLINE_STRING
,
73 RTL_TEXTENCODING_UTF8
);
75 // To get the stuff back in the system's encoding
76 OString
aExpected(OUStringToOString(aString
, osl_getThreadTextEncoding()));
78 CPPUNIT_ASSERT_EQUAL(aExpected
, aData
);
81 DECLARE_TXTEXPORT_TEST(testTdf120574_utf8bom
, "UTF8BOMCRLF.txt")
83 std::vector
<char> aMemStream
= readMemoryStream
<char>();
84 OString
aData(std::string_view(aMemStream
.data(), aMemStream
.size()));
85 CPPUNIT_ASSERT_EQUAL(OString(u8
"\uFEFFフー\r\nバー\r\n"), aData
);
88 DECLARE_TXTEXPORT_TEST(testTdf120574_utf16lebom
, "UTF16LEBOMCRLF.txt")
90 std::vector
<sal_Unicode
> aMemStream
= readMemoryStream
<sal_Unicode
>();
91 OUString
aData(aMemStream
.data(), aMemStream
.size());
92 CPPUNIT_ASSERT_EQUAL(OUString(u
"\uFEFFフー\r\nバー\r\n"), aData
);
95 DECLARE_TXTEXPORT_TEST(testTdf142669_utf8
, "UTF8CRLF.txt")
97 std::vector
<char> aMemStream
= readMemoryStream
<char>();
98 OString
aData(std::string_view(aMemStream
.data(), aMemStream
.size()));
99 CPPUNIT_ASSERT_EQUAL(OString(u8
"フー\r\nバー\r\n"), aData
);
102 DECLARE_TXTEXPORT_TEST(testTdf142669_utf16le
, "UTF16LECRLF.txt")
104 std::vector
<sal_Unicode
> aMemStream
= readMemoryStream
<sal_Unicode
>();
105 OUString
aData(aMemStream
.data(), aMemStream
.size());
106 CPPUNIT_ASSERT_EQUAL(OUString(u
"フー\r\nバー\r\n"), aData
);
109 CPPUNIT_TEST_FIXTURE(TxtExportTest
, testClearingBreakExport
)
111 // Given a document with a clearing break:
113 uno::Reference
<lang::XMultiServiceFactory
> xMSF(mxComponent
, uno::UNO_QUERY
);
114 uno::Reference
<text::XTextDocument
> xTextDocument(mxComponent
, uno::UNO_QUERY
);
115 uno::Reference
<text::XTextContent
> xLineBreak(
116 xMSF
->createInstance("com.sun.star.text.LineBreak"), uno::UNO_QUERY
);
117 uno::Reference
<beans::XPropertySet
> xLineBreakProps(xLineBreak
, uno::UNO_QUERY
);
118 auto eClear
= static_cast<sal_Int16
>(SwLineBreakClear::ALL
);
119 xLineBreakProps
->setPropertyValue("Clear", uno::Any(eClear
));
120 uno::Reference
<text::XText
> xText
= xTextDocument
->getText();
121 uno::Reference
<text::XTextCursor
> xCursor
= xText
->createTextCursor();
122 xText
->insertString(xCursor
, "foo", /*bAbsorb=*/false);
123 xText
->insertTextContent(xCursor
, xLineBreak
, /*bAbsorb=*/false);
124 xText
->insertString(xCursor
, "bar", /*bAbsorb=*/false);
126 // When exporting to plain text:
127 uno::Reference
<frame::XStorable
> xStorable(mxComponent
, uno::UNO_QUERY
);
128 uno::Sequence
<beans::PropertyValue
> aStoreProps
= {
129 comphelper::makePropertyValue("FilterName", OUString("Text")),
131 xStorable
->storeToURL(maTempFile
.GetURL(), aStoreProps
);
133 // Then make sure that the newline is not lost:
134 OString aActual
= readExportedFile();
135 // Without the accompanying fix in place, this test would have failed with:
136 // - Expected: foo\nbar
138 // i.e. the clearing break was not downgraded to a plain line break.
139 CPPUNIT_ASSERT_EQUAL(OString("foo\nbar" SAL_NEWLINE_STRING
), aActual
);
142 CPPUNIT_PLUGIN_IMPLEMENT();
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */