2013-10-10 12:08:22 +02:00
|
|
|
/*
|
|
|
|
* 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>
|
2013-10-28 11:15:43 +01:00
|
|
|
#include <wrtsh.hxx>
|
2014-01-16 11:00:36 +01:00
|
|
|
#include <crsskip.hxx>
|
|
|
|
#include <shellio.hxx>
|
2013-10-10 12:08:22 +02:00
|
|
|
|
|
|
|
#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();
|
2013-10-28 11:15:43 +01:00
|
|
|
void testFdo69893();
|
2014-01-09 22:33:09 -02:00
|
|
|
void testFdo70807();
|
2014-01-16 11:00:36 +01:00
|
|
|
void testImportRTF();
|
2014-01-16 15:07:01 +01:00
|
|
|
void testExportRTF();
|
2013-10-10 12:08:22 +02:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(SwUiWriterTest);
|
|
|
|
CPPUNIT_TEST(testReplaceForward);
|
|
|
|
CPPUNIT_TEST(testReplaceBackward);
|
2013-10-28 11:15:43 +01:00
|
|
|
CPPUNIT_TEST(testFdo69893);
|
2014-01-09 22:33:09 -02:00
|
|
|
CPPUNIT_TEST(testFdo70807);
|
2014-01-16 11:00:36 +01:00
|
|
|
CPPUNIT_TEST(testImportRTF);
|
2014-01-16 15:07:01 +01:00
|
|
|
CPPUNIT_TEST(testExportRTF);
|
2013-10-10 12:08:22 +02:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
private:
|
2013-10-28 11:45:47 +01:00
|
|
|
SwDoc* createDoc(const char* pName = 0);
|
2013-10-10 12:08:22 +02:00
|
|
|
};
|
|
|
|
|
2013-10-28 11:45:47 +01:00
|
|
|
SwDoc* SwUiWriterTest::createDoc(const char* pName)
|
2013-10-10 12:08:22 +02:00
|
|
|
{
|
2013-10-28 11:45:47 +01:00
|
|
|
if (!pName)
|
|
|
|
pName = "empty.odt";
|
|
|
|
load(DATA_DIRECTORY, pName);
|
2013-10-10 12:08:22 +02:00
|
|
|
|
|
|
|
SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get());
|
|
|
|
return pTxtDoc->GetDocShell()->GetDoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Replacement tests
|
|
|
|
|
2013-11-12 00:46:22 +01:00
|
|
|
static void lcl_selectCharacters(SwPaM& rPaM, sal_Int32 first, sal_Int32 end)
|
2013-10-10 12:08:22 +02:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2013-10-28 11:45:47 +01:00
|
|
|
SwDoc* pDoc = createDoc();
|
2013-10-10 12:08:22 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2013-10-28 11:45:47 +01:00
|
|
|
SwDoc* pDoc = createDoc();
|
2013-10-10 12:08:22 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2013-10-28 11:15:43 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2014-01-09 22:33:09 -02:00
|
|
|
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);
|
|
|
|
|
2014-01-10 12:53:03 -02:00
|
|
|
sal_Bool expectedUsedStyle = sal_False;
|
|
|
|
sal_Bool expectedUserDefined = sal_False;
|
2014-01-09 22:33:09 -02:00
|
|
|
|
2014-01-10 12:53:03 -02:00
|
|
|
OUString styleName(xName->getName());
|
2014-01-09 22:33:09 -02:00
|
|
|
|
2014-01-10 12:53:03 -02:00
|
|
|
// just these styles are user defined styles
|
|
|
|
if (styleName == "pagestyle1" || styleName == "pagestyle2")
|
|
|
|
expectedUserDefined = sal_True;
|
|
|
|
|
|
|
|
// just these styles are used in the document
|
|
|
|
if (styleName == "Right Page" || styleName == "pagestyle1" || styleName == "pagestyle2")
|
|
|
|
expectedUsedStyle = sal_True;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(expectedUserDefined, xStyle->isUserDefined());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(expectedUsedStyle, xStyle->isInUse());
|
2014-01-09 22:33:09 -02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 11:00:36 +01:00
|
|
|
void SwUiWriterTest::testImportRTF()
|
|
|
|
{
|
|
|
|
// Insert "foobar" and position the cursor between "foo" and "bar".
|
|
|
|
SwDoc* pDoc = createDoc();
|
|
|
|
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
|
|
|
|
pWrtShell->Insert("foobar");
|
|
|
|
pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/false, 3, /*bBasicCall=*/false);
|
|
|
|
|
|
|
|
// Insert the RTF at the cursor position.
|
|
|
|
OString aData = "{\\rtf1 Hello world!\\par}";
|
|
|
|
SvMemoryStream aStream(const_cast<sal_Char*>(aData.getStr()), aData.getLength(), STREAM_READ);
|
|
|
|
SwReader aReader(aStream, OUString(), OUString(), *pWrtShell->GetCrsr());
|
|
|
|
Reader* pRTFReader = SwReaderWriter::GetReader(READER_WRITER_RTF);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uLong(0), aReader.Read(*pRTFReader));
|
|
|
|
|
|
|
|
sal_uLong nIndex = pWrtShell->GetCrsr()->GetNode()->GetIndex();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(OUString("fooHello world!"), static_cast<SwTxtNode*>(pDoc->GetNodes()[nIndex - 1])->GetTxt());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(OUString("bar"), static_cast<SwTxtNode*>(pDoc->GetNodes()[nIndex])->GetTxt());
|
|
|
|
}
|
|
|
|
|
2014-01-16 15:07:01 +01:00
|
|
|
void SwUiWriterTest::testExportRTF()
|
|
|
|
{
|
|
|
|
// Insert "aaabbbccc" and select "bbb".
|
|
|
|
SwDoc* pDoc = createDoc();
|
|
|
|
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
|
|
|
|
pWrtShell->Insert("aaabbbccc");
|
|
|
|
pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/false, 3, /*bBasicCall=*/false);
|
|
|
|
pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/true, 3, /*bBasicCall=*/false);
|
|
|
|
|
|
|
|
// Create the clipboard document.
|
|
|
|
boost::shared_ptr<SwDoc> pClpDoc(new SwDoc());
|
|
|
|
pClpDoc->SetClipBoard(true);
|
|
|
|
pWrtShell->Copy(pClpDoc.get());
|
|
|
|
|
|
|
|
// And finally export it as RTF.
|
|
|
|
WriterRef xWrt;
|
|
|
|
SwReaderWriter::GetWriter("RTF", OUString(), xWrt);
|
|
|
|
SvMemoryStream aStream;
|
|
|
|
SwWriter aWrt(aStream, *pClpDoc);
|
|
|
|
aWrt.Write(xWrt);
|
|
|
|
|
|
|
|
OString aData(static_cast<const sal_Char*>(aStream.GetBuffer()), aStream.GetSize());
|
|
|
|
CPPUNIT_ASSERT(aData.startsWith("{\\rtf1"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(-1, aData.indexOf("aaa"));
|
|
|
|
CPPUNIT_ASSERT(aData.indexOf("bbb") != -1);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(-1, aData.indexOf("ccc"));
|
|
|
|
}
|
|
|
|
|
2013-10-10 12:08:22 +02:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(SwUiWriterTest);
|
|
|
|
CPPUNIT_PLUGIN_IMPLEMENT();
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|