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::testSetPropertyValue()
44 testGetPropertySetInfo();
46 for (size_t i
= 0, n
= maPropsToTest
.normal
.size(); i
< n
; ++i
)
48 bool bSuccess
= isPropertyValueChangeable(maPropsToTest
.normal
[i
]);
49 CPPUNIT_ASSERT(bSuccess
);
53 void XPropertySet::testGetPropertyValue()
55 testGetPropertySetInfo();
56 uno::Reference
<beans::XPropertySet
> xPropSet(init(), UNO_QUERY_THROW
);
58 // Check read-only properties.
59 for (size_t i
= 0, n
= maPropsToTest
.readonly
.size(); i
< n
; ++i
)
61 bool bSuccess
= getSinglePropertyValue(xPropSet
, maPropsToTest
.readonly
[i
]);
62 CPPUNIT_ASSERT(bSuccess
);
65 // Check writable properties.
66 for (size_t i
= 0, n
= maPropsToTest
.normal
.size(); i
< n
; ++i
)
68 bool bSuccess
= getSinglePropertyValue(xPropSet
, maPropsToTest
.normal
[i
]);
69 CPPUNIT_ASSERT(bSuccess
);
73 bool XPropertySet::isPropertyValueChangeable(const OUString
& rName
)
75 uno::Reference
<beans::XPropertySet
> xPropSet(init(), UNO_QUERY_THROW
);
78 uno::Any any
= xPropSet
->getPropertyValue(rName
);
79 uno::Type type
= any
.getValueType();
80 if (type
== cppu::UnoType
<bool>::get())
83 bool bOld
= any
.get
<sal_Bool
>();
84 xPropSet
->setPropertyValue(rName
, makeAny(!bOld
));
86 else if (type
== cppu::UnoType
<sal_Int8
>::get())
89 sal_Int8 nOld
= any
.get
<sal_Int8
>();
90 sal_Int8 nNew
= nOld
+ 1;
91 xPropSet
->setPropertyValue(rName
, makeAny(nNew
));
93 else if (type
== cppu::UnoType
<sal_Int16
>::get())
96 sal_Int16 nOld
= any
.get
<sal_Int16
>();
97 sal_Int16 nNew
= nOld
+ 2;
98 xPropSet
->setPropertyValue(rName
, makeAny(nNew
));
100 else if (type
== cppu::UnoType
<sal_Int32
>::get())
103 sal_Int32 nOld
= any
.get
<sal_Int32
>();
104 sal_Int32 nNew
= nOld
+ 3;
105 xPropSet
->setPropertyValue(rName
, makeAny(nNew
));
107 else if (type
== cppu::UnoType
<sal_Int64
>::get())
110 sal_Int64 nOld
= any
.get
<sal_Int64
>();
111 sal_Int64 nNew
= nOld
+ 4;
112 xPropSet
->setPropertyValue(rName
, makeAny(nNew
));
114 else if (type
== cppu::UnoType
<float>::get())
117 float fOld
= any
.get
<float>();
118 float fNew
= fOld
+ 1.2;
119 xPropSet
->setPropertyValue(rName
, makeAny(fNew
));
121 else if (type
== cppu::UnoType
<double>::get())
124 double fOld
= any
.get
<double>();
125 double fNew
= fOld
+ 1.3;
126 xPropSet
->setPropertyValue(rName
, makeAny(fNew
));
128 else if (type
== cppu::UnoType
<OUString
>::get())
131 OUString aOld
= any
.get
<OUString
>();
132 OUString aNew
= aOld
+ "foo";
133 xPropSet
->setPropertyValue(rName
, makeAny(aNew
));
135 else if (type
== cppu::UnoType
<util::DateTime
>::get())
138 util::DateTime aDT
= any
.get
<util::DateTime
>();
140 xPropSet
->setPropertyValue(rName
, makeAny(aDT
));
144 CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: unknown type in Any tested.", false);
147 uno::Any anyTest
= xPropSet
->getPropertyValue(rName
);
148 return any
!= anyTest
;
150 catch (const uno::Exception
&)
152 CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: exception thrown while retrieving the property value.", false);
158 void XPropertySet::fillPropsToTest(const uno::Reference
<beans::XPropertySetInfo
>& xPropInfo
)
160 if (maPropsToTest
.initialized
)
163 uno::Sequence
<beans::Property
> aProps
= xPropInfo
->getProperties();
165 // some properties should not be changed in a unspecific way.
166 // TODO: Maybe we should mark these properties read-only, instead of
167 // giving them a special treatment here?
168 std::set
<OUString
> aSkip
;
169 aSkip
.insert("PrinterName");
170 aSkip
.insert("CharRelief");
171 aSkip
.insert("IsLayerMode");
173 for (sal_Int32 i
= 0; i
< aProps
.getLength(); ++i
)
175 beans::Property aProp
= aProps
[i
];
176 if (aSkip
.count(aProp
.Name
) > 0)
179 if ((aProp
.Attributes
& beans::PropertyAttribute::READONLY
) != 0)
181 maPropsToTest
.readonly
.push_back(aProp
.Name
);
185 if ((aProp
.Attributes
& beans::PropertyAttribute::MAYBEVOID
) != 0)
188 bool bBound
= (aProp
.Attributes
& beans::PropertyAttribute::BOUND
) != 0;
189 bool bConstrained
= (aProp
.Attributes
& beans::PropertyAttribute::CONSTRAINED
) != 0;
190 bool bCanChange
= isPropertyValueChangeable(aProp
.Name
);
192 if (bBound
&& bCanChange
)
193 maPropsToTest
.bound
.push_back(aProp
.Name
);
195 if (bConstrained
&& bCanChange
)
196 maPropsToTest
.constrained
.push_back(aProp
.Name
);
199 maPropsToTest
.normal
.push_back(aProp
.Name
);
202 maPropsToTest
.initialized
= true;
205 bool XPropertySet::getSinglePropertyValue(
206 const uno::Reference
<beans::XPropertySet
>& xPropSet
, const OUString
& rName
)
210 xPropSet
->getPropertyValue(rName
);
213 catch (const uno::Exception
&)
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */