LCOV - code coverage report
Current view: top level - cppu/qa - test_reference.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 57 66 86.4 %
Date: 2015-06-13 12:38:46 Functions: 21 23 91.3 %
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           1 :     Foo()
      46           1 :         :m_refCount(0)
      47             :     {
      48           1 :     }
      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 == cppu::UnoType<XInterface>::get())
      55             :         {
      56           0 :             Reference< XInterface > ref( static_cast< XInterface * >( this ) );
      57           0 :             aInterface.setValue( &ref, _type );
      58             :         }
      59           0 :         else if (_type == cppu::UnoType<Interface1>::get())
      60             :         {
      61           0 :             Reference< Interface1 > ref( this );
      62           0 :             aInterface.setValue( &ref, _type );
      63             :         }
      64             : 
      65           0 :         return Any();
      66             :     }
      67             : 
      68           5 :     virtual void SAL_CALL acquire() throw () SAL_OVERRIDE
      69             :     {
      70           5 :         osl_atomic_increment( &m_refCount );
      71           5 :     }
      72             : 
      73           5 :     virtual void SAL_CALL release() throw () SAL_OVERRIDE
      74             :     {
      75           5 :         if ( 0 == osl_atomic_decrement( &m_refCount ) )
      76           1 :             delete this;
      77           5 :     }
      78             : 
      79             : protected:
      80           2 :     virtual ~Foo()
      81           1 :     {
      82           2 :     }
      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           1 : css::uno::Reference< css::uno::XInterface > testUpcast1(
     102             :     css::uno::Reference< Derived > const & ref)
     103           1 : { return ref; }
     104             : 
     105             : // The normal up-cast case:
     106           1 : css::uno::Reference< Base1 > testUpcast2(
     107             :     css::uno::Reference< Base2 > const & ref)
     108           1 : { 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           6 : class Test: public ::CppUnit::TestFixture
     132             : {
     133             : 
     134             : public:
     135             :     void testUnoSetThrow();
     136             :     void testUpcastCompilation();
     137             : 
     138           2 :     CPPUNIT_TEST_SUITE(Test);
     139           1 :     CPPUNIT_TEST(testUnoSetThrow);
     140           1 :     CPPUNIT_TEST(testUpcastCompilation);
     141           5 :     CPPUNIT_TEST_SUITE_END();
     142             : };
     143             : 
     144           1 : void Test::testUnoSetThrow()
     145             : {
     146           1 :     Reference< Interface1 > xNull;
     147           2 :     Reference< Interface1 > xFoo( new Foo );
     148             : 
     149             :     // ctor taking Reference< interface_type >
     150           1 :     bool bCaughtException = false;
     151           2 :     try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
     152           1 :     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
     153             : 
     154           1 :     bCaughtException = false;
     155           1 :     try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
     156           1 :     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
     157             : 
     158             :     // ctor taking interface_type*
     159           1 :     bCaughtException = false;
     160           2 :     try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
     161           1 :     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
     162             : 
     163           1 :     bCaughtException = false;
     164           1 :     try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
     165           1 :     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
     166             : 
     167           2 :     Reference< Interface1 > x;
     168             :     // "set" taking Reference< interface_type >
     169           1 :     bCaughtException = false;
     170           2 :     try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
     171           1 :     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
     172             : 
     173           1 :     bCaughtException = false;
     174           1 :     try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
     175           1 :     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
     176             : 
     177             :     // "set" taking interface_type*
     178           1 :     bCaughtException = false;
     179           2 :     try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
     180           1 :     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
     181             : 
     182           1 :     bCaughtException = false;
     183           1 :     try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
     184           2 :     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
     185           1 : }
     186             : 
     187             : // Include a dummy test calling those functions, to avoid warnings about those
     188             : // functions being unused:
     189           1 : void Test::testUpcastCompilation()
     190             : {
     191           1 :     testUpcast1(css::uno::Reference< Derived >());
     192           1 :     testUpcast2(css::uno::Reference< Base2 >());
     193           1 : }
     194             : 
     195           1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
     196             : 
     197             : }   // namespace
     198             : 
     199           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     200             : 
     201             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11