TextBoxTest.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. public void TextBoxPropertyTest ()
  71. {
  72. Assert.AreEqual (false, textBox.AcceptsReturn, "#21");
  73. Assert.AreEqual (CharacterCasing.Normal, textBox.CharacterCasing, "#22");
  74. Assert.AreEqual ('\0', textBox.PasswordChar, "#23");
  75. textBox.PasswordChar = '*';
  76. Assert.AreEqual ('*', textBox.PasswordChar, "#23b");
  77. Assert.AreEqual (ScrollBars.None, textBox.ScrollBars, "#24");
  78. Assert.AreEqual (-1, textBox.SelectionLength, "#25");
  79. Assert.AreEqual (HorizontalAlignment.Left , textBox.TextAlign, "#26");
  80. }
  81. #if NET_2_0
  82. [Test]
  83. public void UseSystemPasswordCharDefault()
  84. {
  85. Assert.IsFalse(textBox.UseSystemPasswordChar);
  86. }
  87. [Test]
  88. public void UseSystemPasswordCharOverridesPasswordChar()
  89. {
  90. textBox.PasswordChar = '!';
  91. textBox.UseSystemPasswordChar = true;
  92. Assert.AreEqual('*', textBox.PasswordChar);
  93. }
  94. #endif
  95. [Test]
  96. public void AppendTextTest ()
  97. {
  98. Form f = new Form ();
  99. f.ShowInTaskbar = false;
  100. f.Visible = true;
  101. textBox.Visible = true;
  102. textBox.Text = "TextBox1";
  103. TextBox textBox2 = new TextBox ();
  104. textBox2.Visible = true;
  105. f.Controls.Add (textBox);
  106. f.Controls.Add (textBox2);
  107. textBox2.AppendText (textBox.Text);
  108. Assert.AreEqual ("TextBox1", textBox2.Text, "#27");
  109. f.Dispose ();
  110. }
  111. [Test]
  112. public void AppendTextTest2 ()
  113. {
  114. TextBox textBox2 = new TextBox ();
  115. textBox2.AppendText ("hi");
  116. textBox2.AppendText ("ho");
  117. Assert.AreEqual ("hiho", textBox2.Text, "#1");
  118. Assert.IsNotNull (textBox2.Lines, "#2");
  119. Assert.AreEqual (1, textBox2.Lines.Length, "#3");
  120. Assert.AreEqual ("hiho", textBox2.Lines [0], "#4");
  121. }
  122. [Test]
  123. [Category ("NotWorking")] // bug #79799
  124. public void AppendText_Multiline_CRLF ()
  125. {
  126. TextBox textBox = new TextBox ();
  127. textBox.Text = "ha";
  128. textBox.AppendText ("hi\r\n\r\n");
  129. textBox.AppendText ("ho\r\n");
  130. Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#A1");
  131. Assert.IsNotNull (textBox.Lines, "#A2");
  132. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  133. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  134. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  135. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  136. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  137. textBox.Multiline = true;
  138. textBox.Text = "ha";
  139. textBox.AppendText ("hi\r\n\r\n");
  140. textBox.AppendText ("ho\r\n");
  141. Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#B1");
  142. Assert.IsNotNull (textBox.Lines, "#B2");
  143. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  144. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  145. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  146. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  147. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  148. }
  149. [Test]
  150. [Category ("NotWorking")] // bug #79799
  151. public void AppendText_Multiline_LF ()
  152. {
  153. TextBox textBox = new TextBox ();
  154. textBox.Multiline = true;
  155. textBox.Text = "ha";
  156. textBox.AppendText ("hi\n\n");
  157. textBox.AppendText ("ho\n");
  158. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#A1");
  159. Assert.IsNotNull (textBox.Lines, "#A2");
  160. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  161. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  162. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  163. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  164. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  165. textBox.Multiline = true;
  166. textBox.Text = "ha";
  167. textBox.AppendText ("hi\n\n");
  168. textBox.AppendText ("ho\n");
  169. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#B1");
  170. Assert.IsNotNull (textBox.Lines, "#B2");
  171. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  172. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  173. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  174. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  175. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  176. }
  177. [Test]
  178. public void ClearTest ()
  179. {
  180. textBox.Text = "TextBox1";
  181. Assert.AreEqual ("TextBox1", textBox.Text, "#28a" );
  182. textBox.Clear ();
  183. Assert.AreEqual ("", textBox.Text, "#28b");
  184. }
  185. [Test]
  186. public void ClearUndoTest ()
  187. {
  188. textBox.Text = "TextBox1";
  189. textBox.SelectionLength = 4;
  190. textBox.Copy ();
  191. Assert.AreEqual ("Text", textBox.SelectedText, "#29a");
  192. textBox.Paste ();
  193. Assert.AreEqual (true, textBox.CanUndo, "#29b");
  194. textBox.ClearUndo ();
  195. Assert.AreEqual (false, textBox.CanUndo, "#29c");
  196. }
  197. [Test]
  198. public void CopyTest ()
  199. {
  200. textBox.Text = "ABCDE";
  201. textBox.SelectionLength = 4;
  202. textBox.Copy ();
  203. Assert.AreEqual ("ABCD", textBox.SelectedText, "#30");
  204. }
  205. [Test]
  206. public void CutTest ()
  207. {
  208. textBox.Text = "ABCDE";
  209. textBox.SelectionLength = 4;
  210. textBox.Cut ();
  211. Assert.AreEqual ("E", textBox.Text, "#31");
  212. }
  213. [Test]
  214. public void PasteTest ()
  215. {
  216. textBox.Text = "ABCDE";
  217. textBox.SelectionLength = 4;
  218. textBox.Copy ();
  219. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  220. textBox.Paste ();
  221. Assert.AreEqual ("ABCDABCD", textBox.Text, "#32");
  222. }
  223. [Test]
  224. public void SelectTest ()
  225. {
  226. textBox.Text = "This is a sample test.";
  227. textBox.Select (0, 4);
  228. Assert.AreEqual ("This", textBox.SelectedText, "#33");
  229. }
  230. [Test]
  231. public void SelectAllTest ()
  232. {
  233. textBox.Text = "This is a sample test.";
  234. textBox.SelectAll ();
  235. Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
  236. }
  237. [Test]
  238. public void ToStringTest ()
  239. {
  240. Assert.AreEqual ("System.Windows.Forms.TextBox, Text: ", textBox.ToString(), "#35");
  241. }
  242. [Test]
  243. public void UndoTest1 ()
  244. {
  245. textBox.Text = "ABCDE";
  246. textBox.SelectionLength = 4;
  247. textBox.Copy ();
  248. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  249. textBox.Paste ();
  250. Console.WriteLine ("pre paste text: {0}", textBox.Text);
  251. textBox.Undo ();
  252. Assert.AreEqual ("ABCDE", textBox.Text, "#36");
  253. }
  254. [Test] // bug #79851
  255. public void WrappedText ()
  256. {
  257. string text = "blabla blablabalbalbalbalbalbal blabla blablabl bal " +
  258. "bal bla bal balajkdhfk dskfk ersd dsfjksdhf sdkfjshd f";
  259. textBox.Multiline = true;
  260. textBox.Size = new Size (30, 168);
  261. textBox.Text = text;
  262. Form form = new Form ();
  263. form.Controls.Add (textBox);
  264. form.ShowInTaskbar = false;
  265. form.Show ();
  266. Assert.AreEqual (text, textBox.Text);
  267. }
  268. [Test] // bug #79909
  269. public void MultilineText ()
  270. {
  271. string text = "line1\n\nline2\nline3\r\nline4";
  272. textBox.Multiline = true;
  273. textBox.Size = new Size (300, 168);
  274. textBox.Text = text;
  275. Form form = new Form ();
  276. form.Controls.Add (textBox);
  277. form.ShowInTaskbar = false;
  278. form.Show ();
  279. Assert.AreEqual (text, textBox.Text, "#1");
  280. text = "line1\n\nline2\nline3\r\nline4\rline5\r\n\nline6\n\n\nline7";
  281. textBox.Text = text;
  282. form.Visible = false;
  283. form.Show ();
  284. Assert.AreEqual (text, textBox.Text, "#2");
  285. }
  286. }
  287. }