TextBoxTest.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.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.Text = "ha";
  155. textBox.AppendText ("hi\n\n");
  156. textBox.AppendText ("ho\n");
  157. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#A1");
  158. Assert.IsNotNull (textBox.Lines, "#A2");
  159. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  160. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  161. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  162. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  163. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  164. textBox.Multiline = true;
  165. textBox.Text = "ha";
  166. textBox.AppendText ("hi\n\n");
  167. textBox.AppendText ("ho\n");
  168. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#B1");
  169. Assert.IsNotNull (textBox.Lines, "#B2");
  170. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  171. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  172. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  173. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  174. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  175. }
  176. [Test]
  177. public void ClearTest ()
  178. {
  179. textBox.Text = "TextBox1";
  180. Assert.AreEqual ("TextBox1", textBox.Text, "#28a" );
  181. textBox.Clear ();
  182. Assert.AreEqual ("", textBox.Text, "#28b");
  183. }
  184. [Test]
  185. [Category ("NotWorking")]
  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.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  219. textBox.Paste ();
  220. Assert.AreEqual ("ABCDABCD", textBox.Text, "#32");
  221. }
  222. [Test]
  223. public void SelectTest ()
  224. {
  225. textBox.Text = "This is a sample test.";
  226. textBox.Select (0, 4);
  227. Assert.AreEqual ("This", textBox.SelectedText, "#33");
  228. }
  229. [Test]
  230. public void SelectAllTest ()
  231. {
  232. textBox.Text = "This is a sample test.";
  233. textBox.SelectAll ();
  234. Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
  235. }
  236. [Test]
  237. public void ToStringTest ()
  238. {
  239. Assert.AreEqual ("System.Windows.Forms.TextBox, Text: ", textBox.ToString(), "#35");
  240. }
  241. [Test]
  242. [Category ("NotWorking")]
  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. textBox.Undo ();
  251. Assert.AreEqual ("ABCDE", textBox.Text, "#36");
  252. }
  253. }
  254. }