From 2ae173b07228f9d508da391ae821ac05b6f93d28 Mon Sep 17 00:00:00 2001 From: Thorsten Behrens Date: Tue, 29 Mar 2022 00:34:35 +0200 Subject: Drop ComPtr and use sal::systools::COMReference More partial application of commit ed40d477b2412d4f23540052ca0748028c6103e6 Change-Id: If0bd069b6943486b0bd7b6a82304b082b98ad946 --- sal/CppunitTest_sal_comtools.mk | 4 ++++ sal/qa/systools/test_comtools.cxx | 44 +++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/sal/CppunitTest_sal_comtools.mk b/sal/CppunitTest_sal_comtools.mk index f26f4f8dffa1..1f9c24a0c812 100644 --- a/sal/CppunitTest_sal_comtools.mk +++ b/sal/CppunitTest_sal_comtools.mk @@ -17,4 +17,8 @@ $(eval $(call gb_CppunitTest_use_libraries,sal_comtools,\ sal \ )) +$(eval $(call gb_CppunitTest_use_system_win32_libs,sal_comtools,\ + ole32 \ +)) + # vim: set noet sw=4 ts=4: diff --git a/sal/qa/systools/test_comtools.cxx b/sal/qa/systools/test_comtools.cxx index 096851c9e989..e0fc2d3e47e5 100644 --- a/sal/qa/systools/test_comtools.cxx +++ b/sal/qa/systools/test_comtools.cxx @@ -20,6 +20,7 @@ #include #include #include +#include class COMObject : public IUnknown { @@ -179,30 +180,36 @@ namespace test_comtools void test_query_interface() { - try - { - sal::systools::COMReference r1 = comObjectSource(); - sal::systools::COMReference r2 = r1.QueryInterface(IID_IUnknown); - CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count, 2 is expected", ULONG(2), reinterpret_cast(r2.get())->GetRefCount()); - } - catch(const sal::systools::ComError&) - { - CPPUNIT_ASSERT_MESSAGE("Exception should not have been thrown", false); - } + sal::systools::COMReference r1 = comObjectSource(); + sal::systools::COMReference r2; + CPPUNIT_ASSERT_NO_THROW_MESSAGE( + "Exception should not have been thrown", + r2 = r1.QueryInterface(sal::systools::COM_QUERY_THROW)); + CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count, 2 is expected", ULONG(2), + reinterpret_cast(r2.get())->GetRefCount()); } void test_query_interface_throw() { - try - { - sal::systools::COMReference r1 = comObjectSource(); - sal::systools::COMReference r2 = r1.QueryInterface(IID_IPersistFile); - } - catch(const sal::systools::ComError&) - { + sal::systools::COMReference r1 = comObjectSource(); + CPPUNIT_ASSERT_THROW_MESSAGE("Exception should have been thrown", + auto r2 = r1.QueryInterface(sal::systools::COM_QUERY_THROW), + sal::systools::ComError); + } + + void test_CoCreateInstance() + { + if (FAILED(CoInitialize(nullptr))) return; + { + // Use scope to destroy the reference before calling CoUninitialize + sal::systools::COMReference r; + CPPUNIT_ASSERT_NO_THROW(r.CoCreateInstance(__uuidof(FileOpenDialog))); + // Immediately after CoCreateInstance, refcount must be 1; increasing once gives 2 + CPPUNIT_ASSERT_EQUAL(ULONG(2), r->AddRef()); + r->Release(); } - CPPUNIT_ASSERT_MESSAGE("Exception should have been thrown", false); + CoUninitialize(); } // Change the following lines only, if you add, remove or rename @@ -223,6 +230,7 @@ namespace test_comtools CPPUNIT_TEST(test_clear); CPPUNIT_TEST(test_query_interface); CPPUNIT_TEST(test_query_interface_throw); + CPPUNIT_TEST(test_CoCreateInstance); CPPUNIT_TEST_SUITE_END(); }; -- cgit