1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 // autogenerated file with codegen.pl
32 #include <testshl/simpleheader.hxx>
33 #include <systools/win32/comtools.hxx>
35 class COMObject
: public IUnknown
38 COMObject() : ref_count_(0)
46 ULONG __stdcall
AddRef()
52 ULONG __stdcall
Release()
54 ULONG cnt
= --ref_count_
;
60 HRESULT __stdcall
QueryInterface(REFIID riid
, LPVOID
* ppv
)
62 if (riid
== IID_IUnknown
)
71 ULONG
GetRefCount() const
80 sal::systools::COMReference
<IUnknown
> comObjectSource()
82 return sal::systools::COMReference
<IUnknown
>(new COMObject
);
85 bool comObjectSink(sal::systools::COMReference
<IUnknown
> r
, ULONG expectedRefCountOnReturn
)
87 r
= sal::systools::COMReference
<IUnknown
>();
88 COMObject
* p
= reinterpret_cast<COMObject
*>(r
.get());
90 return (p
->GetRefCount() == expectedRefCountOnReturn
);
92 return (0 == expectedRefCountOnReturn
);
95 void comObjectSource2(LPVOID
* ppv
)
97 COMObject
* p
= new COMObject
;
102 namespace test_comtools
105 class test_COMReference
: public CppUnit::TestFixture
109 /// test of COMReference<IUnknown> r;
112 sal::systools::COMReference
<IUnknown
> r
;
113 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r
.get() == NULL
);
116 void test_ctor_manual_AddRef()
118 COMObject
* p
= new COMObject
;
120 sal::systools::COMReference
<IUnknown
> r(p
, false);
121 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
124 void test_copy_ctor()
126 sal::systools::COMReference
<IUnknown
> r(comObjectSource());
127 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
130 void test_copy_assignment()
132 sal::systools::COMReference
<IUnknown
> r
;
133 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r
.get() == NULL
);
135 r
= comObjectSource();
136 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r
.get() != NULL
);
137 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
140 void test_ref_to_ref_assignment()
142 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
143 sal::systools::COMReference
<IUnknown
> r2
= r1
;
144 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount() == 2);
147 void test_pointer_to_ref_assignment()
149 sal::systools::COMReference
<IUnknown
> r
;
151 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
154 void test_pointer_to_ref_assignment2()
156 sal::systools::COMReference
<IUnknown
> r
= comObjectSource();
158 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
161 void test_source_sink()
163 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
166 void test_address_operator()
168 sal::systools::COMReference
<IUnknown
> r
;
169 comObjectSource2(reinterpret_cast<LPVOID
*>(&r
));
170 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
173 void test_address_operator2()
175 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
176 sal::systools::COMReference
<IUnknown
> r2
= r1
;
177 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount() == 2);
178 comObjectSource2(reinterpret_cast<LPVOID
*>(&r1
));
179 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r1
.get())->GetRefCount() == 1);
180 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount() == 1);
185 sal::systools::COMReference
<IUnknown
> r
= comObjectSource();
186 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
188 CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r
.is());
191 void test_query_interface()
195 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
196 sal::systools::COMReference
<IUnknown
> r2
= r1
.QueryInterface
<IUnknown
>(IID_IUnknown
);
197 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 2 is expected", reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount() == 2);
199 catch(const sal::systools::ComError
& ex
)
201 CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false);
205 void test_query_interface_throw()
209 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
210 sal::systools::COMReference
<IPersistFile
> r2
= r1
.QueryInterface
<IPersistFile
>(IID_IPersistFile
);
212 catch(const sal::systools::ComError
& ex
)
216 CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false);
219 // Change the following lines only, if you add, remove or rename
220 // member functions of the current class,
221 // because these macros are need by auto register mechanism.
223 CPPUNIT_TEST_SUITE(test_COMReference
);
224 CPPUNIT_TEST(default_ctor
);
225 CPPUNIT_TEST(test_ctor_manual_AddRef
);
226 CPPUNIT_TEST(test_copy_ctor
);
227 CPPUNIT_TEST(test_copy_assignment
);
228 CPPUNIT_TEST(test_ref_to_ref_assignment
);
229 CPPUNIT_TEST(test_pointer_to_ref_assignment
);
230 CPPUNIT_TEST(test_pointer_to_ref_assignment2
);
231 CPPUNIT_TEST(test_source_sink
);
232 CPPUNIT_TEST(test_address_operator
);
233 CPPUNIT_TEST(test_address_operator2
);
234 CPPUNIT_TEST(test_clear
);
235 CPPUNIT_TEST(test_query_interface
);
236 CPPUNIT_TEST(test_query_interface_throw
);
237 CPPUNIT_TEST_SUITE_END();
240 // -----------------------------------------------------------------------------
241 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_comtools::test_COMReference
, "test_comtools");
243 } // namespace rtl_OUString
246 // this macro creates an empty function, which will called by the RegisterAllFunctions()
247 // to let the user the possibility to also register some functions by hand.
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */