Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / qa / cppunittests / rtftok / rtfdispatchsymbol.cxx
blobbf3ecc937ebc5edeb606361c262424861b697f24
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/text/XTextDocument.hpp>
13 #include <com/sun/star/beans/XPropertySet.hpp>
14 #include <com/sun/star/style/ParagraphAdjust.hpp>
15 #include <com/sun/star/qa/XDumper.hpp>
17 using namespace ::com::sun::star;
19 namespace
21 /// Tests for writerfilter/source/rtftok/rtfdispatchsymbol.cxx.
22 class Test : public UnoApiXmlTest
24 public:
25 Test()
26 : UnoApiXmlTest("/writerfilter/qa/cppunittests/rtftok/data/")
31 CPPUNIT_TEST_FIXTURE(Test, testPage)
33 // Given a file with a \page and 2 \par tokens:
34 loadFromFile(u"page.rtf");
36 // Then make sure we get exactly two paragraphs:
37 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
38 uno::Reference<container::XEnumerationAccess> xText(xTextDocument->getText(), uno::UNO_QUERY);
39 uno::Reference<container::XEnumeration> xParagraphs = xText->createEnumeration();
40 xParagraphs->nextElement();
41 xParagraphs->nextElement();
42 // Without the accompanying fix in place, this test would have failed, the document had 3
43 // paragraphs, not 2.
44 CPPUNIT_ASSERT(!xParagraphs->hasMoreElements());
47 CPPUNIT_TEST_FIXTURE(Test, testCenterAfterPage)
49 // Given a file with a \page, followed by a \qc:
50 // When loading that file:
51 loadFromFile(u"center-after-page.rtf");
53 // Then make sure that the last paragraph is centered:
54 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
55 uno::Reference<container::XEnumerationAccess> xText(xTextDocument->getText(), uno::UNO_QUERY);
56 uno::Reference<container::XEnumeration> xParagraphs = xText->createEnumeration();
57 xParagraphs->nextElement();
58 xParagraphs->nextElement();
59 uno::Reference<beans::XPropertySet> xParagraph(xParagraphs->nextElement(), uno::UNO_QUERY);
60 sal_Int16 eActual{};
61 CPPUNIT_ASSERT(xParagraph->getPropertyValue("ParaAdjust") >>= eActual);
62 // Without the accompanying fix in place, this test would have failed with:
63 // - Expected: 3 (CENTER)
64 // - Actual : 0 (LEFT)
65 // i.e. the paragraph alignment on the second page was lost.
66 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(style::ParagraphAdjust_CENTER), eActual);
69 CPPUNIT_TEST_FIXTURE(Test, testFloattableThenSectBreak)
71 // Given a document with a floating table, immediately followed by \sect:
72 // When loading that file:
73 loadFromFile(u"floattable-then-sect-break.rtf");
75 // Then make sure that the floating table is on the first page:
76 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
77 css::uno::Reference<qa::XDumper> xDumper(xModel->getCurrentController(), uno::UNO_QUERY);
78 OString aDump = xDumper->dump("layout").toUtf8();
79 auto pCharBuffer = reinterpret_cast<const xmlChar*>(aDump.getStr());
80 xmlDocUniquePtr pXmlDoc(xmlParseDoc(pCharBuffer));
81 // Without the accompanying fix in place, this test would have failed with:
82 // - Expected: 1
83 // - Actual : 0
84 // i.e. the floating table was on the 2nd page, not on the 1st page.
85 assertXPath(pXmlDoc, "/root/page[1]/sorted_objs/fly"_ostr, 1);
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */