richtextctrltest.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/richtextctrltest.cpp
  3. // Purpose: wxRichTextCtrl unit test
  4. // Author: Steven Lamerton
  5. // Created: 2010-07-07
  6. // Copyright: (c) 2010 Steven Lamerton
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #if wxUSE_RICHTEXT
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. #ifndef WX_PRECOMP
  14. #include "wx/app.h"
  15. #endif // WX_PRECOMP
  16. #include "wx/richtext/richtextctrl.h"
  17. #include "wx/richtext/richtextstyles.h"
  18. #include "testableframe.h"
  19. #include "asserthelper.h"
  20. #include "wx/uiaction.h"
  21. class RichTextCtrlTestCase : public CppUnit::TestCase
  22. {
  23. public:
  24. RichTextCtrlTestCase() { }
  25. void setUp();
  26. void tearDown();
  27. private:
  28. CPPUNIT_TEST_SUITE( RichTextCtrlTestCase );
  29. WXUISIM_TEST( CharacterEvent );
  30. WXUISIM_TEST( DeleteEvent );
  31. WXUISIM_TEST( ReturnEvent );
  32. CPPUNIT_TEST( StyleEvent );
  33. CPPUNIT_TEST( BufferResetEvent );
  34. WXUISIM_TEST( UrlEvent );
  35. WXUISIM_TEST( TextEvent );
  36. CPPUNIT_TEST( CutCopyPaste );
  37. CPPUNIT_TEST( UndoRedo );
  38. CPPUNIT_TEST( CaretPosition );
  39. CPPUNIT_TEST( Selection );
  40. WXUISIM_TEST( Editable );
  41. CPPUNIT_TEST( Range );
  42. CPPUNIT_TEST( Alignment );
  43. CPPUNIT_TEST( Bold );
  44. CPPUNIT_TEST( Italic );
  45. CPPUNIT_TEST( Underline );
  46. CPPUNIT_TEST( Indent );
  47. CPPUNIT_TEST( LineSpacing );
  48. CPPUNIT_TEST( ParagraphSpacing );
  49. CPPUNIT_TEST( TextColour );
  50. CPPUNIT_TEST( NumberedBullet );
  51. CPPUNIT_TEST( SymbolBullet );
  52. CPPUNIT_TEST( FontSize );
  53. CPPUNIT_TEST( Font );
  54. CPPUNIT_TEST( Delete );
  55. CPPUNIT_TEST( Url );
  56. CPPUNIT_TEST( Table );
  57. CPPUNIT_TEST_SUITE_END();
  58. void CharacterEvent();
  59. void DeleteEvent();
  60. void ReturnEvent();
  61. void StyleEvent();
  62. void BufferResetEvent();
  63. void UrlEvent();
  64. void TextEvent();
  65. void CutCopyPaste();
  66. void UndoRedo();
  67. void CaretPosition();
  68. void Selection();
  69. void Editable();
  70. void Range();
  71. void Alignment();
  72. void Bold();
  73. void Italic();
  74. void Underline();
  75. void Indent();
  76. void LineSpacing();
  77. void ParagraphSpacing();
  78. void TextColour();
  79. void NumberedBullet();
  80. void SymbolBullet();
  81. void FontSize();
  82. void Font();
  83. void Delete();
  84. void Url();
  85. void Table();
  86. wxRichTextCtrl* m_rich;
  87. DECLARE_NO_COPY_CLASS(RichTextCtrlTestCase)
  88. };
  89. // register in the unnamed registry so that these tests are run by default
  90. CPPUNIT_TEST_SUITE_REGISTRATION( RichTextCtrlTestCase );
  91. // also include in its own registry so that these tests can be run alone
  92. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RichTextCtrlTestCase, "RichTextCtrlTestCase" );
  93. void RichTextCtrlTestCase::setUp()
  94. {
  95. m_rich = new wxRichTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
  96. wxDefaultPosition, wxSize(400, 200), wxWANTS_CHARS);
  97. }
  98. void RichTextCtrlTestCase::tearDown()
  99. {
  100. wxDELETE(m_rich);
  101. }
  102. void RichTextCtrlTestCase::CharacterEvent()
  103. {
  104. #if wxUSE_UIACTIONSIMULATOR
  105. // There seems to be an event sequence problem on GTK+ that causes the events
  106. // to be disconnected before they're processed, generating spurious errors.
  107. #if !defined(__WXGTK__)
  108. EventCounter character(m_rich, wxEVT_RICHTEXT_CHARACTER);
  109. EventCounter content(m_rich, wxEVT_RICHTEXT_CONTENT_INSERTED);
  110. m_rich->SetFocus();
  111. wxUIActionSimulator sim;
  112. sim.Text("abcdef");
  113. wxYield();
  114. CPPUNIT_ASSERT_EQUAL(6, character.GetCount());
  115. CPPUNIT_ASSERT_EQUAL(6, content.GetCount());
  116. character.Clear();
  117. content.Clear();
  118. //As these are not characters they shouldn't count
  119. sim.Char(WXK_RETURN);
  120. sim.Char(WXK_SHIFT);
  121. wxYield();
  122. CPPUNIT_ASSERT_EQUAL(0, character.GetCount());
  123. CPPUNIT_ASSERT_EQUAL(1, content.GetCount());
  124. #endif
  125. #endif
  126. }
  127. void RichTextCtrlTestCase::DeleteEvent()
  128. {
  129. #if wxUSE_UIACTIONSIMULATOR
  130. // There seems to be an event sequence problem on GTK+ that causes the events
  131. // to be disconnected before they're processed, generating spurious errors.
  132. #if !defined(__WXGTK__)
  133. EventCounter deleteevent(m_rich, wxEVT_RICHTEXT_DELETE);
  134. EventCounter contentdelete(m_rich, wxEVT_RICHTEXT_CONTENT_DELETED);
  135. m_rich->SetFocus();
  136. wxUIActionSimulator sim;
  137. sim.Text("abcdef");
  138. sim.Char(WXK_BACK);
  139. sim.Char(WXK_DELETE);
  140. wxYield();
  141. CPPUNIT_ASSERT_EQUAL(2, deleteevent.GetCount());
  142. //Only one as the delete doesn't delete anthing
  143. CPPUNIT_ASSERT_EQUAL(1, contentdelete.GetCount());
  144. #endif
  145. #endif
  146. }
  147. void RichTextCtrlTestCase::ReturnEvent()
  148. {
  149. #if wxUSE_UIACTIONSIMULATOR
  150. // There seems to be an event sequence problem on GTK+ that causes the events
  151. // to be disconnected before they're processed, generating spurious errors.
  152. #if !defined(__WXGTK__)
  153. EventCounter returnevent(m_rich, wxEVT_RICHTEXT_RETURN);
  154. m_rich->SetFocus();
  155. wxUIActionSimulator sim;
  156. sim.Char(WXK_RETURN);
  157. wxYield();
  158. CPPUNIT_ASSERT_EQUAL(1, returnevent.GetCount());
  159. #endif
  160. #endif
  161. }
  162. void RichTextCtrlTestCase::StyleEvent()
  163. {
  164. EventCounter stylechanged(m_rich, wxEVT_RICHTEXT_STYLE_CHANGED);
  165. m_rich->SetValue("Sometext");
  166. m_rich->SetStyle(0, 8, wxTextAttr(*wxRED, *wxWHITE));
  167. CPPUNIT_ASSERT_EQUAL(1, stylechanged.GetCount());
  168. }
  169. void RichTextCtrlTestCase::BufferResetEvent()
  170. {
  171. EventCounter reset(m_rich, wxEVT_RICHTEXT_BUFFER_RESET);
  172. m_rich->AppendText("more text!");
  173. m_rich->SetValue("");
  174. CPPUNIT_ASSERT_EQUAL(1, reset.GetCount());
  175. reset.Clear();
  176. m_rich->AppendText("more text!");
  177. m_rich->Clear();
  178. CPPUNIT_ASSERT_EQUAL(1, reset.GetCount());
  179. reset.Clear();
  180. //We expect a buffer reset here as setvalue clears the existing text
  181. m_rich->SetValue("replace");
  182. CPPUNIT_ASSERT_EQUAL(1, reset.GetCount());
  183. }
  184. void RichTextCtrlTestCase::UrlEvent()
  185. {
  186. #if wxUSE_UIACTIONSIMULATOR
  187. // Mouse up event not being caught on GTK+
  188. #if !defined(__WXGTK__)
  189. EventCounter url(m_rich, wxEVT_TEXT_URL);
  190. m_rich->BeginURL("http://www.wxwidgets.org");
  191. m_rich->WriteText("http://www.wxwidgets.org");
  192. m_rich->EndURL();
  193. wxUIActionSimulator sim;
  194. sim.MouseMove(m_rich->ClientToScreen(wxPoint(10, 10)));
  195. wxYield();
  196. sim.MouseClick();
  197. wxYield();
  198. CPPUNIT_ASSERT_EQUAL(1, url.GetCount());
  199. #endif
  200. #endif
  201. }
  202. void RichTextCtrlTestCase::TextEvent()
  203. {
  204. #if wxUSE_UIACTIONSIMULATOR
  205. #if !defined(__WXGTK__)
  206. EventCounter updated(m_rich, wxEVT_TEXT);
  207. m_rich->SetFocus();
  208. wxUIActionSimulator sim;
  209. sim.Text("abcdef");
  210. wxYield();
  211. CPPUNIT_ASSERT_EQUAL("abcdef", m_rich->GetValue());
  212. CPPUNIT_ASSERT_EQUAL(6, updated.GetCount());
  213. #endif
  214. #endif
  215. }
  216. void RichTextCtrlTestCase::CutCopyPaste()
  217. {
  218. #ifndef __WXOSX__
  219. m_rich->AppendText("sometext");
  220. m_rich->SelectAll();
  221. if(m_rich->CanCut() && m_rich->CanPaste())
  222. {
  223. m_rich->Cut();
  224. CPPUNIT_ASSERT(m_rich->IsEmpty());
  225. wxYield();
  226. m_rich->Paste();
  227. CPPUNIT_ASSERT_EQUAL("sometext", m_rich->GetValue());
  228. }
  229. m_rich->SelectAll();
  230. if(m_rich->CanCopy() && m_rich->CanPaste())
  231. {
  232. m_rich->Copy();
  233. m_rich->Clear();
  234. CPPUNIT_ASSERT(m_rich->IsEmpty());
  235. wxYield();
  236. m_rich->Paste();
  237. CPPUNIT_ASSERT_EQUAL("sometext", m_rich->GetValue());
  238. }
  239. #endif
  240. }
  241. void RichTextCtrlTestCase::UndoRedo()
  242. {
  243. m_rich->AppendText("sometext");
  244. CPPUNIT_ASSERT(m_rich->CanUndo());
  245. m_rich->Undo();
  246. CPPUNIT_ASSERT(m_rich->IsEmpty());
  247. CPPUNIT_ASSERT(m_rich->CanRedo());
  248. m_rich->Redo();
  249. CPPUNIT_ASSERT_EQUAL("sometext", m_rich->GetValue());
  250. m_rich->AppendText("Batch undo");
  251. m_rich->SelectAll();
  252. //Also test batch operations
  253. m_rich->BeginBatchUndo("batchtest");
  254. m_rich->ApplyBoldToSelection();
  255. m_rich->ApplyItalicToSelection();
  256. m_rich->EndBatchUndo();
  257. CPPUNIT_ASSERT(m_rich->CanUndo());
  258. m_rich->Undo();
  259. CPPUNIT_ASSERT(!m_rich->IsSelectionBold());
  260. CPPUNIT_ASSERT(!m_rich->IsSelectionItalics());
  261. CPPUNIT_ASSERT(m_rich->CanRedo());
  262. m_rich->Redo();
  263. CPPUNIT_ASSERT(m_rich->IsSelectionBold());
  264. CPPUNIT_ASSERT(m_rich->IsSelectionItalics());
  265. //And surpressing undo
  266. m_rich->BeginSuppressUndo();
  267. m_rich->AppendText("Can't undo this");
  268. CPPUNIT_ASSERT(m_rich->CanUndo());
  269. m_rich->EndSuppressUndo();
  270. }
  271. void RichTextCtrlTestCase::CaretPosition()
  272. {
  273. m_rich->AddParagraph("This is paragraph one");
  274. m_rich->AddParagraph("Paragraph two\n has \nlots of\n lines");
  275. m_rich->SetInsertionPoint(2);
  276. CPPUNIT_ASSERT_EQUAL(1, m_rich->GetCaretPosition());
  277. m_rich->MoveToParagraphStart();
  278. CPPUNIT_ASSERT_EQUAL(0, m_rich->GetCaretPosition());
  279. m_rich->MoveRight();
  280. m_rich->MoveRight(2);
  281. m_rich->MoveLeft(1);
  282. m_rich->MoveLeft(0);
  283. CPPUNIT_ASSERT_EQUAL(2, m_rich->GetCaretPosition());
  284. m_rich->MoveToParagraphEnd();
  285. CPPUNIT_ASSERT_EQUAL(21, m_rich->GetCaretPosition());
  286. m_rich->MoveToLineStart();
  287. CPPUNIT_ASSERT_EQUAL(0, m_rich->GetCaretPosition());
  288. m_rich->MoveToLineEnd();
  289. CPPUNIT_ASSERT_EQUAL(21, m_rich->GetCaretPosition());
  290. }
  291. void RichTextCtrlTestCase::Selection()
  292. {
  293. m_rich->SetValue("some more text");
  294. m_rich->SelectAll();
  295. CPPUNIT_ASSERT_EQUAL("some more text", m_rich->GetStringSelection());
  296. m_rich->SelectNone();
  297. CPPUNIT_ASSERT_EQUAL("", m_rich->GetStringSelection());
  298. m_rich->SelectWord(1);
  299. CPPUNIT_ASSERT_EQUAL("some", m_rich->GetStringSelection());
  300. m_rich->SetSelection(5, 14);
  301. CPPUNIT_ASSERT_EQUAL("more text", m_rich->GetStringSelection());
  302. wxRichTextRange range(5, 9);
  303. m_rich->SetSelectionRange(range);
  304. CPPUNIT_ASSERT_EQUAL("more", m_rich->GetStringSelection());
  305. }
  306. void RichTextCtrlTestCase::Editable()
  307. {
  308. #if wxUSE_UIACTIONSIMULATOR
  309. #if !defined(__WXGTK__)
  310. EventCounter updated(m_rich, wxEVT_TEXT);
  311. m_rich->SetFocus();
  312. wxUIActionSimulator sim;
  313. sim.Text("abcdef");
  314. wxYield();
  315. CPPUNIT_ASSERT_EQUAL("abcdef", m_rich->GetValue());
  316. CPPUNIT_ASSERT_EQUAL(6, updated.GetCount());
  317. updated.Clear();
  318. m_rich->SetEditable(false);
  319. sim.Text("gh");
  320. wxYield();
  321. CPPUNIT_ASSERT_EQUAL("abcdef", m_rich->GetValue());
  322. CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
  323. #endif
  324. #endif
  325. }
  326. void RichTextCtrlTestCase::Range()
  327. {
  328. wxRichTextRange range(0, 10);
  329. CPPUNIT_ASSERT_EQUAL(0, range.GetStart());
  330. CPPUNIT_ASSERT_EQUAL(10, range.GetEnd());
  331. CPPUNIT_ASSERT_EQUAL(11, range.GetLength());
  332. CPPUNIT_ASSERT(range.Contains(5));
  333. wxRichTextRange outside(12, 14);
  334. CPPUNIT_ASSERT(outside.IsOutside(range));
  335. wxRichTextRange inside(6, 7);
  336. CPPUNIT_ASSERT(inside.IsWithin(range));
  337. range.LimitTo(inside);
  338. CPPUNIT_ASSERT(inside == range);
  339. CPPUNIT_ASSERT(inside + range == outside);
  340. CPPUNIT_ASSERT(outside - range == inside);
  341. range.SetStart(4);
  342. range.SetEnd(6);
  343. CPPUNIT_ASSERT_EQUAL(4, range.GetStart());
  344. CPPUNIT_ASSERT_EQUAL(6, range.GetEnd());
  345. CPPUNIT_ASSERT_EQUAL(3, range.GetLength());
  346. inside.SetRange(6, 4);
  347. inside.Swap();
  348. CPPUNIT_ASSERT(inside == range);
  349. }
  350. void RichTextCtrlTestCase::Alignment()
  351. {
  352. m_rich->SetValue("text to align");
  353. m_rich->SelectAll();
  354. m_rich->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT);
  355. CPPUNIT_ASSERT(m_rich->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT));
  356. m_rich->BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
  357. m_rich->AddParagraph("middle aligned");
  358. m_rich->EndAlignment();
  359. m_rich->SetSelection(20, 25);
  360. CPPUNIT_ASSERT(m_rich->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE));
  361. }
  362. void RichTextCtrlTestCase::Bold()
  363. {
  364. m_rich->SetValue("text to bold");
  365. m_rich->SelectAll();
  366. m_rich->ApplyBoldToSelection();
  367. CPPUNIT_ASSERT(m_rich->IsSelectionBold());
  368. m_rich->BeginBold();
  369. m_rich->AddParagraph("bold paragraph");
  370. m_rich->EndBold();
  371. m_rich->AddParagraph("not bold paragraph");
  372. m_rich->SetSelection(15, 20);
  373. CPPUNIT_ASSERT(m_rich->IsSelectionBold());
  374. m_rich->SetSelection(30, 35);
  375. CPPUNIT_ASSERT(!m_rich->IsSelectionBold());
  376. }
  377. void RichTextCtrlTestCase::Italic()
  378. {
  379. m_rich->SetValue("text to italic");
  380. m_rich->SelectAll();
  381. m_rich->ApplyItalicToSelection();
  382. CPPUNIT_ASSERT(m_rich->IsSelectionItalics());
  383. m_rich->BeginItalic();
  384. m_rich->AddParagraph("italic paragraph");
  385. m_rich->EndItalic();
  386. m_rich->AddParagraph("not italic paragraph");
  387. m_rich->SetSelection(20, 25);
  388. CPPUNIT_ASSERT(m_rich->IsSelectionItalics());
  389. m_rich->SetSelection(35, 40);
  390. CPPUNIT_ASSERT(!m_rich->IsSelectionItalics());
  391. }
  392. void RichTextCtrlTestCase::Underline()
  393. {
  394. m_rich->SetValue("text to underline");
  395. m_rich->SelectAll();
  396. m_rich->ApplyUnderlineToSelection();
  397. CPPUNIT_ASSERT(m_rich->IsSelectionUnderlined());
  398. m_rich->BeginUnderline();
  399. m_rich->AddParagraph("underline paragraph");
  400. m_rich->EndUnderline();
  401. m_rich->AddParagraph("not underline paragraph");
  402. m_rich->SetSelection(20, 25);
  403. CPPUNIT_ASSERT(m_rich->IsSelectionUnderlined());
  404. m_rich->SetSelection(40, 45);
  405. CPPUNIT_ASSERT(!m_rich->IsSelectionUnderlined());
  406. }
  407. void RichTextCtrlTestCase::Indent()
  408. {
  409. m_rich->BeginLeftIndent(12, -5);
  410. m_rich->BeginRightIndent(14);
  411. m_rich->AddParagraph("A paragraph with indents");
  412. m_rich->EndLeftIndent();
  413. m_rich->EndRightIndent();
  414. m_rich->AddParagraph("No more indent");
  415. wxTextAttr indent;
  416. m_rich->GetStyle(5, indent);
  417. CPPUNIT_ASSERT_EQUAL(12, indent.GetLeftIndent());
  418. CPPUNIT_ASSERT_EQUAL(-5, indent.GetLeftSubIndent());
  419. CPPUNIT_ASSERT_EQUAL(14, indent.GetRightIndent());
  420. m_rich->GetStyle(35, indent);
  421. CPPUNIT_ASSERT_EQUAL(0, indent.GetLeftIndent());
  422. CPPUNIT_ASSERT_EQUAL(0, indent.GetLeftSubIndent());
  423. CPPUNIT_ASSERT_EQUAL(0, indent.GetRightIndent());
  424. }
  425. void RichTextCtrlTestCase::LineSpacing()
  426. {
  427. m_rich->BeginLineSpacing(20);
  428. m_rich->AddParagraph("double spaced");
  429. m_rich->EndLineSpacing();
  430. m_rich->BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF);
  431. m_rich->AddParagraph("1.5 spaced");
  432. m_rich->EndLineSpacing();
  433. m_rich->AddParagraph("normally spaced");
  434. wxTextAttr spacing;
  435. m_rich->GetStyle(5, spacing);
  436. CPPUNIT_ASSERT_EQUAL(20, spacing.GetLineSpacing());
  437. m_rich->GetStyle(20, spacing);
  438. CPPUNIT_ASSERT_EQUAL(15, spacing.GetLineSpacing());
  439. m_rich->GetStyle(30, spacing);
  440. CPPUNIT_ASSERT_EQUAL(10, spacing.GetLineSpacing());
  441. }
  442. void RichTextCtrlTestCase::ParagraphSpacing()
  443. {
  444. m_rich->BeginParagraphSpacing(15, 20);
  445. m_rich->AddParagraph("spaced paragraph");
  446. m_rich->EndParagraphSpacing();
  447. m_rich->AddParagraph("non-spaced paragraph");
  448. wxTextAttr spacing;
  449. m_rich->GetStyle(5, spacing);
  450. CPPUNIT_ASSERT_EQUAL(15, spacing.GetParagraphSpacingBefore());
  451. CPPUNIT_ASSERT_EQUAL(20, spacing.GetParagraphSpacingAfter());
  452. m_rich->GetStyle(25, spacing);
  453. //Make sure we test against the defaults
  454. CPPUNIT_ASSERT_EQUAL(m_rich->GetBasicStyle().GetParagraphSpacingBefore(),
  455. spacing.GetParagraphSpacingBefore());
  456. CPPUNIT_ASSERT_EQUAL(m_rich->GetBasicStyle().GetParagraphSpacingAfter(),
  457. spacing.GetParagraphSpacingAfter());
  458. }
  459. void RichTextCtrlTestCase::TextColour()
  460. {
  461. m_rich->BeginTextColour(*wxRED);
  462. m_rich->AddParagraph("red paragraph");
  463. m_rich->EndTextColour();
  464. m_rich->AddParagraph("default paragraph");
  465. wxTextAttr colour;
  466. m_rich->GetStyle(5, colour);
  467. CPPUNIT_ASSERT_EQUAL(*wxRED, colour.GetTextColour());
  468. m_rich->GetStyle(25, colour);
  469. CPPUNIT_ASSERT_EQUAL(m_rich->GetBasicStyle().GetTextColour(),
  470. colour.GetTextColour());
  471. }
  472. void RichTextCtrlTestCase::NumberedBullet()
  473. {
  474. m_rich->BeginNumberedBullet(1, 15, 20);
  475. m_rich->AddParagraph("bullet one");
  476. m_rich->EndNumberedBullet();
  477. m_rich->BeginNumberedBullet(2, 25, -5);
  478. m_rich->AddParagraph("bullet two");
  479. m_rich->EndNumberedBullet();
  480. wxTextAttr bullet;
  481. m_rich->GetStyle(5, bullet);
  482. CPPUNIT_ASSERT(bullet.HasBulletStyle());
  483. CPPUNIT_ASSERT(bullet.HasBulletNumber());
  484. CPPUNIT_ASSERT_EQUAL(1, bullet.GetBulletNumber());
  485. CPPUNIT_ASSERT_EQUAL(15, bullet.GetLeftIndent());
  486. CPPUNIT_ASSERT_EQUAL(20, bullet.GetLeftSubIndent());
  487. m_rich->GetStyle(15, bullet);
  488. CPPUNIT_ASSERT(bullet.HasBulletStyle());
  489. CPPUNIT_ASSERT(bullet.HasBulletNumber());
  490. CPPUNIT_ASSERT_EQUAL(2, bullet.GetBulletNumber());
  491. CPPUNIT_ASSERT_EQUAL(25, bullet.GetLeftIndent());
  492. CPPUNIT_ASSERT_EQUAL(-5, bullet.GetLeftSubIndent());
  493. }
  494. void RichTextCtrlTestCase::SymbolBullet()
  495. {
  496. m_rich->BeginSymbolBullet("*", 15, 20);
  497. m_rich->AddParagraph("bullet one");
  498. m_rich->EndSymbolBullet();
  499. m_rich->BeginSymbolBullet("%", 25, -5);
  500. m_rich->AddParagraph("bullet two");
  501. m_rich->EndSymbolBullet();
  502. wxTextAttr bullet;
  503. m_rich->GetStyle(5, bullet);
  504. CPPUNIT_ASSERT(bullet.HasBulletStyle());
  505. CPPUNIT_ASSERT(bullet.HasBulletText());
  506. CPPUNIT_ASSERT_EQUAL("*", bullet.GetBulletText());
  507. CPPUNIT_ASSERT_EQUAL(15, bullet.GetLeftIndent());
  508. CPPUNIT_ASSERT_EQUAL(20, bullet.GetLeftSubIndent());
  509. m_rich->GetStyle(15, bullet);
  510. CPPUNIT_ASSERT(bullet.HasBulletStyle());
  511. CPPUNIT_ASSERT(bullet.HasBulletText());
  512. CPPUNIT_ASSERT_EQUAL("%", bullet.GetBulletText());
  513. CPPUNIT_ASSERT_EQUAL(25, bullet.GetLeftIndent());
  514. CPPUNIT_ASSERT_EQUAL(-5, bullet.GetLeftSubIndent());
  515. }
  516. void RichTextCtrlTestCase::FontSize()
  517. {
  518. m_rich->BeginFontSize(24);
  519. m_rich->AddParagraph("Large text");
  520. m_rich->EndFontSize();
  521. wxTextAttr size;
  522. m_rich->GetStyle(5, size);
  523. CPPUNIT_ASSERT(size.HasFontSize());
  524. CPPUNIT_ASSERT_EQUAL(24, size.GetFontSize());
  525. }
  526. void RichTextCtrlTestCase::Font()
  527. {
  528. wxFont font(14, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
  529. m_rich->BeginFont(font);
  530. m_rich->AddParagraph("paragraph with font");
  531. m_rich->EndFont();
  532. wxTextAttr fontstyle;
  533. m_rich->GetStyle(5, fontstyle);
  534. CPPUNIT_ASSERT_EQUAL(font, fontstyle.GetFont());
  535. }
  536. void RichTextCtrlTestCase::Delete()
  537. {
  538. m_rich->AddParagraph("here is a long long line in a paragraph");
  539. m_rich->SetSelection(0, 6);
  540. CPPUNIT_ASSERT(m_rich->CanDeleteSelection());
  541. m_rich->DeleteSelection();
  542. CPPUNIT_ASSERT_EQUAL("is a long long line in a paragraph", m_rich->GetValue());
  543. m_rich->SetSelection(0, 5);
  544. CPPUNIT_ASSERT(m_rich->CanDeleteSelection());
  545. m_rich->DeleteSelectedContent();
  546. CPPUNIT_ASSERT_EQUAL("long long line in a paragraph", m_rich->GetValue());
  547. m_rich->Delete(wxRichTextRange(14, 29));
  548. CPPUNIT_ASSERT_EQUAL("long long line", m_rich->GetValue());
  549. }
  550. void RichTextCtrlTestCase::Url()
  551. {
  552. m_rich->BeginURL("http://www.wxwidgets.org");
  553. m_rich->WriteText("http://www.wxwidgets.org");
  554. m_rich->EndURL();
  555. wxTextAttr url;
  556. m_rich->GetStyle(5, url);
  557. CPPUNIT_ASSERT(url.HasURL());
  558. CPPUNIT_ASSERT_EQUAL("http://www.wxwidgets.org", url.GetURL());
  559. }
  560. // Helper function for ::Table()
  561. wxRichTextTable* GetCurrentTableInstance(wxRichTextParagraph* para)
  562. {
  563. wxRichTextTable* table = wxDynamicCast(para->FindObjectAtPosition(0), wxRichTextTable);
  564. CPPUNIT_ASSERT(table);
  565. return table;
  566. }
  567. void RichTextCtrlTestCase::Table()
  568. {
  569. m_rich->BeginSuppressUndo();
  570. wxRichTextTable* table = m_rich->WriteTable(1, 1);
  571. m_rich->EndSuppressUndo();
  572. CPPUNIT_ASSERT(table);
  573. CPPUNIT_ASSERT(m_rich->CanUndo() == false);
  574. // Run the tests twice: first for the original table, then for a contained one
  575. for (int t = 0; t < 2; ++t)
  576. {
  577. size_t n; // FIXME-VC6: outside of the loops for VC6 only.
  578. // Undo() and Redo() switch table instances, so invalidating 'table'
  579. // The containing paragraph isn't altered, and so can be used to find the current object
  580. wxRichTextParagraph* para = wxDynamicCast(table->GetParent(), wxRichTextParagraph);
  581. CPPUNIT_ASSERT(para);
  582. CPPUNIT_ASSERT(table->GetColumnCount() == 1);
  583. CPPUNIT_ASSERT(table->GetRowCount() == 1);
  584. // Test adding columns and rows
  585. for (n = 0; n < 3; ++n)
  586. {
  587. m_rich->BeginBatchUndo("Add col and row");
  588. table->AddColumns(0, 1);
  589. table->AddRows(0, 1);
  590. m_rich->EndBatchUndo();
  591. }
  592. CPPUNIT_ASSERT(table->GetColumnCount() == 4);
  593. CPPUNIT_ASSERT(table->GetRowCount() == 4);
  594. // Test deleting columns and rows
  595. for (n = 0; n < 3; ++n)
  596. {
  597. m_rich->BeginBatchUndo("Delete col and row");
  598. table->DeleteColumns(table->GetColumnCount() - 1, 1);
  599. table->DeleteRows(table->GetRowCount() - 1, 1);
  600. m_rich->EndBatchUndo();
  601. }
  602. CPPUNIT_ASSERT(table->GetColumnCount() == 1);
  603. CPPUNIT_ASSERT(table->GetRowCount() == 1);
  604. // Test undo, first of the deletions...
  605. CPPUNIT_ASSERT(m_rich->CanUndo());
  606. for (n = 0; n < 3; ++n)
  607. {
  608. m_rich->Undo();
  609. }
  610. table = GetCurrentTableInstance(para);
  611. CPPUNIT_ASSERT(table->GetColumnCount() == 4);
  612. CPPUNIT_ASSERT(table->GetRowCount() == 4);
  613. // ...then the additions
  614. for (n = 0; n < 3; ++n)
  615. {
  616. m_rich->Undo();
  617. }
  618. table = GetCurrentTableInstance(para);
  619. CPPUNIT_ASSERT(table->GetColumnCount() == 1);
  620. CPPUNIT_ASSERT(table->GetRowCount() == 1);
  621. CPPUNIT_ASSERT(m_rich->CanUndo() == false);
  622. // Similarly test redo. Additions:
  623. CPPUNIT_ASSERT(m_rich->CanRedo());
  624. for (n = 0; n < 3; ++n)
  625. {
  626. m_rich->Redo();
  627. }
  628. table = GetCurrentTableInstance(para);
  629. CPPUNIT_ASSERT(table->GetColumnCount() == 4);
  630. CPPUNIT_ASSERT(table->GetRowCount() == 4);
  631. // Deletions:
  632. for (n = 0; n < 3; ++n)
  633. {
  634. m_rich->Redo();
  635. }
  636. table = GetCurrentTableInstance(para);
  637. CPPUNIT_ASSERT(table->GetColumnCount() == 1);
  638. CPPUNIT_ASSERT(table->GetRowCount() == 1);
  639. CPPUNIT_ASSERT(m_rich->CanRedo() == false);
  640. // Now test multiple addition and deletion, and also suppression
  641. m_rich->BeginSuppressUndo();
  642. table->AddColumns(0, 3);
  643. table->AddRows(0, 3);
  644. CPPUNIT_ASSERT(table->GetColumnCount() == 4);
  645. CPPUNIT_ASSERT(table->GetRowCount() == 4);
  646. // Only delete 2 of these. This makes it easy to be sure we're dealing with the child table when we loop
  647. table->DeleteColumns(0, 2);
  648. table->DeleteRows(0, 2);
  649. CPPUNIT_ASSERT(table->GetColumnCount() == 2);
  650. CPPUNIT_ASSERT(table->GetRowCount() == 2);
  651. m_rich->EndSuppressUndo();
  652. m_rich->GetCommandProcessor()->ClearCommands(); // otherwise the command-history from this loop will cause CPPUNIT_ASSERT failures in the next one
  653. if (t == 0)
  654. {
  655. // For round 2, re-run the tests on another table inside the last cell of the first one
  656. wxRichTextCell* cell = table->GetCell(table->GetRowCount() - 1, table->GetColumnCount() - 1);
  657. CPPUNIT_ASSERT(cell);
  658. m_rich->SetFocusObject(cell);
  659. m_rich->BeginSuppressUndo();
  660. table = m_rich->WriteTable(1, 1);
  661. m_rich->EndSuppressUndo();
  662. CPPUNIT_ASSERT(table);
  663. }
  664. }
  665. // Test ClearTable()
  666. table->ClearTable();
  667. CPPUNIT_ASSERT_EQUAL(0, table->GetCells().GetCount());
  668. CPPUNIT_ASSERT_EQUAL(0, table->GetColumnCount());
  669. CPPUNIT_ASSERT_EQUAL(0, table->GetRowCount());
  670. m_rich->Clear();
  671. m_rich->SetFocusObject(NULL);
  672. }
  673. #endif //wxUSE_RICHTEXT