RichTextBoxTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 : TestHelper
  19. {
  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. #if ONLY_1_1
  61. Assert.AreEqual (null, rTBox.SelectedText, "#S3");
  62. #else
  63. Assert.AreEqual ("", rTBox.SelectedText, "#S3");
  64. #endif
  65. rTBox.Text = "sample TextBox";
  66. Assert.AreEqual (HorizontalAlignment.Left, rTBox.SelectionAlignment, "#S5");
  67. Assert.AreEqual (false, rTBox.SelectionBullet, "#S6");
  68. Assert.AreEqual (0, rTBox.SelectionCharOffset, "#S7");
  69. //Assert.AreEqual (Color.Black, rTBox.SelectionColor, "#S8"); // Random color
  70. //Assert.AreEqual ("Courier New", rTBox.SelectionFont.Name, "#S9a");
  71. Assert.AreEqual (FontStyle.Regular, rTBox.SelectionFont.Style, "#S9b");
  72. Assert.AreEqual (0, rTBox.SelectionHangingIndent, "#S10");
  73. Assert.AreEqual (0, rTBox.SelectionIndent, "#S11");
  74. //Assert.AreEqual (0, rTBox.SelectionLength, "#S12");
  75. Assert.AreEqual (false, rTBox.SelectionProtected, "#S13");
  76. Assert.AreEqual (0, rTBox.SelectionRightIndent, "#S14");
  77. Assert.AreEqual (false, rTBox.ShowSelectionMargin, "#S15");
  78. // [MonoTODO ("Assert.AreEqual (, rTBox.SelectionTabs, "#S16");")]
  79. // [MonoTODO("Assert.AreEqual (TypeCode.Empty, rTBox.SelectionType, "#S17");")]
  80. // T
  81. Assert.AreEqual ("sample TextBox", rTBox.Text, "#T1");
  82. Assert.AreEqual (14, rTBox.TextLength, "#T2");
  83. // UVW
  84. Assert.AreEqual ("", rTBox.UndoActionName, "#U1");
  85. // XYZ
  86. Assert.AreEqual (1, rTBox.ZoomFactor, "#Z1");
  87. }
  88. #if not
  89. [Test]
  90. public void CanPasteTest ()
  91. {
  92. RichTextBox rTextBox = new RichTextBox ();
  93. Bitmap myBitmap = new Bitmap ("M.gif");
  94. Clipboard.SetDataObject (myBitmap);
  95. DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap);
  96. Assert.AreEqual (true, rTextBox.CanPaste (myFormat), "#Mtd1");
  97. }
  98. #endif
  99. [Test]
  100. public void BackColor ()
  101. {
  102. RichTextBox rtb = new RichTextBox ();
  103. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#1");
  104. rtb.BackColor = Color.Red;
  105. Assert.AreEqual (Color.Red, rtb.BackColor, "#2");
  106. rtb.BackColor = Color.Empty;
  107. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#3");
  108. }
  109. [Test] // bug #80626
  110. public void BorderStyle_None ()
  111. {
  112. RichTextBox rtb = new RichTextBox ();
  113. rtb.BorderStyle = BorderStyle.None;
  114. Assert.AreEqual (96, rtb.Height, "#1");
  115. rtb.CreateControl ();
  116. Assert.AreEqual (96, rtb.Height, "#2");
  117. }
  118. [Test] // bug #80620
  119. public void ClientRectangle_Borders ()
  120. {
  121. RichTextBox rtb = new RichTextBox ();
  122. rtb.CreateControl ();
  123. Assert.AreEqual (rtb.ClientRectangle, new RichTextBox ().ClientRectangle);
  124. }
  125. [Test]
  126. public void FindCharTest ()
  127. {
  128. RichTextBox rTextBox = new RichTextBox ();
  129. rTextBox.Text = "something";
  130. Assert.AreEqual (2, rTextBox.Find (new char [] {'m'}), "#Mtd3");
  131. Assert.AreEqual (-1, rTextBox.Find (new char [] {'t'},5), "#Mtd3a");
  132. Assert.AreEqual (4, rTextBox.Find (new char [] {'t'},4,5), "#Mtd3b");
  133. }
  134. [Test]
  135. public void FindStringTest ()
  136. {
  137. RichTextBox rTextBox = new RichTextBox ();
  138. rTextBox.Text = "sample text for richtextbox";
  139. int indexToText1 = rTextBox.Find ("for");
  140. Assert.AreEqual (12, indexToText1, "#Mtd4");
  141. int indexToText2 = rTextBox.Find ("for", 0, 14, RichTextBoxFinds.MatchCase);
  142. Assert.AreEqual (-1, indexToText2, "#Mtd5");
  143. int indexToText3 = rTextBox.Find ("for", 0, 15, RichTextBoxFinds.MatchCase);
  144. Assert.AreEqual (12, indexToText3, "#Mtd6");
  145. int indexToText4 = rTextBox.Find ("richtextbox", 0, RichTextBoxFinds.MatchCase);
  146. Assert.AreEqual (16, indexToText4, "#Mtd7");
  147. int indexToText5 = rTextBox.Find ("text", RichTextBoxFinds.MatchCase);
  148. Assert.AreEqual (7, indexToText5, "#Mtd8");
  149. }
  150. [Test]
  151. public void FindTest() {
  152. RichTextBox t = new RichTextBox();
  153. t.Text = "Testtext and arglblah may not be what we're looking for\n, but blah Blah is";
  154. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}), 9, "Find1");
  155. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 20), 20, "Find2");
  156. Assert.AreEqual(t.Find(new char[] {'b', 'l', 'a', 'h'}, 25, 30), -1, "Find3");
  157. Assert.AreEqual(t.Find("blah"), 17, "Find4");
  158. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.None), 17, "Find5");
  159. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.WholeWord), -1, "Find6");
  160. Assert.AreEqual(t.Find("blah", 10, 30, RichTextBoxFinds.MatchCase), 17, "Find7");
  161. Assert.AreEqual(t.Find("blah", 10, 70, RichTextBoxFinds.Reverse), 62, "Find8");
  162. Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse), 67, "Find9");
  163. Assert.AreEqual(t.Find("blah", 10, 73, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find10");
  164. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.None), 17, "Find11");
  165. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.WholeWord), 62, "Find12");
  166. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.MatchCase), 17, "Find13");
  167. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse), 67, "Find14");
  168. Assert.AreEqual(t.Find("blah", 10, RichTextBoxFinds.Reverse | RichTextBoxFinds.MatchCase), 62, "Find15");
  169. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.Reverse), 67, "Find16");
  170. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.MatchCase), 17, "Find17");
  171. Assert.AreEqual(t.Find("blah", RichTextBoxFinds.WholeWord), 62, "Find18");
  172. // Special cases
  173. Assert.AreEqual(t.Find("blah", 10, 11, RichTextBoxFinds.None), -1, "Find19"); // Range to short to ever match
  174. Assert.AreEqual(t.Find("blah", 17, 18, RichTextBoxFinds.None), -1, "Find20"); // Range to short to ever match, but starts matching
  175. Assert.AreEqual(t.Find("is", RichTextBoxFinds.WholeWord), 72, "Find21"); // Last word in document
  176. Assert.AreEqual(t.Find("for", RichTextBoxFinds.WholeWord), 52, "Find22"); // word followed by \n
  177. Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord), 0, "Find23"); // First word in document
  178. Assert.AreEqual(t.Find("Testtext", RichTextBoxFinds.WholeWord | RichTextBoxFinds.Reverse), 0, "Find24"); // First word in document, searched in reverse
  179. }
  180. [Test] // bug #80301
  181. [Ignore ("Depends on specific DPI")]
  182. public void PreferredHeight ()
  183. {
  184. RichTextBox rtb = new RichTextBox ();
  185. rtb.Font = new Font ("Arial", 14);
  186. Assert.AreEqual (29, rtb.PreferredHeight, "#A1");
  187. rtb.Font = new Font ("Arial", 16);
  188. Assert.AreEqual (32, rtb.PreferredHeight, "#A2");
  189. rtb.Font = new Font ("Arial", 17);
  190. Assert.AreEqual (34, rtb.PreferredHeight, "#A3");
  191. rtb.BorderStyle = BorderStyle.None;
  192. Assert.AreEqual (27, rtb.PreferredHeight, "#B1");
  193. rtb.Font = new Font ("Arial", 14);
  194. Assert.AreEqual (22, rtb.PreferredHeight, "#B2");
  195. rtb.Font = new Font ("Arial", 16);
  196. Assert.AreEqual (25, rtb.PreferredHeight, "#B3");
  197. }
  198. [Test]
  199. public void ReadOnly_BackColor_NotSet ()
  200. {
  201. RichTextBox rtb = new RichTextBox ();
  202. rtb.ReadOnly = true;
  203. Assert.IsTrue (rtb.ReadOnly, "#A1");
  204. #if NET_2_0
  205. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#A2");
  206. #else
  207. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#A2");
  208. #endif
  209. Form form = new Form ();
  210. form.ShowInTaskbar = false;
  211. form.Controls.Add (rtb);
  212. form.Show ();
  213. Assert.IsTrue (rtb.ReadOnly, "#B1");
  214. #if NET_2_0
  215. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#B2");
  216. #else
  217. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#B2");
  218. #endif
  219. rtb.ResetBackColor ();
  220. Assert.IsTrue (rtb.ReadOnly, "#C1");
  221. #if NET_2_0
  222. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#C2");
  223. #else
  224. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#C2");
  225. #endif
  226. rtb.ReadOnly = false;
  227. Assert.IsFalse (rtb.ReadOnly, "#D1");
  228. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#D2");
  229. rtb.ReadOnly = true;
  230. Assert.IsTrue (rtb.ReadOnly, "#E1");
  231. #if NET_2_0
  232. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#E2");
  233. #else
  234. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#E2");
  235. #endif
  236. rtb.BackColor = Color.Red;
  237. Assert.IsTrue (rtb.ReadOnly, "#F1");
  238. Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
  239. rtb.ReadOnly = false;
  240. Assert.IsFalse (rtb.ReadOnly, "#G1");
  241. Assert.AreEqual (Color.Red, rtb.BackColor, "#G2");
  242. rtb.ReadOnly = true;
  243. Assert.IsTrue (rtb.ReadOnly, "#H1");
  244. Assert.AreEqual (Color.Red, rtb.BackColor, "#H2");
  245. rtb.ResetBackColor ();
  246. Assert.IsTrue (rtb.ReadOnly, "#I1");
  247. #if NET_2_0
  248. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#I2");
  249. #else
  250. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#I2");
  251. #endif
  252. form.Close ();
  253. }
  254. [Test]
  255. public void ReadOnly_BackColor_Set ()
  256. {
  257. RichTextBox rtb = new RichTextBox ();
  258. rtb.BackColor = Color.Blue;
  259. rtb.ReadOnly = true;
  260. Assert.IsTrue (rtb.ReadOnly, "#A1");
  261. Assert.AreEqual (Color.Blue, rtb.BackColor, "#A2");
  262. Form form = new Form ();
  263. form.ShowInTaskbar = false;
  264. form.Controls.Add (rtb);
  265. form.Show ();
  266. Assert.IsTrue (rtb.ReadOnly, "#B1");
  267. Assert.AreEqual (Color.Blue, rtb.BackColor, "#B2");
  268. rtb.ReadOnly = false;
  269. Assert.IsFalse (rtb.ReadOnly, "#C1");
  270. Assert.AreEqual (Color.Blue, rtb.BackColor, "#C2");
  271. rtb.ReadOnly = true;
  272. Assert.IsTrue (rtb.ReadOnly, "#D1");
  273. Assert.AreEqual (Color.Blue, rtb.BackColor, "#D2");
  274. rtb.BackColor = Color.Red;
  275. Assert.IsTrue (rtb.ReadOnly, "#E1");
  276. Assert.AreEqual (Color.Red, rtb.BackColor, "#E2");
  277. rtb.ReadOnly = false;
  278. Assert.IsFalse (rtb.ReadOnly, "#F1");
  279. Assert.AreEqual (Color.Red, rtb.BackColor, "#F2");
  280. rtb.ReadOnly = true;
  281. rtb.ResetBackColor ();
  282. Assert.IsTrue (rtb.ReadOnly, "#G1");
  283. #if NET_2_0
  284. Assert.AreEqual (SystemColors.Control, rtb.BackColor, "#G2");
  285. #else
  286. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#G2");
  287. #endif
  288. form.Dispose ();
  289. rtb = new RichTextBox ();
  290. rtb.ReadOnly = true;
  291. rtb.BackColor = Color.Blue;
  292. Assert.IsTrue (rtb.ReadOnly, "#H1");
  293. Assert.AreEqual (Color.Blue, rtb.BackColor, "#H2");
  294. form = new Form ();
  295. form.ShowInTaskbar = false;
  296. form.Controls.Add (rtb);
  297. form.Show ();
  298. Assert.IsTrue (rtb.ReadOnly, "#I1");
  299. Assert.AreEqual (Color.Blue, rtb.BackColor, "#I2");
  300. rtb.ReadOnly = false;
  301. Assert.IsFalse (rtb.ReadOnly, "#J1");
  302. Assert.AreEqual (Color.Blue, rtb.BackColor, "#J2");
  303. rtb.ResetBackColor ();
  304. Assert.IsFalse (rtb.ReadOnly, "#K1");
  305. Assert.AreEqual (SystemColors.Window, rtb.BackColor, "#K2");
  306. form.Close ();
  307. }
  308. [Test]
  309. public void ScrollBarsTest ()
  310. {
  311. RichTextBox rtb = new RichTextBox ();
  312. Assert.AreEqual (RichTextBoxScrollBars.Both, rtb.ScrollBars, "#1");
  313. rtb.ScrollBars = RichTextBoxScrollBars.Vertical;
  314. Assert.AreEqual (RichTextBoxScrollBars.Vertical, rtb.ScrollBars, "#2");
  315. }
  316. [Test]
  317. [ExpectedException (typeof (InvalidEnumArgumentException))]
  318. public void ScrollBars_Invalid ()
  319. {
  320. RichTextBox rtb = new RichTextBox ();
  321. rtb.ScrollBars = (RichTextBoxScrollBars) 666;
  322. }
  323. [Test]
  324. public void SelectionFontTest ()
  325. {
  326. RichTextBox t = new RichTextBox();
  327. t.Text = "123";
  328. t.SelectionStart = 1;
  329. t.SelectionLength = 1;
  330. Font f = new Font(FontFamily.GenericMonospace, 120);
  331. t.SelectionFont = f;
  332. Assert.AreEqual (t.SelectionFont.Size, f.Size, "A1");
  333. }
  334. [Test]
  335. public void SelectionLength_Negative ()
  336. {
  337. RichTextBox rtb = new RichTextBox ();
  338. try {
  339. rtb.SelectionLength = -1;
  340. Assert.Fail ("#1");
  341. #if NET_2_0
  342. } catch (ArgumentOutOfRangeException ex) {
  343. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  344. Assert.IsNull (ex.InnerException, "#3");
  345. Assert.IsNotNull (ex.Message, "#4");
  346. Assert.IsNotNull (ex.ParamName, "#5");
  347. Assert.AreEqual ("SelectionLength", ex.ParamName, "#6");
  348. }
  349. #else
  350. } catch (ArgumentException ex) {
  351. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  352. Assert.IsNull (ex.InnerException, "#3");
  353. Assert.IsNotNull (ex.Message, "#4");
  354. Assert.IsNull (ex.ParamName, "#5");
  355. }
  356. #endif
  357. }
  358. [Test]
  359. public void SelectionStart_Negative ()
  360. {
  361. RichTextBox rtb = new RichTextBox ();
  362. try {
  363. rtb.SelectionStart = -1;
  364. Assert.Fail ("#1");
  365. #if NET_2_0
  366. } catch (ArgumentOutOfRangeException ex) {
  367. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  368. Assert.IsNull (ex.InnerException, "#3");
  369. Assert.IsNotNull (ex.Message, "#4");
  370. Assert.IsNotNull (ex.ParamName, "#5");
  371. Assert.AreEqual ("SelectionStart", ex.ParamName, "#6");
  372. }
  373. #else
  374. } catch (ArgumentException ex) {
  375. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  376. Assert.IsNull (ex.InnerException, "#3");
  377. Assert.IsNotNull (ex.Message, "#4");
  378. Assert.IsNull (ex.ParamName, "#5");
  379. }
  380. #endif
  381. }
  382. RichTextBox rtb;
  383. [Test] // This test passes if it doesn't throw an NRE.
  384. public void Bug351886 ()
  385. {
  386. Form form = new Form ();
  387. rtb = new RichTextBox ();
  388. rtb.Dock = DockStyle.Fill;
  389. rtb.SelectionFont = new Font (FontFamily.GenericSansSerif, 72f);
  390. rtb.AppendText ("Left and make this very long so that it can be word wrapped ");
  391. form.Load += new EventHandler (Bug351886_Load);
  392. form.Controls.Add (rtb);
  393. form.Show ();
  394. form.Close ();
  395. form.Dispose ();
  396. }
  397. void Bug351886_Load (object sender, EventArgs e)
  398. {
  399. rtb.SelectAll ();
  400. rtb.SelectionAlignment = HorizontalAlignment.Center;
  401. }
  402. }
  403. }