xref: /aoo42x/main/cppu/qa/test_reference.cxx (revision 4ad76906)
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         throw (RuntimeException)
54     {
55         Any aInterface;
56         if (_type == getCppuType< Reference< XInterface > >())
57         {
58             Reference< XInterface > ref( static_cast< XInterface * >( this ) );
59             aInterface.setValue( &ref, _type );
60         }
61         else if (_type == getCppuType< Reference< Interface1 > >())
62         {
63             Reference< Interface1 > ref( this );
64             aInterface.setValue( &ref, _type );
65         }
66 
67         return Any();
68     }
69 
acquire()70     virtual void SAL_CALL acquire() throw ()
71     {
72         osl_incrementInterlockedCount( &m_refCount );
73     }
74 
release()75     virtual void SAL_CALL release() throw ()
76     {
77         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
78             delete this;
79     }
80 
81 protected:
~Foo()82     virtual ~Foo()
83     {
84     }
85 
86 private:
87     Foo(Foo &); // not declared
88     Foo& operator =(const Foo&); // not declared
89 
90 private:
91     oslInterlockedCount m_refCount;
92 };
93 
94 class Test: public ::testing::Test
95 {
96 
97 public:
98 };
99 
TEST_F(Test,testUnoSetThrow)100 TEST_F(Test, testUnoSetThrow)
101 {
102     Reference< Interface1 > xNull;
103     Reference< Interface1 > xFoo( new Foo );
104 
105     // ctor taking Reference< interface_type >
106     bool bCaughtException = false;
107     try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
108     ASSERT_EQ( true, bCaughtException );
109 
110     bCaughtException = false;
111     try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
112     ASSERT_EQ( false, bCaughtException );
113 
114     // ctor taking interface_type*
115     bCaughtException = false;
116     try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
117     ASSERT_EQ( true, bCaughtException );
118 
119     bCaughtException = false;
120     try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
121     ASSERT_EQ( false, bCaughtException );
122 
123     Reference< Interface1 > x;
124     // "set" taking Reference< interface_type >
125     bCaughtException = false;
126     try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
127     ASSERT_EQ( true, bCaughtException );
128 
129     bCaughtException = false;
130     try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
131     ASSERT_EQ( false, bCaughtException );
132 
133     // "set" taking interface_type*
134     bCaughtException = false;
135     try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
136     ASSERT_EQ( true, bCaughtException );
137 
138     bCaughtException = false;
139     try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
140     ASSERT_EQ( false, bCaughtException );
141 }
142 
143 }   // namespace
144