Debian Codegraph @

sw/qa/writerfilter/dmapper/DomainMapperTableHandler.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/beans/XPropertySet.hpp>
13 #include <com/sun/star/table/BorderLine2.hpp>
14 #include <com/sun/star/text/XTextTable.hpp>
15 #include <com/sun/star/text/XTextTablesSupplier.hpp>
16 #include <com/sun/star/text/XTextFramesSupplier.hpp>
17 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
18 #include <com/sun/star/text/XTextDocument.hpp>
19 #include <com/sun/star/style/BreakType.hpp>
20 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
21 #include <com/sun/star/text/XPageCursor.hpp>
22 #include <com/sun/star/qa/XDumper.hpp>
23
24 #include <frmatr.hxx>
25 #include <swtable.hxx>
26 #include <IDocumentLayoutAccess.hxx>
27 #include <rootfrm.hxx>
28 #include <pagefrm.hxx>
29 #include <sortedobjs.hxx>
30
31 using namespace ::com::sun::star;
32
33 namespace
34 {
35 /// Tests for sw/source/writerfilter/dmapper/DomainMapperTableHandler.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, test1cellInsidevRightborder)
46 {
47 loadFromFile(u"1cell-insidev-rightborder.docx");
48 uno::Reference<text::XTextTablesSupplier> xTextDocument(mxComponent, uno::UNO_QUERY);
49 uno::Reference<container::XIndexAccess> xTables(xTextDocument->getTextTables(), uno::UNO_QUERY);
50 uno::Reference<text::XTextTable> xTable(xTables->getByIndex(0), uno::UNO_QUERY);
51 uno::Reference<beans::XPropertySet> xCell(xTable->getCellByName(u"A1"_ustr), uno::UNO_QUERY);
52 table::BorderLine2 aBorder;
53 xCell->getPropertyValue(u"RightBorder"_ustr) >>= aBorder;
54 // Without the accompanying fix in place, this test would have failed with:
55 // - Expected: 0
56 // - Actual : 18
57 // i.e. the request to have no table-level right border was lost on import.
58 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(0), aBorder.LineWidth);
59 }
60
61 CPPUNIT_TEST_FIXTURE(Test, testNestedFloatingTable)
62 {
63 loadFromFile(u"nested-floating-table.docx");
64 // Normal outer table, floating inner tables.
65 uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(mxComponent, uno::UNO_QUERY);
66 uno::Reference<container::XIndexAccess> xIndexAccess(xTextTablesSupplier->getTextTables(),
67 uno::UNO_QUERY);
68 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), xIndexAccess->getCount());
69 }
70
71 CPPUNIT_TEST_FIXTURE(Test, testFloatingTableBreakBefore)
72 {
73 // Given a 3 pages document: page break, then a multi-page floating table on pages 2 and 3:
74 // When laying out that document:
75 loadFromFile(u"floattable-break-before.docx");
76
77 // Then make sure the page break property is on the anchor of the floating table, otherwise it
78 // has no effect:
79 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
80 uno::Reference<container::XEnumerationAccess> xText(xTextDocument->getText(), uno::UNO_QUERY);
81 uno::Reference<container::XEnumeration> xParagraphs = xText->createEnumeration();
82 xParagraphs->nextElement();
83 xParagraphs->nextElement();
84 uno::Reference<beans::XPropertySet> xParagraph(xParagraphs->nextElement(), uno::UNO_QUERY);
85 style::BreakType eBreakType{};
86 xParagraph->getPropertyValue(u"BreakType"_ustr) >>= eBreakType;
87 // Without the accompanying fix in place, this test would have failed with:
88 // - Expected: 4 (style::BreakType_PAGE_BEFORE)
89 // - Actual : 0 (style::BreakType_NONE)
90 // i.e. the page break was lost.
91 CPPUNIT_ASSERT_EQUAL(style::BreakType_PAGE_BEFORE, eBreakType);
92 }
93
94 CPPUNIT_TEST_FIXTURE(Test, test3NestedFloatingTables)
95 {
96 // Given a document with nested tables: outer and inner one is normal, middle one is floating:
97 // When laying out that document:
98 loadFromFile(u"floattable-nested-3tables.docx");
99
100 // Then make sure we don't crash and create the 3 tables:
101 // Without the accompanying fix in place, this would have crashed, layout can't handle nested
102 // floating tables currently.
103 uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(mxComponent, uno::UNO_QUERY);
104 uno::Reference<container::XIndexAccess> xIndexAccess(xTextTablesSupplier->getTextTables(),
105 uno::UNO_QUERY);
106 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), xIndexAccess->getCount());
107 }
108
109 CPPUNIT_TEST_FIXTURE(Test, testFloatingTablesOuterNonsplitInner)
110 {
111 // Given a document with a normal table, 3 outer floating tables and an inner floating table in
112 // the last floating table:
113 loadFromFile(u"floattable-outer-nonsplit-inner.docx");
114
115 // When counting the floating tables in the document:
116 uno::Reference<text::XTextFramesSupplier> xFramesSupplier(mxComponent, uno::UNO_QUERY);
117 uno::Reference<container::XIndexAccess> xFrames(xFramesSupplier->getTextFrames(),
118 uno::UNO_QUERY);
119
120 // Then make sure no floating tables are missing:
121 // Without the accompanying fix in place, this test would have failed with:
122 // - Expected: 4
123 // - Actual : 3
124 // i.e. the inner floating table was not floating.
125 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), xFrames->getCount());
126 }
127
128 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableHiddenAnchor)
129 {
130 // Given a document with a floating table, anchored in a paragraph that is hidden:
131 loadFromFile(u"floattable-hidden-anchor.docx");
132
133 // When checking the visibility of the anchor paragraph:
134 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
135 uno::Reference<container::XEnumerationAccess> xText(xTextDocument->getText(), uno::UNO_QUERY);
136 uno::Reference<container::XEnumeration> xParagraphs = xText->createEnumeration();
137 uno::Reference<beans::XPropertySet> xAnchor(xParagraphs->nextElement(), uno::UNO_QUERY);
138
139 // Then make sure the anchor (and thus the table) is visible:
140 bool bCharHidden{};
141 CPPUNIT_ASSERT(xAnchor->getPropertyValue(u"CharHidden"_ustr) >>= bCharHidden);
142 // Without the accompanying fix in place, this test would have failed, the paragraph + table was
143 // hidden.
144 CPPUNIT_ASSERT(!bCharHidden);
145 }
146
147 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableNested)
148 {
149 // Given a document with nested, multi-page floating tables:
150 // When loading that document:
151 loadFromFile(u"floattable-nested.docx");
152
153 // Then make sure that both floating tables are allowed to split:
154 uno::Reference<text::XTextFramesSupplier> xFramesSupplier(mxComponent, uno::UNO_QUERY);
155 uno::Reference<container::XIndexAccess> xFrames(xFramesSupplier->getTextFrames(),
156 uno::UNO_QUERY);
157 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xFrames->getCount());
158 // Outer frame:
159 uno::Reference<beans::XPropertySet> xFrame1;
160 xFrames->getByIndex(0) >>= xFrame1;
161 bool bIsSplitAllowed = false;
162 xFrame1->getPropertyValue(u"IsSplitAllowed"_ustr) >>= bIsSplitAllowed;
163 CPPUNIT_ASSERT(bIsSplitAllowed);
164 // Inner frame:
165 uno::Reference<beans::XPropertySet> xFrame2;
166 xFrames->getByIndex(1) >>= xFrame2;
167 bIsSplitAllowed = false;
168 xFrame2->getPropertyValue(u"IsSplitAllowed"_ustr) >>= bIsSplitAllowed;
169 // Without the accompanying fix in place, this test would have failed, the inner frame could not
170 // split.
171 CPPUNIT_ASSERT(bIsSplitAllowed);
172 }
173
174 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableHeader)
175 {
176 // Given a document with a header that has a floating table and some large images in the body
177 // text:
178 loadFromFile(u"floattable-header.docx");
179
180 // When breaking that document into pages:
181 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
182 uno::Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(
183 xModel->getCurrentController(), uno::UNO_QUERY);
184 uno::Reference<text::XPageCursor> xCursor(xTextViewCursorSupplier->getViewCursor(),
185 uno::UNO_QUERY);
186 xCursor->jumpToLastPage();
187
188 // Then make sure we get 2 pages:
189 sal_Int32 nLastPage = xCursor->getPage();
190 // Without the accompanying fix in place, this test would have failed, the page count went to
191 // 2233 pages and then there was a layout loop.
192 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), nLastPage);
193 }
194
195 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableFootnoteRedline)
196 {
197 // Given a document with a floating table in a footnote, with track changes recorded (but not
198 // visible):
199 // When importing that document:
200 loadFromFile(u"floattable-footnote-redline.docx");
201
202 // Then make sure the table is imported as inline, till Writer layout is ready to handle
203 // floating tables in footnotes:
204 uno::Reference<drawing::XDrawPageSupplier> xModel(mxComponent, uno::UNO_QUERY);
205 uno::Reference<drawing::XDrawPage> xDrawPage = xModel->getDrawPage();
206 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), xDrawPage->getCount());
207 }
208
209 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableHeaderBodyOverlap)
210 {
211 // Given a document with a floating table in a header, the floating table extends the header
212 // frame:
213 // When importing that document:
214 loadFromFile(u"floattable-header-overlap.docx");
215
216 // Then make sure the fly bottom is less than the top of the body text:
217 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
218 css::uno::Reference<qa::XDumper> xDumper(xModel->getCurrentController(), uno::UNO_QUERY);
219 OString aDump = xDumper->dump(u"layout"_ustr).toUtf8();
220 auto pCharBuffer = reinterpret_cast<const xmlChar*>(aDump.getStr());
221 xmlDocUniquePtr pXmlDoc(xmlParseDoc(pCharBuffer));
222 sal_Int32 nFlyBottom = getXPath(pXmlDoc, "//fly/infos/bounds", "bottom").toInt32();
223 // Body text top is body top + height of the first line, that's just a fly portion (kind of a
224 // top margin).
225 sal_Int32 nBodyTop = getXPath(pXmlDoc, "//page[1]/body/txt[1]/infos/bounds", "top").toInt32();
226 // Without the accompanying fix in place, this test would have failed, the first line was not a
227 // fly portion but it was actual text, above the floating table.
228 assertXPath(pXmlDoc, "//page[1]/body/txt[1]/SwParaPortion/SwLineLayout[1]/child::*", "type",
229 u"PortionType::Fly");
230 sal_Int32 nBodyFlyPortionHeight
231 = getXPath(pXmlDoc, "//page[1]/body/txt[1]/SwParaPortion/SwLineLayout[1]", "height")
232 .toInt32();
233 sal_Int32 nBodyTextTop = nBodyTop + nBodyFlyPortionHeight;
234 // Fly bottom was 3063, body text top was 7148.
235 CPPUNIT_ASSERT_LESS(nBodyTextTop, nFlyBottom);
236 }
237
238 CPPUNIT_TEST_FIXTURE(Test, testTableStyleParaBorderSpacing)
239 {
240 // Given a document with a table style, table style defines top and bottom border for
241 // paragraphs:
242 // When loading that document:
243 loadFromFile(u"table-style-para-border-spacing.docx");
244
245 // Then make sure the in-table paragraph gets its top border:
246 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
247 uno::Reference<container::XEnumerationAccess> xText(xTextDocument->getText(), uno::UNO_QUERY);
248 uno::Reference<container::XEnumeration> xParaEnum = xText->createEnumeration();
249 uno::Reference<text::XTextTable> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
250 uno::Reference<container::XEnumerationAccess> xCell(xPara->getCellByName(u"A1"_ustr),
251 uno::UNO_QUERY);
252 xParaEnum = xCell->createEnumeration();
253 uno::Reference<beans::XPropertySet> xParaProps(xParaEnum->nextElement(), uno::UNO_QUERY);
254 sal_Int32 nTopBorderDistance{};
255 xParaProps->getPropertyValue(u"TopBorderDistance"_ustr) >>= nTopBorderDistance;
256 // Without the accompanying fix in place, this test would have failed with:
257 // - Expected: 35
258 // - Actual : 0
259 // i.e. the top and bottom border and its 1pt spacing was not set on the in-table paragraph.
260 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(35), nTopBorderDistance);
261 }
262
263 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableNestedLayout)
264 {
265 // Given a floating table inside an outer table, both in body text:
266 // When loading that document:
267 loadFromFile(u"floattable-nested-layout.docx");
268
269 // Then make sure the inner table stays inside the outer table:
270 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
271 css::uno::Reference<qa::XDumper> xDumper(xModel->getCurrentController(), uno::UNO_QUERY);
272 OString aDump = xDumper->dump(u"layout"_ustr).toUtf8();
273 auto pCharBuffer = reinterpret_cast<const xmlChar*>(aDump.getStr());
274 xmlDocUniquePtr pXmlDoc(xmlParseDoc(pCharBuffer));
275 sal_Int32 nFlyTop = getXPath(pXmlDoc, "//fly/infos/bounds", "top").toInt32();
276 sal_Int32 nFlyBottom = getXPath(pXmlDoc, "//fly/infos/bounds", "bottom").toInt32();
277 sal_Int32 nTableTop = getXPath(pXmlDoc, "//page[1]/body/tab/infos/bounds", "top").toInt32();
278 sal_Int32 nTableBottom
279 = getXPath(pXmlDoc, "//page[1]/body/tab/infos/bounds", "bottom").toInt32();
280 // Without the accompanying fix in place, this test would have failed with:
281 // - Expected greater than: 1839
282 // - Actual : 360
283 // i.e. the floating table was outside the outer table.
284 CPPUNIT_ASSERT_GREATER(nTableTop, nFlyTop);
285 CPPUNIT_ASSERT_LESS(nTableBottom, nFlyBottom);
286 }
287
288 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableRedline)
289 {
290 // Given a document with 3 floating tables, the last table has a last cell with a custom cell
291 // background:
292 // When importing that document from DOCX:
293 createSwDoc("floattable-redline.docx");
294
295 // Then make sure that the cell background is not lost:
296 SwDoc* pDoc = getSwDoc();
297 sw::TableFrameFormats& rTableFormats = *pDoc->GetTableFrameFormats();
298 SwTableFormat* pTableFormat = rTableFormats[2];
299 SwTable* pTable = SwTable::FindTable(pTableFormat);
300 const SwTableBox* pCell = pTable->GetTableBox(u"B2"_ustr);
301 const SwAttrSet& rCellSet = pCell->GetFrameFormat()->GetAttrSet();
302 const SvxBrushItem& rCellBackground = rCellSet.GetBackground();
303 // Without the accompanying fix in place, this test would have failed with:
304 // - Expected: rgba[d0d8e8ff]
305 // - Actual : rgba[ffffff00]
306 // i.e. the cell background was white, should be gray.
307 CPPUNIT_ASSERT_EQUAL(Color(0xD0D8E8), rCellBackground.GetColor());
308 }
309
310 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableHidemark)
311 {
312 // Given a document with an inline table, then a floating table inside, the outer table's cell
313 // has "hide mark" set to true:
314 // When loading the document:
315 createSwDoc("floattable-hidemark.docx");
316
317 // Then make sure the resulting layout is correct:
318 SwDoc* pDoc = getSwDoc();
319 SwRootFrame* pLayout = pDoc->getIDocumentLayoutAccess().GetCurrentLayout();
320 SwPageFrame* pPage1 = pLayout->Lower()->DynCastPageFrame();
321 CPPUNIT_ASSERT(pPage1);
322 CPPUNIT_ASSERT(pPage1->GetSortedObjs());
323 const SwSortedObjs& rPage1Objs = *pPage1->GetSortedObjs();
324 // Without the accompanying fix in place, this test would have failed with:
325 // - Expected: 1
326 // - Actual : 2
327 // i.e. the follow fly was on page 1, not page 2.
328 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rPage1Objs.size());
329 SwPageFrame* pPage2 = pPage1->GetNext()->DynCastPageFrame();
330 CPPUNIT_ASSERT(pPage2);
331 CPPUNIT_ASSERT(pPage2->GetSortedObjs());
332 const SwSortedObjs& rPage2Objs = *pPage2->GetSortedObjs();
333 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rPage2Objs.size());
334 CPPUNIT_ASSERT(!pPage2->GetNext());
335 }
336 }
337
338 CPPUNIT_PLUGIN_IMPLEMENT();
339
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */