bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / systools / test_comtools.cxx
blobd4d4db492403520f495c6b2baebf3736816f8ec4
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 // autogenerated file with codegen.pl
22 #include <testshl/simpleheader.hxx>
23 #include <systools/win32/comtools.hxx>
25 class COMObject : public IUnknown
27 public:
28 COMObject() : ref_count_(0)
32 ~COMObject()
36 ULONG __stdcall AddRef()
38 ref_count_++;
39 return ref_count_;
42 ULONG __stdcall Release()
44 ULONG cnt = --ref_count_;
45 if (cnt == 0)
46 delete this;
47 return cnt;
50 HRESULT __stdcall QueryInterface(REFIID riid, LPVOID* ppv)
52 if (riid == IID_IUnknown)
54 AddRef();
55 *ppv = this;
56 return S_OK;
58 return E_NOINTERFACE;
61 ULONG GetRefCount() const
63 return ref_count_;
66 private:
67 ULONG ref_count_;
70 sal::systools::COMReference<IUnknown> comObjectSource()
72 return sal::systools::COMReference<IUnknown>(new COMObject);
75 bool comObjectSink(sal::systools::COMReference<IUnknown> r, ULONG expectedRefCountOnReturn)
77 r = sal::systools::COMReference<IUnknown>();
78 COMObject* p = reinterpret_cast<COMObject*>(r.get());
79 if (p)
80 return (p->GetRefCount() == expectedRefCountOnReturn);
81 else
82 return (0 == expectedRefCountOnReturn);
85 void comObjectSource2(LPVOID* ppv)
87 COMObject* p = new COMObject;
88 p->AddRef();
89 *ppv = p;
92 namespace test_comtools
95 class test_COMReference : public CppUnit::TestFixture
98 public:
99 /// test of COMReference<IUnknown> r;
100 void default_ctor()
102 sal::systools::COMReference<IUnknown> r;
103 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
106 void test_ctor_manual_AddRef()
108 COMObject* p = new COMObject;
109 p->AddRef();
110 sal::systools::COMReference<IUnknown> r(p, false);
111 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
114 void test_copy_ctor()
116 sal::systools::COMReference<IUnknown> r(comObjectSource());
117 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
120 void test_copy_assignment()
122 sal::systools::COMReference<IUnknown> r;
123 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
125 r = comObjectSource();
126 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() != NULL);
127 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
130 void test_ref_to_ref_assignment()
132 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
133 sal::systools::COMReference<IUnknown> r2 = r1;
134 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
137 void test_pointer_to_ref_assignment()
139 sal::systools::COMReference<IUnknown> r;
140 r = new COMObject;
141 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
144 void test_pointer_to_ref_assignment2()
146 sal::systools::COMReference<IUnknown> r = comObjectSource();
147 r = new COMObject;
148 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
151 void test_source_sink()
153 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
156 void test_address_operator()
158 sal::systools::COMReference<IUnknown> r;
159 comObjectSource2(reinterpret_cast<LPVOID*>(&r));
160 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
163 void test_address_operator2()
165 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
166 sal::systools::COMReference<IUnknown> r2 = r1;
167 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
168 comObjectSource2(reinterpret_cast<LPVOID*>(&r1));
169 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r1.get())->GetRefCount() == 1);
170 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 1);
173 void test_clear()
175 sal::systools::COMReference<IUnknown> r = comObjectSource();
176 CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
177 r.clear();
178 CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r.is());
181 void test_query_interface()
185 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
186 sal::systools::COMReference<IUnknown> r2 = r1.QueryInterface<IUnknown>(IID_IUnknown);
187 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
189 catch(const sal::systools::ComError& ex)
191 CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false);
195 void test_query_interface_throw()
199 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
200 sal::systools::COMReference<IPersistFile> r2 = r1.QueryInterface<IPersistFile>(IID_IPersistFile);
202 catch(const sal::systools::ComError& ex)
204 return;
206 CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false);
209 // Change the following lines only, if you add, remove or rename
210 // member functions of the current class,
211 // because these macros are need by auto register mechanism.
213 CPPUNIT_TEST_SUITE(test_COMReference);
214 CPPUNIT_TEST(default_ctor);
215 CPPUNIT_TEST(test_ctor_manual_AddRef);
216 CPPUNIT_TEST(test_copy_ctor);
217 CPPUNIT_TEST(test_copy_assignment);
218 CPPUNIT_TEST(test_ref_to_ref_assignment);
219 CPPUNIT_TEST(test_pointer_to_ref_assignment);
220 CPPUNIT_TEST(test_pointer_to_ref_assignment2);
221 CPPUNIT_TEST(test_source_sink);
222 CPPUNIT_TEST(test_address_operator);
223 CPPUNIT_TEST(test_address_operator2);
224 CPPUNIT_TEST(test_clear);
225 CPPUNIT_TEST(test_query_interface);
226 CPPUNIT_TEST(test_query_interface_throw);
227 CPPUNIT_TEST_SUITE_END();
230 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_comtools::test_COMReference, "test_comtools");
232 } // namespace rtl_OUString
234 // this macro creates an empty function, which will called by the RegisterAllFunctions()
235 // to let the user the possibility to also register some functions by hand.
236 NOADDITIONAL;
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */