xref: /trunk/main/cppu/qa/test_reference.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppu.hxx"
26 
27 #include "sal/config.h"
28 
29 #include "Interface1.hpp"
30 
31 #include "rtl/ustring.hxx"
32 #include "sal/types.h"
33 #include "gtest/gtest.h"
34 
35 namespace
36 {
37 
38 using ::com::sun::star::uno::Type;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::RuntimeException;
42 using ::com::sun::star::uno::UNO_SET_THROW;
43 
44 class Foo: public Interface1
45 {
46 public:
Foo()47     Foo()
48         :m_refCount(0)
49     {
50     }
51 
queryInterface(const Type & _type)52     virtual Any SAL_CALL queryInterface(const Type & _type)
53     {
54         Any aInterface;
55         if (_type == getCppuType< Reference< XInterface > >())
56         {
57             Reference< XInterface > ref( static_cast< XInterface * >( this ) );
58             aInterface.setValue( &ref, _type );
59         }
60         else if (_type == getCppuType< Reference< Interface1 > >())
61         {
62             Reference< Interface1 > ref( this );
63             aInterface.setValue( &ref, _type );
64         }
65 
66         return Any();
67     }
68 
acquire()69     virtual void SAL_CALL acquire() throw ()
70     {
71         osl_incrementInterlockedCount( &m_refCount );
72     }
73 
release()74     virtual void SAL_CALL release() throw ()
75     {
76         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
77             delete this;
78     }
79 
80 protected:
~Foo()81     virtual ~Foo()
82     {
83     }
84 
85 private:
86     Foo(Foo &); // not declared
87     Foo& operator =(const Foo&); // not declared
88 
89 private:
90     oslInterlockedCount m_refCount;
91 };
92 
93 class Test: public ::testing::Test
94 {
95 
96 public:
97 };
98 
TEST_F(Test,testUnoSetThrow)99 TEST_F(Test, testUnoSetThrow)
100 {
101     Reference< Interface1 > xNull;
102     Reference< Interface1 > xFoo( new Foo );
103 
104     // ctor taking Reference< interface_type >
105     bool bCaughtException = false;
106     try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
107     ASSERT_EQ( true, bCaughtException );
108 
109     bCaughtException = false;
110     try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
111     ASSERT_EQ( false, bCaughtException );
112 
113     // ctor taking interface_type*
114     bCaughtException = false;
115     try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
116     ASSERT_EQ( true, bCaughtException );
117 
118     bCaughtException = false;
119     try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
120     ASSERT_EQ( false, bCaughtException );
121 
122     Reference< Interface1 > x;
123     // "set" taking Reference< interface_type >
124     bCaughtException = false;
125     try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
126     ASSERT_EQ( true, bCaughtException );
127 
128     bCaughtException = false;
129     try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
130     ASSERT_EQ( false, bCaughtException );
131 
132     // "set" taking interface_type*
133     bCaughtException = false;
134     try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
135     ASSERT_EQ( true, bCaughtException );
136 
137     bCaughtException = false;
138     try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
139     ASSERT_EQ( false, bCaughtException );
140 }
141 
142 }   // namespace
143