1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
;
31 class Test
: public test::BootstrapFixture
37 virtual void tearDown();
39 void testConstruction();
42 * Test UNO service class that implements text field items.
44 void testUnoTextFields();
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();
58 EditEngineItemPool
* mpItemPool
;
61 Test::Test() : mpItemPool(NULL
) {}
65 test::BootstrapFixture::setUp();
67 mpItemPool
= new EditEngineItemPool(true);
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
);
86 bool includes(const uno::Sequence
<OUString
>& rSeq
, const OUString
& rVal
)
88 for (sal_Int32 i
= 0, n
= rSeq
.getLength(); i
< n
; ++i
)
97 void Test::testUnoTextFields()
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
215 /// just like the real thing, this dummy modifies the rText parameter :(
216 TestAutoCorrDoc(OUString
&rText
, LanguageType eLang
)
221 OUString
const& getResult() const
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
, "");
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
);
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
);
251 virtual sal_Bool
SetAttr( xub_StrLen
, xub_StrLen
, sal_uInt16
, SfxPoolItem
& )
253 //fprintf(stderr, "TestAutoCorrDoc::SetAttr\n");
256 virtual sal_Bool
SetINetAttr( xub_StrLen
, xub_StrLen
, const String
& )
258 //fprintf(stderr, "TestAutoCorrDoc::SetINetAttr\n");
261 virtual const String
* GetPrevPara( sal_Bool
)
263 //fprintf(stderr, "TestAutoCorrDoc::GetPrevPara\n");
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())
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());
281 *ppPara
= NULL
;//&pCurNode->GetString();
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
,
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: */