1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 // autogenerated file with codegen.pl
23 #include <testshl/simpleheader.hxx>
24 #include <systools/win32/comtools.hxx>
26 class COMObject
: public IUnknown
29 COMObject() : ref_count_(0)
37 ULONG __stdcall
AddRef()
43 ULONG __stdcall
Release()
45 ULONG cnt
= --ref_count_
;
51 HRESULT __stdcall
QueryInterface(REFIID riid
, LPVOID
* ppv
)
53 if (riid
== IID_IUnknown
)
62 ULONG
GetRefCount() const
71 sal::systools::COMReference
<IUnknown
> comObjectSource()
73 return sal::systools::COMReference
<IUnknown
>(new COMObject
);
76 bool comObjectSink(sal::systools::COMReference
<IUnknown
> r
, ULONG expectedRefCountOnReturn
)
78 r
= sal::systools::COMReference
<IUnknown
>();
79 COMObject
* p
= reinterpret_cast<COMObject
*>(r
.get());
81 return (p
->GetRefCount() == expectedRefCountOnReturn
);
83 return (0 == expectedRefCountOnReturn
);
86 void comObjectSource2(LPVOID
* ppv
)
88 COMObject
* p
= new COMObject
;
93 namespace test_comtools
96 class test_COMReference
: public CppUnit::TestFixture
100 /// test of COMReference<IUnknown> r;
103 sal::systools::COMReference
<IUnknown
> r
;
104 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r
.get() == NULL
);
107 void test_ctor_manual_AddRef()
109 COMObject
* p
= new COMObject
;
111 sal::systools::COMReference
<IUnknown
> r(p
, false);
112 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
115 void test_copy_ctor()
117 sal::systools::COMReference
<IUnknown
> r(comObjectSource());
118 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
121 void test_copy_assignment()
123 sal::systools::COMReference
<IUnknown
> r
;
124 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r
.get() == NULL
);
126 r
= comObjectSource();
127 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r
.get() != NULL
);
128 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
131 void test_ref_to_ref_assignment()
133 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
134 sal::systools::COMReference
<IUnknown
> r2
= r1
;
135 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount() == 2);
138 void test_pointer_to_ref_assignment()
140 sal::systools::COMReference
<IUnknown
> r
;
142 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
145 void test_pointer_to_ref_assignment2()
147 sal::systools::COMReference
<IUnknown
> r
= comObjectSource();
149 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
152 void test_source_sink()
154 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
157 void test_address_operator()
159 sal::systools::COMReference
<IUnknown
> r
;
160 comObjectSource2(reinterpret_cast<LPVOID
*>(&r
));
161 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
164 void test_address_operator2()
166 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
167 sal::systools::COMReference
<IUnknown
> r2
= r1
;
168 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount() == 2);
169 comObjectSource2(reinterpret_cast<LPVOID
*>(&r1
));
170 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r1
.get())->GetRefCount() == 1);
171 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount() == 1);
176 sal::systools::COMReference
<IUnknown
> r
= comObjectSource();
177 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject
*>(r
.get())->GetRefCount() == 1);
179 CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r
.is());
182 void test_query_interface()
186 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
187 sal::systools::COMReference
<IUnknown
> r2
= r1
.QueryInterface
<IUnknown
>(IID_IUnknown
);
188 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 2 is expected", reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount() == 2);
190 catch(const sal::systools::ComError
& ex
)
192 CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false);
196 void test_query_interface_throw()
200 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
201 sal::systools::COMReference
<IPersistFile
> r2
= r1
.QueryInterface
<IPersistFile
>(IID_IPersistFile
);
203 catch(const sal::systools::ComError
& ex
)
207 CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false);
210 // Change the following lines only, if you add, remove or rename
211 // member functions of the current class,
212 // because these macros are need by auto register mechanism.
214 CPPUNIT_TEST_SUITE(test_COMReference
);
215 CPPUNIT_TEST(default_ctor
);
216 CPPUNIT_TEST(test_ctor_manual_AddRef
);
217 CPPUNIT_TEST(test_copy_ctor
);
218 CPPUNIT_TEST(test_copy_assignment
);
219 CPPUNIT_TEST(test_ref_to_ref_assignment
);
220 CPPUNIT_TEST(test_pointer_to_ref_assignment
);
221 CPPUNIT_TEST(test_pointer_to_ref_assignment2
);
222 CPPUNIT_TEST(test_source_sink
);
223 CPPUNIT_TEST(test_address_operator
);
224 CPPUNIT_TEST(test_address_operator2
);
225 CPPUNIT_TEST(test_clear
);
226 CPPUNIT_TEST(test_query_interface
);
227 CPPUNIT_TEST(test_query_interface_throw
);
228 CPPUNIT_TEST_SUITE_END();
231 // -----------------------------------------------------------------------------
232 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_comtools::test_COMReference
, "test_comtools");
234 } // namespace rtl_OUString
237 // this macro creates an empty function, which will called by the RegisterAllFunctions()
238 // to let the user the possibility to also register some functions by hand.
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */