1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
22 #include <boost/noncopyable.hpp>
23 #include <cppunit/TestSuite.h>
24 #include <cppunit/TestFixture.h>
25 #include <cppunit/TestCase.h>
26 #include <cppunit/plugin/TestPlugIn.h>
27 #include <cppunit/extensions/HelperMacros.h>
29 #include "Interface1.hpp"
31 #include "rtl/ustring.hxx"
36 using ::com::sun::star::uno::Type
;
37 using ::com::sun::star::uno::Any
;
38 using ::com::sun::star::uno::Reference
;
39 using ::com::sun::star::uno::RuntimeException
;
40 using ::com::sun::star::uno::UNO_SET_THROW
;
42 class Foo
: public Interface1
, private boost::noncopyable
50 virtual Any SAL_CALL
queryInterface(const Type
& _type
)
51 throw (RuntimeException
, std::exception
) SAL_OVERRIDE
54 if (_type
== cppu::UnoType
<XInterface
>::get())
56 Reference
< XInterface
> ref( static_cast< XInterface
* >( this ) );
57 aInterface
.setValue( &ref
, _type
);
59 else if (_type
== cppu::UnoType
<Interface1
>::get())
61 Reference
< Interface1
> ref( this );
62 aInterface
.setValue( &ref
, _type
);
68 virtual void SAL_CALL
acquire() throw () SAL_OVERRIDE
70 osl_atomic_increment( &m_refCount
);
73 virtual void SAL_CALL
release() throw () SAL_OVERRIDE
75 if ( 0 == osl_atomic_decrement( &m_refCount
) )
85 oslInterlockedCount m_refCount
;
88 // Check that the up-casting Reference conversion constructor catches the
91 struct Base1
: public css::uno::XInterface
{
92 virtual ~Base1() SAL_DELETED_FUNCTION
;
94 struct Base2
: public Base1
{ virtual ~Base2() SAL_DELETED_FUNCTION
; };
95 struct Base3
: public Base1
{ virtual ~Base3() SAL_DELETED_FUNCTION
; };
96 struct Derived
: public Base2
, public Base3
{
97 virtual ~Derived() SAL_DELETED_FUNCTION
;
100 // The special case using the conversion operator instead:
101 css::uno::Reference
< css::uno::XInterface
> testUpcast1(
102 css::uno::Reference
< Derived
> const & ref
)
105 // The normal up-cast case:
106 css::uno::Reference
< Base1
> testUpcast2(
107 css::uno::Reference
< Base2
> const & ref
)
110 // Commenting this in should cause a compiler error due to an ambiguous up-cast:
112 css::uno::Reference< Base1 > testFailingUpcast3(
113 css::uno::Reference< Derived > const & ref)
117 // Commenting this in should cause a compiler error due to a down-cast:
119 css::uno::Reference< Base2 > testFailingUpcast4(
120 css::uno::Reference< Base1 > const & ref)
124 // Commenting this in should cause a compiler error due to a down-cast:
126 css::uno::Reference< Base1 > testFailingUpcast5(
127 css::uno::Reference< css::uno::XInterface > const & ref)
131 class Test
: public ::CppUnit::TestFixture
135 void testUnoSetThrow();
136 void testUpcastCompilation();
138 CPPUNIT_TEST_SUITE(Test
);
139 CPPUNIT_TEST(testUnoSetThrow
);
140 CPPUNIT_TEST(testUpcastCompilation
);
141 CPPUNIT_TEST_SUITE_END();
144 void Test::testUnoSetThrow()
146 Reference
< Interface1
> xNull
;
147 Reference
< Interface1
> xFoo( new Foo
);
149 // ctor taking Reference< interface_type >
150 bool bCaughtException
= false;
151 try { Reference
< Interface1
> x( xNull
, UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
152 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
154 bCaughtException
= false;
155 try { Reference
< Interface1
> x( xFoo
, UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
156 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
158 // ctor taking interface_type*
159 bCaughtException
= false;
160 try { Reference
< Interface1
> x( xNull
.get(), UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
161 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
163 bCaughtException
= false;
164 try { Reference
< Interface1
> x( xFoo
.get(), UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
165 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
167 Reference
< Interface1
> x
;
168 // "set" taking Reference< interface_type >
169 bCaughtException
= false;
170 try { x
.set( xNull
, UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
171 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
173 bCaughtException
= false;
174 try { x
.set( xFoo
, UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
175 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
177 // "set" taking interface_type*
178 bCaughtException
= false;
179 try { x
.set( xNull
.get(), UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
180 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
182 bCaughtException
= false;
183 try { x
.set( xFoo
.get(), UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
184 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
187 // Include a dummy test calling those functions, to avoid warnings about those
188 // functions being unused:
189 void Test::testUpcastCompilation()
191 testUpcast1(css::uno::Reference
< Derived
>());
192 testUpcast2(css::uno::Reference
< Base2
>());
195 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
199 CPPUNIT_PLUGIN_IMPLEMENT();
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */