RichTextBoxTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.ComponentModel;
  11. using System.Drawing;
  12. using System.Reflection;
  13. using System.Windows.Forms;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Windows.Forms
  16. {
  17. [TestFixture]
  18. public class RichTextBoxTest
  19. {
  20. #if not
  21. [Test]
  22. public void RichTextBoxPropertyTest ()
  23. {
  24. RichTextBox rTBox = new RichTextBox ();
  25. // A
  26. Assert.AreEqual (false, rTBox.AllowDrop, "#A1");
  27. rTBox.Multiline = true;
  28. rTBox.AcceptsTab = true;
  29. SendKeys.SendWait ("^%");
  30. Assert.AreEqual (false, rTBox.AutoSize, "#A2");
  31. Assert.AreEqual (false, rTBox.AutoWordSelection, "#A3");
  32. // B
  33. rTBox.BackColor = Color.White;
  34. Assert.AreEqual (null, rTBox.BackgroundImage, "#B1");
  35. string gif = "M.gif";
  36. rTBox.BackgroundImage = Image.FromFile (gif);
  37. // comparing image objects fails on MS .Net so using Size property
  38. Assert.AreEqual (Image.FromFile(gif, true).Size, rTBox.BackgroundImage.Size, "#B2");
  39. Assert.AreEqual (0, rTBox.BulletIndent, "#B3");
  40. // C
  41. Assert.AreEqual (false, rTBox.CanRedo, "#C1");
  42. rTBox.Paste ();
  43. Assert.AreEqual (false, rTBox.CanRedo, "#C2");
  44. rTBox.ClearUndo ();
  45. Assert.AreEqual (false, rTBox.CanRedo, "#C3");
  46. // D
  47. Assert.AreEqual (true, rTBox.DetectUrls, "#D1");
  48. // F
  49. Assert.AreEqual (FontStyle.Regular, rTBox.Font.Style, "#F1");
  50. Assert.AreEqual ("WindowText", rTBox.ForeColor.Name, "#F2");
  51. //M
  52. Assert.AreEqual (2147483647, rTBox.MaxLength, "#M1");
  53. Assert.AreEqual (true, rTBox.Multiline, "#M2");
  54. rTBox.WordWrap = false;
  55. Assert.AreEqual (true, rTBox.Multiline, "#M3");
  56. // R
  57. Assert.AreEqual ("", rTBox.RedoActionName, "#R1");
  58. Assert.AreEqual (0, rTBox.RightMargin, "#R2");
  59. // [MonoTODO ("Assert.AreEqual (false, rTBox.Rtf, "#R3");") ]
  60. // S
  61. Assert.AreEqual ("", rTBox.SelectedText, "#S3");
  62. rTBox.Text = "sample TextBox";
  63. Assert.AreEqual (HorizontalAlignment.Left, rTBox.SelectionAlignment, "#S5");
  64. Assert.AreEqual (false, rTBox.SelectionBullet, "#S6");
  65. Assert.AreEqual (0, rTBox.SelectionCharOffset, "#S7");
  66. //Assert.AreEqual (Color.Black, rTBox.SelectionColor, "#S8"); // Random color
  67. Assert.AreEqual ("Courier New", rTBox.SelectionFont.Name, "#S9a");
  68. Assert.AreEqual (FontStyle.Regular, rTBox.SelectionFont.Style, "#S9b");
  69. Assert.AreEqual (0, rTBox.SelectionHangingIndent, "#S10");
  70. Assert.AreEqual (0, rTBox.SelectionIndent, "#S11");
  71. Assert.AreEqual (0, rTBox.SelectionLength, "#S12");
  72. Assert.AreEqual (false, rTBox.SelectionProtected, "#S13");
  73. Assert.AreEqual (0, rTBox.SelectionRightIndent, "#S14");
  74. Assert.AreEqual (false, rTBox.ShowSelectionMargin, "#S15");
  75. // [MonoTODO ("Assert.AreEqual (, rTBox.SelectionTabs, "#S16");")]
  76. // [MonoTODO("Assert.AreEqual (TypeCode.Empty, rTBox.SelectionType, "#S17");")]
  77. // T
  78. Assert.AreEqual ("sample TextBox", rTBox.Text, "#T1");
  79. Assert.AreEqual (14, rTBox.TextLength, "#T2");
  80. // UVW
  81. Assert.AreEqual ("", rTBox.UndoActionName, "#U1");
  82. // XYZ
  83. Assert.AreEqual (1, rTBox.ZoomFactor, "#Z1");
  84. }
  85. [Test]
  86. public void CanPasteTest ()
  87. {
  88. RichTextBox rTextBox = new RichTextBox ();
  89. Bitmap myBitmap = new Bitmap ("M.gif");
  90. Clipboard.SetDataObject (myBitmap);
  91. DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap);
  92. Assert.AreEqual (true, rTextBox.CanPaste (myFormat), "#Mtd1");
  93. }
  94. #endif
  95. [Test]
  96. public void BackColor ()
  97. {
  98. RichTextBox rtb = new RichTextBox ();
  99. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#1");
  100. rtb.BackColor = Color.Red;
  101. Assert.AreEqual (Color.Red, rtb.BackColor, "#2");
  102. rtb.BackColor = Color.Empty;
  103. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#3");
  104. }
  105. [Test] // bug #80620
  106. public void ClientRectangle_Borders ()
  107. {
  108. RichTextBox rtb = new RichTextBox ();
  109. rtb.CreateControl ();
  110. Assert.AreEqual (rtb.ClientRectangle, new RichTextBox ().ClientRectangle);
  111. }
  112. [Test]
  113. public void FindCharTest ()
  114. {
  115. RichTextBox rTextBox = new RichTextBox ();
  116. rTextBox.Text = "something";
  117. Assert.AreEqual (2, rTextBox.Find (new char [] {'m'}), "#Mtd3");
  118. Assert.AreEqual (-1, rTextBox.Find (new char [] {'t'},5), "#Mtd3a");
  119. Assert.AreEqual (4, rTextBox.Find (new char [] {'t'},4,5), "#Mtd3b");
  120. }
  121. [Test]
  122. public void FindStringTest ()
  123. {
  124. RichTextBox rTextBox = new RichTextBox ();
  125. rTextBox.Text = "sample text for richtextbox";
  126. int indexToText1 = rTextBox.Find ("for");
  127. Assert.AreEqual (12, indexToText1, "#Mtd4");
  128. int indexToText2 = rTextBox.Find ("for", 0, 14, RichTextBoxFinds.MatchCase);
  129. Assert.AreEqual (-1, indexToText2, "#Mtd5");
  130. int indexToText3 = rTextBox.Find ("for", 0, 15, RichTextBoxFinds.MatchCase);
  131. Assert.AreEqual (12, indexToText3, "#Mtd6");
  132. int indexToText4 = rTextBox.Find ("richtextbox", 0, RichTextBoxFinds.MatchCase);
  133. Assert.AreEqual (16, indexToText4, "#Mtd7");
  134. int indexToText5 = rTextBox.Find ("text", RichTextBoxFinds.MatchCase);
  135. Assert.AreEqual (7, indexToText5, "#Mtd8");
  136. }
  137. [Test]
  138. public void FindTest() {
  139. RichTextBox t = new RichTextBox();
  140. t.Text = "Testtext and arglblah may not be what we're looking for\n, but blah Blah is";
  141. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}), 9, "Find1");
  142. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 20), 20, "Find2");
  143. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 25, 30), -1, "Find3");
  144. Assert.AreEqual(t.Find("blah"), 17, "Find4");
  145. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.None), 17, "Find5");
  146. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.WholeWord), -1, "Find6");
  147. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.MatchCase), 17, "Find7");
  148. Assert.AreEqual(t.Find("blah", 10, 70, RichTextBoxFinds.Reverse), 62, "Find8");
  149. Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse), 67, "Find9");
  150. Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find10");
  151. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.None), 17, "Find11");
  152. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.WholeWord), 62, "Find12");
  153. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.MatchCase), 17, "Find13");
  154. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse), 67, "Find14");
  155. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find15");
  156. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.Reverse), 67, "Find16");
  157. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.MatchCase), 17, "Find17");
  158. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.WholeWord), 62, "Find18");
  159. // Special cases
  160. Assert.AreEqual(t.Find("blah", 10, 11, RichTextBoxFinds.None), -1, "Find19"); // Range to short to ever match
  161. Assert.AreEqual(t.Find("blah", 17, 18, RichTextBoxFinds.None), -1, "Find20"); // Range to short to ever match, but starts matching
  162. Assert.AreEqual(t.Find("is", RichTextBoxFinds.WholeWord), 72, "Find21"); // Last word in document
  163. Assert.AreEqual(t.Find("for", RichTextBoxFinds.WholeWord), 52, "Find22"); // word followed by \n
  164. Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord), 0, "Find23"); // First word in document
  165. Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord | RichTextBoxFinds.Reverse), 0, "Find24"); // First word in document, searched in reverse
  166. }
  167. [Test] // bug #80301
  168. [Ignore ("Depends on specific DPI")]
  169. public void PreferredHeight ()
  170. {
  171. RichTextBox rtb = new RichTextBox ();
  172. rtb.Font = new Font ("Arial", 14);
  173. Assert.AreEqual (29, rtb.PreferredHeight, "#A1");
  174. rtb.Font = new Font ("Arial", 16);
  175. Assert.AreEqual (32, rtb.PreferredHeight, "#A2");
  176. rtb.Font = new Font ("Arial", 17);
  177. Assert.AreEqual (34, rtb.PreferredHeight, "#A3");
  178. rtb.BorderStyle = BorderStyle.None;
  179. Assert.AreEqual (27, rtb.PreferredHeight, "#B1");
  180. rtb.Font = new Font ("Arial", 14);
  181. Assert.AreEqual (22, rtb.PreferredHeight, "#B2");
  182. rtb.Font = new Font ("Arial", 16);
  183. Assert.AreEqual (25, rtb.PreferredHeight, "#B3");
  184. }
  185. [Test]
  186. public void ReadOnly_BackColor_NotSet ()
  187. {
  188. RichTextBox rtb = new RichTextBox ();
  189. rtb.ReadOnly = true;
  190. Assert.IsTrue (rtb.ReadOnly, "#A1");
  191. #if NET_2_0
  192. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#A2");
  193. #else
  194. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#A2");
  195. #endif
  196. Form form = new Form ();
  197. form.ShowInTaskbar = false;
  198. form.Controls.Add (rtb);
  199. form.Show ();
  200. Assert.IsTrue (rtb.ReadOnly, "#B1");
  201. #if NET_2_0
  202. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#B2");
  203. #else
  204. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#B2");
  205. #endif
  206. rtb.ResetBackColor ();
  207. Assert.IsTrue (rtb.ReadOnly, "#C1");
  208. #if NET_2_0
  209. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#C2");
  210. #else
  211. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#C2");
  212. #endif
  213. rtb.ReadOnly = false;
  214. Assert.IsFalse (rtb.ReadOnly, "#D1");
  215. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#D2");
  216. rtb.ReadOnly = true;
  217. Assert.IsTrue (rtb.ReadOnly, "#E1");
  218. #if NET_2_0
  219. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#E2");
  220. #else
  221. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#E2");
  222. #endif
  223. rtb.BackColor = Color.Red;
  224. Assert.IsTrue (rtb.ReadOnly, "#F1");
  225. Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
  226. rtb.ReadOnly = false;
  227. Assert.IsFalse (rtb.ReadOnly, "#G1");
  228. Assert.AreEqual (Color.Red, rtb.BackColor, "#G2");
  229. rtb.ReadOnly = true;
  230. Assert.IsTrue (rtb.ReadOnly, "#H1");
  231. Assert.AreEqual (Color.Red, rtb.BackColor, "#H2");
  232. rtb.ResetBackColor ();
  233. Assert.IsTrue (rtb.ReadOnly, "#I1");
  234. #if NET_2_0
  235. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#I2");
  236. #else
  237. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#I2");
  238. #endif
  239. }
  240. [Test]
  241. public void ReadOnly_BackColor_Set ()
  242. {
  243. RichTextBox rtb = new RichTextBox ();
  244. rtb.BackColor = Color.Blue;
  245. rtb.ReadOnly = true;
  246. Assert.IsTrue (rtb.ReadOnly, "#A1");
  247. Assert.AreEqual (Color.Blue, rtb.BackColor, "#A2");
  248. Form form = new Form ();
  249. form.ShowInTaskbar = false;
  250. form.Controls.Add (rtb);
  251. form.Show ();
  252. Assert.IsTrue (rtb.ReadOnly, "#B1");
  253. Assert.AreEqual (Color.Blue, rtb.BackColor, "#B2");
  254. rtb.ReadOnly = false;
  255. Assert.IsFalse (rtb.ReadOnly, "#C1");
  256. Assert.AreEqual (Color.Blue, rtb.BackColor, "#C2");
  257. rtb.ReadOnly = true;
  258. Assert.IsTrue (rtb.ReadOnly, "#D1");
  259. Assert.AreEqual (Color.Blue, rtb.BackColor, "#D2");
  260. rtb.BackColor = Color.Red;
  261. Assert.IsTrue (rtb.ReadOnly, "#E1");
  262. Assert.AreEqual (Color.Red, rtb.BackColor, "#E2");
  263. rtb.ReadOnly = false;
  264. Assert.IsFalse (rtb.ReadOnly, "#F1");
  265. Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
  266. rtb.ReadOnly = true;
  267. rtb.ResetBackColor ();
  268. Assert.IsTrue (rtb.ReadOnly, "#G1");
  269. #if NET_2_0
  270. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#G2");
  271. #else
  272. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#G2");
  273. #endif
  274. form.Dispose ();
  275. rtb = new RichTextBox ();
  276. rtb.ReadOnly = true;
  277. rtb.BackColor = Color.Blue;
  278. Assert.IsTrue (rtb.ReadOnly, "#H1");
  279. Assert.AreEqual (Color.Blue, rtb.BackColor, "#H2");
  280. form = new Form ();
  281. form.ShowInTaskbar = false;
  282. form.Controls.Add (rtb);
  283. form.Show ();
  284. Assert.IsTrue (rtb.ReadOnly, "#I1");
  285. Assert.AreEqual (Color.Blue, rtb.BackColor, "#I2");
  286. rtb.ReadOnly = false;
  287. Assert.IsFalse (rtb.ReadOnly, "#J1");
  288. Assert.AreEqual (Color.Blue, rtb.BackColor, "#J2");
  289. rtb.ResetBackColor ();
  290. Assert.IsFalse (rtb.ReadOnly, "#K1");
  291. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#K2");
  292. }
  293. [Test]
  294. public void ScrollBarsTest ()
  295. {
  296. RichTextBox rtb = new RichTextBox ();
  297. Assert.AreEqual (RichTextBoxScrollBars.Both, rtb.ScrollBars, "#1");
  298. rtb.ScrollBars = RichTextBoxScrollBars.Vertical;
  299. Assert.AreEqual (RichTextBoxScrollBars.Vertical, rtb.ScrollBars, "#2");
  300. }
  301. [Test]
  302. [ExpectedException (typeof (InvalidEnumArgumentException))]
  303. public void ScrollBars_Invalid ()
  304. {
  305. RichTextBox rtb = new RichTextBox ();
  306. rtb.ScrollBars = (RichTextBoxScrollBars) 666;
  307. }
  308. [Test]
  309. public void SelectionFontTest ()
  310. {
  311. RichTextBox t = new RichTextBox();
  312. t.Text = "123";
  313. t.SelectionStart = 1;
  314. t.SelectionLength = 1;
  315. Font f = new Font(FontFamily.GenericMonospace, 120);
  316. t.SelectionFont = f;
  317. Assert.AreEqual (t.SelectionFont.Size, f.Size, "A1");
  318. }
  319. }
  320. }