LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/qa/extras - sceditfieldobj-cell.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 79 80 98.8 %
Date: 2013-07-09 Functions: 16 18 88.9 %
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/unoapi_test.hxx>
      11             : #include <test/beans/xpropertyset.hxx>
      12             : #include <test/text/xtextfield.hxx>
      13             : #include <test/text/xtextcontent.hxx>
      14             : 
      15             : #include <com/sun/star/beans/XPropertySet.hpp>
      16             : #include <com/sun/star/container/XEnumerationAccess.hpp>
      17             : #include <com/sun/star/text/XText.hpp>
      18             : #include <com/sun/star/text/XTextField.hpp>
      19             : #include <com/sun/star/text/XTextFieldsSupplier.hpp>
      20             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      21             : #include <com/sun/star/sheet/XSpreadsheet.hpp>
      22             : 
      23             : #define NUMBER_OF_TESTS 7
      24             : 
      25             : using namespace css;
      26             : using namespace css::uno;
      27             : 
      28             : namespace sc_apitest {
      29             : 
      30          14 : class ScEditFieldObj_Cell : public UnoApiTest, apitest::XTextField, apitest::XTextContent, apitest::XPropertySet
      31             : {
      32             : public:
      33             :     ScEditFieldObj_Cell();
      34             : 
      35             :     virtual void setUp();
      36             :     virtual void tearDown();
      37             :     virtual uno::Reference<uno::XInterface> init();
      38             :     virtual uno::Reference<text::XTextContent> getTextContent();
      39             :     virtual uno::Reference<text::XTextRange> getTextRange();
      40           0 :     virtual bool isAttachSupported() { return true; }
      41             : 
      42             :     void testEditFieldProperties();
      43             : 
      44           2 :     CPPUNIT_TEST_SUITE(ScEditFieldObj_Cell);
      45             : 
      46             :     // XPropertySet
      47           1 :     CPPUNIT_TEST(testGetPropertySetInfo);
      48           1 :     CPPUNIT_TEST(testGetPropertyValue);
      49           1 :     CPPUNIT_TEST(testSetPropertyValue);
      50             : 
      51             :     // XTextField
      52           1 :     CPPUNIT_TEST(testGetPresentation);
      53             : 
      54             :     // XTextContent
      55           1 :     CPPUNIT_TEST(testGetAnchor);
      56           1 :     CPPUNIT_TEST(testAttach);
      57             : 
      58             :     // Tests specific to this service implementation.
      59           1 :     CPPUNIT_TEST(testEditFieldProperties);
      60             : 
      61           2 :     CPPUNIT_TEST_SUITE_END();
      62             : 
      63             : private:
      64             :     static sal_Int32 nTest;
      65             :     static uno::Reference<lang::XComponent> mxComponent;
      66             :     static uno::Reference<text::XTextField> mxField;
      67             : };
      68             : 
      69             : sal_Int32 ScEditFieldObj_Cell::nTest = 0;
      70           1 : uno::Reference<lang::XComponent> ScEditFieldObj_Cell::mxComponent;
      71           1 : uno::Reference<text::XTextField> ScEditFieldObj_Cell::mxField;
      72             : 
      73           7 : ScEditFieldObj_Cell::ScEditFieldObj_Cell()
      74           7 :      : UnoApiTest("/sc/qa/extras/testdocuments")
      75             : {
      76           7 : }
      77             : 
      78           7 : void ScEditFieldObj_Cell::setUp()
      79             : {
      80           7 :     ++nTest;
      81           7 :     UnoApiTest::setUp();
      82           7 : }
      83             : 
      84           7 : void ScEditFieldObj_Cell::tearDown()
      85             : {
      86           7 :     if (nTest == NUMBER_OF_TESTS)
      87             :     {
      88           1 :         mxField.clear();
      89           1 :         closeDocument(mxComponent);
      90             :     }
      91             : 
      92           7 :     UnoApiTest::tearDown();
      93           7 : }
      94             : 
      95             : namespace {
      96             : 
      97           2 : uno::Reference<text::XTextField> getNewField(const uno::Reference<lang::XMultiServiceFactory>& xSM)
      98             : {
      99             :     uno::Reference<text::XTextField> xField(
     100           2 :         xSM->createInstance("com.sun.star.text.TextField.URL"), UNO_QUERY_THROW);
     101           4 :     uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
     102           2 :     xPropSet->setPropertyValue("Representation", uno::makeAny(OUString("LibreOffice")));
     103           2 :     xPropSet->setPropertyValue("URL", uno::makeAny(OUString("http://www.libreoffice.org/")));
     104           4 :     return xField;
     105             : }
     106             : 
     107             : }
     108             : 
     109          19 : uno::Reference<uno::XInterface> ScEditFieldObj_Cell::init()
     110             : {
     111             :     // Return a field that's already in the cell.
     112          19 :     if (!mxField.is())
     113             :     {
     114           1 :         if (!mxComponent.is())
     115             :             // Load an empty document.
     116           1 :             mxComponent = loadFromDesktop("private:factory/scalc");
     117             : 
     118           1 :         uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
     119             : 
     120             :         // Create a new URL field object, and populate it with name and URL.
     121           1 :         mxField = getNewField(xSM);
     122             : 
     123             :         // Insert this field into a cell.
     124           2 :         uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
     125           2 :         uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
     126           2 :         uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
     127             :         // Use cell A1 for this.
     128           2 :         uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 0);
     129           2 :         uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
     130             : 
     131           2 :         uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
     132           2 :         uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
     133           2 :         uno::Reference<text::XTextContent> xContent(mxField, UNO_QUERY_THROW);
     134           2 :         xText->insertTextContent(xRange, xContent, sal_False);
     135             :     }
     136          19 :     return mxField;
     137             : }
     138             : 
     139           1 : uno::Reference<text::XTextContent> ScEditFieldObj_Cell::getTextContent()
     140             : {
     141             :     // Return a field object that's not yet inserted.
     142           1 :     uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
     143           1 :     return uno::Reference<text::XTextContent>(getNewField(xSM), UNO_QUERY_THROW);
     144             : }
     145             : 
     146           1 : uno::Reference<text::XTextRange> ScEditFieldObj_Cell::getTextRange()
     147             : {
     148             :     // Use cell A2 for this.
     149           1 :     uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
     150           2 :     uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
     151           2 :     uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
     152           2 :     uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 1);
     153           2 :     uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
     154             : 
     155           2 :     uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
     156           1 :     uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
     157           2 :     return xRange;
     158             : }
     159             : 
     160           1 : void ScEditFieldObj_Cell::testEditFieldProperties()
     161             : {
     162           1 :     CPPUNIT_ASSERT_MESSAGE("component doesn't exist.", mxComponent.is());
     163           1 :     uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
     164             : 
     165             :     {
     166             :         // Test properties of date time field.
     167             :         uno::Reference<text::XTextField> xField(
     168           1 :             xSM->createInstance("com.sun.star.text.textfield.DateTime"), UNO_QUERY_THROW);
     169           2 :         uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
     170             : 
     171           2 :         uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
     172           1 :         CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
     173             : 
     174           2 :         CPPUNIT_ASSERT_MESSAGE("Calc's date time field should have 'IsFixed' property.",
     175           2 :                                xInfo->hasPropertyByName("IsFixed"));
     176             :     }
     177             : 
     178             :     {
     179             :         // Test properties of document title field.
     180             :         uno::Reference<text::XTextField> xField(
     181           1 :             xSM->createInstance("com.sun.star.text.textfield.docinfo.Title"), UNO_QUERY_THROW);
     182           2 :         uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
     183             : 
     184           2 :         uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
     185           1 :         CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
     186             : 
     187           2 :         CPPUNIT_ASSERT_MESSAGE("Calc's title field shouldn't have 'IsFixed' property.",
     188           2 :                                !xInfo->hasPropertyByName("IsFixed"));
     189           1 :     }
     190           1 : }
     191             : 
     192           1 : CPPUNIT_TEST_SUITE_REGISTRATION(ScEditFieldObj_Cell);
     193             : 
     194           1 : CPPUNIT_PLUGIN_IMPLEMENT();
     195             : 
     196           3 : }
     197             : 
     198             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10