Debian Codegraph @

sw/qa/writerfilter/dmapper/PropertyMap.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/text/XPageCursor.hpp>
13 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
14 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
15 #include <com/sun/star/beans/XPropertySet.hpp>
16 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
17 #include <com/sun/star/table/BorderLine2.hpp>
18 #include <com/sun/star/text/XTextDocument.hpp>
19 #include <com/sun/star/document/XFilter.hpp>
20 #include <com/sun/star/document/XImporter.hpp>
21
22 #include <unotools/streamwrap.hxx>
23 #include <comphelper/propertyvalue.hxx>
24
25 #include <docsh.hxx>
26 #include <IDocumentLayoutAccess.hxx>
27 #include <rootfrm.hxx>
28 #include <pagefrm.hxx>
29
30 using namespace ::com::sun::star;
31
32 namespace
33 {
34 /// Tests for sw/source/writerfilter/dmapper/PropertyMap.cxx.
35 class Test : public SwModelTestBase
36 {
37 public:
38 Test()
39 : SwModelTestBase(u"/sw/qa/writerfilter/dmapper/data/"_ustr)
40 {
41 }
42 };
43
44 CPPUNIT_TEST_FIXTURE(Test, testFloatingTableHeader)
45 {
46 loadFromFile(u"floating-table-header.docx");
47 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
48 uno::Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(
49 xModel->getCurrentController(), uno::UNO_QUERY);
50 uno::Reference<text::XPageCursor> xCursor(xTextViewCursorSupplier->getViewCursor(),
51 uno::UNO_QUERY);
52 xCursor->jumpToLastPage();
53 // Without the accompanying fix in place, this test would have failed with:
54 // - Expected: 1
55 // - Actual : 3
56 // i.e. a document which is 1 page in Word was imported as a 3 page one.
57 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(1), xCursor->getPage());
58 }
59
60 // TODO - First Page Headers Support
61 CPPUNIT_TEST_FIXTURE(Test, testFollowPageTopMargin)
62 {
63 // Load a document with 2 pages: first page has larger top margin, second page has smaller top
64 // margin.
65 loadFromFile(u"follow-page-top-margin.docx");
66 uno::Reference<style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(mxComponent,
67 uno::UNO_QUERY);
68 uno::Reference<container::XNameAccess> xStyleFamilies
69 = xStyleFamiliesSupplier->getStyleFamilies();
70 uno::Reference<container::XNameAccess> xStyleFamily(
71 xStyleFamilies->getByName(u"PageStyles"_ustr), uno::UNO_QUERY);
72 uno::Reference<beans::XPropertySet> xStyle(xStyleFamily->getByName(u"Standard"_ustr),
73 uno::UNO_QUERY);
74 auto nTopMargin = xStyle->getPropertyValue(u"TopMargin"_ustr).get<sal_Int32>();
75
76 // Without the accompanying fix in place, this test would have failed with:
77 // - Expected: 250
78 // - Actual : 1249
79 // i.e. the top margin on page 2 was too large.
80 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1249), nTopMargin);
81 }
82
83 CPPUNIT_TEST_FIXTURE(Test, testTableNegativeVerticalPos)
84 {
85 // Given a document with a table which has a negative vertical position (moves up to overlap
86 // with the header):
87 loadFromFile(u"table-negative-vertical-pos.docx");
88
89 // Then make sure we don't import that as a plain table, which can't have a negative top margin:
90 uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
91 uno::Reference<drawing::XDrawPage> xDrawPage = xDrawPageSupplier->getDrawPage();
92 // Without the accompanying fix in place, this test would have failed with:
93 // - Expected: 1
94 // - Actual : 0
95 // i.e. this was imported as a plain table, resulting in a 0 top margin (y pos too large).
96 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xDrawPage->getCount());
97 }
98
99 CPPUNIT_TEST_FIXTURE(Test, testNegativePageBorder)
100 {
101 // Given a document with a top margin and a border which has more spacing than the margin:
102 loadFromFile(u"negative-page-border.docx");
103
104 // Then make sure that the border distance is negative, so it can appear at the correct
105 // position:
106 uno::Reference<style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(mxComponent,
107 uno::UNO_QUERY);
108 uno::Reference<container::XNameAccess> xStyleFamilies
109 = xStyleFamiliesSupplier->getStyleFamilies();
110 uno::Reference<container::XNameAccess> xStyleFamily(
111 xStyleFamilies->getByName(u"PageStyles"_ustr), uno::UNO_QUERY);
112 uno::Reference<beans::XPropertySet> xStyle(xStyleFamily->getByName(u"Standard"_ustr),
113 uno::UNO_QUERY);
114 auto nTopMargin = xStyle->getPropertyValue(u"TopMargin"_ustr).get<sal_Int32>();
115 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(501), nTopMargin);
116 auto aTopBorder = xStyle->getPropertyValue(u"TopBorder"_ustr).get<table::BorderLine2>();
117 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(159), aTopBorder.LineWidth);
118 auto nTopBorderDistance = xStyle->getPropertyValue(u"TopBorderDistance"_ustr).get<sal_Int32>();
119 // Without the accompanying fix in place, this test would have failed with:
120 // - Expected: -646
121 // - Actual : 0
122 // i.e. the border negative distance was lost.
123 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(-646), nTopBorderDistance);
124 }
125
126 CPPUNIT_TEST_FIXTURE(Test, testNegativePageBorderNoMargin)
127 {
128 // Given a document with no top margin and a border which has spacing:
129 loadFromFile(u"negative-page-border-no-margin.docx");
130
131 // Then make sure that the border distance is negative, so it can appear at the correct
132 // position:
133 uno::Reference<style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(mxComponent,
134 uno::UNO_QUERY);
135 uno::Reference<container::XNameAccess> xStyleFamilies
136 = xStyleFamiliesSupplier->getStyleFamilies();
137 uno::Reference<container::XNameAccess> xStyleFamily(
138 xStyleFamilies->getByName(u"PageStyles"_ustr), uno::UNO_QUERY);
139 uno::Reference<beans::XPropertySet> xStyle(xStyleFamily->getByName(u"Standard"_ustr),
140 uno::UNO_QUERY);
141 auto nTopMargin = xStyle->getPropertyValue(u"TopMargin"_ustr).get<sal_Int32>();
142 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), nTopMargin);
143 auto aTopBorder = xStyle->getPropertyValue(u"TopBorder"_ustr).get<table::BorderLine2>();
144 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(159), aTopBorder.LineWidth);
145 auto nTopBorderDistance = xStyle->getPropertyValue(u"TopBorderDistance"_ustr).get<sal_Int32>();
146 // Without the accompanying fix in place, this test would have failed with:
147 // - Expected: -1147
148 // - Actual : 0
149 // i.e. the border negative distance was lost.
150 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(-1147), nTopBorderDistance);
151 }
152
153 CPPUNIT_TEST_FIXTURE(Test, testPasteHeaderDisable)
154 {
155 // Given an empty document with a turned on header:
156 loadFromURL(u"private:factory/swriter"_ustr);
157 uno::Reference<style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(mxComponent,
158 uno::UNO_QUERY);
159 uno::Reference<container::XNameAccess> xStyleFamilies
160 = xStyleFamiliesSupplier->getStyleFamilies();
161 uno::Reference<container::XNameAccess> xStyleFamily(
162 xStyleFamilies->getByName(u"PageStyles"_ustr), uno::UNO_QUERY);
163 uno::Reference<beans::XPropertySet> xStyle(xStyleFamily->getByName(u"Standard"_ustr),
164 uno::UNO_QUERY);
165 xStyle->setPropertyValue(u"HeaderIsOn"_ustr, uno::Any(true));
166
167 // When pasting RTF content:
168 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
169 uno::Reference<text::XTextRange> xText = xTextDocument->getText();
170 uno::Reference<text::XTextRange> xBodyEnd = xText->getEnd();
171 uno::Reference<document::XFilter> xFilter(
172 m_xSFactory->createInstance(u"com.sun.star.comp.Writer.RtfFilter"_ustr), uno::UNO_QUERY);
173 uno::Reference<document::XImporter> xImporter(xFilter, uno::UNO_QUERY);
174 xImporter->setTargetDocument(mxComponent);
175 std::unique_ptr<SvStream> pStream(new SvMemoryStream);
176 pStream->WriteOString("{\\rtf1 paste}");
177 pStream->Seek(0);
178 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(std::move(pStream)));
179 uno::Sequence aDescriptor{ comphelper::makePropertyValue(u"InputStream"_ustr, xStream),
180 comphelper::makePropertyValue(u"InsertMode"_ustr, true),
181 comphelper::makePropertyValue(u"TextInsertModeRange"_ustr,
182 xBodyEnd) };
183 CPPUNIT_ASSERT(xFilter->filter(aDescriptor));
184
185 // Then make sure the header stays on:
186 CPPUNIT_ASSERT(xStyle->getPropertyValue(u"HeaderIsOn"_ustr).get<bool>());
187 }
188
189 CPPUNIT_TEST_FIXTURE(Test, testPasteHeaderEmptied)
190 {
191 // Given an empty document with a turned on footer with content:
192 loadFromFile(u"page-break-footer-table.docx");
193 uno::Reference<style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(mxComponent,
194 uno::UNO_QUERY);
195 uno::Reference<container::XNameAccess> xStyleFamilies
196 = xStyleFamiliesSupplier->getStyleFamilies();
197 uno::Reference<container::XNameAccess> xStyleFamily(
198 xStyleFamilies->getByName(u"PageStyles"_ustr), uno::UNO_QUERY);
199 uno::Reference<beans::XPropertySet> xStyle(xStyleFamily->getByName(u"Standard"_ustr),
200 uno::UNO_QUERY);
201
202 // When pasting RTF content:
203 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
204 uno::Reference<text::XTextRange> xText = xTextDocument->getText();
205 uno::Reference<text::XTextRange> xBodyEnd = xText->getEnd();
206 uno::Reference<document::XFilter> xFilter(
207 m_xSFactory->createInstance(u"com.sun.star.comp.Writer.RtfFilter"_ustr), uno::UNO_QUERY);
208 uno::Reference<document::XImporter> xImporter(xFilter, uno::UNO_QUERY);
209 xImporter->setTargetDocument(mxComponent);
210 std::unique_ptr<SvStream> pStream(new SvMemoryStream);
211 pStream->WriteOString("{\\rtf1 paste}");
212 pStream->Seek(0);
213 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(std::move(pStream)));
214 uno::Sequence aDescriptor{ comphelper::makePropertyValue(u"InputStream"_ustr, xStream),
215 comphelper::makePropertyValue(u"InsertMode"_ustr, true),
216 comphelper::makePropertyValue(u"TextInsertModeRange"_ustr,
217 xBodyEnd) };
218 CPPUNIT_ASSERT(xFilter->filter(aDescriptor));
219
220 // Then make sure the header retains its contents:
221 uno::Reference<text::XTextRange> xFooterText(xStyle->getPropertyValue(u"FooterText"_ustr),
222 uno::UNO_QUERY);
223 CPPUNIT_ASSERT_EQUAL(u"Odd page footer"_ustr, xFooterText->getString());
224 }
225
226 CPPUNIT_TEST_FIXTURE(Test, testAltChunkHeader)
227 {
228 // Given a document with an altChunk and a header:
229 // When loading that document:
230 createSwDoc("alt-chunk-header.docx");
231
232 // Then make sure the first page has a header:
233 SwDocShell* pDocShell = getSwDocShell();
234 SwDoc* pDoc = pDocShell->GetDoc();
235 SwRootFrame* pLayout = pDoc->getIDocumentLayoutAccess().GetCurrentLayout();
236 auto pPage1 = pLayout->Lower()->DynCastPageFrame();
237 SwFrame* pFrame = pPage1->GetLower();
238 // Without the accompanying fix in place, this test would have failed, the first page had only a
239 // body frame, not a header-body-footer chain.
240 CPPUNIT_ASSERT(pFrame->IsHeaderFrame());
241 }
242 }
243
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */