/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
staticbool comObjectSink(sal::systools::COMReference<IUnknown> r, ULONG expectedRefCountOnReturn)
{
r = sal::systools::COMReference<IUnknown>();
COMObject* p = reinterpret_cast<COMObject*>(r.get()); if (p) return (p->GetRefCount() == expectedRefCountOnReturn); else return (0 == expectedRefCountOnReturn);
}
staticvoid comObjectSource2(LPVOID* ppv)
{
COMObject* p = new COMObject;
p->AddRef();
*ppv = p;
}
namespace test_comtools
{
class test_COMReference : public CppUnit::TestFixture
{
public: /// test of COMReference<IUnknown> r; void default_ctor()
{
sal::systools::COMReference<IUnknown> r;
CPPUNIT_ASSERT_EQUAL_MESSAGE("COMReference should be empty", static_cast<IUnknown *>(nullptr), r.get());
}
void test_ctor_manual_AddRef()
{
COMObject* p = new COMObject;
p->AddRef();
sal::systools::COMReference<IUnknown> r(p, false);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
}
void test_copy_assignment()
{
sal::systools::COMReference<IUnknown> r;
CPPUNIT_ASSERT_EQUAL_MESSAGE("COMReference should be empty", static_cast<IUnknown *>(nullptr), r.get());
r = comObjectSource();
CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() != nullptr);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
}
void test_pointer_to_ref_assignment()
{
sal::systools::COMReference<IUnknown> r;
r = new COMObject;
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
}
void test_pointer_to_ref_assignment2()
{
sal::systools::COMReference<IUnknown> r = comObjectSource();
r = new COMObject;
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
}
void test_clear()
{
sal::systools::COMReference<IUnknown> r = comObjectSource();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
r.clear();
CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r.is());
}
void test_query_interface()
{
sal::systools::COMReference<IUnknown> r1 = comObjectSource();
sal::systools::COMReference<IUnknown> r2;
CPPUNIT_ASSERT_NO_THROW_MESSAGE( "Exception should not have been thrown",
r2 = r1.QueryInterface<IUnknown>(sal::systools::COM_QUERY_THROW));
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count, 2 is expected", ULONG(2), reinterpret_cast<COMObject*>(r2.get())->GetRefCount());
}
void test_query_interface_throw()
{
sal::systools::COMReference<IUnknown> r1 = comObjectSource();
CPPUNIT_ASSERT_THROW_MESSAGE("Exception should have been thrown", auto r2 = r1.QueryInterface<IPersistFile>(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<IFileOpenDialog> 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();
}
CoUninitialize();
}
// Change the following lines only, if you add, remove or rename // member functions of the current class, // because these macros are need by auto register mechanism.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.