LibreOffice Module test (master) 1
xpropertyset.cxx
Go to the documentation of this file.
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
11
12#include <com/sun/star/beans/Property.hpp>
13#include <com/sun/star/beans/PropertyAttribute.hpp>
14#include <com/sun/star/beans/PropertyChangeEvent.hpp>
15#include <com/sun/star/beans/XPropertyChangeListener.hpp>
16#include <com/sun/star/beans/XPropertySet.hpp>
17#include <com/sun/star/beans/XVetoableChangeListener.hpp>
18#include <com/sun/star/lang/EventObject.hpp>
19#include <com/sun/star/util/DateTime.hpp>
20#include <com/sun/star/util/XComplexColor.hpp>
21
22#include <com/sun/star/uno/Any.hxx>
23#include <com/sun/star/uno/Reference.hxx>
24#include <com/sun/star/uno/Type.h>
25
28#include <rtl/ref.hxx>
29
30#include <cppunit/TestAssert.h>
31
32using namespace css;
33using namespace css::uno;
34
35namespace apitest
36{
38 : initialized(false)
39{
40}
41
42namespace
43{
44class MockedPropertyChangeListener : public ::cppu::WeakImplHelper<beans::XPropertyChangeListener>
45{
46public:
47 MockedPropertyChangeListener()
48 : m_bListenerCalled(false)
49 {
50 }
51
53
54 virtual void SAL_CALL propertyChange(const beans::PropertyChangeEvent& /* xEvent */) override
55 {
56 m_bListenerCalled = true;
57 }
58
59 virtual void SAL_CALL disposing(const lang::EventObject& /* xEventObj */) override {}
60};
61
62class MockedVetoableChangeListener : public ::cppu::WeakImplHelper<beans::XVetoableChangeListener>
63{
64public:
65 MockedVetoableChangeListener()
66 : m_bListenerCalled(false)
67 {
68 }
69
71
72 virtual void SAL_CALL vetoableChange(const beans::PropertyChangeEvent& /* xEvent */) override
73 {
74 m_bListenerCalled = true;
75 }
76
77 virtual void SAL_CALL disposing(const lang::EventObject& /* xEventObj */) override {}
78};
79}
80
82{
83 uno::Reference<beans::XPropertySet> xPropSet(init(), uno::UNO_QUERY_THROW);
84 uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropSet->getPropertySetInfo();
85 fillPropsToTest(xPropInfo);
86
87 for (const auto& aName : maPropsToTest.bound)
88 {
89 rtl::Reference<MockedPropertyChangeListener> xListener = new MockedPropertyChangeListener();
90 xPropSet->addPropertyChangeListener(
91 aName, uno::Reference<beans::XPropertyChangeListener>(xListener));
93 continue;
94
95 CPPUNIT_ASSERT(xListener->m_bListenerCalled);
96
97 xListener->m_bListenerCalled = false;
98 xPropSet->removePropertyChangeListener(
99 aName, uno::Reference<beans::XPropertyChangeListener>(xListener));
101 CPPUNIT_ASSERT(!xListener->m_bListenerCalled);
102 }
103}
104
106{
107 uno::Reference<beans::XPropertySet> xPropSet(init(), uno::UNO_QUERY_THROW);
108 uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropSet->getPropertySetInfo();
109 fillPropsToTest(xPropInfo);
110
111 for (const auto& aName : maPropsToTest.bound)
112 {
113 rtl::Reference<MockedVetoableChangeListener> xListener = new MockedVetoableChangeListener();
114 xPropSet->addVetoableChangeListener(
115 aName, uno::Reference<beans::XVetoableChangeListener>(xListener));
117 continue;
118
119 CPPUNIT_ASSERT(xListener->m_bListenerCalled);
120
121 xListener->m_bListenerCalled = false;
122 xPropSet->removeVetoableChangeListener(
123 aName, uno::Reference<beans::XVetoableChangeListener>(xListener));
125 CPPUNIT_ASSERT(!xListener->m_bListenerCalled);
126 }
127}
128
130{
131 uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
132 uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropSet->getPropertySetInfo();
133 if (xPropInfo.is())
134 {
135 fillPropsToTest(xPropInfo);
136 }
137 else
138 {
139 // TODO: Add a means for the client code to populate the PropsToTest.
140 }
141}
142
144{
146
147 for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
148 {
150 CPPUNIT_ASSERT(bSuccess);
151 }
152}
153
155{
157 uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
158
159 // Check read-only properties.
160 for (size_t i = 0, n = maPropsToTest.readonly.size(); i < n; ++i)
161 {
162 bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.readonly[i]);
163 CPPUNIT_ASSERT(bSuccess);
164 }
165
166 // Check writable properties.
167 for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
168 {
169 bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.normal[i]);
170 CPPUNIT_ASSERT(bSuccess);
171 }
172}
173
175{
176 bool bIgnore = isPropertyIgnored(rName);
177 if (bIgnore)
178 return false;
179
180 uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
181 try
182 {
183 uno::Any any = xPropSet->getPropertyValue(rName);
184 const uno::Type& type = any.getValueType();
186 {
187 // boolean type
188 bool bOld = any.get<bool>();
189 xPropSet->setPropertyValue(rName, Any(!bOld));
190 }
192 {
193 // 8-bit integer
194 sal_Int8 nOld = any.get<sal_Int8>();
195 sal_Int8 nNew = nOld + 1;
196 xPropSet->setPropertyValue(rName, Any(nNew));
197 }
199 {
200 // 16-bit integer
201 sal_Int16 nOld = any.get<sal_Int16>();
202 sal_Int16 nNew = nOld + 1;
203 xPropSet->setPropertyValue(rName, Any(nNew));
204 }
206 {
207 // 32-bit integer
208 sal_Int32 nOld = any.get<sal_Int32>();
209 sal_Int32 nNew = nOld + 3;
210 xPropSet->setPropertyValue(rName, Any(nNew));
211 }
213 {
214 // 64-bit integer
215 sal_Int64 nOld = any.get<sal_Int64>();
216 sal_Int64 nNew = nOld + 4;
217 xPropSet->setPropertyValue(rName, Any(nNew));
218 }
219 else if (type == cppu::UnoType<float>::get())
220 {
221 // single precision
222 float fOld = any.get<float>();
223 float fNew = fOld + 1.2;
224 xPropSet->setPropertyValue(rName, Any(fNew));
225 }
226 else if (type == cppu::UnoType<double>::get())
227 {
228 // double precision
229 double fOld = any.get<double>();
230 double fNew = fOld + 1.3;
231 xPropSet->setPropertyValue(rName, Any(fNew));
232 }
234 {
235 // string type
236 OUString aOld = any.get<OUString>();
237 OUString aNew = aOld + "foo";
238 xPropSet->setPropertyValue(rName, Any(aNew));
239 }
241 {
242 // date time type
243 util::DateTime aDT = any.get<util::DateTime>();
244 aDT.Year += 1;
245 xPropSet->setPropertyValue(rName, Any(aDT));
246 }
247 else if (type == cppu::UnoType<uno::Reference<util::XComplexColor>>::get())
248 {
249 auto xComplexColor = any.get<uno::Reference<util::XComplexColor>>();
250 auto aComplexColor = model::color::getFromXComplexColor(xComplexColor);
251 xPropSet->setPropertyValue(rName,
252 Any(model::color::createXComplexColor(aComplexColor)));
253 }
254 else
255 {
256 std::cout << "Unknown type:\n"
257 "Type: "
258 << type.getTypeName()
259 << "\n"
260 "Name: "
261 << rName << "\n";
262 CPPUNIT_ASSERT_MESSAGE(
263 "XPropertySet::isPropertyValueChangeable: unknown type in Any tested.", false);
264 }
265
266 uno::Any anyTest = xPropSet->getPropertyValue(rName);
267 return any != anyTest;
268 }
269 catch (const uno::Exception&)
270 {
271 std::cout << "Exception thrown while retrieving with property: " << rName << "\n";
272 CPPUNIT_ASSERT_MESSAGE("XPropertySet::isPropertyValueChangeable: exception thrown while "
273 "retrieving the property value.",
274 false);
275 }
276
277 return false;
278}
279
280void XPropertySet::fillPropsToTest(const uno::Reference<beans::XPropertySetInfo>& xPropInfo)
281{
283 return;
284
285 const uno::Sequence<beans::Property> aProps = xPropInfo->getProperties();
286
287 // some properties should not be changed in a unspecific way.
288 // TODO: Maybe we should mark these properties read-only, instead of
289 // giving them a special treatment here?
290 std::set<OUString> aSkip;
291 aSkip.insert("PrinterName");
292 aSkip.insert("CharRelief");
293 aSkip.insert("IsLayerMode");
294
295 for (const beans::Property& aProp : aProps)
296 {
297 if (aSkip.count(aProp.Name) > 0)
298 continue;
299
300 if ((aProp.Attributes & beans::PropertyAttribute::READONLY) != 0)
301 {
302 maPropsToTest.readonly.push_back(aProp.Name);
303 continue;
304 }
305
306 if ((aProp.Attributes & beans::PropertyAttribute::MAYBEVOID) != 0)
307 continue;
308
309 bool bBound = (aProp.Attributes & beans::PropertyAttribute::BOUND) != 0;
310 bool bConstrained = (aProp.Attributes & beans::PropertyAttribute::CONSTRAINED) != 0;
311 bool bCanChange = isPropertyValueChangeable(aProp.Name);
312
313 if (bBound && bCanChange)
314 maPropsToTest.bound.push_back(aProp.Name);
315
316 if (bConstrained && bCanChange)
317 maPropsToTest.constrained.push_back(aProp.Name);
318
319 if (bCanChange)
320 maPropsToTest.normal.push_back(aProp.Name);
321 }
322
324}
325
326bool XPropertySet::getSinglePropertyValue(const uno::Reference<beans::XPropertySet>& xPropSet,
327 const OUString& rName)
328{
329 try
330 {
331 xPropSet->getPropertyValue(rName);
332 return true;
333 }
334 catch (const uno::Exception&)
335 {
336 }
337 return false;
338}
339
340bool XPropertySet::isPropertyIgnored(const OUString& rName)
341{
342 return m_IgnoreValue.count(rName) > 0;
343}
344
345} // namespace apitest
346
347/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const Any & any
void fillPropsToTest(const css::uno::Reference< css::beans::XPropertySetInfo > &xPropInfo)
PropsToTest maPropsToTest
bool isPropertyValueChangeable(const OUString &rName)
virtual css::uno::Reference< css::uno::XInterface > init()=0
static bool getSinglePropertyValue(const css::uno::Reference< css::beans::XPropertySet > &xPropSet, const OUString &rName)
std::set< OUString > m_IgnoreValue
virtual bool isPropertyIgnored(const OUString &rName)
OUString aName
sal_Int64 n
int i
model::ComplexColor getFromXComplexColor(uno::Reference< util::XComplexColor > const &rxColor)
uno::Reference< util::XComplexColor > createXComplexColor(model::ComplexColor const &rColor)
std::vector< OUString > readonly
std::vector< OUString > normal
std::vector< OUString > bound
std::vector< OUString > constrained
signed char sal_Int8
ResultType type
bool m_bListenerCalled