1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
20 using namespace css::uno
;
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();
34 fillPropsToTest(xPropInfo
);
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
);
98 uno::Any any
= xPropSet
->getPropertyValue(rName
);
99 uno::Type type
= any
.getValueType();
100 if (type
== getCppuType
<sal_Bool
>())
103 sal_Bool bOld
= any
.get
<sal_Bool
>();
104 xPropSet
->setPropertyValue(rName
, makeAny(!bOld
));
106 else if (type
== getCppuType
<sal_Int8
>())
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
>())
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
>())
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
>())
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>())
137 float fOld
= any
.get
<float>();
138 float fNew
= fOld
+ 1.2;
139 xPropSet
->setPropertyValue(rName
, makeAny(fNew
));
141 else if (type
== getCppuType
<double>())
144 double fOld
= any
.get
<double>();
145 double fNew
= fOld
+ 1.3;
146 xPropSet
->setPropertyValue(rName
, makeAny(fNew
));
148 else if (type
== getCppuType
<OUString
>())
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
>())
158 util::DateTime aDT
= any
.get
<util::DateTime
>();
160 xPropSet
->setPropertyValue(rName
, makeAny(aDT
));
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);
178 void XPropertySet::fillPropsToTest(const uno::Reference
<beans::XPropertySetInfo
>& xPropInfo
)
180 if (maPropsToTest
.initialized
)
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)
199 if ((aProp
.Attributes
& beans::PropertyAttribute::READONLY
) != 0)
201 maPropsToTest
.readonly
.push_back(aProp
.Name
);
205 if ((aProp
.Attributes
& beans::PropertyAttribute::MAYBEVOID
) != 0)
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
);
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
);
233 catch (const uno::Exception
&)
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */