Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / cppu / qa / test_reference.cxx
blobb998238f6b393602ef65bbcd22b7ae66fe425f5e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <cppunit/TestSuite.h>
23 #include <cppunit/TestFixture.h>
24 #include <cppunit/TestCase.h>
25 #include <cppunit/plugin/TestPlugIn.h>
26 #include <cppunit/extensions/HelperMacros.h>
28 #include "Interface1.hpp"
30 #include "rtl/ustring.hxx"
31 #include "sal/types.h"
33 namespace
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
44 public:
45 Foo()
46 :m_refCount(0)
50 virtual Any SAL_CALL queryInterface(const Type & _type)
51 throw (RuntimeException)
53 Any aInterface;
54 if (_type == getCppuType< Reference< XInterface > >())
56 Reference< XInterface > ref( static_cast< XInterface * >( this ) );
57 aInterface.setValue( &ref, _type );
59 else if (_type == getCppuType< Reference< Interface1 > >())
61 Reference< Interface1 > ref( this );
62 aInterface.setValue( &ref, _type );
65 return Any();
68 virtual void SAL_CALL acquire() throw ()
70 osl_atomic_increment( &m_refCount );
73 virtual void SAL_CALL release() throw ()
75 if ( 0 == osl_atomic_decrement( &m_refCount ) )
76 delete this;
79 protected:
80 virtual ~Foo()
84 private:
85 Foo(Foo &); // not declared
86 Foo& operator =(const Foo&); // not declared
88 private:
89 oslInterlockedCount m_refCount;
92 class Test: public ::CppUnit::TestFixture
95 public:
96 void testUnoSetThrow();
98 CPPUNIT_TEST_SUITE(Test);
99 CPPUNIT_TEST(testUnoSetThrow);
100 CPPUNIT_TEST_SUITE_END();
103 void Test::testUnoSetThrow()
105 Reference< Interface1 > xNull;
106 Reference< Interface1 > xFoo( new Foo );
108 // ctor taking Reference< interface_type >
109 bool bCaughtException = false;
110 try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
111 CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
113 bCaughtException = false;
114 try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
115 CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
117 // ctor taking interface_type*
118 bCaughtException = false;
119 try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
120 CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
122 bCaughtException = false;
123 try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
124 CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
126 Reference< Interface1 > x;
127 // "set" taking Reference< interface_type >
128 bCaughtException = false;
129 try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
130 CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
132 bCaughtException = false;
133 try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
134 CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
136 // "set" taking interface_type*
137 bCaughtException = false;
138 try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
139 CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
141 bCaughtException = false;
142 try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
143 CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
146 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
148 } // namespace
150 CPPUNIT_PLUGIN_IMPLEMENT();
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */