RichTextBoxTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 #80626
  106. public void BorderStyle_None ()
  107. {
  108. RichTextBox rtb = new RichTextBox ();
  109. rtb.BorderStyle = BorderStyle.None;
  110. Assert.AreEqual (96, rtb.Height, "#1");
  111. rtb.CreateControl ();
  112. Assert.AreEqual (96, rtb.Height, "#2");
  113. }
  114. [Test] // bug #80620
  115. public void ClientRectangle_Borders ()
  116. {
  117. RichTextBox rtb = new RichTextBox ();
  118. rtb.CreateControl ();
  119. Assert.AreEqual (rtb.ClientRectangle, new RichTextBox ().ClientRectangle);
  120. }
  121. [Test]
  122. public void FindCharTest ()
  123. {
  124. RichTextBox rTextBox = new RichTextBox ();
  125. rTextBox.Text = "something";
  126. Assert.AreEqual (2, rTextBox.Find (new char [] {'m'}), "#Mtd3");
  127. Assert.AreEqual (-1, rTextBox.Find (new char [] {'t'},5), "#Mtd3a");
  128. Assert.AreEqual (4, rTextBox.Find (new char [] {'t'},4,5), "#Mtd3b");
  129. }
  130. [Test]
  131. public void FindStringTest ()
  132. {
  133. RichTextBox rTextBox = new RichTextBox ();
  134. rTextBox.Text = "sample text for richtextbox";
  135. int indexToText1 = rTextBox.Find ("for");
  136. Assert.AreEqual (12, indexToText1, "#Mtd4");
  137. int indexToText2 = rTextBox.Find ("for", 0, 14, RichTextBoxFinds.MatchCase);
  138. Assert.AreEqual (-1, indexToText2, "#Mtd5");
  139. int indexToText3 = rTextBox.Find ("for", 0, 15, RichTextBoxFinds.MatchCase);
  140. Assert.AreEqual (12, indexToText3, "#Mtd6");
  141. int indexToText4 = rTextBox.Find ("richtextbox", 0, RichTextBoxFinds.MatchCase);
  142. Assert.AreEqual (16, indexToText4, "#Mtd7");
  143. int indexToText5 = rTextBox.Find ("text", RichTextBoxFinds.MatchCase);
  144. Assert.AreEqual (7, indexToText5, "#Mtd8");
  145. }
  146. [Test]
  147. public void FindTest() {
  148. RichTextBox t = new RichTextBox();
  149. t.Text = "Testtext and arglblah may not be what we're looking for\n, but blah Blah is";
  150. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}), 9, "Find1");
  151. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 20), 20, "Find2");
  152. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 25, 30), -1, "Find3");
  153. Assert.AreEqual(t.Find("blah"), 17, "Find4");
  154. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.None), 17, "Find5");
  155. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.WholeWord), -1, "Find6");
  156. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.MatchCase), 17, "Find7");
  157. Assert.AreEqual(t.Find("blah", 10, 70, RichTextBoxFinds.Reverse), 62, "Find8");
  158. Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse), 67, "Find9");
  159. Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find10");
  160. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.None), 17, "Find11");
  161. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.WholeWord), 62, "Find12");
  162. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.MatchCase), 17, "Find13");
  163. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse), 67, "Find14");
  164. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find15");
  165. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.Reverse), 67, "Find16");
  166. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.MatchCase), 17, "Find17");
  167. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.WholeWord), 62, "Find18");
  168. // Special cases
  169. Assert.AreEqual(t.Find("blah", 10, 11, RichTextBoxFinds.None), -1, "Find19"); // Range to short to ever match
  170. Assert.AreEqual(t.Find("blah", 17, 18, RichTextBoxFinds.None), -1, "Find20"); // Range to short to ever match, but starts matching
  171. Assert.AreEqual(t.Find("is", RichTextBoxFinds.WholeWord), 72, "Find21"); // Last word in document
  172. Assert.AreEqual(t.Find("for", RichTextBoxFinds.WholeWord), 52, "Find22"); // word followed by \n
  173. Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord), 0, "Find23"); // First word in document
  174. Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord | RichTextBoxFinds.Reverse), 0, "Find24"); // First word in document, searched in reverse
  175. }
  176. [Test] // bug #80301
  177. [Ignore ("Depends on specific DPI")]
  178. public void PreferredHeight ()
  179. {
  180. RichTextBox rtb = new RichTextBox ();
  181. rtb.Font = new Font ("Arial", 14);
  182. Assert.AreEqual (29, rtb.PreferredHeight, "#A1");
  183. rtb.Font = new Font ("Arial", 16);
  184. Assert.AreEqual (32, rtb.PreferredHeight, "#A2");
  185. rtb.Font = new Font ("Arial", 17);
  186. Assert.AreEqual (34, rtb.PreferredHeight, "#A3");
  187. rtb.BorderStyle = BorderStyle.None;
  188. Assert.AreEqual (27, rtb.PreferredHeight, "#B1");
  189. rtb.Font = new Font ("Arial", 14);
  190. Assert.AreEqual (22, rtb.PreferredHeight, "#B2");
  191. rtb.Font = new Font ("Arial", 16);
  192. Assert.AreEqual (25, rtb.PreferredHeight, "#B3");
  193. }
  194. [Test]
  195. public void ReadOnly_BackColor_NotSet ()
  196. {
  197. RichTextBox rtb = new RichTextBox ();
  198. rtb.ReadOnly = true;
  199. Assert.IsTrue (rtb.ReadOnly, "#A1");
  200. #if NET_2_0
  201. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#A2");
  202. #else
  203. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#A2");
  204. #endif
  205. Form form = new Form ();
  206. form.ShowInTaskbar = false;
  207. form.Controls.Add (rtb);
  208. form.Show ();
  209. Assert.IsTrue (rtb.ReadOnly, "#B1");
  210. #if NET_2_0
  211. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#B2");
  212. #else
  213. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#B2");
  214. #endif
  215. rtb.ResetBackColor ();
  216. Assert.IsTrue (rtb.ReadOnly, "#C1");
  217. #if NET_2_0
  218. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#C2");
  219. #else
  220. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#C2");
  221. #endif
  222. rtb.ReadOnly = false;
  223. Assert.IsFalse (rtb.ReadOnly, "#D1");
  224. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#D2");
  225. rtb.ReadOnly = true;
  226. Assert.IsTrue (rtb.ReadOnly, "#E1");
  227. #if NET_2_0
  228. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#E2");
  229. #else
  230. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#E2");
  231. #endif
  232. rtb.BackColor = Color.Red;
  233. Assert.IsTrue (rtb.ReadOnly, "#F1");
  234. Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
  235. rtb.ReadOnly = false;
  236. Assert.IsFalse (rtb.ReadOnly, "#G1");
  237. Assert.AreEqual (Color.Red, rtb.BackColor, "#G2");
  238. rtb.ReadOnly = true;
  239. Assert.IsTrue (rtb.ReadOnly, "#H1");
  240. Assert.AreEqual (Color.Red, rtb.BackColor, "#H2");
  241. rtb.ResetBackColor ();
  242. Assert.IsTrue (rtb.ReadOnly, "#I1");
  243. #if NET_2_0
  244. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#I2");
  245. #else
  246. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#I2");
  247. #endif
  248. form.Close ();
  249. }
  250. [Test]
  251. public void ReadOnly_BackColor_Set ()
  252. {
  253. RichTextBox rtb = new RichTextBox ();
  254. rtb.BackColor = Color.Blue;
  255. rtb.ReadOnly = true;
  256. Assert.IsTrue (rtb.ReadOnly, "#A1");
  257. Assert.AreEqual (Color.Blue, rtb.BackColor, "#A2");
  258. Form form = new Form ();
  259. form.ShowInTaskbar = false;
  260. form.Controls.Add (rtb);
  261. form.Show ();
  262. Assert.IsTrue (rtb.ReadOnly, "#B1");
  263. Assert.AreEqual (Color.Blue, rtb.BackColor, "#B2");
  264. rtb.ReadOnly = false;
  265. Assert.IsFalse (rtb.ReadOnly, "#C1");
  266. Assert.AreEqual (Color.Blue, rtb.BackColor, "#C2");
  267. rtb.ReadOnly = true;
  268. Assert.IsTrue (rtb.ReadOnly, "#D1");
  269. Assert.AreEqual (Color.Blue, rtb.BackColor, "#D2");
  270. rtb.BackColor = Color.Red;
  271. Assert.IsTrue (rtb.ReadOnly, "#E1");
  272. Assert.AreEqual (Color.Red, rtb.BackColor, "#E2");
  273. rtb.ReadOnly = false;
  274. Assert.IsFalse (rtb.ReadOnly, "#F1");
  275. Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
  276. rtb.ReadOnly = true;
  277. rtb.ResetBackColor ();
  278. Assert.IsTrue (rtb.ReadOnly, "#G1");
  279. #if NET_2_0
  280. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#G2");
  281. #else
  282. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#G2");
  283. #endif
  284. form.Dispose ();
  285. rtb = new RichTextBox ();
  286. rtb.ReadOnly = true;
  287. rtb.BackColor = Color.Blue;
  288. Assert.IsTrue (rtb.ReadOnly, "#H1");
  289. Assert.AreEqual (Color.Blue, rtb.BackColor, "#H2");
  290. form = new Form ();
  291. form.ShowInTaskbar = false;
  292. form.Controls.Add (rtb);
  293. form.Show ();
  294. Assert.IsTrue (rtb.ReadOnly, "#I1");
  295. Assert.AreEqual (Color.Blue, rtb.BackColor, "#I2");
  296. rtb.ReadOnly = false;
  297. Assert.IsFalse (rtb.ReadOnly, "#J1");
  298. Assert.AreEqual (Color.Blue, rtb.BackColor, "#J2");
  299. rtb.ResetBackColor ();
  300. Assert.IsFalse (rtb.ReadOnly, "#K1");
  301. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#K2");
  302. form.Close ();
  303. }
  304. [Test]
  305. public void ScrollBarsTest ()
  306. {
  307. RichTextBox rtb = new RichTextBox ();
  308. Assert.AreEqual (RichTextBoxScrollBars.Both, rtb.ScrollBars, "#1");
  309. rtb.ScrollBars = RichTextBoxScrollBars.Vertical;
  310. Assert.AreEqual (RichTextBoxScrollBars.Vertical, rtb.ScrollBars, "#2");
  311. }
  312. [Test]
  313. [ExpectedException (typeof (InvalidEnumArgumentException))]
  314. public void ScrollBars_Invalid ()
  315. {
  316. RichTextBox rtb = new RichTextBox ();
  317. rtb.ScrollBars = (RichTextBoxScrollBars) 666;
  318. }
  319. [Test]
  320. public void SelectionFontTest ()
  321. {
  322. RichTextBox t = new RichTextBox();
  323. t.Text = "123";
  324. t.SelectionStart = 1;
  325. t.SelectionLength = 1;
  326. Font f = new Font(FontFamily.GenericMonospace, 120);
  327. t.SelectionFont = f;
  328. Assert.AreEqual (t.SelectionFont.Size, f.Size, "A1");
  329. }
  330. }
  331. }