1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*87d2adbcSAndrew Rist * distributed with this work for additional information
6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10*87d2adbcSAndrew Rist *
11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist *
13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
17*87d2adbcSAndrew Rist * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist * under the License.
19*87d2adbcSAndrew Rist *
20*87d2adbcSAndrew Rist *************************************************************/
21*87d2adbcSAndrew Rist
22*87d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_sal.hxx"
27cdf0e10cSrcweir // autogenerated file with codegen.pl
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <testshl/simpleheader.hxx>
30cdf0e10cSrcweir #include <systools/win32/comtools.hxx>
31cdf0e10cSrcweir
32cdf0e10cSrcweir class COMObject : public IUnknown
33cdf0e10cSrcweir {
34cdf0e10cSrcweir public:
COMObject()35cdf0e10cSrcweir COMObject() : ref_count_(0)
36cdf0e10cSrcweir {
37cdf0e10cSrcweir }
38cdf0e10cSrcweir
~COMObject()39cdf0e10cSrcweir ~COMObject()
40cdf0e10cSrcweir {
41cdf0e10cSrcweir }
42cdf0e10cSrcweir
AddRef()43cdf0e10cSrcweir ULONG __stdcall AddRef()
44cdf0e10cSrcweir {
45cdf0e10cSrcweir ref_count_++;
46cdf0e10cSrcweir return ref_count_;
47cdf0e10cSrcweir }
48cdf0e10cSrcweir
Release()49cdf0e10cSrcweir ULONG __stdcall Release()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir ULONG cnt = --ref_count_;
52cdf0e10cSrcweir if (cnt == 0)
53cdf0e10cSrcweir delete this;
54cdf0e10cSrcweir return cnt;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
QueryInterface(REFIID riid,LPVOID * ppv)57cdf0e10cSrcweir HRESULT __stdcall QueryInterface(REFIID riid, LPVOID* ppv)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir if (riid == IID_IUnknown)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir AddRef();
62cdf0e10cSrcweir *ppv = this;
63cdf0e10cSrcweir return S_OK;
64cdf0e10cSrcweir }
65cdf0e10cSrcweir return E_NOINTERFACE;
66cdf0e10cSrcweir }
67cdf0e10cSrcweir
GetRefCount() const68cdf0e10cSrcweir ULONG GetRefCount() const
69cdf0e10cSrcweir {
70cdf0e10cSrcweir return ref_count_;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir
73cdf0e10cSrcweir private:
74cdf0e10cSrcweir ULONG ref_count_;
75cdf0e10cSrcweir };
76cdf0e10cSrcweir
comObjectSource()77cdf0e10cSrcweir sal::systools::COMReference<IUnknown> comObjectSource()
78cdf0e10cSrcweir {
79cdf0e10cSrcweir return sal::systools::COMReference<IUnknown>(new COMObject);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
comObjectSink(sal::systools::COMReference<IUnknown> r,ULONG expectedRefCountOnReturn)82cdf0e10cSrcweir bool comObjectSink(sal::systools::COMReference<IUnknown> r, ULONG expectedRefCountOnReturn)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir r = sal::systools::COMReference<IUnknown>();
85cdf0e10cSrcweir COMObject* p = reinterpret_cast<COMObject*>(r.get());
86cdf0e10cSrcweir if (p)
87cdf0e10cSrcweir return (p->GetRefCount() == expectedRefCountOnReturn);
88cdf0e10cSrcweir else
89cdf0e10cSrcweir return (0 == expectedRefCountOnReturn);
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
comObjectSource2(LPVOID * ppv)92cdf0e10cSrcweir void comObjectSource2(LPVOID* ppv)
93cdf0e10cSrcweir {
94cdf0e10cSrcweir COMObject* p = new COMObject;
95cdf0e10cSrcweir p->AddRef();
96cdf0e10cSrcweir *ppv = p;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir
99cdf0e10cSrcweir namespace test_comtools
100cdf0e10cSrcweir {
101cdf0e10cSrcweir
102cdf0e10cSrcweir class test_COMReference : public CppUnit::TestFixture
103cdf0e10cSrcweir {
104cdf0e10cSrcweir
105cdf0e10cSrcweir public:
106cdf0e10cSrcweir /// test of COMReference<IUnknown> r;
default_ctor()107cdf0e10cSrcweir void default_ctor()
108cdf0e10cSrcweir {
109cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r;
110cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
test_ctor_manual_AddRef()113cdf0e10cSrcweir void test_ctor_manual_AddRef()
114cdf0e10cSrcweir {
115cdf0e10cSrcweir COMObject* p = new COMObject;
116cdf0e10cSrcweir p->AddRef();
117cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r(p, false);
118cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
test_copy_ctor()121cdf0e10cSrcweir void test_copy_ctor()
122cdf0e10cSrcweir {
123cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r(comObjectSource());
124cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
test_copy_assignment()127cdf0e10cSrcweir void test_copy_assignment()
128cdf0e10cSrcweir {
129cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r;
130cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL);
131cdf0e10cSrcweir
132cdf0e10cSrcweir r = comObjectSource();
133cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() != NULL);
134cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
test_ref_to_ref_assignment()137cdf0e10cSrcweir void test_ref_to_ref_assignment()
138cdf0e10cSrcweir {
139cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r1 = comObjectSource();
140cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r2 = r1;
141cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
142cdf0e10cSrcweir }
143cdf0e10cSrcweir
test_pointer_to_ref_assignment()144cdf0e10cSrcweir void test_pointer_to_ref_assignment()
145cdf0e10cSrcweir {
146cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r;
147cdf0e10cSrcweir r = new COMObject;
148cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
149cdf0e10cSrcweir }
150cdf0e10cSrcweir
test_pointer_to_ref_assignment2()151cdf0e10cSrcweir void test_pointer_to_ref_assignment2()
152cdf0e10cSrcweir {
153cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r = comObjectSource();
154cdf0e10cSrcweir r = new COMObject;
155cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
156cdf0e10cSrcweir }
157cdf0e10cSrcweir
test_source_sink()158cdf0e10cSrcweir void test_source_sink()
159cdf0e10cSrcweir {
160cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
161cdf0e10cSrcweir }
162cdf0e10cSrcweir
test_address_operator()163cdf0e10cSrcweir void test_address_operator()
164cdf0e10cSrcweir {
165cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r;
166cdf0e10cSrcweir comObjectSource2(reinterpret_cast<LPVOID*>(&r));
167cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
168cdf0e10cSrcweir }
169cdf0e10cSrcweir
test_address_operator2()170cdf0e10cSrcweir void test_address_operator2()
171cdf0e10cSrcweir {
172cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r1 = comObjectSource();
173cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r2 = r1;
174cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
175cdf0e10cSrcweir comObjectSource2(reinterpret_cast<LPVOID*>(&r1));
176cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r1.get())->GetRefCount() == 1);
177cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 1);
178cdf0e10cSrcweir }
179cdf0e10cSrcweir
test_clear()180cdf0e10cSrcweir void test_clear()
181cdf0e10cSrcweir {
182cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r = comObjectSource();
183cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1);
184cdf0e10cSrcweir r.clear();
185cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r.is());
186cdf0e10cSrcweir }
187cdf0e10cSrcweir
test_query_interface()188cdf0e10cSrcweir void test_query_interface()
189cdf0e10cSrcweir {
190cdf0e10cSrcweir try
191cdf0e10cSrcweir {
192cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r1 = comObjectSource();
193cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r2 = r1.QueryInterface<IUnknown>(IID_IUnknown);
194cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2);
195cdf0e10cSrcweir }
196cdf0e10cSrcweir catch(sal::systools::ComError& ex)
197cdf0e10cSrcweir {
198cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false);
199cdf0e10cSrcweir }
200cdf0e10cSrcweir }
201cdf0e10cSrcweir
test_query_interface_throw()202cdf0e10cSrcweir void test_query_interface_throw()
203cdf0e10cSrcweir {
204cdf0e10cSrcweir try
205cdf0e10cSrcweir {
206cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r1 = comObjectSource();
207cdf0e10cSrcweir sal::systools::COMReference<IPersistFile> r2 = r1.QueryInterface<IPersistFile>(IID_IPersistFile);
208cdf0e10cSrcweir }
209cdf0e10cSrcweir catch(sal::systools::ComError& ex)
210cdf0e10cSrcweir {
211cdf0e10cSrcweir return;
212cdf0e10cSrcweir }
213cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false);
214cdf0e10cSrcweir }
215cdf0e10cSrcweir
216cdf0e10cSrcweir // Change the following lines only, if you add, remove or rename
217cdf0e10cSrcweir // member functions of the current class,
218cdf0e10cSrcweir // because these macros are need by auto register mechanism.
219cdf0e10cSrcweir
220cdf0e10cSrcweir CPPUNIT_TEST_SUITE(test_COMReference);
221cdf0e10cSrcweir CPPUNIT_TEST(default_ctor);
222cdf0e10cSrcweir CPPUNIT_TEST(test_ctor_manual_AddRef);
223cdf0e10cSrcweir CPPUNIT_TEST(test_copy_ctor);
224cdf0e10cSrcweir CPPUNIT_TEST(test_copy_assignment);
225cdf0e10cSrcweir CPPUNIT_TEST(test_ref_to_ref_assignment);
226cdf0e10cSrcweir CPPUNIT_TEST(test_pointer_to_ref_assignment);
227cdf0e10cSrcweir CPPUNIT_TEST(test_pointer_to_ref_assignment2);
228cdf0e10cSrcweir CPPUNIT_TEST(test_source_sink);
229cdf0e10cSrcweir CPPUNIT_TEST(test_address_operator);
230cdf0e10cSrcweir CPPUNIT_TEST(test_address_operator2);
231cdf0e10cSrcweir CPPUNIT_TEST(test_clear);
232cdf0e10cSrcweir CPPUNIT_TEST(test_query_interface);
233cdf0e10cSrcweir CPPUNIT_TEST(test_query_interface_throw);
234cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END();
235cdf0e10cSrcweir };
236cdf0e10cSrcweir
237cdf0e10cSrcweir // -----------------------------------------------------------------------------
238cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_comtools::test_COMReference, "test_comtools");
239cdf0e10cSrcweir
240cdf0e10cSrcweir } // namespace rtl_OUString
241cdf0e10cSrcweir
242cdf0e10cSrcweir
243cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions()
244cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand.
245cdf0e10cSrcweir NOADDITIONAL;
246cdf0e10cSrcweir
247