test_tb_style_edit.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_test.h"
  6. #include "tb_editfield.h"
  7. #ifdef TB_UNIT_TESTING
  8. using namespace tb;
  9. TB_TEST_GROUP(tb_editfield)
  10. {
  11. TBEditField *edit;
  12. TBStyleEdit *sedit;
  13. // == Setup & helpers =====================================================
  14. TB_TEST(Setup)
  15. {
  16. TB_VERIFY(edit = new TBEditField());
  17. edit->SetMultiline(true);
  18. sedit = edit->GetStyleEdit();
  19. /** Set a size so the layout code will be called and we can do some layout tests. */
  20. edit->SetRect(TBRect(0, 0, 1000, 1000));
  21. /** Force windows style line breaks so testing is the same on all platforms. */
  22. sedit->SetWindowsStyleBreak(true);
  23. }
  24. TB_TEST(Cleanup) { delete edit; }
  25. // == Tests ===============================================================
  26. TB_TEST(settext_singleline)
  27. {
  28. edit->SetMultiline(false);
  29. edit->SetText("One\nTwo", TB_CARET_POS_END);
  30. TB_VERIFY_STR(edit->GetText(), "One");
  31. }
  32. TB_TEST(settext_multiline)
  33. {
  34. // Both unix and windows line endings should be ok.
  35. edit->SetText("One\nTwo", TB_CARET_POS_END);
  36. TB_VERIFY_STR(edit->GetText(), "One\nTwo");
  37. edit->SetText("One\r\nTwo", TB_CARET_POS_END);
  38. TB_VERIFY_STR(edit->GetText(), "One\r\nTwo");
  39. }
  40. TB_TEST(settext_singleline_malformed_utf8)
  41. {
  42. // Should not detect these sequences as having new line character
  43. edit->SetText("\xC0\x8A", TB_CARET_POS_END);
  44. TB_VERIFY(sedit->blocks.CountLinks() == 1);
  45. edit->SetText("\xE0\x80\x8A", TB_CARET_POS_END);
  46. TB_VERIFY(sedit->blocks.CountLinks() == 1);
  47. edit->SetText("\xF0\x80\x80\x8A", TB_CARET_POS_END);
  48. TB_VERIFY(sedit->blocks.CountLinks() == 1);
  49. edit->SetText("\xF8\x80\x80\x80\x8A", TB_CARET_POS_END);
  50. TB_VERIFY(sedit->blocks.CountLinks() == 1);
  51. edit->SetText("\xFC\x80\x80\x80\x80\x8A", TB_CARET_POS_END);
  52. TB_VERIFY(sedit->blocks.CountLinks() == 1);
  53. // Should detect the new line character
  54. edit->SetText("\xF0\nHello", TB_CARET_POS_END);
  55. TB_VERIFY(sedit->blocks.CountLinks() == 2);
  56. }
  57. TB_TEST(settext_undoredo_ins)
  58. {
  59. // 1 character insertions in sequence should be merged to word boundary.
  60. sedit->InsertText("O"); sedit->InsertText("N"); sedit->InsertText("E"); sedit->InsertText(" ");
  61. sedit->InsertText("T"); sedit->InsertText("W"); sedit->InsertText("O");
  62. TB_VERIFY_STR(edit->GetText(), "ONE TWO");
  63. sedit->Undo();
  64. TB_VERIFY_STR(edit->GetText(), "ONE ");
  65. sedit->Undo();
  66. TB_VERIFY_STR(edit->GetText(), "");
  67. sedit->Redo();
  68. TB_VERIFY_STR(edit->GetText(), "ONE ");
  69. sedit->Redo();
  70. TB_VERIFY_STR(edit->GetText(), "ONE TWO");
  71. // 1 character insertions that are multi byte utf8 should also merge.
  72. // Note: this will also replace "TWO" since redoing keeps the redoed part selected.
  73. sedit->InsertText("L"); sedit->InsertText("Ö"); sedit->InsertText("K");
  74. TB_VERIFY_STR(edit->GetText(), "ONE LÖK");
  75. sedit->Undo();
  76. TB_VERIFY_STR(edit->GetText(), "ONE ");
  77. }
  78. TB_TEST(settext_undoredo_ins_scattered)
  79. {
  80. sedit->InsertText("AAA");
  81. sedit->caret.SetGlobalOfs(2);
  82. sedit->InsertText(".");
  83. sedit->caret.SetGlobalOfs(1);
  84. sedit->InsertText(".");
  85. TB_VERIFY_STR(edit->GetText(), "A.A.A");
  86. sedit->Undo();
  87. TB_VERIFY_STR(edit->GetText(), "AA.A");
  88. sedit->Undo();
  89. TB_VERIFY_STR(edit->GetText(), "AAA");
  90. sedit->Redo();
  91. TB_VERIFY_STR(edit->GetText(), "AA.A");
  92. sedit->Redo();
  93. TB_VERIFY_STR(edit->GetText(), "A.A.A");
  94. }
  95. TB_TEST(settext_undoredo_ins_multiline)
  96. {
  97. sedit->InsertText("ONE\nTWO");
  98. TB_VERIFY_STR(edit->GetText(), "ONE\nTWO");
  99. sedit->Undo();
  100. TB_VERIFY_STR(edit->GetText(), "");
  101. sedit->Redo();
  102. TB_VERIFY_STR(edit->GetText(), "ONE\nTWO");
  103. }
  104. TB_TEST(settext_undoredo_del)
  105. {
  106. sedit->InsertText("ONE TWO");
  107. sedit->selection.Select(3, 7);
  108. sedit->selection.RemoveContent();
  109. TB_VERIFY_STR(edit->GetText(), "ONE");
  110. sedit->Undo();
  111. TB_VERIFY_STR(edit->GetText(), "ONE TWO");
  112. sedit->Redo();
  113. TB_VERIFY_STR(edit->GetText(), "ONE");
  114. }
  115. TB_TEST(settext_undoredo_ins_linebreak_1)
  116. {
  117. sedit->InsertText("ONETWO");
  118. sedit->caret.SetGlobalOfs(3);
  119. sedit->InsertText("\n");
  120. TB_VERIFY_STR(edit->GetText(), "ONE\nTWO");
  121. sedit->Undo();
  122. TB_VERIFY_STR(edit->GetText(), "ONETWO");
  123. sedit->Redo();
  124. TB_VERIFY_STR(edit->GetText(), "ONE\nTWO");
  125. }
  126. TB_TEST(settext_undoredo_ins_linebreak_2)
  127. {
  128. // Inserting a linebreak at the end when we don't end
  129. // the line with a linebreak character must generate
  130. // one extra linebreak.
  131. sedit->InsertBreak();
  132. TB_VERIFY_STR(edit->GetText(), "\r\n\r\n");
  133. sedit->InsertBreak();
  134. TB_VERIFY_STR(edit->GetText(), "\r\n\r\n\r\n");
  135. sedit->Undo();
  136. TB_VERIFY_STR(edit->GetText(), "\r\n\r\n");
  137. sedit->Undo();
  138. TB_VERIFY_STR(edit->GetText(), "");
  139. }
  140. TB_TEST(settext_undoredo_ins_linebreak_3)
  141. {
  142. sedit->SetWindowsStyleBreak(false);
  143. sedit->InsertBreak();
  144. TB_VERIFY_STR(edit->GetText(), "\n\n");
  145. sedit->InsertBreak();
  146. TB_VERIFY_STR(edit->GetText(), "\n\n\n");
  147. sedit->Undo();
  148. TB_VERIFY_STR(edit->GetText(), "\n\n");
  149. sedit->Undo();
  150. TB_VERIFY_STR(edit->GetText(), "");
  151. }
  152. TB_TEST(settext_undoredo_ins_linebreak_4)
  153. {
  154. sedit->InsertText("ONE");
  155. sedit->InsertBreak();
  156. TB_VERIFY_STR(edit->GetText(), "ONE\r\n\r\n");
  157. sedit->Undo();
  158. TB_VERIFY_STR(edit->GetText(), "ONE");
  159. sedit->Redo();
  160. TB_VERIFY_STR(edit->GetText(), "ONE\r\n\r\n");
  161. }
  162. TB_TEST(settext_undoredo_bugfix1)
  163. {
  164. // Make sure we use the test dummy font (ID 0), so we're not dependant on
  165. // the available fonts & font backend in this test.
  166. TBFontDescription fd;
  167. const int font_size = 48;
  168. fd.SetSize(font_size);
  169. edit->SetFontDescription(fd);
  170. sedit->InsertBreak();
  171. sedit->InsertText("A");
  172. TB_VERIFY_STR(edit->GetText(), "\r\nA\r\n");
  173. TB_VERIFY(sedit->GetContentHeight() == font_size * 2);
  174. sedit->KeyDown(0, TB_KEY_BACKSPACE, TB_MODIFIER_NONE);
  175. TB_VERIFY_STR(edit->GetText(), "\r\n\r\n");
  176. TB_VERIFY(sedit->GetContentHeight() == font_size * 2);
  177. sedit->Undo();
  178. TB_VERIFY_STR(edit->GetText(), "\r\nA\r\n");
  179. TB_VERIFY(sedit->GetContentHeight() == font_size * 2);
  180. sedit->Redo();
  181. TB_VERIFY_STR(edit->GetText(), "\r\n\r\n");
  182. TB_VERIFY(sedit->GetContentHeight() == font_size * 2);
  183. }
  184. TB_TEST(settext_insert_linebreaks_move)
  185. {
  186. sedit->InsertText("Foo\n");
  187. sedit->InsertText("Foo\n");
  188. sedit->InsertText("Foo\n");
  189. TB_VERIFY_STR(edit->GetText(), "Foo\nFoo\nFoo\n");
  190. }
  191. TB_TEST(multiline_overflow_1)
  192. {
  193. // Make sure we use the test dummy font (ID 0), so we're not dependant on
  194. // the available fonts & font backend in this test.
  195. TBFontDescription fd;
  196. const int font_size = 48;
  197. fd.SetSize(font_size);
  198. edit->SetFontDescription(fd);
  199. // Test that a long line that overflow but has no allowed break position doesn't wrap.
  200. edit->SetText("this_is_a_long_line_that_should_not_wrap\n"
  201. "this_is_a_long_line_that_should_not_wrap", TB_CARET_POS_END);
  202. TB_VERIFY(sedit->GetContentHeight() == font_size * 2);
  203. }
  204. }
  205. #endif // TB_UNIT_TESTING