LibreOffice Module test (master)  1
unoapi_property_testers.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/uno/Any.hxx>
13 #include <cppunit/TestAssert.h>
14 
15 using namespace css;
16 
17 namespace apitest
18 {
19 void testBooleanProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
20  const OUString& name)
21 {
22  uno::Any aNewValue;
23 
24  bool bPropertyGet = false;
25  bool bPropertySet = false;
26 
27  OString msgGet
28  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
29  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= bPropertyGet);
30 
31  aNewValue <<= !bPropertyGet;
32  xPropertySet->setPropertyValue(name, aNewValue);
33  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= bPropertySet);
34  OString msgSet
35  = "Unable to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
36  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), !bPropertyGet, bPropertySet);
37 }
38 
39 void testBooleanOptionalProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
40  const OUString& rName)
41 {
42  try
43  {
44  testBooleanProperty(xPropertySet, rName);
45  }
46  catch (const css::beans::UnknownPropertyException& /*ex*/)
47  {
48  // ignore if the property is unknown as it is optional
49  }
50 }
51 
52 void testBooleanReadonlyProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
53  const OUString& name)
54 {
55  uno::Any aNewValue;
56 
57  bool bPropertyGet = false;
58  bool bPropertySet = false;
59 
60  OString msgGet
61  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
62  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= bPropertyGet);
63 
64  aNewValue <<= !bPropertyGet;
65  xPropertySet->setPropertyValue(name, aNewValue);
66  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= bPropertySet);
67  OString msgSet = "Able to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
68  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), bPropertyGet, bPropertySet);
69 }
70 
71 void testDoubleProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
72  const OUString& name, const double& dValue)
73 {
74  uno::Any aNewValue;
75 
76  double dPropertyGet;
77  double dPropertySet;
78 
79  OString msgGet
80  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
81  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= dPropertyGet);
82 
83  aNewValue <<= dValue;
84  xPropertySet->setPropertyValue(name, aNewValue);
85  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= dPropertySet);
86  OString msgSet
87  = "Unable to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
88  CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msgSet.getStr(), dValue, dPropertySet, 0.5);
89 }
90 
91 void testDoubleReadonlyProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
92  const OUString& name, const double& dValue)
93 {
94  uno::Any aNewValue;
95 
96  double dPropertyGet;
97  double dPropertySet;
98 
99  OString msgGet
100  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
101  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= dPropertyGet);
102 
103  aNewValue <<= dValue;
104  xPropertySet->setPropertyValue(name, aNewValue);
105  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= dPropertySet);
106  OString msgSet = "Able to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
107  CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msgSet.getStr(), dPropertyGet, dPropertySet, 0.5);
108 }
109 
110 void testLongProperty(uno::Reference<beans::XPropertySet> const& xPropertySet, const OUString& name,
111  const sal_Int32& nValue)
112 {
113  uno::Any aNewValue;
114 
115  sal_Int32 nPropertyGet;
116  sal_Int32 nPropertySet;
117 
118  OString msgGet
119  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
120  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= nPropertyGet);
121 
122  aNewValue <<= nValue;
123  xPropertySet->setPropertyValue(name, aNewValue);
124  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= nPropertySet);
125  OString msgSet
126  = "Unable to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
127  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), nValue, nPropertySet);
128 }
129 
130 void testLongOptionalProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
131  const OUString& rName, const sal_Int32& rValue)
132 {
133  try
134  {
135  testLongProperty(xPropertySet, rName, rValue);
136  }
137  catch (const css::beans::UnknownPropertyException& /*ex*/)
138  {
139  // ignore if the property is unknown as it is optional
140  }
141 }
142 
143 void testLongReadonlyProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
144  const OUString& name, const sal_Int32& nValue)
145 {
146  uno::Any aNewValue;
147 
148  sal_Int32 nPropertyGet;
149  sal_Int32 nPropertySet;
150 
151  OString msgGet
152  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
153  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= nPropertyGet);
154 
155  aNewValue <<= nValue;
156  xPropertySet->setPropertyValue(name, aNewValue);
157  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= nPropertySet);
158  OString msgSet = "Able to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
159  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), nPropertyGet, nPropertySet);
160 }
161 
162 void testShortProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
163  const OUString& name, const sal_Int16& nValue)
164 {
165  uno::Any aNewValue;
166 
167  sal_Int16 nPropertyGet;
168  sal_Int16 nPropertySet;
169 
170  OString msgGet
171  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
172  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= nPropertyGet);
173 
174  aNewValue <<= nValue;
175  xPropertySet->setPropertyValue(name, aNewValue);
176  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= nPropertySet);
177  OString msgSet
178  = "Unable to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
179  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), nValue, nPropertySet);
180 }
181 
182 void testShortOptionalProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
183  const OUString& rName, const sal_Int16& rValue)
184 {
185  try
186  {
187  testShortProperty(xPropertySet, rName, rValue);
188  }
189  catch (const css::beans::UnknownPropertyException& /*ex*/)
190  {
191  // ignore if the property is unknown as it is optional
192  }
193 }
194 
195 void testShortReadonlyProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
196  const OUString& name, const sal_Int16& nValue)
197 {
198  uno::Any aNewValue;
199 
200  sal_Int16 nPropertyGet;
201  sal_Int16 nPropertySet;
202 
203  OString msgGet
204  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
205  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= nPropertyGet);
206 
207  aNewValue <<= nValue;
208  xPropertySet->setPropertyValue(name, aNewValue);
209  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= nPropertySet);
210  OString msgSet = "Able to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
211  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), nPropertyGet, nPropertySet);
212 }
213 
214 void testStringOptionalProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
215  const OUString& rName, const OUString& rValue)
216 {
217  try
218  {
219  testStringProperty(xPropertySet, rName, rValue);
220  }
221  catch (const css::beans::UnknownPropertyException& /*ex*/)
222  {
223  // ignore if the property is unknown as it is optional
224  }
225 }
226 
227 void testStringProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
228  const OUString& name, const OUString& rValue)
229 {
230  uno::Any aNewValue;
231 
232  OUString sPropertyGet;
233  OUString sPropertySet;
234 
235  OString msgGet
236  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
237  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= sPropertyGet);
238 
239  aNewValue <<= rValue;
240  xPropertySet->setPropertyValue(name, aNewValue);
241  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= sPropertySet);
242  OString msgSet
243  = "Unable to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
244  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), rValue, sPropertySet);
245 }
246 
247 void testStringReadonlyProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
248  const OUString& name, const OUString& rValue)
249 {
250  uno::Any aNewValue;
251 
252  OUString sPropertyGet;
253  OUString sPropertySet;
254 
255  OString msgGet
256  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
257  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= sPropertyGet);
258 
259  aNewValue <<= rValue;
260  xPropertySet->setPropertyValue(name, aNewValue);
261  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= sPropertySet);
262  OString msgSet = "Able to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
263  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), sPropertyGet, sPropertySet);
264 }
265 
266 void testColorProperty(uno::Reference<beans::XPropertySet> const& xPropertySet,
267  const OUString& name, const util::Color& rValue)
268 {
269  uno::Any aNewValue;
270 
271  util::Color sPropertyGet;
272  util::Color sPropertySet;
273 
274  OString msgGet
275  = "Unable to get PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
276  CPPUNIT_ASSERT_MESSAGE(msgGet.getStr(), xPropertySet->getPropertyValue(name) >>= sPropertyGet);
277 
278  aNewValue <<= rValue;
279  xPropertySet->setPropertyValue(name, aNewValue);
280  CPPUNIT_ASSERT(xPropertySet->getPropertyValue(name) >>= sPropertySet);
281  OString msgSet
282  = "Unable to set PropertyValue: " + OUStringToOString(name, RTL_TEXTENCODING_UTF8);
283  CPPUNIT_ASSERT_EQUAL_MESSAGE(msgSet.getStr(), rValue, sPropertySet);
284 }
285 
286 } // namespace apitest
287 
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
void testDoubleProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const double &dValue)
void testStringOptionalProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &rName, const OUString &rValue)
void testBooleanReadonlyProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name)
void testDoubleReadonlyProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const double &dValue)
void testColorProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const util::Color &rValue)
void testLongReadonlyProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const sal_Int32 &nValue)
void testBooleanOptionalProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &rName)
void testShortOptionalProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &rName, const sal_Int16 &rValue)
void testShortProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const sal_Int16 &nValue)
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
void testStringReadonlyProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const OUString &rValue)
void testLongOptionalProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &rName, const sal_Int32 &rValue)
void testBooleanProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name)
void testStringProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const OUString &rValue)
void testLongProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const sal_Int32 &nValue)
void testShortReadonlyProperty(uno::Reference< beans::XPropertySet > const &xPropertySet, const OUString &name, const sal_Int16 &nValue)