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 12 : XPropertySet::~XPropertySet() {}
25 :
26 12 : XPropertySet::PropsToTest::PropsToTest() : initialized(false) {}
27 :
28 6 : void XPropertySet::testGetPropertySetInfo()
29 : {
30 6 : uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
31 12 : uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropSet->getPropertySetInfo();
32 6 : if (xPropInfo.is())
33 : {
34 6 : fillPropsToTest(xPropInfo);
35 : }
36 : else
37 : {
38 : // TODO: Add a means for the client code to populate the PropsToTest.
39 6 : }
40 6 : }
41 :
42 2 : void XPropertySet::testSetPropertyValue()
43 : {
44 2 : testGetPropertySetInfo();
45 :
46 5 : for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
47 : {
48 3 : bool bSuccess = isPropertyValueChangeable(maPropsToTest.normal[i]);
49 3 : CPPUNIT_ASSERT(bSuccess);
50 : }
51 2 : }
52 :
53 2 : void XPropertySet::testGetPropertyValue()
54 : {
55 2 : testGetPropertySetInfo();
56 2 : uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
57 :
58 : // Check read-only properties.
59 5 : for (size_t i = 0, n = maPropsToTest.readonly.size(); i < n; ++i)
60 : {
61 3 : bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.readonly[i]);
62 3 : CPPUNIT_ASSERT(bSuccess);
63 : }
64 :
65 : // Check writable properties.
66 5 : for (size_t i = 0, n = maPropsToTest.normal.size(); i < n; ++i)
67 : {
68 3 : bool bSuccess = getSinglePropertyValue(xPropSet, maPropsToTest.normal[i]);
69 3 : CPPUNIT_ASSERT(bSuccess);
70 2 : }
71 2 : }
72 :
73 24 : bool XPropertySet::isPropertyValueChangeable(const OUString& rName)
74 : {
75 24 : uno::Reference<beans::XPropertySet> xPropSet(init(), UNO_QUERY_THROW);
76 : try
77 : {
78 24 : uno::Any any = xPropSet->getPropertyValue(rName);
79 48 : uno::Type type = any.getValueType();
80 24 : if (type == cppu::UnoType<bool>::get())
81 : {
82 : // boolean type
83 6 : bool bOld = any.get<sal_Bool>();
84 6 : xPropSet->setPropertyValue(rName, makeAny(!bOld));
85 : }
86 18 : else if (type == cppu::UnoType<sal_Int8>::get())
87 : {
88 : // 8-bit integer
89 0 : sal_Int8 nOld = any.get<sal_Int8>();
90 0 : sal_Int8 nNew = nOld + 1;
91 0 : xPropSet->setPropertyValue(rName, makeAny(nNew));
92 : }
93 18 : else if (type == cppu::UnoType<sal_Int16>::get())
94 : {
95 : // 16-bit integer
96 0 : sal_Int16 nOld = any.get<sal_Int16>();
97 0 : sal_Int16 nNew = nOld + 2;
98 0 : xPropSet->setPropertyValue(rName, makeAny(nNew));
99 : }
100 18 : else if (type == cppu::UnoType<sal_Int32>::get())
101 : {
102 : // 32-bit integer
103 3 : sal_Int32 nOld = any.get<sal_Int32>();
104 3 : sal_Int32 nNew = nOld + 3;
105 3 : xPropSet->setPropertyValue(rName, makeAny(nNew));
106 : }
107 15 : else if (type == cppu::UnoType<sal_Int64>::get())
108 : {
109 : // 64-bit integer
110 0 : sal_Int64 nOld = any.get<sal_Int64>();
111 0 : sal_Int64 nNew = nOld + 4;
112 0 : xPropSet->setPropertyValue(rName, makeAny(nNew));
113 : }
114 15 : else if (type == cppu::UnoType<float>::get())
115 : {
116 : // single precision
117 0 : float fOld = any.get<float>();
118 0 : float fNew = fOld + 1.2;
119 0 : xPropSet->setPropertyValue(rName, makeAny(fNew));
120 : }
121 15 : else if (type == cppu::UnoType<double>::get())
122 : {
123 : // double precision
124 0 : double fOld = any.get<double>();
125 0 : double fNew = fOld + 1.3;
126 0 : xPropSet->setPropertyValue(rName, makeAny(fNew));
127 : }
128 15 : else if (type == cppu::UnoType<OUString>::get())
129 : {
130 : // string type
131 12 : OUString aOld = any.get<OUString>();
132 24 : OUString aNew = aOld + "foo";
133 24 : xPropSet->setPropertyValue(rName, makeAny(aNew));
134 : }
135 3 : else if (type == cppu::UnoType<util::DateTime>::get())
136 : {
137 : // date time type
138 3 : util::DateTime aDT = any.get<util::DateTime>();
139 3 : aDT.Year += 1;
140 3 : xPropSet->setPropertyValue(rName, makeAny(aDT));
141 : }
142 : else
143 : {
144 0 : CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: unknown type in Any tested.", false);
145 : }
146 :
147 48 : uno::Any anyTest = xPropSet->getPropertyValue(rName);
148 48 : return any != anyTest;
149 : }
150 0 : catch (const uno::Exception&)
151 : {
152 0 : CPPUNIT_ASSERT_MESSAGE("XPropertySet::isChangeable: exception thrown while retrieving the property value.", false);
153 : }
154 :
155 24 : return false;
156 : }
157 :
158 6 : void XPropertySet::fillPropsToTest(const uno::Reference<beans::XPropertySetInfo>& xPropInfo)
159 : {
160 6 : if (maPropsToTest.initialized)
161 6 : return;
162 :
163 6 : uno::Sequence<beans::Property> aProps = xPropInfo->getProperties();
164 :
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 12 : std::set<OUString> aSkip;
169 6 : aSkip.insert("PrinterName");
170 6 : aSkip.insert("CharRelief");
171 6 : aSkip.insert("IsLayerMode");
172 :
173 36 : for (sal_Int32 i = 0; i < aProps.getLength(); ++i)
174 : {
175 30 : beans::Property aProp = aProps[i];
176 30 : if (aSkip.count(aProp.Name) > 0)
177 0 : continue;
178 :
179 30 : if ((aProp.Attributes & beans::PropertyAttribute::READONLY) != 0)
180 : {
181 9 : maPropsToTest.readonly.push_back(aProp.Name);
182 9 : continue;
183 : }
184 :
185 21 : if ((aProp.Attributes & beans::PropertyAttribute::MAYBEVOID) != 0)
186 0 : continue;
187 :
188 21 : bool bBound = (aProp.Attributes & beans::PropertyAttribute::BOUND) != 0;
189 21 : bool bConstrained = (aProp.Attributes & beans::PropertyAttribute::CONSTRAINED) != 0;
190 21 : bool bCanChange = isPropertyValueChangeable(aProp.Name);
191 :
192 21 : if (bBound && bCanChange)
193 0 : maPropsToTest.bound.push_back(aProp.Name);
194 :
195 21 : if (bConstrained && bCanChange)
196 0 : maPropsToTest.constrained.push_back(aProp.Name);
197 :
198 21 : if (bCanChange)
199 9 : maPropsToTest.normal.push_back(aProp.Name);
200 21 : }
201 :
202 12 : maPropsToTest.initialized = true;
203 : }
204 :
205 6 : bool XPropertySet::getSinglePropertyValue(
206 : const uno::Reference<beans::XPropertySet>& xPropSet, const OUString& rName)
207 : {
208 : try
209 : {
210 6 : xPropSet->getPropertyValue(rName);
211 6 : return true;
212 : }
213 0 : catch (const uno::Exception&)
214 : {
215 : }
216 0 : return false;
217 : }
218 :
219 90 : }
220 :
221 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|