Debian Codegraph @

sw/qa/writerfilter/ooxml/ooxml.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/style/LineSpacing.hpp>
14 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
15 #include <com/sun/star/text/XTextTablesSupplier.hpp>
16 #include <com/sun/star/text/XTextDocument.hpp>
17
18 #include <fmtcntnt.hxx>
19 #include <ndtxt.hxx>
20 #include <docsh.hxx>
21 #include <wrtsh.hxx>
22 #include <rootfrm.hxx>
23 #include <pagefrm.hxx>
24 #include <txtfrm.hxx>
25 #include <sortedobjs.hxx>
26 #include <anchoredobject.hxx>
27 #include <flyfrm.hxx>
28 #include <tabfrm.hxx>
29
30 using namespace ::com::sun::star;
31
32 namespace
33 {
34 /// Tests for sw/source/writerfilter/ooxml/.
35 class Test : public SwModelTestBase
36 {
37 public:
38 Test()
39 : SwModelTestBase(u"/sw/qa/writerfilter/ooxml/data/"_ustr)
40 {
41 }
42 };
43
44 CPPUNIT_TEST_FIXTURE(Test, testFloatingTablesLost)
45 {
46 // Given a document with 2 floating tables, the 2nd has an inner floating table as well:
47 loadFromFile(u"floattable-tables-lost.docx");
48
49 // When counting the created Writer tables:
50 uno::Reference<text::XTextTablesSupplier> xTextDocument(mxComponent, uno::UNO_QUERY);
51 uno::Reference<container::XIndexAccess> xTables(xTextDocument->getTextTables(), uno::UNO_QUERY);
52
53 // Then make sure that all 3 tables are imported:
54 // Without the accompanying fix in place, this test would have failed with:
55 // - Expected: 3
56 // - Actual : 1
57 // i.e. only the inner table was imported, the 2 others were lost.
58 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), xTables->getCount());
59 }
60
61 CPPUNIT_TEST_FIXTURE(Test, testFloatingTableLeak)
62 {
63 // Given an outer table and 2 inner tables at B1 start:
64 // When importing that document:
65 loadFromFile(u"floattable-leak.docx");
66
67 // Then make sure the body text only contains a table and an empty final paragraph:
68 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
69 uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
70 uno::UNO_QUERY);
71 uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
72 uno::Reference<lang::XServiceInfo> xTable(xParaEnum->nextElement(), uno::UNO_QUERY);
73 // Without the accompanying fix in place, this test would have failed, the document started with
74 // a paragraph instead of a table.
75 CPPUNIT_ASSERT(xTable->supportsService(u"com.sun.star.text.TextTable"_ustr));
76 uno::Reference<lang::XServiceInfo> xParagraph(xParaEnum->nextElement(), uno::UNO_QUERY);
77 CPPUNIT_ASSERT(xParagraph->supportsService(u"com.sun.star.text.Paragraph"_ustr));
78 CPPUNIT_ASSERT(!xParaEnum->hasMoreElements());
79 }
80
81 CPPUNIT_TEST_FIXTURE(Test, testRecursiveHeaderRels)
82 {
83 // Given a document with self-referencing rels in a header/footer:
84 loadFromFile(u"recursive_header_rels.docx");
85 // It should not crash/hang on load
86
87 // given an essentially corrupt document, which parses styles.xml multiple times,
88 uno::Reference<style::XStyleFamiliesSupplier> xSupplier(mxComponent, uno::UNO_QUERY);
89 uno::Reference<container::XNameAccess> xStyleFamilies = xSupplier->getStyleFamilies();
90 uno::Reference<container::XNameAccess> xStyleFamily(
91 xStyleFamilies->getByName(u"ParagraphStyles"_ustr), uno::UNO_QUERY_THROW);
92 uno::Reference<beans::XPropertySet> xStandard(xStyleFamily->getByName(u"Standard"_ustr),
93 uno::UNO_QUERY_THROW);
94 style::LineSpacing aLineSpacing;
95 xStandard->getPropertyValue(u"ParaLineSpacing"_ustr) >>= aLineSpacing;
96 // the proportional line spacing defined as default for the entire document should be 108%
97 CPPUNIT_ASSERT_EQUAL_MESSAGE("Default para style has 1.08 line-spacing", sal_Int16(108),
98 aLineSpacing.Height);
99 }
100
101 CPPUNIT_TEST_FIXTURE(Test, testNestedRuns)
102 {
103 // Given a document with nested <w:r> in a shape text:
104 // When importing that document:
105 createSwDoc("nested-runs.docx");
106
107 // Then make sure the shape text is not lost:
108 SwDoc* pDoc = getSwDoc();
109 sw::FrameFormats<sw::SpzFrameFormat*>& rFlys = *pDoc->GetSpzFrameFormats();
110 CPPUNIT_ASSERT(!rFlys.empty());
111 sw::SpzFrameFormat* pFly = rFlys[0];
112 const SwNodeIndex* pFlyStartIndex = pFly->GetContent().GetContentIdx();
113 CPPUNIT_ASSERT(pFlyStartIndex);
114 SwNodeIndex aNodeIndex(*pFlyStartIndex);
115 ++aNodeIndex;
116 SwTextNode* pTextNode = aNodeIndex.GetNode().GetTextNode();
117 CPPUNIT_ASSERT(pTextNode);
118 // Without the accompanying fix in place, this test would have failed, the shape was empty.
119 CPPUNIT_ASSERT_EQUAL(u"Test text box"_ustr, pTextNode->GetText());
120 }
121
122 CPPUNIT_TEST_FIXTURE(Test, testFloatingTableAnchorPos)
123 {
124 // Given a document with a floating table (text: C) and a visually first body paragraph (text:
125 // D):
126 // When loading that document:
127 createSwDoc("floattable-anchorpos.docx");
128
129 // Then make sure that D is not shifted down vertically, compared to C:
130 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
131 SwRootFrame* pLayout = pWrtShell->GetLayout();
132 SwPageFrame* pPage = pLayout->GetLower()->DynCastPageFrame();
133 SwTextFrame* pBodyPara1 = pPage->FindFirstBodyContent()->DynCastTextFrame();
134 CPPUNIT_ASSERT(pBodyPara1->HasSplitFlyDrawObjs());
135 SwSortedObjs& rFlys1 = *pBodyPara1->GetDrawObjs();
136 SwFlyFrame* pFly1 = rFlys1[0]->DynCastFlyFrame();
137 SwTextFrame* pFly1Text = pFly1->ContainsContent()->DynCastTextFrame();
138 CPPUNIT_ASSERT_EQUAL(u"C"_ustr, pFly1Text->GetText());
139 SwTwips nFlyTop = pFly1Text->getFrameArea().Top();
140 SwTextFrame* pBodyPara2 = pBodyPara1->GetNext()->DynCastTextFrame();
141 CPPUNIT_ASSERT_EQUAL(u"D"_ustr, pBodyPara2->GetText());
142 SwTwips nBodyTop = pBodyPara2->getFrameArea().Top();
143 SwTwips nDiff = std::abs(nBodyTop - nFlyTop);
144 // Without the accompanying fix in place, this test would have failed with:
145 // - Expected less or equal than: 1
146 // - Actual : 243
147 // i.e. the vertical position of D was too big.
148 CPPUNIT_ASSERT_LESSEQUAL(static_cast<SwTwips>(1), nDiff);
149 }
150
151 CPPUNIT_TEST_FIXTURE(Test, testTableWafterRowHeight)
152 {
153 // Given a document with a table, 2 rows, 2nd row's height is 12pt:
154 // When laying out that document:
155 createSwDoc("table-wafter-row-height.docx");
156
157 // Then make sure that <w:gridAfter> / <w:wAfter> doesn't increase the row height:
158 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
159 SwRootFrame* pLayout = pWrtShell->GetLayout();
160 SwPageFrame* pPage = pLayout->GetLower()->DynCastPageFrame();
161 auto pBeforeTableFrame = pPage->FindFirstBodyContent()->DynCastTextFrame();
162 auto pTabFrame = pBeforeTableFrame->GetNext()->DynCastTabFrame();
163 const SwFrame* pRow1Frame = pTabFrame->Lower();
164 assert(pRow1Frame);
165 const SwFrame* pRow2Frame = pRow1Frame->GetNext();
166 SwTwips nRow2Height = pRow2Frame->getFrameArea().Height();
167 // Without the accompanying fix in place, this test would have failed with:
168 // - Expected: 240
169 // - Actual : 580
170 // i.e. the row height was too large, should be exactly 12 points (11 points from line spacing,
171 // 1 point for the border).
172 CPPUNIT_ASSERT_EQUAL(static_cast<SwTwips>(240), nRow2Height);
173 }
174
175 CPPUNIT_TEST_FIXTURE(Test, testFloattableMultiNested)
176 {
177 // Given a document with 3 nested floating tables (outer, middle, inner):
178 // When laying out that document:
179 createSwDoc("floattable-multi-nested.docx");
180
181 // Then no layout loop should happen:
182 // Without the accompanying fix in place, this test would have failed with a layout loop.
183 xmlDocUniquePtr pXmlDoc = parseLayoutDump();
184 // Page 1 has the 3 fly masters, the innermost as inline is OK:
185 int nPage1Flys = countXPathNodes(pXmlDoc, "//page[1]/sorted_objs/fly");
186 CPPUNIT_ASSERT_GREATEREQUAL(2, nPage1Flys);
187 // Page 2 has the 3 fly follows, the innermost as inline is OK:
188 int nPage2Flys = countXPathNodes(pXmlDoc, "//page[2]/sorted_objs/fly");
189 CPPUNIT_ASSERT_GREATEREQUAL(2, nPage2Flys);
190 // 6 table frames: 3 masters and 3 follows, all listed on the page of the master's anchor:
191 int nTables = countXPathNodes(pXmlDoc, "//page[1]//tab");
192 CPPUNIT_ASSERT_EQUAL(6, nTables);
193 }
194 }
195
196 CPPUNIT_PLUGIN_IMPLEMENT();
197
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */