Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / qa / cppunittests / dmapper / DomainMapper.cxx
blob885443c5a210edc972d43049d145b4f48af338bf
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/unoapi_test.hxx>
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>
17 #include <tools/UnitConversion.hxx>
19 using namespace ::com::sun::star;
21 namespace
23 /// Tests for writerfilter/source/dmapper/DomainMapper.cxx.
24 class Test : public UnoApiTest
26 public:
27 Test()
28 : UnoApiTest("/writerfilter/qa/cppunittests/dmapper/data/")
33 CPPUNIT_TEST_FIXTURE(Test, testLargeParaTopMargin)
35 // Given a document with a paragraph with a large "before" spacing.
36 loadFromFile(u"large-para-top-margin.docx");
38 // When checking the first paragraph.
39 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
40 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
41 uno::UNO_QUERY);
42 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
43 uno::Reference<beans::XPropertySet> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
45 // Then assert its top margin.
46 sal_Int32 nParaTopMargin{};
47 xPara->getPropertyValue("ParaTopMargin") >>= nParaTopMargin;
48 // <w:spacing w:before="37050"/> in the document.
49 sal_Int32 nExpected = convertTwipToMm100(37050);
50 // Without the accompanying fix in place, this test would have failed with:
51 // - Expected: 65352
52 // - Actual : 0
53 // i.e. the paragraph margin was lost, which shifted the paragraph to the right (no top margin
54 // -> wrap around a TextBox), which shifted the triangle shape out of the page frame.
55 CPPUNIT_ASSERT_EQUAL(nExpected, nParaTopMargin);
58 CPPUNIT_TEST_FIXTURE(Test, testSdtRunInPara)
60 // Given a document with a block SDT, and inside that some content + a run SDT:
61 loadFromFile(u"sdt-run-in-para.docx");
63 // Then make sure the content inside the block SDT but outside the run SDT is not lost:
64 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
65 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
66 uno::UNO_QUERY);
67 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
68 uno::Reference<text::XTextRange> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
69 // Without the accompanying fix in place, this test would have failed with:
70 // - Expected: first-second
71 // - Actual : second
72 // i.e. the block-SDT-only string was lost.
73 CPPUNIT_ASSERT_EQUAL(OUString("first-second"), xPara->getString());
76 CPPUNIT_TEST_FIXTURE(Test, testSdtDropdownNoDisplayText)
78 // Given a document with <w:listItem w:value="..."/> (no display text):
79 loadFromFile(u"sdt-dropdown-no-display-text.docx");
81 // Then make sure we create a dropdown content control, not a rich text one:
82 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
83 uno::Reference<container::XEnumerationAccess> xParagraphsAccess(xTextDocument->getText(),
84 uno::UNO_QUERY);
85 uno::Reference<container::XEnumeration> xParagraphs = xParagraphsAccess->createEnumeration();
86 uno::Reference<container::XEnumerationAccess> xParagraph(xParagraphs->nextElement(),
87 uno::UNO_QUERY);
88 uno::Reference<container::XEnumeration> xPortions = xParagraph->createEnumeration();
89 uno::Reference<beans::XPropertySet> xTextPortion(xPortions->nextElement(), uno::UNO_QUERY);
90 OUString aPortionType;
91 xTextPortion->getPropertyValue("TextPortionType") >>= aPortionType;
92 CPPUNIT_ASSERT_EQUAL(OUString("ContentControl"), aPortionType);
93 uno::Reference<text::XTextContent> xContentControl;
94 xTextPortion->getPropertyValue("ContentControl") >>= xContentControl;
95 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
96 uno::Sequence<beans::PropertyValues> aListItems;
97 xContentControlProps->getPropertyValue("ListItems") >>= aListItems;
98 // Without the accompanying fix in place, this test would have failed with:
99 // - Expected: 1
100 // - Actual : 0
101 // i.e. the list item was lost on import.
102 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aListItems.getLength());
105 CPPUNIT_TEST_FIXTURE(Test, testFloattableThenTable)
107 // Given a document with an in-section floating table, followed by a table:
108 // When laying out that document:
109 loadFromFile(u"floattable-then-table.docx");
111 // Then make sure that instead of crashing, the floating table is anchored inside the body text
112 // (and not a cell):
113 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
114 uno::Reference<text::XText> xBodyText = xTextDocument->getText();
115 uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
116 uno::Reference<drawing::XDrawPage> xDrawPage = xDrawPageSupplier->getDrawPage();
117 uno::Reference<text::XTextContent> xShape(xDrawPage->getByIndex(0), uno::UNO_QUERY);
118 uno::Reference<text::XTextRange> xAnchor = xShape->getAnchor();
119 // Make sure the anchor text is the body text, not some cell.
120 CPPUNIT_ASSERT_EQUAL(xBodyText, xAnchor->getText());
123 CPPUNIT_TEST_FIXTURE(Test, testSdtBlockText)
125 // Given a document with a block SDT that only contains text:
126 loadFromFile(u"sdt-block-text.docx");
128 // Then make sure that the text inside the SDT is imported as a content control:
129 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
130 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
131 uno::UNO_QUERY);
132 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
133 uno::Reference<container::XEnumerationAccess> xPara(xParaEnum->nextElement(), uno::UNO_QUERY);
134 uno::Reference<container::XEnumeration> xPortionEnum = xPara->createEnumeration();
135 uno::Reference<beans::XPropertySet> xPortion(xPortionEnum->nextElement(), uno::UNO_QUERY);
136 OUString aTextPortionType;
137 xPortion->getPropertyValue("TextPortionType") >>= aTextPortionType;
138 // Without the accompanying fix in place, this test would have failed with:
139 // - Expected: ContentControl
140 // - Actual : TextField
141 // i.e. the SDT was imported as a text field, not as a content control.
142 CPPUNIT_ASSERT_EQUAL(OUString("ContentControl"), aTextPortionType);
144 // Make sure the properties are imported
145 uno::Reference<text::XTextContent> xContentControl;
146 xPortion->getPropertyValue("ContentControl") >>= xContentControl;
147 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
148 OUString aAlias;
149 xContentControlProps->getPropertyValue("Alias") >>= aAlias;
150 CPPUNIT_ASSERT_EQUAL(OUString("myalias"), aAlias);
153 CPPUNIT_TEST_FIXTURE(Test, testFdo78333)
155 // just care that it doesn't crash/assert
156 loadFromFile(u"fdo78333-1-minimized.docx");
159 CPPUNIT_TEST_FIXTURE(Test, testTdf158360)
161 // just test that doc with annotation in TOC doesn't crash/assert
162 loadFromFile(u"tdf158360.docx");
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */