LCOV - code coverage report
Current view: top level - test/source/sheet - xspreadsheets2.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 154 154 100.0 %
Date: 2014-04-11 Functions: 18 19 94.7 %
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/sheet/xspreadsheets2.hxx>
      11             : 
      12             : #include <com/sun/star/beans/XPropertySet.hpp>
      13             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      14             : #include <com/sun/star/sheet/XSpreadsheet.hpp>
      15             : #include <com/sun/star/sheet/XSpreadsheets2.hpp>
      16             : #include <com/sun/star/table/XCellRange.hpp>
      17             : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
      18             : #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
      19             : #include <com/sun/star/sheet/XNamedRanges.hpp>
      20             : #include <com/sun/star/sheet/XNamedRange.hpp>
      21             : #include <com/sun/star/table/XCell.hpp>
      22             : #include <com/sun/star/text/XTextRange.hpp>
      23             : #include <com/sun/star/container/XIndexAccess.hpp>
      24             : 
      25             : #include <com/sun/star/table/CellAddress.hpp>
      26             : #include <com/sun/star/table/CellRangeAddress.hpp>
      27             : #include <com/sun/star/sheet/Border.hpp>
      28             : #include <com/sun/star/sheet/NamedRangeFlag.hpp>
      29             : 
      30             : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
      31             : #include <com/sun/star/container/XNameContainer.hpp>
      32             : #include <com/sun/star/table/CellVertJustify.hpp>
      33             : 
      34             : #include <rtl/ustring.hxx>
      35             : #include "cppunit/extensions/HelperMacros.h"
      36             : 
      37             : using namespace css;
      38             : using namespace css::uno;
      39             : 
      40             : namespace apitest {
      41             : 
      42          13 : XSpreadsheets2::XSpreadsheets2():
      43             :     aSrcSheetName("SheetToCopy"),
      44             :     aSrcFileName("rangenamessrc.ods"),
      45          13 :     aDestFileBase("ScNamedRangeObj.ods")
      46             : {
      47          13 : }
      48             : 
      49          13 : XSpreadsheets2::~XSpreadsheets2()
      50             : {
      51          13 : }
      52             : 
      53           1 : void XSpreadsheets2::testImportedSheetNameAndIndex()
      54             : {
      55             : /**
      56             :     Verfiy that the imported sheet has the correct name and is placed at the right requested index
      57             : */
      58             : 
      59           1 :     importSheetToCopy();
      60             : 
      61           1 :     uno::Reference< container::XNameAccess > xDestSheetNameAccess(xDestDoc->getSheets(), UNO_QUERY_THROW);
      62           1 :     CPPUNIT_ASSERT_MESSAGE("Wrong sheet name", xDestSheetNameAccess->hasByName(aSrcSheetName));
      63             : 
      64           1 : }
      65             : 
      66           2 : void XSpreadsheets2::testImportString()
      67             : {
      68             : /**
      69             :     tests the cell A1 containing a string correctly imported
      70             : */
      71           2 :     importSheetToCopy();
      72             : 
      73           2 :     uno::Reference< table::XCell > xSrcCell = xSrcSheet->getCellByPosition(0,0);
      74           4 :     uno::Reference< text::XTextRange > xSrcTextRange(xSrcCell, UNO_QUERY_THROW);
      75           4 :     OUString aSrcString = xSrcTextRange->getString();
      76             : 
      77           4 :     uno::Reference< table::XCell > xDestCell = xDestSheet->getCellByPosition(0,0);
      78           4 :     uno::Reference< text::XTextRange > xDestTextRange(xDestCell, UNO_QUERY_THROW);
      79           4 :     OUString aDestString = xDestTextRange->getString();
      80             : 
      81           4 :     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong string imported", aDestString, aSrcString);
      82           2 : }
      83             : 
      84           2 : void XSpreadsheets2::testImportValue()
      85             : {
      86             : /**
      87             :     tests the cell B1 containing a value correctly imported
      88             : */
      89           2 :     importSheetToCopy();
      90             : 
      91           2 :     uno::Reference< table::XCell > xSrcCell = xSrcSheet->getCellByPosition(1,0);
      92           2 :     sal_Int32 aSrcValue = xSrcCell->getValue();
      93             : 
      94           4 :     uno::Reference< table::XCell > xDestCell = xDestSheet->getCellByPosition(1,0);
      95           2 :     sal_Int32 aDestValue = xDestCell->getValue();
      96             : 
      97           4 :     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong value imported", aSrcValue, aDestValue);
      98           2 : }
      99             : 
     100           1 : void XSpreadsheets2::testImportFormulaBasicMath()
     101             : {
     102             : /**
     103             :     tests the cell C1 containing an arithmetic formula correctly imported
     104             : */
     105           1 :     importSheetToCopy();
     106             : 
     107           1 :     uno::Reference< table::XCell > xSrcCell = xSrcSheet->getCellByPosition(2,0);
     108           2 :     OUString aSrcFormula = xSrcCell->getFormula();
     109             : 
     110           2 :     uno::Reference< table::XCell > xDestCell = xDestSheet->getCellByPosition(2,0);
     111           2 :     OUString aDestFormula = xDestCell->getFormula();
     112             : 
     113             :     // potential problem later: formulas might be adjusted
     114             :     // add some tests that the formulas are correctly adjusted
     115           2 :     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong formula imported", aDestFormula, aSrcFormula);
     116           1 : }
     117             : 
     118           1 : void XSpreadsheets2::testImportFormulaWithNamedRange()
     119             : {
     120             : /**
     121             :     tests the cell D1 containing a formula that uses a NamedRange expression
     122             : */
     123           1 :     importSheetToCopy();
     124             : 
     125           1 :     uno::Reference< table::XCell > xSrcCell = xSrcSheet->getCellByPosition(3,0);
     126           2 :     OUString aSrcFormula = xSrcCell->getFormula();
     127             : 
     128           2 :     uno::Reference< table::XCell > xDestCell = xDestSheet->getCellByPosition(3,0);
     129           2 :     OUString aDestFormula = xDestCell->getFormula();
     130             : 
     131           2 :     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong Namedrange formula imported", aDestFormula, aSrcFormula);
     132           1 : }
     133             : 
     134           1 : void XSpreadsheets2::testImportOverExistingNamedRange()
     135             : {
     136             : /**
     137             :     Both Source and Target file define the named range initial1
     138             :     in Source, initial1 is defined outside the copied sheet
     139             :     In Target, after import sheet, initial1 should point on its initial definition $Sheet1.$B$1
     140             : 
     141             :     NEED MORE WORK
     142             : */
     143           1 :     importSheetToCopy();
     144             : 
     145           1 :     OUString aNamedRangeString("initial1");
     146             : 
     147           2 :     uno::Reference< container::XNameAccess > xDestNamedRangesNameAccess(getNamedRanges(xDestDoc), UNO_QUERY_THROW);
     148           2 :     uno::Any aNr = xDestNamedRangesNameAccess->getByName(aNamedRangeString);
     149           2 :     uno::Reference< sheet::XNamedRange > xDestNamedRange(aNr, UNO_QUERY_THROW);
     150           2 :     OUString aNrDestContent = xDestNamedRange->getContent();
     151             : 
     152           2 :     OUString aExpectedContent("$Sheet1.$B$1");
     153             : 
     154           1 :     std::cout << "testImportSheet : initial1 aNrDestContent " << aNrDestContent << std::endl;
     155           2 :     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong address for initial1", aNrDestContent, aExpectedContent);
     156             : 
     157           1 : }
     158             : 
     159           1 : void XSpreadsheets2::testImportNamedRangeDefinedInSource()
     160             : {
     161             : /**
     162             :     in Source file, InSheetRangeName named range is defined in the copied sheet
     163             :     it does not exists in target file
     164             :     test that the range named is created in target and that it points in the target copied sheet
     165             : */
     166           1 :     importSheetToCopy();
     167             : 
     168             :     // New range name defined in imported sheet $SheetToCopy.$A$7
     169           1 :     OUString aNewInSheetNamedRangeString("InSheetRangeName");
     170           2 :     uno::Reference< container::XNameAccess > xDestNamedRangesNameAccess(getNamedRanges(xDestDoc), UNO_QUERY_THROW);
     171           1 :     CPPUNIT_ASSERT_MESSAGE("InSheetRangeName", xDestNamedRangesNameAccess->hasByName(aNewInSheetNamedRangeString));
     172             : 
     173           2 :     uno::Any aNewInSheetNr = xDestNamedRangesNameAccess->getByName(aNewInSheetNamedRangeString);
     174           2 :     uno::Reference< sheet::XNamedRange > xDestNewInSheetNamedRange(aNewInSheetNr, UNO_QUERY_THROW);
     175           2 :     OUString aNewInSheetNrDestContent = xDestNewInSheetNamedRange->getContent();
     176           2 :     OUString aNewInSheetExpectedContent("$SheetToCopy.$A$7");
     177             : 
     178           1 :     std::cout << "testImportSheet : InSheetRangeName content " << aNewInSheetNrDestContent << std::endl;
     179           1 :     std::cout << "testImportSheet : InSheetRangeName expected " << aNewInSheetExpectedContent << std::endl;
     180           2 :     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong address for InSheetRangeName", aNewInSheetNrDestContent, aNewInSheetExpectedContent);
     181           1 : }
     182             : 
     183           1 : void XSpreadsheets2::testImportNamedRangeRedefinedInSource()
     184             : {
     185             : /**
     186             :     in Source file, initial2 named range is defined in the copied sheet
     187             :     it is defined in another sheet of target file
     188             :     test that the range named points in the target copied sheet
     189             : */
     190           1 :     importSheetToCopy();
     191             : 
     192             :     // the source file redefines an existing named range in the imported sheet --> the target should not be changed
     193           1 :     OUString aRedefinedInSheetNamedRangeString("initial2");
     194           2 :     uno::Reference< container::XNameAccess > xDestNamedRangesNameAccess(getNamedRanges(xDestDoc), UNO_QUERY_THROW);
     195           1 :     CPPUNIT_ASSERT_MESSAGE("aRedefinedInSheetNamedRangeString", xDestNamedRangesNameAccess->hasByName(aRedefinedInSheetNamedRangeString));
     196             : 
     197           2 :     uno::Any aRedefinedInSheetNr = xDestNamedRangesNameAccess->getByName(aRedefinedInSheetNamedRangeString);
     198           2 :     uno::Reference< sheet::XNamedRange > xDestRedefinedInSheetNamedRange(aRedefinedInSheetNr, UNO_QUERY_THROW);
     199           2 :     OUString aRedefinedInSheetNrDestContent = xDestRedefinedInSheetNamedRange->getContent();
     200           2 :     OUString aRedefinedInSheetExpectedContent("$Sheet1.$B$2");
     201           1 :     std::cout << "testImportSheet : initial2 content " << aRedefinedInSheetNrDestContent << std::endl;
     202           2 :     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong address for Redefined InSheet named range", aRedefinedInSheetNrDestContent, aRedefinedInSheetExpectedContent);
     203           1 : }
     204             : 
     205           1 : void XSpreadsheets2::testImportNewNamedRange()
     206             : {
     207             : /**
     208             :     in Source file, new_rangename range named is defined outside the copied sheet
     209             :     it does not exists in target file test that new_rangename is created and its
     210             :     content points to source file as an external reference
     211             : */
     212           1 :     importSheetToCopy();
     213             : 
     214             :     //formula with a non-existant named range in dest - new_rangename
     215           1 :     OUString aNewNamedRangeString("new_rangename");
     216           2 :     uno::Reference< container::XNameAccess > xDestNamedRangesNameAccess(getNamedRanges(xDestDoc), UNO_QUERY_THROW);
     217           1 :     CPPUNIT_ASSERT_MESSAGE("New NamedRange not created", xDestNamedRangesNameAccess->hasByName(aNewNamedRangeString));
     218             : 
     219             :     // verify the content of this new namedrange, pointing on $Sheet1.$B$1 in source. This address is already defined in target as NR content
     220             : 
     221           2 :     uno::Any aNewNr = xDestNamedRangesNameAccess->getByName(aNewNamedRangeString);
     222           2 :     uno::Reference< sheet::XNamedRange > xDestNewNamedRange(aNewNr, UNO_QUERY_THROW);
     223           2 :     OUString aNewNrDestContent = xDestNewNamedRange->getContent();
     224             : 
     225           2 :     OUString aNewExpectedContent("$Sheet1.$B$1");
     226             : 
     227           1 :     std::cout << "testImportSheet : new_rangename aNewExpectedContent " << aNewExpectedContent << std::endl;
     228           1 :     std::cout << "testImportSheet : new_rangename aNewNrDestContent " << aNewNrDestContent << std::endl;
     229           2 :     CPPUNIT_ASSERT_MESSAGE("Wrong New NamedRange formula string value", isExternalReference(aNewNrDestContent, aNewExpectedContent));
     230           1 : }
     231             : 
     232           1 : void XSpreadsheets2::testImportCellStyle()
     233             : {
     234             : /**
     235             :     in source file, imported sheet uses a cellstyle that does not exists in target
     236             :     test that
     237             :         - an imported cell D1 uses the right cellStyle
     238             :         - the cellStyle is created in CellStyles family
     239             :         - a property of the cellStyle (VertJustify) is correctly set
     240             : */
     241           1 :     importSheetToCopy();
     242             : 
     243           1 :     uno::Reference< table::XCell > xSrcCell = xSrcSheet->getCellByPosition(3,0);
     244           2 :     uno::Reference< table::XCell > xDestCell = xDestSheet->getCellByPosition(3,0);
     245             : 
     246             :     //new style created in dest
     247           2 :     uno::Reference< beans::XPropertySet > xSrcCellPropSet (xSrcCell, UNO_QUERY_THROW);
     248           2 :     const OUString aCellProperty("CellStyle");
     249           2 :     OUString aSrcStyleName;
     250           1 :     CPPUNIT_ASSERT(xSrcCellPropSet->getPropertyValue(aCellProperty) >>= aSrcStyleName);
     251             : 
     252           2 :     uno::Reference< beans::XPropertySet > xDestCellPropSet (xSrcCell, UNO_QUERY_THROW);
     253           2 :     OUString aDestStyleName;
     254           1 :     CPPUNIT_ASSERT(xDestCellPropSet->getPropertyValue(aCellProperty) >>= aDestStyleName);
     255             : 
     256           1 :     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong imported Cell Style", aDestStyleName, aSrcStyleName);
     257             : 
     258           2 :     uno::Reference< style::XStyleFamiliesSupplier > xFamiliesSupplier (xDestDoc, UNO_QUERY_THROW);
     259           2 :     uno::Reference< container::XNameAccess > xFamiliesNameAccess (xFamiliesSupplier->getStyleFamilies(), UNO_QUERY_THROW);
     260           2 :     OUString aCellFamilyName("CellStyles");
     261           2 :     uno::Any xCellStylesFamily = xFamiliesNameAccess->getByName(aCellFamilyName);
     262           2 :     uno::Reference< container::XNameContainer > xCellStylesFamilyNameAccess (xCellStylesFamily, UNO_QUERY_THROW);
     263             : 
     264           1 :     CPPUNIT_ASSERT_MESSAGE("New cell style not present", xCellStylesFamilyNameAccess->hasByName(aDestStyleName));
     265             : 
     266           2 :     uno::Any aCellStyle = xCellStylesFamilyNameAccess->getByName(aDestStyleName);
     267           2 :     uno::Reference< beans::XPropertySet > xCellStyleProp (aCellStyle, UNO_QUERY_THROW);
     268           2 :     OUString aProperty("VertJustify");
     269           1 :     sal_Int32 aVertJustify = 0;
     270           1 :     CPPUNIT_ASSERT(xCellStyleProp->getPropertyValue(aProperty) >>= aVertJustify);
     271             : 
     272           2 :     CPPUNIT_ASSERT_MESSAGE("New style: VertJustify not set", aVertJustify == table::CellVertJustify_CENTER);
     273           1 : }
     274             : 
     275           1 : uno::Reference< sheet::XSpreadsheetDocument> XSpreadsheets2::getDoc(const OUString& aFileBase, uno::Reference< lang::XComponent >& xComp)
     276             : {
     277           1 :     OUString aFileURL;
     278           1 :     createFileURL(aFileBase, aFileURL);
     279             : 
     280           1 :     if (!xComp.is())
     281           1 :         xComp = loadFromDesktop(aFileURL);
     282             : 
     283           1 :     CPPUNIT_ASSERT(xComp.is());
     284             : 
     285           1 :     uno::Reference< sheet::XSpreadsheetDocument > xDoc(xComp, UNO_QUERY_THROW);
     286           1 :     CPPUNIT_ASSERT(xDoc.is());
     287           1 :     return xDoc;
     288             : }
     289             : 
     290           4 : uno::Reference< sheet::XNamedRanges> XSpreadsheets2::getNamedRanges(uno::Reference< sheet::XSpreadsheetDocument> xDoc)
     291             : {
     292           4 :     uno::Reference< beans::XPropertySet > xPropSet (xDoc, UNO_QUERY_THROW);
     293           8 :     OUString NamedRangesPropertyString("NamedRanges");
     294           4 :     uno::Reference< sheet::XNamedRanges > xNamedRanges(xPropSet->getPropertyValue(NamedRangesPropertyString), UNO_QUERY_THROW);
     295           4 :     CPPUNIT_ASSERT(xNamedRanges.is());
     296             : 
     297           8 :     return xNamedRanges;
     298             : }
     299             : 
     300          12 : void XSpreadsheets2::importSheetToCopy()
     301             : {
     302          12 :     uno::Reference< container::XNameAccess> xSrcNameAccess(init(),UNO_QUERY_THROW);
     303          12 :     xSrcSheet = uno::Reference< sheet::XSpreadsheet >( xSrcNameAccess->getByName(aSrcSheetName), UNO_QUERY_THROW);
     304             : 
     305          12 :     static uno::Reference< lang::XComponent > xDestComponent;
     306          12 :     if (!xDestComponent.is())
     307             :     {
     308           1 :         xDestDoc = getDoc(aDestFileBase, xDestComponent);
     309           1 :         CPPUNIT_ASSERT(xDestDoc.is());
     310             : 
     311             :         // import sheet
     312           1 :         uno::Reference< sheet::XSpreadsheets2 > xDestSheets (xDestDoc->getSheets(), UNO_QUERY_THROW);
     313           1 :         sal_Int32 nDestPos = 0;
     314           1 :         sal_Int32 nDestPosEffective = xDestSheets->importSheet(xDocument, aSrcSheetName, nDestPos);
     315           1 :         CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong sheet index", nDestPosEffective, nDestPos);
     316             :     }
     317             :     else
     318             :     {
     319          11 :         xDestDoc = uno::Reference< sheet::XSpreadsheetDocument >(xDestComponent,UNO_QUERY_THROW);
     320             :     }
     321             : 
     322          24 :     uno::Reference< container::XNameAccess > xDestSheetNameAccess (xDestDoc->getSheets(), UNO_QUERY_THROW);
     323          24 :     xDestSheet = uno::Reference< sheet::XSpreadsheet > ( xDestSheetNameAccess->getByName(aSrcSheetName), UNO_QUERY_THROW);
     324          12 : }
     325             : 
     326           1 : bool XSpreadsheets2::isExternalReference(const OUString& aDestContent, const OUString& aSrcContent )
     327             : {
     328           1 :     OUString aStart("'file://");
     329             : 
     330           1 :     CPPUNIT_ASSERT(aDestContent.startsWith(aStart));
     331             : 
     332           1 :     return  (aDestContent.endsWithIgnoreAsciiCase(aSrcContent, NULL) // same cell address
     333           1 :             && aDestContent.indexOf(aSrcFileName)>0); // contains source file name
     334             : }
     335             : 
     336          60 : }
     337             : 
     338             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10