Bump for 3.6-28
[LibreOffice.git] / sal / qa / systools / test_comtools.cxx
blob982e5000d9b0414b44e1f1a758a93937f75e81e5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 // autogenerated file with codegen.pl
32 #include <testshl/simpleheader.hxx>
33 #include <systools/win32/comtools.hxx>
35 class COMObject : public IUnknown
37 public:
38 COMObject() : ref_count_(0)
42 ~COMObject()
46 ULONG __stdcall AddRef()
48 ref_count_++;
49 return ref_count_;
52 ULONG __stdcall Release()
54 ULONG cnt = --ref_count_;
55 if (cnt == 0)
56 delete this;
57 return cnt;
60 HRESULT __stdcall QueryInterface(REFIID riid, LPVOID* ppv)
62 if (riid == IID_IUnknown)
64 AddRef();
65 *ppv = this;
66 return S_OK;
68 return E_NOINTERFACE;
71 ULONG GetRefCount() const
73 return ref_count_;
76 private:
77 ULONG ref_count_;
80 sal::systools::COMReference<IUnknown> comObjectSource()
82 return sal::systools::COMReference<IUnknown>(new COMObject);
85 bool comObjectSink(sal::systools::COMReference<IUnknown> r, ULONG expectedRefCountOnReturn)
87 r = sal::systools::COMReference<IUnknown>();
88 COMObject* p = reinterpret_cast<COMObject*>(r.get());
89 if (p)
90 return (p->GetRefCount() == expectedRefCountOnReturn);
91 else
92 return (0 == expectedRefCountOnReturn);
95 void comObjectSource2(LPVOID* ppv)
97 COMObject* p = new COMObject;
98 p->AddRef();
99 *ppv = p;
102 namespace test_comtools
105 class test_COMReference : public CppUnit::TestFixture
108 public:
109 /// test of COMReference<IUnknown> r;
110 void default_ctor()
112 sal::systools::COMReference<IUnknown> r;
113 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
116 void test_ctor_manual_AddRef()
118 COMObject* p = new COMObject;
119 p->AddRef();
120 sal::systools::COMReference<IUnknown> r(p, false);
121 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
124 void test_copy_ctor()
126 sal::systools::COMReference<IUnknown> r(comObjectSource());
127 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
130 void test_copy_assignment()
132 sal::systools::COMReference<IUnknown> r;
133 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
135 r = comObjectSource();
136 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() != NULL);
137 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
140 void test_ref_to_ref_assignment()
142 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
143 sal::systools::COMReference<IUnknown> r2 = r1;
144 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
147 void test_pointer_to_ref_assignment()
149 sal::systools::COMReference<IUnknown> r;
150 r = new COMObject;
151 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
154 void test_pointer_to_ref_assignment2()
156 sal::systools::COMReference<IUnknown> r = comObjectSource();
157 r = new COMObject;
158 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
161 void test_source_sink()
163 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
166 void test_address_operator()
168 sal::systools::COMReference<IUnknown> r;
169 comObjectSource2(reinterpret_cast<LPVOID*>(&r));
170 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
173 void test_address_operator2()
175 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
176 sal::systools::COMReference<IUnknown> r2 = r1;
177 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
178 comObjectSource2(reinterpret_cast<LPVOID*>(&r1));
179 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r1.get())->GetRefCount() == 1);
180 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 1);
183 void test_clear()
185 sal::systools::COMReference<IUnknown> r = comObjectSource();
186 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
187 r.clear();
188 CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r.is());
191 void test_query_interface()
195 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
196 sal::systools::COMReference<IUnknown> r2 = r1.QueryInterface<IUnknown>(IID_IUnknown);
197 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
199 catch(const sal::systools::ComError& ex)
201 CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false);
205 void test_query_interface_throw()
209 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
210 sal::systools::COMReference<IPersistFile> r2 = r1.QueryInterface<IPersistFile>(IID_IPersistFile);
212 catch(const sal::systools::ComError& ex)
214 return;
216 CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false);
219 // Change the following lines only, if you add, remove or rename
220 // member functions of the current class,
221 // because these macros are need by auto register mechanism.
223 CPPUNIT_TEST_SUITE(test_COMReference);
224 CPPUNIT_TEST(default_ctor);
225 CPPUNIT_TEST(test_ctor_manual_AddRef);
226 CPPUNIT_TEST(test_copy_ctor);
227 CPPUNIT_TEST(test_copy_assignment);
228 CPPUNIT_TEST(test_ref_to_ref_assignment);
229 CPPUNIT_TEST(test_pointer_to_ref_assignment);
230 CPPUNIT_TEST(test_pointer_to_ref_assignment2);
231 CPPUNIT_TEST(test_source_sink);
232 CPPUNIT_TEST(test_address_operator);
233 CPPUNIT_TEST(test_address_operator2);
234 CPPUNIT_TEST(test_clear);
235 CPPUNIT_TEST(test_query_interface);
236 CPPUNIT_TEST(test_query_interface_throw);
237 CPPUNIT_TEST_SUITE_END();
240 // -----------------------------------------------------------------------------
241 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_comtools::test_COMReference, "test_comtools");
243 } // namespace rtl_OUString
246 // this macro creates an empty function, which will called by the RegisterAllFunctions()
247 // to let the user the possibility to also register some functions by hand.
248 NOADDITIONAL;
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */