LCOV - code coverage report
Current view: top level - test/source/beans - xpropertyset.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 81 113 71.7 %
Date: 2012-08-25 Functions: 10 15 66.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 109 288 37.8 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*
       3                 :            :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       4                 :            :  *
       5                 :            :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :            :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :            :  * the License or as specified alternatively below. You may obtain a copy of
       8                 :            :  * the License at http://www.mozilla.org/MPL/
       9                 :            :  *
      10                 :            :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :            :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :            :  * for the specific language governing rights and limitations under the
      13                 :            :  * License.
      14                 :            :  *
      15                 :            :  * Major Contributor(s):
      16                 :            :  *   Copyright (C) 2012 Kohei Yoshida <kohei.yoshida@suse.com>
      17                 :            :  *
      18                 :            :  * All Rights Reserved.
      19                 :            :  *
      20                 :            :  * For minor contributions see the git repository.
      21                 :            :  *
      22                 :            :  * Alternatively, the contents of this file may be used under the terms of
      23                 :            :  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      24                 :            :  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
      25                 :            :  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
      26                 :            :  * instead of those above.
      27                 :            :  */
      28                 :            : 
      29                 :            : #include "test/beans/xpropertyset.hxx"
      30                 :            : #include "cppunit/extensions/HelperMacros.h"
      31                 :            : 
      32                 :            : #include <com/sun/star/uno/Type.h>
      33                 :            : #include <com/sun/star/beans/PropertyAttribute.hpp>
      34                 :            : #include <com/sun/star/util/DateTime.hpp>
      35                 :            : 
      36                 :            : #include <set>
      37                 :            : 
      38                 :            : using namespace com::sun::star::uno;
      39                 :            : 
      40                 :            : namespace apitest {
      41                 :            : 
      42         [ -  + ]:         24 : XPropertySet::~XPropertySet() {}
      43                 :            : 
      44 [ +  - ][ +  - ]:         24 : XPropertySet::PropsToTest::PropsToTest() : initialized(false) {}
                 [ +  - ]
      45                 :            : 
      46                 :         12 : void XPropertySet::testGetPropertySetInfo()
      47                 :            : {
      48 [ +  - ][ +  - ]:         12 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
      49 [ +  - ][ +  - ]:         12 :     uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropSet->getPropertySetInfo();
      50         [ +  - ]:         12 :     if (xPropInfo.is())
      51                 :            :     {
      52         [ +  - ]:         12 :         fillPropsToTest(xPropInfo);
      53                 :            :     }
      54                 :            :     else
      55                 :            :     {
      56                 :            :         // TODO: Add a means for the client code to populate the PropsToTest.
      57                 :         12 :     }
      58                 :         12 : }
      59                 :            : 
      60                 :          0 : void XPropertySet::testAddPropertyChangeListener()
      61                 :            : {
      62                 :            :     // TODO: implement this.
      63                 :          0 : }
      64                 :            : 
      65                 :          0 : void XPropertySet::testAddVetoableChangeListener()
      66                 :            : {
      67                 :            :     // TODO: implement this.
      68                 :          0 : }
      69                 :            : 
      70                 :          4 : void XPropertySet::testSetPropertyValue()
      71                 :            : {
      72                 :          4 :     testGetPropertySetInfo();
      73                 :            : 
      74         [ +  + ]:         10 :     for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
      75                 :            :     {
      76                 :          6 :         bool bSuccess = isPropertyValueChangeable(maPropsToTest.normal[i]);
      77 [ +  - ][ +  - ]:          6 :         CPPUNIT_ASSERT(bSuccess);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
      78                 :            :     }
      79                 :          4 : }
      80                 :            : 
      81                 :          4 : void XPropertySet::testGetPropertyValue()
      82                 :            : {
      83         [ +  - ]:          4 :     testGetPropertySetInfo();
      84 [ +  - ][ +  - ]:          4 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
      85                 :            : 
      86                 :            :     // Check read-only properties.
      87         [ +  + ]:         10 :     for (size_t i = 0, n = maPropsToTest.readonly.size(); i < n; ++i)
      88                 :            :     {
      89         [ +  - ]:          6 :         bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.readonly[i]);
      90 [ +  - ][ +  - ]:          6 :         CPPUNIT_ASSERT(bSuccess);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
      91                 :            :     }
      92                 :            : 
      93                 :            :     // Check writable properties.
      94         [ +  + ]:         10 :     for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
      95                 :            :     {
      96         [ +  - ]:          6 :         bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.normal[i]);
      97 [ +  - ][ +  - ]:          6 :         CPPUNIT_ASSERT(bSuccess);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
      98                 :          4 :     }
      99                 :          4 : }
     100                 :            : 
     101                 :          0 : void XPropertySet::testRemovePropertyChangeListener()
     102                 :            : {
     103                 :            :     // TODO: implement this.
     104                 :          0 : }
     105                 :            : 
     106                 :          0 : void XPropertySet::testRemoveVetoableChangeListener()
     107                 :            : {
     108                 :            :     // TODO: implement this.
     109                 :          0 : }
     110                 :            : 
     111                 :         48 : bool XPropertySet::isPropertyValueChangeable(const rtl::OUString& rName)
     112                 :            : {
     113 [ +  - ][ +  - ]:         48 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
     114                 :            :     try
     115                 :            :     {
     116 [ +  - ][ +  - ]:         48 :         uno::Any any = xPropSet->getPropertyValue(rName);
     117                 :         48 :         uno::Type type = any.getValueType();
     118 [ +  + ][ +  - ]:         48 :         if (type == getCppuType<sal_Bool>())
     119                 :            :         {
     120                 :            :             // boolean type
     121         [ +  - ]:         12 :             sal_Bool bOld = any.get<sal_Bool>();
     122 [ +  - ][ +  - ]:         12 :             xPropSet->setPropertyValue(rName, makeAny(!bOld));
                 [ +  - ]
     123                 :            :         }
     124         [ -  + ]:         36 :         else if (type == getCppuType<sal_Int8>())
     125                 :            :         {
     126                 :            :             // 8-bit integer
     127         [ #  # ]:          0 :             sal_Int8 nOld = any.get<sal_Int8>();
     128                 :          0 :             sal_Int8 nNew = nOld + 1;
     129 [ #  # ][ #  # ]:          0 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
                 [ #  # ]
     130                 :            :         }
     131         [ -  + ]:         36 :         else if (type == getCppuType<sal_Int16>())
     132                 :            :         {
     133                 :            :             // 16-bit integer
     134         [ #  # ]:          0 :             sal_Int16 nOld = any.get<sal_Int16>();
     135                 :          0 :             sal_Int16 nNew = nOld + 2;
     136 [ #  # ][ #  # ]:          0 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
                 [ #  # ]
     137                 :            :         }
     138         [ +  + ]:         36 :         else if (type == getCppuType<sal_Int32>())
     139                 :            :         {
     140                 :            :             // 32-bit integer
     141         [ +  - ]:          6 :             sal_Int32 nOld = any.get<sal_Int32>();
     142                 :          6 :             sal_Int32 nNew = nOld + 3;
     143 [ +  - ][ +  - ]:          6 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
                 [ +  - ]
     144                 :            :         }
     145         [ -  + ]:         30 :         else if (type == getCppuType<sal_Int64>())
     146                 :            :         {
     147                 :            :             // 64-bit integer
     148         [ #  # ]:          0 :             sal_Int64 nOld = any.get<sal_Int64>();
     149                 :          0 :             sal_Int64 nNew = nOld + 4;
     150 [ #  # ][ #  # ]:          0 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
                 [ #  # ]
     151                 :            :         }
     152         [ -  + ]:         30 :         else if (type == getCppuType<float>())
     153                 :            :         {
     154                 :            :             // single precision
     155         [ #  # ]:          0 :             float fOld = any.get<float>();
     156                 :          0 :             float fNew = fOld + 1.2;
     157 [ #  # ][ #  # ]:          0 :             xPropSet->setPropertyValue(rName, makeAny(fNew));
                 [ #  # ]
     158                 :            :         }
     159         [ -  + ]:         30 :         else if (type == getCppuType<double>())
     160                 :            :         {
     161                 :            :             // double precision
     162         [ #  # ]:          0 :             double fOld = any.get<double>();
     163                 :          0 :             double fNew = fOld + 1.3;
     164 [ #  # ][ #  # ]:          0 :             xPropSet->setPropertyValue(rName, makeAny(fNew));
                 [ #  # ]
     165                 :            :         }
     166         [ +  + ]:         30 :         else if (type == getCppuType<rtl::OUString>())
     167                 :            :         {
     168                 :            :             // string type
     169         [ +  - ]:         24 :             rtl::OUString aOld = any.get<rtl::OUString>();
     170                 :         24 :             rtl::OUString aNew = aOld + rtl::OUString("foo");
     171 [ +  - ][ +  - ]:         24 :             xPropSet->setPropertyValue(rName, makeAny(aNew));
                 [ +  - ]
     172                 :            :         }
     173         [ +  - ]:          6 :         else if (type == getCppuType<util::DateTime>())
     174                 :            :         {
     175                 :            :             // date time type
     176         [ +  - ]:          6 :             util::DateTime aDT = any.get<util::DateTime>();
     177                 :          6 :             aDT.Year += 1;
     178 [ +  - ][ +  - ]:          6 :             xPropSet->setPropertyValue(rName, makeAny(aDT));
                 [ +  - ]
     179                 :            :         }
     180                 :            :         else
     181                 :            :         {
     182 [ #  # ][ #  # ]:          0 :             CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: unknown type in Any tested.", false);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     183                 :            :         }
     184                 :            : 
     185 [ +  - ][ +  - ]:         48 :         uno::Any anyTest = xPropSet->getPropertyValue(rName);
     186                 :         48 :         return any != anyTest;
     187                 :            :     }
     188   [ #  #  #  # ]:          0 :     catch (const uno::Exception&)
     189                 :            :     {
     190   [ #  #  #  #  :          0 :         CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: exception thrown while retrieving the property value.", false);
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     191                 :            :     }
     192                 :            : 
     193                 :         48 :     return false;
     194                 :            : }
     195                 :            : 
     196                 :         12 : void XPropertySet::fillPropsToTest(const uno::Reference<beans::XPropertySetInfo>& xPropInfo)
     197                 :            : {
     198         [ +  - ]:         12 :     if (maPropsToTest.initialized)
     199                 :         12 :         return;
     200                 :            : 
     201 [ +  - ][ +  - ]:         12 :     uno::Sequence<beans::Property> aProps = xPropInfo->getProperties();
     202                 :            : 
     203                 :            :     // some properties should not be changed in a unspecific way.
     204                 :            :     // TODO: Maybe we should mark these properties read-only, instead of
     205                 :            :     // giving them a special treatment here?
     206         [ +  - ]:         12 :     std::set<rtl::OUString> aSkip;
     207         [ +  - ]:         12 :     aSkip.insert("PrinterName");
     208         [ +  - ]:         12 :     aSkip.insert("CharRelief");
     209         [ +  - ]:         12 :     aSkip.insert("IsLayerMode");
     210                 :            : 
     211         [ +  + ]:         72 :     for (sal_Int32 i = 0; i < aProps.getLength(); ++i)
     212                 :            :     {
     213         [ +  - ]:         60 :         beans::Property aProp = aProps[i];
     214 [ -  + ][ +  - ]:         60 :         if (aSkip.count(aProp.Name) > 0)
     215                 :          0 :             continue;
     216                 :            : 
     217         [ +  + ]:         60 :         if ((aProp.Attributes & beans::PropertyAttribute::READONLY) != 0)
     218                 :            :         {
     219         [ +  - ]:         18 :             maPropsToTest.readonly.push_back(aProp.Name);
     220                 :         18 :             continue;
     221                 :            :         }
     222                 :            : 
     223         [ -  + ]:         42 :         if ((aProp.Attributes & beans::PropertyAttribute::MAYBEVOID) != 0)
     224                 :          0 :             continue;
     225                 :            : 
     226                 :         42 :         bool bBound       = (aProp.Attributes & beans::PropertyAttribute::BOUND) != 0;
     227                 :         42 :         bool bConstrained = (aProp.Attributes & beans::PropertyAttribute::CONSTRAINED) != 0;
     228         [ +  - ]:         42 :         bool bCanChange = isPropertyValueChangeable(aProp.Name);
     229                 :            : 
     230 [ -  + ][ #  # ]:         42 :         if (bBound && bCanChange)
     231         [ #  # ]:          0 :             maPropsToTest.bound.push_back(aProp.Name);
     232                 :            : 
     233 [ -  + ][ #  # ]:         42 :         if (bConstrained && bCanChange)
     234         [ #  # ]:          0 :             maPropsToTest.constrained.push_back(aProp.Name);
     235                 :            : 
     236         [ +  + ]:         42 :         if (bCanChange)
     237         [ +  - ]:         42 :             maPropsToTest.normal.push_back(aProp.Name);
     238         [ +  + ]:         60 :     }
     239                 :            : 
     240         [ +  - ]:         12 :     maPropsToTest.initialized = true;
     241                 :            : }
     242                 :            : 
     243                 :         12 : bool XPropertySet::getSinglePropertyValue(
     244                 :            :     const uno::Reference<beans::XPropertySet>& xPropSet, const rtl::OUString& rName)
     245                 :            : {
     246                 :            :     try
     247                 :            :     {
     248 [ +  - ][ +  - ]:         12 :         xPropSet->getPropertyValue(rName);
     249         [ #  # ]:         12 :         return true;
     250                 :            :     }
     251                 :          0 :     catch (const uno::Exception&)
     252                 :            :     {
     253                 :            :     }
     254                 :          0 :     return false;
     255                 :            : }
     256                 :            : 
     257 [ +  - ][ +  - ]:         66 : }
     258                 :            : 
     259                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10