update credits
[LibreOffice.git] / test / source / beans / xpropertyset.cxx
blobc0f26d8aac176b1302e5cd26a33bb296f4d5664c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include "test/beans/xpropertyset.hxx"
11 #include "cppunit/extensions/HelperMacros.h"
13 #include <com/sun/star/uno/Type.h>
14 #include <com/sun/star/beans/PropertyAttribute.hpp>
15 #include <com/sun/star/util/DateTime.hpp>
17 #include <set>
19 using namespace css;
20 using namespace css::uno;
22 namespace apitest {
24 XPropertySet::~XPropertySet() {}
26 XPropertySet::PropsToTest::PropsToTest() : initialized(false) {}
28 void XPropertySet::testGetPropertySetInfo()
30 uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
31 uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropSet->getPropertySetInfo();
32 if (xPropInfo.is())
34 fillPropsToTest(xPropInfo);
36 else
38 // TODO: Add a means for the client code to populate the PropsToTest.
42 void XPropertySet::testAddPropertyChangeListener()
44 // TODO: implement this.
47 void XPropertySet::testAddVetoableChangeListener()
49 // TODO: implement this.
52 void XPropertySet::testSetPropertyValue()
54 testGetPropertySetInfo();
56 for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
58 bool bSuccess = isPropertyValueChangeable(maPropsToTest.normal[i]);
59 CPPUNIT_ASSERT(bSuccess);
63 void XPropertySet::testGetPropertyValue()
65 testGetPropertySetInfo();
66 uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
68 // Check read-only properties.
69 for (size_t i = 0, n = maPropsToTest.readonly.size(); i < n; ++i)
71 bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.readonly[i]);
72 CPPUNIT_ASSERT(bSuccess);
75 // Check writable properties.
76 for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
78 bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.normal[i]);
79 CPPUNIT_ASSERT(bSuccess);
83 void XPropertySet::testRemovePropertyChangeListener()
85 // TODO: implement this.
88 void XPropertySet::testRemoveVetoableChangeListener()
90 // TODO: implement this.
93 bool XPropertySet::isPropertyValueChangeable(const OUString& rName)
95 uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
96 try
98 uno::Any any = xPropSet->getPropertyValue(rName);
99 uno::Type type = any.getValueType();
100 if (type == getCppuType<sal_Bool>())
102 // boolean type
103 sal_Bool bOld = any.get<sal_Bool>();
104 xPropSet->setPropertyValue(rName, makeAny(!bOld));
106 else if (type == getCppuType<sal_Int8>())
108 // 8-bit integer
109 sal_Int8 nOld = any.get<sal_Int8>();
110 sal_Int8 nNew = nOld + 1;
111 xPropSet->setPropertyValue(rName, makeAny(nNew));
113 else if (type == getCppuType<sal_Int16>())
115 // 16-bit integer
116 sal_Int16 nOld = any.get<sal_Int16>();
117 sal_Int16 nNew = nOld + 2;
118 xPropSet->setPropertyValue(rName, makeAny(nNew));
120 else if (type == getCppuType<sal_Int32>())
122 // 32-bit integer
123 sal_Int32 nOld = any.get<sal_Int32>();
124 sal_Int32 nNew = nOld + 3;
125 xPropSet->setPropertyValue(rName, makeAny(nNew));
127 else if (type == getCppuType<sal_Int64>())
129 // 64-bit integer
130 sal_Int64 nOld = any.get<sal_Int64>();
131 sal_Int64 nNew = nOld + 4;
132 xPropSet->setPropertyValue(rName, makeAny(nNew));
134 else if (type == getCppuType<float>())
136 // single precision
137 float fOld = any.get<float>();
138 float fNew = fOld + 1.2;
139 xPropSet->setPropertyValue(rName, makeAny(fNew));
141 else if (type == getCppuType<double>())
143 // double precision
144 double fOld = any.get<double>();
145 double fNew = fOld + 1.3;
146 xPropSet->setPropertyValue(rName, makeAny(fNew));
148 else if (type == getCppuType<OUString>())
150 // string type
151 OUString aOld = any.get<OUString>();
152 OUString aNew = aOld + OUString("foo");
153 xPropSet->setPropertyValue(rName, makeAny(aNew));
155 else if (type == getCppuType<util::DateTime>())
157 // date time type
158 util::DateTime aDT = any.get<util::DateTime>();
159 aDT.Year += 1;
160 xPropSet->setPropertyValue(rName, makeAny(aDT));
162 else
164 CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: unknown type in Any tested.", false);
167 uno::Any anyTest = xPropSet->getPropertyValue(rName);
168 return any != anyTest;
170 catch (const uno::Exception&)
172 CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: exception thrown while retrieving the property value.", false);
175 return false;
178 void XPropertySet::fillPropsToTest(const uno::Reference<beans::XPropertySetInfo>& xPropInfo)
180 if (maPropsToTest.initialized)
181 return;
183 uno::Sequence<beans::Property> aProps = xPropInfo->getProperties();
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 std::set<OUString> aSkip;
189 aSkip.insert("PrinterName");
190 aSkip.insert("CharRelief");
191 aSkip.insert("IsLayerMode");
193 for (sal_Int32 i = 0; i < aProps.getLength(); ++i)
195 beans::Property aProp = aProps[i];
196 if (aSkip.count(aProp.Name) > 0)
197 continue;
199 if ((aProp.Attributes & beans::PropertyAttribute::READONLY) != 0)
201 maPropsToTest.readonly.push_back(aProp.Name);
202 continue;
205 if ((aProp.Attributes & beans::PropertyAttribute::MAYBEVOID) != 0)
206 continue;
208 bool bBound = (aProp.Attributes & beans::PropertyAttribute::BOUND) != 0;
209 bool bConstrained = (aProp.Attributes & beans::PropertyAttribute::CONSTRAINED) != 0;
210 bool bCanChange = isPropertyValueChangeable(aProp.Name);
212 if (bBound && bCanChange)
213 maPropsToTest.bound.push_back(aProp.Name);
215 if (bConstrained && bCanChange)
216 maPropsToTest.constrained.push_back(aProp.Name);
218 if (bCanChange)
219 maPropsToTest.normal.push_back(aProp.Name);
222 maPropsToTest.initialized = true;
225 bool XPropertySet::getSinglePropertyValue(
226 const uno::Reference<beans::XPropertySet>& xPropSet, const OUString& rName)
230 xPropSet->getPropertyValue(rName);
231 return true;
233 catch (const uno::Exception&)
236 return false;
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */