Upgrade mdds to 3.0.0 and liborcus to 0.20.0
[LibreOffice.git] / sw / qa / core / frmedt / frmedt.cxx
blobbfa776d98a86afabf49dd7431dc5349373f8d23a
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 <swmodeltestbase.hxx>
12 #include <com/sun/star/text/VertOrientation.hpp>
13 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
15 #include <svx/svdpage.hxx>
17 #include <wrtsh.hxx>
18 #include <fmtanchr.hxx>
19 #include <IDocumentDrawModelAccess.hxx>
20 #include <drawdoc.hxx>
21 #include <dcontact.hxx>
22 #include <frameformats.hxx>
23 #include <unotxdoc.hxx>
24 #include <docsh.hxx>
25 #include <swdtflvr.hxx>
26 #include <caption.hxx>
27 #include <view.hxx>
28 #include <formatflysplit.hxx>
29 #include <itabenum.hxx>
30 #include <frmmgr.hxx>
31 #include <UndoManager.hxx>
33 /// Covers sw/source/core/frmedt/ fixes.
34 class SwCoreFrmedtTest : public SwModelTestBase
36 public:
37 SwCoreFrmedtTest()
38 : SwModelTestBase(u"/sw/qa/core/frmedt/data/"_ustr)
43 CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, testTextboxReanchor)
45 // Load a document with a textframe and a textbox(shape+textframe).
46 createSwDoc("textbox-reanchor.odt");
47 SwDoc* pDoc = getSwDoc();
48 SdrPage* pDrawPage = pDoc->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
49 SdrObject* pDrawShape = pDrawPage->GetObj(1);
50 CPPUNIT_ASSERT_EQUAL(u"draw shape"_ustr, pDrawShape->GetName());
52 // Select the shape of the textbox.
53 Point aPoint;
54 SwWrtShell* pShell = getSwDocShell()->GetWrtShell();
55 pShell->SelectObj(aPoint, /*nFlag=*/0, pDrawShape);
57 // Anchor the shape of the textbox into its own textframe.
58 SdrObject* pTextFrameObj = pDrawPage->GetObj(2);
59 CPPUNIT_ASSERT(pTextFrameObj);
60 SwFrameFormat* pTextFrameFormat = FindFrameFormat(pTextFrameObj);
61 CPPUNIT_ASSERT(pTextFrameFormat);
62 CPPUNIT_ASSERT_EQUAL(u"Frame2"_ustr, pTextFrameFormat->GetName());
63 SwFrameFormat* pDrawShapeFormat = FindFrameFormat(pDrawShape);
64 SwNodeOffset nOldAnchor = pDrawShapeFormat->GetAnchor().GetAnchorNode()->GetIndex();
65 pShell->FindAnchorPos(pTextFrameObj->GetLastBoundRect().Center(), true);
66 SwNodeOffset nNewAnchor = pDrawShapeFormat->GetAnchor().GetAnchorNode()->GetIndex();
67 // Without the accompanying fix in place, this test would have failed with:
68 // - Expected: 6
69 // - Actual : 9
70 // i.e. SwFEShell allowed to anchor the textframe of a textbox into itself.
71 CPPUNIT_ASSERT_EQUAL(nOldAnchor, nNewAnchor);
74 CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, testVertPosFromBottomBoundingBox)
76 // Insert a shape and anchor it vertically in a way, so its position is from the top of the page
77 // bottom margin area.
78 createSwDoc();
79 uno::Reference<css::lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
80 uno::Reference<drawing::XShape> xShape(
81 xFactory->createInstance(u"com.sun.star.drawing.RectangleShape"_ustr), uno::UNO_QUERY);
82 xShape->setSize(awt::Size(10000, 10000));
83 uno::Reference<beans::XPropertySet> xShapeProps(xShape, uno::UNO_QUERY);
84 xShapeProps->setPropertyValue(u"AnchorType"_ustr,
85 uno::Any(text::TextContentAnchorType_AT_CHARACTER));
86 xShapeProps->setPropertyValue(u"VertOrient"_ustr, uno::Any(text::VertOrientation::NONE));
87 xShapeProps->setPropertyValue(u"VertOrientRelation"_ustr,
88 uno::Any(text::RelOrientation::PAGE_PRINT_AREA_BOTTOM));
89 xShapeProps->setPropertyValue(u"VertOrientPosition"_ustr,
90 uno::Any(static_cast<sal_Int32>(-11000)));
91 uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
92 xDrawPageSupplier->getDrawPage()->add(xShape);
94 // Get the absolute position of the top of the page bottom margin area.
95 xmlDocUniquePtr pXmlDoc = parseLayoutDump();
96 SwTwips nPagePrintAreaBottom = getXPath(pXmlDoc, "//page/infos/prtBounds", "bottom").toInt32();
98 // Calculate the allowed bounding box of the shape, e.g. the shape's position & size dialog uses
99 // this to limit the vertical position to sensible values.
100 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
101 SwRect aBoundRect;
102 RndStdIds eAnchorType = RndStdIds::FLY_AT_CHAR;
103 SwDoc* pDoc = getSwDoc();
104 const auto& rFrameFormats = *pDoc->GetFrameFormats();
105 const SwFormatAnchor* pFormatAhchor = &rFrameFormats[0]->GetAnchor();
106 sal_Int16 eHoriRelOrient = text::RelOrientation::PAGE_FRAME;
107 sal_Int16 eVertRelOrient = text::RelOrientation::PAGE_PRINT_AREA_BOTTOM;
108 bool bFollowTextFlow = false;
109 bool bMirror = false;
110 Size aPercentSize;
111 pWrtShell->CalcBoundRect(aBoundRect, eAnchorType, eHoriRelOrient, eVertRelOrient, pFormatAhchor,
112 bFollowTextFlow, bMirror, nullptr, &aPercentSize);
114 // Without the accompanying fix in place, this test would have failed with:
115 // - Expected: -14705
116 // - Actual : -1134
117 // i.e. UI did not allow anchoring a shape 10cm above the bottom of the page due to wrong
118 // bounding box.
119 CPPUNIT_ASSERT_EQUAL(-1 * nPagePrintAreaBottom, aBoundRect.Pos().getY());
122 CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, testPasteFlyInTextBox)
124 // Given a document that contains a textbox, which contains an sw image (fly frame)
125 createSwDoc("paste-fly-in-textbox.docx");
126 SwDocShell* pDocShell = getSwDocShell();
127 SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
128 SwDoc* pDoc = pDocShell->GetDoc();
129 SdrPage* pPage = pDoc->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
130 SdrObject* pObject = pPage->GetObj(0);
131 pWrtShell->SelectObj(Point(), 0, pObject);
132 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), pDoc->GetSpzFrameFormats()->GetFormatCount());
133 rtl::Reference<SwTransferable> pTransfer = new SwTransferable(*pWrtShell);
134 pTransfer->Cut();
135 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), pDoc->GetSpzFrameFormats()->GetFormatCount());
136 TransferableDataHelper aHelper(pTransfer);
138 // When pasting that to an empty document.
139 SwTransferable::Paste(*pWrtShell, aHelper);
141 // Then we should have the image only once: 3 formats (draw+fly formats for the textbox and a
142 // fly format for the image).
143 // Without the accompanying fix in place, this test would have failed with:
144 // - Expected: 3
145 // - Actual : 4
146 // i.e. the image was pasted twice.
147 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), pDoc->GetSpzFrameFormats()->GetFormatCount());
150 CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, testTextBoxSelectCursorPos)
152 // Given a document with a fly+draw format pair (textbox):
153 createSwDoc("paste-fly-in-textbox.docx");
155 // When selecting the fly format:
156 SwDoc* pDoc = getSwDoc();
157 SdrPage* pPage = pDoc->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
158 SdrObject* pFlyObject = pPage->GetObj(1);
159 SwContact* pFlyContact = static_cast<SwContact*>(pFlyObject->GetUserCall());
160 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(RES_FLYFRMFMT), pFlyContact->GetFormat()->Which());
161 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
162 pWrtShell->SelectObj(Point(), 0, pFlyObject);
164 // Then make sure the cursor is the anchor of the draw format:
165 SdrObject* pDrawObject = pPage->GetObj(0);
166 SwDrawContact* pDrawContact = static_cast<SwDrawContact*>(pDrawObject->GetUserCall());
167 SwFrameFormat* pDrawFormat = pDrawContact->GetFormat();
168 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(RES_DRAWFRMFMT), pDrawFormat->Which());
169 SwNodeOffset nAnchor = pDrawFormat->GetAnchor().GetContentAnchor()->GetNode().GetIndex();
170 SwNodeOffset nCursor = pWrtShell->GetCurrentShellCursor().GetPointNode().GetIndex();
171 // Without the accompanying fix in place, this test would have failed with:
172 // - Expected: 15 (anchor of draw format)
173 // - Actual : 6 (in-fly-format position)
174 // i.e. the cursor had a broken position after trying to select the fly format.
175 CPPUNIT_ASSERT_EQUAL(nAnchor, nCursor);
178 CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, testSplitFlyInsertCaption)
180 // Given a document with a full-page floating table:
181 createSwDoc("floating-table-caption.docx");
183 // When trying to insert a caption below that table:
184 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
185 pWrtShell->GotoTable(u"Table1"_ustr);
186 InsCaptionOpt aOpt;
187 SwView& rView = pWrtShell->GetView();
188 aOpt.SetCategory(u"Table"_ustr);
189 aOpt.SetCaption(u"Numbers English-German"_ustr);
190 // After, not before.
191 aOpt.SetPos(1);
192 // Without the accompanying fix in place, this call never finished, layout didn't handle content
193 // after the table in a floating table.
194 rView.InsertCaption(&aOpt);
196 // Then make sure the insertion finishes and now this is just a plain table-in-frame:
197 SwDoc* pDoc = getSwDoc();
198 sw::SpzFrameFormats& rFlys = *pDoc->GetSpzFrameFormats();
199 sw::SpzFrameFormat* pFly = rFlys[0];
200 CPPUNIT_ASSERT(!pFly->GetAttrSet().GetFlySplit().GetValue());
203 CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, testSplitFlyUnfloat)
205 // Given a document with a floating table:
206 createSwDoc();
207 SwDoc* pDoc = getSwDocShell()->GetDoc();
208 CPPUNIT_ASSERT(pDoc->GetUndoManager().IsUndoEnabled());
209 pDoc->GetUndoManager().EnableUndo(false);
210 sw::FrameFormats<sw::SpzFrameFormat*>& rFlyFormats = *pDoc->GetSpzFrameFormats();
211 CPPUNIT_ASSERT(rFlyFormats.empty());
212 // Insert a table:
213 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
214 SwInsertTableOptions aTableOptions(SwInsertTableFlags::DefaultBorder, 0);
215 pWrtShell->InsertTable(aTableOptions, /*nRows=*/2, /*nCols=*/1);
216 pWrtShell->MoveTable(GotoPrevTable, fnTableStart);
217 pWrtShell->GoPrevCell();
218 pWrtShell->Insert(u"A1"_ustr);
219 pWrtShell->GoNextCell();
220 pWrtShell->Insert(u"A2"_ustr);
221 // Select cell:
222 pWrtShell->SelAll();
223 // Select table:
224 pWrtShell->SelAll();
225 // Wrap the table in a text frame:
226 SwFlyFrameAttrMgr aMgr(true, pWrtShell, Frmmgr_Type::TEXT, nullptr);
227 pWrtShell->StartAllAction();
228 aMgr.InsertFlyFrame(RndStdIds::FLY_AT_PARA, aMgr.GetPos(), aMgr.GetSize());
229 pWrtShell->EndAllAction();
230 CPPUNIT_ASSERT(!rFlyFormats.empty());
231 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc->GetTableFrameFormatCount(/*bUsed=*/true));
232 pDoc->GetUndoManager().EnableUndo(true);
234 // When marking that frame and unfloating it:
235 selectShape(1);
236 pWrtShell->UnfloatFlyFrame();
238 // Then make sure the frame is removed, but the table is still part of the document:
239 // Without the accompanying fix in place (empty SwFEShell::UnfloatFlyFrame()), this test would
240 // have failed, the frame was not removed.
241 CPPUNIT_ASSERT(rFlyFormats.empty());
242 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc->GetTableFrameFormatCount(/*bUsed=*/true));
244 // When undoing the conversion to inline:
245 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc->GetUndoManager().GetUndoActionCount());
246 pDoc->GetUndoManager().Undo();
248 // Then the undo stack had 2 undo actions and undo-all crashed.
249 CPPUNIT_ASSERT(!rFlyFormats.empty());
250 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc->GetTableFrameFormatCount(/*bUsed=*/true));
253 CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, testInsertOnGrfNodeAsChar)
255 // Given a selected as-char image:
256 createSwDoc();
257 SwDoc* pDoc = getSwDocShell()->GetDoc();
258 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
260 SfxItemSet aFrameSet(pDoc->GetAttrPool(), svl::Items<RES_FRMATR_BEGIN, RES_FRMATR_END - 1>);
261 SwFormatAnchor aAnchor(RndStdIds::FLY_AS_CHAR);
262 aFrameSet.Put(aAnchor);
263 Graphic aGrf;
264 pWrtShell->SwFEShell::Insert(OUString(), OUString(), &aGrf, &aFrameSet);
267 // When inserting another as-char image:
268 SfxItemSet aFrameSet(pDoc->GetAttrPool(), svl::Items<RES_FRMATR_BEGIN, RES_FRMATR_END - 1>);
269 SwFormatAnchor aAnchor(RndStdIds::FLY_AS_CHAR);
270 aFrameSet.Put(aAnchor);
271 Graphic aGrf;
272 // Without the accompanying fix in place, this call crashed, we try to set a graphic node as an
273 // anchor of an as-char image (which should be a text node).
274 pWrtShell->SwFEShell::Insert(OUString(), OUString(), &aGrf, &aFrameSet);
276 // Then make sure that the anchor of the second image is next to the first anchor:
277 CPPUNIT_ASSERT(pDoc->GetSpzFrameFormats());
278 sw::FrameFormats<sw::SpzFrameFormat*>& rFormats = *pDoc->GetSpzFrameFormats();
279 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), rFormats.size());
280 const sw::SpzFrameFormat& rFormat1 = *rFormats[0];
281 const SwPosition* pAnchor1 = rFormat1.GetAnchor().GetContentAnchor();
282 const sw::SpzFrameFormat& rFormat2 = *rFormats[1];
283 const SwPosition* pAnchor2 = rFormat2.GetAnchor().GetContentAnchor();
284 CPPUNIT_ASSERT_EQUAL(pAnchor1->nNode, pAnchor2->nNode);
285 CPPUNIT_ASSERT_EQUAL(pAnchor1->GetContentIndex() + 1, pAnchor2->GetContentIndex());
288 CPPUNIT_PLUGIN_IMPLEMENT();
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */