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 .
20 #include <cppunit/extensions/HelperMacros.h>
21 #include <cppunit/plugin/TestPlugIn.h>
22 #include <systools/win32/comtools.hxx>
24 class COMObject
: public IUnknown
27 COMObject() : ref_count_(0)
35 ULONG __stdcall
AddRef() override
41 ULONG __stdcall
Release() override
43 ULONG cnt
= --ref_count_
;
49 HRESULT __stdcall
QueryInterface(REFIID riid
, void** ppv
) override
51 if (riid
== IID_IUnknown
)
60 ULONG
GetRefCount() const
69 static sal::systools::COMReference
<IUnknown
> comObjectSource()
71 return sal::systools::COMReference
<IUnknown
>(new COMObject
);
74 static bool comObjectSink(sal::systools::COMReference
<IUnknown
> r
, ULONG expectedRefCountOnReturn
)
76 r
= sal::systools::COMReference
<IUnknown
>();
77 COMObject
* p
= reinterpret_cast<COMObject
*>(r
.get());
79 return (p
->GetRefCount() == expectedRefCountOnReturn
);
81 return (0 == expectedRefCountOnReturn
);
84 static void comObjectSource2(LPVOID
* ppv
)
86 COMObject
* p
= new COMObject
;
91 namespace test_comtools
94 class test_COMReference
: public CppUnit::TestFixture
98 /// test of COMReference<IUnknown> r;
101 sal::systools::COMReference
<IUnknown
> r
;
102 CPPUNIT_ASSERT_EQUAL_MESSAGE("COMReference should be empty", static_cast<IUnknown
*>(nullptr), r
.get());
105 void test_ctor_manual_AddRef()
107 COMObject
* p
= new COMObject
;
109 sal::systools::COMReference
<IUnknown
> r(p
, false);
110 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r
.get())->GetRefCount());
113 void test_copy_ctor()
115 sal::systools::COMReference
<IUnknown
> r(comObjectSource());
116 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r
.get())->GetRefCount());
119 void test_copy_assignment()
121 sal::systools::COMReference
<IUnknown
> r
;
122 CPPUNIT_ASSERT_EQUAL_MESSAGE("COMReference should be empty", static_cast<IUnknown
*>(nullptr), r
.get());
124 r
= comObjectSource();
125 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r
.get() != nullptr);
126 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r
.get())->GetRefCount());
129 void test_ref_to_ref_assignment()
131 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
132 sal::systools::COMReference
<IUnknown
> r2
= r1
;
133 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 2 is expected", ULONG(2), reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount());
136 void test_pointer_to_ref_assignment()
138 sal::systools::COMReference
<IUnknown
> r
;
140 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r
.get())->GetRefCount());
143 void test_pointer_to_ref_assignment2()
145 sal::systools::COMReference
<IUnknown
> r
= comObjectSource();
147 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r
.get())->GetRefCount());
150 void test_source_sink()
152 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
155 void test_address_operator()
157 sal::systools::COMReference
<IUnknown
> r
;
158 comObjectSource2(reinterpret_cast<LPVOID
*>(&r
));
159 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count, 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r
.get())->GetRefCount());
162 void test_address_operator2()
164 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
165 sal::systools::COMReference
<IUnknown
> r2
= r1
;
166 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 2 is expected", ULONG(2), reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount());
167 comObjectSource2(reinterpret_cast<LPVOID
*>(&r1
));
168 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r1
.get())->GetRefCount());
169 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount());
174 sal::systools::COMReference
<IUnknown
> r
= comObjectSource();
175 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject
*>(r
.get())->GetRefCount());
177 CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r
.is());
180 void test_query_interface()
184 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
185 sal::systools::COMReference
<IUnknown
> r2
= r1
.QueryInterface
<IUnknown
>(IID_IUnknown
);
186 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count, 2 is expected", ULONG(2), reinterpret_cast<COMObject
*>(r2
.get())->GetRefCount());
188 catch(const sal::systools::ComError
&)
190 CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false);
194 void test_query_interface_throw()
198 sal::systools::COMReference
<IUnknown
> r1
= comObjectSource();
199 sal::systools::COMReference
<IPersistFile
> r2
= r1
.QueryInterface
<IPersistFile
>(IID_IPersistFile
);
201 catch(const sal::systools::ComError
&)
205 CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false);
208 // Change the following lines only, if you add, remove or rename
209 // member functions of the current class,
210 // because these macros are need by auto register mechanism.
212 CPPUNIT_TEST_SUITE(test_COMReference
);
213 CPPUNIT_TEST(default_ctor
);
214 CPPUNIT_TEST(test_ctor_manual_AddRef
);
215 CPPUNIT_TEST(test_copy_ctor
);
216 CPPUNIT_TEST(test_copy_assignment
);
217 CPPUNIT_TEST(test_ref_to_ref_assignment
);
218 CPPUNIT_TEST(test_pointer_to_ref_assignment
);
219 CPPUNIT_TEST(test_pointer_to_ref_assignment2
);
220 CPPUNIT_TEST(test_source_sink
);
221 CPPUNIT_TEST(test_address_operator
);
222 CPPUNIT_TEST(test_address_operator2
);
223 CPPUNIT_TEST(test_clear
);
224 CPPUNIT_TEST(test_query_interface
);
225 CPPUNIT_TEST(test_query_interface_throw
);
226 CPPUNIT_TEST_SUITE_END();
229 CPPUNIT_TEST_SUITE_REGISTRATION(test_comtools::test_COMReference
);
231 } // namespace rtl_OUString
233 CPPUNIT_PLUGIN_IMPLEMENT();
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */