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 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/types.h>
21 :
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>
28 :
29 : #include "Interface1.hpp"
30 :
31 : #include "rtl/ustring.hxx"
32 : #include "sal/types.h"
33 :
34 : namespace
35 : {
36 :
37 : using ::com::sun::star::uno::Type;
38 : using ::com::sun::star::uno::Any;
39 : using ::com::sun::star::uno::Reference;
40 : using ::com::sun::star::uno::RuntimeException;
41 : using ::com::sun::star::uno::UNO_SET_THROW;
42 :
43 : class Foo: public Interface1, private boost::noncopyable
44 : {
45 : public:
46 1 : Foo()
47 1 : :m_refCount(0)
48 : {
49 1 : }
50 :
51 0 : virtual Any SAL_CALL queryInterface(const Type & _type)
52 : throw (RuntimeException, std::exception) SAL_OVERRIDE
53 : {
54 0 : Any aInterface;
55 0 : if (_type == getCppuType< Reference< XInterface > >())
56 : {
57 0 : Reference< XInterface > ref( static_cast< XInterface * >( this ) );
58 0 : aInterface.setValue( &ref, _type );
59 : }
60 0 : else if (_type == getCppuType< Reference< Interface1 > >())
61 : {
62 0 : Reference< Interface1 > ref( this );
63 0 : aInterface.setValue( &ref, _type );
64 : }
65 :
66 0 : return Any();
67 : }
68 :
69 5 : virtual void SAL_CALL acquire() throw () SAL_OVERRIDE
70 : {
71 5 : osl_atomic_increment( &m_refCount );
72 5 : }
73 :
74 5 : virtual void SAL_CALL release() throw () SAL_OVERRIDE
75 : {
76 5 : if ( 0 == osl_atomic_decrement( &m_refCount ) )
77 1 : delete this;
78 5 : }
79 :
80 : protected:
81 2 : virtual ~Foo()
82 1 : {
83 2 : }
84 :
85 : private:
86 : oslInterlockedCount m_refCount;
87 : };
88 :
89 : // Check that the up-casting Reference conversion constructor catches the
90 : // intended cases:
91 :
92 : struct Base1: public css::uno::XInterface {
93 : virtual ~Base1() SAL_DELETED_FUNCTION;
94 : };
95 : struct Base2: public Base1 { virtual ~Base2() SAL_DELETED_FUNCTION; };
96 : struct Base3: public Base1 { virtual ~Base3() SAL_DELETED_FUNCTION; };
97 : struct Derived: public Base2, public Base3 {
98 : virtual ~Derived() SAL_DELETED_FUNCTION;
99 : };
100 :
101 : // The special case using the conversion operator instead:
102 1 : css::uno::Reference< css::uno::XInterface > testUpcast1(
103 : css::uno::Reference< Derived > const & ref)
104 1 : { return ref; }
105 :
106 : // The normal up-cast case:
107 1 : css::uno::Reference< Base1 > testUpcast2(
108 : css::uno::Reference< Base2 > const & ref)
109 1 : { return ref; }
110 :
111 : // Commenting this in should cause a compiler error due to an ambiguous up-cast:
112 : /*
113 : css::uno::Reference< Base1 > testFailingUpcast3(
114 : css::uno::Reference< Derived > const & ref)
115 : { return ref; }
116 : */
117 :
118 : // Commenting this in should cause a compiler error due to a down-cast:
119 : /*
120 : css::uno::Reference< Base2 > testFailingUpcast4(
121 : css::uno::Reference< Base1 > const & ref)
122 : { return ref; }
123 : */
124 :
125 : // Commenting this in should cause a compiler error due to a down-cast:
126 : /*
127 : css::uno::Reference< Base1 > testFailingUpcast5(
128 : css::uno::Reference< css::uno::XInterface > const & ref)
129 : { return ref; }
130 : */
131 :
132 6 : class Test: public ::CppUnit::TestFixture
133 : {
134 :
135 : public:
136 : void testUnoSetThrow();
137 : void testUpcastCompilation();
138 :
139 2 : CPPUNIT_TEST_SUITE(Test);
140 1 : CPPUNIT_TEST(testUnoSetThrow);
141 1 : CPPUNIT_TEST(testUpcastCompilation);
142 2 : CPPUNIT_TEST_SUITE_END();
143 : };
144 :
145 1 : void Test::testUnoSetThrow()
146 : {
147 1 : Reference< Interface1 > xNull;
148 2 : Reference< Interface1 > xFoo( new Foo );
149 :
150 : // ctor taking Reference< interface_type >
151 1 : bool bCaughtException = false;
152 2 : try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
153 1 : CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
154 :
155 1 : bCaughtException = false;
156 1 : try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
157 1 : CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
158 :
159 : // ctor taking interface_type*
160 1 : bCaughtException = false;
161 2 : try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
162 1 : CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
163 :
164 1 : bCaughtException = false;
165 1 : try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
166 1 : CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
167 :
168 2 : Reference< Interface1 > x;
169 : // "set" taking Reference< interface_type >
170 1 : bCaughtException = false;
171 2 : try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
172 1 : CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
173 :
174 1 : bCaughtException = false;
175 1 : try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
176 1 : CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
177 :
178 : // "set" taking interface_type*
179 1 : bCaughtException = false;
180 2 : try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
181 1 : CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
182 :
183 1 : bCaughtException = false;
184 1 : try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
185 2 : CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
186 1 : }
187 :
188 : // Include a dummy test calling those functions, to avoid warnings about those
189 : // functions being unused:
190 1 : void Test::testUpcastCompilation()
191 : {
192 1 : testUpcast1(css::uno::Reference< Derived >());
193 1 : testUpcast2(css::uno::Reference< Base2 >());
194 1 : }
195 :
196 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
197 :
198 : } // namespace
199 :
200 4 : CPPUNIT_PLUGIN_IMPLEMENT();
201 :
202 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|