merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / qa / systools / test_comtools.cxx
blobe72d11ecfb7c8fbbd7bf9d1d9d8696ea1877b010
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_sal.hxx"
31 // autogenerated file with codegen.pl
33 #include <testshl/simpleheader.hxx>
34 #include <systools/win32/comtools.hxx>
36 class COMObject : public IUnknown
38 public:
39 COMObject() : ref_count_(0)
43 ~COMObject()
47 ULONG __stdcall AddRef()
49 ref_count_++;
50 return ref_count_;
53 ULONG __stdcall Release()
55 ULONG cnt = --ref_count_;
56 if (cnt == 0)
57 delete this;
58 return cnt;
61 HRESULT __stdcall QueryInterface(REFIID riid, LPVOID* ppv)
63 if (riid == IID_IUnknown)
65 AddRef();
66 *ppv = this;
67 return S_OK;
69 return E_NOINTERFACE;
72 ULONG GetRefCount() const
74 return ref_count_;
77 private:
78 ULONG ref_count_;
81 sal::systools::COMReference<IUnknown> comObjectSource()
83 return sal::systools::COMReference<IUnknown>(new COMObject);
86 bool comObjectSink(sal::systools::COMReference<IUnknown> r, ULONG expectedRefCountOnReturn)
88 r = sal::systools::COMReference<IUnknown>();
89 COMObject* p = reinterpret_cast<COMObject*>(r.get());
90 if (p)
91 return (p->GetRefCount() == expectedRefCountOnReturn);
92 else
93 return (0 == expectedRefCountOnReturn);
96 void comObjectSource2(LPVOID* ppv)
98 COMObject* p = new COMObject;
99 p->AddRef();
100 *ppv = p;
103 namespace test_comtools
106 class test_COMReference : public CppUnit::TestFixture
109 public:
110 /// test of COMReference<IUnknown> r;
111 void default_ctor()
113 sal::systools::COMReference<IUnknown> r;
114 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
117 void test_ctor_manual_AddRef()
119 COMObject* p = new COMObject;
120 p->AddRef();
121 sal::systools::COMReference<IUnknown> r(p, false);
122 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
125 void test_copy_ctor()
127 sal::systools::COMReference<IUnknown> r(comObjectSource());
128 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
131 void test_copy_assignment()
133 sal::systools::COMReference<IUnknown> r;
134 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
136 r = comObjectSource();
137 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() != NULL);
138 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
141 void test_ref_to_ref_assignment()
143 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
144 sal::systools::COMReference<IUnknown> r2 = r1;
145 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
148 void test_pointer_to_ref_assignment()
150 sal::systools::COMReference<IUnknown> r;
151 r = new COMObject;
152 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
155 void test_pointer_to_ref_assignment2()
157 sal::systools::COMReference<IUnknown> r = comObjectSource();
158 r = new COMObject;
159 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
162 void test_source_sink()
164 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
167 void test_address_operator()
169 sal::systools::COMReference<IUnknown> r;
170 comObjectSource2(reinterpret_cast<LPVOID*>(&r));
171 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
174 void test_address_operator2()
176 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
177 sal::systools::COMReference<IUnknown> r2 = r1;
178 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
179 comObjectSource2(reinterpret_cast<LPVOID*>(&r1));
180 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r1.get())->GetRefCount() == 1);
181 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 1);
184 void test_clear()
186 sal::systools::COMReference<IUnknown> r = comObjectSource();
187 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
188 r.clear();
189 CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r.is());
192 void test_query_interface()
196 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
197 sal::systools::COMReference<IUnknown> r2 = r1.QueryInterface<IUnknown>(IID_IUnknown);
198 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
200 catch(sal::systools::ComError& ex)
202 CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false);
206 void test_query_interface_throw()
210 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
211 sal::systools::COMReference<IPersistFile> r2 = r1.QueryInterface<IPersistFile>(IID_IPersistFile);
213 catch(sal::systools::ComError& ex)
215 return;
217 CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false);
220 // Change the following lines only, if you add, remove or rename
221 // member functions of the current class,
222 // because these macros are need by auto register mechanism.
224 CPPUNIT_TEST_SUITE(test_COMReference);
225 CPPUNIT_TEST(default_ctor);
226 CPPUNIT_TEST(test_ctor_manual_AddRef);
227 CPPUNIT_TEST(test_copy_ctor);
228 CPPUNIT_TEST(test_copy_assignment);
229 CPPUNIT_TEST(test_ref_to_ref_assignment);
230 CPPUNIT_TEST(test_pointer_to_ref_assignment);
231 CPPUNIT_TEST(test_pointer_to_ref_assignment2);
232 CPPUNIT_TEST(test_source_sink);
233 CPPUNIT_TEST(test_address_operator);
234 CPPUNIT_TEST(test_address_operator2);
235 CPPUNIT_TEST(test_clear);
236 CPPUNIT_TEST(test_query_interface);
237 CPPUNIT_TEST(test_query_interface_throw);
238 CPPUNIT_TEST_SUITE_END();
241 // -----------------------------------------------------------------------------
242 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_comtools::test_COMReference, "test_comtools");
244 } // namespace rtl_OUString
247 // this macro creates an empty function, which will called by the RegisterAllFunctions()
248 // to let the user the possibility to also register some functions by hand.
249 NOADDITIONAL;