LCOV - code coverage report
Current view: top level - cppu/qa - test_reference.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 57 66 86.4 %
Date: 2014-11-03 Functions: 18 20 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <sal/types.h>
      21             : 
      22             : #include <boost/noncopyable.hpp>
      23             : #include <cppunit/TestSuite.h>
      24             : #include <cppunit/TestFixture.h>
      25             : #include <cppunit/TestCase.h>
      26             : #include <cppunit/plugin/TestPlugIn.h>
      27             : #include <cppunit/extensions/HelperMacros.h>
      28             : 
      29             : #include "Interface1.hpp"
      30             : 
      31             : #include "rtl/ustring.hxx"
      32             : 
      33             : namespace
      34             : {
      35             : 
      36             : using ::com::sun::star::uno::Type;
      37             : using ::com::sun::star::uno::Any;
      38             : using ::com::sun::star::uno::Reference;
      39             : using ::com::sun::star::uno::RuntimeException;
      40             : using ::com::sun::star::uno::UNO_SET_THROW;
      41             : 
      42             : class Foo: public Interface1, private boost::noncopyable
      43             : {
      44             : public:
      45           2 :     Foo()
      46           2 :         :m_refCount(0)
      47             :     {
      48           2 :     }
      49             : 
      50           0 :     virtual Any SAL_CALL queryInterface(const Type & _type)
      51             :         throw (RuntimeException, std::exception) SAL_OVERRIDE
      52             :     {
      53           0 :         Any aInterface;
      54           0 :         if (_type == getCppuType< Reference< XInterface > >())
      55             :         {
      56           0 :             Reference< XInterface > ref( static_cast< XInterface * >( this ) );
      57           0 :             aInterface.setValue( &ref, _type );
      58             :         }
      59           0 :         else if (_type == getCppuType< Reference< Interface1 > >())
      60             :         {
      61           0 :             Reference< Interface1 > ref( this );
      62           0 :             aInterface.setValue( &ref, _type );
      63             :         }
      64             : 
      65           0 :         return Any();
      66             :     }
      67             : 
      68          10 :     virtual void SAL_CALL acquire() throw () SAL_OVERRIDE
      69             :     {
      70          10 :         osl_atomic_increment( &m_refCount );
      71          10 :     }
      72             : 
      73          10 :     virtual void SAL_CALL release() throw () SAL_OVERRIDE
      74             :     {
      75          10 :         if ( 0 == osl_atomic_decrement( &m_refCount ) )
      76           2 :             delete this;
      77          10 :     }
      78             : 
      79             : protected:
      80           4 :     virtual ~Foo()
      81           2 :     {
      82           4 :     }
      83             : 
      84             : private:
      85             :     oslInterlockedCount m_refCount;
      86             : };
      87             : 
      88             : // Check that the up-casting Reference conversion constructor catches the
      89             : // intended cases:
      90             : 
      91             : struct Base1: public css::uno::XInterface {
      92             :     virtual ~Base1() SAL_DELETED_FUNCTION;
      93             : };
      94             : struct Base2: public Base1 { virtual ~Base2() SAL_DELETED_FUNCTION; };
      95             : struct Base3: public Base1 { virtual ~Base3() SAL_DELETED_FUNCTION; };
      96             : struct Derived: public Base2, public Base3 {
      97             :     virtual ~Derived() SAL_DELETED_FUNCTION;
      98             : };
      99             : 
     100             : // The special case using the conversion operator instead:
     101           2 : css::uno::Reference< css::uno::XInterface > testUpcast1(
     102             :     css::uno::Reference< Derived > const & ref)
     103           2 : { return ref; }
     104             : 
     105             : // The normal up-cast case:
     106           2 : css::uno::Reference< Base1 > testUpcast2(
     107             :     css::uno::Reference< Base2 > const & ref)
     108           2 : { return ref; }
     109             : 
     110             : // Commenting this in should cause a compiler error due to an ambiguous up-cast:
     111             : /*
     112             : css::uno::Reference< Base1 > testFailingUpcast3(
     113             :     css::uno::Reference< Derived > const & ref)
     114             : { return ref; }
     115             : */
     116             : 
     117             : // Commenting this in should cause a compiler error due to a down-cast:
     118             : /*
     119             : css::uno::Reference< Base2 > testFailingUpcast4(
     120             :     css::uno::Reference< Base1 > const & ref)
     121             : { return ref; }
     122             : */
     123             : 
     124             : // Commenting this in should cause a compiler error due to a down-cast:
     125             : /*
     126             : css::uno::Reference< Base1 > testFailingUpcast5(
     127             :     css::uno::Reference< css::uno::XInterface > const & ref)
     128             : { return ref; }
     129             : */
     130             : 
     131          12 : class Test: public ::CppUnit::TestFixture
     132             : {
     133             : 
     134             : public:
     135             :     void testUnoSetThrow();
     136             :     void testUpcastCompilation();
     137             : 
     138           4 :     CPPUNIT_TEST_SUITE(Test);
     139           2 :     CPPUNIT_TEST(testUnoSetThrow);
     140           2 :     CPPUNIT_TEST(testUpcastCompilation);
     141           4 :     CPPUNIT_TEST_SUITE_END();
     142             : };
     143             : 
     144           2 : void Test::testUnoSetThrow()
     145             : {
     146           2 :     Reference< Interface1 > xNull;
     147           4 :     Reference< Interface1 > xFoo( new Foo );
     148             : 
     149             :     // ctor taking Reference< interface_type >
     150           2 :     bool bCaughtException = false;
     151           4 :     try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
     152           2 :     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
     153             : 
     154           2 :     bCaughtException = false;
     155           2 :     try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
     156           2 :     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
     157             : 
     158             :     // ctor taking interface_type*
     159           2 :     bCaughtException = false;
     160           4 :     try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
     161           2 :     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
     162             : 
     163           2 :     bCaughtException = false;
     164           2 :     try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
     165           2 :     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
     166             : 
     167           4 :     Reference< Interface1 > x;
     168             :     // "set" taking Reference< interface_type >
     169           2 :     bCaughtException = false;
     170           4 :     try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
     171           2 :     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
     172             : 
     173           2 :     bCaughtException = false;
     174           2 :     try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
     175           2 :     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
     176             : 
     177             :     // "set" taking interface_type*
     178           2 :     bCaughtException = false;
     179           4 :     try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
     180           2 :     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
     181             : 
     182           2 :     bCaughtException = false;
     183           2 :     try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
     184           4 :     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
     185           2 : }
     186             : 
     187             : // Include a dummy test calling those functions, to avoid warnings about those
     188             : // functions being unused:
     189           2 : void Test::testUpcastCompilation()
     190             : {
     191           2 :     testUpcast1(css::uno::Reference< Derived >());
     192           2 :     testUpcast2(css::uno::Reference< Base2 >());
     193           2 : }
     194             : 
     195           2 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
     196             : 
     197             : }   // namespace
     198             : 
     199           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     200             : 
     201             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10