merge the formfield patch from ooo-build
[ooovba.git] / cppu / test / FreeReference / FreeReference.test.cxx
blob6fead2bfcb8076178fb17fbb157971a3f18353e0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FreeReference.test.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <iostream>
33 #include "sal/main.h"
35 #include "uno/environment.hxx"
37 #include "cppu/FreeReference.hxx"
38 #include "cppu/EnvGuards.hxx"
40 #include "../ObjectFactory/ObjectFactory.hxx"
44 using namespace com::sun::star;
46 static rtl::OUString s_comment;
47 static uno::Environment s_env;
49 extern "C" {
50 static void s_callee_in(rtl_uString * pMethod_name)
52 rtl::OUString method_name(pMethod_name);
54 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\ts_callee_in method:\""));
55 s_comment += method_name;
57 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" env: "));
58 s_comment += uno::Environment::getCurrent().getTypeName();
60 if (!s_env.is())
62 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: s_env not set"));
63 return;
66 rtl::OUString reason;
67 int valid = s_env.isValid(&reason);
69 if (valid)
70 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
72 else
74 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: "));
75 s_comment += reason;
76 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
80 static void s_callee_out(rtl_uString * pMethod_name)
82 rtl::OUString method_name(pMethod_name);
84 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\ts_callee_out method:\""));
85 s_comment += method_name;
87 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" env: "));
88 s_comment += uno::Environment::getCurrent().getTypeName();
90 if (!s_env.is())
92 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: s_env not set"));
93 return;
96 rtl::OUString reason;
97 int valid = s_env.isValid(&reason);
99 if (!valid)
100 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
102 else
104 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: is in\n"));
109 static cppu::FreeReference<uno::XInterface> s_get_envObject(void)
111 cppu::EnvGuard envGuard(s_env);
113 uno::XInterface * pObject = reinterpret_cast<uno::XInterface *>(
114 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))),
115 s_callee_in));
117 cppu::FreeReference<uno::XInterface> env_obj(pObject, SAL_NO_ACQUIRE);
119 return env_obj;
122 static cppu::FreeReference<uno::XInterface> s_get_flatObject(void)
124 uno::XInterface * pObject = reinterpret_cast<uno::XInterface *>(
125 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))),
126 s_callee_out));
128 return cppu::FreeReference<uno::XInterface>(pObject, SAL_NO_ACQUIRE);
132 static void s_test_operator_arrow(void)
134 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_arrow:\n"));
136 cppu::FreeReference<uno::XInterface> env_obj(s_get_envObject());
138 env_obj->acquire();
139 env_obj->release();
143 static void s_test_operator_assign_empty_reference(void)
145 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_assign_empty_reference\n"));
147 cppu::FreeReference<uno::XInterface> flat_obj(s_get_flatObject());
148 cppu::FreeReference<uno::XInterface> empty_ref;
150 flat_obj = empty_ref;
153 static void s_test_operator_assign_inner_reference_inside(void)
155 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_assign_inner_reference_inside\n"));
157 cppu::FreeReference<uno::XInterface> env_obj(s_get_envObject());
158 cppu::FreeReference<uno::XInterface> flat_obj(s_get_flatObject());
161 cppu::EnvGuard envGuard(s_env);
162 env_obj = flat_obj;
166 static void s_test_method_clear_inner_reference_inside(void)
168 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_clear_inner_reference_inside\n"));
170 cppu::FreeReference<uno::XInterface> env_obj(s_get_envObject());
173 cppu::EnvGuard envGuard(s_env);
174 env_obj.clear();
178 static void s_test_method_clear_inner_reference_outside(void)
180 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_clear_inner_reference_outside\n"));
182 cppu::FreeReference<uno::XInterface> env_obj(s_get_envObject());
184 env_obj.clear();
187 static void s_test_method_clear_outer_reference_inside(void)
189 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_clear_outer_reference_inside\n"));
191 cppu::FreeReference<uno::XInterface> flat_obj(s_get_flatObject());
194 cppu::EnvGuard envGuard(s_env);
195 flat_obj.clear();
199 static void s_test_method_clear_outer_reference_outside(void)
201 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_clear_outer_reference_outside\n"));
203 cppu::FreeReference<uno::XInterface> flat_obj(s_get_flatObject());
205 flat_obj.clear();
208 static void s_test_method_set_inside_with_inner_reference(void)
210 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_inside_with_inner_reference\n"));
212 cppu::FreeReference<uno::XInterface> env_obj(s_get_envObject());
215 cppu::EnvGuard envGuard(s_env);
216 uno::XInterface * pObject2 = reinterpret_cast<uno::XInterface *>(
217 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))),
218 s_callee_in));
220 uno::Reference<uno::XInterface> ref(pObject2, SAL_NO_ACQUIRE);
222 env_obj.set(ref);
227 static void s_test_method_set_inside_with_outer_reference(void)
229 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_inside_with_outer_reference\n"));
231 cppu::FreeReference<uno::XInterface> env_obj(s_get_envObject());
233 uno::XInterface * pObject = reinterpret_cast<uno::XInterface *>(
234 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))),
235 s_callee_out));
237 uno::Reference<uno::XInterface> ref(pObject, SAL_NO_ACQUIRE);
239 env_obj.set(ref);
243 static void s_test_method_set_with_empty_reference(void)
245 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_outside_with_empty_reference\n"));
247 cppu::FreeReference<uno::XInterface> flat_obj(s_get_flatObject());
250 uno::Reference<uno::XInterface> ref;
252 flat_obj.set(ref);
256 static void s_test_method_set_outside_with_inner_reference(void)
258 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_outside_with_inner_reference\n"));
260 cppu::FreeReference<uno::XInterface> flat_obj(s_get_flatObject());
263 cppu::EnvGuard envGuard(s_env);
265 uno::XInterface * pObject2 = reinterpret_cast<uno::XInterface *>(
266 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))),
267 s_callee_in));
269 uno::Reference<uno::XInterface> ref(pObject2, SAL_NO_ACQUIRE);
271 flat_obj.set(ref);
276 static void s_test_method_set_outside_with_outer_reference(void)
278 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_outside_with_outer_reference\n"));
280 cppu::FreeReference<uno::XInterface> flat_obj(s_get_flatObject());
282 uno::XInterface * pObject2 = reinterpret_cast<uno::XInterface *>(
283 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))),
284 s_callee_out));
286 uno::Reference<uno::XInterface> ref(pObject2, SAL_NO_ACQUIRE);
288 flat_obj.set(ref);
292 static void s_test_operator_equal(void)
294 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_equal\n"));
297 cppu::FreeReference<uno::XInterface> env_obj;
298 uno::XInterface * pObject = NULL;
300 cppu::EnvGuard envGuard(s_env);
301 pObject = reinterpret_cast<uno::XInterface *>(
302 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))),
303 s_callee_in));
305 env_obj = cppu::FreeReference<uno::XInterface>(pObject, SAL_NO_ACQUIRE);
309 uno::Mapping mapping(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV) ":unsafe")),
310 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))));
311 uno::Reference<uno::XInterface> tmp;
312 uno::XInterface * pMappedObject = reinterpret_cast<uno::XInterface *>(mapping.mapInterface(pObject, ::getCppuType(&tmp)));
315 cppu::FreeReference<uno::XInterface> flat_obj(pMappedObject, SAL_NO_ACQUIRE);
318 cppu::EnvGuard envGuard(s_env);
320 if (!(env_obj == flat_obj))
321 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\trefs are not equal inside - FAILURE\n"));
324 if (!(env_obj == flat_obj))
325 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\trefs are not equal outside - FAILURE\n"));
328 static void s_test_operator_unequal(void)
330 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_unequal\n"));
333 uno::XInterface * pObject = NULL;
334 cppu::FreeReference<uno::XInterface> env_obj;
336 cppu::EnvGuard envGuard(s_env);
338 pObject = reinterpret_cast<uno::XInterface *>(
339 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))),
340 s_callee_in));
342 env_obj = cppu::FreeReference<uno::XInterface>(pObject, SAL_NO_ACQUIRE);
346 uno::Mapping mapping(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV) ":unsafe")),
347 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))));
348 uno::Reference<uno::XInterface> tmp;
349 uno::XInterface * pMappedObject = reinterpret_cast<uno::XInterface *>(mapping.mapInterface(pObject, ::getCppuType(&tmp)));
352 cppu::FreeReference<uno::XInterface> flat_obj(pMappedObject, SAL_NO_ACQUIRE);
355 cppu::EnvGuard envGuard(s_env);
357 if (env_obj != flat_obj)
358 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\trefs are not equal inside - FAILURE\n"));
361 if (env_obj != flat_obj)
362 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\trefs are not equal outside - FAILURE\n"));
366 SAL_IMPLEMENT_MAIN_WITH_ARGS(/*argc*/, argv)
368 uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO))).enter();
370 s_env = uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("uno:unsafe")));
373 rtl::OUString result;
374 s_test_operator_arrow();
375 s_test_operator_equal();
376 s_test_operator_unequal();
378 s_test_operator_assign_empty_reference();
379 s_test_operator_assign_inner_reference_inside();
381 s_test_method_set_with_empty_reference();
382 s_test_method_set_inside_with_inner_reference();
383 s_test_method_set_inside_with_outer_reference();
384 s_test_method_set_outside_with_inner_reference();
385 s_test_method_set_outside_with_outer_reference();
387 s_test_method_clear_inner_reference_inside();
388 s_test_method_clear_inner_reference_outside();
389 s_test_method_clear_outer_reference_inside();
390 s_test_method_clear_outer_reference_outside();
392 s_env.clear();
395 uno_Environment_enter(NULL);
398 int ret;
399 if (s_comment.indexOf(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILURE"))) == -1)
401 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS PASSED\n"));
402 ret = 0;
404 else
406 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS _NOT_ PASSED\n"));
407 ret = -1;
410 std::cerr
411 << argv[0]
412 << std::endl
413 << rtl::OUStringToOString(s_comment, RTL_TEXTENCODING_ASCII_US).getStr()
414 << std::endl;
416 return ret;