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 CPPUNIT_TEST( CaretPosition
);
48 CPPUNIT_TEST( Selection
);
49 WXUISIM_TEST( Editable
);
50 CPPUNIT_TEST( Range
);
51 CPPUNIT_TEST( Alignment
);
53 CPPUNIT_TEST( Italic
);
54 CPPUNIT_TEST( Underline
);
55 CPPUNIT_TEST( Indent
);
56 CPPUNIT_TEST( LineSpacing
);
57 CPPUNIT_TEST( ParagraphSpacing
);
58 CPPUNIT_TEST( TextColour
);
59 CPPUNIT_TEST( NumberedBullet
);
60 CPPUNIT_TEST( SymbolBullet
);
61 CPPUNIT_TEST( FontSize
);
63 CPPUNIT_TEST( Delete
);
65 CPPUNIT_TEST_SUITE_END();
67 void CharacterEvent();
71 void BufferResetEvent();
86 void ParagraphSpacing();
88 void NumberedBullet();
95 wxRichTextCtrl
* m_rich
;
97 DECLARE_NO_COPY_CLASS(RichTextCtrlTestCase
)
100 // register in the unnamed registry so that these tests are run by default
101 CPPUNIT_TEST_SUITE_REGISTRATION( RichTextCtrlTestCase
);
103 // also include in its own registry so that these tests can be run alone
104 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RichTextCtrlTestCase
, "RichTextCtrlTestCase" );
106 void RichTextCtrlTestCase::setUp()
108 m_rich
= new wxRichTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
109 wxDefaultPosition
, wxSize(400, 200), wxWANTS_CHARS
);
112 void RichTextCtrlTestCase::tearDown()
117 void RichTextCtrlTestCase::CharacterEvent()
119 #if wxUSE_UIACTIONSIMULATOR
121 // There seems to be an event sequence problem on GTK+ that causes the events
122 // to be disconnected before they're processed, generating spurious errors.
123 #if !defined(__WXGTK__)
124 EventCounter
character(m_rich
, wxEVT_COMMAND_RICHTEXT_CHARACTER
);
125 EventCounter
content(m_rich
, wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
);
129 wxUIActionSimulator sim
;
133 CPPUNIT_ASSERT_EQUAL(6, character
.GetCount());
134 CPPUNIT_ASSERT_EQUAL(6, content
.GetCount());
139 //As these are not characters they shouldn't count
140 sim
.Char(WXK_RETURN
);
144 CPPUNIT_ASSERT_EQUAL(0, character
.GetCount());
145 CPPUNIT_ASSERT_EQUAL(1, content
.GetCount());
150 void RichTextCtrlTestCase::DeleteEvent()
152 #if wxUSE_UIACTIONSIMULATOR
153 // There seems to be an event sequence problem on GTK+ that causes the events
154 // to be disconnected before they're processed, generating spurious errors.
155 #if !defined(__WXGTK__)
156 EventCounter
deleteevent(m_rich
, wxEVT_COMMAND_RICHTEXT_DELETE
);
157 EventCounter
contentdelete(m_rich
, wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
);
161 wxUIActionSimulator sim
;
164 sim
.Char(WXK_DELETE
);
167 CPPUNIT_ASSERT_EQUAL(2, deleteevent
.GetCount());
168 //Only one as the delete doesn't delete anthing
169 CPPUNIT_ASSERT_EQUAL(1, contentdelete
.GetCount());
174 void RichTextCtrlTestCase::ReturnEvent()
176 #if wxUSE_UIACTIONSIMULATOR
177 // There seems to be an event sequence problem on GTK+ that causes the events
178 // to be disconnected before they're processed, generating spurious errors.
179 #if !defined(__WXGTK__)
180 EventCounter
returnevent(m_rich
, wxEVT_COMMAND_RICHTEXT_RETURN
);
184 wxUIActionSimulator sim
;
185 sim
.Char(WXK_RETURN
);
188 CPPUNIT_ASSERT_EQUAL(1, returnevent
.GetCount());
193 void RichTextCtrlTestCase::StyleEvent()
195 EventCounter
stylechanged(m_rich
, wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
);
197 m_rich
->SetValue("Sometext");
198 m_rich
->SetStyle(0, 8, wxTextAttr(*wxRED
, *wxWHITE
));
200 CPPUNIT_ASSERT_EQUAL(1, stylechanged
.GetCount());
203 void RichTextCtrlTestCase::BufferResetEvent()
205 EventCounter
reset(m_rich
, wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
);
207 m_rich
->AppendText("more text!");
208 m_rich
->SetValue("");
210 CPPUNIT_ASSERT_EQUAL(1, reset
.GetCount());
213 m_rich
->AppendText("more text!");
216 CPPUNIT_ASSERT_EQUAL(1, reset
.GetCount());
220 //We expect a buffer reset here as setvalue clears the existing text
221 m_rich
->SetValue("replace");
222 CPPUNIT_ASSERT_EQUAL(1, reset
.GetCount());
225 void RichTextCtrlTestCase::UrlEvent()
227 #if wxUSE_UIACTIONSIMULATOR
228 // Mouse up event not being caught on GTK+
229 #if !defined(__WXGTK__)
230 EventCounter
url(m_rich
, wxEVT_COMMAND_TEXT_URL
);
232 m_rich
->BeginURL("http://www.wxwidgets.org");
233 m_rich
->WriteText("http://www.wxwidgets.org");
236 wxUIActionSimulator sim
;
237 sim
.MouseMove(m_rich
->ClientToScreen(wxPoint(10, 10)));
243 CPPUNIT_ASSERT_EQUAL(1, url
.GetCount());
248 void RichTextCtrlTestCase::TextEvent()
250 #if wxUSE_UIACTIONSIMULATOR
251 #if !defined(__WXGTK__)
252 EventCounter
updated(m_rich
, wxEVT_COMMAND_TEXT_UPDATED
);
256 wxUIActionSimulator sim
;
260 CPPUNIT_ASSERT_EQUAL("abcdef", m_rich
->GetValue());
261 CPPUNIT_ASSERT_EQUAL(6, updated
.GetCount());
266 void RichTextCtrlTestCase::CutCopyPaste()
269 m_rich
->AppendText("sometext");
272 if(m_rich
->CanCut() && m_rich
->CanPaste())
275 CPPUNIT_ASSERT(m_rich
->IsEmpty());
280 CPPUNIT_ASSERT_EQUAL("sometext", m_rich
->GetValue());
285 if(m_rich
->CanCopy() && m_rich
->CanPaste())
289 CPPUNIT_ASSERT(m_rich
->IsEmpty());
294 CPPUNIT_ASSERT_EQUAL("sometext", m_rich
->GetValue());
299 void RichTextCtrlTestCase::UndoRedo()
301 m_rich
->AppendText("sometext");
303 CPPUNIT_ASSERT(m_rich
->CanUndo());
307 CPPUNIT_ASSERT(m_rich
->IsEmpty());
308 CPPUNIT_ASSERT(m_rich
->CanRedo());
312 CPPUNIT_ASSERT_EQUAL("sometext", m_rich
->GetValue());
314 m_rich
->AppendText("Batch undo");
317 //Also test batch operations
318 m_rich
->BeginBatchUndo("batchtest");
320 m_rich
->ApplyBoldToSelection();
321 m_rich
->ApplyItalicToSelection();
323 m_rich
->EndBatchUndo();
325 CPPUNIT_ASSERT(m_rich
->CanUndo());
329 CPPUNIT_ASSERT(!m_rich
->IsSelectionBold());
330 CPPUNIT_ASSERT(!m_rich
->IsSelectionItalics());
331 CPPUNIT_ASSERT(m_rich
->CanRedo());
335 CPPUNIT_ASSERT(m_rich
->IsSelectionBold());
336 CPPUNIT_ASSERT(m_rich
->IsSelectionItalics());
338 //And surpressing undo
339 m_rich
->BeginSuppressUndo();
341 m_rich
->AppendText("Can't undo this");
343 CPPUNIT_ASSERT(m_rich
->CanUndo());
345 m_rich
->EndSuppressUndo();
348 void RichTextCtrlTestCase::CaretPosition()
350 m_rich
->AddParagraph("This is paragraph one");
351 m_rich
->AddParagraph("Paragraph two\n has \nlots of\n lines");
353 m_rich
->SetInsertionPoint(2);
355 CPPUNIT_ASSERT_EQUAL(1, m_rich
->GetCaretPosition());
357 m_rich
->MoveToParagraphStart();
359 CPPUNIT_ASSERT_EQUAL(0, m_rich
->GetCaretPosition());
362 m_rich
->MoveRight(2);
366 CPPUNIT_ASSERT_EQUAL(2, m_rich
->GetCaretPosition());
368 m_rich
->MoveToParagraphEnd();
370 CPPUNIT_ASSERT_EQUAL(21, m_rich
->GetCaretPosition());
372 m_rich
->MoveToLineStart();
374 CPPUNIT_ASSERT_EQUAL(0, m_rich
->GetCaretPosition());
376 m_rich
->MoveToLineEnd();
378 CPPUNIT_ASSERT_EQUAL(21, m_rich
->GetCaretPosition());
381 void RichTextCtrlTestCase::Selection()
383 m_rich
->SetValue("some more text");
387 CPPUNIT_ASSERT_EQUAL("some more text", m_rich
->GetStringSelection());
389 m_rich
->SelectNone();
391 CPPUNIT_ASSERT_EQUAL("", m_rich
->GetStringSelection());
393 m_rich
->SelectWord(1);
395 CPPUNIT_ASSERT_EQUAL("some", m_rich
->GetStringSelection());
397 m_rich
->SetSelection(5, 14);
399 CPPUNIT_ASSERT_EQUAL("more text", m_rich
->GetStringSelection());
401 wxRichTextRange
range(5, 9);
403 m_rich
->SetSelectionRange(range
);
405 CPPUNIT_ASSERT_EQUAL("more", m_rich
->GetStringSelection());
408 void RichTextCtrlTestCase::Editable()
410 #if wxUSE_UIACTIONSIMULATOR
411 #if !defined(__WXGTK__)
412 EventCounter
updated(m_rich
, wxEVT_COMMAND_TEXT_UPDATED
);
416 wxUIActionSimulator sim
;
420 CPPUNIT_ASSERT_EQUAL("abcdef", m_rich
->GetValue());
421 CPPUNIT_ASSERT_EQUAL(6, updated
.GetCount());
424 m_rich
->SetEditable(false);
428 CPPUNIT_ASSERT_EQUAL("abcdef", m_rich
->GetValue());
429 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
434 void RichTextCtrlTestCase::Range()
436 wxRichTextRange
range(0, 10);
438 CPPUNIT_ASSERT_EQUAL(0, range
.GetStart());
439 CPPUNIT_ASSERT_EQUAL(10, range
.GetEnd());
440 CPPUNIT_ASSERT_EQUAL(11, range
.GetLength());
441 CPPUNIT_ASSERT(range
.Contains(5));
443 wxRichTextRange
outside(12, 14);
445 CPPUNIT_ASSERT(outside
.IsOutside(range
));
447 wxRichTextRange
inside(6, 7);
449 CPPUNIT_ASSERT(inside
.IsWithin(range
));
451 range
.LimitTo(inside
);
453 CPPUNIT_ASSERT(inside
== range
);
454 CPPUNIT_ASSERT(inside
+ range
== outside
);
455 CPPUNIT_ASSERT(outside
- range
== inside
);
460 CPPUNIT_ASSERT_EQUAL(4, range
.GetStart());
461 CPPUNIT_ASSERT_EQUAL(6, range
.GetEnd());
462 CPPUNIT_ASSERT_EQUAL(3, range
.GetLength());
464 inside
.SetRange(6, 4);
467 CPPUNIT_ASSERT(inside
== range
);
470 void RichTextCtrlTestCase::Alignment()
472 m_rich
->SetValue("text to align");
475 m_rich
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT
);
477 CPPUNIT_ASSERT(m_rich
->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT
));
479 m_rich
->BeginAlignment(wxTEXT_ALIGNMENT_CENTRE
);
480 m_rich
->AddParagraph("middle aligned");
481 m_rich
->EndAlignment();
483 m_rich
->SetSelection(20, 25);
485 CPPUNIT_ASSERT(m_rich
->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE
));
488 void RichTextCtrlTestCase::Bold()
490 m_rich
->SetValue("text to bold");
492 m_rich
->ApplyBoldToSelection();
494 CPPUNIT_ASSERT(m_rich
->IsSelectionBold());
497 m_rich
->AddParagraph("bold paragraph");
499 m_rich
->AddParagraph("not bold paragraph");
501 m_rich
->SetSelection(15, 20);
503 CPPUNIT_ASSERT(m_rich
->IsSelectionBold());
505 m_rich
->SetSelection(30, 35);
507 CPPUNIT_ASSERT(!m_rich
->IsSelectionBold());
510 void RichTextCtrlTestCase::Italic()
512 m_rich
->SetValue("text to italic");
514 m_rich
->ApplyItalicToSelection();
516 CPPUNIT_ASSERT(m_rich
->IsSelectionItalics());
518 m_rich
->BeginItalic();
519 m_rich
->AddParagraph("italic paragraph");
521 m_rich
->AddParagraph("not italic paragraph");
523 m_rich
->SetSelection(20, 25);
525 CPPUNIT_ASSERT(m_rich
->IsSelectionItalics());
527 m_rich
->SetSelection(35, 40);
529 CPPUNIT_ASSERT(!m_rich
->IsSelectionItalics());
532 void RichTextCtrlTestCase::Underline()
534 m_rich
->SetValue("text to underline");
536 m_rich
->ApplyUnderlineToSelection();
538 CPPUNIT_ASSERT(m_rich
->IsSelectionUnderlined());
540 m_rich
->BeginUnderline();
541 m_rich
->AddParagraph("underline paragraph");
542 m_rich
->EndUnderline();
543 m_rich
->AddParagraph("not underline paragraph");
545 m_rich
->SetSelection(20, 25);
547 CPPUNIT_ASSERT(m_rich
->IsSelectionUnderlined());
549 m_rich
->SetSelection(40, 45);
551 CPPUNIT_ASSERT(!m_rich
->IsSelectionUnderlined());
554 void RichTextCtrlTestCase::Indent()
556 m_rich
->BeginLeftIndent(12, -5);
557 m_rich
->BeginRightIndent(14);
558 m_rich
->AddParagraph("A paragraph with indents");
559 m_rich
->EndLeftIndent();
560 m_rich
->EndRightIndent();
561 m_rich
->AddParagraph("No more indent");
564 m_rich
->GetStyle(5, indent
);
566 CPPUNIT_ASSERT_EQUAL(12, indent
.GetLeftIndent());
567 CPPUNIT_ASSERT_EQUAL(-5, indent
.GetLeftSubIndent());
568 CPPUNIT_ASSERT_EQUAL(14, indent
.GetRightIndent());
570 m_rich
->GetStyle(35, indent
);
572 CPPUNIT_ASSERT_EQUAL(0, indent
.GetLeftIndent());
573 CPPUNIT_ASSERT_EQUAL(0, indent
.GetLeftSubIndent());
574 CPPUNIT_ASSERT_EQUAL(0, indent
.GetRightIndent());
577 void RichTextCtrlTestCase::LineSpacing()
579 m_rich
->BeginLineSpacing(20);
580 m_rich
->AddParagraph("double spaced");
581 m_rich
->EndLineSpacing();
582 m_rich
->BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF
);
583 m_rich
->AddParagraph("1.5 spaced");
584 m_rich
->EndLineSpacing();
585 m_rich
->AddParagraph("normally spaced");
588 m_rich
->GetStyle(5, spacing
);
590 CPPUNIT_ASSERT_EQUAL(20, spacing
.GetLineSpacing());
592 m_rich
->GetStyle(20, spacing
);
594 CPPUNIT_ASSERT_EQUAL(15, spacing
.GetLineSpacing());
596 m_rich
->GetStyle(30, spacing
);
598 CPPUNIT_ASSERT_EQUAL(10, spacing
.GetLineSpacing());
601 void RichTextCtrlTestCase::ParagraphSpacing()
603 m_rich
->BeginParagraphSpacing(15, 20);
604 m_rich
->AddParagraph("spaced paragraph");
605 m_rich
->EndParagraphSpacing();
606 m_rich
->AddParagraph("non-spaced paragraph");
609 m_rich
->GetStyle(5, spacing
);
611 CPPUNIT_ASSERT_EQUAL(15, spacing
.GetParagraphSpacingBefore());
612 CPPUNIT_ASSERT_EQUAL(20, spacing
.GetParagraphSpacingAfter());
614 m_rich
->GetStyle(25, spacing
);
616 //Make sure we test against the defaults
617 CPPUNIT_ASSERT_EQUAL(m_rich
->GetBasicStyle().GetParagraphSpacingBefore(),
618 spacing
.GetParagraphSpacingBefore());
619 CPPUNIT_ASSERT_EQUAL(m_rich
->GetBasicStyle().GetParagraphSpacingAfter(),
620 spacing
.GetParagraphSpacingAfter());
623 void RichTextCtrlTestCase::TextColour()
625 m_rich
->BeginTextColour(*wxRED
);
626 m_rich
->AddParagraph("red paragraph");
627 m_rich
->EndTextColour();
628 m_rich
->AddParagraph("default paragraph");
631 m_rich
->GetStyle(5, colour
);
633 CPPUNIT_ASSERT_EQUAL(*wxRED
, colour
.GetTextColour());
635 m_rich
->GetStyle(25, colour
);
637 CPPUNIT_ASSERT_EQUAL(m_rich
->GetBasicStyle().GetTextColour(),
638 colour
.GetTextColour());
641 void RichTextCtrlTestCase::NumberedBullet()
643 m_rich
->BeginNumberedBullet(1, 15, 20);
644 m_rich
->AddParagraph("bullet one");
645 m_rich
->EndNumberedBullet();
646 m_rich
->BeginNumberedBullet(2, 25, -5);
647 m_rich
->AddParagraph("bullet two");
648 m_rich
->EndNumberedBullet();
651 m_rich
->GetStyle(5, bullet
);
653 CPPUNIT_ASSERT(bullet
.HasBulletStyle());
654 CPPUNIT_ASSERT(bullet
.HasBulletNumber());
655 CPPUNIT_ASSERT_EQUAL(1, bullet
.GetBulletNumber());
656 CPPUNIT_ASSERT_EQUAL(15, bullet
.GetLeftIndent());
657 CPPUNIT_ASSERT_EQUAL(20, bullet
.GetLeftSubIndent());
659 m_rich
->GetStyle(15, bullet
);
661 CPPUNIT_ASSERT(bullet
.HasBulletStyle());
662 CPPUNIT_ASSERT(bullet
.HasBulletNumber());
663 CPPUNIT_ASSERT_EQUAL(2, bullet
.GetBulletNumber());
664 CPPUNIT_ASSERT_EQUAL(25, bullet
.GetLeftIndent());
665 CPPUNIT_ASSERT_EQUAL(-5, bullet
.GetLeftSubIndent());
668 void RichTextCtrlTestCase::SymbolBullet()
670 m_rich
->BeginSymbolBullet("*", 15, 20);
671 m_rich
->AddParagraph("bullet one");
672 m_rich
->EndSymbolBullet();
673 m_rich
->BeginSymbolBullet("%", 25, -5);
674 m_rich
->AddParagraph("bullet two");
675 m_rich
->EndSymbolBullet();
678 m_rich
->GetStyle(5, bullet
);
680 CPPUNIT_ASSERT(bullet
.HasBulletStyle());
681 CPPUNIT_ASSERT(bullet
.HasBulletText());
682 CPPUNIT_ASSERT_EQUAL("*", bullet
.GetBulletText());
683 CPPUNIT_ASSERT_EQUAL(15, bullet
.GetLeftIndent());
684 CPPUNIT_ASSERT_EQUAL(20, bullet
.GetLeftSubIndent());
686 m_rich
->GetStyle(15, bullet
);
688 CPPUNIT_ASSERT(bullet
.HasBulletStyle());
689 CPPUNIT_ASSERT(bullet
.HasBulletText());
690 CPPUNIT_ASSERT_EQUAL("%", bullet
.GetBulletText());
691 CPPUNIT_ASSERT_EQUAL(25, bullet
.GetLeftIndent());
692 CPPUNIT_ASSERT_EQUAL(-5, bullet
.GetLeftSubIndent());
695 void RichTextCtrlTestCase::FontSize()
697 m_rich
->BeginFontSize(24);
698 m_rich
->AddParagraph("Large text");
699 m_rich
->EndFontSize();
702 m_rich
->GetStyle(5, size
);
704 CPPUNIT_ASSERT(size
.HasFontSize());
705 CPPUNIT_ASSERT_EQUAL(24, size
.GetFontSize());
708 void RichTextCtrlTestCase::Font()
710 wxFont
font(14, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
711 m_rich
->BeginFont(font
);
712 m_rich
->AddParagraph("paragraph with font");
715 wxTextAttr fontstyle
;
716 m_rich
->GetStyle(5, fontstyle
);
718 CPPUNIT_ASSERT_EQUAL(font
, fontstyle
.GetFont());
721 void RichTextCtrlTestCase::Delete()
723 m_rich
->AddParagraph("here is a long long line in a paragraph");
724 m_rich
->SetSelection(0, 6);
726 CPPUNIT_ASSERT(m_rich
->CanDeleteSelection());
728 m_rich
->DeleteSelection();
730 CPPUNIT_ASSERT_EQUAL("is a long long line in a paragraph", m_rich
->GetValue());
732 m_rich
->SetSelection(0, 5);
734 CPPUNIT_ASSERT(m_rich
->CanDeleteSelection());
736 m_rich
->DeleteSelectedContent();
738 CPPUNIT_ASSERT_EQUAL("long long line in a paragraph", m_rich
->GetValue());
740 m_rich
->Delete(wxRichTextRange(14, 29));
742 CPPUNIT_ASSERT_EQUAL("long long line", m_rich
->GetValue());
745 void RichTextCtrlTestCase::Url()
747 m_rich
->BeginURL("http://www.wxwidgets.org");
748 m_rich
->WriteText("http://www.wxwidgets.org");
752 m_rich
->GetStyle(5, url
);
754 CPPUNIT_ASSERT(url
.HasURL());
755 CPPUNIT_ASSERT_EQUAL("http://www.wxwidgets.org", url
.GetURL());
758 #endif //wxUSE_RICHTEXT