xref: /trunk/main/cppuhelper/qa/weak/test_weak.cxx (revision 5b75f11e)
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_cppuhelper.hxx"
26 
27 #include "sal/config.h"
28 
29 #include "com/sun/star/lang/DisposedException.hpp"
30 #include "com/sun/star/uno/Reference.hxx"
31 #include "com/sun/star/uno/RuntimeException.hpp"
32 #include "com/sun/star/uno/XAdapter.hpp"
33 #include "com/sun/star/uno/XReference.hpp"
34 #include "com/sun/star/uno/XWeak.hpp"
35 #include "cppuhelper/implbase1.hxx"
36 #include "cppuhelper/weak.hxx"
37 #include "rtl/ref.hxx"
38 #include "sal/types.h"
39 #include "gtest/gtest.h"
40 
41 namespace {
42 
43 namespace css = com::sun::star;
44 
45 class Reference: public cppu::WeakImplHelper1< css::uno::XReference > {
46 public:
Reference()47     Reference(): m_disposed(false) {}
48 
dispose()49     virtual void SAL_CALL dispose() throw (css::uno::RuntimeException) {
50         m_disposed = true;
51         handleDispose();
52     }
53 
isDisposed() const54     bool isDisposed() const { return m_disposed; }
55 
56 protected:
handleDispose()57     virtual void handleDispose() {};
58 
59 private:
60     bool m_disposed;
61 };
62 
63 class RuntimeExceptionReference: public Reference {
64 protected:
handleDispose()65     virtual void handleDispose() {
66         throw css::uno::RuntimeException();
67     }
68 };
69 
70 class DisposedExceptionReference: public Reference {
71 protected:
handleDispose()72     virtual void handleDispose() {
73         throw css::lang::DisposedException();
74     }
75 };
76 
77 class Test: public ::testing::Test {
78 public:
79 };
80 
TEST_F(Test,testReferenceDispose)81 TEST_F(Test, testReferenceDispose) {
82     css::uno::Reference< css::uno::XWeak > w(new ::cppu::OWeakObject);
83     css::uno::Reference< css::uno::XAdapter > a(w->queryAdapter());
84     ::rtl::Reference< Reference > r1(new RuntimeExceptionReference);
85     ::rtl::Reference< Reference > r2(new Reference);
86     ::rtl::Reference< Reference > r3(new DisposedExceptionReference);
87     a->addReference(r1.get());
88     a->addReference(r2.get());
89     a->addReference(r3.get());
90     w.clear();
91     ASSERT_TRUE(r1->isDisposed());
92     ASSERT_TRUE(r2->isDisposed());
93     ASSERT_TRUE(r3->isDisposed());
94 }
95 
96 }
97 
98