LCOV - code coverage report
Current view: top level - test/source/beans - xpropertyset.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 81 113 71.7 %
Date: 2014-04-11 Functions: 10 15 66.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/beans/xpropertyset.hxx"
      11             : #include "cppunit/extensions/HelperMacros.h"
      12             : 
      13             : #include <com/sun/star/uno/Type.h>
      14             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      15             : #include <com/sun/star/util/DateTime.hpp>
      16             : 
      17             : #include <set>
      18             : 
      19             : using namespace css;
      20             : using namespace css::uno;
      21             : 
      22             : namespace apitest {
      23             : 
      24          12 : XPropertySet::~XPropertySet() {}
      25             : 
      26          12 : XPropertySet::PropsToTest::PropsToTest() : initialized(false) {}
      27             : 
      28           6 : void XPropertySet::testGetPropertySetInfo()
      29             : {
      30           6 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
      31          12 :     uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropSet->getPropertySetInfo();
      32           6 :     if (xPropInfo.is())
      33             :     {
      34           6 :         fillPropsToTest(xPropInfo);
      35             :     }
      36             :     else
      37             :     {
      38             :         // TODO: Add a means for the client code to populate the PropsToTest.
      39           6 :     }
      40           6 : }
      41             : 
      42           0 : void XPropertySet::testAddPropertyChangeListener()
      43             : {
      44             :     // TODO: implement this.
      45           0 : }
      46             : 
      47           0 : void XPropertySet::testAddVetoableChangeListener()
      48             : {
      49             :     // TODO: implement this.
      50           0 : }
      51             : 
      52           2 : void XPropertySet::testSetPropertyValue()
      53             : {
      54           2 :     testGetPropertySetInfo();
      55             : 
      56           5 :     for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
      57             :     {
      58           3 :         bool bSuccess = isPropertyValueChangeable(maPropsToTest.normal[i]);
      59           3 :         CPPUNIT_ASSERT(bSuccess);
      60             :     }
      61           2 : }
      62             : 
      63           2 : void XPropertySet::testGetPropertyValue()
      64             : {
      65           2 :     testGetPropertySetInfo();
      66           2 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
      67             : 
      68             :     // Check read-only properties.
      69           5 :     for (size_t i = 0, n = maPropsToTest.readonly.size(); i < n; ++i)
      70             :     {
      71           3 :         bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.readonly[i]);
      72           3 :         CPPUNIT_ASSERT(bSuccess);
      73             :     }
      74             : 
      75             :     // Check writable properties.
      76           5 :     for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
      77             :     {
      78           3 :         bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.normal[i]);
      79           3 :         CPPUNIT_ASSERT(bSuccess);
      80           2 :     }
      81           2 : }
      82             : 
      83           0 : void XPropertySet::testRemovePropertyChangeListener()
      84             : {
      85             :     // TODO: implement this.
      86           0 : }
      87             : 
      88           0 : void XPropertySet::testRemoveVetoableChangeListener()
      89             : {
      90             :     // TODO: implement this.
      91           0 : }
      92             : 
      93          24 : bool XPropertySet::isPropertyValueChangeable(const OUString& rName)
      94             : {
      95          24 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
      96             :     try
      97             :     {
      98          24 :         uno::Any any = xPropSet->getPropertyValue(rName);
      99          48 :         uno::Type type = any.getValueType();
     100          24 :         if (type == getCppuType<sal_Bool>())
     101             :         {
     102             :             // boolean type
     103           6 :             sal_Bool bOld = any.get<sal_Bool>();
     104           6 :             xPropSet->setPropertyValue(rName, makeAny(!bOld));
     105             :         }
     106          18 :         else if (type == getCppuType<sal_Int8>())
     107             :         {
     108             :             // 8-bit integer
     109           0 :             sal_Int8 nOld = any.get<sal_Int8>();
     110           0 :             sal_Int8 nNew = nOld + 1;
     111           0 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
     112             :         }
     113          18 :         else if (type == getCppuType<sal_Int16>())
     114             :         {
     115             :             // 16-bit integer
     116           0 :             sal_Int16 nOld = any.get<sal_Int16>();
     117           0 :             sal_Int16 nNew = nOld + 2;
     118           0 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
     119             :         }
     120          18 :         else if (type == getCppuType<sal_Int32>())
     121             :         {
     122             :             // 32-bit integer
     123           3 :             sal_Int32 nOld = any.get<sal_Int32>();
     124           3 :             sal_Int32 nNew = nOld + 3;
     125           3 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
     126             :         }
     127          15 :         else if (type == getCppuType<sal_Int64>())
     128             :         {
     129             :             // 64-bit integer
     130           0 :             sal_Int64 nOld = any.get<sal_Int64>();
     131           0 :             sal_Int64 nNew = nOld + 4;
     132           0 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
     133             :         }
     134          15 :         else if (type == getCppuType<float>())
     135             :         {
     136             :             // single precision
     137           0 :             float fOld = any.get<float>();
     138           0 :             float fNew = fOld + 1.2;
     139           0 :             xPropSet->setPropertyValue(rName, makeAny(fNew));
     140             :         }
     141          15 :         else if (type == getCppuType<double>())
     142             :         {
     143             :             // double precision
     144           0 :             double fOld = any.get<double>();
     145           0 :             double fNew = fOld + 1.3;
     146           0 :             xPropSet->setPropertyValue(rName, makeAny(fNew));
     147             :         }
     148          15 :         else if (type == getCppuType<OUString>())
     149             :         {
     150             :             // string type
     151          12 :             OUString aOld = any.get<OUString>();
     152          24 :             OUString aNew = aOld + "foo";
     153          24 :             xPropSet->setPropertyValue(rName, makeAny(aNew));
     154             :         }
     155           3 :         else if (type == getCppuType<util::DateTime>())
     156             :         {
     157             :             // date time type
     158           3 :             util::DateTime aDT = any.get<util::DateTime>();
     159           3 :             aDT.Year += 1;
     160           3 :             xPropSet->setPropertyValue(rName, makeAny(aDT));
     161             :         }
     162             :         else
     163             :         {
     164           0 :             CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: unknown type in Any tested.", false);
     165             :         }
     166             : 
     167          48 :         uno::Any anyTest = xPropSet->getPropertyValue(rName);
     168          48 :         return any != anyTest;
     169             :     }
     170           0 :     catch (const uno::Exception&)
     171             :     {
     172           0 :         CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: exception thrown while retrieving the property value.", false);
     173             :     }
     174             : 
     175          24 :     return false;
     176             : }
     177             : 
     178           6 : void XPropertySet::fillPropsToTest(const uno::Reference<beans::XPropertySetInfo>& xPropInfo)
     179             : {
     180           6 :     if (maPropsToTest.initialized)
     181           6 :         return;
     182             : 
     183           6 :     uno::Sequence<beans::Property> aProps = xPropInfo->getProperties();
     184             : 
     185             :     // some properties should not be changed in a unspecific way.
     186             :     // TODO: Maybe we should mark these properties read-only, instead of
     187             :     // giving them a special treatment here?
     188          12 :     std::set<OUString> aSkip;
     189           6 :     aSkip.insert("PrinterName");
     190           6 :     aSkip.insert("CharRelief");
     191           6 :     aSkip.insert("IsLayerMode");
     192             : 
     193          36 :     for (sal_Int32 i = 0; i < aProps.getLength(); ++i)
     194             :     {
     195          30 :         beans::Property aProp = aProps[i];
     196          30 :         if (aSkip.count(aProp.Name) > 0)
     197           0 :             continue;
     198             : 
     199          30 :         if ((aProp.Attributes & beans::PropertyAttribute::READONLY) != 0)
     200             :         {
     201           9 :             maPropsToTest.readonly.push_back(aProp.Name);
     202           9 :             continue;
     203             :         }
     204             : 
     205          21 :         if ((aProp.Attributes & beans::PropertyAttribute::MAYBEVOID) != 0)
     206           0 :             continue;
     207             : 
     208          21 :         bool bBound       = (aProp.Attributes & beans::PropertyAttribute::BOUND) != 0;
     209          21 :         bool bConstrained = (aProp.Attributes & beans::PropertyAttribute::CONSTRAINED) != 0;
     210          21 :         bool bCanChange = isPropertyValueChangeable(aProp.Name);
     211             : 
     212          21 :         if (bBound && bCanChange)
     213           0 :             maPropsToTest.bound.push_back(aProp.Name);
     214             : 
     215          21 :         if (bConstrained && bCanChange)
     216           0 :             maPropsToTest.constrained.push_back(aProp.Name);
     217             : 
     218          21 :         if (bCanChange)
     219           9 :             maPropsToTest.normal.push_back(aProp.Name);
     220          21 :     }
     221             : 
     222          12 :     maPropsToTest.initialized = true;
     223             : }
     224             : 
     225           6 : bool XPropertySet::getSinglePropertyValue(
     226             :     const uno::Reference<beans::XPropertySet>& xPropSet, const OUString& rName)
     227             : {
     228             :     try
     229             :     {
     230           6 :         xPropSet->getPropertyValue(rName);
     231           6 :         return true;
     232             :     }
     233           0 :     catch (const uno::Exception&)
     234             :     {
     235             :     }
     236           0 :     return false;
     237             : }
     238             : 
     239          60 : }
     240             : 
     241             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10