android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / qa / extras / txtexport / txtexport.cxx
blob338af339884e1eaf108eb4d640b0264bc8138db9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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
22 public:
23 TxtExportTest()
24 : SwModelTestBase("/sw/qa/extras/txtexport/data/", "Text")
28 protected:
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>();
44 int offset = 0;
45 if (aMemStream.size() > 2 && aMemStream[0] == '\xEF' && aMemStream[1] == '\xBB'
46 && aMemStream[2] == '\xBF')
47 offset = 3;
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:
112 createSwDoc();
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
137 // - Actual : foobar
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: */