Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * Thorsten Behrens <tbehrens@novell.com>
17 : * Portions created by the Initial Developer are Copyright (C) 2011 the
18 : * Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s):
21 : * Thorsten Behrens <tbehrens@novell.com>
22 : * Caolán McNamara <caolanm@redhat.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
26 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
27 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
28 : * instead of those above.
29 : */
30 :
31 : #include <sal/config.h>
32 : #include <test/bootstrapfixture.hxx>
33 :
34 : #include <rtl/strbuf.hxx>
35 : #include <osl/file.hxx>
36 :
37 : #include <tools/urlobj.hxx>
38 : #include <unotools/tempfile.hxx>
39 :
40 : #include <editeng/langitem.hxx>
41 : #include <editeng/charhiddenitem.hxx>
42 :
43 : #include <sfx2/app.hxx>
44 : #include <sfx2/docfilt.hxx>
45 : #include <sfx2/docfile.hxx>
46 : #include <sfx2/sfxmodelfactory.hxx>
47 :
48 : #include "breakit.hxx"
49 : #include "doc.hxx"
50 : #include "docsh.hxx"
51 : #include "docstat.hxx"
52 : #include "docufld.hxx"
53 : #include "fmtanchr.hxx"
54 : #include "init.hxx"
55 : #include "ndtxt.hxx"
56 : #include "shellio.hxx"
57 : #include "shellres.hxx"
58 : #include "swcrsr.hxx"
59 : #include "swscanner.hxx"
60 : #include "swmodule.hxx"
61 : #include "swtypes.hxx"
62 : #include "fmtftn.hxx"
63 : #include "fmtrfmrk.hxx"
64 : #include "fmtfld.hxx"
65 : #include "redline.hxx"
66 : #include "docary.hxx"
67 : #include "modeltoviewhelper.hxx"
68 : #include "scriptinfo.hxx"
69 :
70 20 : SV_DECL_REF(SwDocShell)
71 48 : SV_IMPL_REF(SwDocShell)
72 :
73 : using namespace ::com::sun::star;
74 :
75 : /* Implementation of Swdoc-Test class */
76 :
77 24 : class SwDocTest : public test::BootstrapFixture
78 : {
79 : public:
80 : virtual void setUp();
81 : virtual void tearDown();
82 :
83 : void randomTest();
84 : void testPageDescName();
85 : void testFileNameFields();
86 : void testDocStat();
87 : void testModelToViewHelper();
88 : void testSwScanner();
89 : void testUserPerceivedCharCount();
90 : void testGraphicAnchorDeletion();
91 :
92 2 : CPPUNIT_TEST_SUITE(SwDocTest);
93 1 : CPPUNIT_TEST(randomTest);
94 1 : CPPUNIT_TEST(testPageDescName);
95 1 : CPPUNIT_TEST(testFileNameFields);
96 1 : CPPUNIT_TEST(testDocStat);
97 1 : CPPUNIT_TEST(testModelToViewHelper);
98 1 : CPPUNIT_TEST(testSwScanner);
99 1 : CPPUNIT_TEST(testUserPerceivedCharCount);
100 1 : CPPUNIT_TEST(testGraphicAnchorDeletion);
101 2 : CPPUNIT_TEST_SUITE_END();
102 :
103 : private:
104 : SwDoc *m_pDoc;
105 : SwDocShellRef m_xDocShRef;
106 : };
107 :
108 1 : void SwDocTest::testPageDescName()
109 : {
110 1 : ShellResource aShellResources;
111 :
112 1 : std::vector<rtl::OUString> aResults;
113 :
114 : //These names must be unique for each different combination, otherwise
115 : //duplicate page description names may exist, which will causes lookup
116 : //by name to be incorrect, and so the corresponding export to .odt
117 1 : aResults.push_back(aShellResources.GetPageDescName(1, ShellResource::NORMAL_PAGE));
118 1 : aResults.push_back(aShellResources.GetPageDescName(1, ShellResource::FIRST_PAGE));
119 1 : aResults.push_back(aShellResources.GetPageDescName(1, ShellResource::FOLLOW_PAGE));
120 :
121 1 : std::sort(aResults.begin(), aResults.end());
122 1 : aResults.erase(std::unique(aResults.begin(), aResults.end()), aResults.end());
123 :
124 1 : CPPUNIT_ASSERT_MESSAGE("GetPageDescName results must be unique", aResults.size() == 3);
125 1 : }
126 :
127 : //See https://bugs.freedesktop.org/show_bug.cgi?id=32463
128 1 : void SwDocTest::testFileNameFields()
129 : {
130 : //Here's a file name with some chars in it that will be %% encoded, when expanding
131 : //SwFileNameFields we want to restore the original readable filename
132 1 : utl::TempFile aTempFile(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("demo [name]")));
133 1 : aTempFile.EnableKillingFile();
134 :
135 1 : INetURLObject aTempFileURL(aTempFile.GetURL());
136 1 : String sFileURL = aTempFileURL.GetMainURL(INetURLObject::NO_DECODE);
137 1 : SfxMedium aDstMed(sFileURL, STREAM_STD_READWRITE);
138 :
139 : SfxFilter aFilter(
140 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Text")),
141 : rtl::OUString(), 0, 0, rtl::OUString(), 0, rtl::OUString(),
142 1 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TEXT")), rtl::OUString() );
143 1 : aDstMed.SetFilter(&aFilter);
144 :
145 1 : m_xDocShRef->DoSaveAs(aDstMed);
146 1 : m_xDocShRef->DoSaveCompleted(&aDstMed);
147 :
148 1 : const INetURLObject &rUrlObj = m_xDocShRef->GetMedium()->GetURLObject();
149 :
150 1 : SwFileNameFieldType aNameField(m_pDoc);
151 :
152 : {
153 1 : rtl::OUString sResult(aNameField.Expand(FF_NAME));
154 : rtl::OUString sExpected(rUrlObj.getName(INetURLObject::LAST_SEGMENT,
155 1 : true,INetURLObject::DECODE_WITH_CHARSET));
156 1 : CPPUNIT_ASSERT_MESSAGE("Expected Readable FileName", sResult == sExpected);
157 : }
158 :
159 : {
160 1 : rtl::OUString sResult(aNameField.Expand(FF_PATHNAME));
161 1 : rtl::OUString sExpected(rUrlObj.GetFull());
162 1 : CPPUNIT_ASSERT_MESSAGE("Expected Readable FileName", sResult == sExpected);
163 : }
164 :
165 : {
166 1 : rtl::OUString sResult(aNameField.Expand(FF_PATH));
167 1 : INetURLObject aTemp(rUrlObj);
168 1 : aTemp.removeSegment();
169 1 : rtl::OUString sExpected(aTemp.PathToFileName());
170 1 : CPPUNIT_ASSERT_MESSAGE("Expected Readable FileName", sResult == sExpected);
171 : }
172 :
173 : {
174 1 : rtl::OUString sResult(aNameField.Expand(FF_NAME_NOEXT));
175 : rtl::OUString sExpected(rUrlObj.getName(INetURLObject::LAST_SEGMENT,
176 1 : true,INetURLObject::DECODE_WITH_CHARSET));
177 : //Chop off .tmp
178 1 : sExpected = sExpected.copy(0, sExpected.getLength() - 4);
179 1 : CPPUNIT_ASSERT_MESSAGE("Expected Readable FileName", sResult == sExpected);
180 : }
181 :
182 1 : m_xDocShRef->DoInitNew(0);
183 1 : }
184 :
185 : //See http://lists.freedesktop.org/archives/libreoffice/2011-August/016666.html
186 : //Remove unnecessary parameter to IDocumentStatistics::UpdateDocStat for
187 : //motivation
188 1 : void SwDocTest::testDocStat()
189 : {
190 1 : CPPUNIT_ASSERT_MESSAGE("Expected initial 0 count", m_pDoc->GetDocStat().nChar == 0);
191 :
192 1 : SwNodeIndex aIdx(m_pDoc->GetNodes().GetEndOfContent(), -1);
193 1 : SwPaM aPaM(aIdx);
194 :
195 1 : rtl::OUString sText(RTL_CONSTASCII_USTRINGPARAM("Hello World"));
196 1 : m_pDoc->InsertString(aPaM, sText);
197 :
198 1 : CPPUNIT_ASSERT_MESSAGE("Should still be non-updated 0 count", m_pDoc->GetDocStat().nChar == 0);
199 :
200 1 : SwDocStat aDocStat = m_pDoc->GetUpdatedDocStat();
201 1 : sal_uLong nLen = static_cast<sal_uLong>(sText.getLength());
202 :
203 1 : CPPUNIT_ASSERT_MESSAGE("Should now have updated count", aDocStat.nChar == nLen);
204 :
205 1 : CPPUNIT_ASSERT_MESSAGE("And cache is updated too", m_pDoc->GetDocStat().nChar == nLen);
206 1 : }
207 :
208 : //For UI character counts we should follow UAX#29 and display the user
209 : //perceived characters, not the number of codepoints, nor the number of code
210 : //units http://unicode.org/reports/tr29/
211 1 : void SwDocTest::testUserPerceivedCharCount()
212 : {
213 1 : SwBreakIt *pBreakIter = SwBreakIt::Get();
214 :
215 : //Grapheme example, two different unicode code-points perceived by the user as a single
216 : //glyph
217 1 : const sal_Unicode ALEF_QAMATS [] = { 0x05D0, 0x05B8 };
218 1 : ::rtl::OUString sALEF_QAMATS(ALEF_QAMATS, SAL_N_ELEMENTS(ALEF_QAMATS));
219 1 : sal_Int32 nGraphemeCount = pBreakIter->getGraphemeCount(sALEF_QAMATS);
220 1 : CPPUNIT_ASSERT_MESSAGE("Grapheme Count should be 1", nGraphemeCount == 1);
221 :
222 : //Surrogate pair example, one single unicode code-point (U+1D11E)
223 : //represented as two code units in UTF-16
224 1 : const sal_Unicode GCLEF[] = { 0xD834, 0xDD1E };
225 1 : ::rtl::OUString sGCLEF(GCLEF, SAL_N_ELEMENTS(GCLEF));
226 1 : sal_Int32 nCount = pBreakIter->getGraphemeCount(sGCLEF);
227 1 : CPPUNIT_ASSERT_MESSAGE("Surrogate Pair should be counted as single character", nCount == 1);
228 1 : }
229 :
230 1 : void SwDocTest::testModelToViewHelper()
231 : {
232 1 : SwNodeIndex aIdx(m_pDoc->GetNodes().GetEndOfContent(), -1);
233 1 : SwPaM aPaM(aIdx);
234 :
235 : {
236 1 : SwFmtFtn aFtn;
237 1 : aFtn.SetNumStr(rtl::OUString("foo"));
238 :
239 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
240 1 : m_pDoc->InsertString(aPaM, rtl::OUString("AAAAA BBBBB "));
241 1 : SwTxtNode* pTxtNode = aPaM.GetNode()->GetTxtNode();
242 1 : xub_StrLen nPos = aPaM.GetPoint()->nContent.GetIndex();
243 1 : pTxtNode->InsertItem(aFtn, nPos, nPos);
244 1 : m_pDoc->InsertString(aPaM, rtl::OUString(" CCCCC "));
245 1 : nPos = aPaM.GetPoint()->nContent.GetIndex();
246 1 : pTxtNode->InsertItem(aFtn, nPos, nPos);
247 1 : m_pDoc->InsertString(aPaM, rtl::OUString(" DDDDD"));
248 1 : CPPUNIT_ASSERT(pTxtNode->GetTxt().Len() == (4*5) + 5 + 2);
249 :
250 : //set start of selection to first B
251 1 : aPaM.GetPoint()->nContent.Assign(aPaM.GetCntntNode(), 6);
252 1 : aPaM.SetMark();
253 : //set end of selection to last C
254 1 : aPaM.GetPoint()->nContent.Assign(aPaM.GetCntntNode(), 14);
255 : //set character attribute hidden on range
256 1 : SvxCharHiddenItem aHidden(true, RES_CHRATR_HIDDEN);
257 1 : m_pDoc->InsertPoolItem(aPaM, aHidden, 0 );
258 :
259 : //turn on red-lining and show changes
260 1 : m_pDoc->SetRedlineMode(nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_SHOW_DELETE|nsRedlineMode_t::REDLINE_SHOW_INSERT);
261 1 : CPPUNIT_ASSERT_MESSAGE("redlining should be on", m_pDoc->IsRedlineOn());
262 1 : CPPUNIT_ASSERT_MESSAGE("redlines should be visible", IDocumentRedlineAccess::IsShowChanges(m_pDoc->GetRedlineMode()));
263 :
264 : //set start of selection to last A
265 1 : aPaM.GetPoint()->nContent.Assign(aPaM.GetCntntNode(), 4);
266 1 : aPaM.SetMark();
267 : //set end of selection to second last B
268 1 : aPaM.GetPoint()->nContent.Assign(aPaM.GetCntntNode(), 9);
269 1 : m_pDoc->DeleteAndJoin(aPaM); //redline-aware deletion api
270 :
271 : {
272 1 : ModelToViewHelper aModelToViewHelper(*pTxtNode, PASSTHROUGH);
273 1 : rtl::OUString sViewText = aModelToViewHelper.getViewText();
274 1 : rtl::OUString sModelText = pTxtNode->GetTxt();
275 1 : CPPUNIT_ASSERT(sViewText == sModelText);
276 : }
277 :
278 : {
279 1 : ModelToViewHelper aModelToViewHelper(*pTxtNode, EXPANDFIELDS);
280 1 : rtl::OUString sViewText = aModelToViewHelper.getViewText();
281 1 : CPPUNIT_ASSERT(sViewText == "AAAAA BBBBB foo CCCCC foo DDDDD");
282 : }
283 :
284 : {
285 1 : ModelToViewHelper aModelToViewHelper(*pTxtNode, HIDEINVISIBLE);
286 1 : rtl::OUString sViewText = aModelToViewHelper.getViewText();
287 1 : rtl::OUStringBuffer aBuffer;
288 1 : aBuffer.append("AAAAA CCCCC ");
289 1 : aBuffer.append(CH_TXTATR_BREAKWORD);
290 1 : aBuffer.append(" DDDDD");
291 1 : CPPUNIT_ASSERT(sViewText == aBuffer.makeStringAndClear());
292 : }
293 :
294 : {
295 1 : ModelToViewHelper aModelToViewHelper(*pTxtNode, HIDEREDLINED);
296 1 : rtl::OUString sViewText = aModelToViewHelper.getViewText();
297 1 : rtl::OUStringBuffer aBuffer;
298 1 : aBuffer.append("AAAABB ");
299 1 : aBuffer.append(CH_TXTATR_BREAKWORD);
300 1 : aBuffer.append(" CCCCC ");
301 1 : aBuffer.append(CH_TXTATR_BREAKWORD);
302 1 : aBuffer.append(" DDDDD");
303 1 : CPPUNIT_ASSERT(sViewText == aBuffer.makeStringAndClear());
304 : }
305 :
306 : {
307 1 : ModelToViewHelper aModelToViewHelper(*pTxtNode, EXPANDFIELDS | HIDEINVISIBLE);
308 1 : rtl::OUString sViewText = aModelToViewHelper.getViewText();
309 1 : CPPUNIT_ASSERT(sViewText == "AAAAA CCCCC foo DDDDD");
310 : }
311 :
312 : {
313 1 : ModelToViewHelper aModelToViewHelper(*pTxtNode, EXPANDFIELDS | HIDEREDLINED);
314 1 : rtl::OUString sViewText = aModelToViewHelper.getViewText();
315 1 : CPPUNIT_ASSERT(sViewText == "AAAABB foo CCCCC foo DDDDD");
316 : }
317 :
318 : {
319 1 : ModelToViewHelper aModelToViewHelper(*pTxtNode, HIDEINVISIBLE | HIDEREDLINED);
320 1 : rtl::OUString sViewText = aModelToViewHelper.getViewText();
321 1 : rtl::OUStringBuffer aBuffer;
322 1 : aBuffer.append("AAAACCCCC ");
323 1 : aBuffer.append(CH_TXTATR_BREAKWORD);
324 1 : aBuffer.append(" DDDDD");
325 1 : CPPUNIT_ASSERT(sViewText == aBuffer.makeStringAndClear());
326 : }
327 :
328 : {
329 1 : ModelToViewHelper aModelToViewHelper(*pTxtNode, EXPANDFIELDS | HIDEINVISIBLE | HIDEREDLINED);
330 1 : rtl::OUString sViewText = aModelToViewHelper.getViewText();
331 1 : CPPUNIT_ASSERT(sViewText == "AAAACCCCC foo DDDDD");
332 1 : }
333 1 : }
334 1 : }
335 :
336 1 : void SwDocTest::testSwScanner()
337 : {
338 1 : SwNodeIndex aIdx(m_pDoc->GetNodes().GetEndOfContent(), -1);
339 1 : SwPaM aPaM(aIdx);
340 :
341 1 : SwTxtNode* pTxtNode = aPaM.GetNode()->GetTxtNode();
342 :
343 1 : CPPUNIT_ASSERT_MESSAGE("Has Text Node", pTxtNode);
344 :
345 : //See https://bugs.freedesktop.org/show_bug.cgi?id=40449
346 : //See https://bugs.freedesktop.org/show_bug.cgi?id=39365
347 : //Use a temporary rtl::OUString as the arg, as that's the trouble behind
348 : //fdo#40449 and fdo#39365
349 : {
350 : SwScanner aScanner(*pTxtNode,
351 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Hello World")),
352 : 0, ModelToViewHelper(), i18n::WordType::DICTIONARY_WORD, 0,
353 1 : RTL_CONSTASCII_LENGTH("Hello World"));
354 :
355 1 : bool bFirstOk = aScanner.NextWord();
356 1 : CPPUNIT_ASSERT_MESSAGE("First Token", bFirstOk);
357 1 : const rtl::OUString &rHello = aScanner.GetWord();
358 2 : CPPUNIT_ASSERT_MESSAGE("Should be Hello",
359 1 : rHello.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Hello")));
360 :
361 1 : bool bSecondOk = aScanner.NextWord();
362 1 : CPPUNIT_ASSERT_MESSAGE("Second Token", bSecondOk);
363 1 : const rtl::OUString &rWorld = aScanner.GetWord();
364 2 : CPPUNIT_ASSERT_MESSAGE("Should be World",
365 2 : rWorld.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("World")));
366 : }
367 :
368 : //See https://www.libreoffice.org/bugzilla/show_bug.cgi?id=45271
369 : {
370 1 : const sal_Unicode IDEOGRAPHICFULLSTOP_D[] = { 0x3002, 'D' };
371 :
372 : m_pDoc->InsertString(aPaM, rtl::OUString(IDEOGRAPHICFULLSTOP_D,
373 1 : SAL_N_ELEMENTS(IDEOGRAPHICFULLSTOP_D)));
374 :
375 1 : SvxLanguageItem aCJKLangItem( LANGUAGE_CHINESE_SIMPLIFIED, RES_CHRATR_CJK_LANGUAGE );
376 1 : SvxLanguageItem aWestLangItem( LANGUAGE_ENGLISH_US, RES_CHRATR_LANGUAGE );
377 1 : m_pDoc->InsertPoolItem(aPaM, aCJKLangItem, 0 );
378 1 : m_pDoc->InsertPoolItem(aPaM, aWestLangItem, 0 );
379 :
380 1 : SwDocStat aDocStat;
381 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
382 1 : pTxtNode->CountWords(aDocStat, 0, SAL_N_ELEMENTS(IDEOGRAPHICFULLSTOP_D));
383 :
384 1 : CPPUNIT_ASSERT_MESSAGE("Should be 2", aDocStat.nChar == 2);
385 1 : CPPUNIT_ASSERT_MESSAGE("Should be 2", aDocStat.nCharExcludingSpaces == 2);
386 : }
387 : {
388 : const sal_Unicode test[] =
389 : {
390 : 0x3053, 0x306E, 0x65E5, 0x672C, 0x8A9E, 0x306F, 0x6B63, 0x3057,
391 : 0x304F, 0x6570, 0x3048, 0x3089, 0x308C, 0x308B, 0x3067, 0x3057,
392 : 0x3087, 0x3046, 0x304B, 0x3002, 0x0041, 0x006E, 0x0064, 0x0020,
393 : 0x006C, 0x0065, 0x0074, 0x0027, 0x0073, 0x0020, 0x0074, 0x0068,
394 : 0x0072, 0x006F, 0x0077, 0x0020, 0x0073, 0x006F, 0x006D, 0x0065,
395 : 0x0020, 0x0045, 0x006E, 0x0067, 0x006C, 0x0069, 0x0073, 0x0068,
396 : 0x0020, 0x0069, 0x006E, 0x0020, 0x0074, 0x006F, 0x0020, 0x006D,
397 : 0x0061, 0x006B, 0x0065, 0x0020, 0x0069, 0x0074, 0x0020, 0x0069,
398 : 0x006E, 0x0074, 0x0065, 0x0072, 0x0065, 0x0073, 0x0074, 0x0069,
399 : 0x006E, 0x0067, 0x002E, 0x0020, 0x0020, 0x305D, 0x3057, 0x3066,
400 : 0x3001, 0x307E, 0x305F, 0x65E5, 0x672C, 0x8A9E, 0x3000, 0x3000,
401 : 0x3067, 0x3082, 0x4ECA, 0x56DE, 0x306F, 0x7A7A, 0x767D, 0x3092,
402 : 0x3000, 0x3000, 0x5165, 0x308C, 0x307E, 0x3057, 0x305F, 0x3002,
403 : 0x0020, 0x0020, 0x0053, 0x006F, 0x0020, 0x0068, 0x006F, 0x0077,
404 : 0x0020, 0x0064, 0x006F, 0x0065, 0x0073, 0x0020, 0x0074, 0x0068,
405 : 0x0069, 0x0073, 0x0020, 0x0064, 0x006F, 0x003F, 0x0020, 0x0020
406 1 : };
407 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
408 : m_pDoc->InsertString(aPaM, rtl::OUString(test,
409 1 : SAL_N_ELEMENTS(test)));
410 :
411 1 : SvxLanguageItem aCJKLangItem( LANGUAGE_JAPANESE, RES_CHRATR_CJK_LANGUAGE );
412 1 : SvxLanguageItem aWestLangItem( LANGUAGE_ENGLISH_US, RES_CHRATR_LANGUAGE );
413 1 : m_pDoc->InsertPoolItem(aPaM, aCJKLangItem, 0 );
414 1 : m_pDoc->InsertPoolItem(aPaM, aWestLangItem, 0 );
415 :
416 1 : SwDocStat aDocStat;
417 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
418 1 : pTxtNode->CountWords(aDocStat, 0, SAL_N_ELEMENTS(test));
419 1 : CPPUNIT_ASSERT_MESSAGE("58 words", aDocStat.nWord == 58);
420 1 : CPPUNIT_ASSERT_MESSAGE("43 Asian characters and Korean syllables", aDocStat.nAsianWord == 43);
421 1 : CPPUNIT_ASSERT_MESSAGE("105 non-whitespace chars", aDocStat.nCharExcludingSpaces == 105);
422 1 : CPPUNIT_ASSERT_MESSAGE("128 characters", aDocStat.nChar == 128);
423 : }
424 :
425 : //See https://issues.apache.org/ooo/show_bug.cgi?id=89042
426 : //See https://bugs.freedesktop.org/show_bug.cgi?id=53399
427 : {
428 1 : SwDocStat aDocStat;
429 :
430 : const sal_Unicode aShouldBeThree[] = {
431 : 0x0053, 0x0068, 0x006F, 0x0075, 0x006C, 0x0064, 0x0020,
432 : 0x2018, 0x0062, 0x0065, 0x0020, 0x0074, 0x0068, 0x0072,
433 : 0x0065, 0x0065, 0x2019
434 1 : };
435 :
436 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
437 1 : m_pDoc->InsertString(aPaM, rtl::OUString(aShouldBeThree, SAL_N_ELEMENTS(aShouldBeThree)));
438 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
439 1 : pTxtNode->CountWords(aDocStat, 0, SAL_N_ELEMENTS(aShouldBeThree));
440 1 : CPPUNIT_ASSERT_MESSAGE("Should be 3", aDocStat.nWord == 3);
441 :
442 : const sal_Unicode aShouldBeFive[] = {
443 : // f r e n c h space
444 : 0x0046, 0x0072, 0x0065, 0x006E, 0x0063, 0x0068, 0x0020,
445 : // << nbsp s a v o i
446 : 0x00AB, 0x00A0, 0x0073, 0x0061, 0x0076, 0x006F, 0x0069,
447 : // r nnbsp c a l c u
448 : 0x0072, 0x202f, 0x0063, 0x0061, 0x006C, 0x0063, 0x0075,
449 : // l e r idspace >>
450 : 0x006C, 0x0065, 0x0072, 0x3000, 0x00BB
451 1 : };
452 :
453 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
454 1 : m_pDoc->InsertString(aPaM, rtl::OUString(aShouldBeFive, SAL_N_ELEMENTS(aShouldBeFive)));
455 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
456 1 : aDocStat.Reset();
457 1 : pTxtNode->CountWords(aDocStat, 0, SAL_N_ELEMENTS(aShouldBeFive));
458 1 : CPPUNIT_ASSERT_MESSAGE("Should be 5", aDocStat.nWord == 5);
459 : }
460 :
461 : //See https://bugs.freedesktop.org/show_bug.cgi?id=49629
462 : {
463 1 : SwDocStat aDocStat;
464 :
465 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
466 1 : m_pDoc->InsertString(aPaM, rtl::OUString("Apple"));
467 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
468 1 : xub_StrLen nPos = aPaM.GetPoint()->nContent.GetIndex();
469 1 : SwFmtFtn aFtn;
470 1 : aFtn.SetNumStr(rtl::OUString("banana"));
471 1 : SwTxtAttr* pTA = pTxtNode->InsertItem(aFtn, nPos, nPos);
472 1 : CPPUNIT_ASSERT(pTA);
473 1 : CPPUNIT_ASSERT(pTxtNode->Len() == 6); //Apple + 0x02
474 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
475 1 : CPPUNIT_ASSERT(aDocStat.nWord == 1);
476 1 : CPPUNIT_ASSERT_MESSAGE("footnote should be expanded", aDocStat.nChar == 11);
477 :
478 1 : xub_StrLen nNextPos = aPaM.GetPoint()->nContent.GetIndex();
479 1 : CPPUNIT_ASSERT(nNextPos == nPos+1);
480 1 : SwFmtRefMark aRef(rtl::OUString("refmark"));
481 1 : pTA = pTxtNode->InsertItem(aRef, nNextPos, nNextPos);
482 1 : CPPUNIT_ASSERT(pTA);
483 :
484 1 : aDocStat.Reset();
485 1 : pTxtNode->SetWordCountDirty(true);
486 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
487 1 : CPPUNIT_ASSERT(aDocStat.nWord == 1);
488 1 : CPPUNIT_ASSERT_MESSAGE("refmark anchor should not be counted", aDocStat.nChar == 11);
489 :
490 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
491 1 : m_pDoc->InsertString(aPaM, rtl::OUString("Apple"));
492 :
493 1 : DateTime aDate(DateTime::SYSTEM);
494 : SwPostItField aPostIt(
495 1 : (SwPostItFieldType*)m_pDoc->GetSysFldType(RES_POSTITFLD), rtl::OUString("An Author"),
496 2 : rtl::OUString("Some Text"), rtl::OUString("Initials"), OUString("Name"), aDate );
497 1 : m_pDoc->InsertPoolItem(aPaM, SwFmtFld(aPostIt), 0);
498 :
499 1 : m_pDoc->InsertString(aPaM, rtl::OUString("Apple"));
500 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
501 1 : aDocStat.Reset();
502 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
503 1 : CPPUNIT_ASSERT(aDocStat.nWord == 1);
504 1 : CPPUNIT_ASSERT_MESSAGE("postit anchor should effectively not exist", aDocStat.nChar == 10);
505 1 : CPPUNIT_ASSERT(pTxtNode->Len() == 11);
506 :
507 1 : aDocStat.Reset();
508 : }
509 :
510 : //See https://bugs.freedesktop.org/show_bug.cgi?id=46757
511 : {
512 1 : SwDocStat aDocStat;
513 :
514 1 : const char aString[] = "Lorem ipsum";
515 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
516 1 : m_pDoc->InsertString(aPaM, rtl::OUString(aString));
517 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
518 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
519 1 : CPPUNIT_ASSERT_EQUAL(aDocStat.nWord, static_cast<sal_uLong>(2));
520 :
521 : //turn on red-lining and show changes
522 1 : m_pDoc->SetRedlineMode(nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_SHOW_DELETE|nsRedlineMode_t::REDLINE_SHOW_INSERT);
523 1 : CPPUNIT_ASSERT_MESSAGE("redlining should be on", m_pDoc->IsRedlineOn());
524 1 : CPPUNIT_ASSERT_MESSAGE("redlines should be visible", IDocumentRedlineAccess::IsShowChanges(m_pDoc->GetRedlineMode()));
525 :
526 : //delete everything except the first word
527 1 : aPaM.SetMark(); //set start of selection to current pos
528 1 : aPaM.GetPoint()->nContent.Assign(aPaM.GetCntntNode(), 5); //set end of selection to fifth char of current node
529 1 : m_pDoc->DeleteAndJoin(aPaM); //redline-aware deletion api
530 1 : CPPUNIT_ASSERT_MESSAGE("real underlying text should be the same", pTxtNode->GetTxt().EqualsAscii(aString));
531 :
532 1 : aDocStat.Reset();
533 1 : pTxtNode->SetWordCountDirty(true);
534 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len()); //but word-counting the text should only count the non-deleted text
535 1 : CPPUNIT_ASSERT_EQUAL(aDocStat.nWord, static_cast<sal_uLong>(1));
536 :
537 1 : pTxtNode->SetWordCountDirty(true);
538 :
539 : //keep red-lining on but hide changes
540 1 : m_pDoc->SetRedlineMode(nsRedlineMode_t::REDLINE_ON);
541 1 : CPPUNIT_ASSERT_MESSAGE("redlining should be still on", m_pDoc->IsRedlineOn());
542 1 : CPPUNIT_ASSERT_MESSAGE("redlines should be invisible", !IDocumentRedlineAccess::IsShowChanges(m_pDoc->GetRedlineMode()));
543 :
544 1 : aDocStat.Reset();
545 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len()); //but word-counting the text should only count the non-deleted text
546 1 : CPPUNIT_ASSERT_EQUAL(aDocStat.nWord, static_cast<sal_uLong>(1));
547 :
548 1 : rtl::OUString sLorem = pTxtNode->GetTxt();
549 1 : CPPUNIT_ASSERT(sLorem == "Lorem");
550 :
551 1 : const SwRedlineTbl& rTbl = m_pDoc->GetRedlineTbl();
552 :
553 1 : SwNodes& rNds = m_pDoc->GetNodes();
554 1 : CPPUNIT_ASSERT(rTbl.size() == 1);
555 :
556 1 : SwNodeIndex* pNodeIdx = rTbl[0]->GetContentIdx();
557 1 : CPPUNIT_ASSERT(pNodeIdx);
558 :
559 1 : pTxtNode = rNds[ pNodeIdx->GetIndex() + 1 ]->GetTxtNode(); //first deleted txtnode
560 1 : CPPUNIT_ASSERT(pTxtNode);
561 :
562 1 : rtl::OUString sIpsum = pTxtNode->GetTxt();
563 1 : CPPUNIT_ASSERT(sIpsum == " ipsum");
564 :
565 1 : aDocStat.Reset();
566 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len()); //word-counting the text should only count the non-deleted text, and this whole chunk should be ignored
567 1 : CPPUNIT_ASSERT_EQUAL(aDocStat.nWord, static_cast<sal_uLong>(0));
568 1 : CPPUNIT_ASSERT_EQUAL(aDocStat.nChar, static_cast<sal_uLong>(0));
569 : }
570 :
571 : //See https://bugs.freedesktop.org/show_bug.cgi?id=38983
572 : {
573 1 : SwDocStat aDocStat;
574 :
575 1 : rtl::OUString sTemplate("ThisXis a test.");
576 :
577 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
578 1 : m_pDoc->InsertString(aPaM, sTemplate.replace('X', ' '));
579 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
580 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
581 2 : CPPUNIT_ASSERT(aDocStat.nWord == 4 &&
582 : aDocStat.nCharExcludingSpaces == 12 &&
583 1 : aDocStat.nChar == 15);
584 1 : aDocStat.Reset();
585 :
586 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
587 1 : m_pDoc->InsertString(aPaM, sTemplate.replaceAll(rtl::OUString('X'), rtl::OUString(" = ")));
588 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
589 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
590 2 : CPPUNIT_ASSERT(aDocStat.nWord == 5 &&
591 : aDocStat.nCharExcludingSpaces == 13 &&
592 1 : aDocStat.nChar == 17);
593 1 : aDocStat.Reset();
594 :
595 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
596 1 : m_pDoc->InsertString(aPaM, sTemplate.replaceAll(rtl::OUString('X'), rtl::OUString(" _ ")));
597 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
598 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
599 2 : CPPUNIT_ASSERT(aDocStat.nWord == 5 &&
600 : aDocStat.nCharExcludingSpaces == 13 &&
601 1 : aDocStat.nChar == 17);
602 1 : aDocStat.Reset();
603 :
604 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
605 1 : m_pDoc->InsertString(aPaM, sTemplate.replaceAll(rtl::OUString('X'), rtl::OUString(" -- ")));
606 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
607 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
608 2 : CPPUNIT_ASSERT(aDocStat.nWord == 5 &&
609 : aDocStat.nCharExcludingSpaces == 14 &&
610 1 : aDocStat.nChar == 18);
611 1 : aDocStat.Reset();
612 :
613 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
614 1 : m_pDoc->InsertString(aPaM, sTemplate.replace('X', '_'));
615 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
616 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
617 2 : CPPUNIT_ASSERT(aDocStat.nWord == 3 &&
618 : aDocStat.nCharExcludingSpaces == 13 &&
619 1 : aDocStat.nChar == 15);
620 1 : aDocStat.Reset();
621 :
622 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
623 1 : m_pDoc->InsertString(aPaM, sTemplate.replace('X', '-'));
624 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
625 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
626 2 : CPPUNIT_ASSERT(aDocStat.nWord == 3 &&
627 : aDocStat.nCharExcludingSpaces == 13 &&
628 1 : aDocStat.nChar == 15);
629 1 : aDocStat.Reset();
630 :
631 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
632 1 : m_pDoc->InsertString(aPaM, sTemplate.replace('X', 0x2012));
633 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
634 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
635 2 : CPPUNIT_ASSERT(aDocStat.nWord == 3 &&
636 : aDocStat.nCharExcludingSpaces == 13 &&
637 1 : aDocStat.nChar == 15);
638 1 : aDocStat.Reset();
639 :
640 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
641 1 : m_pDoc->InsertString(aPaM, sTemplate.replace('X', 0x2015));
642 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
643 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
644 2 : CPPUNIT_ASSERT(aDocStat.nWord == 3 &&
645 : aDocStat.nCharExcludingSpaces == 13 &&
646 1 : aDocStat.nChar == 15);
647 1 : aDocStat.Reset();
648 :
649 : //But default configuration should, msword-alike treak emdash
650 : //and endash as word seperators for word-counting
651 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
652 1 : m_pDoc->InsertString(aPaM, sTemplate.replace('X', 0x2013));
653 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
654 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
655 2 : CPPUNIT_ASSERT(aDocStat.nWord == 4 &&
656 : aDocStat.nCharExcludingSpaces == 13 &&
657 1 : aDocStat.nChar == 15);
658 1 : aDocStat.Reset();
659 :
660 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
661 1 : m_pDoc->InsertString(aPaM, sTemplate.replace('X', 0x2014));
662 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
663 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
664 2 : CPPUNIT_ASSERT(aDocStat.nWord == 4 &&
665 : aDocStat.nCharExcludingSpaces == 13 &&
666 1 : aDocStat.nChar == 15);
667 1 : aDocStat.Reset();
668 :
669 1 : const sal_Unicode aChunk[] = {' ', 0x2013, ' '};
670 1 : rtl::OUString sChunk(aChunk, SAL_N_ELEMENTS(aChunk));
671 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
672 1 : m_pDoc->InsertString(aPaM, sTemplate.replaceAll(rtl::OUString('X'), sChunk));
673 1 : pTxtNode = aPaM.GetNode()->GetTxtNode();
674 1 : pTxtNode->CountWords(aDocStat, 0, pTxtNode->Len());
675 2 : CPPUNIT_ASSERT(aDocStat.nWord == 4 &&
676 : aDocStat.nCharExcludingSpaces == 13 &&
677 1 : aDocStat.nChar == 17);
678 1 : aDocStat.Reset();
679 1 : }
680 1 : }
681 :
682 : //See https://bugs.freedesktop.org/show_bug.cgi?id=40599
683 1 : void SwDocTest::testGraphicAnchorDeletion()
684 : {
685 1 : CPPUNIT_ASSERT_MESSAGE("Expected initial 0 count", m_pDoc->GetDocStat().nChar == 0);
686 :
687 1 : SwNodeIndex aIdx(m_pDoc->GetNodes().GetEndOfContent(), -1);
688 1 : SwPaM aPaM(aIdx);
689 :
690 1 : m_pDoc->InsertString(aPaM, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Paragraph 1")));
691 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
692 :
693 1 : m_pDoc->InsertString(aPaM, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("graphic anchor>><<graphic anchor")));
694 1 : SwNodeIndex nPara2 = aPaM.GetPoint()->nNode;
695 1 : m_pDoc->AppendTxtNode(*aPaM.GetPoint());
696 :
697 1 : m_pDoc->InsertString(aPaM, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Paragraph 3")));
698 :
699 1 : aPaM.GetPoint()->nNode = nPara2;
700 1 : aPaM.GetPoint()->nContent.Assign(aPaM.GetCntntNode(), RTL_CONSTASCII_LENGTH("graphic anchor>>"));
701 :
702 : //Insert a graphic at X of >>X<< in paragraph 2
703 1 : SfxItemSet aFlySet(m_pDoc->GetAttrPool(), RES_FRMATR_BEGIN, RES_FRMATR_END-1);
704 1 : SwFmtAnchor aAnchor(FLY_AS_CHAR);
705 1 : aAnchor.SetAnchor(aPaM.GetPoint());
706 1 : aFlySet.Put(aAnchor);
707 1 : SwFlyFrmFmt *pFrame = m_pDoc->Insert(aPaM, rtl::OUString(), rtl::OUString(), NULL, &aFlySet, NULL, NULL);
708 1 : CPPUNIT_ASSERT_MESSAGE("Expected frame", pFrame != NULL);
709 :
710 1 : CPPUNIT_ASSERT_MESSAGE("Should be 1 graphic", m_pDoc->GetFlyCount(FLYCNTTYPE_GRF) == 1);
711 :
712 : //Delete >X<
713 1 : aPaM.GetPoint()->nNode = nPara2;
714 2 : aPaM.GetPoint()->nContent.Assign(aPaM.GetCntntNode(),
715 2 : RTL_CONSTASCII_LENGTH("graphic anchor>><")+1);
716 1 : aPaM.SetMark();
717 1 : aPaM.GetPoint()->nNode = nPara2;
718 1 : aPaM.GetPoint()->nContent.Assign(aPaM.GetCntntNode(), RTL_CONSTASCII_LENGTH("graphic anchor>"));
719 1 : m_pDoc->DeleteRange(aPaM);
720 :
721 : #ifdef DEBUG_AS_HTML
722 : {
723 : SvFileStream aPasteDebug(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
724 : "cppunitDEBUG.html")), STREAM_WRITE|STREAM_TRUNC);
725 : WriterRef xWrt;
726 : GetHTMLWriter( String(), String(), xWrt );
727 : SwWriter aDbgWrt( aPasteDebug, *m_pDoc );
728 : aDbgWrt.Write( xWrt );
729 : }
730 : #endif
731 :
732 1 : CPPUNIT_ASSERT_MESSAGE("Should be 0 graphics", m_pDoc->GetFlyCount(FLYCNTTYPE_GRF) == 0);
733 :
734 : //Now, if instead we swap FLY_AS_CHAR (inline graphic) to FLY_AT_CHAR (anchored to character)
735 : //and repeat the above, graphic is *not* deleted, i.e. it belongs to the paragraph, not the
736 : //range to which its anchored, which is annoying.
737 1 : }
738 :
739 : static int
740 36328 : getRand(int modulus)
741 : {
742 36328 : if (modulus <= 0)
743 0 : return 0;
744 36328 : return rand() % modulus;
745 : }
746 :
747 : static rtl::OUString
748 3995 : getRandString()
749 : {
750 3995 : rtl::OUString aText("AAAAA BBBB CCC DD E \n");
751 3995 : int s = getRand(aText.getLength());
752 3995 : int j = getRand(aText.getLength() - s);
753 3995 : rtl::OUString aRet(aText.copy(s, j));
754 3995 : if (!getRand(5))
755 811 : aRet += rtl::OUString(sal_Unicode('\n'));
756 : // fprintf (stderr, "rand string '%s'\n", OUStringToOString(aRet, RTL_TEXTENCODING_UTF8).getStr());
757 3995 : return aRet;
758 : }
759 :
760 : static SwPosition
761 18572 : getRandomPosition(SwDoc *pDoc, int /* nOffset */)
762 : {
763 18572 : const SwPosition aPos(pDoc->GetNodes().GetEndOfContent());
764 18572 : sal_uLong nNodes = aPos.nNode.GetNode().GetIndex() - aPos.nNode.GetNode().StartOfSectionIndex();
765 18572 : sal_uLong n = (rand() * nNodes) / RAND_MAX;
766 18572 : SwPaM pam(aPos);
767 27928 : for (sal_uLong i = 0; i < n; ++i) {
768 9356 : pam.Move(fnMoveBackward, fnGoNode);
769 : }
770 18572 : return *pam.GetPoint();
771 : }
772 :
773 1 : void SwDocTest::randomTest()
774 : {
775 1 : CPPUNIT_ASSERT_MESSAGE("SwDoc::IsRedlineOn()", !m_pDoc->IsRedlineOn());
776 : RedlineMode_t modes[] = {
777 : nsRedlineMode_t::REDLINE_ON,
778 : nsRedlineMode_t::REDLINE_SHOW_MASK,
779 : nsRedlineMode_t::REDLINE_NONE,
780 : nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_SHOW_MASK,
781 : nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_IGNORE,
782 : nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_IGNORE | nsRedlineMode_t::REDLINE_SHOW_MASK,
783 : nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_SHOW_INSERT,
784 : nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_SHOW_DELETE
785 1 : };
786 : static const char *authors[] = {
787 : "Jim", "Bob", "JimBobina", "Helga", "Gertrude", "Spagna", "Hurtleweed"
788 : };
789 :
790 9 : for( sal_uInt16 rlm = 0; rlm < SAL_N_ELEMENTS(modes); rlm++ )
791 : {
792 8 : m_pDoc->ClearDoc();
793 :
794 : // setup redlining
795 8 : m_pDoc->SetRedlineMode(modes[rlm]);
796 8 : SW_MOD()->SetRedlineAuthor(rtl::OUString::createFromAscii(authors[0]));
797 :
798 16008 : for( int i = 0; i < 2000; i++ )
799 : {
800 16000 : SwPaM aPam(m_pDoc->GetNodes());
801 16000 : SwCursor aCrs(getRandomPosition(m_pDoc, i/20), 0, false);
802 16000 : aCrs.SetMark();
803 :
804 16000 : switch (getRand (i < 50 ? 3 : 6)) {
805 : // insert ops first
806 : case 0: {
807 2705 : if (!m_pDoc->InsertString(aCrs, getRandString())) {
808 : // fprintf (stderr, "failed to insert string !\n");
809 : }
810 2705 : break;
811 : }
812 : case 1:
813 2768 : break;
814 : case 2: { // switch author
815 2754 : int a = getRand(SAL_N_ELEMENTS(authors));
816 2754 : SW_MOD()->SetRedlineAuthor(rtl::OUString::createFromAscii(authors[a]));
817 2754 : break;
818 : }
819 :
820 : // movement / deletion ops later
821 : case 3: // deletion
822 2577 : switch (getRand(6)) {
823 : case 0:
824 450 : m_pDoc->DelFullPara(aCrs);
825 450 : break;
826 : case 1:
827 397 : m_pDoc->DeleteRange(aCrs);
828 397 : break;
829 : case 2:
830 440 : m_pDoc->DeleteAndJoin(aCrs, !!getRand(1));
831 440 : break;
832 : case 3:
833 : default:
834 1290 : m_pDoc->Overwrite(aCrs, getRandString());
835 1290 : break;
836 : }
837 2577 : break;
838 : case 4: { // movement
839 : IDocumentContentOperations::SwMoveFlags nFlags =
840 : (IDocumentContentOperations::SwMoveFlags)
841 2572 : (getRand(1) ? // FIXME: puterb this more ?
842 : IDocumentContentOperations::DOC_MOVEDEFAULT :
843 : IDocumentContentOperations::DOC_MOVEALLFLYS |
844 : IDocumentContentOperations::DOC_CREATEUNDOOBJ |
845 : IDocumentContentOperations::DOC_MOVEREDLINES |
846 2572 : IDocumentContentOperations::DOC_NO_DELFRMS);
847 2572 : SwPosition aTo(getRandomPosition(m_pDoc, i/10));
848 2572 : m_pDoc->MoveRange(aCrs, aTo, nFlags);
849 2572 : break;
850 : }
851 :
852 : case 5:
853 2624 : break;
854 :
855 : // undo / redo ?
856 : default:
857 0 : break;
858 : }
859 16000 : }
860 :
861 : // Debug / verify the produced document has real content
862 : #if 0
863 : rtl::OStringBuffer aBuffer("nodes-");
864 : aBuffer.append(sal_Int32(rlm));
865 : aBuffer.append(".xml");
866 :
867 : xmlTextWriterPtr writer;
868 : writer = xmlNewTextWriterFilename( aBuffer.makeStringAndClear().getStr(), 0 );
869 : xmlTextWriterStartDocument( writer, NULL, NULL, NULL );
870 : m_pDoc->dumpAsXml(writer);
871 : xmlTextWriterEndDocument( writer );
872 : xmlFreeTextWriter( writer );
873 : #endif
874 : }
875 1 : }
876 :
877 8 : void SwDocTest::setUp()
878 : {
879 8 : BootstrapFixture::setUp();
880 :
881 8 : SwGlobals::ensure();
882 8 : m_pDoc = new SwDoc;
883 8 : m_xDocShRef = new SwDocShell(m_pDoc, SFX_CREATE_MODE_EMBEDDED);
884 8 : m_xDocShRef->DoInitNew(0);
885 8 : }
886 :
887 8 : void SwDocTest::tearDown()
888 : {
889 8 : m_xDocShRef.Clear();
890 8 : delete m_pDoc;
891 :
892 8 : BootstrapFixture::tearDown();
893 8 : }
894 :
895 1 : CPPUNIT_TEST_SUITE_REGISTRATION(SwDocTest);
896 :
897 4 : CPPUNIT_PLUGIN_IMPLEMENT();
898 :
899 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|