Files
libreoffice/sw/qa/extras/uiwriter/uiwriter.cxx

143 lines
4.0 KiB
C++
Raw Normal View History

/*
* 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/.
*/
#include <swmodeltestbase.hxx>
#include <ndtxt.hxx>
#include <wrtsh.hxx>
#include "UndoManager.hxx"
static const char* DATA_DIRECTORY = "/sw/qa/extras/uiwriter/data/";
class SwUiWriterTest : public SwModelTestBase
{
public:
void testReplaceForward();
//Regression test of fdo#70143
//EDITING: undo search&replace corrupt text when searching backward
void testReplaceBackward();
void testFdo69893();
void testFdo70807();
CPPUNIT_TEST_SUITE(SwUiWriterTest);
CPPUNIT_TEST(testReplaceForward);
CPPUNIT_TEST(testReplaceBackward);
CPPUNIT_TEST(testFdo69893);
CPPUNIT_TEST(testFdo70807);
CPPUNIT_TEST_SUITE_END();
private:
SwDoc* createDoc(const char* pName = 0);
};
SwDoc* SwUiWriterTest::createDoc(const char* pName)
{
if (!pName)
pName = "empty.odt";
load(DATA_DIRECTORY, pName);
SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get());
return pTxtDoc->GetDocShell()->GetDoc();
}
//Replacement tests
static void lcl_selectCharacters(SwPaM& rPaM, sal_Int32 first, sal_Int32 end)
{
rPaM.GetPoint()->nContent.Assign(rPaM.GetCntntNode(), first);
rPaM.SetMark();
rPaM.GetPoint()->nContent.Assign(rPaM.GetCntntNode(), end);
}
static const OUString ORIGINAL_REPLACE_CONTENT("toto titi tutu");
static const OUString EXPECTED_REPLACE_CONTENT("toto toto tutu");
void SwUiWriterTest::testReplaceForward()
{
SwDoc* pDoc = createDoc();
sw::UndoManager& rUndoManager = pDoc->GetUndoManager();
SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
SwPaM aPaM(aIdx);
pDoc->InsertString(aPaM, ORIGINAL_REPLACE_CONTENT);
SwTxtNode* pTxtNode = aPaM.GetNode()->GetTxtNode();
lcl_selectCharacters(aPaM, 5, 9);
pDoc->ReplaceRange(aPaM, OUString("toto"), false);
CPPUNIT_ASSERT_EQUAL(EXPECTED_REPLACE_CONTENT, pTxtNode->GetTxt());
rUndoManager.Undo();
CPPUNIT_ASSERT_EQUAL(ORIGINAL_REPLACE_CONTENT, pTxtNode->GetTxt());
}
void SwUiWriterTest::testReplaceBackward()
{
SwDoc* pDoc = createDoc();
sw::UndoManager& rUndoManager = pDoc->GetUndoManager();
SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
SwPaM aPaM(aIdx);
pDoc->InsertString(aPaM, OUString("toto titi tutu"));
SwTxtNode* pTxtNode = aPaM.GetNode()->GetTxtNode();
lcl_selectCharacters(aPaM, 9, 5);
pDoc->ReplaceRange(aPaM, OUString("toto"), false);
CPPUNIT_ASSERT_EQUAL(EXPECTED_REPLACE_CONTENT, pTxtNode->GetTxt());
rUndoManager.Undo();
CPPUNIT_ASSERT_EQUAL(ORIGINAL_REPLACE_CONTENT, pTxtNode->GetTxt());
}
void SwUiWriterTest::testFdo69893()
{
SwDoc* pDoc = createDoc("fdo69893.odt");
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->SelAll();
SwShellCrsr* pShellCrsr = pWrtShell->getShellCrsr(false);
SwTxtNode& rEnd = dynamic_cast<SwTxtNode&>(pShellCrsr->End()->nNode.GetNode());
// Selection did not include the para after table, this was "B1".
CPPUNIT_ASSERT_EQUAL(OUString("Para after table."), rEnd.GetTxt());
}
void SwUiWriterTest::testFdo70807()
{
load(DATA_DIRECTORY, "fdo70807.odt");
uno::Reference<container::XIndexAccess> stylesIter(getStyles("PageStyles"), uno::UNO_QUERY);
for (sal_Int32 i = 0; i < stylesIter->getCount(); ++i)
{
uno::Reference<style::XStyle> xStyle(stylesIter->getByIndex(i), uno::UNO_QUERY);
uno::Reference<container::XNamed> xName(xStyle, uno::UNO_QUERY);
sal_Bool isUsed = xStyle->isInUse();
sal_Bool used = sal_False; // just "Right Page" is used
if (xName->getName() == "Right Page")
used = sal_True;
CPPUNIT_ASSERT_EQUAL(used, isUsed);
}
}
CPPUNIT_TEST_SUITE_REGISTRATION(SwUiWriterTest);
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */