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