Line data Source code
1 : /*
2 : * This file is part of the LibreOffice project.
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : */
8 :
9 : #include <swmodeltestbase.hxx>
10 : #include <ndtxt.hxx>
11 : #include <wrtsh.hxx>
12 : #include <crsskip.hxx>
13 : #include <shellio.hxx>
14 : #include <expfld.hxx>
15 :
16 : #include "UndoManager.hxx"
17 :
18 : static const char* DATA_DIRECTORY = "/sw/qa/extras/uiwriter/data/";
19 :
20 27 : class SwUiWriterTest : public SwModelTestBase
21 : {
22 :
23 : public:
24 : void testReplaceForward();
25 : //Regression test of fdo#70143
26 : //EDITING: undo search&replace corrupt text when searching backward
27 : void testReplaceBackward();
28 : void testFdo69893();
29 : void testFdo70807();
30 : void testImportRTF();
31 : void testExportRTF();
32 : void testFdo75110();
33 : void testFdo75898();
34 : void testFdo74981();
35 :
36 2 : CPPUNIT_TEST_SUITE(SwUiWriterTest);
37 1 : CPPUNIT_TEST(testReplaceForward);
38 1 : CPPUNIT_TEST(testReplaceBackward);
39 1 : CPPUNIT_TEST(testFdo69893);
40 1 : CPPUNIT_TEST(testFdo70807);
41 1 : CPPUNIT_TEST(testImportRTF);
42 1 : CPPUNIT_TEST(testExportRTF);
43 1 : CPPUNIT_TEST(testFdo75110);
44 1 : CPPUNIT_TEST(testFdo75898);
45 1 : CPPUNIT_TEST(testFdo74981);
46 2 : CPPUNIT_TEST_SUITE_END();
47 :
48 : private:
49 : SwDoc* createDoc(const char* pName = 0);
50 : };
51 :
52 8 : SwDoc* SwUiWriterTest::createDoc(const char* pName)
53 : {
54 8 : if (!pName)
55 5 : pName = "empty.odt";
56 8 : load(DATA_DIRECTORY, pName);
57 :
58 8 : SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get());
59 8 : CPPUNIT_ASSERT(pTxtDoc);
60 8 : return pTxtDoc->GetDocShell()->GetDoc();
61 : }
62 :
63 : //Replacement tests
64 :
65 2 : static void lcl_selectCharacters(SwPaM& rPaM, sal_Int32 first, sal_Int32 end)
66 : {
67 2 : rPaM.GetPoint()->nContent.Assign(rPaM.GetCntntNode(), first);
68 2 : rPaM.SetMark();
69 2 : rPaM.GetPoint()->nContent.Assign(rPaM.GetCntntNode(), end);
70 2 : }
71 :
72 1 : static const OUString ORIGINAL_REPLACE_CONTENT("toto titi tutu");
73 1 : static const OUString EXPECTED_REPLACE_CONTENT("toto toto tutu");
74 :
75 1 : void SwUiWriterTest::testReplaceForward()
76 : {
77 1 : SwDoc* pDoc = createDoc();
78 :
79 1 : sw::UndoManager& rUndoManager = pDoc->GetUndoManager();
80 :
81 1 : SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
82 2 : SwPaM aPaM(aIdx);
83 :
84 1 : pDoc->InsertString(aPaM, ORIGINAL_REPLACE_CONTENT);
85 :
86 1 : SwTxtNode* pTxtNode = aPaM.GetNode()->GetTxtNode();
87 1 : lcl_selectCharacters(aPaM, 5, 9);
88 1 : pDoc->ReplaceRange(aPaM, OUString("toto"), false);
89 :
90 1 : CPPUNIT_ASSERT_EQUAL(EXPECTED_REPLACE_CONTENT, pTxtNode->GetTxt());
91 :
92 1 : rUndoManager.Undo();
93 :
94 2 : CPPUNIT_ASSERT_EQUAL(ORIGINAL_REPLACE_CONTENT, pTxtNode->GetTxt());
95 1 : }
96 :
97 1 : void SwUiWriterTest::testFdo75110()
98 : {
99 1 : SwDoc* pDoc = createDoc("fdo75110.odt");
100 1 : SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
101 :
102 1 : pWrtShell->SelAll();
103 : // The problem was that SwEditShell::DeleteSel() what this Delete() invokes took the wrong selection...
104 1 : pWrtShell->Delete();
105 1 : sw::UndoManager& rUndoManager = pDoc->GetUndoManager();
106 : // ... so this Undo() call resulted in a crash.
107 1 : rUndoManager.Undo();
108 1 : }
109 :
110 1 : void SwUiWriterTest::testFdo75898()
111 : {
112 1 : SwDoc* pDoc = createDoc("fdo75898.odt");
113 1 : SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
114 1 : pWrtShell->SelAll();
115 1 : pWrtShell->InsertRow(1, true);
116 1 : pWrtShell->InsertRow(1, true);
117 :
118 : // Now check if the table has 3 lines.
119 1 : SwShellCrsr* pShellCrsr = pWrtShell->getShellCrsr(false);
120 1 : SwTableNode* pTableNode = pShellCrsr->Start()->nNode.GetNode().FindTableNode();
121 : // This was 1, when doing the same using the UI, Writer even crashed.
122 1 : CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), pTableNode->GetTable().GetTabLines().size());
123 1 : }
124 :
125 1 : void SwUiWriterTest::testReplaceBackward()
126 : {
127 1 : SwDoc* pDoc = createDoc();
128 :
129 1 : sw::UndoManager& rUndoManager = pDoc->GetUndoManager();
130 :
131 1 : SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
132 2 : SwPaM aPaM(aIdx);
133 :
134 1 : pDoc->InsertString(aPaM, OUString("toto titi tutu"));
135 1 : SwTxtNode* pTxtNode = aPaM.GetNode()->GetTxtNode();
136 1 : lcl_selectCharacters(aPaM, 9, 5);
137 :
138 1 : pDoc->ReplaceRange(aPaM, OUString("toto"), false);
139 :
140 1 : CPPUNIT_ASSERT_EQUAL(EXPECTED_REPLACE_CONTENT, pTxtNode->GetTxt());
141 :
142 1 : rUndoManager.Undo();
143 :
144 2 : CPPUNIT_ASSERT_EQUAL(ORIGINAL_REPLACE_CONTENT, pTxtNode->GetTxt());
145 1 : }
146 :
147 1 : void SwUiWriterTest::testFdo69893()
148 : {
149 1 : SwDoc* pDoc = createDoc("fdo69893.odt");
150 1 : SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
151 :
152 1 : pWrtShell->SelAll();
153 :
154 1 : SwShellCrsr* pShellCrsr = pWrtShell->getShellCrsr(false);
155 1 : SwTxtNode& rEnd = dynamic_cast<SwTxtNode&>(pShellCrsr->End()->nNode.GetNode());
156 : // Selection did not include the para after table, this was "B1".
157 1 : CPPUNIT_ASSERT_EQUAL(OUString("Para after table."), rEnd.GetTxt());
158 1 : }
159 :
160 1 : void SwUiWriterTest::testFdo70807()
161 : {
162 1 : load(DATA_DIRECTORY, "fdo70807.odt");
163 :
164 1 : uno::Reference<container::XIndexAccess> stylesIter(getStyles("PageStyles"), uno::UNO_QUERY);
165 :
166 13 : for (sal_Int32 i = 0; i < stylesIter->getCount(); ++i)
167 : {
168 12 : uno::Reference<style::XStyle> xStyle(stylesIter->getByIndex(i), uno::UNO_QUERY);
169 24 : uno::Reference<container::XNamed> xName(xStyle, uno::UNO_QUERY);
170 :
171 12 : sal_Bool expectedUsedStyle = sal_False;
172 12 : sal_Bool expectedUserDefined = sal_False;
173 :
174 24 : OUString styleName(xName->getName());
175 :
176 : // just these styles are user defined styles
177 12 : if (styleName == "pagestyle1" || styleName == "pagestyle2")
178 2 : expectedUserDefined = sal_True;
179 :
180 : // just these styles are used in the document
181 12 : if (styleName == "Right Page" || styleName == "pagestyle1" || styleName == "pagestyle2")
182 3 : expectedUsedStyle = sal_True;
183 :
184 12 : CPPUNIT_ASSERT_EQUAL(expectedUserDefined, xStyle->isUserDefined());
185 12 : CPPUNIT_ASSERT_EQUAL(expectedUsedStyle, xStyle->isInUse());
186 13 : }
187 1 : }
188 :
189 1 : void SwUiWriterTest::testImportRTF()
190 : {
191 : // Insert "foobar" and position the cursor between "foo" and "bar".
192 1 : SwDoc* pDoc = createDoc();
193 1 : SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
194 1 : pWrtShell->Insert("foobar");
195 1 : pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/false, 3, /*bBasicCall=*/false);
196 :
197 : // Insert the RTF at the cursor position.
198 1 : OString aData = "{\\rtf1 Hello world!\\par}";
199 2 : SvMemoryStream aStream(const_cast<sal_Char*>(aData.getStr()), aData.getLength(), STREAM_READ);
200 2 : SwReader aReader(aStream, OUString(), OUString(), *pWrtShell->GetCrsr());
201 1 : Reader* pRTFReader = SwReaderWriter::GetReader(READER_WRITER_RTF);
202 1 : CPPUNIT_ASSERT(pRTFReader != 0);
203 1 : CPPUNIT_ASSERT_EQUAL(sal_uLong(0), aReader.Read(*pRTFReader));
204 :
205 1 : sal_uLong nIndex = pWrtShell->GetCrsr()->GetNode()->GetIndex();
206 1 : CPPUNIT_ASSERT_EQUAL(OUString("fooHello world!"), static_cast<SwTxtNode*>(pDoc->GetNodes()[nIndex - 1])->GetTxt());
207 2 : CPPUNIT_ASSERT_EQUAL(OUString("bar"), static_cast<SwTxtNode*>(pDoc->GetNodes()[nIndex])->GetTxt());
208 1 : }
209 :
210 1 : void SwUiWriterTest::testExportRTF()
211 : {
212 : // Insert "aaabbbccc" and select "bbb".
213 1 : SwDoc* pDoc = createDoc();
214 1 : SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
215 1 : pWrtShell->Insert("aaabbbccc");
216 1 : pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/false, 3, /*bBasicCall=*/false);
217 1 : pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/true, 3, /*bBasicCall=*/false);
218 :
219 : // Create the clipboard document.
220 1 : boost::shared_ptr<SwDoc> pClpDoc(new SwDoc());
221 1 : pClpDoc->SetClipBoard(true);
222 1 : pWrtShell->Copy(pClpDoc.get());
223 :
224 : // And finally export it as RTF.
225 2 : WriterRef xWrt;
226 1 : SwReaderWriter::GetWriter("RTF", OUString(), xWrt);
227 2 : SvMemoryStream aStream;
228 2 : SwWriter aWrt(aStream, *pClpDoc);
229 1 : aWrt.Write(xWrt);
230 :
231 2 : OString aData(static_cast<const sal_Char*>(aStream.GetBuffer()), aStream.GetSize());
232 1 : CPPUNIT_ASSERT(aData.startsWith("{\\rtf1"));
233 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aData.indexOf("aaa"));
234 1 : CPPUNIT_ASSERT(aData.indexOf("bbb") != -1);
235 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aData.indexOf("ccc"));
236 1 : }
237 :
238 1 : void SwUiWriterTest::testFdo74981()
239 : {
240 : // create a document with an input field
241 1 : SwDoc* pDoc = createDoc();
242 1 : SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
243 1 : SwInputField aField((SwInputFieldType*)pWrtShell->GetFldType(0, RES_INPUTFLD), OUString("foo"), OUString("bar"), 0, 0);
244 1 : pWrtShell->Insert(aField);
245 :
246 : // expect hints
247 2 : SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
248 1 : SwTxtNode* pTxtNode = aIdx.GetNode().GetTxtNode();
249 1 : CPPUNIT_ASSERT(pTxtNode->HasHints());
250 :
251 : // go to the begin of the paragraph and split this node
252 1 : pWrtShell->Left(CRSR_SKIP_CHARS, false, 100, false);
253 1 : pWrtShell->SplitNode();
254 :
255 : // expect only the second paragraph to have hints
256 1 : aIdx = SwNodeIndex(pDoc->GetNodes().GetEndOfContent(), -1);
257 1 : pTxtNode = aIdx.GetNode().GetTxtNode();
258 1 : CPPUNIT_ASSERT(pTxtNode->HasHints());
259 1 : aIdx--;
260 1 : pTxtNode = aIdx.GetNode().GetTxtNode();
261 2 : CPPUNIT_ASSERT(!pTxtNode->HasHints());
262 1 : }
263 :
264 1 : CPPUNIT_TEST_SUITE_REGISTRATION(SwUiWriterTest);
265 4 : CPPUNIT_PLUGIN_IMPLEMENT();
266 :
267 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|