RichTextBoxTest.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // RichTextBoxTest.cs: Test cases for RichTextBox.
  3. //
  4. // Author:
  5. // Ritvik Mayank ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc. (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Drawing;
  11. using System.Reflection;
  12. using System.Windows.Forms;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Windows.Forms
  15. {
  16. [TestFixture]
  17. public class RichTextBoxBaseTest
  18. {
  19. #if not
  20. [Test]
  21. public void RichTextBoxPropertyTest ()
  22. {
  23. RichTextBox rTBox = new RichTextBox ();
  24. // A
  25. Assert.AreEqual (false, rTBox.AllowDrop, "#A1");
  26. rTBox.Multiline = true;
  27. rTBox.AcceptsTab = true;
  28. SendKeys.SendWait ("^%");
  29. Assert.AreEqual (false, rTBox.AutoSize, "#A2");
  30. Assert.AreEqual (false, rTBox.AutoWordSelection, "#A3");
  31. // B
  32. rTBox.BackColor = Color.White;
  33. Assert.AreEqual (null, rTBox.BackgroundImage, "#B1");
  34. string gif = "M.gif";
  35. rTBox.BackgroundImage = Image.FromFile (gif);
  36. // comparing image objects fails on MS .Net so using Size property
  37. Assert.AreEqual (Image.FromFile(gif, true).Size, rTBox.BackgroundImage.Size, "#B2");
  38. Assert.AreEqual (0, rTBox.BulletIndent, "#B3");
  39. // C
  40. Assert.AreEqual (false, rTBox.CanRedo, "#C1");
  41. rTBox.Paste ();
  42. Assert.AreEqual (false, rTBox.CanRedo, "#C2");
  43. rTBox.ClearUndo ();
  44. Assert.AreEqual (false, rTBox.CanRedo, "#C3");
  45. // D
  46. Assert.AreEqual (true, rTBox.DetectUrls, "#D1");
  47. // F
  48. Assert.AreEqual (FontStyle.Regular, rTBox.Font.Style, "#F1");
  49. Assert.AreEqual ("WindowText", rTBox.ForeColor.Name, "#F2");
  50. //M
  51. Assert.AreEqual (2147483647, rTBox.MaxLength, "#M1");
  52. Assert.AreEqual (true, rTBox.Multiline, "#M2");
  53. rTBox.WordWrap = false;
  54. Assert.AreEqual (true, rTBox.Multiline, "#M3");
  55. // R
  56. Assert.AreEqual ("", rTBox.RedoActionName, "#R1");
  57. Assert.AreEqual (0, rTBox.RightMargin, "#R2");
  58. // [MonoTODO ("Assert.AreEqual (false, rTBox.Rtf, "#R3");") ]
  59. // S
  60. // [MonoTODO ("Assert.AreEqual (ScrollBars.Both, rTBox.ScrollBars, "#S1");")]
  61. Assert.AreEqual (RichTextBoxScrollBars.Both, rTBox.ScrollBars, "#S1");
  62. Assert.AreEqual ("", rTBox.SelectedText, "#S3");
  63. rTBox.Text = "sample TextBox";
  64. Assert.AreEqual (HorizontalAlignment.Left, rTBox.SelectionAlignment, "#S5");
  65. Assert.AreEqual (false, rTBox.SelectionBullet, "#S6");
  66. Assert.AreEqual (0, rTBox.SelectionCharOffset, "#S7");
  67. //Assert.AreEqual (Color.Black, rTBox.SelectionColor, "#S8"); // Random color
  68. Assert.AreEqual ("Courier New", rTBox.SelectionFont.Name, "#S9a");
  69. Assert.AreEqual (FontStyle.Regular, rTBox.SelectionFont.Style, "#S9b");
  70. Assert.AreEqual (0, rTBox.SelectionHangingIndent, "#S10");
  71. Assert.AreEqual (0, rTBox.SelectionIndent, "#S11");
  72. Assert.AreEqual (0, rTBox.SelectionLength, "#S12");
  73. Assert.AreEqual (false, rTBox.SelectionProtected, "#S13");
  74. Assert.AreEqual (0, rTBox.SelectionRightIndent, "#S14");
  75. Assert.AreEqual (false, rTBox.ShowSelectionMargin, "#S15");
  76. // [MonoTODO ("Assert.AreEqual (, rTBox.SelectionTabs, "#S16");")]
  77. // [MonoTODO("Assert.AreEqual (TypeCode.Empty, rTBox.SelectionType, "#S17");")]
  78. // T
  79. Assert.AreEqual ("sample TextBox", rTBox.Text, "#T1");
  80. Assert.AreEqual (14, rTBox.TextLength, "#T2");
  81. // UVW
  82. Assert.AreEqual ("", rTBox.UndoActionName, "#U1");
  83. // XYZ
  84. Assert.AreEqual (1, rTBox.ZoomFactor, "#Z1");
  85. }
  86. [Test]
  87. public void CanPasteTest ()
  88. {
  89. RichTextBox rTextBox = new RichTextBox ();
  90. Bitmap myBitmap = new Bitmap ("M.gif");
  91. Clipboard.SetDataObject (myBitmap);
  92. DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap);
  93. Assert.AreEqual (true, rTextBox.CanPaste (myFormat), "#Mtd1");
  94. }
  95. #endif
  96. [Test]
  97. public void FindCharTest ()
  98. {
  99. RichTextBox rTextBox = new RichTextBox ();
  100. rTextBox.Text = "something";
  101. Assert.AreEqual (2, rTextBox.Find (new char [] {'m'}), "#Mtd3");
  102. Assert.AreEqual (-1, rTextBox.Find (new char [] {'t'},5), "#Mtd3a");
  103. Assert.AreEqual (4, rTextBox.Find (new char [] {'t'},4,5), "#Mtd3b");
  104. }
  105. [Test]
  106. public void FindStringTest ()
  107. {
  108. RichTextBox rTextBox = new RichTextBox ();
  109. rTextBox.Text = "sample text for richtextbox";
  110. int indexToText1 = rTextBox.Find ("for");
  111. Assert.AreEqual (12, indexToText1, "#Mtd4");
  112. int indexToText2 = rTextBox.Find ("for", 0, 14, RichTextBoxFinds.MatchCase);
  113. Assert.AreEqual (-1, indexToText2, "#Mtd5");
  114. int indexToText3 = rTextBox.Find ("for", 0, 15, RichTextBoxFinds.MatchCase);
  115. Assert.AreEqual (12, indexToText3, "#Mtd6");
  116. int indexToText4 = rTextBox.Find ("richtextbox", 0, RichTextBoxFinds.MatchCase);
  117. Assert.AreEqual (16, indexToText4, "#Mtd7");
  118. int indexToText5 = rTextBox.Find ("text", RichTextBoxFinds.MatchCase);
  119. Assert.AreEqual (7, indexToText5, "#Mtd8");
  120. }
  121. [Test]
  122. [Category ("NotWorking")] // Find21 is failing with -1 instead of 72
  123. public void FindTest() {
  124. RichTextBox t = new RichTextBox();
  125. t.Text = "Testtext and arglblah may not be what we're looking for\n, but blah Blah is";
  126. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}), 9, "Find1");
  127. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 20), 20, "Find2");
  128. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 25, 30), -1, "Find3");
  129. Assert.AreEqual(t.Find("blah"), 17, "Find4");
  130. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.None), 17, "Find5");
  131. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.WholeWord), -1, "Find6");
  132. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.MatchCase), 17, "Find7");
  133. Assert.AreEqual(t.Find("blah", 10, 70, RichTextBoxFinds.Reverse), 62, "Find8");
  134. Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse), 67, "Find9");
  135. Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find10");
  136. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.None), 17, "Find11");
  137. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.WholeWord), 62, "Find12");
  138. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.MatchCase), 17, "Find13");
  139. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse), 67, "Find14");
  140. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find15");
  141. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.Reverse), 67, "Find16");
  142. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.MatchCase), 17, "Find17");
  143. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.WholeWord), 62, "Find18");
  144. // Special cases
  145. Assert.AreEqual(t.Find("blah", 10, 11, RichTextBoxFinds.None), -1, "Find19"); // Range to short to ever match
  146. Assert.AreEqual(t.Find("blah", 17, 18, RichTextBoxFinds.None), -1, "Find20"); // Range to short to ever match, but starts matching
  147. Assert.AreEqual(t.Find("is", RichTextBoxFinds.WholeWord), 72, "Find21"); // Last word in document
  148. Assert.AreEqual(t.Find("for", RichTextBoxFinds.WholeWord), 52, "Find22"); // word followed by \n
  149. Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord), 0, "Find23"); // First word in document
  150. Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord | RichTextBoxFinds.Reverse), 0, "Find24"); // First word in document, searched in reverse
  151. }
  152. [Test] // bug #80301
  153. [Ignore ("Depends on specific DPI")]
  154. public void PreferredHeight ()
  155. {
  156. RichTextBox rtb = new RichTextBox ();
  157. rtb.Font = new Font ("Arial", 14);
  158. Assert.AreEqual (29, rtb.PreferredHeight, "#A1");
  159. rtb.Font = new Font ("Arial", 16);
  160. Assert.AreEqual (32, rtb.PreferredHeight, "#A2");
  161. rtb.Font = new Font ("Arial", 17);
  162. Assert.AreEqual (34, rtb.PreferredHeight, "#A3");
  163. rtb.BorderStyle = BorderStyle.None;
  164. Assert.AreEqual (27, rtb.PreferredHeight, "#B1");
  165. rtb.Font = new Font ("Arial", 14);
  166. Assert.AreEqual (22, rtb.PreferredHeight, "#B2");
  167. rtb.Font = new Font ("Arial", 16);
  168. Assert.AreEqual (25, rtb.PreferredHeight, "#B3");
  169. }
  170. }
  171. }