LCOV - code coverage report
Current view: top level - starmath/qa/cppunit - test_starmath.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 162 162 100.0 %
Date: 2015-06-13 12:38:46 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             : 
      10             : #include <sal/config.h>
      11             : #include <test/bootstrapfixture.hxx>
      12             : 
      13             : #include <smdll.hxx>
      14             : #include <document.hxx>
      15             : #include <view.hxx>
      16             : 
      17             : #include <sfx2/sfxmodelfactory.hxx>
      18             : #include <sfx2/bindings.hxx>
      19             : #include <sfx2/request.hxx>
      20             : #include <sfx2/dispatch.hxx>
      21             : 
      22             : #include <editeng/editeng.hxx>
      23             : #include <editeng/editview.hxx>
      24             : 
      25             : #include <sfx2/zoomitem.hxx>
      26             : #include <memory>
      27             : 
      28             : typedef tools::SvRef<SmDocShell> SmDocShellRef;
      29             : 
      30             : using namespace ::com::sun::star;
      31             : 
      32             : namespace {
      33             : 
      34           8 : class Test : public test::BootstrapFixture
      35             : {
      36             : public:
      37             :     Test();
      38             : 
      39             :     // init
      40             :     virtual void setUp() SAL_OVERRIDE;
      41             :     virtual void tearDown() SAL_OVERRIDE;
      42             : 
      43             :     // tests
      44             :     void editUndoRedo();
      45             :     void editMarker();
      46             :     void editFailure();
      47             : 
      48             :     void viewZoom();
      49             : 
      50           2 :     CPPUNIT_TEST_SUITE(Test);
      51           1 :     CPPUNIT_TEST(editUndoRedo);
      52           1 :     CPPUNIT_TEST(editMarker);
      53           1 :     CPPUNIT_TEST(editFailure);
      54           1 :     CPPUNIT_TEST(viewZoom);
      55           5 :     CPPUNIT_TEST_SUITE_END();
      56             : 
      57             : private:
      58             :     SfxBindings m_aBindings;
      59             :     std::unique_ptr<SfxDispatcher> m_pDispatcher;
      60             :     VclPtr<SmCmdBoxWindow> m_pSmCmdBoxWindow;
      61             :     VclPtr<SmEditWindow> m_pEditWindow;
      62             :     SmDocShellRef m_xDocShRef;
      63             :     SmViewShell *m_pViewShell;
      64             : };
      65             : 
      66           4 : Test::Test()
      67           4 :     : m_pViewShell(nullptr)
      68             : {
      69           4 : }
      70             : 
      71           4 : void Test::setUp()
      72             : {
      73           4 :     BootstrapFixture::setUp();
      74             : 
      75           4 :     SmGlobals::ensure();
      76             : 
      77          12 :     m_xDocShRef = new SmDocShell(
      78             :         SfxModelFlags::EMBEDDED_OBJECT |
      79           8 :         SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS |
      80          12 :         SfxModelFlags::DISABLE_DOCUMENT_RECOVERY);
      81           4 :     m_xDocShRef->DoInitNew(0);
      82             : 
      83           4 :     SfxViewFrame *pViewFrame = SfxViewFrame::LoadHiddenDocument(*m_xDocShRef, 0);
      84             : 
      85           4 :     CPPUNIT_ASSERT_MESSAGE("Should have a SfxViewFrame", pViewFrame);
      86             : 
      87           4 :     m_pDispatcher.reset(new SfxDispatcher(pViewFrame));
      88           4 :     m_aBindings.SetDispatcher(m_pDispatcher.get());
      89           4 :     m_aBindings.EnterRegistrations();
      90           4 :     m_pSmCmdBoxWindow.reset(VclPtr<SmCmdBoxWindow>::Create(&m_aBindings, nullptr, nullptr));
      91           4 :     m_aBindings.LeaveRegistrations();
      92           4 :     m_pEditWindow = VclPtr<SmEditWindow>::Create(*m_pSmCmdBoxWindow);
      93           4 :     m_pViewShell = m_pEditWindow->GetView();
      94           4 :     CPPUNIT_ASSERT_MESSAGE("Should have a SmViewShell", m_pViewShell);
      95           4 : }
      96             : 
      97           4 : void Test::tearDown()
      98             : {
      99           4 :     m_pEditWindow.disposeAndClear();
     100           4 :     m_pSmCmdBoxWindow.disposeAndClear();
     101           4 :     m_pDispatcher.reset();
     102           4 :     m_xDocShRef->DoClose();
     103           4 :     m_xDocShRef.Clear();
     104             : 
     105           4 :     BootstrapFixture::tearDown();
     106           4 : }
     107             : 
     108           1 : void Test::editMarker()
     109             : {
     110             :     {
     111           1 :         OUString sMarkedText("<?> under <?> under <?>");
     112           1 :         m_pEditWindow->SetText(sMarkedText);
     113           1 :         m_pEditWindow->Flush();
     114           2 :         OUString sFinalText = m_pEditWindow->GetText();
     115           2 :         CPPUNIT_ASSERT_MESSAGE("Should be equal text", sFinalText == sMarkedText);
     116             :     }
     117             : 
     118             :     {
     119           1 :         OUString sTargetText("a under b under c");
     120             : 
     121           1 :         m_pEditWindow->SelNextMark();
     122           1 :         m_pEditWindow->Delete();
     123           1 :         m_pEditWindow->InsertText("a");
     124             : 
     125           1 :         m_pEditWindow->SelNextMark();
     126           1 :         m_pEditWindow->SelNextMark();
     127           1 :         m_pEditWindow->Delete();
     128           1 :         m_pEditWindow->InsertText("c");
     129             : 
     130           1 :         m_pEditWindow->SelPrevMark();
     131           1 :         m_pEditWindow->Delete();
     132           1 :         m_pEditWindow->InsertText("b");
     133             : 
     134           1 :         m_pEditWindow->Flush();
     135           2 :         OUString sFinalText = m_pEditWindow->GetText();
     136           2 :         CPPUNIT_ASSERT_MESSAGE("Should be a under b under c", sFinalText == sTargetText);
     137             :     }
     138             : 
     139             :     {
     140           1 :         m_pEditWindow->SetText(OUString());
     141           1 :         m_pEditWindow->Flush();
     142             :     }
     143           1 : }
     144             : 
     145           1 : void Test::editFailure()
     146             : {
     147           1 :     m_xDocShRef->SetText("color a b over {a/}");
     148             : 
     149           1 :     const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
     150             : 
     151           2 :     CPPUNIT_ASSERT_MESSAGE("Should be a PE_COLOR_EXPECTED",
     152           1 :         pErrorDesc && pErrorDesc->Type == PE_COLOR_EXPECTED);
     153             : 
     154           1 :     pErrorDesc = m_xDocShRef->GetParser().PrevError();
     155             : 
     156           2 :     CPPUNIT_ASSERT_MESSAGE("Should be a PE_UNEXPECTED_CHAR",
     157           1 :         pErrorDesc && pErrorDesc->Type == PE_UNEXPECTED_CHAR);
     158             : 
     159           1 :     pErrorDesc = m_xDocShRef->GetParser().PrevError();
     160             : 
     161           2 :     CPPUNIT_ASSERT_MESSAGE("Should be a PE_RGROUP_EXPECTED",
     162           1 :         pErrorDesc && pErrorDesc->Type == PE_RGROUP_EXPECTED);
     163             : 
     164           1 :     const SmErrorDesc *pLastErrorDesc = m_xDocShRef->GetParser().PrevError();
     165             : 
     166           2 :     CPPUNIT_ASSERT_MESSAGE("Should be three syntax errors",
     167           1 :         pLastErrorDesc && pLastErrorDesc == pErrorDesc);
     168           1 : }
     169             : 
     170           1 : void Test::editUndoRedo()
     171             : {
     172           1 :     EditEngine &rEditEngine = m_xDocShRef->GetEditEngine();
     173             : 
     174           1 :     OUString sStringOne("a under b");
     175             :     {
     176           1 :         rEditEngine.SetText(0, sStringOne);
     177           1 :         m_xDocShRef->UpdateText();
     178           1 :         OUString sFinalText = m_xDocShRef->GetText();
     179           1 :         CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringOne == sFinalText);
     180             :     }
     181             : 
     182           2 :     OUString sStringTwo("a over b");
     183             :     {
     184           1 :         rEditEngine.SetText(0, sStringTwo);
     185           1 :         m_xDocShRef->UpdateText();
     186           1 :         OUString sFinalText = m_xDocShRef->GetText();
     187           1 :         CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringTwo == sFinalText);
     188             :     }
     189             : 
     190           2 :     SfxRequest aUndo(SID_UNDO, SfxCallMode::SYNCHRON, SmDocShell::GetPool());
     191             : 
     192             :     {
     193           1 :         m_xDocShRef->Execute(aUndo);
     194           1 :         m_xDocShRef->UpdateText();
     195           1 :         OUString sFinalText = m_xDocShRef->GetText();
     196           1 :         CPPUNIT_ASSERT_MESSAGE("Strings much match", sStringOne == sFinalText);
     197             :     }
     198             : 
     199             :     {
     200           1 :         m_xDocShRef->Execute(aUndo);
     201           1 :         m_xDocShRef->UpdateText();
     202           1 :         OUString sFinalText = m_xDocShRef->GetText();
     203           1 :         CPPUNIT_ASSERT_MESSAGE("Must now be empty", !sFinalText.getLength());
     204             :     }
     205             : 
     206           2 :     SfxRequest aRedo(SID_REDO, SfxCallMode::SYNCHRON, SmDocShell::GetPool());
     207             :     {
     208           1 :         m_xDocShRef->Execute(aRedo);
     209           1 :         m_xDocShRef->UpdateText();
     210           1 :         OUString sFinalText = m_xDocShRef->GetText();
     211           1 :         CPPUNIT_ASSERT_MESSAGE("Strings much match", sStringOne == sFinalText);
     212             :     }
     213             : 
     214             :     {
     215           1 :         rEditEngine.SetText(0, OUString());
     216           1 :         m_xDocShRef->UpdateText();
     217           1 :         rEditEngine.ClearModifyFlag();
     218           1 :         OUString sFinalText = m_xDocShRef->GetText();
     219           1 :         CPPUNIT_ASSERT_MESSAGE("Must be empty", !sFinalText.getLength());
     220           1 :     }
     221             : 
     222           1 : }
     223             : 
     224           1 : void Test::viewZoom()
     225             : {
     226             :     sal_uInt16 nOrigZoom, nNextZoom, nFinalZoom;
     227             : 
     228           1 :     EditEngine &rEditEngine = m_xDocShRef->GetEditEngine();
     229             : 
     230           1 :     OUString sStringOne("a under b");
     231             :     {
     232           1 :         rEditEngine.SetText(0, sStringOne);
     233           1 :         m_xDocShRef->UpdateText();
     234           1 :         OUString sFinalText = m_xDocShRef->GetText();
     235           1 :         CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringOne == sFinalText);
     236             :     }
     237             : 
     238           1 :     SmGraphicWindow &rGraphicWindow = m_pViewShell->GetGraphicWindow();
     239           1 :     rGraphicWindow.SetSizePixel(Size(1024, 800));
     240           1 :     nOrigZoom = rGraphicWindow.GetZoom();
     241             : 
     242             :     {
     243           1 :         SfxRequest aZoomIn(SID_ZOOMIN, SfxCallMode::SYNCHRON, m_pViewShell->GetPool());
     244           1 :         m_pViewShell->Execute(aZoomIn);
     245           1 :         nNextZoom = rGraphicWindow.GetZoom();
     246           1 :         CPPUNIT_ASSERT_MESSAGE("Should be bigger", nNextZoom > nOrigZoom);
     247             :     }
     248             : 
     249             :     {
     250           1 :         SfxRequest aZoomOut(SID_ZOOMOUT, SfxCallMode::SYNCHRON, m_pViewShell->GetPool());
     251           1 :         m_pViewShell->Execute(aZoomOut);
     252           1 :         nFinalZoom = rGraphicWindow.GetZoom();
     253           1 :         CPPUNIT_ASSERT_MESSAGE("Should be equal", nFinalZoom == nOrigZoom);
     254             :     }
     255             : 
     256           1 :     sal_uInt16 nOptimalZoom=0;
     257             : 
     258             :     {
     259           1 :         SfxRequest aZoom(SID_ZOOM_OPTIMAL, SfxCallMode::SYNCHRON, m_pViewShell->GetPool());
     260           1 :         m_pViewShell->Execute(aZoom);
     261           1 :         nOptimalZoom = rGraphicWindow.GetZoom();
     262           1 :         CPPUNIT_ASSERT_MESSAGE("Should be about 800%", nOptimalZoom > nOrigZoom);
     263             :     }
     264             : 
     265             :     {
     266           1 :         SfxItemSet aSet(SmDocShell::GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
     267           1 :         aSet.Put(SvxZoomItem(SvxZoomType::OPTIMAL, 0));
     268           2 :         SfxRequest aZoom(SID_ATTR_ZOOM, SfxCallMode::SYNCHRON, aSet);
     269           1 :         m_pViewShell->Execute(aZoom);
     270           1 :         nFinalZoom = rGraphicWindow.GetZoom();
     271           2 :         CPPUNIT_ASSERT_MESSAGE("Should be optimal zoom", nFinalZoom == nOptimalZoom);
     272             :     }
     273             : 
     274             : //To-Do: investigate GetPrinter logic of SvxZoomType::PAGEWIDTH/SvxZoomType::WHOLEPAGE to ensure
     275             : //consistent value regardless of
     276             : #if 0
     277             :     {
     278             :         SfxRequest aZoomOut(SID_ZOOMOUT, SfxCallMode::SYNCHRON, m_pViewShell->GetPool());
     279             :         m_pViewShell->Execute(aZoomOut);
     280             :         nFinalZoom = rGraphicWindow.GetZoom();
     281             :         CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
     282             : 
     283             :         SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
     284             :         aSet.Put(SvxZoomItem(SvxZoomType::PAGEWIDTH, 0));
     285             :         SfxRequest aZoom(SID_ATTR_ZOOM, SfxCallMode::SYNCHRON, aSet);
     286             :         m_pViewShell->Execute(aZoom);
     287             :         nFinalZoom = rGraphicWindow.GetZoom();
     288             :         CPPUNIT_ASSERT_MESSAGE("Should be same as optimal zoom", nFinalZoom == nOptimalZoom);
     289             :     }
     290             : 
     291             :     {
     292             :         SfxRequest aZoomOut(SID_ZOOMOUT, SfxCallMode::SYNCHRON, m_pViewShell->GetPool());
     293             :         m_pViewShell->Execute(aZoomOut);
     294             :         nFinalZoom = rGraphicWindow.GetZoom();
     295             :         CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
     296             : 
     297             :         SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
     298             :         aSet.Put(SvxZoomItem(SvxZoomType::WHOLEPAGE, 0));
     299             :         SfxRequest aZoom(SID_ATTR_ZOOM, SfxCallMode::SYNCHRON, aSet);
     300             :         m_pViewShell->Execute(aZoom);
     301             :         nFinalZoom = rGraphicWindow.GetZoom();
     302             :         CPPUNIT_ASSERT_MESSAGE("Should be same as optimal zoom", nFinalZoom == nOptimalZoom);
     303             :     }
     304             : #endif
     305             : 
     306             :     {
     307           1 :         SfxRequest aZoomOut(SID_ZOOMOUT, SfxCallMode::SYNCHRON, m_pViewShell->GetPool());
     308           1 :         m_pViewShell->Execute(aZoomOut);
     309           1 :         nFinalZoom = rGraphicWindow.GetZoom();
     310           1 :         CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
     311             : 
     312           2 :         SfxItemSet aSet(SmDocShell::GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
     313           1 :         aSet.Put(SvxZoomItem(SvxZoomType::PERCENT, 50));
     314           2 :         SfxRequest aZoom(SID_ATTR_ZOOM, SfxCallMode::SYNCHRON, aSet);
     315           1 :         m_pViewShell->Execute(aZoom);
     316           1 :         nFinalZoom = rGraphicWindow.GetZoom();
     317           2 :         CPPUNIT_ASSERT_MESSAGE("Should be 50%", nFinalZoom == 50);
     318             :     }
     319             : 
     320             :     {
     321           1 :         SfxItemSet aSet(SmDocShell::GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
     322           1 :         aSet.Put(SvxZoomItem(SvxZoomType::PERCENT, 5));
     323           2 :         SfxRequest aZoom(SID_ATTR_ZOOM, SfxCallMode::SYNCHRON, aSet);
     324           1 :         m_pViewShell->Execute(aZoom);
     325           1 :         nFinalZoom = rGraphicWindow.GetZoom();
     326           2 :         CPPUNIT_ASSERT_MESSAGE("Should be Clipped to 25%", nFinalZoom == 25);
     327             :     }
     328             : 
     329             :     {
     330           1 :         SfxItemSet aSet(SmDocShell::GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
     331           1 :         aSet.Put(SvxZoomItem(SvxZoomType::PERCENT, 1000));
     332           2 :         SfxRequest aZoom(SID_ATTR_ZOOM, SfxCallMode::SYNCHRON, aSet);
     333           1 :         m_pViewShell->Execute(aZoom);
     334           1 :         nFinalZoom = rGraphicWindow.GetZoom();
     335           2 :         CPPUNIT_ASSERT_MESSAGE("Should be Clipped to 800%", nFinalZoom == 800);
     336           1 :     }
     337             : 
     338           1 : }
     339             : 
     340           1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
     341             : 
     342             : }
     343             : 
     344           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     345             : 
     346             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11