1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 30*cdf0e10cSrcweir #include "precompiled_sal.hxx" 31*cdf0e10cSrcweir // autogenerated file with codegen.pl 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <testshl/simpleheader.hxx> 34*cdf0e10cSrcweir #include <systools/win32/comtools.hxx> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir class COMObject : public IUnknown 37*cdf0e10cSrcweir { 38*cdf0e10cSrcweir public: 39*cdf0e10cSrcweir COMObject() : ref_count_(0) 40*cdf0e10cSrcweir { 41*cdf0e10cSrcweir } 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir ~COMObject() 44*cdf0e10cSrcweir { 45*cdf0e10cSrcweir } 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir ULONG __stdcall AddRef() 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir ref_count_++; 50*cdf0e10cSrcweir return ref_count_; 51*cdf0e10cSrcweir } 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir ULONG __stdcall Release() 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir ULONG cnt = --ref_count_; 56*cdf0e10cSrcweir if (cnt == 0) 57*cdf0e10cSrcweir delete this; 58*cdf0e10cSrcweir return cnt; 59*cdf0e10cSrcweir } 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir HRESULT __stdcall QueryInterface(REFIID riid, LPVOID* ppv) 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir if (riid == IID_IUnknown) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir AddRef(); 66*cdf0e10cSrcweir *ppv = this; 67*cdf0e10cSrcweir return S_OK; 68*cdf0e10cSrcweir } 69*cdf0e10cSrcweir return E_NOINTERFACE; 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir ULONG GetRefCount() const 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir return ref_count_; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir private: 78*cdf0e10cSrcweir ULONG ref_count_; 79*cdf0e10cSrcweir }; 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> comObjectSource() 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir return sal::systools::COMReference<IUnknown>(new COMObject); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir bool comObjectSink(sal::systools::COMReference<IUnknown> r, ULONG expectedRefCountOnReturn) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir r = sal::systools::COMReference<IUnknown>(); 89*cdf0e10cSrcweir COMObject* p = reinterpret_cast<COMObject*>(r.get()); 90*cdf0e10cSrcweir if (p) 91*cdf0e10cSrcweir return (p->GetRefCount() == expectedRefCountOnReturn); 92*cdf0e10cSrcweir else 93*cdf0e10cSrcweir return (0 == expectedRefCountOnReturn); 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir void comObjectSource2(LPVOID* ppv) 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir COMObject* p = new COMObject; 99*cdf0e10cSrcweir p->AddRef(); 100*cdf0e10cSrcweir *ppv = p; 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir namespace test_comtools 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir class test_COMReference : public CppUnit::TestFixture 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir public: 110*cdf0e10cSrcweir /// test of COMReference<IUnknown> r; 111*cdf0e10cSrcweir void default_ctor() 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r; 114*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir void test_ctor_manual_AddRef() 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir COMObject* p = new COMObject; 120*cdf0e10cSrcweir p->AddRef(); 121*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r(p, false); 122*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir void test_copy_ctor() 126*cdf0e10cSrcweir { 127*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r(comObjectSource()); 128*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir void test_copy_assignment() 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r; 134*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() == NULL); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir r = comObjectSource(); 137*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() != NULL); 138*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir void test_ref_to_ref_assignment() 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r1 = comObjectSource(); 144*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r2 = r1; 145*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2); 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir void test_pointer_to_ref_assignment() 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r; 151*cdf0e10cSrcweir r = new COMObject; 152*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1); 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir void test_pointer_to_ref_assignment2() 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r = comObjectSource(); 158*cdf0e10cSrcweir r = new COMObject; 159*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1); 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir void test_source_sink() 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0)); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir void test_address_operator() 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r; 170*cdf0e10cSrcweir comObjectSource2(reinterpret_cast<LPVOID*>(&r)); 171*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir void test_address_operator2() 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r1 = comObjectSource(); 177*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r2 = r1; 178*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2); 179*cdf0e10cSrcweir comObjectSource2(reinterpret_cast<LPVOID*>(&r1)); 180*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r1.get())->GetRefCount() == 1); 181*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 1); 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir void test_clear() 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r = comObjectSource(); 187*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count 1 is expected", reinterpret_cast<COMObject*>(r.get())->GetRefCount() == 1); 188*cdf0e10cSrcweir r.clear(); 189*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r.is()); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir void test_query_interface() 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir try 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r1 = comObjectSource(); 197*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r2 = r1.QueryInterface<IUnknown>(IID_IUnknown); 198*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 2 is expected", reinterpret_cast<COMObject*>(r2.get())->GetRefCount() == 2); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir catch(sal::systools::ComError& ex) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false); 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir void test_query_interface_throw() 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir try 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir sal::systools::COMReference<IUnknown> r1 = comObjectSource(); 211*cdf0e10cSrcweir sal::systools::COMReference<IPersistFile> r2 = r1.QueryInterface<IPersistFile>(IID_IPersistFile); 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir catch(sal::systools::ComError& ex) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir return; 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir // Change the following lines only, if you add, remove or rename 221*cdf0e10cSrcweir // member functions of the current class, 222*cdf0e10cSrcweir // because these macros are need by auto register mechanism. 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir CPPUNIT_TEST_SUITE(test_COMReference); 225*cdf0e10cSrcweir CPPUNIT_TEST(default_ctor); 226*cdf0e10cSrcweir CPPUNIT_TEST(test_ctor_manual_AddRef); 227*cdf0e10cSrcweir CPPUNIT_TEST(test_copy_ctor); 228*cdf0e10cSrcweir CPPUNIT_TEST(test_copy_assignment); 229*cdf0e10cSrcweir CPPUNIT_TEST(test_ref_to_ref_assignment); 230*cdf0e10cSrcweir CPPUNIT_TEST(test_pointer_to_ref_assignment); 231*cdf0e10cSrcweir CPPUNIT_TEST(test_pointer_to_ref_assignment2); 232*cdf0e10cSrcweir CPPUNIT_TEST(test_source_sink); 233*cdf0e10cSrcweir CPPUNIT_TEST(test_address_operator); 234*cdf0e10cSrcweir CPPUNIT_TEST(test_address_operator2); 235*cdf0e10cSrcweir CPPUNIT_TEST(test_clear); 236*cdf0e10cSrcweir CPPUNIT_TEST(test_query_interface); 237*cdf0e10cSrcweir CPPUNIT_TEST(test_query_interface_throw); 238*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 239*cdf0e10cSrcweir }; 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 242*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_comtools::test_COMReference, "test_comtools"); 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir } // namespace rtl_OUString 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions() 248*cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand. 249*cdf0e10cSrcweir NOADDITIONAL; 250*cdf0e10cSrcweir 251