1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/richtextctrltest.cpp
3 // Purpose: wxRichTextCtrl unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
22 #include "wx/richtext/richtextctrl.h"
23 #include "wx/richtext/richtextstyles.h"
24 #include "testableframe.h"
25 #include "asserthelper.h"
26 #include "wx/uiaction.h"
28 class RichTextCtrlTestCase
: public CppUnit::TestCase
31 RichTextCtrlTestCase() { }
37 CPPUNIT_TEST_SUITE( RichTextCtrlTestCase
);
38 WXUISIM_TEST( CharacterEvent
);
39 WXUISIM_TEST( DeleteEvent
);
40 WXUISIM_TEST( ReturnEvent
);
41 CPPUNIT_TEST( StyleEvent
);
42 CPPUNIT_TEST( BufferResetEvent
);
43 WXUISIM_TEST( UrlEvent
);
44 WXUISIM_TEST( TextEvent
);
45 CPPUNIT_TEST( CutCopyPaste
);
46 CPPUNIT_TEST( UndoRedo
);
47 // FIXME: This test is temporarily disabled as it currently fails
48 // but it should be reenabled and fixed a.s.a.p. after the
51 CPPUNIT_TEST( CaretPosition
);
53 CPPUNIT_TEST( Selection
);
54 WXUISIM_TEST( Editable
);
55 CPPUNIT_TEST( Range
);
56 CPPUNIT_TEST( Alignment
);
58 CPPUNIT_TEST( Italic
);
59 CPPUNIT_TEST( Underline
);
60 CPPUNIT_TEST( Indent
);
61 CPPUNIT_TEST( LineSpacing
);
62 CPPUNIT_TEST( ParagraphSpacing
);
63 CPPUNIT_TEST( TextColour
);
64 CPPUNIT_TEST( NumberedBullet
);
65 CPPUNIT_TEST( SymbolBullet
);
66 CPPUNIT_TEST( FontSize
);
68 CPPUNIT_TEST( Delete
);
70 CPPUNIT_TEST_SUITE_END();
72 void CharacterEvent();
76 void BufferResetEvent();
91 void ParagraphSpacing();
93 void NumberedBullet();
100 wxRichTextCtrl
* m_rich
;
102 DECLARE_NO_COPY_CLASS(RichTextCtrlTestCase
)
105 // register in the unnamed registry so that these tests are run by default
106 CPPUNIT_TEST_SUITE_REGISTRATION( RichTextCtrlTestCase
);
108 // also include in its own registry so that these tests can be run alone
109 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RichTextCtrlTestCase
, "RichTextCtrlTestCase" );
111 void RichTextCtrlTestCase::setUp()
113 m_rich
= new wxRichTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
114 wxDefaultPosition
, wxSize(400, 200));
117 void RichTextCtrlTestCase::tearDown()
122 void RichTextCtrlTestCase::CharacterEvent()
124 #if wxUSE_UIACTIONSIMULATOR
126 // There seems to be an event sequence problem on GTK+ that causes the events
127 // to be disconnected before they're processed, generating spurious errors.
128 #if !defined(__WXGTK__)
129 EventCounter
character(m_rich
, wxEVT_COMMAND_RICHTEXT_CHARACTER
);
130 EventCounter
content(m_rich
, wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
);
134 wxUIActionSimulator sim
;
138 CPPUNIT_ASSERT_EQUAL(6, character
.GetCount());
139 CPPUNIT_ASSERT_EQUAL(6, content
.GetCount());
144 //As these are not characters they shouldn't count
145 sim
.Char(WXK_RETURN
);
149 CPPUNIT_ASSERT_EQUAL(0, character
.GetCount());
150 CPPUNIT_ASSERT_EQUAL(1, content
.GetCount());
155 void RichTextCtrlTestCase::DeleteEvent()
157 #if wxUSE_UIACTIONSIMULATOR
158 // There seems to be an event sequence problem on GTK+ that causes the events
159 // to be disconnected before they're processed, generating spurious errors.
160 #if !defined(__WXGTK__)
161 EventCounter
deleteevent(m_rich
, wxEVT_COMMAND_RICHTEXT_DELETE
);
162 EventCounter
contentdelete(m_rich
, wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
);
166 wxUIActionSimulator sim
;
169 sim
.Char(WXK_DELETE
);
172 CPPUNIT_ASSERT_EQUAL(2, deleteevent
.GetCount());
173 //Only one as the delete doesn't delete anthing
174 CPPUNIT_ASSERT_EQUAL(1, contentdelete
.GetCount());
179 void RichTextCtrlTestCase::ReturnEvent()
181 #if wxUSE_UIACTIONSIMULATOR
182 // There seems to be an event sequence problem on GTK+ that causes the events
183 // to be disconnected before they're processed, generating spurious errors.
184 #if !defined(__WXGTK__)
185 EventCounter
returnevent(m_rich
, wxEVT_COMMAND_RICHTEXT_RETURN
);
189 wxUIActionSimulator sim
;
190 sim
.Char(WXK_RETURN
);
193 CPPUNIT_ASSERT_EQUAL(1, returnevent
.GetCount());
198 void RichTextCtrlTestCase::StyleEvent()
200 EventCounter
stylechanged(m_rich
, wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
);
202 m_rich
->SetValue("Sometext");
203 m_rich
->SetStyle(0, 8, wxTextAttr(*wxRED
, *wxWHITE
));
205 CPPUNIT_ASSERT_EQUAL(1, stylechanged
.GetCount());
208 void RichTextCtrlTestCase::BufferResetEvent()
210 EventCounter
reset(m_rich
, wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
);
212 m_rich
->AppendText("more text!");
213 m_rich
->SetValue("");
215 CPPUNIT_ASSERT_EQUAL(1, reset
.GetCount());
218 m_rich
->AppendText("more text!");
221 CPPUNIT_ASSERT_EQUAL(1, reset
.GetCount());
225 //We expect a buffer reset here as setvalue clears the existing text
226 m_rich
->SetValue("replace");
227 CPPUNIT_ASSERT_EQUAL(1, reset
.GetCount());
230 void RichTextCtrlTestCase::UrlEvent()
232 #if wxUSE_UIACTIONSIMULATOR
233 // Mouse up event not being caught on GTK+
234 #if !defined(__WXGTK__)
235 EventCounter
url(m_rich
, wxEVT_COMMAND_TEXT_URL
);
237 m_rich
->BeginURL("http://www.wxwidgets.org");
238 m_rich
->WriteText("http://www.wxwidgets.org");
241 wxUIActionSimulator sim
;
242 sim
.MouseMove(m_rich
->ClientToScreen(wxPoint(10, 10)));
248 CPPUNIT_ASSERT_EQUAL(1, url
.GetCount());
253 void RichTextCtrlTestCase::TextEvent()
255 #if wxUSE_UIACTIONSIMULATOR
256 #if !defined(__WXGTK__)
257 EventCounter
updated(m_rich
, wxEVT_COMMAND_TEXT_UPDATED
);
261 wxUIActionSimulator sim
;
265 CPPUNIT_ASSERT_EQUAL("abcdef", m_rich
->GetValue());
266 CPPUNIT_ASSERT_EQUAL(6, updated
.GetCount());
271 void RichTextCtrlTestCase::CutCopyPaste()
274 m_rich
->AppendText("sometext");
277 if(m_rich
->CanCut() && m_rich
->CanPaste())
280 CPPUNIT_ASSERT(m_rich
->IsEmpty());
285 CPPUNIT_ASSERT_EQUAL("sometext", m_rich
->GetValue());
290 if(m_rich
->CanCopy() && m_rich
->CanPaste())
294 CPPUNIT_ASSERT(m_rich
->IsEmpty());
299 CPPUNIT_ASSERT_EQUAL("sometext", m_rich
->GetValue());
304 void RichTextCtrlTestCase::UndoRedo()
306 m_rich
->AppendText("sometext");
308 CPPUNIT_ASSERT(m_rich
->CanUndo());
312 CPPUNIT_ASSERT(m_rich
->IsEmpty());
313 CPPUNIT_ASSERT(m_rich
->CanRedo());
317 CPPUNIT_ASSERT_EQUAL("sometext", m_rich
->GetValue());
319 m_rich
->AppendText("Batch undo");
322 //Also test batch operations
323 m_rich
->BeginBatchUndo("batchtest");
325 m_rich
->ApplyBoldToSelection();
326 m_rich
->ApplyItalicToSelection();
328 m_rich
->EndBatchUndo();
330 CPPUNIT_ASSERT(m_rich
->CanUndo());
334 CPPUNIT_ASSERT(!m_rich
->IsSelectionBold());
335 CPPUNIT_ASSERT(!m_rich
->IsSelectionItalics());
336 CPPUNIT_ASSERT(m_rich
->CanRedo());
340 CPPUNIT_ASSERT(m_rich
->IsSelectionBold());
341 CPPUNIT_ASSERT(m_rich
->IsSelectionItalics());
343 //And surpressing undo
344 m_rich
->BeginSuppressUndo();
346 m_rich
->AppendText("Can't undo this");
348 CPPUNIT_ASSERT(m_rich
->CanUndo());
350 m_rich
->EndSuppressUndo();
353 void RichTextCtrlTestCase::CaretPosition()
355 m_rich
->AddParagraph("This is paragraph one");
356 m_rich
->AddParagraph("Paragraph two\n has \nlots of\n lines");
358 m_rich
->MoveCaret(1);
360 CPPUNIT_ASSERT_EQUAL(1, m_rich
->GetCaretPosition());
362 m_rich
->MoveToParagraphStart();
364 CPPUNIT_ASSERT_EQUAL(0, m_rich
->GetCaretPosition());
367 m_rich
->MoveRight(2);
371 CPPUNIT_ASSERT_EQUAL(2, m_rich
->GetCaretPosition());
373 m_rich
->MoveToParagraphEnd();
375 CPPUNIT_ASSERT_EQUAL(21, m_rich
->GetCaretPosition());
377 m_rich
->MoveToLineStart();
379 CPPUNIT_ASSERT_EQUAL(0, m_rich
->GetCaretPosition());
381 m_rich
->MoveToLineEnd();
383 CPPUNIT_ASSERT_EQUAL(21, m_rich
->GetCaretPosition());
386 void RichTextCtrlTestCase::Selection()
388 m_rich
->SetValue("some more text");
392 CPPUNIT_ASSERT_EQUAL("some more text", m_rich
->GetStringSelection());
394 m_rich
->SelectNone();
396 CPPUNIT_ASSERT_EQUAL("", m_rich
->GetStringSelection());
398 m_rich
->SelectWord(1);
400 CPPUNIT_ASSERT_EQUAL("some", m_rich
->GetStringSelection());
402 m_rich
->SetSelection(5, 14);
404 CPPUNIT_ASSERT_EQUAL("more text", m_rich
->GetStringSelection());
406 wxRichTextRange
range(5, 9);
408 m_rich
->SetSelectionRange(range
);
410 CPPUNIT_ASSERT_EQUAL("more", m_rich
->GetStringSelection());
413 void RichTextCtrlTestCase::Editable()
415 #if wxUSE_UIACTIONSIMULATOR
416 #if !defined(__WXGTK__)
417 EventCounter
updated(m_rich
, wxEVT_COMMAND_TEXT_UPDATED
);
421 wxUIActionSimulator sim
;
425 CPPUNIT_ASSERT_EQUAL("abcdef", m_rich
->GetValue());
426 CPPUNIT_ASSERT_EQUAL(6, updated
.GetCount());
429 m_rich
->SetEditable(false);
433 CPPUNIT_ASSERT_EQUAL("abcdef", m_rich
->GetValue());
434 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
439 void RichTextCtrlTestCase::Range()
441 wxRichTextRange
range(0, 10);
443 CPPUNIT_ASSERT_EQUAL(0, range
.GetStart());
444 CPPUNIT_ASSERT_EQUAL(10, range
.GetEnd());
445 CPPUNIT_ASSERT_EQUAL(11, range
.GetLength());
446 CPPUNIT_ASSERT(range
.Contains(5));
448 wxRichTextRange
outside(12, 14);
450 CPPUNIT_ASSERT(outside
.IsOutside(range
));
452 wxRichTextRange
inside(6, 7);
454 CPPUNIT_ASSERT(inside
.IsWithin(range
));
456 range
.LimitTo(inside
);
458 CPPUNIT_ASSERT(inside
== range
);
459 CPPUNIT_ASSERT(inside
+ range
== outside
);
460 CPPUNIT_ASSERT(outside
- range
== inside
);
465 CPPUNIT_ASSERT_EQUAL(4, range
.GetStart());
466 CPPUNIT_ASSERT_EQUAL(6, range
.GetEnd());
467 CPPUNIT_ASSERT_EQUAL(3, range
.GetLength());
469 inside
.SetRange(6, 4);
472 CPPUNIT_ASSERT(inside
== range
);
475 void RichTextCtrlTestCase::Alignment()
477 m_rich
->SetValue("text to align");
480 m_rich
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT
);
482 CPPUNIT_ASSERT(m_rich
->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT
));
484 m_rich
->BeginAlignment(wxTEXT_ALIGNMENT_CENTRE
);
485 m_rich
->AddParagraph("middle aligned");
486 m_rich
->EndAlignment();
488 m_rich
->SetSelection(20, 25);
490 CPPUNIT_ASSERT(m_rich
->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE
));
493 void RichTextCtrlTestCase::Bold()
495 m_rich
->SetValue("text to bold");
497 m_rich
->ApplyBoldToSelection();
499 CPPUNIT_ASSERT(m_rich
->IsSelectionBold());
502 m_rich
->AddParagraph("bold paragraph");
504 m_rich
->AddParagraph("not bold paragraph");
506 m_rich
->SetSelection(15, 20);
508 CPPUNIT_ASSERT(m_rich
->IsSelectionBold());
510 m_rich
->SetSelection(30, 35);
512 CPPUNIT_ASSERT(!m_rich
->IsSelectionBold());
515 void RichTextCtrlTestCase::Italic()
517 m_rich
->SetValue("text to italic");
519 m_rich
->ApplyItalicToSelection();
521 CPPUNIT_ASSERT(m_rich
->IsSelectionItalics());
523 m_rich
->BeginItalic();
524 m_rich
->AddParagraph("italic paragraph");
526 m_rich
->AddParagraph("not italic paragraph");
528 m_rich
->SetSelection(20, 25);
530 CPPUNIT_ASSERT(m_rich
->IsSelectionItalics());
532 m_rich
->SetSelection(35, 40);
534 CPPUNIT_ASSERT(!m_rich
->IsSelectionItalics());
537 void RichTextCtrlTestCase::Underline()
539 m_rich
->SetValue("text to underline");
541 m_rich
->ApplyUnderlineToSelection();
543 CPPUNIT_ASSERT(m_rich
->IsSelectionUnderlined());
545 m_rich
->BeginUnderline();
546 m_rich
->AddParagraph("underline paragraph");
547 m_rich
->EndUnderline();
548 m_rich
->AddParagraph("not underline paragraph");
550 m_rich
->SetSelection(20, 25);
552 CPPUNIT_ASSERT(m_rich
->IsSelectionUnderlined());
554 m_rich
->SetSelection(40, 45);
556 CPPUNIT_ASSERT(!m_rich
->IsSelectionUnderlined());
559 void RichTextCtrlTestCase::Indent()
561 m_rich
->BeginLeftIndent(12, -5);
562 m_rich
->BeginRightIndent(14);
563 m_rich
->AddParagraph("A paragraph with indents");
564 m_rich
->EndLeftIndent();
565 m_rich
->EndRightIndent();
566 m_rich
->AddParagraph("No more indent");
569 m_rich
->GetStyle(5, indent
);
571 CPPUNIT_ASSERT_EQUAL(12, indent
.GetLeftIndent());
572 CPPUNIT_ASSERT_EQUAL(-5, indent
.GetLeftSubIndent());
573 CPPUNIT_ASSERT_EQUAL(14, indent
.GetRightIndent());
575 m_rich
->GetStyle(35, indent
);
577 CPPUNIT_ASSERT_EQUAL(0, indent
.GetLeftIndent());
578 CPPUNIT_ASSERT_EQUAL(0, indent
.GetLeftSubIndent());
579 CPPUNIT_ASSERT_EQUAL(0, indent
.GetRightIndent());
582 void RichTextCtrlTestCase::LineSpacing()
584 m_rich
->BeginLineSpacing(20);
585 m_rich
->AddParagraph("double spaced");
586 m_rich
->EndLineSpacing();
587 m_rich
->BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF
);
588 m_rich
->AddParagraph("1.5 spaced");
589 m_rich
->EndLineSpacing();
590 m_rich
->AddParagraph("normally spaced");
593 m_rich
->GetStyle(5, spacing
);
595 CPPUNIT_ASSERT_EQUAL(20, spacing
.GetLineSpacing());
597 m_rich
->GetStyle(20, spacing
);
599 CPPUNIT_ASSERT_EQUAL(15, spacing
.GetLineSpacing());
601 m_rich
->GetStyle(30, spacing
);
603 CPPUNIT_ASSERT_EQUAL(10, spacing
.GetLineSpacing());
606 void RichTextCtrlTestCase::ParagraphSpacing()
608 m_rich
->BeginParagraphSpacing(15, 20);
609 m_rich
->AddParagraph("spaced paragraph");
610 m_rich
->EndParagraphSpacing();
611 m_rich
->AddParagraph("non-spaced paragraph");
614 m_rich
->GetStyle(5, spacing
);
616 CPPUNIT_ASSERT_EQUAL(15, spacing
.GetParagraphSpacingBefore());
617 CPPUNIT_ASSERT_EQUAL(20, spacing
.GetParagraphSpacingAfter());
619 m_rich
->GetStyle(25, spacing
);
621 //Make sure we test against the defaults
622 CPPUNIT_ASSERT_EQUAL(m_rich
->GetBasicStyle().GetParagraphSpacingBefore(),
623 spacing
.GetParagraphSpacingBefore());
624 CPPUNIT_ASSERT_EQUAL(m_rich
->GetBasicStyle().GetParagraphSpacingAfter(),
625 spacing
.GetParagraphSpacingAfter());
628 void RichTextCtrlTestCase::TextColour()
630 m_rich
->BeginTextColour(*wxRED
);
631 m_rich
->AddParagraph("red paragraph");
632 m_rich
->EndTextColour();
633 m_rich
->AddParagraph("default paragraph");
636 m_rich
->GetStyle(5, colour
);
638 CPPUNIT_ASSERT_EQUAL(*wxRED
, colour
.GetTextColour());
640 m_rich
->GetStyle(25, colour
);
642 CPPUNIT_ASSERT_EQUAL(m_rich
->GetBasicStyle().GetTextColour(),
643 colour
.GetTextColour());
646 void RichTextCtrlTestCase::NumberedBullet()
648 m_rich
->BeginNumberedBullet(1, 15, 20);
649 m_rich
->AddParagraph("bullet one");
650 m_rich
->EndNumberedBullet();
651 m_rich
->BeginNumberedBullet(2, 25, -5);
652 m_rich
->AddParagraph("bullet two");
653 m_rich
->EndNumberedBullet();
656 m_rich
->GetStyle(5, bullet
);
658 CPPUNIT_ASSERT(bullet
.HasBulletStyle());
659 CPPUNIT_ASSERT(bullet
.HasBulletNumber());
660 CPPUNIT_ASSERT_EQUAL(1, bullet
.GetBulletNumber());
661 CPPUNIT_ASSERT_EQUAL(15, bullet
.GetLeftIndent());
662 CPPUNIT_ASSERT_EQUAL(20, bullet
.GetLeftSubIndent());
664 m_rich
->GetStyle(15, bullet
);
666 CPPUNIT_ASSERT(bullet
.HasBulletStyle());
667 CPPUNIT_ASSERT(bullet
.HasBulletNumber());
668 CPPUNIT_ASSERT_EQUAL(2, bullet
.GetBulletNumber());
669 CPPUNIT_ASSERT_EQUAL(25, bullet
.GetLeftIndent());
670 CPPUNIT_ASSERT_EQUAL(-5, bullet
.GetLeftSubIndent());
673 void RichTextCtrlTestCase::SymbolBullet()
675 m_rich
->BeginSymbolBullet("*", 15, 20);
676 m_rich
->AddParagraph("bullet one");
677 m_rich
->EndSymbolBullet();
678 m_rich
->BeginSymbolBullet("%", 25, -5);
679 m_rich
->AddParagraph("bullet two");
680 m_rich
->EndSymbolBullet();
683 m_rich
->GetStyle(5, bullet
);
685 CPPUNIT_ASSERT(bullet
.HasBulletStyle());
686 CPPUNIT_ASSERT(bullet
.HasBulletText());
687 CPPUNIT_ASSERT_EQUAL("*", bullet
.GetBulletText());
688 CPPUNIT_ASSERT_EQUAL(15, bullet
.GetLeftIndent());
689 CPPUNIT_ASSERT_EQUAL(20, bullet
.GetLeftSubIndent());
691 m_rich
->GetStyle(15, bullet
);
693 CPPUNIT_ASSERT(bullet
.HasBulletStyle());
694 CPPUNIT_ASSERT(bullet
.HasBulletText());
695 CPPUNIT_ASSERT_EQUAL("%", bullet
.GetBulletText());
696 CPPUNIT_ASSERT_EQUAL(25, bullet
.GetLeftIndent());
697 CPPUNIT_ASSERT_EQUAL(-5, bullet
.GetLeftSubIndent());
700 void RichTextCtrlTestCase::FontSize()
702 m_rich
->BeginFontSize(24);
703 m_rich
->AddParagraph("Large text");
704 m_rich
->EndFontSize();
707 m_rich
->GetStyle(5, size
);
709 CPPUNIT_ASSERT(size
.HasFontSize());
710 CPPUNIT_ASSERT_EQUAL(24, size
.GetFontSize());
713 void RichTextCtrlTestCase::Font()
715 wxFont
font(14, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
716 m_rich
->BeginFont(font
);
717 m_rich
->AddParagraph("paragraph with font");
720 wxTextAttr fontstyle
;
721 m_rich
->GetStyle(5, fontstyle
);
723 CPPUNIT_ASSERT_EQUAL(font
, fontstyle
.GetFont());
726 void RichTextCtrlTestCase::Delete()
728 m_rich
->AddParagraph("here is a long long line in a paragraph");
729 m_rich
->SetSelection(0, 6);
731 CPPUNIT_ASSERT(m_rich
->CanDeleteSelection());
733 m_rich
->DeleteSelection();
735 CPPUNIT_ASSERT_EQUAL("is a long long line in a paragraph", m_rich
->GetValue());
737 m_rich
->SetSelection(0, 5);
739 CPPUNIT_ASSERT(m_rich
->CanDeleteSelection());
741 m_rich
->DeleteSelectedContent();
743 CPPUNIT_ASSERT_EQUAL("long long line in a paragraph", m_rich
->GetValue());
745 m_rich
->Delete(wxRichTextRange(14, 29));
747 CPPUNIT_ASSERT_EQUAL("long long line", m_rich
->GetValue());
750 void RichTextCtrlTestCase::Url()
752 m_rich
->BeginURL("http://www.wxwidgets.org");
753 m_rich
->WriteText("http://www.wxwidgets.org");
757 m_rich
->GetStyle(5, url
);
759 CPPUNIT_ASSERT(url
.HasURL());
760 CPPUNIT_ASSERT_EQUAL("http://www.wxwidgets.org", url
.GetURL());
763 #endif //wxUSE_RICHTEXT