LCOV - code coverage report
Current view: top level - sc/qa/extras - sccondformats.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 123 123 100.0 %
Date: 2015-06-13 12:38:46 Functions: 17 18 94.4 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <test/calc_unoapi_test.hxx>
      11             : #include <svx/svdograf.hxx>
      12             : #include <svx/svdpage.hxx>
      13             : #include <sfx2/dispatch.hxx>
      14             : 
      15             : #include <com/sun/star/beans/XPropertySet.hpp>
      16             : #include <com/sun/star/sheet/ConditionOperator.hpp>
      17             : #include <com/sun/star/sheet/XSheetConditionalEntries.hpp>
      18             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      19             : #include <com/sun/star/sheet/XSpreadsheet.hpp>
      20             : #include <com/sun/star/table/CellAddress.hpp>
      21             : #include <unonames.hxx>
      22             : 
      23             : #include "tabvwsh.hxx"
      24             : #include "docsh.hxx"
      25             : 
      26             : #include "sc.hrc"
      27             : 
      28             : using namespace css;
      29             : 
      30             : namespace sc_apitest {
      31             : 
      32             : #define NUMBER_OF_TESTS 1
      33             : 
      34           4 : class ScConditionalFormatTest : public CalcUnoApiTest
      35             : {
      36             : public:
      37             :     ScConditionalFormatTest();
      38             : 
      39             :     virtual void setUp() SAL_OVERRIDE;
      40             :     virtual void tearDown() SAL_OVERRIDE;
      41             : 
      42             :     uno::Reference< uno::XInterface > init();
      43             :     void testCondFormat();
      44             :     void testUndoAnchor();
      45             : 
      46           2 :     CPPUNIT_TEST_SUITE(ScConditionalFormatTest);
      47           1 :     CPPUNIT_TEST(testCondFormat);
      48           1 :     CPPUNIT_TEST(testUndoAnchor);
      49           5 :     CPPUNIT_TEST_SUITE_END();
      50             : private:
      51             : 
      52             :     static sal_Int32 nTest;
      53             :     static uno::Reference< lang::XComponent > mxComponent;
      54             : };
      55             : 
      56             : sal_Int32 ScConditionalFormatTest::nTest = 0;
      57           1 : uno::Reference< lang::XComponent > ScConditionalFormatTest::mxComponent;
      58             : 
      59           2 : ScConditionalFormatTest::ScConditionalFormatTest()
      60           2 :     : CalcUnoApiTest("sc/qa/unit/data/ods")
      61             : {
      62           2 : }
      63             : 
      64           1 : uno::Reference< uno::XInterface > ScConditionalFormatTest::init()
      65             : {
      66             :     // get the test file
      67           1 :     OUString aFileURL;
      68           1 :     createFileURL(OUString("new_cond_format_test.ods"), aFileURL);
      69           1 :     if(!mxComponent.is())
      70           1 :         mxComponent = loadFromDesktop(aFileURL);
      71           1 :     CPPUNIT_ASSERT_MESSAGE("Component not loaded",mxComponent.is());
      72             : 
      73             :     // get the first sheet
      74           2 :     uno::Reference< sheet::XSpreadsheetDocument > xDoc(mxComponent, uno::UNO_QUERY_THROW);
      75           2 :     uno::Reference< container::XIndexAccess > xIndex (xDoc->getSheets(), uno::UNO_QUERY_THROW);
      76           2 :     uno::Reference< sheet::XSpreadsheet > xSheet( xIndex->getByIndex(0), uno::UNO_QUERY_THROW);
      77             : 
      78           2 :     return xSheet;
      79             : }
      80             : 
      81           1 : void ScConditionalFormatTest::testCondFormat()
      82             : {
      83           1 :     uno::Reference< sheet::XSpreadsheet > aSheet(init(), uno::UNO_QUERY_THROW);
      84           2 :     uno::Reference< beans::XPropertySet > xProps;
      85           2 :     uno::Reference< sheet::XSheetConditionalEntries > xSheetConditionalEntries;
      86             : 
      87           1 :     xProps.set(aSheet->getCellRangeByPosition(1, 1, 1, 21), uno::UNO_QUERY_THROW);
      88           1 :     xSheetConditionalEntries.set(xProps->getPropertyValue(SC_UNONAME_CONDFMT), uno::UNO_QUERY_THROW);
      89           1 :     CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xSheetConditionalEntries->getCount());
      90             : 
      91           2 :     uno::Sequence< beans::PropertyValue > aPropertyValueList(5);
      92           1 :     aPropertyValueList[0].Name = SC_UNONAME_STYLENAME;
      93           1 :     aPropertyValueList[0].Value <<= uno::makeAny<OUString>("Result2");
      94           1 :     aPropertyValueList[1].Name = SC_UNONAME_FORMULA1;
      95           1 :     aPropertyValueList[1].Value <<= uno::makeAny<OUString>("$Sheet1.$B$2");
      96           1 :     aPropertyValueList[2].Name = SC_UNONAME_FORMULA2;
      97           1 :     aPropertyValueList[2].Value <<= uno::makeAny<OUString>("$Sheet1.$A$2");
      98           1 :     aPropertyValueList[3].Name = SC_UNONAME_OPERATOR;
      99           1 :     aPropertyValueList[3].Value <<= sheet::ConditionOperator_EQUAL;
     100           1 :     aPropertyValueList[4].Name = SC_UNONAME_SOURCEPOS;
     101           1 :     aPropertyValueList[4].Value <<= uno::makeAny(table::CellAddress(0, 0, 16));
     102           1 :     xSheetConditionalEntries->addNew(aPropertyValueList);
     103           1 :     CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xSheetConditionalEntries->getCount());
     104           1 :     xProps->setPropertyValue(SC_UNONAME_CONDFMT, uno::makeAny(xSheetConditionalEntries));
     105             : 
     106           1 :     xProps.set(aSheet->getCellByPosition(0, 16), uno::UNO_QUERY_THROW);
     107           1 :     xSheetConditionalEntries.set(xProps->getPropertyValue(SC_UNONAME_CONDFMT), uno::UNO_QUERY_THROW);
     108           1 :     CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xSheetConditionalEntries->getCount());
     109             : 
     110             :     // clear conditional formatting in cell $B$17
     111           1 :     xProps.set(aSheet->getCellByPosition(1, 16), uno::UNO_QUERY_THROW);
     112           1 :     xSheetConditionalEntries.set(xProps->getPropertyValue(SC_UNONAME_CONDFMT), uno::UNO_QUERY_THROW);
     113           1 :     CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xSheetConditionalEntries->getCount());
     114           1 :     xSheetConditionalEntries->clear();
     115           1 :     xProps->setPropertyValue(SC_UNONAME_CONDFMT, uno::makeAny(xSheetConditionalEntries));
     116             : 
     117           1 :     xProps.set(aSheet->getCellByPosition(1, 16), uno::UNO_QUERY_THROW);
     118           1 :     xSheetConditionalEntries.set(xProps->getPropertyValue(SC_UNONAME_CONDFMT), uno::UNO_QUERY_THROW);
     119             :     // This was 1 before - conditional formats were not removed
     120           2 :     CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xSheetConditionalEntries->getCount());
     121           1 : }
     122             : 
     123           1 : void ScConditionalFormatTest::testUndoAnchor()
     124             : {
     125           1 :     const OString sFailedMessage = OString("Failed on :");
     126           2 :     OUString aFileURL;
     127           1 :     createFileURL(OUString("document_with_linked_graphic.ods"), aFileURL);
     128             :     // open the document with graphic included
     129           2 :     uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileURL);
     130           1 :     CPPUNIT_ASSERT(xComponent.is());
     131             : 
     132             :     // Get the document model
     133           1 :     SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
     134           1 :     CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
     135             : 
     136           1 :     ScDocShell* xDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
     137           1 :     CPPUNIT_ASSERT(xDocSh != NULL);
     138             : 
     139             :     // Check whether graphic imported well
     140           1 :     ScDocument& rDoc = xDocSh->GetDocument();
     141           1 :     ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
     142           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pDrawLayer != NULL );
     143             : 
     144           1 :     const SdrPage *pPage = pDrawLayer->GetPage(0);
     145           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pPage != NULL );
     146             : 
     147           1 :     SdrGrafObj* pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(0));
     148           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pObject != NULL );
     149           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pObject->IsLinkedGraphic() );
     150             : 
     151           1 :     const GraphicObject& rGraphicObj = pObject->GetGraphicObject(true);
     152           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), !rGraphicObj.IsSwappedOut());
     153           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
     154           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
     155             : 
     156             :     // Get the document controller
     157           1 :     ScTabViewShell* pViewShell = xDocSh->GetBestViewShell(false);
     158           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pViewShell != NULL );
     159             : 
     160             :     // Get the draw view of the document
     161           1 :     ScDrawView* pDrawView = pViewShell->GetViewData().GetScDrawView();
     162           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pDrawView != NULL );
     163             : 
     164             :     // Select graphic object
     165           1 :     pDrawView->MarkNextObj(false);
     166           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pDrawView->AreObjectsMarked() );
     167             : 
     168             :     // Set Cell Anchor
     169           1 :     ScDrawLayer::SetCellAnchoredFromPosition(*pObject, rDoc, 0);
     170             :     // Check state
     171           1 :     ScAnchorType oldType = ScDrawLayer::GetAnchorType(*pObject);
     172           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), oldType == SCA_CELL );
     173             : 
     174             :     // Change all selected objects to page anchor
     175           1 :     pViewShell->GetViewData().GetDispatcher().Execute(SID_ANCHOR_PAGE);
     176             :     // Check state
     177           1 :     ScAnchorType newType = ScDrawLayer::GetAnchorType(*pObject);
     178           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), newType == SCA_PAGE );
     179             : 
     180             :     // Undo and check its result.
     181           1 :     SfxUndoManager* pUndoMgr = rDoc.GetUndoManager();
     182           1 :     CPPUNIT_ASSERT(pUndoMgr);
     183           1 :     pUndoMgr->Undo();
     184             : 
     185             :     // Check anchor type
     186           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), oldType == ScDrawLayer::GetAnchorType(*pObject) );
     187           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
     188           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
     189             : 
     190           1 :     pUndoMgr->Redo();
     191             : 
     192             :     // Check anchor type
     193           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), newType == ScDrawLayer::GetAnchorType(*pObject) );
     194           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
     195           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
     196             : 
     197           1 :     ScDrawLayer::SetPageAnchored(*pObject);
     198             :     // Check state
     199           1 :     oldType = ScDrawLayer::GetAnchorType(*pObject);
     200           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), oldType == SCA_PAGE );
     201             : 
     202             :     // Change all selected objects to cell anchor
     203           1 :     pViewShell->GetViewData().GetDispatcher().Execute(SID_ANCHOR_CELL);
     204             :     // Check state
     205           1 :     newType = ScDrawLayer::GetAnchorType(*pObject);
     206           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), newType == SCA_CELL );
     207             : 
     208           1 :     pUndoMgr->Undo();
     209             : 
     210             :     // Check anchor type
     211           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), oldType == ScDrawLayer::GetAnchorType(*pObject) );
     212           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
     213           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
     214             : 
     215           1 :     pUndoMgr->Redo();
     216             : 
     217             :     // Check anchor type
     218           1 :     CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), newType == ScDrawLayer::GetAnchorType(*pObject) );
     219           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
     220           2 :     CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
     221           1 : }
     222             : 
     223           2 : void ScConditionalFormatTest::setUp()
     224             : {
     225           2 :     nTest++;
     226           2 :     CalcUnoApiTest::setUp();
     227           2 : }
     228             : 
     229           2 : void ScConditionalFormatTest::tearDown()
     230             : {
     231           2 :     if (nTest == NUMBER_OF_TESTS)
     232             :     {
     233           1 :         closeDocument(mxComponent);
     234           1 :         mxComponent.clear();
     235             :     }
     236             : 
     237           2 :     CalcUnoApiTest::tearDown();
     238           2 : }
     239             : 
     240           1 : CPPUNIT_TEST_SUITE_REGISTRATION(ScConditionalFormatTest);
     241             : 
     242             : }
     243             : 
     244           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     245             : 
     246             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11