nss: upgrade to release 3.73
[LibreOffice.git] / sc / qa / extras / anchor.cxx
blob922ef1dbf319df955863c6561fb85a7bffa86f1c
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 <sfx2/dispatch.hxx>
11 #include <svx/svdograf.hxx>
12 #include <svx/svdpage.hxx>
13 #include <test/calc_unoapi_test.hxx>
15 #include <com/sun/star/sheet/XSpreadsheet.hpp>
16 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
17 #include <com/sun/star/text/XText.hpp>
19 #include <attrib.hxx>
20 #include <docsh.hxx>
21 #include <drwlayer.hxx>
22 #include <scitems.hxx>
23 #include <svx/svdocirc.hxx>
24 #include <vcl/scheduler.hxx>
25 #include <tabvwsh.hxx>
27 #include <sc.hrc>
29 using namespace css;
31 namespace sc_apitest
33 class ScAnchorTest : public CalcUnoApiTest
35 public:
36 ScAnchorTest();
38 void testUndoAnchor();
39 void testTdf76183();
40 void testODFAnchorTypes();
41 void testCopyColumnWithImages();
42 void testCutWithImages();
43 void testTdf121963();
44 void testTdf129552();
45 void testTdf130556();
46 void testTdf134161();
48 CPPUNIT_TEST_SUITE(ScAnchorTest);
49 CPPUNIT_TEST(testUndoAnchor);
50 CPPUNIT_TEST(testTdf76183);
51 CPPUNIT_TEST(testODFAnchorTypes);
52 CPPUNIT_TEST(testCopyColumnWithImages);
53 CPPUNIT_TEST(testCutWithImages);
54 CPPUNIT_TEST(testTdf121963);
55 CPPUNIT_TEST(testTdf129552);
56 CPPUNIT_TEST(testTdf130556);
57 CPPUNIT_TEST(testTdf134161);
58 CPPUNIT_TEST_SUITE_END();
61 ScAnchorTest::ScAnchorTest()
62 : CalcUnoApiTest("sc/qa/unit/data/ods")
66 void ScAnchorTest::testUndoAnchor()
68 OUString aFileURL;
69 createFileURL("document_with_linked_graphic.ods", aFileURL);
70 // open the document with graphic included
71 uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
72 CPPUNIT_ASSERT(xComponent.is());
74 // Get the document model
75 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
76 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
78 ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
79 CPPUNIT_ASSERT(pDocSh);
81 // Check whether graphic imported well
82 ScDocument& rDoc = pDocSh->GetDocument();
83 ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
84 CPPUNIT_ASSERT(pDrawLayer);
86 const SdrPage* pPage = pDrawLayer->GetPage(0);
87 CPPUNIT_ASSERT(pPage);
89 SdrGrafObj* pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(0));
90 CPPUNIT_ASSERT(pObject);
91 CPPUNIT_ASSERT(pObject->IsLinkedGraphic());
93 const GraphicObject& rGraphicObj = pObject->GetGraphicObject(true);
94 CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
95 CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());
97 // Get the document controller
98 ScTabViewShell* pViewShell = pDocSh->GetBestViewShell(false);
99 CPPUNIT_ASSERT(pViewShell);
101 // Get the draw view of the document
102 ScDrawView* pDrawView = pViewShell->GetViewData().GetScDrawView();
103 CPPUNIT_ASSERT(pDrawView);
105 // Select graphic object
106 pDrawView->MarkNextObj();
107 CPPUNIT_ASSERT(pDrawView->AreObjectsMarked());
109 // Set Cell Anchor
110 ScDrawLayer::SetCellAnchoredFromPosition(*pObject, rDoc, 0, false);
111 // Check state
112 ScAnchorType oldType = ScDrawLayer::GetAnchorType(*pObject);
113 CPPUNIT_ASSERT_EQUAL(SCA_CELL, oldType);
115 // Change all selected objects to page anchor
116 pViewShell->GetViewData().GetDispatcher().Execute(SID_ANCHOR_PAGE);
117 // Check state
118 ScAnchorType newType = ScDrawLayer::GetAnchorType(*pObject);
119 CPPUNIT_ASSERT_EQUAL(SCA_PAGE, newType);
121 // Undo and check its result.
122 SfxUndoManager* pUndoMgr = rDoc.GetUndoManager();
123 CPPUNIT_ASSERT(pUndoMgr);
124 pUndoMgr->Undo();
126 // Check anchor type
127 CPPUNIT_ASSERT_EQUAL(oldType, ScDrawLayer::GetAnchorType(*pObject));
128 CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
129 CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());
131 pUndoMgr->Redo();
133 // Check anchor type
134 CPPUNIT_ASSERT_EQUAL(newType, ScDrawLayer::GetAnchorType(*pObject));
135 CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
136 CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());
138 ScDrawLayer::SetPageAnchored(*pObject);
139 // Check state
140 oldType = ScDrawLayer::GetAnchorType(*pObject);
141 CPPUNIT_ASSERT_EQUAL(SCA_PAGE, oldType);
143 // Change all selected objects to cell anchor
144 pViewShell->GetViewData().GetDispatcher().Execute(SID_ANCHOR_CELL);
145 // Check state
146 newType = ScDrawLayer::GetAnchorType(*pObject);
147 CPPUNIT_ASSERT_EQUAL(SCA_CELL, newType);
149 pUndoMgr->Undo();
151 // Check anchor type
152 CPPUNIT_ASSERT_EQUAL(oldType, ScDrawLayer::GetAnchorType(*pObject));
153 CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
154 CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());
156 pUndoMgr->Redo();
158 // Check anchor type
159 CPPUNIT_ASSERT_EQUAL(newType, ScDrawLayer::GetAnchorType(*pObject));
160 CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
161 CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());
163 xComponent->dispose();
166 void ScAnchorTest::testTdf76183()
168 uno::Reference<lang::XComponent> xComponent = loadFromDesktop("private:factory/scalc");
169 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
170 ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
171 ScDocument& rDoc = pDocSh->GetDocument();
172 ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
173 SdrPage* pPage = pDrawLayer->GetPage(0);
175 // Add a circle somewhere below first row.
176 const tools::Rectangle aOrigRect(1000, 1000, 1200, 1200);
177 SdrCircObj* pObj = new SdrCircObj(*pDrawLayer, SdrCircKind::Full, aOrigRect);
178 pPage->InsertObject(pObj);
179 // Anchor to cell
180 ScDrawLayer::SetCellAnchoredFromPosition(*pObj, rDoc, 0, false);
181 const tools::Rectangle& rNewRect = pObj->GetLogicRect();
183 // Set word wrap to true
184 rDoc.ApplyAttr(0, 0, 0, ScLineBreakCell(true));
185 // Add multi-line text to cell to initiate optimal height change
186 uno::Reference<sheet::XSpreadsheetDocument> xDoc(xComponent, uno::UNO_QUERY_THROW);
187 uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), uno::UNO_QUERY_THROW);
188 uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), uno::UNO_QUERY_THROW);
189 uno::Reference<text::XText> xText(xSheet->getCellByPosition(0, 0), uno::UNO_QUERY_THROW);
190 xText->setString("first\nsecond\nthird");
192 // The resize of first row must have moved the object down after its anchor cell
193 CPPUNIT_ASSERT(aOrigRect.Top() < rNewRect.Top());
195 pDocSh->DoClose();
198 void ScAnchorTest::testODFAnchorTypes()
200 OUString aFileURL;
201 createFileURL("3AnchorTypes.ods", aFileURL);
202 // open the document with graphic included
203 uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
204 CPPUNIT_ASSERT(xComponent.is());
206 // Get the document model
207 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
208 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
210 ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
211 CPPUNIT_ASSERT(pDocSh);
213 // Check whether graphic imported well
214 ScDocument& rDoc = pDocSh->GetDocument();
215 ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
216 CPPUNIT_ASSERT(pDrawLayer);
218 const SdrPage* pPage = pDrawLayer->GetPage(0);
219 CPPUNIT_ASSERT(pPage);
221 // Check 1st object: Page anchored
222 SdrGrafObj* pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(0));
223 CPPUNIT_ASSERT(pObject);
224 ScAnchorType anchorType = ScDrawLayer::GetAnchorType(*pObject);
225 CPPUNIT_ASSERT_EQUAL(SCA_PAGE, anchorType);
227 // Check 2nd object: Cell anchored, resize with cell
228 pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(1));
229 CPPUNIT_ASSERT(pObject);
230 anchorType = ScDrawLayer::GetAnchorType(*pObject);
231 CPPUNIT_ASSERT_EQUAL(SCA_CELL_RESIZE, anchorType);
233 // Check 3rd object: Cell anchored
234 pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(2));
235 CPPUNIT_ASSERT(pObject);
236 anchorType = ScDrawLayer::GetAnchorType(*pObject);
237 CPPUNIT_ASSERT_EQUAL(SCA_CELL, anchorType);
239 pDocSh->DoClose();
242 /// Test that copying a column with an image anchored to it also copies the image
243 void ScAnchorTest::testCopyColumnWithImages()
245 OUString aFileURL;
246 createFileURL("3AnchorTypes.ods", aFileURL);
247 // open the document with graphic included
248 uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
249 CPPUNIT_ASSERT(xComponent.is());
251 // Get the document model
252 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
253 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
255 ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
256 CPPUNIT_ASSERT(pDocSh);
258 ScDocument* pDoc = &(pDocSh->GetDocument());
259 ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
260 CPPUNIT_ASSERT(pDrawLayer);
262 // Get the document controller
263 ScTabViewShell* pViewShell = pDocSh->GetBestViewShell(false);
264 CPPUNIT_ASSERT(pViewShell != nullptr);
266 ScDocument aClipDoc(SCDOCMODE_CLIP);
268 // Copy whole column
270 // 1. Copy source range
271 ScRange aSrcRange;
272 aSrcRange.Parse("A1:A11", *pDoc, pDoc->GetAddressConvention());
273 pViewShell->GetViewData().GetMarkData().SetMarkArea(aSrcRange);
274 pViewShell->GetViewData().GetView()->CopyToClip(&aClipDoc, false, false, true, false);
276 // 2. Paste to target range
277 ScRange aDstRange;
278 aDstRange.Parse("D1:D11", *pDoc, pDoc->GetAddressConvention());
279 pViewShell->GetViewData().GetMarkData().SetMarkArea(aDstRange);
280 pViewShell->GetViewData().GetView()->PasteFromClip(InsertDeleteFlags::ALL, &aClipDoc);
282 // 3. Make sure the images have been copied too
283 std::map<SCROW, std::vector<SdrObject*>> aRowObjects
284 = pDrawLayer->GetObjectsAnchoredToRange(0, 3, 0, 11);
285 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be an image anchored to D3", 1,
286 static_cast<int>(aRowObjects[2].size()));
287 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be an image anchored to D11", 1,
288 static_cast<int>(aRowObjects[10].size()));
291 // Copy individual cells
293 // 1. Copy source cells
294 ScRange aSrcRange;
295 aSrcRange.Parse("A3:B3", *pDoc, pDoc->GetAddressConvention());
296 pViewShell->GetViewData().GetMarkData().SetMarkArea(aSrcRange);
297 pViewShell->GetViewData().GetView()->CopyToClip(&aClipDoc, false, false, true, false);
299 // 2. Paste to target cells
300 ScRange aDstRange;
301 aDstRange.Parse("G3:H3", *pDoc, pDoc->GetAddressConvention());
302 pViewShell->GetViewData().GetMarkData().SetMarkArea(aDstRange);
303 pViewShell->GetViewData().GetView()->PasteFromClip(InsertDeleteFlags::ALL, &aClipDoc);
305 // 3. Make sure the image has been copied too
306 std::map<SCROW, std::vector<SdrObject*>> aRowObjects
307 = pDrawLayer->GetObjectsAnchoredToRange(0, 6, 2, 2);
308 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be an image anchored to G3", 1,
309 static_cast<int>(aRowObjects[2].size()));
312 pDocSh->DoClose();
315 void ScAnchorTest::testCutWithImages()
317 OUString aFileURL;
318 createFileURL("3AnchorTypes.ods", aFileURL);
319 // open the document with graphic included
320 uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
321 CPPUNIT_ASSERT(xComponent.is());
323 // Get the document model
324 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
325 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
327 ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
328 CPPUNIT_ASSERT(pDocSh);
330 ScDocument* pDoc = &(pDocSh->GetDocument());
331 ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
332 CPPUNIT_ASSERT(pDrawLayer);
334 // Get the document controller
335 ScTabViewShell* pViewShell = pDocSh->GetBestViewShell(false);
336 CPPUNIT_ASSERT(pViewShell != nullptr);
338 // Cut whole column
340 // Cut source range
341 ScRange aSrcRange;
342 aSrcRange.Parse("A1:A11", *pDoc, pDoc->GetAddressConvention());
343 pViewShell->GetViewData().GetMarkData().SetMarkArea(aSrcRange);
344 pViewShell->GetViewData().GetView()->CutToClip();
346 std::map<SCROW, std::vector<SdrObject*>> aRowObjects
347 = pDrawLayer->GetObjectsAnchoredToRange(0, 0, 0, 11);
349 // Images should have been removed from the cells
350 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no image anchored to A3", 0,
351 static_cast<int>(aRowObjects[2].size()));
352 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no image anchored to A11", 0,
353 static_cast<int>(aRowObjects[10].size()));
356 // Cut individual cells
358 // Cut source cells
359 ScRange aSrcRange;
360 aSrcRange.Parse("A3:B3", *pDoc, pDoc->GetAddressConvention());
361 pViewShell->GetViewData().GetMarkData().SetMarkArea(aSrcRange);
362 pViewShell->GetViewData().GetView()->CutToClip();
364 // Image should have been removed from the cell
365 std::map<SCROW, std::vector<SdrObject*>> aRowObjects
366 = pDrawLayer->GetObjectsAnchoredToRange(0, 0, 2, 2);
367 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no image anchored to A3", 0,
368 static_cast<int>(aRowObjects[2].size()));
371 pDocSh->DoClose();
374 void ScAnchorTest::testTdf121963()
376 OUString aFileURL;
377 createFileURL("tdf121963.ods", aFileURL);
378 uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
379 CPPUNIT_ASSERT(xComponent.is());
381 // Without the accompanying fix in place, this test would have never returned due to an infinite
382 // invalidation loop, where ScGridWindow::Paint() invalidated itself.
383 Scheduler::ProcessEventsToIdle();
385 xComponent->dispose();
388 void ScAnchorTest::testTdf129552()
390 OUString aFileURL;
391 createFileURL("tdf129552.fods", aFileURL);
392 uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
393 CPPUNIT_ASSERT(xComponent.is());
395 // Without the accompanying fix in place, this test would have never returned due to an infinite
396 // invalidation loop, where ScGridWindow::Paint() invalidated itself.
397 Scheduler::ProcessEventsToIdle();
399 xComponent->dispose();
402 void ScAnchorTest::testTdf130556()
404 OUString aFileURL;
405 createFileURL("tdf130556.ods", aFileURL);
406 uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
407 CPPUNIT_ASSERT(xComponent.is());
409 // Without the accompanying fix in place, this test would have never returned due to an infinite
410 // invalidation loop, where ScGridWindow::Paint() invalidated itself.
411 Scheduler::ProcessEventsToIdle();
413 xComponent->dispose();
416 void ScAnchorTest::testTdf134161()
418 OUString aFileURL;
419 createFileURL("tdf134161.ods", aFileURL);
420 uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
421 CPPUNIT_ASSERT(xComponent.is());
423 // Without the accompanying fix in place, this test would have never returned due to an infinite
424 // invalidation loop
425 Scheduler::ProcessEventsToIdle();
427 xComponent->dispose();
430 CPPUNIT_TEST_SUITE_REGISTRATION(ScAnchorTest);
433 CPPUNIT_PLUGIN_IMPLEMENT();
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */