TextBoxTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // Copyright (c) 2005 Novell, Inc.
  3. //
  4. // Authors:
  5. // Ritvik Mayank ([email protected])
  6. //
  7. using System;
  8. using System.Drawing;
  9. using System.Reflection;
  10. using System.Windows.Forms;
  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. Clipboard.SetDataObject ("TEST");
  44. textBox.Paste ();
  45. Assert.AreEqual (true, textBox.CanUndo, "#6b");
  46. textBox.Undo ();
  47. textBox.ClearUndo ();
  48. Assert.AreEqual (false, textBox.CanUndo, "#6c");
  49. Assert.AreEqual ("WindowText", textBox.ForeColor.Name, "#7");
  50. Assert.AreEqual (true, textBox.HideSelection, "#8");
  51. Assert.AreEqual (1, textBox.Lines.Length, "#9");
  52. Assert.AreEqual (32767, textBox.MaxLength, "#10");
  53. Assert.AreEqual (true, textBox.Modified, "#11");
  54. Assert.AreEqual (true, textBox.Multiline, "#12a");
  55. textBox.WordWrap = false;
  56. Assert.AreEqual (true, textBox.Multiline, "#12b");
  57. textBox.AcceptsReturn = true;
  58. Assert.AreEqual (true, textBox.Multiline, "#12c");
  59. Assert.AreEqual (20, textBox.PreferredHeight, "#13");
  60. Assert.AreEqual (false, textBox.ReadOnly, "#14");
  61. Assert.AreEqual ("", textBox.SelectedText, "#15");
  62. textBox.Text = "sample TextBox";
  63. Assert.AreEqual (0, textBox.SelectionLength, "#16b");
  64. Assert.AreEqual (0, textBox.SelectionStart, "#17");
  65. textBox.WordWrap = false;
  66. textBox.AcceptsReturn = true;
  67. Assert.AreEqual ("sample TextBox", textBox.Text, "#18");
  68. Assert.AreEqual (14, textBox.TextLength, "#19");
  69. Assert.AreEqual (false, textBox.WordWrap, "#20");
  70. }
  71. [Test]
  72. public void TextBoxPropertyTest ()
  73. {
  74. Assert.AreEqual (false, textBox.AcceptsReturn, "#21");
  75. Assert.AreEqual (CharacterCasing.Normal, textBox.CharacterCasing, "#22");
  76. Assert.AreEqual ('\0', textBox.PasswordChar, "#23");
  77. textBox.PasswordChar = '*';
  78. Assert.AreEqual ('*', textBox.PasswordChar, "#23b");
  79. Assert.AreEqual (ScrollBars.None, textBox.ScrollBars, "#24");
  80. Assert.AreEqual (-1, textBox.SelectionLength, "#25");
  81. Assert.AreEqual (HorizontalAlignment.Left , textBox.TextAlign, "#26");
  82. #if NET_2_0
  83. Assert.AreEqual (true, textBox.AutoCompleteCustomSource != null, "#27");
  84. Assert.AreEqual (AutoCompleteMode.None, textBox.AutoCompleteMode, "#28");
  85. Assert.AreEqual (AutoCompleteSource.None, textBox.AutoCompleteSource, "#29");
  86. textBox.AutoCompleteCustomSource = null;
  87. Assert.AreEqual (true, textBox.AutoCompleteCustomSource != null, "#30");
  88. #endif
  89. }
  90. #if NET_2_0
  91. [Test]
  92. public void UseSystemPasswordCharDefault()
  93. {
  94. Assert.IsFalse(textBox.UseSystemPasswordChar);
  95. }
  96. [Test]
  97. public void UseSystemPasswordCharOverridesPasswordChar()
  98. {
  99. textBox.PasswordChar = '!';
  100. textBox.UseSystemPasswordChar = true;
  101. Assert.AreEqual('*', textBox.PasswordChar);
  102. }
  103. #endif
  104. [Test]
  105. public void AppendTextTest ()
  106. {
  107. Form f = new Form ();
  108. f.ShowInTaskbar = false;
  109. f.Visible = true;
  110. textBox.Visible = true;
  111. textBox.Text = "TextBox1";
  112. TextBox textBox2 = new TextBox ();
  113. textBox2.Visible = true;
  114. f.Controls.Add (textBox);
  115. f.Controls.Add (textBox2);
  116. textBox2.AppendText (textBox.Text);
  117. Assert.AreEqual ("TextBox1", textBox2.Text, "#27");
  118. f.Dispose ();
  119. }
  120. [Test]
  121. public void AppendTextTest2 ()
  122. {
  123. TextBox textBox2 = new TextBox ();
  124. textBox2.AppendText ("hi");
  125. textBox2.AppendText ("ho");
  126. Assert.AreEqual ("hiho", textBox2.Text, "#1");
  127. Assert.IsNotNull (textBox2.Lines, "#2");
  128. Assert.AreEqual (1, textBox2.Lines.Length, "#3");
  129. Assert.AreEqual ("hiho", textBox2.Lines [0], "#4");
  130. }
  131. [Test]
  132. public void AppendText_Multiline_CRLF ()
  133. {
  134. TextBox textBox = new TextBox ();
  135. textBox.Text = "ha";
  136. textBox.AppendText ("hi\r\n\r\n");
  137. textBox.AppendText ("ho\r\n");
  138. Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#A1");
  139. Assert.IsNotNull (textBox.Lines, "#A2");
  140. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  141. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  142. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  143. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  144. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  145. textBox.Multiline = true;
  146. textBox.Text = "ha";
  147. textBox.AppendText ("hi\r\n\r\n");
  148. textBox.AppendText ("ho\r\n");
  149. Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#B1");
  150. Assert.IsNotNull (textBox.Lines, "#B2");
  151. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  152. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  153. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  154. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  155. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  156. }
  157. [Test]
  158. public void AppendText_Multiline_LF ()
  159. {
  160. TextBox textBox = new TextBox ();
  161. textBox.Text = "ha";
  162. textBox.AppendText ("hi\n\n");
  163. textBox.AppendText ("ho\n");
  164. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#A1");
  165. Assert.IsNotNull (textBox.Lines, "#A2");
  166. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  167. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  168. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  169. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  170. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  171. textBox.Multiline = true;
  172. textBox.Text = "ha";
  173. textBox.AppendText ("hi\n\n");
  174. textBox.AppendText ("ho\n");
  175. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#B1");
  176. Assert.IsNotNull (textBox.Lines, "#B2");
  177. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  178. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  179. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  180. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  181. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  182. }
  183. [Test]
  184. public void ClearTest ()
  185. {
  186. textBox.Text = "TextBox1";
  187. Assert.AreEqual ("TextBox1", textBox.Text, "#28a" );
  188. textBox.Clear ();
  189. Assert.AreEqual ("", textBox.Text, "#28b");
  190. }
  191. [Test]
  192. public void ClearUndoTest ()
  193. {
  194. textBox.Text = "TextBox1";
  195. textBox.SelectionLength = 4;
  196. textBox.Copy ();
  197. Assert.AreEqual ("Text", textBox.SelectedText, "#29a");
  198. textBox.Paste ();
  199. Assert.AreEqual (true, textBox.CanUndo, "#29b");
  200. textBox.ClearUndo ();
  201. Assert.AreEqual (false, textBox.CanUndo, "#29c");
  202. }
  203. [Test] // bug #80163
  204. public void ContextMenu ()
  205. {
  206. TextBox textBox = new TextBox ();
  207. Assert.IsNull (textBox.ContextMenu);
  208. }
  209. [Test]
  210. public void CopyTest ()
  211. {
  212. textBox.Text = "ABCDE";
  213. textBox.SelectionLength = 4;
  214. textBox.Copy ();
  215. Assert.AreEqual ("ABCD", textBox.SelectedText, "#30");
  216. }
  217. [Test]
  218. public void CutTest ()
  219. {
  220. textBox.Text = "ABCDE";
  221. textBox.SelectionLength = 4;
  222. textBox.Cut ();
  223. Assert.AreEqual ("E", textBox.Text, "#31");
  224. }
  225. [Test]
  226. public void PasteTest ()
  227. {
  228. textBox.Text = "ABCDE";
  229. textBox.SelectionLength = 4;
  230. textBox.Copy ();
  231. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  232. textBox.Paste ();
  233. Assert.AreEqual ("ABCDABCD", textBox.Text, "#32");
  234. }
  235. [Test] // bug #80301
  236. [Ignore ("Depends on specific DPI")]
  237. public void PreferredHeight ()
  238. {
  239. textBox.Font = new Font ("Arial", 14);
  240. Assert.AreEqual (29, textBox.PreferredHeight, "#A1");
  241. textBox.Font = new Font ("Arial", 16);
  242. Assert.AreEqual (32, textBox.PreferredHeight, "#A2");
  243. textBox.Font = new Font ("Arial", 17);
  244. Assert.AreEqual (34, textBox.PreferredHeight, "#A3");
  245. textBox.BorderStyle = BorderStyle.None;
  246. Assert.AreEqual (27, textBox.PreferredHeight, "#B1");
  247. textBox.Font = new Font ("Arial", 14);
  248. Assert.AreEqual (22, textBox.PreferredHeight, "#B2");
  249. textBox.Font = new Font ("Arial", 16);
  250. Assert.AreEqual (25, textBox.PreferredHeight, "#B3");
  251. }
  252. [Test]
  253. public void SelectTest ()
  254. {
  255. textBox.Text = "This is a sample test.";
  256. textBox.Select (0, 4);
  257. Assert.AreEqual ("This", textBox.SelectedText, "#33");
  258. }
  259. [Test]
  260. public void SelectAllTest ()
  261. {
  262. textBox.Text = "This is a sample test.";
  263. textBox.SelectAll ();
  264. Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
  265. }
  266. [Test]
  267. public void FocusSelectsAllTest ()
  268. {
  269. textBox.Text = "This is a sample test.";
  270. textBox.CreateControl ();
  271. textBox.Focus ();
  272. Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
  273. }
  274. [Test]
  275. public void ToStringTest ()
  276. {
  277. Assert.AreEqual ("System.Windows.Forms.TextBox, Text: ", textBox.ToString(), "#35");
  278. }
  279. [Test]
  280. public void UndoTest1 ()
  281. {
  282. textBox.Text = "ABCDE";
  283. textBox.SelectionLength = 4;
  284. textBox.Copy ();
  285. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  286. textBox.Paste ();
  287. Console.WriteLine ("pre paste text: {0}", textBox.Text);
  288. textBox.Undo ();
  289. Assert.AreEqual ("ABCDE", textBox.Text, "#36");
  290. }
  291. [Test] // bug #79851
  292. public void WrappedText ()
  293. {
  294. string text = "blabla blablabalbalbalbalbalbal blabla blablabl bal " +
  295. "bal bla bal balajkdhfk dskfk ersd dsfjksdhf sdkfjshd f";
  296. textBox.Multiline = true;
  297. textBox.Size = new Size (30, 168);
  298. textBox.Text = text;
  299. Form form = new Form ();
  300. form.Controls.Add (textBox);
  301. form.ShowInTaskbar = false;
  302. form.Show ();
  303. Assert.AreEqual (text, textBox.Text);
  304. }
  305. [Test] // bug #79909
  306. public void MultilineText ()
  307. {
  308. string text = "line1\n\nline2\nline3\r\nline4";
  309. textBox.Size = new Size (300, 168);
  310. textBox.Text = text;
  311. Form form = new Form ();
  312. form.Controls.Add (textBox);
  313. form.ShowInTaskbar = false;
  314. form.Show ();
  315. Assert.AreEqual (text, textBox.Text, "#1");
  316. text = "line1\n\nline2\nline3\r\nline4\rline5\r\n\nline6\n\n\nline7";
  317. textBox.Text = text;
  318. form.Visible = false;
  319. form.Show ();
  320. Assert.AreEqual (text, textBox.Text, "#2");
  321. }
  322. [Test]
  323. public void ModifiedTest ()
  324. {
  325. Assert.AreEqual (true, textBox.Modified, "modified-1");
  326. textBox.Modified = false;
  327. Assert.AreEqual (false, textBox.Modified, "modified-2");
  328. textBox.Modified = true;
  329. Assert.AreEqual (true, textBox.Modified, "modified-2");
  330. textBox.Text = "TEXT";
  331. Assert.AreEqual (false, textBox.Modified, "modified-3");
  332. }
  333. }
  334. }