update dev300-m58
[ooovba.git] / sal / qa / systools / test_comtools.cxx
blobf79b009961ea2dd54f5dfcbe4bc68d65ad814644
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_comtools.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
34 // autogenerated file with codegen.pl
36 #include <cppunit/simpleheader.hxx>
37 #include <systools/win32/comtools.hxx>
39 class COMObject : public IUnknown
41 public:
42 COMObject() : ref_count_(0)
46 ~COMObject()
50 ULONG __stdcall AddRef()
52 ref_count_++;
53 return ref_count_;
56 ULONG __stdcall Release()
58 ULONG cnt = --ref_count_;
59 if (cnt == 0)
60 delete this;
61 return cnt;
64 HRESULT __stdcall QueryInterface(REFIID riid, LPVOID* ppv)
66 if (riid == IID_IUnknown)
68 AddRef();
69 *ppv = this;
70 return S_OK;
72 return E_NOINTERFACE;
75 ULONG GetRefCount() const
77 return ref_count_;
80 private:
81 ULONG ref_count_;
84 sal::systools::COMReference<IUnknown> comObjectSource()
86 return sal::systools::COMReference<IUnknown>(new COMObject);
89 bool comObjectSink(sal::systools::COMReference<IUnknown> r, ULONG expectedRefCountOnReturn)
91 r = sal::systools::COMReference<IUnknown>();
92 COMObject* p = reinterpret_cast<COMObject*>(r.get());
93 if (p)
94 return (p->GetRefCount() == expectedRefCountOnReturn);
95 else
96 return (0 == expectedRefCountOnReturn);
99 void comObjectSource2(LPVOID* ppv)
101 COMObject* p = new COMObject;
102 p->AddRef();
103 *ppv = p;
106 namespace test_comtools
109 class test_COMReference : public CppUnit::TestFixture
112 public:
113 /// test of COMReference<IUnknown> r;
114 void default_ctor()
116 sal::systools::COMReference<IUnknown> r;
117 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
120 void test_ctor_manual_AddRef()
122 COMObject* p = new COMObject;
123 p->AddRef();
124 sal::systools::COMReference<IUnknown> r(p, false);
125 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
128 void test_copy_ctor()
130 sal::systools::COMReference<IUnknown> r(comObjectSource());
131 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
134 void test_copy_assignment()
136 sal::systools::COMReference<IUnknown> r;
137 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
139 r = comObjectSource();
140 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() != NULL);
141 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
144 void test_ref_to_ref_assignment()
146 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
147 sal::systools::COMReference<IUnknown> r2 = r1;
148 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
151 void test_pointer_to_ref_assignment()
153 sal::systools::COMReference<IUnknown> r;
154 r = new COMObject;
155 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
158 void test_pointer_to_ref_assignment2()
160 sal::systools::COMReference<IUnknown> r = comObjectSource();
161 r = new COMObject;
162 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
165 void test_source_sink()
167 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
170 void test_address_operator()
172 sal::systools::COMReference<IUnknown> r;
173 comObjectSource2(reinterpret_cast<LPVOID*>(&r));
174 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
177 void test_address_operator2()
179 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
180 sal::systools::COMReference<IUnknown> r2 = r1;
181 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
182 comObjectSource2(reinterpret_cast<LPVOID*>(&r1));
183 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r1.get())->GetRefCount() == 1);
184 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 1);
187 void test_clear()
189 sal::systools::COMReference<IUnknown> r = comObjectSource();
190 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
191 r.clear();
192 CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r.is());
195 void test_query_interface()
199 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
200 sal::systools::COMReference<IUnknown> r2 = r1.QueryInterface<IUnknown>(IID_IUnknown);
201 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
203 catch(sal::systools::ComError& ex)
205 CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false);
209 void test_query_interface_throw()
213 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
214 sal::systools::COMReference<IPersistFile> r2 = r1.QueryInterface<IPersistFile>(IID_IPersistFile);
216 catch(sal::systools::ComError& ex)
218 return;
220 CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false);
223 // Change the following lines only, if you add, remove or rename
224 // member functions of the current class,
225 // because these macros are need by auto register mechanism.
227 CPPUNIT_TEST_SUITE(test_COMReference);
228 CPPUNIT_TEST(default_ctor);
229 CPPUNIT_TEST(test_ctor_manual_AddRef);
230 CPPUNIT_TEST(test_copy_ctor);
231 CPPUNIT_TEST(test_copy_assignment);
232 CPPUNIT_TEST(test_ref_to_ref_assignment);
233 CPPUNIT_TEST(test_pointer_to_ref_assignment);
234 CPPUNIT_TEST(test_pointer_to_ref_assignment2);
235 CPPUNIT_TEST(test_source_sink);
236 CPPUNIT_TEST(test_address_operator);
237 CPPUNIT_TEST(test_address_operator2);
238 CPPUNIT_TEST(test_clear);
239 CPPUNIT_TEST(test_query_interface);
240 CPPUNIT_TEST(test_query_interface_throw);
241 CPPUNIT_TEST_SUITE_END();
244 // -----------------------------------------------------------------------------
245 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_comtools::test_COMReference, "test_comtools");
247 } // namespace rtl_OUString
250 // this macro creates an empty function, which will called by the RegisterAllFunctions()
251 // to let the user the possibility to also register some functions by hand.
252 NOADDITIONAL;