LCOV - code coverage report
Current view: top level - basic/qa/cppunit - test_nested_struct.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 134 134 100.0 %
Date: 2014-04-11 Functions: 18 19 94.7 %
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             : #include "basictest.hxx"
      10             : #include <osl/file.hxx>
      11             : #include <osl/process.h>
      12             : 
      13             : #include <basic/sbmod.hxx>
      14             : #include <basic/sbmeth.hxx>
      15             : #include <com/sun/star/awt/WindowDescriptor.hpp>
      16             : #include <com/sun/star/table/TableBorder.hpp>
      17             : #include <basic/sbuno.hxx>
      18             : 
      19             : namespace
      20             : {
      21             :     using namespace com::sun::star;
      22          18 :     class Nested_Struct : public test::BootstrapFixture
      23             :     {
      24             :         public:
      25           9 :         Nested_Struct(): BootstrapFixture(true, false) {};
      26             :         void testAssign1();
      27             :         void testAssign1Alt(); // result is uno-ised and tested
      28             :         void testOldAssign();
      29             :         void testOldAssignAlt(); // result is uno-ised and tested
      30             :         void testUnfixedVarAssign();
      31             :         void testUnfixedVarAssignAlt(); // result is uno-ised and tested
      32             :         void testFixedVarAssign();
      33             :         void testFixedVarAssignAlt(); // result is uno-ised and tested
      34             :         void testUnoAccess(); // fdo#60117 specific test
      35             : 
      36             :         // Adds code needed to register the test suite
      37           2 :         CPPUNIT_TEST_SUITE(Nested_Struct);
      38             : 
      39             :         // Declares the method as a test to call
      40           1 :         CPPUNIT_TEST(testAssign1);
      41           1 :         CPPUNIT_TEST(testAssign1Alt);
      42           1 :         CPPUNIT_TEST(testOldAssign);
      43           1 :         CPPUNIT_TEST(testOldAssignAlt);
      44           1 :         CPPUNIT_TEST(testUnfixedVarAssign);
      45           1 :         CPPUNIT_TEST(testUnfixedVarAssignAlt);
      46           1 :         CPPUNIT_TEST(testFixedVarAssign);
      47           1 :         CPPUNIT_TEST(testFixedVarAssignAlt);
      48           1 :         CPPUNIT_TEST(testUnoAccess);
      49             : 
      50             :         // End of test suite definition
      51           2 :         CPPUNIT_TEST_SUITE_END();
      52             :     };
      53             : 
      54             : // tests the new behaviour, we should be able to
      55             : // directly modify the value of the nested 'HorizontalLine' struct
      56           2 : OUString sTestSource1(
      57             :     "Function doUnitTest() as Integer\n"
      58             :     "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
      59             :     "b0.HorizontalLine.OuterLineWidth = 9\n"
      60             :     "doUnitTest = b0.HorizontalLine.OuterLineWidth\n"
      61             :     "End Function\n"
      62           1 : );
      63             : 
      64           2 : OUString sTestSource1Alt(
      65             :     "Function doUnitTest() as Object\n"
      66             :     "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
      67             :     "b0.HorizontalLine.OuterLineWidth = 9\n"
      68             :     "doUnitTest = b0\n"
      69             :     "End Function\n"
      70           1 : );
      71             : 
      72             : // tests the old behaviour, we should still be able
      73             : // to use the old workaround of
      74             : // a) creating a new instance BorderLine,
      75             : // b) cloning the new instance with the value of b0.HorizontalLine
      76             : // c) modifying the new instance
      77             : // d) setting b0.HorizontalLine with the value of the new instance
      78           2 : OUString sTestSource2(
      79             :     "Function doUnitTest()\n"
      80             :     "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
      81             :     "l = b0.HorizontalLine\n"
      82             :     "l.OuterLineWidth = 9\n"
      83             :     "b0.HorizontalLine = l\n"
      84             :     "doUnitTest = b0.HorizontalLine.OuterLineWidth\n"
      85             : "End Function\n"
      86           1 : );
      87             : 
      88           2 : OUString sTestSource2Alt(
      89             :     "Function doUnitTest()\n"
      90             :     "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
      91             :     "l = b0.HorizontalLine\n"
      92             :     "l.OuterLineWidth = 9\n"
      93             :     "b0.HorizontalLine = l\n"
      94             :     "doUnitTest = b0\n"
      95             : "End Function\n"
      96           1 : );
      97             : // it should be legal to assign a variant to a struct ( and copy by val )
      98             : // make sure we aren't copying by reference, we make sure that l is not
      99             : // a reference copy of b0.HorizontalLine, each one should have an
     100             : // OuterLineWidth of 4 & 9 respectively and we should be returning
     101             : // 13 the sum of the two ( hopefully unique values if we haven't copied by reference )
     102           2 : OUString sTestSource3(
     103             :     "Function doUnitTest()\n"
     104             :     "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
     105             :     "l = b0.HorizontalLine\n"
     106             :     "l.OuterLineWidth = 9\n"
     107             :     "b0.HorizontalLine = l\n"
     108             :     "l.OuterLineWidth = 4\n"
     109             :     "doUnitTest = b0.HorizontalLine.OuterLineWidth + l.OuterLineWidth\n"
     110             : "End Function\n"
     111           1 : );
     112             : 
     113           2 : OUString sTestSource3Alt(
     114             :     "Function doUnitTest()\n"
     115             :     "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
     116             :     "l = b0.HorizontalLine\n"
     117             :     "l.OuterLineWidth = 9\n"
     118             :     "b0.HorizontalLine = l\n"
     119             :     "l.OuterLineWidth = 4\n"
     120             :     "Dim result(1)\n"
     121             :     "result(0) = b0\n"
     122             :     "result(1) = l\n"
     123             :     "doUnitTest = result\n"
     124             : "End Function\n"
     125           1 : );
     126             : 
     127             : // nearly the same as above but this time for a fixed type
     128             : // variable
     129           2 : OUString sTestSource4(
     130             :     "Function doUnitTest()\n"
     131             :     "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
     132             :     "l = b0.HorizontalLine\n"
     133             :     "l.OuterLineWidth = 9\n"
     134             :     "b0.HorizontalLine = l\n"
     135             :     "l.OuterLineWidth = 4\n"
     136             :     "doUnitTest = b0.HorizontalLine.OuterLineWidth + l.OuterLineWidth\n"
     137             : "End Function\n"
     138           1 : );
     139             : 
     140           2 : OUString sTestSource4Alt(
     141             :     "Function doUnitTest()\n"
     142             :     "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
     143             :     "l = b0.HorizontalLine\n"
     144             :     "l.OuterLineWidth = 9\n"
     145             :     "b0.HorizontalLine = l\n"
     146             :     "l.OuterLineWidth = 4\n"
     147             :     "Dim result(1)\n"
     148             :     "result(0) = b0\n"
     149             :     "result(1) = l\n"
     150             :     "doUnitTest = result\n"
     151             : "End Function\n"
     152           1 : );
     153             : 
     154             : // Although basic might appear to correctly change nested struct elements
     155             : // fdo#60117 shows that basic can be fooled ( and even the watch(ed) variable
     156             : // in the debugger shows the expected values )
     157             : // We need to additionally check the actual uno struct to see if the
     158             : // changes made are *really* reflected in the object
     159           2 : OUString sTestSource5(
     160             :     "Function doUnitTest() as Object\n"
     161             :     "Dim aWinDesc as new \"com.sun.star.awt.WindowDescriptor\"\n"
     162             :     "Dim aRect as new \"com.sun.star.awt.Rectangle\"\n"
     163             :     "aRect.X = 200\n"
     164             :     "aWinDesc.Bounds = aRect\n"
     165             :     "doUnitTest = aWinDesc\n"
     166             : "End Function\n"
     167           1 : );
     168             : 
     169             : 
     170           1 : void Nested_Struct::testAssign1()
     171             : {
     172           1 :     MacroSnippet myMacro( sTestSource1 );
     173           1 :     myMacro.Compile();
     174           1 :     CPPUNIT_ASSERT_MESSAGE("testAssign1 fails with compile error",!myMacro.HasError() );
     175           2 :     SbxVariableRef pNew = myMacro.Run();
     176           2 :     CPPUNIT_ASSERT(pNew->GetInteger() == 9 );
     177           1 : }
     178             : 
     179           1 : void Nested_Struct::testAssign1Alt()
     180             : {
     181           1 :     MacroSnippet myMacro( sTestSource1Alt );
     182           1 :     myMacro.Compile();
     183           1 :     CPPUNIT_ASSERT_MESSAGE("testAssign1Alt fails with compile error",!myMacro.HasError() );
     184           2 :     SbxVariableRef pNew = myMacro.Run();
     185           2 :     uno::Any aRet = sbxToUnoValue( pNew );
     186           1 :     table::TableBorder aBorder;
     187           1 :     aRet >>= aBorder;
     188             : 
     189           1 :     int result = aBorder.HorizontalLine.OuterLineWidth;
     190           2 :     CPPUNIT_ASSERT_EQUAL( 9, result );
     191           1 : }
     192             : 
     193           1 : void Nested_Struct::testOldAssign()
     194             : {
     195           1 :     MacroSnippet myMacro( sTestSource2 );
     196           1 :     myMacro.Compile();
     197           1 :     CPPUNIT_ASSERT_MESSAGE("testOldAssign fails with compile error",!myMacro.HasError() );
     198           2 :     SbxVariableRef pNew = myMacro.Run();
     199           2 :     CPPUNIT_ASSERT(pNew->GetInteger() == 9 );
     200           1 : }
     201             : 
     202           1 : void Nested_Struct::testOldAssignAlt()
     203             : {
     204           1 :     MacroSnippet myMacro( sTestSource2Alt );
     205           1 :     myMacro.Compile();
     206           1 :     CPPUNIT_ASSERT_MESSAGE("testOldAssign fails with compile error",!myMacro.HasError() );
     207           2 :     SbxVariableRef pNew = myMacro.Run();
     208           2 :     uno::Any aRet = sbxToUnoValue( pNew );
     209           1 :     table::TableBorder aBorder;
     210           1 :     aRet >>= aBorder;
     211             : 
     212           1 :     int result = aBorder.HorizontalLine.OuterLineWidth;
     213           2 :     CPPUNIT_ASSERT_EQUAL( 9, result );
     214           1 : }
     215             : 
     216           1 : void Nested_Struct::testUnfixedVarAssign()
     217             : {
     218           1 :     MacroSnippet myMacro( sTestSource3 );
     219           1 :     myMacro.Compile();
     220           1 :     CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssign fails with compile error",!myMacro.HasError() );
     221             :     // forces a broadcast
     222           2 :     SbxVariableRef pNew = myMacro.Run();
     223           2 :     CPPUNIT_ASSERT(pNew->GetInteger() == 13 );
     224           1 : }
     225             : 
     226           1 : void Nested_Struct::testUnfixedVarAssignAlt()
     227             : {
     228           1 :     MacroSnippet myMacro( sTestSource3Alt );
     229           1 :     myMacro.Compile();
     230           1 :     CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssignAlt fails with compile error",!myMacro.HasError() );
     231           2 :     SbxVariableRef pNew = myMacro.Run();
     232           2 :     uno::Any aRet = sbxToUnoValue( pNew );
     233             : 
     234           2 :     uno::Sequence< uno::Any > aResult;
     235           1 :     bool bRes = aRet >>= aResult;
     236           1 :     CPPUNIT_ASSERT_EQUAL(true, bRes );
     237             : 
     238           1 :     int result = aResult.getLength();
     239             :     // should have 2 elements in a sequence returned
     240           1 :     CPPUNIT_ASSERT_EQUAL(2, result );
     241             : 
     242           1 :     table::TableBorder aBorder;
     243           1 :     aResult[0] >>= aBorder;
     244             : 
     245           1 :     table::BorderLine aBorderLine;
     246           1 :     aResult[1] >>= aBorderLine;
     247           1 :     result = aBorder.HorizontalLine.OuterLineWidth;
     248           1 :     CPPUNIT_ASSERT_EQUAL(9, result );
     249           1 :     result = aBorderLine.OuterLineWidth;
     250           2 :     CPPUNIT_ASSERT_EQUAL(4, result );
     251           1 : }
     252             : 
     253           1 : void Nested_Struct::testFixedVarAssign()
     254             : {
     255           1 :     MacroSnippet myMacro( sTestSource4 );
     256           1 :     myMacro.Compile();
     257           1 :     CPPUNIT_ASSERT_MESSAGE("testFixedVarAssign fails with compile error",!myMacro.HasError() );
     258           2 :     SbxVariableRef pNew = myMacro.Run();
     259           2 :     CPPUNIT_ASSERT(pNew->GetInteger() == 13 );
     260           1 : }
     261             : 
     262           1 : void Nested_Struct::testFixedVarAssignAlt()
     263             : {
     264           1 :     MacroSnippet myMacro( sTestSource4Alt );
     265           1 :     myMacro.Compile();
     266           1 :     CPPUNIT_ASSERT_MESSAGE("testFixedVarAssignAlt fails with compile error",!myMacro.HasError() );
     267           2 :     SbxVariableRef pNew = myMacro.Run();
     268           2 :     uno::Any aRet = sbxToUnoValue( pNew );
     269             : 
     270           2 :     uno::Sequence< uno::Any > aResult;
     271           1 :     bool bRes = aRet >>= aResult;
     272           1 :     CPPUNIT_ASSERT_EQUAL(true, bRes );
     273             : 
     274           1 :     int result = aResult.getLength();
     275             :     // should have 2 elements in a sequence returned
     276           1 :     CPPUNIT_ASSERT_EQUAL(2, result );
     277             : 
     278           1 :     table::TableBorder aBorder;
     279           1 :     aResult[0] >>= aBorder;
     280             : 
     281           1 :     table::BorderLine aBorderLine;
     282           1 :     aResult[1] >>= aBorderLine;
     283           1 :     result = aBorder.HorizontalLine.OuterLineWidth;
     284           1 :     CPPUNIT_ASSERT_EQUAL(9, result );
     285           1 :     result = aBorderLine.OuterLineWidth;
     286           2 :     CPPUNIT_ASSERT_EQUAL(4, result );
     287           1 : }
     288             : 
     289           1 : void Nested_Struct::testUnoAccess()
     290             : {
     291           1 :     MacroSnippet myMacro( sTestSource5 );
     292           1 :     myMacro.Compile();
     293           1 :     CPPUNIT_ASSERT_MESSAGE("testUnoAccess fails with compile error",!myMacro.HasError() );
     294           2 :     SbxVariableRef pNew = myMacro.Run();
     295           2 :     uno::Any aRet = sbxToUnoValue( pNew );
     296           2 :     awt::WindowDescriptor aWinDesc;
     297           1 :     aRet >>= aWinDesc;
     298             : 
     299           1 :     int result = aWinDesc.Bounds.X;
     300           2 :     CPPUNIT_ASSERT_EQUAL(200, result );
     301           1 : }
     302             : 
     303             :   // Put the test suite in the registry
     304           1 :   CPPUNIT_TEST_SUITE_REGISTRATION(Nested_Struct);
     305             : } // namespace
     306           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     307             : 
     308             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10