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 <shellio.hxx>
13 #include <unotextrange.hxx>
14 #include <iodetect.hxx>
15 #include <unotxdoc.hxx>
18 #include <rtl/ustrbuf.hxx>
20 class TxtImportTest
: public SwModelTestBase
24 : SwModelTestBase("/sw/qa/extras/txtimport/data/", "Text")
28 // Export & assert part of the document (defined by SwPaM).
29 void assertExportedRange(const OString
& aExpected
, SwPaM
& rPaM
)
31 WriterRef rAsciiWriter
;
32 SwReaderWriter::GetWriter(FILTER_TEXT
, OUString(), rAsciiWriter
);
33 CPPUNIT_ASSERT(rAsciiWriter
.is());
36 rAsciiWriter
->m_bUCS2_WithStartChar
= false;
38 SvMemoryStream aMemoryStream
;
40 SwWriter
aWriter(aMemoryStream
, rPaM
);
41 ErrCode nError
= aWriter
.Write(rAsciiWriter
);
42 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE
, nError
);
44 const char* pData
= static_cast<const char*>(aMemoryStream
.GetData());
45 OString
aResult(pData
, aMemoryStream
.GetSize());
47 CPPUNIT_ASSERT_EQUAL(aExpected
, aResult
);
51 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf112191
)
53 createSwDoc("bullets.odt");
54 SwXTextDocument
* pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
55 CPPUNIT_ASSERT(pTextDoc
);
56 SwDoc
* pDoc
= pTextDoc
->GetDocShell()->GetDoc();
59 // just the 5th paragraph - no bullet
60 uno::Reference
<text::XTextRange
> xPara(getParagraph(5));
61 SwUnoInternalPaM
aPaM(*pDoc
);
62 bool bSuccess
= sw::XTextRangeToSwPaM(aPaM
, xPara
);
63 CPPUNIT_ASSERT(bSuccess
);
65 assertExportedRange("First bullet", aPaM
);
67 // but when we extend to the next paragraph - now there are bullets
68 xPara
= getParagraph(6);
69 SwUnoInternalPaM
aPaM2(*pDoc
);
70 bSuccess
= sw::XTextRangeToSwPaM(aPaM2
, xPara
);
71 CPPUNIT_ASSERT(bSuccess
);
73 OUString aString
= OStringToOUString(" \xe2\x80\xa2 First bullet" SAL_NEWLINE_STRING
74 " \xe2\x80\xa2 Second bullet",
75 RTL_TEXTENCODING_UTF8
);
77 SwPaM
aPaM3(*aPaM2
.GetMark(), *aPaM
.GetPoint());
78 assertExportedRange(OUStringToOString(aString
, osl_getThreadTextEncoding()), aPaM3
);
81 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf60145_utf8withoutbom
)
83 createSwDoc("UTF8WITHOUTBOM.txt");
84 SwXTextDocument
* pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
85 CPPUNIT_ASSERT(pTextDoc
);
86 SwDoc
* pDoc
= pTextDoc
->GetDocShell()->GetDoc();
89 uno::Reference
<text::XTextRange
> xPara(getParagraph(1));
91 CPPUNIT_ASSERT_EQUAL(OUString(u
"漢a'"), xPara
->getString());
94 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf60145_utf8withbom
)
96 createSwDoc("UTF8WITHBOM.txt");
97 SwXTextDocument
* pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
98 CPPUNIT_ASSERT(pTextDoc
);
99 SwDoc
* pDoc
= pTextDoc
->GetDocShell()->GetDoc();
100 CPPUNIT_ASSERT(pDoc
);
102 uno::Reference
<text::XTextRange
> xPara(getParagraph(1));
104 CPPUNIT_ASSERT_EQUAL(OUString(u
"漢a'"), xPara
->getString());
107 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf60145_utf16lewithoutbom
)
109 createSwDoc("UTF16LEWITHOUTBOM.txt");
110 SwXTextDocument
* pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
111 CPPUNIT_ASSERT(pTextDoc
);
112 SwDoc
* pDoc
= pTextDoc
->GetDocShell()->GetDoc();
113 CPPUNIT_ASSERT(pDoc
);
115 uno::Reference
<text::XTextRange
> xPara(getParagraph(1));
117 CPPUNIT_ASSERT_EQUAL(OUString(u
"漢a'"), xPara
->getString());
120 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf60145_utf16lewithbom
)
122 createSwDoc("UTF16LEWITHBOM.txt");
123 SwXTextDocument
* pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
124 CPPUNIT_ASSERT(pTextDoc
);
125 SwDoc
* pDoc
= pTextDoc
->GetDocShell()->GetDoc();
126 CPPUNIT_ASSERT(pDoc
);
128 uno::Reference
<text::XTextRange
> xPara(getParagraph(1));
130 CPPUNIT_ASSERT_EQUAL(OUString(u
"漢a'"), xPara
->getString());
133 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf92161_gb18030
)
135 createSwDoc("GB18030.txt");
136 SwXTextDocument
* pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
137 CPPUNIT_ASSERT(pTextDoc
);
138 SwDoc
* pDoc
= pTextDoc
->GetDocShell()->GetDoc();
139 CPPUNIT_ASSERT(pDoc
);
141 uno::Reference
<text::XTextRange
> xPara(getParagraph(1));
143 CPPUNIT_ASSERT_EQUAL(OUString(u
"盖闻天地之数,有十二万九千六百岁为一元。"), xPara
->getString());
146 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf60145_utf16bewithoutbom
)
148 createSwDoc("UTF16BEWITHOUTBOM.txt");
149 SwXTextDocument
* pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
150 CPPUNIT_ASSERT(pTextDoc
);
151 SwDoc
* pDoc
= pTextDoc
->GetDocShell()->GetDoc();
152 CPPUNIT_ASSERT(pDoc
);
154 uno::Reference
<text::XTextRange
> xPara(getParagraph(1));
156 CPPUNIT_ASSERT_EQUAL(OUString(u
"漢a'"), xPara
->getString());
159 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf60145_utf16bewithbom
)
161 createSwDoc("UTF16BEWITHBOM.txt");
162 SwXTextDocument
* pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
163 CPPUNIT_ASSERT(pTextDoc
);
164 SwDoc
* pDoc
= pTextDoc
->GetDocShell()->GetDoc();
165 CPPUNIT_ASSERT(pDoc
);
167 uno::Reference
<text::XTextRange
> xPara(getParagraph(1));
169 CPPUNIT_ASSERT_EQUAL(OUString(u
"漢a'"), xPara
->getString());
172 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf115088
)
175 SwDoc
* pDoc
= getSwDoc();
176 SwWrtShell
* pWrtShell
= pDoc
->GetDocShell()->GetWrtShell();
177 pWrtShell
->Insert("1");
178 pWrtShell
->SplitNode();
179 pWrtShell
->Insert("1");
182 dispatchCommand(mxComponent
, ".uno:Cut", {});
183 pWrtShell
->Insert("test");
184 pWrtShell
->Left(SwCursorSkipMode::Chars
, /*bSelect=*/false, 4, /*bBasicCall=*/false);
185 dispatchCommand(mxComponent
, ".uno:PasteUnformatted", {});
186 uno::Reference
<text::XTextDocument
> xTextDocument(mxComponent
, uno::UNO_QUERY
);
187 OUString aActual
= xTextDocument
->getText()->getString().copy(0, 2);
188 // Without the accompanying fix in place, this test would have failed with:
191 CPPUNIT_ASSERT_EQUAL(OUString("1\n"), aActual
.replaceAll("\r", "\n"));
194 CPPUNIT_TEST_FIXTURE(TxtImportTest
, testTdf70423
)
197 SwDoc
* pDoc
= getSwDoc();
198 CPPUNIT_ASSERT(pDoc
);
200 SwWrtShell
* pWrtShell
= pDoc
->GetDocShell()->GetWrtShell();
202 constexpr sal_Int32 size
= 30000; // It should be multiple of 10
203 constexpr sal_Int32 parts
= size
/ 10;
205 rtl::OUStringBuffer
s(size
);
207 for (size_t i
= 0; i
< parts
; i
++)
209 s
.append("0123456789");
212 OUString aResStr
= s
.makeStringAndClear();
213 pWrtShell
->Insert(aResStr
);
215 saveAndReload("Text"); //Reloading the file again
217 // Without the fix, this test would have failed with:
220 CPPUNIT_ASSERT_EQUAL(1, getParagraphs());
222 uno::Reference
<text::XTextRange
> xPara(getParagraph(1));
223 OUString aPara
= xPara
->getString();
225 // Without the fix, this test would have failed with:
228 CPPUNIT_ASSERT_EQUAL(size
, aPara
.getLength());
230 //Matching the paragraph text and created string
231 CPPUNIT_ASSERT_EQUAL(aResStr
, aPara
);
234 CPPUNIT_PLUGIN_IMPLEMENT();
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */