1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <sfx2/dispatch.hxx>
#include <svx/svdograf.hxx>
#include <svx/svdpage.hxx>
#include <test/calc_unoapi_test.hxx>

#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/text/XText.hpp>

#include <attrib.hxx>
#include <docsh.hxx>
#include <drwlayer.hxx>
#include <scitems.hxx>
#include <svx/svdocirc.hxx>
#include <vcl/scheduler.hxx>
#include <tabvwsh.hxx>

#include <sc.hrc>

using namespace css;

namespace sc_apitest
{
class ScAnchorTest : public CalcUnoApiTest
{
public:
    ScAnchorTest();

    void testUndoAnchor();
    void testTdf76183();
    void testODFAnchorTypes();
    void testCopyColumnWithImages();
    void testCutWithImages();
    void testTdf121963();
    void testTdf129552();
    void testTdf130556();

    CPPUNIT_TEST_SUITE(ScAnchorTest);
    CPPUNIT_TEST(testUndoAnchor);
    CPPUNIT_TEST(testTdf76183);
    CPPUNIT_TEST(testODFAnchorTypes);
    CPPUNIT_TEST(testCopyColumnWithImages);
    CPPUNIT_TEST(testCutWithImages);
    CPPUNIT_TEST(testTdf121963);
    CPPUNIT_TEST(testTdf129552);
    CPPUNIT_TEST(testTdf130556);
    CPPUNIT_TEST_SUITE_END();
};

ScAnchorTest::ScAnchorTest()
    : CalcUnoApiTest("sc/qa/unit/data/ods")
{
}

void ScAnchorTest::testUndoAnchor()
{
    OUString aFileURL;
    createFileURL("document_with_linked_graphic.ods", aFileURL);
    // open the document with graphic included
    uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
    CPPUNIT_ASSERT(xComponent.is());

    // Get the document model
    SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
    CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);

    ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
    CPPUNIT_ASSERT(pDocSh);

    // Check whether graphic imported well
    ScDocument& rDoc = pDocSh->GetDocument();
    ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
    CPPUNIT_ASSERT(pDrawLayer);

    const SdrPage* pPage = pDrawLayer->GetPage(0);
    CPPUNIT_ASSERT(pPage);

    SdrGrafObj* pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(0));
    CPPUNIT_ASSERT(pObject);
    CPPUNIT_ASSERT(pObject->IsLinkedGraphic());

    const GraphicObject& rGraphicObj = pObject->GetGraphicObject(true);
    CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
    CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());

    // Get the document controller
    ScTabViewShell* pViewShell = pDocSh->GetBestViewShell(false);
    CPPUNIT_ASSERT(pViewShell);

    // Get the draw view of the document
    ScDrawView* pDrawView = pViewShell->GetViewData().GetScDrawView();
    CPPUNIT_ASSERT(pDrawView);

    // Select graphic object
    pDrawView->MarkNextObj();
    CPPUNIT_ASSERT(pDrawView->AreObjectsMarked());

    // Set Cell Anchor
    ScDrawLayer::SetCellAnchoredFromPosition(*pObject, rDoc, 0, false);
    // Check state
    ScAnchorType oldType = ScDrawLayer::GetAnchorType(*pObject);
    CPPUNIT_ASSERT_EQUAL(SCA_CELL, oldType);

    // Change all selected objects to page anchor
    pViewShell->GetViewData().GetDispatcher().Execute(SID_ANCHOR_PAGE);
    // Check state
    ScAnchorType newType = ScDrawLayer::GetAnchorType(*pObject);
    CPPUNIT_ASSERT_EQUAL(SCA_PAGE, newType);

    // Undo and check its result.
    SfxUndoManager* pUndoMgr = rDoc.GetUndoManager();
    CPPUNIT_ASSERT(pUndoMgr);
    pUndoMgr->Undo();

    // Check anchor type
    CPPUNIT_ASSERT_EQUAL(oldType, ScDrawLayer::GetAnchorType(*pObject));
    CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
    CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());

    pUndoMgr->Redo();

    // Check anchor type
    CPPUNIT_ASSERT_EQUAL(newType, ScDrawLayer::GetAnchorType(*pObject));
    CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
    CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());

    ScDrawLayer::SetPageAnchored(*pObject);
    // Check state
    oldType = ScDrawLayer::GetAnchorType(*pObject);
    CPPUNIT_ASSERT_EQUAL(SCA_PAGE, oldType);

    // Change all selected objects to cell anchor
    pViewShell->GetViewData().GetDispatcher().Execute(SID_ANCHOR_CELL);
    // Check state
    newType = ScDrawLayer::GetAnchorType(*pObject);
    CPPUNIT_ASSERT_EQUAL(SCA_CELL, newType);

    pUndoMgr->Undo();

    // Check anchor type
    CPPUNIT_ASSERT_EQUAL(oldType, ScDrawLayer::GetAnchorType(*pObject));
    CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
    CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());

    pUndoMgr->Redo();

    // Check anchor type
    CPPUNIT_ASSERT_EQUAL(newType, ScDrawLayer::GetAnchorType(*pObject));
    CPPUNIT_ASSERT_EQUAL(int(GraphicType::Bitmap), int(rGraphicObj.GetGraphic().GetType()));
    CPPUNIT_ASSERT_EQUAL(sal_uLong(864900), rGraphicObj.GetGraphic().GetSizeBytes());

    xComponent->dispose();
}

void ScAnchorTest::testTdf76183()
{
    uno::Reference<lang::XComponent> xComponent = loadFromDesktop("private:factory/scalc");
    SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
    ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
    ScDocument& rDoc = pDocSh->GetDocument();
    ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
    SdrPage* pPage = pDrawLayer->GetPage(0);

    // Add a circle somewhere below first row.
    const tools::Rectangle aOrigRect(1000, 1000, 1200, 1200);
    SdrCircObj* pObj = new SdrCircObj(*pDrawLayer, SdrCircKind::Full, aOrigRect);
    pPage->InsertObject(pObj);
    // Anchor to cell
    ScDrawLayer::SetCellAnchoredFromPosition(*pObj, rDoc, 0, false);
    const tools::Rectangle& rNewRect = pObj->GetLogicRect();

    // Set word wrap to true
    rDoc.ApplyAttr(0, 0, 0, ScLineBreakCell(true));
    // Add multi-line text to cell to initiate optimal height change
    uno::Reference<sheet::XSpreadsheetDocument> xDoc(xComponent, uno::UNO_QUERY_THROW);
    uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), uno::UNO_QUERY_THROW);
    uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), uno::UNO_QUERY_THROW);
    uno::Reference<text::XText> xText(xSheet->getCellByPosition(0, 0), uno::UNO_QUERY_THROW);
    xText->setString("first\nsecond\nthird");

    // The resize of first row must have moved the object down after its anchor cell
    CPPUNIT_ASSERT(aOrigRect.Top() < rNewRect.Top());

    pDocSh->DoClose();
}

void ScAnchorTest::testODFAnchorTypes()
{
    OUString aFileURL;
    createFileURL("3AnchorTypes.ods", aFileURL);
    // open the document with graphic included
    uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
    CPPUNIT_ASSERT(xComponent.is());

    // Get the document model
    SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
    CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);

    ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
    CPPUNIT_ASSERT(pDocSh);

    // Check whether graphic imported well
    ScDocument& rDoc = pDocSh->GetDocument();
    ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
    CPPUNIT_ASSERT(pDrawLayer);

    const SdrPage* pPage = pDrawLayer->GetPage(0);
    CPPUNIT_ASSERT(pPage);

    // Check 1st object: Page anchored
    SdrGrafObj* pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(0));
    CPPUNIT_ASSERT(pObject);
    ScAnchorType anchorType = ScDrawLayer::GetAnchorType(*pObject);
    CPPUNIT_ASSERT_EQUAL(SCA_PAGE, anchorType);

    // Check 2nd object: Cell anchored, resize with cell
    pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(1));
    CPPUNIT_ASSERT(pObject);
    anchorType = ScDrawLayer::GetAnchorType(*pObject);
    CPPUNIT_ASSERT_EQUAL(SCA_CELL_RESIZE, anchorType);

    // Check 3rd object: Cell anchored
    pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(2));
    CPPUNIT_ASSERT(pObject);
    anchorType = ScDrawLayer::GetAnchorType(*pObject);
    CPPUNIT_ASSERT_EQUAL(SCA_CELL, anchorType);

    pDocSh->DoClose();
}

/// Test that copying a column with an image anchored to it also copies the image
void ScAnchorTest::testCopyColumnWithImages()
{
    OUString aFileURL;
    createFileURL("3AnchorTypes.ods", aFileURL);
    // open the document with graphic included
    uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
    CPPUNIT_ASSERT(xComponent.is());

    // Get the document model
    SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
    CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);

    ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
    CPPUNIT_ASSERT(pDocSh);

    ScDocument* pDoc = &(pDocSh->GetDocument());
    ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
    CPPUNIT_ASSERT(pDrawLayer);

    // Get the document controller
    ScTabViewShell* pViewShell = pDocSh->GetBestViewShell(false);<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0
    CPPUNIT_ASSERT(pViewShell != nullptr);<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant

    ScDocument aClipDoc(SCDOCMODE_CLIP);

    // Copy whole column
    {
        // 1. Copy source range
        ScRange aSrcRange;
        aSrcRange.Parse("A1:A11", pDoc, pDoc->GetAddressConvention());
        pViewShell->GetViewData().GetMarkData().SetMarkArea(aSrcRange);<--- Null pointer dereference
        pViewShell->GetViewData().GetView()->CopyToClip(&aClipDoc, false, false, true, false);<--- Null pointer dereference

        // 2. Paste to target range
        ScRange aDstRange;
        aDstRange.Parse("D1:D11", pDoc, pDoc->GetAddressConvention());
        pViewShell->GetViewData().GetMarkData().SetMarkArea(aDstRange);<--- Null pointer dereference
        pViewShell->GetViewData().GetView()->PasteFromClip(InsertDeleteFlags::ALL, &aClipDoc);<--- Null pointer dereference

        // 3. Make sure the images have been copied too
        std::map<SCROW, std::vector<SdrObject*>> aRowObjects
            = pDrawLayer->GetObjectsAnchoredToRange(0, 3, 0, 11);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be an image anchored to D3", 1,
                                     static_cast<int>(aRowObjects[2].size()));
        CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be an image anchored to D11", 1,
                                     static_cast<int>(aRowObjects[10].size()));
    }

    // Copy individual cells
    {
        // 1. Copy source cells
        ScRange aSrcRange;
        aSrcRange.Parse("A3:B3", pDoc, pDoc->GetAddressConvention());
        pViewShell->GetViewData().GetMarkData().SetMarkArea(aSrcRange);<--- Null pointer dereference
        pViewShell->GetViewData().GetView()->CopyToClip(&aClipDoc, false, false, true, false);<--- Null pointer dereference

        // 2. Paste to target cells
        ScRange aDstRange;
        aDstRange.Parse("G3:H3", pDoc, pDoc->GetAddressConvention());
        pViewShell->GetViewData().GetMarkData().SetMarkArea(aDstRange);<--- Null pointer dereference
        pViewShell->GetViewData().GetView()->PasteFromClip(InsertDeleteFlags::ALL, &aClipDoc);<--- Null pointer dereference

        // 3. Make sure the image has been copied too
        std::map<SCROW, std::vector<SdrObject*>> aRowObjects
            = pDrawLayer->GetObjectsAnchoredToRange(0, 6, 2, 2);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be an image anchored to G3", 1,
                                     static_cast<int>(aRowObjects[2].size()));
    }

    pDocSh->DoClose();
}

void ScAnchorTest::testCutWithImages()
{
    OUString aFileURL;
    createFileURL("3AnchorTypes.ods", aFileURL);
    // open the document with graphic included
    uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
    CPPUNIT_ASSERT(xComponent.is());

    // Get the document model
    SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
    CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);

    ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
    CPPUNIT_ASSERT(pDocSh);

    ScDocument* pDoc = &(pDocSh->GetDocument());
    ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
    CPPUNIT_ASSERT(pDrawLayer);

    // Get the document controller
    ScTabViewShell* pViewShell = pDocSh->GetBestViewShell(false);<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0<--- Assignment 'pViewShell=pDocSh->GetBestViewShell(false)', assigned value is 0
    CPPUNIT_ASSERT(pViewShell != nullptr);<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant<--- Assuming that condition 'pViewShell!=nullptr' is not redundant

    // Cut whole column
    {
        // Cut source range
        ScRange aSrcRange;
        aSrcRange.Parse("A1:A11", pDoc, pDoc->GetAddressConvention());
        pViewShell->GetViewData().GetMarkData().SetMarkArea(aSrcRange);<--- Null pointer dereference
        pViewShell->GetViewData().GetView()->CutToClip();<--- Null pointer dereference

        std::map<SCROW, std::vector<SdrObject*>> aRowObjects
            = pDrawLayer->GetObjectsAnchoredToRange(0, 0, 0, 11);

        // Images should have been removed from the cells
        CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no image anchored to A3", 0,
                                     static_cast<int>(aRowObjects[2].size()));
        CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no image anchored to A11", 0,
                                     static_cast<int>(aRowObjects[10].size()));
    }

    // Cut individual cells
    {
        // Cut source cells
        ScRange aSrcRange;
        aSrcRange.Parse("A3:B3", pDoc, pDoc->GetAddressConvention());
        pViewShell->GetViewData().GetMarkData().SetMarkArea(aSrcRange);<--- Null pointer dereference
        pViewShell->GetViewData().GetView()->CutToClip();<--- Null pointer dereference

        // Image should have been removed from the cell
        std::map<SCROW, std::vector<SdrObject*>> aRowObjects
            = pDrawLayer->GetObjectsAnchoredToRange(0, 0, 2, 2);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no image anchored to A3", 0,
                                     static_cast<int>(aRowObjects[2].size()));
    }

    pDocSh->DoClose();
}

void ScAnchorTest::testTdf121963()
{
    OUString aFileURL;
    createFileURL("tdf121963.ods", aFileURL);
    uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
    CPPUNIT_ASSERT(xComponent.is());

    // Without the accompanying fix in place, this test would have never returned due to an infinite
    // invalidation loop, where ScGridWindow::Paint() invalidated itself.
    Scheduler::ProcessEventsToIdle();

    xComponent->dispose();
}

void ScAnchorTest::testTdf129552()
{
    OUString aFileURL;
    createFileURL("tdf129552.fods", aFileURL);
    uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
    CPPUNIT_ASSERT(xComponent.is());

    // Without the accompanying fix in place, this test would have never returned due to an infinite
    // invalidation loop, where ScGridWindow::Paint() invalidated itself.
    Scheduler::ProcessEventsToIdle();

    xComponent->dispose();
}

void ScAnchorTest::testTdf130556()
{
    OUString aFileURL;
    createFileURL("tdf130556.ods", aFileURL);
    uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileURL);
    CPPUNIT_ASSERT(xComponent.is());

    // Without the accompanying fix in place, this test would have never returned due to an infinite
    // invalidation loop, where ScGridWindow::Paint() invalidated itself.
    Scheduler::ProcessEventsToIdle();

    xComponent->dispose();
}

CPPUNIT_TEST_SUITE_REGISTRATION(ScAnchorTest);
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */