Bump version to 4.1-6
[LibreOffice.git] / editeng / qa / unit / core-test.cxx
blob0975efb9afdee89f309422d749ee15b19bc2e3ca
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include "test/bootstrapfixture.hxx"
12 #include <cppunit/TestFixture.h>
13 #include <cppunit/extensions/HelperMacros.h>
15 #include "svl/itempool.hxx"
16 #include "editeng/eerdll.hxx"
17 #include "editeng/eerdll2.hxx"
18 #include "editeng/editeng.hxx"
19 #include "editeng/eeitem.hxx"
20 #include "editeng/editids.hrc"
21 #include "editeng/editdoc.hxx"
22 #include "editeng/svxacorr.hxx"
23 #include "editeng/unofield.hxx"
25 #include <com/sun/star/text/textfield/Type.hpp>
27 using namespace com::sun::star;
29 namespace {
31 class Test : public test::BootstrapFixture
33 public:
34 Test();
36 virtual void setUp();
37 virtual void tearDown();
39 void testConstruction();
41 /**
42 * Test UNO service class that implements text field items.
44 void testUnoTextFields();
46 /**
47 * AutoCorrect tests
49 void testAutocorrect();
51 CPPUNIT_TEST_SUITE(Test);
52 CPPUNIT_TEST(testConstruction);
53 CPPUNIT_TEST(testUnoTextFields);
54 CPPUNIT_TEST(testAutocorrect);
55 CPPUNIT_TEST_SUITE_END();
57 private:
58 EditEngineItemPool* mpItemPool;
61 Test::Test() : mpItemPool(NULL) {}
63 void Test::setUp()
65 test::BootstrapFixture::setUp();
67 mpItemPool = new EditEngineItemPool(true);
70 void Test::tearDown()
72 SfxItemPool::Free(mpItemPool);
73 test::BootstrapFixture::tearDown();
76 void Test::testConstruction()
78 EditEngine aEngine(mpItemPool);
80 OUString aParaText = "I am Edit Engine.";
81 aEngine.SetText(aParaText);
84 namespace {
86 bool includes(const uno::Sequence<OUString>& rSeq, const OUString& rVal)
88 for (sal_Int32 i = 0, n = rSeq.getLength(); i < n; ++i)
89 if (rSeq[i] == rVal)
90 return true;
92 return false;
97 void Test::testUnoTextFields()
100 // DATE
101 SvxUnoTextField aField(text::textfield::Type::DATE);
102 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
103 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.DateTime");
104 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
108 // URL
109 SvxUnoTextField aField(text::textfield::Type::URL);
110 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
111 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.URL");
112 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
116 // PAGE
117 SvxUnoTextField aField(text::textfield::Type::PAGE);
118 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
119 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.PageNumber");
120 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
124 // PAGES
125 SvxUnoTextField aField(text::textfield::Type::PAGES);
126 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
127 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.PageCount");
128 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
132 // TIME
133 SvxUnoTextField aField(text::textfield::Type::TIME);
134 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
135 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.DateTime");
136 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
140 // FILE
141 SvxUnoTextField aField(text::textfield::Type::DOCINFO_TITLE);
142 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
143 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.docinfo.Title");
144 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
148 // TABLE
149 SvxUnoTextField aField(text::textfield::Type::TABLE);
150 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
151 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.SheetName");
152 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
156 // EXTENDED TIME
157 SvxUnoTextField aField(text::textfield::Type::EXTENDED_TIME);
158 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
159 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.DateTime");
160 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
164 // EXTENDED FILE
165 SvxUnoTextField aField(text::textfield::Type::EXTENDED_FILE);
166 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
167 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.FileName");
168 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
172 // AUTHOR
173 SvxUnoTextField aField(text::textfield::Type::AUTHOR);
174 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
175 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.Author");
176 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
180 // MEASURE
181 SvxUnoTextField aField(text::textfield::Type::MEASURE);
182 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
183 bool bGood = includes(aSvcs, "com.sun.star.text.textfield.Measure");
184 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
188 // PRESENTATION HEADER
189 SvxUnoTextField aField(text::textfield::Type::PRESENTATION_HEADER);
190 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
191 bool bGood = includes(aSvcs, "com.sun.star.presentation.textfield.Header");
192 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
196 // PRESENTATION FOOTER
197 SvxUnoTextField aField(text::textfield::Type::PRESENTATION_FOOTER);
198 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
199 bool bGood = includes(aSvcs, "com.sun.star.presentation.textfield.Footer");
200 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
204 // PRESENTATION DATE TIME
205 SvxUnoTextField aField(text::textfield::Type::PRESENTATION_DATE_TIME);
206 uno::Sequence<OUString> aSvcs = aField.getSupportedServiceNames();
207 bool bGood = includes(aSvcs, "com.sun.star.presentation.textfield.DateTime");
208 CPPUNIT_ASSERT_MESSAGE("expected service is not present.", bGood);
212 class TestAutoCorrDoc : public SvxAutoCorrDoc
214 public:
215 /// just like the real thing, this dummy modifies the rText parameter :(
216 TestAutoCorrDoc(OUString &rText, LanguageType eLang)
217 : m_rText(rText)
218 , m_eLang(eLang)
221 OUString const& getResult() const
223 return m_rText;
225 private:
226 OUString & m_rText;
227 LanguageType m_eLang;
228 virtual sal_Bool Delete( xub_StrLen nStt, xub_StrLen nEnd )
230 //fprintf(stderr, "TestAutoCorrDoc::Delete\n");
231 m_rText = m_rText.replaceAt(nStt, nEnd-nStt, "");
232 return true;
234 virtual sal_Bool Insert( xub_StrLen nPos, const String& rTxt )
236 //fprintf(stderr, "TestAutoCorrDoc::Insert\n");
237 m_rText = m_rText.replaceAt(nPos, 0, rTxt);
238 return true;
240 virtual sal_Bool Replace( xub_StrLen nPos, const String& rTxt )
242 //fprintf(stderr, "TestAutoCorrDoc::Replace\n");
243 return ReplaceRange( nPos, rTxt.Len(), rTxt );
245 virtual sal_Bool ReplaceRange( xub_StrLen nPos, xub_StrLen nLen, const String& rTxt )
247 //fprintf(stderr, "TestAutoCorrDoc::ReplaceRange %d %d %s\n", nPos, nLen, OUStringToOString(rTxt, RTL_TEXTENCODING_UTF8).getStr());
248 m_rText = m_rText.replaceAt(nPos, nLen, rTxt);
249 return true;
251 virtual sal_Bool SetAttr( xub_StrLen, xub_StrLen, sal_uInt16, SfxPoolItem& )
253 //fprintf(stderr, "TestAutoCorrDoc::SetAttr\n");
254 return true;
256 virtual sal_Bool SetINetAttr( xub_StrLen, xub_StrLen, const String& )
258 //fprintf(stderr, "TestAutoCorrDoc::SetINetAttr\n");
259 return true;
261 virtual const String* GetPrevPara( sal_Bool )
263 //fprintf(stderr, "TestAutoCorrDoc::GetPrevPara\n");
264 return NULL;
266 virtual sal_Bool ChgAutoCorrWord( sal_uInt16& rSttPos,
267 sal_uInt16 nEndPos, SvxAutoCorrect& rACorrect,
268 const String** ppPara )
270 //fprintf(stderr, "TestAutoCorrDoc::ChgAutoCorrWord\n");
272 if (m_rText.isEmpty())
273 return false;
275 const SvxAutocorrWord* pFnd = rACorrect.SearchWordsInList(
276 m_rText, rSttPos, nEndPos, *this, m_eLang);
277 if (pFnd && pFnd->IsTextOnly())
279 m_rText = m_rText.replaceAt(rSttPos, nEndPos, pFnd->GetLong());
280 if( ppPara )
281 *ppPara = NULL;//&pCurNode->GetString();
282 return true;
285 return false;
289 //https://bugs.freedesktop.org/show_bug.cgi?id=55693
290 //Two capitalized letters are not corrected if dash or slash are directly
291 //before the two letters
292 void Test::testAutocorrect()
294 OUString sShareAutocorrFile;
295 OUString sUserAutocorrFile;
296 SvxAutoCorrect aAutoCorrect(sShareAutocorrFile, sUserAutocorrFile);
299 OUString sInput("TEst-TEst");
300 sal_Unicode cNextChar(' ');
301 OUString sExpected("Test-Test ");
303 TestAutoCorrDoc aFoo(sInput, LANGUAGE_ENGLISH_US);
304 aAutoCorrect.DoAutoCorrect(aFoo, sInput, sInput.getLength(), cNextChar, true);
306 CPPUNIT_ASSERT_MESSAGE("autocorrect", aFoo.getResult() == sExpected);
310 OUString sInput("TEst/TEst");
311 sal_Unicode cNextChar(' ');
312 OUString sExpected("Test/Test ");
314 TestAutoCorrDoc aFoo(sInput, LANGUAGE_ENGLISH_US);
315 aAutoCorrect.DoAutoCorrect(aFoo, sInput, sInput.getLength(), cNextChar, true);
317 CPPUNIT_ASSERT_MESSAGE("autocorrect", aFoo.getResult() == sExpected);
321 // test auto-bolding with '*'
322 OUString sInput("*foo");
323 sal_Unicode cNextChar('*');
324 OUString sExpected("foo");
326 TestAutoCorrDoc aFoo(sInput, LANGUAGE_ENGLISH_US);
327 String const& rInput2(reinterpret_cast<String const&>(aFoo.getResult()));
328 aAutoCorrect.DoAutoCorrect(aFoo,
329 rInput2,
330 sInput.getLength(), cNextChar, true);
332 CPPUNIT_ASSERT_EQUAL(sExpected, aFoo.getResult());
337 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
341 CPPUNIT_PLUGIN_IMPLEMENT();
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */