Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / qa / cppunittests / dmapper / DomainMapperTableHandler.cxx
blob5924fb90bc2a3fb38e242919a7f9ffbae4c8a2b0
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 <test/unoapixml_test.hxx>
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>
24 using namespace ::com::sun::star;
26 namespace
28 /// Tests for writerfilter/source/dmapper/DomainMapperTableHandler.cxx.
29 class Test : public UnoApiXmlTest
31 public:
32 Test()
33 : UnoApiXmlTest("/writerfilter/qa/cppunittests/dmapper/data/")
38 CPPUNIT_TEST_FIXTURE(Test, test1cellInsidevRightborder)
40 loadFromFile(u"1cell-insidev-rightborder.docx");
41 uno::Reference<text::XTextTablesSupplier> xTextDocument(mxComponent, uno::UNO_QUERY);
42 uno::Reference<container::XIndexAccess> xTables(xTextDocument->getTextTables(), uno::UNO_QUERY);
43 uno::Reference<text::XTextTable> xTable(xTables->getByIndex(0), uno::UNO_QUERY);
44 uno::Reference<beans::XPropertySet> xCell(xTable->getCellByName("A1"), uno::UNO_QUERY);
45 table::BorderLine2 aBorder;
46 xCell->getPropertyValue("RightBorder") >>= aBorder;
47 // Without the accompanying fix in place, this test would have failed with:
48 // - Expected: 0
49 // - Actual : 18
50 // i.e. the request to have no table-level right border was lost on import.
51 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(0), aBorder.LineWidth);
54 CPPUNIT_TEST_FIXTURE(Test, testNestedFloatingTable)
56 loadFromFile(u"nested-floating-table.docx");
57 // Normal outer table, floating inner tables.
58 uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(mxComponent, uno::UNO_QUERY);
59 uno::Reference<container::XIndexAccess> xIndexAccess(xTextTablesSupplier->getTextTables(),
60 uno::UNO_QUERY);
61 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), xIndexAccess->getCount());
64 CPPUNIT_TEST_FIXTURE(Test, testFloatingTableBreakBefore)
66 // Given a 3 pages document: page break, then a multi-page floating table on pages 2 and 3:
67 // When laying out that document:
68 loadFromFile(u"floattable-break-before.docx");
70 // Then make sure the page break property is on the anchor of the floating table, otherwise it
71 // has no effect:
72 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
73 uno::Reference<container::XEnumerationAccess> xText(xTextDocument->getText(), uno::UNO_QUERY);
74 uno::Reference<container::XEnumeration> xParagraphs = xText->createEnumeration();
75 xParagraphs->nextElement();
76 xParagraphs->nextElement();
77 uno::Reference<beans::XPropertySet> xParagraph(xParagraphs->nextElement(), uno::UNO_QUERY);
78 style::BreakType eBreakType{};
79 xParagraph->getPropertyValue("BreakType") >>= eBreakType;
80 // Without the accompanying fix in place, this test would have failed with:
81 // - Expected: 4 (style::BreakType_PAGE_BEFORE)
82 // - Actual : 0 (style::BreakType_NONE)
83 // i.e. the page break was lost.
84 CPPUNIT_ASSERT_EQUAL(style::BreakType_PAGE_BEFORE, eBreakType);
87 CPPUNIT_TEST_FIXTURE(Test, test3NestedFloatingTables)
89 // Given a document with nested tables: outer and inner one is normal, middle one is floating:
90 // When laying out that document:
91 loadFromFile(u"floattable-nested-3tables.docx");
93 // Then make sure we don't crash and create the 3 tables:
94 // Without the accompanying fix in place, this would have crashed, layout can't handle nested
95 // floating tables currently.
96 uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(mxComponent, uno::UNO_QUERY);
97 uno::Reference<container::XIndexAccess> xIndexAccess(xTextTablesSupplier->getTextTables(),
98 uno::UNO_QUERY);
99 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), xIndexAccess->getCount());
102 CPPUNIT_TEST_FIXTURE(Test, testFloatingTablesOuterNonsplitInner)
104 // Given a document with a normal table, 3 outer floating tables and an inner floating table in
105 // the last floating table:
106 loadFromFile(u"floattable-outer-nonsplit-inner.docx");
108 // When counting the floating tables in the document:
109 uno::Reference<text::XTextFramesSupplier> xFramesSupplier(mxComponent, uno::UNO_QUERY);
110 uno::Reference<container::XIndexAccess> xFrames(xFramesSupplier->getTextFrames(),
111 uno::UNO_QUERY);
113 // Then make sure no floating tables are missing:
114 // Without the accompanying fix in place, this test would have failed with:
115 // - Expected: 4
116 // - Actual : 3
117 // i.e. the inner floating table was not floating.
118 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), xFrames->getCount());
121 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableHiddenAnchor)
123 // Given a document with a floating table, anchored in a paragraph that is hidden:
124 loadFromFile(u"floattable-hidden-anchor.docx");
126 // When checking the visibility of the anchor paragraph:
127 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
128 uno::Reference<container::XEnumerationAccess> xText(xTextDocument->getText(), uno::UNO_QUERY);
129 uno::Reference<container::XEnumeration> xParagraphs = xText->createEnumeration();
130 uno::Reference<beans::XPropertySet> xAnchor(xParagraphs->nextElement(), uno::UNO_QUERY);
132 // Then make sure the anchor (and thus the table) is visible:
133 bool bCharHidden{};
134 CPPUNIT_ASSERT(xAnchor->getPropertyValue("CharHidden") >>= bCharHidden);
135 // Without the accompanying fix in place, this test would have failed, the paragraph + table was
136 // hidden.
137 CPPUNIT_ASSERT(!bCharHidden);
140 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableNested)
142 // Given a document with nested, multi-page floating tables:
143 // When loading that document:
144 loadFromFile(u"floattable-nested.docx");
146 // Then make sure that both floating tables are allowed to split:
147 uno::Reference<text::XTextFramesSupplier> xFramesSupplier(mxComponent, uno::UNO_QUERY);
148 uno::Reference<container::XIndexAccess> xFrames(xFramesSupplier->getTextFrames(),
149 uno::UNO_QUERY);
150 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xFrames->getCount());
151 // Outer frame:
152 uno::Reference<beans::XPropertySet> xFrame1;
153 xFrames->getByIndex(0) >>= xFrame1;
154 bool bIsSplitAllowed = false;
155 xFrame1->getPropertyValue("IsSplitAllowed") >>= bIsSplitAllowed;
156 CPPUNIT_ASSERT(bIsSplitAllowed);
157 // Inner frame:
158 uno::Reference<beans::XPropertySet> xFrame2;
159 xFrames->getByIndex(1) >>= xFrame2;
160 bIsSplitAllowed = false;
161 xFrame2->getPropertyValue("IsSplitAllowed") >>= bIsSplitAllowed;
162 // Without the accompanying fix in place, this test would have failed, the inner frame could not
163 // split.
164 CPPUNIT_ASSERT(bIsSplitAllowed);
167 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableHeader)
169 // Given a document with a header that has a floating table and some large images in the body
170 // text:
171 loadFromFile(u"floattable-header.docx");
173 // When breaking that document into pages:
174 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
175 uno::Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(
176 xModel->getCurrentController(), uno::UNO_QUERY);
177 uno::Reference<text::XPageCursor> xCursor(xTextViewCursorSupplier->getViewCursor(),
178 uno::UNO_QUERY);
179 xCursor->jumpToLastPage();
181 // Then make sure we get 2 pages:
182 sal_Int32 nLastPage = xCursor->getPage();
183 // Without the accompanying fix in place, this test would have failed, the page count went to
184 // 2233 pages and then there was a layout loop.
185 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), nLastPage);
188 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableFootnoteRedline)
190 // Given a document with a floating table in a footnote, with track changes recorded (but not
191 // visible):
192 // When importing that document:
193 loadFromFile(u"floattable-footnote-redline.docx");
195 // Then make sure the table is imported as inline, till Writer layout is ready to handle
196 // floating tables in footnotes:
197 uno::Reference<drawing::XDrawPageSupplier> xModel(mxComponent, uno::UNO_QUERY);
198 uno::Reference<drawing::XDrawPage> xDrawPage = xModel->getDrawPage();
199 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), xDrawPage->getCount());
202 CPPUNIT_TEST_FIXTURE(Test, testDOCXFloatingTableHeaderBodyOverlap)
204 // Given a document with a floating table in a header, the floating table extends the header
205 // frame:
206 // When importing that document:
207 loadFromFile(u"floattable-header-overlap.docx");
209 // Then make sure the fly bottom is less than the top of the body text:
210 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
211 css::uno::Reference<qa::XDumper> xDumper(xModel->getCurrentController(), uno::UNO_QUERY);
212 OString aDump = xDumper->dump("layout").toUtf8();
213 auto pCharBuffer = reinterpret_cast<const xmlChar*>(aDump.getStr());
214 xmlDocUniquePtr pXmlDoc(xmlParseDoc(pCharBuffer));
215 sal_Int32 nFlyBottom = getXPath(pXmlDoc, "//fly/infos/bounds"_ostr, "bottom"_ostr).toInt32();
216 // Body text top is body top + height of the first line, that's just a fly portion (kind of a
217 // top margin).
218 sal_Int32 nBodyTop
219 = getXPath(pXmlDoc, "//page[1]/body/txt[1]/infos/bounds"_ostr, "top"_ostr).toInt32();
220 // Without the accompanying fix in place, this test would have failed, the first line was not a
221 // fly portion but it was actual text, above the floating table.
222 assertXPath(pXmlDoc, "//page[1]/body/txt[1]/SwParaPortion/SwLineLayout[1]/child::*"_ostr,
223 "type"_ostr, "PortionType::Fly");
224 sal_Int32 nBodyFlyPortionHeight
225 = getXPath(pXmlDoc, "//page[1]/body/txt[1]/SwParaPortion/SwLineLayout[1]"_ostr,
226 "height"_ostr)
227 .toInt32();
228 sal_Int32 nBodyTextTop = nBodyTop + nBodyFlyPortionHeight;
229 // Fly bottom was 3063, body text top was 7148.
230 CPPUNIT_ASSERT_LESS(nBodyTextTop, nFlyBottom);
234 CPPUNIT_PLUGIN_IMPLEMENT();
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */