Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sal / qa / systools / test_comtools.cxx
blob096851c9e989225db689daf2e4a561f367731dee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
26 public:
27 COMObject() : ref_count_(0)
31 virtual ~COMObject()
35 ULONG __stdcall AddRef() override
37 ref_count_++;
38 return ref_count_;
41 ULONG __stdcall Release() override
43 ULONG cnt = --ref_count_;
44 if (cnt == 0)
45 delete this;
46 return cnt;
49 HRESULT __stdcall QueryInterface(REFIID riid, void** ppv) override
51 if (riid == IID_IUnknown)
53 AddRef();
54 *ppv = this;
55 return S_OK;
57 return E_NOINTERFACE;
60 ULONG GetRefCount() const
62 return ref_count_;
65 private:
66 ULONG ref_count_;
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());
78 if (p)
79 return (p->GetRefCount() == expectedRefCountOnReturn);
80 else
81 return (0 == expectedRefCountOnReturn);
84 static void comObjectSource2(LPVOID* ppv)
86 COMObject* p = new COMObject;
87 p->AddRef();
88 *ppv = p;
91 namespace test_comtools
94 class test_COMReference : public CppUnit::TestFixture
97 public:
98 /// test of COMReference<IUnknown> r;
99 void default_ctor()
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;
108 p->AddRef();
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;
139 r = new COMObject;
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();
146 r = new COMObject;
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());
172 void test_clear()
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());
176 r.clear();
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&)
203 return;
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: */