TextBoxTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. //
  2. // Copyright (c) 2005 Novell, Inc.
  3. //
  4. // Authors:
  5. // Ritvik Mayank ([email protected])
  6. //
  7. using System;
  8. using System.ComponentModel;
  9. using System.Drawing;
  10. using System.Reflection;
  11. using System.Windows.Forms;
  12. using NUnit.Framework;
  13. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  14. namespace MonoTests.System.Windows.Forms
  15. {
  16. [TestFixture]
  17. public class TextBoxTest
  18. {
  19. TextBox textBox;
  20. int _changed;
  21. int _invalidated;
  22. int _paint;
  23. [SetUp]
  24. public void SetUp()
  25. {
  26. textBox = new TextBox();
  27. textBox.Invalidated += new InvalidateEventHandler (TextBox_Invalidated);
  28. textBox.Paint += new PaintEventHandler (TextBox_Paint);
  29. textBox.TextChanged += new EventHandler (TextBox_TextChanged);
  30. Reset ();
  31. }
  32. [Test]
  33. [Category ("NotWorking")]
  34. public void TextBoxBasePropertyTest ()
  35. {
  36. Assert.AreEqual (false, textBox.AcceptsTab, "#1a");
  37. textBox.Multiline = true;
  38. textBox.AcceptsTab = true;
  39. // SendKeys.SendWait ("^%");
  40. Assert.AreEqual (true, textBox.AcceptsTab, "#1b");
  41. Assert.AreEqual (true, textBox.AutoSize, "#2");
  42. Assert.AreEqual (null, textBox.BackgroundImage, "#4a");
  43. string gif = "M.gif";
  44. textBox.BackgroundImage = Image.FromFile (gif);
  45. // comparing image objects fails on MS .Net so using Size property
  46. Assert.AreEqual (Image.FromFile(gif, true).Size, textBox.BackgroundImage.Size, "#4b");
  47. Assert.AreEqual (BorderStyle.Fixed3D, textBox.BorderStyle, "#5");
  48. Assert.AreEqual (false, textBox.CanUndo, "#6a");
  49. Clipboard.SetDataObject ("TEST");
  50. textBox.Paste ();
  51. Assert.AreEqual (true, textBox.CanUndo, "#6b");
  52. textBox.Undo ();
  53. textBox.ClearUndo ();
  54. Assert.AreEqual (false, textBox.CanUndo, "#6c");
  55. Assert.AreEqual (true, textBox.HideSelection, "#8");
  56. Assert.AreEqual (0, textBox.Lines.Length, "#9");
  57. Assert.AreEqual (32767, textBox.MaxLength, "#10");
  58. Assert.AreEqual (true, textBox.Modified, "#11");
  59. Assert.AreEqual (true, textBox.Multiline, "#12a");
  60. textBox.WordWrap = false;
  61. Assert.AreEqual (true, textBox.Multiline, "#12b");
  62. textBox.AcceptsReturn = true;
  63. Assert.AreEqual (true, textBox.Multiline, "#12c");
  64. Assert.AreEqual (20, textBox.PreferredHeight, "#13");
  65. Assert.AreEqual (false, textBox.ReadOnly, "#14");
  66. Assert.AreEqual ("", textBox.SelectedText, "#15");
  67. textBox.Text = "sample TextBox";
  68. Assert.AreEqual (0, textBox.SelectionLength, "#16b");
  69. Assert.AreEqual (0, textBox.SelectionStart, "#17");
  70. textBox.WordWrap = false;
  71. textBox.AcceptsReturn = true;
  72. Assert.AreEqual ("sample TextBox", textBox.Text, "#18");
  73. Assert.AreEqual (14, textBox.TextLength, "#19");
  74. Assert.AreEqual (false, textBox.WordWrap, "#20");
  75. }
  76. [Test]
  77. public void TextBoxPropertyTest ()
  78. {
  79. Assert.AreEqual (false, textBox.AcceptsReturn, "#21");
  80. Assert.AreEqual (CharacterCasing.Normal, textBox.CharacterCasing, "#22");
  81. Assert.AreEqual ('\0', textBox.PasswordChar, "#23");
  82. textBox.PasswordChar = '*';
  83. Assert.AreEqual ('*', textBox.PasswordChar, "#23b");
  84. Assert.AreEqual (ScrollBars.None, textBox.ScrollBars, "#24");
  85. #if NET_2_0
  86. Assert.AreEqual (0, textBox.SelectionLength, "#25-NET20");
  87. #else
  88. Assert.AreEqual (-1, textBox.SelectionLength, "#25-NET11");
  89. #endif
  90. Assert.AreEqual (HorizontalAlignment.Left , textBox.TextAlign, "#26");
  91. #if NET_2_0
  92. Assert.AreEqual (true, textBox.AutoCompleteCustomSource != null, "#27");
  93. Assert.AreEqual (AutoCompleteMode.None, textBox.AutoCompleteMode, "#28");
  94. Assert.AreEqual (AutoCompleteSource.None, textBox.AutoCompleteSource, "#29");
  95. textBox.AutoCompleteCustomSource = null;
  96. Assert.AreEqual (true, textBox.AutoCompleteCustomSource != null, "#30");
  97. #endif
  98. }
  99. #if NET_2_0
  100. [Test]
  101. public void UseSystemPasswordCharDefault()
  102. {
  103. Assert.IsFalse(textBox.UseSystemPasswordChar);
  104. }
  105. [Test]
  106. public void UseSystemPasswordCharOverridesPasswordChar()
  107. {
  108. textBox.PasswordChar = '!';
  109. textBox.UseSystemPasswordChar = true;
  110. Assert.AreEqual('*', textBox.PasswordChar);
  111. }
  112. #endif
  113. [Test]
  114. public void AppendTextTest ()
  115. {
  116. Form f = new Form ();
  117. f.ShowInTaskbar = false;
  118. f.Visible = true;
  119. textBox.Visible = true;
  120. textBox.Text = "TextBox1";
  121. TextBox textBox2 = new TextBox ();
  122. textBox2.Visible = true;
  123. f.Controls.Add (textBox);
  124. f.Controls.Add (textBox2);
  125. textBox2.AppendText (textBox.Text);
  126. Assert.AreEqual ("TextBox1", textBox2.Text, "#27");
  127. f.Dispose ();
  128. }
  129. [Test]
  130. public void AppendTextTest2 ()
  131. {
  132. TextBox textBox2 = new TextBox ();
  133. textBox2.AppendText ("hi");
  134. textBox2.AppendText ("ho");
  135. Assert.AreEqual ("hiho", textBox2.Text, "#1");
  136. Assert.IsNotNull (textBox2.Lines, "#2");
  137. Assert.AreEqual (1, textBox2.Lines.Length, "#3");
  138. Assert.AreEqual ("hiho", textBox2.Lines [0], "#4");
  139. }
  140. [Test]
  141. public void AppendText_Multiline_CRLF ()
  142. {
  143. TextBox textBox = new TextBox ();
  144. textBox.Text = "ha";
  145. textBox.AppendText ("hi\r\n\r\n");
  146. textBox.AppendText ("ho\r\n");
  147. Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#A1");
  148. Assert.IsNotNull (textBox.Lines, "#A2");
  149. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  150. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  151. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  152. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  153. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  154. textBox.Multiline = true;
  155. textBox.Text = "ha";
  156. textBox.AppendText ("hi\r\n\r\n");
  157. textBox.AppendText ("ho\r\n");
  158. Assert.AreEqual ("hahi\r\n\r\nho\r\n", textBox.Text, "#B1");
  159. Assert.IsNotNull (textBox.Lines, "#B2");
  160. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  161. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  162. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  163. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  164. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  165. }
  166. [Test]
  167. public void AppendText_Multiline_LF ()
  168. {
  169. TextBox textBox = new TextBox ();
  170. textBox.Text = "ha";
  171. textBox.AppendText ("hi\n\n");
  172. textBox.AppendText ("ho\n");
  173. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#A1");
  174. Assert.IsNotNull (textBox.Lines, "#A2");
  175. Assert.AreEqual (4, textBox.Lines.Length, "#A3");
  176. Assert.AreEqual ("hahi", textBox.Lines [0], "#A4");
  177. Assert.AreEqual (string.Empty, textBox.Lines [1], "#A5");
  178. Assert.AreEqual ("ho", textBox.Lines [2], "#A6");
  179. Assert.AreEqual (string.Empty, textBox.Lines [3], "#A7");
  180. textBox.Multiline = true;
  181. textBox.Text = "ha";
  182. textBox.AppendText ("hi\n\n");
  183. textBox.AppendText ("ho\n");
  184. Assert.AreEqual ("hahi\n\nho\n", textBox.Text, "#B1");
  185. Assert.IsNotNull (textBox.Lines, "#B2");
  186. Assert.AreEqual (4, textBox.Lines.Length, "#B3");
  187. Assert.AreEqual ("hahi", textBox.Lines [0], "#B4");
  188. Assert.AreEqual (string.Empty, textBox.Lines [1], "#B5");
  189. Assert.AreEqual ("ho", textBox.Lines [2], "#B6");
  190. Assert.AreEqual (string.Empty, textBox.Lines [3], "#B7");
  191. }
  192. [Test]
  193. public void BackColorTest ()
  194. {
  195. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#A1");
  196. textBox.BackColor = Color.Red;
  197. Assert.AreEqual (Color.Red, textBox.BackColor, "#A2");
  198. textBox.BackColor = Color.White;
  199. Assert.AreEqual (Color.White, textBox.BackColor, "#A3");
  200. Assert.AreEqual (0, _invalidated, "#A4");
  201. Assert.AreEqual (0, _paint, "#A5");
  202. Form form = new Form ();
  203. form.ShowInTaskbar = false;
  204. form.Controls.Add (textBox);
  205. form.Show ();
  206. _invalidated = 0;
  207. _paint = 0;
  208. Assert.AreEqual (Color.White, textBox.BackColor, "#B1");
  209. Assert.AreEqual (0, _invalidated, "#B2");
  210. Assert.AreEqual (0, _paint, "#B3");
  211. textBox.BackColor = Color.Red;
  212. Assert.AreEqual (Color.Red, textBox.BackColor, "#B4");
  213. Assert.AreEqual (1, _invalidated, "#B5");
  214. Assert.AreEqual (0, _paint, "#B6");
  215. textBox.BackColor = Color.Red;
  216. Assert.AreEqual (Color.Red, textBox.BackColor, "#B7");
  217. Assert.AreEqual (1, _invalidated, "#B8");
  218. Assert.AreEqual (0, _paint, "#B9");
  219. textBox.BackColor = Color.Blue;
  220. Assert.AreEqual (Color.Blue, textBox.BackColor, "#B10");
  221. Assert.AreEqual (2, _invalidated, "#B11");
  222. Assert.AreEqual (0, _paint, "#B12");
  223. textBox.BackColor = Color.Empty;
  224. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#B13");
  225. Assert.AreEqual (3, _invalidated, "#B14");
  226. Assert.AreEqual (0, _paint, "#B15");
  227. form.Close ();
  228. }
  229. [Test] // bug #80626
  230. [Ignore ("Depends on default font height")]
  231. public void BorderStyle_None ()
  232. {
  233. textBox.BorderStyle = BorderStyle.None;
  234. Assert.AreEqual (20, textBox.Height, "#1");
  235. textBox.CreateControl ();
  236. Assert.AreEqual (13, textBox.Height, "#2");
  237. }
  238. [Test]
  239. public void ClearTest ()
  240. {
  241. textBox.Text = "TextBox1";
  242. Assert.AreEqual ("TextBox1", textBox.Text, "#28a" );
  243. textBox.Clear ();
  244. Assert.AreEqual ("", textBox.Text, "#28b");
  245. }
  246. [Test]
  247. public void ClearUndoTest ()
  248. {
  249. textBox.Text = "TextBox1";
  250. textBox.SelectionLength = 4;
  251. textBox.Copy ();
  252. Assert.AreEqual ("Text", textBox.SelectedText, "#29a");
  253. textBox.Paste ();
  254. Assert.AreEqual (true, textBox.CanUndo, "#29b");
  255. textBox.ClearUndo ();
  256. Assert.AreEqual (false, textBox.CanUndo, "#29c");
  257. }
  258. [Test] // bug #80620
  259. [Ignore ("Depends on default font height")]
  260. public void ClientRectangle_Borders ()
  261. {
  262. textBox.CreateControl ();
  263. Assert.AreEqual (textBox.ClientRectangle, new TextBox ().ClientRectangle);
  264. }
  265. [Test] // bug #80163
  266. public void ContextMenu ()
  267. {
  268. TextBox textBox = new TextBox ();
  269. Assert.IsNull (textBox.ContextMenu);
  270. }
  271. [Test]
  272. public void CopyTest ()
  273. {
  274. textBox.Text = "ABCDE";
  275. textBox.SelectionLength = 4;
  276. Assert.AreEqual (1, _changed, "#1");
  277. textBox.Copy ();
  278. Assert.AreEqual (1, _changed, "#2");
  279. Assert.AreEqual ("ABCD", textBox.SelectedText, "#3");
  280. }
  281. [Test]
  282. public void CutTest ()
  283. {
  284. textBox.Text = "ABCDE";
  285. textBox.SelectionLength = 4;
  286. Assert.AreEqual (1, _changed, "#1");
  287. textBox.Cut ();
  288. Assert.AreEqual (2, _changed, "#2");
  289. Assert.AreEqual ("E", textBox.Text, "#3");
  290. }
  291. [Test]
  292. [Category ("NotWorking")]
  293. public void PasteTest ()
  294. {
  295. textBox.Text = "ABCDE";
  296. textBox.SelectionLength = 4;
  297. Assert.AreEqual (1, _changed, "#1");
  298. textBox.Copy ();
  299. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  300. Assert.AreEqual (1, _changed, "#2");
  301. textBox.Paste ();
  302. Assert.AreEqual (2, _changed, "#3");
  303. Assert.AreEqual ("ABCDABCD", textBox.Text, "#4");
  304. }
  305. [Test] // bug #80301
  306. [Ignore ("Depends on specific DPI")]
  307. public void PreferredHeight ()
  308. {
  309. textBox.Font = new Font ("Arial", 14);
  310. Assert.AreEqual (29, textBox.PreferredHeight, "#A1");
  311. textBox.Font = new Font ("Arial", 16);
  312. Assert.AreEqual (32, textBox.PreferredHeight, "#A2");
  313. textBox.Font = new Font ("Arial", 17);
  314. Assert.AreEqual (34, textBox.PreferredHeight, "#A3");
  315. textBox.BorderStyle = BorderStyle.None;
  316. Assert.AreEqual (27, textBox.PreferredHeight, "#B1");
  317. textBox.Font = new Font ("Arial", 14);
  318. Assert.AreEqual (22, textBox.PreferredHeight, "#B2");
  319. textBox.Font = new Font ("Arial", 16);
  320. Assert.AreEqual (25, textBox.PreferredHeight, "#B3");
  321. }
  322. [Test]
  323. public void SelectTest ()
  324. {
  325. textBox.Text = "This is a sample test.";
  326. textBox.Select (0, 4);
  327. Assert.AreEqual ("This", textBox.SelectedText, "#33");
  328. }
  329. [Test]
  330. public void SelectAllTest ()
  331. {
  332. textBox.Text = "This is a sample test.";
  333. textBox.SelectAll ();
  334. Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
  335. }
  336. [Test]
  337. [Category ("NotWorking")]
  338. public void FocusSelectsAllTest ()
  339. {
  340. Form form = new Form ();
  341. form.ShowInTaskbar = false;
  342. TextBox textBoxA = new TextBox ();
  343. textBoxA.Text = "This is a sample testA.";
  344. textBoxA.TabIndex = 0;
  345. form.Controls.Add (textBoxA);
  346. TextBox textBoxB = new TextBox ();
  347. textBoxB.Text = "This is a sample testB.";
  348. textBoxB.TabIndex = 1;
  349. form.Controls.Add (textBoxB);
  350. #if NET_2_0
  351. Assert.AreEqual (String.Empty, textBoxA.SelectedText, "#A1 (2.0)");
  352. Assert.AreEqual (String.Empty, textBoxB.SelectedText, "#A2 (2.0)");
  353. #else
  354. Assert.IsNull (textBoxA.SelectedText, "#A1");
  355. Assert.IsNull (textBoxB.SelectedText, "#A2");
  356. #endif
  357. form.Show ();
  358. textBoxA.Focus ();
  359. Assert.AreEqual ("This is a sample testA.", textBoxA.SelectedText, "#B1");
  360. Assert.AreEqual (string.Empty, textBoxB.SelectedText, "#B2");
  361. textBoxB.Focus ();
  362. Assert.AreEqual ("This is a sample testA.", textBoxA.SelectedText, "#C1");
  363. Assert.AreEqual ("This is a sample testB.", textBoxB.SelectedText, "#C2");
  364. textBoxA.Text = "another testA.";
  365. textBoxB.Text = "another testB.";
  366. Assert.AreEqual (string.Empty, textBoxA.SelectedText, "#D1");
  367. Assert.AreEqual (string.Empty, textBoxB.SelectedText, "#D2");
  368. textBoxA.Focus ();
  369. Assert.AreEqual ("another testA.", textBoxA.SelectedText, "#E1");
  370. Assert.AreEqual (string.Empty, textBoxB.SelectedText, "#E2");
  371. textBoxB.Focus ();
  372. Assert.AreEqual ("another testA.", textBoxA.SelectedText, "#F1");
  373. Assert.AreEqual ("another testB.", textBoxB.SelectedText, "#F2");
  374. form.Dispose ();
  375. }
  376. [Test]
  377. [Category ("NotWorking")]
  378. public void ForeColorTest ()
  379. {
  380. Assert.AreEqual (SystemColors.WindowText, textBox.ForeColor, "#A1");
  381. textBox.ForeColor = Color.Red;
  382. Assert.AreEqual (Color.Red, textBox.ForeColor, "#A2");
  383. textBox.ForeColor = Color.White;
  384. Assert.AreEqual (Color.White, textBox.ForeColor, "#A3");
  385. Assert.AreEqual (0, _invalidated, "#A4");
  386. Assert.AreEqual (0, _paint, "#A5");
  387. Form form = new Form ();
  388. form.ShowInTaskbar = false;
  389. form.Controls.Add (textBox);
  390. form.Show ();
  391. Assert.AreEqual (Color.White, textBox.ForeColor, "#B1");
  392. Assert.AreEqual (0, _invalidated, "#B2");
  393. Assert.AreEqual (0, _paint, "#B3");
  394. textBox.ForeColor = Color.Red;
  395. Assert.AreEqual (Color.Red, textBox.ForeColor, "#B4");
  396. Assert.AreEqual (1, _invalidated, "#B5");
  397. Assert.AreEqual (0, _paint, "#B6");
  398. textBox.ForeColor = Color.Red;
  399. Assert.AreEqual (Color.Red, textBox.ForeColor, "#B7");
  400. Assert.AreEqual (1, _invalidated, "#B8");
  401. Assert.AreEqual (0, _paint, "#B9");
  402. textBox.ForeColor = Color.Blue;
  403. Assert.AreEqual (Color.Blue, textBox.ForeColor, "#B10");
  404. Assert.AreEqual (2, _invalidated, "#B11");
  405. Assert.AreEqual (0, _paint, "#B12");
  406. }
  407. [Test]
  408. public void ReadOnly_BackColor_NotSet ()
  409. {
  410. textBox.ReadOnly = true;
  411. Assert.IsTrue (textBox.ReadOnly, "#A1");
  412. #if NET_2_0
  413. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#A2");
  414. #else
  415. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#A2");
  416. #endif
  417. Form form = new Form ();
  418. form.ShowInTaskbar = false;
  419. form.Controls.Add (textBox);
  420. form.Show ();
  421. Assert.IsTrue (textBox.ReadOnly, "#B1");
  422. #if NET_2_0
  423. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#B2");
  424. #else
  425. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#B2");
  426. #endif
  427. textBox.ResetBackColor ();
  428. Assert.IsTrue (textBox.ReadOnly, "#C1");
  429. #if NET_2_0
  430. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#C2");
  431. #else
  432. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#C2");
  433. #endif
  434. textBox.ReadOnly = false;
  435. Assert.IsFalse (textBox.ReadOnly, "#D1");
  436. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#D2");
  437. textBox.ReadOnly = true;
  438. Assert.IsTrue (textBox.ReadOnly, "#E1");
  439. #if NET_2_0
  440. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#E2");
  441. #else
  442. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#E2");
  443. #endif
  444. textBox.BackColor = Color.Red;
  445. Assert.IsTrue (textBox.ReadOnly, "#F1");
  446. Assert.AreEqual (Color.Red, textBox.BackColor, "#F2");
  447. textBox.ReadOnly = false;
  448. Assert.IsFalse (textBox.ReadOnly, "#G1");
  449. Assert.AreEqual (Color.Red, textBox.BackColor, "#G2");
  450. textBox.ReadOnly = true;
  451. Assert.IsTrue (textBox.ReadOnly, "#H1");
  452. Assert.AreEqual (Color.Red, textBox.BackColor, "#H2");
  453. textBox.ResetBackColor ();
  454. Assert.IsTrue (textBox.ReadOnly, "#I1");
  455. #if NET_2_0
  456. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#I2");
  457. #else
  458. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#I2");
  459. #endif
  460. form.Close ();
  461. }
  462. [Test]
  463. public void ReadOnly_BackColor_Set ()
  464. {
  465. textBox.BackColor = Color.Blue;
  466. textBox.ReadOnly = true;
  467. Assert.IsTrue (textBox.ReadOnly, "#A1");
  468. Assert.AreEqual (Color.Blue, textBox.BackColor, "#A2");
  469. Form form = new Form ();
  470. form.ShowInTaskbar = false;
  471. form.Controls.Add (textBox);
  472. form.Show ();
  473. Assert.IsTrue (textBox.ReadOnly, "#B1");
  474. Assert.AreEqual (Color.Blue, textBox.BackColor, "#B2");
  475. textBox.ReadOnly = false;
  476. Assert.IsFalse (textBox.ReadOnly, "#C1");
  477. Assert.AreEqual (Color.Blue, textBox.BackColor, "#C2");
  478. textBox.ReadOnly = true;
  479. Assert.IsTrue (textBox.ReadOnly, "#D1");
  480. Assert.AreEqual (Color.Blue, textBox.BackColor, "#D2");
  481. textBox.BackColor = Color.Red;
  482. Assert.IsTrue (textBox.ReadOnly, "#E1");
  483. Assert.AreEqual (Color.Red, textBox.BackColor, "#E2");
  484. textBox.ReadOnly = false;
  485. Assert.IsFalse (textBox.ReadOnly, "#F1");
  486. Assert.AreEqual (Color.Red, textBox.BackColor, "#F2");
  487. textBox.ReadOnly = true;
  488. textBox.ResetBackColor ();
  489. Assert.IsTrue (textBox.ReadOnly, "#G1");
  490. #if NET_2_0
  491. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#G2");
  492. #else
  493. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#G2");
  494. #endif
  495. form.Dispose ();
  496. textBox = new TextBox ();
  497. textBox.ReadOnly = true;
  498. textBox.BackColor = Color.Blue;
  499. Assert.IsTrue (textBox.ReadOnly, "#H1");
  500. Assert.AreEqual (Color.Blue, textBox.BackColor, "#H2");
  501. form = new Form ();
  502. form.ShowInTaskbar = false;
  503. form.Controls.Add (textBox);
  504. form.Show ();
  505. Assert.IsTrue (textBox.ReadOnly, "#I1");
  506. Assert.AreEqual (Color.Blue, textBox.BackColor, "#I2");
  507. textBox.ReadOnly = false;
  508. Assert.IsFalse (textBox.ReadOnly, "#J1");
  509. Assert.AreEqual (Color.Blue, textBox.BackColor, "#J2");
  510. textBox.ResetBackColor ();
  511. Assert.IsFalse (textBox.ReadOnly, "#K1");
  512. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#K2");
  513. form.Close ();
  514. }
  515. [Test]
  516. public void ScrollBarsTest ()
  517. {
  518. Assert.AreEqual (ScrollBars.None, textBox.ScrollBars, "#1");
  519. textBox.ScrollBars = ScrollBars.Vertical;
  520. Assert.AreEqual (ScrollBars.Vertical, textBox.ScrollBars, "#2");
  521. }
  522. [Test]
  523. [ExpectedException (typeof (InvalidEnumArgumentException))]
  524. public void ScrollBars_Invalid ()
  525. {
  526. textBox.ScrollBars = (ScrollBars) 666;
  527. }
  528. [Test]
  529. public void ToStringTest ()
  530. {
  531. Assert.AreEqual ("System.Windows.Forms.TextBox, Text: ", textBox.ToString(), "#35");
  532. }
  533. [Test]
  534. public void UndoTest1 ()
  535. {
  536. textBox.Text = "ABCDE";
  537. textBox.SelectionLength = 4;
  538. textBox.Copy ();
  539. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  540. textBox.Paste ();
  541. Console.WriteLine ("pre paste text: {0}", textBox.Text);
  542. textBox.Undo ();
  543. Assert.AreEqual ("ABCDE", textBox.Text, "#36");
  544. }
  545. [Test] // bug #79851
  546. public void WrappedText ()
  547. {
  548. string text = "blabla blablabalbalbalbalbalbal blabla blablabl bal " +
  549. "bal bla bal balajkdhfk dskfk ersd dsfjksdhf sdkfjshd f";
  550. textBox.Multiline = true;
  551. textBox.Size = new Size (30, 168);
  552. textBox.Text = text;
  553. Form form = new Form ();
  554. form.Controls.Add (textBox);
  555. form.ShowInTaskbar = false;
  556. form.Show ();
  557. Assert.AreEqual (text, textBox.Text);
  558. form.Close ();
  559. }
  560. [Test] // bug #79909
  561. public void MultilineText ()
  562. {
  563. string text = "line1\n\nline2\nline3\r\nline4";
  564. textBox.Size = new Size (300, 168);
  565. textBox.Text = text;
  566. Form form = new Form ();
  567. form.Controls.Add (textBox);
  568. form.ShowInTaskbar = false;
  569. form.Show ();
  570. Assert.AreEqual (text, textBox.Text, "#1");
  571. text = "line1\n\nline2\nline3\r\nline4\rline5\r\n\nline6\n\n\nline7";
  572. textBox.Text = text;
  573. form.Visible = false;
  574. form.Show ();
  575. Assert.AreEqual (text, textBox.Text, "#2");
  576. form.Close ();
  577. }
  578. [Test] // bug 82371
  579. public void PropertySelectionLength ()
  580. {
  581. TextBox tb = new TextBox ();
  582. RichTextBox rtb = new RichTextBox ();
  583. #if NET_2_0
  584. Assert.AreEqual (0, tb.SelectionLength, "A1-NET20");
  585. Assert.AreEqual (0, rtb.SelectionLength, "A2-NET20");
  586. #else
  587. Assert.AreEqual (-1, tb.SelectionLength, "A1-NET11");
  588. Assert.AreEqual (-1, rtb.SelectionLength, "A2-NET11");
  589. #endif
  590. IntPtr i = tb.Handle;
  591. i = rtb.Handle;
  592. Assert.AreEqual (0, tb.SelectionLength, "A3");
  593. Assert.AreEqual (0, rtb.SelectionLength, "A4");
  594. }
  595. [Test]
  596. public void Bug82749 ()
  597. {
  598. Form f = new Form ();
  599. f.ShowInTaskbar = false;
  600. TextBox _textBox = new TextBox ();
  601. _textBox.Dock = DockStyle.Top;
  602. _textBox.Height = 100;
  603. _textBox.Multiline = true;
  604. f.Controls.Add (_textBox);
  605. f.Show ();
  606. Assert.AreEqual (100, _textBox.Height, "A1");
  607. _textBox.Multiline = false;
  608. Assert.AreEqual (20, _textBox.Height, "A2");
  609. _textBox.Multiline = true;
  610. Assert.AreEqual (100, _textBox.Height, "A3");
  611. f.Close ();
  612. f.Dispose ();
  613. }
  614. [Test]
  615. public void ModifiedEventTest ()
  616. {
  617. TextBox tb = new TextBox ();
  618. string events = string.Empty;
  619. tb.ModifiedChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("ModifiedChanged"); });
  620. tb.Modified = true;
  621. Assert.AreEqual ("ModifiedChanged", events, "me-1");
  622. }
  623. [Test]
  624. public void BorderStyleEventTest ()
  625. {
  626. TextBox tb = new TextBox ();
  627. string events = string.Empty;
  628. tb.BorderStyleChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("BorderStyleChanged"); });
  629. tb.BorderStyle = BorderStyle.None;
  630. Assert.AreEqual ("BorderStyleChanged", events, "bse-1");
  631. }
  632. [Test]
  633. public void ModifiedTest ()
  634. {
  635. Assert.AreEqual (true, textBox.Modified, "modified-1");
  636. textBox.Modified = false;
  637. Assert.AreEqual (false, textBox.Modified, "modified-2");
  638. textBox.Modified = true;
  639. Assert.AreEqual (true, textBox.Modified, "modified-2");
  640. textBox.Text = "TEXT";
  641. Assert.AreEqual (false, textBox.Modified, "modified-3");
  642. }
  643. void TextBox_TextChanged (object sender, EventArgs e)
  644. {
  645. _changed++;
  646. }
  647. void TextBox_Invalidated (object sender, InvalidateEventArgs e)
  648. {
  649. _invalidated++;
  650. }
  651. void TextBox_Paint (object sender, PaintEventArgs e)
  652. {
  653. _paint++;
  654. }
  655. void Reset ()
  656. {
  657. _changed = 0;
  658. _invalidated = 0;
  659. _paint = 0;
  660. }
  661. [Test]
  662. public void MethodIsInputChar ()
  663. {
  664. // Basically, show that this method always returns true
  665. InputCharControl m = new InputCharControl ();
  666. bool result = true;
  667. for (int i = 0; i < 256; i++)
  668. result &= m.PublicIsInputChar ((char)i);
  669. Assert.AreEqual (true, result, "I1");
  670. }
  671. private class InputCharControl : TextBox
  672. {
  673. public bool PublicIsInputChar (char charCode)
  674. {
  675. return base.IsInputChar (charCode);
  676. }
  677. }
  678. }
  679. }