LCOV - code coverage report
Current view: top level - sc/qa/extras - sceditfieldobj-cell.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 79 80 98.8 %
Date: 2015-06-13 12:38:46 Functions: 19 21 90.5 %
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 <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 CalcUnoApiTest, apitest::XTextField, apitest::XTextContent, apitest::XPropertySet
      31             : {
      32             : public:
      33             :     ScEditFieldObj_Cell();
      34             : 
      35             :     virtual void setUp() SAL_OVERRIDE;
      36             :     virtual void tearDown() SAL_OVERRIDE;
      37             :     virtual uno::Reference<uno::XInterface> init() SAL_OVERRIDE;
      38             :     virtual uno::Reference<text::XTextContent> getTextContent() SAL_OVERRIDE;
      39             :     virtual uno::Reference<text::XTextRange> getTextRange() SAL_OVERRIDE;
      40           0 :     virtual bool isAttachSupported() SAL_OVERRIDE { 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           5 :     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 :      : CalcUnoApiTest("/sc/qa/extras/testdocuments")
      75             : {
      76           7 : }
      77             : 
      78           7 : void ScEditFieldObj_Cell::setUp()
      79             : {
      80           7 :     ++nTest;
      81           7 :     CalcUnoApiTest::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           1 :         mxComponent.clear();
      91             :     }
      92             : 
      93           7 :     CalcUnoApiTest::tearDown();
      94           7 : }
      95             : 
      96             : namespace {
      97             : 
      98           2 : uno::Reference<text::XTextField> getNewField(const uno::Reference<lang::XMultiServiceFactory>& xSM)
      99             : {
     100             :     uno::Reference<text::XTextField> xField(
     101           2 :         xSM->createInstance("com.sun.star.text.TextField.URL"), UNO_QUERY_THROW);
     102           4 :     uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
     103           2 :     xPropSet->setPropertyValue("Representation", uno::makeAny(OUString("LibreOffice")));
     104           2 :     xPropSet->setPropertyValue("URL", uno::makeAny(OUString("http://www.libreoffice.org/")));
     105           4 :     return xField;
     106             : }
     107             : 
     108             : }
     109             : 
     110          19 : uno::Reference<uno::XInterface> ScEditFieldObj_Cell::init()
     111             : {
     112             :     // Return a field that's already in the cell.
     113          19 :     if (!mxField.is())
     114             :     {
     115           1 :         if (!mxComponent.is())
     116             :             // Load an empty document.
     117           1 :             mxComponent = loadFromDesktop("private:factory/scalc");
     118             : 
     119           1 :         uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
     120             : 
     121             :         // Create a new URL field object, and populate it with name and URL.
     122           1 :         mxField = getNewField(xSM);
     123             : 
     124             :         // Insert this field into a cell.
     125           2 :         uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
     126           2 :         uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
     127           2 :         uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
     128             :         // Use cell A1 for this.
     129           2 :         uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 0);
     130           2 :         uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
     131             : 
     132           2 :         uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
     133           2 :         uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
     134           2 :         uno::Reference<text::XTextContent> xContent(mxField, UNO_QUERY_THROW);
     135           2 :         xText->insertTextContent(xRange, xContent, sal_False);
     136             :     }
     137          19 :     return mxField;
     138             : }
     139             : 
     140           1 : uno::Reference<text::XTextContent> ScEditFieldObj_Cell::getTextContent()
     141             : {
     142             :     // Return a field object that's not yet inserted.
     143           1 :     uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
     144           1 :     return uno::Reference<text::XTextContent>(getNewField(xSM), UNO_QUERY_THROW);
     145             : }
     146             : 
     147           1 : uno::Reference<text::XTextRange> ScEditFieldObj_Cell::getTextRange()
     148             : {
     149             :     // Use cell A2 for this.
     150           1 :     uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
     151           2 :     uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
     152           2 :     uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
     153           2 :     uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 1);
     154           2 :     uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
     155             : 
     156           2 :     uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
     157           1 :     uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
     158           2 :     return xRange;
     159             : }
     160             : 
     161           1 : void ScEditFieldObj_Cell::testEditFieldProperties()
     162             : {
     163           1 :     CPPUNIT_ASSERT_MESSAGE("component doesn't exist.", mxComponent.is());
     164           1 :     uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
     165             : 
     166             :     {
     167             :         // Test properties of date time field.
     168             :         uno::Reference<text::XTextField> xField(
     169           1 :             xSM->createInstance("com.sun.star.text.textfield.DateTime"), UNO_QUERY_THROW);
     170           2 :         uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
     171             : 
     172           2 :         uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
     173           1 :         CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
     174             : 
     175           2 :         CPPUNIT_ASSERT_MESSAGE("Calc's date time field should have 'IsFixed' property.",
     176           2 :                                xInfo->hasPropertyByName("IsFixed"));
     177             :     }
     178             : 
     179             :     {
     180             :         // Test properties of document title field.
     181             :         uno::Reference<text::XTextField> xField(
     182           1 :             xSM->createInstance("com.sun.star.text.textfield.docinfo.Title"), UNO_QUERY_THROW);
     183           2 :         uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
     184             : 
     185           2 :         uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
     186           1 :         CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
     187             : 
     188           2 :         CPPUNIT_ASSERT_MESSAGE("Calc's title field shouldn't have 'IsFixed' property.",
     189           2 :                                !xInfo->hasPropertyByName("IsFixed"));
     190           1 :     }
     191           1 : }
     192             : 
     193           1 : CPPUNIT_TEST_SUITE_REGISTRATION(ScEditFieldObj_Cell);
     194             : 
     195             : }
     196             : 
     197           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     198             : 
     199             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11