TextBoxTest.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // Copyright (c) 2005 Novell, Inc.
  3. //
  4. // Authors:
  5. // Ritvik Mayank ([email protected])
  6. //
  7. using System;
  8. using System.Windows.Forms;
  9. using System.Drawing;
  10. using System.Reflection;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Windows.Forms
  13. {
  14. [TestFixture]
  15. public class TextBoxTest
  16. {
  17. TextBox textBox;
  18. [SetUp]
  19. public void SetUp()
  20. {
  21. textBox = new TextBox();
  22. }
  23. [Test]
  24. [Category ("NotWorking")]
  25. public void TextBoxBasePropertyTest ()
  26. {
  27. Assert.AreEqual (false, textBox.AcceptsTab, "#1a");
  28. textBox.Multiline = true;
  29. textBox.AcceptsTab = true;
  30. SendKeys.SendWait ("^%");
  31. Assert.AreEqual (true, textBox.AcceptsTab, "#1b");
  32. Assert.AreEqual (true, textBox.AutoSize, "#2");
  33. Assert.AreEqual ("Window", textBox.BackColor.Name, "#3a");
  34. textBox.BackColor = Color.White;
  35. Assert.AreEqual ("White", textBox.BackColor.Name, "#3b");
  36. Assert.AreEqual (null, textBox.BackgroundImage, "#4a");
  37. string gif = "M.gif";
  38. textBox.BackgroundImage = Image.FromFile (gif);
  39. // comparing image objects fails on MS .Net so using Size property
  40. Assert.AreEqual (Image.FromFile(gif, true).Size, textBox.BackgroundImage.Size, "#4b");
  41. Assert.AreEqual (BorderStyle.Fixed3D, textBox.BorderStyle, "#5");
  42. Assert.AreEqual (false, textBox.CanUndo, "#6a");
  43. textBox.Paste ();
  44. Assert.AreEqual (true, textBox.CanUndo, "#6b");
  45. textBox.ClearUndo ();
  46. Assert.AreEqual (false, textBox.CanUndo, "#6c");
  47. Assert.AreEqual ("WindowText", textBox.ForeColor.Name, "#7");
  48. Assert.AreEqual (true, textBox.HideSelection, "#8");
  49. Assert.AreEqual (1, textBox.Lines.Length, "#9");
  50. Assert.AreEqual (32767, textBox.MaxLength, "#10");
  51. Assert.AreEqual (true, textBox.Modified, "#11");
  52. Assert.AreEqual (true, textBox.Multiline, "#12a");
  53. textBox.WordWrap = false;
  54. Assert.AreEqual (true, textBox.Multiline, "#12b");
  55. textBox.AcceptsReturn = true;
  56. Assert.AreEqual (true, textBox.Multiline, "#12c");
  57. Assert.AreEqual (20, textBox.PreferredHeight, "#13");
  58. Assert.AreEqual (false, textBox.ReadOnly, "#14");
  59. Assert.AreEqual ("", textBox.SelectedText, "#15");
  60. textBox.Text = "sample TextBox";
  61. Assert.AreEqual (0, textBox.SelectionLength, "#16b");
  62. Assert.AreEqual (0, textBox.SelectionStart, "#17");
  63. textBox.WordWrap = false;
  64. textBox.AcceptsReturn = true;
  65. Assert.AreEqual ("sample TextBox", textBox.Text, "#18");
  66. Assert.AreEqual (14, textBox.TextLength, "#19");
  67. Assert.AreEqual (false, textBox.WordWrap, "#20");
  68. }
  69. [Test]
  70. [Category ("NotWorking")]
  71. public void TextBoxPropertyTest ()
  72. {
  73. Assert.AreEqual (false, textBox.AcceptsReturn, "#21");
  74. Assert.AreEqual (CharacterCasing.Normal, textBox.CharacterCasing, "#22");
  75. Assert.AreEqual ('\0', textBox.PasswordChar, "#23");
  76. textBox.PasswordChar = '*';
  77. Assert.AreEqual ('*', textBox.PasswordChar, "#23b");
  78. Assert.AreEqual (ScrollBars.None, textBox.ScrollBars, "#24");
  79. Assert.AreEqual (-1, textBox.SelectionLength, "#25");
  80. Assert.AreEqual (HorizontalAlignment.Left , textBox.TextAlign, "#26");
  81. }
  82. #if NET_2_0
  83. [Test]
  84. public void UseSystemPasswordCharDefault()
  85. {
  86. Assert.IsFalse(textBox.UseSystemPasswordChar);
  87. }
  88. [Test]
  89. public void UseSystemPasswordCharOverridesPasswordChar()
  90. {
  91. textBox.PasswordChar = '!';
  92. textBox.UseSystemPasswordChar = true;
  93. Assert.AreEqual('*', textBox.PasswordChar);
  94. }
  95. #endif
  96. [Test]
  97. public void AppendTextTest ()
  98. {
  99. Form f = new Form ();
  100. f.ShowInTaskbar = false;
  101. f.Visible = true;
  102. textBox.Visible = true;
  103. textBox.Text = "TextBox1";
  104. TextBox textBox2 = new TextBox ();
  105. textBox2.Visible = true;
  106. f.Controls.Add (textBox);
  107. f.Controls.Add (textBox2);
  108. textBox2.AppendText (textBox.Text);
  109. Assert.AreEqual ("TextBox1", textBox2.Text, "#27");
  110. f.Dispose ();
  111. }
  112. [Test]
  113. public void AppendTextTest2 ()
  114. {
  115. TextBox textBox2 = new TextBox ();
  116. textBox2.AppendText ("hi");
  117. textBox2.AppendText ("ho");
  118. Assert.AreEqual ("hiho", textBox2.Text, "#1");
  119. Assert.IsNotNull (textBox2.Lines, "#2");
  120. Assert.AreEqual (1, textBox2.Lines.Length, "#3");
  121. Assert.AreEqual ("hiho", textBox2.Lines [0], "#4");
  122. }
  123. [Test]
  124. [Category ("NotWorking")] // bug #79799
  125. public void AppendText_Multiline_CRLF ()
  126. {
  127. TextBox textBox = new TextBox ();
  128. textBox.Text = "ha";
  129. textBox.AppendText ("hi\r\n\r\n");
  130. textBox.AppendText ("ho\r\n");
  131. Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#A1");
  132. Assert.IsNotNull (textBox.Lines, "#A2");
  133. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  134. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  135. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  136. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  137. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  138. textBox.Multiline = true;
  139. textBox.Text = "ha";
  140. textBox.AppendText ("hi\r\n\r\n");
  141. textBox.AppendText ("ho\r\n");
  142. Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#B1");
  143. Assert.IsNotNull (textBox.Lines, "#B2");
  144. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  145. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  146. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  147. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  148. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  149. }
  150. [Test]
  151. [Category ("NotWorking")] // bug #79799
  152. public void AppendText_Multiline_LF ()
  153. {
  154. TextBox textBox = new TextBox ();
  155. textBox.Multiline = true;
  156. textBox.Text = "ha";
  157. textBox.AppendText ("hi\n\n");
  158. textBox.AppendText ("ho\n");
  159. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#A1");
  160. Assert.IsNotNull (textBox.Lines, "#A2");
  161. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  162. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  163. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  164. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  165. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  166. textBox.Multiline = true;
  167. textBox.Text = "ha";
  168. textBox.AppendText ("hi\n\n");
  169. textBox.AppendText ("ho\n");
  170. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#B1");
  171. Assert.IsNotNull (textBox.Lines, "#B2");
  172. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  173. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  174. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  175. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  176. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  177. }
  178. [Test]
  179. public void ClearTest ()
  180. {
  181. textBox.Text = "TextBox1";
  182. Assert.AreEqual ("TextBox1", textBox.Text, "#28a" );
  183. textBox.Clear ();
  184. Assert.AreEqual ("", textBox.Text, "#28b");
  185. }
  186. [Test]
  187. public void ClearUndoTest ()
  188. {
  189. textBox.Text = "TextBox1";
  190. textBox.SelectionLength = 4;
  191. textBox.Copy ();
  192. Assert.AreEqual ("Text", textBox.SelectedText, "#29a");
  193. textBox.Paste ();
  194. Assert.AreEqual (true, textBox.CanUndo, "#29b");
  195. textBox.ClearUndo ();
  196. Assert.AreEqual (false, textBox.CanUndo, "#29c");
  197. }
  198. [Test]
  199. public void CopyTest ()
  200. {
  201. textBox.Text = "ABCDE";
  202. textBox.SelectionLength = 4;
  203. textBox.Copy ();
  204. Assert.AreEqual ("ABCD", textBox.SelectedText, "#30");
  205. }
  206. [Test]
  207. public void CutTest ()
  208. {
  209. textBox.Text = "ABCDE";
  210. textBox.SelectionLength = 4;
  211. textBox.Cut ();
  212. Assert.AreEqual ("E", textBox.Text, "#31");
  213. }
  214. [Test]
  215. public void PasteTest ()
  216. {
  217. textBox.Text = "ABCDE";
  218. textBox.SelectionLength = 4;
  219. textBox.Copy ();
  220. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  221. textBox.Paste ();
  222. Assert.AreEqual ("ABCDABCD", textBox.Text, "#32");
  223. }
  224. [Test]
  225. public void SelectTest ()
  226. {
  227. textBox.Text = "This is a sample test.";
  228. textBox.Select (0, 4);
  229. Assert.AreEqual ("This", textBox.SelectedText, "#33");
  230. }
  231. [Test]
  232. public void SelectAllTest ()
  233. {
  234. textBox.Text = "This is a sample test.";
  235. textBox.SelectAll ();
  236. Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
  237. }
  238. [Test]
  239. public void ToStringTest ()
  240. {
  241. Assert.AreEqual ("System.Windows.Forms.TextBox, Text: ", textBox.ToString(), "#35");
  242. }
  243. [Test]
  244. public void UndoTest1 ()
  245. {
  246. textBox.Text = "ABCDE";
  247. textBox.SelectionLength = 4;
  248. textBox.Copy ();
  249. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  250. textBox.Paste ();
  251. Console.WriteLine ("pre paste text: {0}", textBox.Text);
  252. textBox.Undo ();
  253. Assert.AreEqual ("ABCDE", textBox.Text, "#36");
  254. }
  255. [Test] // bug #79851
  256. public void WrappedText ()
  257. {
  258. string text = "blabla blablabalbalbalbalbalbal blabla blablabl bal " +
  259. "bal bla bal balajkdhfk dskfk ersd dsfjksdhf sdkfjshd f";
  260. textBox.Multiline = true;
  261. textBox.Size = new Size (30, 168);
  262. textBox.Text = text;
  263. Form form = new Form ();
  264. form.Controls.Add (textBox);
  265. form.ShowInTaskbar = false;
  266. form.Show ();
  267. Assert.AreEqual (text, textBox.Text);
  268. }
  269. [Test] // bug #79909
  270. public void MultilineText ()
  271. {
  272. string text = "line1\n\nline2\nline3\r\nline4";
  273. textBox.Multiline = true;
  274. textBox.Size = new Size (300, 168);
  275. textBox.Text = text;
  276. Form form = new Form ();
  277. form.Controls.Add (textBox);
  278. form.ShowInTaskbar = false;
  279. form.Show ();
  280. Assert.AreEqual (text, textBox.Text, "#1");
  281. text = "line1\n\nline2\nline3\r\nline4\rline5\r\n\nline6\n\n\nline7";
  282. textBox.Text = text;
  283. form.Visible = false;
  284. form.Show ();
  285. Assert.AreEqual (text, textBox.Text, "#2");
  286. }
  287. }
  288. }