Debian Codegraph @

sw/qa/writerfilter/dmapper/DomainMapper.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10 #include <swmodeltestbase.hxx>
11
12 #include <com/sun/star/text/XTextDocument.hpp>
13 #include <com/sun/star/beans/XPropertySet.hpp>
14 #include <com/sun/star/beans/PropertyValues.hpp>
15 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
16 #include <com/sun/star/text/XTextTable.hpp>
17 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
18 #include <com/sun/star/document/XFilter.hpp>
19 #include <com/sun/star/document/XImporter.hpp>
20 #include <com/sun/star/awt/FontFamily.hpp>
21
22 #include <tools/UnitConversion.hxx>
23 #include <unotools/streamwrap.hxx>
24 #include <comphelper/propertyvalue.hxx>
25
26 #include <docsh.hxx>
27 #include <IDocumentLayoutAccess.hxx>
28 #include <rootfrm.hxx>
29 #include <pagefrm.hxx>
30
31 using namespace ::com::sun::star;
32
33 namespace
34 {
35 /// Tests for sw/source/writerfilter/dmapper/DomainMapper.cxx.
36 class Test : public SwModelTestBase
37 {
38 public:
39 Test()
40 : SwModelTestBase(u"/sw/qa/writerfilter/dmapper/data/"_ustr)
41 {
42 }
43 };
44
45 CPPUNIT_TEST_FIXTURE(Test, testLargeParaTopMargin)
46 {
47 // Given a document with a paragraph with a large "before" spacing.
48 loadFromFile(u"large-para-top-margin.docx");
49
50 // When checking the first paragraph.
51 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
52 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
53 uno::UNO_QUERY);
54 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
55 uno::Reference<beans::XPropertySet> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
56
57 // Then assert its top margin.
58 sal_Int32 nParaTopMargin{};
59 xPara->getPropertyValue(u"ParaTopMargin"_ustr) >>= nParaTopMargin;
60 // <w:spacing w:before="37050"/> in the document.
61 sal_Int32 nExpected = convertTwipToMm100(37050);
62 // Without the accompanying fix in place, this test would have failed with:
63 // - Expected: 65352
64 // - Actual : 0
65 // i.e. the paragraph margin was lost, which shifted the paragraph to the right (no top margin
66 // -> wrap around a TextBox), which shifted the triangle shape out of the page frame.
67 CPPUNIT_ASSERT_EQUAL(nExpected, nParaTopMargin);
68 }
69
70 CPPUNIT_TEST_FIXTURE(Test, testSdtRunInPara)
71 {
72 // Given a document with a block SDT, and inside that some content + a run SDT:
73 loadFromFile(u"sdt-run-in-para.docx");
74
75 // Then make sure the content inside the block SDT but outside the run SDT is not lost:
76 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
77 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
78 uno::UNO_QUERY);
79 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
80 uno::Reference<text::XTextRange> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
81 // Without the accompanying fix in place, this test would have failed with:
82 // - Expected: first-second
83 // - Actual : second
84 // i.e. the block-SDT-only string was lost.
85 CPPUNIT_ASSERT_EQUAL(u"first-second"_ustr, xPara->getString());
86 }
87
88 CPPUNIT_TEST_FIXTURE(Test, testSdtDropdownNoDisplayText)
89 {
90 // Given a document with <w:listItem w:value="..."/> (no display text):
91 loadFromFile(u"sdt-dropdown-no-display-text.docx");
92
93 // Then make sure we create a dropdown content control, not a rich text one:
94 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
95 uno::Reference<container::XEnumerationAccess> xParagraphsAccess(xTextDocument->getText(),
96 uno::UNO_QUERY);
97 uno::Reference<container::XEnumeration> xParagraphs = xParagraphsAccess->createEnumeration();
98 uno::Reference<container::XEnumerationAccess> xParagraph(xParagraphs->nextElement(),
99 uno::UNO_QUERY);
100 uno::Reference<container::XEnumeration> xPortions = xParagraph->createEnumeration();
101 uno::Reference<beans::XPropertySet> xTextPortion(xPortions->nextElement(), uno::UNO_QUERY);
102 OUString aPortionType;
103 xTextPortion->getPropertyValue(u"TextPortionType"_ustr) >>= aPortionType;
104 CPPUNIT_ASSERT_EQUAL(u"ContentControl"_ustr, aPortionType);
105 uno::Reference<text::XTextContent> xContentControl;
106 xTextPortion->getPropertyValue(u"ContentControl"_ustr) >>= xContentControl;
107 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
108 uno::Sequence<beans::PropertyValues> aListItems;
109 xContentControlProps->getPropertyValue(u"ListItems"_ustr) >>= aListItems;
110 // Without the accompanying fix in place, this test would have failed with:
111 // - Expected: 1
112 // - Actual : 0
113 // i.e. the list item was lost on import.
114 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aListItems.getLength());
115 }
116
117 CPPUNIT_TEST_FIXTURE(Test, testFloattableThenTable)
118 {
119 // Given a document with an in-section floating table, followed by a table:
120 // When laying out that document:
121 loadFromFile(u"floattable-then-table.docx");
122
123 // Then make sure that instead of crashing, the floating table is anchored inside the body text
124 // (and not a cell):
125 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
126 uno::Reference<text::XText> xBodyText = xTextDocument->getText();
127 uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
128 uno::Reference<drawing::XDrawPage> xDrawPage = xDrawPageSupplier->getDrawPage();
129 uno::Reference<text::XTextContent> xShape(xDrawPage->getByIndex(0), uno::UNO_QUERY);
130 uno::Reference<text::XTextRange> xAnchor = xShape->getAnchor();
131 // Make sure the anchor text is the body text, not some cell.
132 CPPUNIT_ASSERT_EQUAL(xBodyText, xAnchor->getText());
133 }
134
135 CPPUNIT_TEST_FIXTURE(Test, testSdtBlockText)
136 {
137 // Given a document with a block SDT that only contains text:
138 loadFromFile(u"sdt-block-text.docx");
139
140 // Then make sure that the text inside the SDT is imported as a content control:
141 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
142 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
143 uno::UNO_QUERY);
144 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
145 uno::Reference<container::XEnumerationAccess> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
146 uno::Reference<container::XEnumeration> xPortionEnum = xPara->createEnumeration();
147 uno::Reference<beans::XPropertySet> xPortion(xPortionEnum->nextElement(), uno::UNO_QUERY);
148 OUString aTextPortionType;
149 xPortion->getPropertyValue(u"TextPortionType"_ustr) >>= aTextPortionType;
150 // Without the accompanying fix in place, this test would have failed with:
151 // - Expected: ContentControl
152 // - Actual : TextField
153 // i.e. the SDT was imported as a text field, not as a content control.
154 CPPUNIT_ASSERT_EQUAL(u"ContentControl"_ustr, aTextPortionType);
155
156 // Make sure the properties are imported
157 uno::Reference<text::XTextContent> xContentControl;
158 xPortion->getPropertyValue(u"ContentControl"_ustr) >>= xContentControl;
159 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
160 OUString aAlias;
161 xContentControlProps->getPropertyValue(u"Alias"_ustr) >>= aAlias;
162 CPPUNIT_ASSERT_EQUAL(u"myalias"_ustr, aAlias);
163 }
164
165 CPPUNIT_TEST_FIXTURE(Test, testFdo78333)
166 {
167 // just care that it doesn't crash/assert
168 loadFromFile(u"fdo78333-1-minimized.docx");
169 }
170
171 CPPUNIT_TEST_FIXTURE(Test, testTdf158360)
172 {
173 // just test that doc with annotation in TOC doesn't crash/assert
174 loadFromFile(u"tdf158360.docx");
175 }
176
177 CPPUNIT_TEST_FIXTURE(Test, testTableStyleParaBorder)
178 {
179 // Given a document with a table, table style defines 115 twips left cell margin and an empty
180 // paragraph border:
181 // When importing that file:
182 loadFromFile(u"table-style-para-border.docx");
183
184 // Then make sure the cell margin is not lost:
185 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
186 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
187 uno::UNO_QUERY);
188 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
189 uno::Reference<text::XTextTable> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
190 uno::Reference<beans::XPropertySet> xCell(xPara->getCellByName(u"A1"_ustr), uno::UNO_QUERY);
191 sal_Int32 nLeftBorderDistance{};
192 xCell->getPropertyValue(u"LeftBorderDistance"_ustr) >>= nLeftBorderDistance;
193 // Without the accompanying fix in place, this test would have failed with:
194 // - Expected: 203
195 // - Actual : 0
196 // i.e. the 0 para border distance was applied on the cell instead.
197 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(203), nLeftBorderDistance);
198 }
199
200 CPPUNIT_TEST_FIXTURE(Test, testRTFStylePaste)
201 {
202 // Given an empty Writer document:
203 loadFromURL(u"private:factory/swriter"_ustr);
204
205 // When pasting RTF that has unreferenced paragraph styles:
206 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
207 uno::Reference<text::XTextRange> xText = xTextDocument->getText();
208 uno::Reference<text::XTextRange> xBodyEnd = xText->getEnd();
209 uno::Reference<document::XFilter> xFilter(
210 m_xSFactory->createInstance(u"com.sun.star.comp.Writer.RtfFilter"_ustr), uno::UNO_QUERY);
211 uno::Reference<document::XImporter> xImporter(xFilter, uno::UNO_QUERY);
212 xImporter->setTargetDocument(mxComponent);
213 std::unique_ptr<SvStream> pStream(
214 new SvFileStream(createFileURL(u"clipboard.rtf"), StreamMode::READ));
215 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(std::move(pStream)));
216 uno::Sequence aDescriptor{ comphelper::makePropertyValue(u"InputStream"_ustr, xStream),
217 comphelper::makePropertyValue(u"InsertMode"_ustr, true),
218 comphelper::makePropertyValue(u"TextInsertModeRange"_ustr,
219 xBodyEnd) };
220 CPPUNIT_ASSERT(xFilter->filter(aDescriptor));
221
222 // Then make sure those paragraph styles don't show up in the past result:
223 uno::Reference<style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(mxComponent,
224 uno::UNO_QUERY);
225 uno::Reference<container::XNameAccess> xStyleFamilies
226 = xStyleFamiliesSupplier->getStyleFamilies();
227 uno::Reference<container::XNameAccess> xStyleFamily(
228 xStyleFamilies->getByName(u"ParagraphStyles"_ustr), uno::UNO_QUERY);
229 // Without the accompanying fix in place, this test would have failed, 'Default Drawing Style'
230 // was imported, even if no pasted content referenced it.
231 CPPUNIT_ASSERT(!xStyleFamily->hasByName(u"Default Drawing Style"_ustr));
232 }
233
234 CPPUNIT_TEST_FIXTURE(Test, testRTFFontFamily)
235 {
236 // Given an RTF file with a 'Times New (W1)' font:
237 loadFromFile(u"font-family.rtf");
238
239 // When checking the font family:
240 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
241 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
242 uno::UNO_QUERY);
243 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
244 uno::Reference<container::XEnumerationAccess> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
245 uno::Reference<container::XEnumeration> xPortionEnum = xPara->createEnumeration();
246 uno::Reference<beans::XPropertySet> xPortion(xPortionEnum->nextElement(), uno::UNO_QUERY);
247 sal_Int16 eFamily{};
248 xPortion->getPropertyValue(u"CharFontFamily"_ustr) >>= eFamily;
249
250 // Then make sure it's roman:
251 // Without the accompanying fix in place, this test would have failed with:
252 // - Expected: 3 (ROMAN)
253 // - Actual : 0 (DONTKNOW)
254 // i.e. the final result was a sans fallback instead of a serif fallback.
255 CPPUNIT_ASSERT_EQUAL(awt::FontFamily::ROMAN, eFamily);
256 }
257
258 CPPUNIT_TEST_FIXTURE(Test, testFrameprPagebreakStyle)
259 {
260 // Given a document with a heading 1 style that sets both framePr and pageBreakBefore:
261 // When loading that document:
262 createSwDoc("framepr-pagebreak-style.docx");
263
264 // Then make sure the page break request is not ignored:
265 SwDocShell* pDocShell = getSwDocShell();
266 SwDoc* pDoc = pDocShell->GetDoc();
267 IDocumentLayoutAccess& rIDLA = pDoc->getIDocumentLayoutAccess();
268 SwRootFrame* pLayout = rIDLA.GetCurrentLayout();
269 SwPageFrame* pLastPage = pLayout->GetLastPage();
270 // Without the accompanying fix in place, this test would have failed with:
271 // - Expected: 2
272 // - Actual : 1
273 // i.e. the page break was lost, the document didn't have the correct page count.
274 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(2), pLastPage->GetPhyPageNum());
275 }
276 }
277
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */