xref: /trunk/main/cppuhelper/qa/weak/test_weak.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
19d7e27acSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39d7e27acSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49d7e27acSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59d7e27acSAndrew Rist  * distributed with this work for additional information
69d7e27acSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79d7e27acSAndrew Rist  * to you under the Apache License, Version 2.0 (the
89d7e27acSAndrew Rist  * "License"); you may not use this file except in compliance
99d7e27acSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
119d7e27acSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
139d7e27acSAndrew Rist  * Unless required by applicable law or agreed to in writing,
149d7e27acSAndrew Rist  * software distributed under the License is distributed on an
159d7e27acSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169d7e27acSAndrew Rist  * KIND, either express or implied.  See the License for the
179d7e27acSAndrew Rist  * specific language governing permissions and limitations
189d7e27acSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
209d7e27acSAndrew Rist  *************************************************************/
219d7e27acSAndrew Rist 
229d7e27acSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sal/config.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "com/sun/star/lang/DisposedException.hpp"
30cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
31cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
32cdf0e10cSrcweir #include "com/sun/star/uno/XAdapter.hpp"
33cdf0e10cSrcweir #include "com/sun/star/uno/XReference.hpp"
34cdf0e10cSrcweir #include "com/sun/star/uno/XWeak.hpp"
35cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
36cdf0e10cSrcweir #include "cppuhelper/weak.hxx"
37cdf0e10cSrcweir #include "rtl/ref.hxx"
38cdf0e10cSrcweir #include "sal/types.h"
39*5b75f11eSDamjan Jovanovic #include "gtest/gtest.h"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace css = com::sun::star;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class Reference: public cppu::WeakImplHelper1< css::uno::XReference > {
46cdf0e10cSrcweir public:
Reference()47cdf0e10cSrcweir     Reference(): m_disposed(false) {}
48cdf0e10cSrcweir 
dispose()49cdf0e10cSrcweir     virtual void SAL_CALL dispose() throw (css::uno::RuntimeException) {
50cdf0e10cSrcweir         m_disposed = true;
51cdf0e10cSrcweir         handleDispose();
52cdf0e10cSrcweir     }
53cdf0e10cSrcweir 
isDisposed() const54cdf0e10cSrcweir     bool isDisposed() const { return m_disposed; }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir protected:
handleDispose()57cdf0e10cSrcweir     virtual void handleDispose() {};
58cdf0e10cSrcweir 
59cdf0e10cSrcweir private:
60cdf0e10cSrcweir     bool m_disposed;
61cdf0e10cSrcweir };
62cdf0e10cSrcweir 
63cdf0e10cSrcweir class RuntimeExceptionReference: public Reference {
64cdf0e10cSrcweir protected:
handleDispose()65cdf0e10cSrcweir     virtual void handleDispose() {
66cdf0e10cSrcweir         throw css::uno::RuntimeException();
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir };
69cdf0e10cSrcweir 
70cdf0e10cSrcweir class DisposedExceptionReference: public Reference {
71cdf0e10cSrcweir protected:
handleDispose()72cdf0e10cSrcweir     virtual void handleDispose() {
73cdf0e10cSrcweir         throw css::lang::DisposedException();
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir };
76cdf0e10cSrcweir 
77*5b75f11eSDamjan Jovanovic class Test: public ::testing::Test {
78cdf0e10cSrcweir public:
79cdf0e10cSrcweir };
80cdf0e10cSrcweir 
TEST_F(Test,testReferenceDispose)81*5b75f11eSDamjan Jovanovic TEST_F(Test, testReferenceDispose) {
82cdf0e10cSrcweir     css::uno::Reference< css::uno::XWeak > w(new ::cppu::OWeakObject);
83cdf0e10cSrcweir     css::uno::Reference< css::uno::XAdapter > a(w->queryAdapter());
84cdf0e10cSrcweir     ::rtl::Reference< Reference > r1(new RuntimeExceptionReference);
85cdf0e10cSrcweir     ::rtl::Reference< Reference > r2(new Reference);
86cdf0e10cSrcweir     ::rtl::Reference< Reference > r3(new DisposedExceptionReference);
87cdf0e10cSrcweir     a->addReference(r1.get());
88cdf0e10cSrcweir     a->addReference(r2.get());
89cdf0e10cSrcweir     a->addReference(r3.get());
90cdf0e10cSrcweir     w.clear();
91*5b75f11eSDamjan Jovanovic     ASSERT_TRUE(r1->isDisposed());
92*5b75f11eSDamjan Jovanovic     ASSERT_TRUE(r2->isDisposed());
93*5b75f11eSDamjan Jovanovic     ASSERT_TRUE(r3->isDisposed());
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir }
97