LCOV - code coverage report
Current view: top level - test/source/beans - xpropertyset.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 81 113 71.7 %
Date: 2014-11-03 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          24 : XPropertySet::~XPropertySet() {}
      25             : 
      26          24 : XPropertySet::PropsToTest::PropsToTest() : initialized(false) {}
      27             : 
      28          12 : void XPropertySet::testGetPropertySetInfo()
      29             : {
      30          12 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
      31          24 :     uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropSet->getPropertySetInfo();
      32          12 :     if (xPropInfo.is())
      33             :     {
      34          12 :         fillPropsToTest(xPropInfo);
      35             :     }
      36             :     else
      37             :     {
      38             :         // TODO: Add a means for the client code to populate the PropsToTest.
      39          12 :     }
      40          12 : }
      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           4 : void XPropertySet::testSetPropertyValue()
      53             : {
      54           4 :     testGetPropertySetInfo();
      55             : 
      56          10 :     for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
      57             :     {
      58           6 :         bool bSuccess = isPropertyValueChangeable(maPropsToTest.normal[i]);
      59           6 :         CPPUNIT_ASSERT(bSuccess);
      60             :     }
      61           4 : }
      62             : 
      63           4 : void XPropertySet::testGetPropertyValue()
      64             : {
      65           4 :     testGetPropertySetInfo();
      66           4 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
      67             : 
      68             :     // Check read-only properties.
      69          10 :     for (size_t i = 0, n = maPropsToTest.readonly.size(); i < n; ++i)
      70             :     {
      71           6 :         bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.readonly[i]);
      72           6 :         CPPUNIT_ASSERT(bSuccess);
      73             :     }
      74             : 
      75             :     // Check writable properties.
      76          10 :     for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
      77             :     {
      78           6 :         bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.normal[i]);
      79           6 :         CPPUNIT_ASSERT(bSuccess);
      80           4 :     }
      81           4 : }
      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          48 : bool XPropertySet::isPropertyValueChangeable(const OUString& rName)
      94             : {
      95          48 :     uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
      96             :     try
      97             :     {
      98          48 :         uno::Any any = xPropSet->getPropertyValue(rName);
      99          96 :         uno::Type type = any.getValueType();
     100          48 :         if (type == getCppuType<sal_Bool>())
     101             :         {
     102             :             // boolean type
     103          12 :             bool bOld = any.get<sal_Bool>();
     104          12 :             xPropSet->setPropertyValue(rName, makeAny(!bOld));
     105             :         }
     106          36 :         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          36 :         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          36 :         else if (type == getCppuType<sal_Int32>())
     121             :         {
     122             :             // 32-bit integer
     123           6 :             sal_Int32 nOld = any.get<sal_Int32>();
     124           6 :             sal_Int32 nNew = nOld + 3;
     125           6 :             xPropSet->setPropertyValue(rName, makeAny(nNew));
     126             :         }
     127          30 :         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          30 :         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          30 :         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          30 :         else if (type == getCppuType<OUString>())
     149             :         {
     150             :             // string type
     151          24 :             OUString aOld = any.get<OUString>();
     152          48 :             OUString aNew = aOld + "foo";
     153          48 :             xPropSet->setPropertyValue(rName, makeAny(aNew));
     154             :         }
     155           6 :         else if (type == getCppuType<util::DateTime>())
     156             :         {
     157             :             // date time type
     158           6 :             util::DateTime aDT = any.get<util::DateTime>();
     159           6 :             aDT.Year += 1;
     160           6 :             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          96 :         uno::Any anyTest = xPropSet->getPropertyValue(rName);
     168          96 :         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          48 :     return false;
     176             : }
     177             : 
     178          12 : void XPropertySet::fillPropsToTest(const uno::Reference<beans::XPropertySetInfo>& xPropInfo)
     179             : {
     180          12 :     if (maPropsToTest.initialized)
     181          12 :         return;
     182             : 
     183          12 :     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          24 :     std::set<OUString> aSkip;
     189          12 :     aSkip.insert("PrinterName");
     190          12 :     aSkip.insert("CharRelief");
     191          12 :     aSkip.insert("IsLayerMode");
     192             : 
     193          72 :     for (sal_Int32 i = 0; i < aProps.getLength(); ++i)
     194             :     {
     195          60 :         beans::Property aProp = aProps[i];
     196          60 :         if (aSkip.count(aProp.Name) > 0)
     197           0 :             continue;
     198             : 
     199          60 :         if ((aProp.Attributes & beans::PropertyAttribute::READONLY) != 0)
     200             :         {
     201          18 :             maPropsToTest.readonly.push_back(aProp.Name);
     202          18 :             continue;
     203             :         }
     204             : 
     205          42 :         if ((aProp.Attributes & beans::PropertyAttribute::MAYBEVOID) != 0)
     206           0 :             continue;
     207             : 
     208          42 :         bool bBound       = (aProp.Attributes & beans::PropertyAttribute::BOUND) != 0;
     209          42 :         bool bConstrained = (aProp.Attributes & beans::PropertyAttribute::CONSTRAINED) != 0;
     210          42 :         bool bCanChange = isPropertyValueChangeable(aProp.Name);
     211             : 
     212          42 :         if (bBound && bCanChange)
     213           0 :             maPropsToTest.bound.push_back(aProp.Name);
     214             : 
     215          42 :         if (bConstrained && bCanChange)
     216           0 :             maPropsToTest.constrained.push_back(aProp.Name);
     217             : 
     218          42 :         if (bCanChange)
     219          18 :             maPropsToTest.normal.push_back(aProp.Name);
     220          42 :     }
     221             : 
     222          24 :     maPropsToTest.initialized = true;
     223             : }
     224             : 
     225          12 : bool XPropertySet::getSinglePropertyValue(
     226             :     const uno::Reference<beans::XPropertySet>& xPropSet, const OUString& rName)
     227             : {
     228             :     try
     229             :     {
     230          12 :         xPropSet->getPropertyValue(rName);
     231          12 :         return true;
     232             :     }
     233           0 :     catch (const uno::Exception&)
     234             :     {
     235             :     }
     236           0 :     return false;
     237             : }
     238             : 
     239         144 : }
     240             : 
     241             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10