TextBoxTest.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 : TestHelper
  18. {
  19. TextBox textBox;
  20. int _changed;
  21. int _invalidated;
  22. int _paint;
  23. [SetUp]
  24. protected override void SetUp () {
  25. textBox = new TextBox();
  26. textBox.Invalidated += new InvalidateEventHandler (TextBox_Invalidated);
  27. textBox.Paint += new PaintEventHandler (TextBox_Paint);
  28. textBox.TextChanged += new EventHandler (TextBox_TextChanged);
  29. Reset ();
  30. base.SetUp ();
  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. public void PasteTest ()
  293. {
  294. textBox.Text = "ABCDE";
  295. textBox.SelectionLength = 4;
  296. Assert.AreEqual (1, _changed, "#1");
  297. textBox.Copy ();
  298. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  299. Assert.AreEqual (1, _changed, "#2");
  300. textBox.Paste ();
  301. Assert.AreEqual (2, _changed, "#3");
  302. Assert.AreEqual ("ABCDABCD", textBox.Text, "#4");
  303. }
  304. [Test] // bug #80301
  305. [Ignore ("Depends on specific DPI")]
  306. public void PreferredHeight ()
  307. {
  308. textBox.Font = new Font ("Arial", 14);
  309. Assert.AreEqual (29, textBox.PreferredHeight, "#A1");
  310. textBox.Font = new Font ("Arial", 16);
  311. Assert.AreEqual (32, textBox.PreferredHeight, "#A2");
  312. textBox.Font = new Font ("Arial", 17);
  313. Assert.AreEqual (34, textBox.PreferredHeight, "#A3");
  314. textBox.BorderStyle = BorderStyle.None;
  315. Assert.AreEqual (27, textBox.PreferredHeight, "#B1");
  316. textBox.Font = new Font ("Arial", 14);
  317. Assert.AreEqual (22, textBox.PreferredHeight, "#B2");
  318. textBox.Font = new Font ("Arial", 16);
  319. Assert.AreEqual (25, textBox.PreferredHeight, "#B3");
  320. }
  321. [Test]
  322. public void SelectTest ()
  323. {
  324. textBox.Text = "This is a sample test.";
  325. textBox.Select (0, 4);
  326. Assert.AreEqual ("This", textBox.SelectedText, "#33");
  327. }
  328. [Test]
  329. public void SelectAllTest ()
  330. {
  331. textBox.Text = "This is a sample test.";
  332. textBox.SelectAll ();
  333. Assert.AreEqual ("This is a sample test.", textBox.SelectedText, "#34");
  334. }
  335. [Test]
  336. public void FocusSelectsAllTest ()
  337. {
  338. Form form = new Form ();
  339. form.ShowInTaskbar = false;
  340. TextBox textBoxA = new TextBox ();
  341. textBoxA.Text = "This is a sample testA.";
  342. textBoxA.TabIndex = 0;
  343. form.Controls.Add (textBoxA);
  344. TextBox textBoxB = new TextBox ();
  345. textBoxB.Text = "This is a sample testB.";
  346. textBoxB.TabIndex = 1;
  347. form.Controls.Add (textBoxB);
  348. #if NET_2_0
  349. Assert.AreEqual (String.Empty, textBoxA.SelectedText, "#A1 (2.0)");
  350. Assert.AreEqual (String.Empty, textBoxB.SelectedText, "#A2 (2.0)");
  351. #else
  352. Assert.IsNull (textBoxA.SelectedText, "#A1");
  353. Assert.IsNull (textBoxB.SelectedText, "#A2");
  354. #endif
  355. form.Show ();
  356. textBoxA.Focus ();
  357. Assert.AreEqual ("This is a sample testA.", textBoxA.SelectedText, "#B1");
  358. Assert.AreEqual (string.Empty, textBoxB.SelectedText, "#B2");
  359. textBoxB.Focus ();
  360. Assert.AreEqual ("This is a sample testA.", textBoxA.SelectedText, "#C1");
  361. Assert.AreEqual ("This is a sample testB.", textBoxB.SelectedText, "#C2");
  362. textBoxA.Text = "another testA.";
  363. textBoxB.Text = "another testB.";
  364. Assert.AreEqual (string.Empty, textBoxA.SelectedText, "#D1");
  365. Assert.AreEqual (string.Empty, textBoxB.SelectedText, "#D2");
  366. textBoxA.Focus ();
  367. Assert.AreEqual ("another testA.", textBoxA.SelectedText, "#E1");
  368. Assert.AreEqual (string.Empty, textBoxB.SelectedText, "#E2");
  369. textBoxB.Focus ();
  370. Assert.AreEqual ("another testA.", textBoxA.SelectedText, "#F1");
  371. Assert.AreEqual ("another testB.", textBoxB.SelectedText, "#F2");
  372. form.Dispose ();
  373. }
  374. [Test]
  375. [Category ("NotWorking")]
  376. public void ForeColorTest ()
  377. {
  378. Assert.AreEqual (SystemColors.WindowText, textBox.ForeColor, "#A1");
  379. textBox.ForeColor = Color.Red;
  380. Assert.AreEqual (Color.Red, textBox.ForeColor, "#A2");
  381. textBox.ForeColor = Color.White;
  382. Assert.AreEqual (Color.White, textBox.ForeColor, "#A3");
  383. Assert.AreEqual (0, _invalidated, "#A4");
  384. Assert.AreEqual (0, _paint, "#A5");
  385. Form form = new Form ();
  386. form.ShowInTaskbar = false;
  387. form.Controls.Add (textBox);
  388. form.Show ();
  389. Assert.AreEqual (Color.White, textBox.ForeColor, "#B1");
  390. Assert.AreEqual (0, _invalidated, "#B2");
  391. Assert.AreEqual (0, _paint, "#B3");
  392. textBox.ForeColor = Color.Red;
  393. Assert.AreEqual (Color.Red, textBox.ForeColor, "#B4");
  394. Assert.AreEqual (1, _invalidated, "#B5");
  395. Assert.AreEqual (0, _paint, "#B6");
  396. textBox.ForeColor = Color.Red;
  397. Assert.AreEqual (Color.Red, textBox.ForeColor, "#B7");
  398. Assert.AreEqual (1, _invalidated, "#B8");
  399. Assert.AreEqual (0, _paint, "#B9");
  400. textBox.ForeColor = Color.Blue;
  401. Assert.AreEqual (Color.Blue, textBox.ForeColor, "#B10");
  402. Assert.AreEqual (2, _invalidated, "#B11");
  403. Assert.AreEqual (0, _paint, "#B12");
  404. }
  405. [Test]
  406. public void ReadOnly_BackColor_NotSet ()
  407. {
  408. textBox.ReadOnly = true;
  409. Assert.IsTrue (textBox.ReadOnly, "#A1");
  410. #if NET_2_0
  411. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#A2");
  412. #else
  413. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#A2");
  414. #endif
  415. Form form = new Form ();
  416. form.ShowInTaskbar = false;
  417. form.Controls.Add (textBox);
  418. form.Show ();
  419. Assert.IsTrue (textBox.ReadOnly, "#B1");
  420. #if NET_2_0
  421. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#B2");
  422. #else
  423. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#B2");
  424. #endif
  425. textBox.ResetBackColor ();
  426. Assert.IsTrue (textBox.ReadOnly, "#C1");
  427. #if NET_2_0
  428. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#C2");
  429. #else
  430. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#C2");
  431. #endif
  432. textBox.ReadOnly = false;
  433. Assert.IsFalse (textBox.ReadOnly, "#D1");
  434. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#D2");
  435. textBox.ReadOnly = true;
  436. Assert.IsTrue (textBox.ReadOnly, "#E1");
  437. #if NET_2_0
  438. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#E2");
  439. #else
  440. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#E2");
  441. #endif
  442. textBox.BackColor = Color.Red;
  443. Assert.IsTrue (textBox.ReadOnly, "#F1");
  444. Assert.AreEqual (Color.Red, textBox.BackColor, "#F2");
  445. textBox.ReadOnly = false;
  446. Assert.IsFalse (textBox.ReadOnly, "#G1");
  447. Assert.AreEqual (Color.Red, textBox.BackColor, "#G2");
  448. textBox.ReadOnly = true;
  449. Assert.IsTrue (textBox.ReadOnly, "#H1");
  450. Assert.AreEqual (Color.Red, textBox.BackColor, "#H2");
  451. textBox.ResetBackColor ();
  452. Assert.IsTrue (textBox.ReadOnly, "#I1");
  453. #if NET_2_0
  454. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#I2");
  455. #else
  456. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#I2");
  457. #endif
  458. form.Close ();
  459. }
  460. [Test]
  461. public void ReadOnly_BackColor_Set ()
  462. {
  463. textBox.BackColor = Color.Blue;
  464. textBox.ReadOnly = true;
  465. Assert.IsTrue (textBox.ReadOnly, "#A1");
  466. Assert.AreEqual (Color.Blue, textBox.BackColor, "#A2");
  467. Form form = new Form ();
  468. form.ShowInTaskbar = false;
  469. form.Controls.Add (textBox);
  470. form.Show ();
  471. Assert.IsTrue (textBox.ReadOnly, "#B1");
  472. Assert.AreEqual (Color.Blue, textBox.BackColor, "#B2");
  473. textBox.ReadOnly = false;
  474. Assert.IsFalse (textBox.ReadOnly, "#C1");
  475. Assert.AreEqual (Color.Blue, textBox.BackColor, "#C2");
  476. textBox.ReadOnly = true;
  477. Assert.IsTrue (textBox.ReadOnly, "#D1");
  478. Assert.AreEqual (Color.Blue, textBox.BackColor, "#D2");
  479. textBox.BackColor = Color.Red;
  480. Assert.IsTrue (textBox.ReadOnly, "#E1");
  481. Assert.AreEqual (Color.Red, textBox.BackColor, "#E2");
  482. textBox.ReadOnly = false;
  483. Assert.IsFalse (textBox.ReadOnly, "#F1");
  484. Assert.AreEqual (Color.Red, textBox.BackColor, "#F2");
  485. textBox.ReadOnly = true;
  486. textBox.ResetBackColor ();
  487. Assert.IsTrue (textBox.ReadOnly, "#G1");
  488. #if NET_2_0
  489. Assert.AreEqual (SystemColors.Control, textBox.BackColor, "#G2");
  490. #else
  491. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#G2");
  492. #endif
  493. form.Dispose ();
  494. textBox = new TextBox ();
  495. textBox.ReadOnly = true;
  496. textBox.BackColor = Color.Blue;
  497. Assert.IsTrue (textBox.ReadOnly, "#H1");
  498. Assert.AreEqual (Color.Blue, textBox.BackColor, "#H2");
  499. form = new Form ();
  500. form.ShowInTaskbar = false;
  501. form.Controls.Add (textBox);
  502. form.Show ();
  503. Assert.IsTrue (textBox.ReadOnly, "#I1");
  504. Assert.AreEqual (Color.Blue, textBox.BackColor, "#I2");
  505. textBox.ReadOnly = false;
  506. Assert.IsFalse (textBox.ReadOnly, "#J1");
  507. Assert.AreEqual (Color.Blue, textBox.BackColor, "#J2");
  508. textBox.ResetBackColor ();
  509. Assert.IsFalse (textBox.ReadOnly, "#K1");
  510. Assert.AreEqual (SystemColors.Window, textBox.BackColor, "#K2");
  511. form.Close ();
  512. }
  513. [Test]
  514. public void ScrollBarsTest ()
  515. {
  516. Assert.AreEqual (ScrollBars.None, textBox.ScrollBars, "#1");
  517. textBox.ScrollBars = ScrollBars.Vertical;
  518. Assert.AreEqual (ScrollBars.Vertical, textBox.ScrollBars, "#2");
  519. }
  520. [Test]
  521. [ExpectedException (typeof (InvalidEnumArgumentException))]
  522. public void ScrollBars_Invalid ()
  523. {
  524. textBox.ScrollBars = (ScrollBars) 666;
  525. }
  526. [Test]
  527. public void ToStringTest ()
  528. {
  529. Assert.AreEqual ("System.Windows.Forms.TextBox, Text: ", textBox.ToString(), "#35");
  530. }
  531. [Test]
  532. public void UndoTest1 ()
  533. {
  534. textBox.Text = "ABCDE";
  535. textBox.SelectionLength = 4;
  536. textBox.Copy ();
  537. textBox.SelectionStart = textBox.SelectionStart + textBox.SelectionLength;
  538. textBox.Paste ();
  539. textBox.Undo ();
  540. Assert.AreEqual ("ABCDE", textBox.Text, "#36");
  541. }
  542. [Test] // bug #79851
  543. public void WrappedText ()
  544. {
  545. string text = "blabla blablabalbalbalbalbalbal blabla blablabl bal " +
  546. "bal bla bal balajkdhfk dskfk ersd dsfjksdhf sdkfjshd f";
  547. textBox.Multiline = true;
  548. textBox.Size = new Size (30, 168);
  549. textBox.Text = text;
  550. Form form = new Form ();
  551. form.Controls.Add (textBox);
  552. form.ShowInTaskbar = false;
  553. form.Show ();
  554. Assert.AreEqual (text, textBox.Text);
  555. form.Close ();
  556. }
  557. [Test] // bug #79909
  558. public void MultilineText ()
  559. {
  560. string text = "line1\n\nline2\nline3\r\nline4";
  561. textBox.Size = new Size (300, 168);
  562. textBox.Text = text;
  563. Form form = new Form ();
  564. form.Controls.Add (textBox);
  565. form.ShowInTaskbar = false;
  566. form.Show ();
  567. Assert.AreEqual (text, textBox.Text, "#1");
  568. text = "line1\n\nline2\nline3\r\nline4\rline5\r\n\nline6\n\n\nline7";
  569. textBox.Text = text;
  570. form.Visible = false;
  571. form.Show ();
  572. Assert.AreEqual (text, textBox.Text, "#2");
  573. form.Close ();
  574. }
  575. [Test] // bug #82371
  576. public void SelectionLength ()
  577. {
  578. TextBox tb = new TextBox ();
  579. RichTextBox rtb = new RichTextBox ();
  580. #if NET_2_0
  581. Assert.AreEqual (0, tb.SelectionLength, "#1");
  582. Assert.AreEqual (0, rtb.SelectionLength, "#2");
  583. #else
  584. Assert.AreEqual (-1, tb.SelectionLength, "#1");
  585. Assert.AreEqual (-1, rtb.SelectionLength, "#2");
  586. #endif
  587. IntPtr i = tb.Handle;
  588. i = rtb.Handle;
  589. Assert.AreEqual (0, tb.SelectionLength, "A3");
  590. Assert.AreEqual (0, rtb.SelectionLength, "A4");
  591. }
  592. [Test]
  593. public void SelectionLength_Negative ()
  594. {
  595. TextBox tb = new TextBox ();
  596. try {
  597. tb.SelectionLength = -1;
  598. Assert.Fail ("#1");
  599. #if NET_2_0
  600. } catch (ArgumentOutOfRangeException ex) {
  601. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  602. Assert.IsNull (ex.InnerException, "#3");
  603. Assert.IsNotNull (ex.Message, "#4");
  604. Assert.IsNotNull (ex.ParamName, "#5");
  605. Assert.AreEqual ("SelectionLength", ex.ParamName, "#6");
  606. }
  607. #else
  608. } catch (ArgumentException ex) {
  609. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  610. Assert.IsNull (ex.InnerException, "#3");
  611. Assert.IsNotNull (ex.Message, "#4");
  612. Assert.IsNull (ex.ParamName, "#5");
  613. }
  614. #endif
  615. }
  616. [Test]
  617. public void SelectionStart_Negative ()
  618. {
  619. TextBox tb = new TextBox ();
  620. try {
  621. tb.SelectionStart = -1;
  622. Assert.Fail ("#1");
  623. #if NET_2_0
  624. } catch (ArgumentOutOfRangeException ex) {
  625. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  626. Assert.IsNull (ex.InnerException, "#3");
  627. Assert.IsNotNull (ex.Message, "#4");
  628. Assert.IsNotNull (ex.ParamName, "#5");
  629. Assert.AreEqual ("SelectionStart", ex.ParamName, "#6");
  630. }
  631. #else
  632. } catch (ArgumentException ex) {
  633. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  634. Assert.IsNull (ex.InnerException, "#3");
  635. Assert.IsNotNull (ex.Message, "#4");
  636. Assert.IsNull (ex.ParamName, "#5");
  637. }
  638. #endif
  639. }
  640. [Test]
  641. public void Bug82749 ()
  642. {
  643. Form f = new Form ();
  644. f.ShowInTaskbar = false;
  645. TextBox _textBox = new TextBox ();
  646. _textBox.Dock = DockStyle.Top;
  647. _textBox.Height = 100;
  648. _textBox.Multiline = true;
  649. f.Controls.Add (_textBox);
  650. f.Show ();
  651. Assert.AreEqual (100, _textBox.Height, "A1");
  652. // Font dependent, but should be less than 30.
  653. _textBox.Multiline = false;
  654. Assert.IsTrue (_textBox.Height < 30, "A2");
  655. _textBox.Multiline = true;
  656. Assert.AreEqual (100, _textBox.Height, "A3");
  657. f.Close ();
  658. f.Dispose ();
  659. }
  660. [Test]
  661. public void ModifiedEventTest ()
  662. {
  663. TextBox tb = new TextBox ();
  664. EventLogger eventLogger = new EventLogger (tb);
  665. tb.Modified = true;
  666. Assert.AreEqual (1, eventLogger.EventsRaised);
  667. Assert.IsTrue (eventLogger.EventRaised ("ModifiedChanged"));
  668. }
  669. [Test]
  670. public void BorderStyleEventTest ()
  671. {
  672. TextBox tb = new TextBox ();
  673. EventLogger eventLogger = new EventLogger (tb);
  674. tb.BorderStyle = BorderStyle.None;
  675. Assert.IsTrue (eventLogger.EventRaised ("BorderStyleChanged"));
  676. }
  677. [Test]
  678. public void FixedHeightControlStyle ()
  679. {
  680. TextBoxPublic t = new TextBoxPublic ();
  681. t.Multiline = true;
  682. Assert.IsFalse (t.GetStylePublic (ControlStyles.FixedHeight), "A1");
  683. t.Multiline = false;
  684. Assert.IsTrue (t.GetStylePublic (ControlStyles.FixedHeight), "A2");
  685. }
  686. [Test]
  687. public void ModifiedTest ()
  688. {
  689. TextBox t = new TextBox ();
  690. Assert.AreEqual (false, t.Modified, "modified-1");
  691. t.Modified = true;
  692. Assert.AreEqual (true, t.Modified, "modified-2");
  693. t.Modified = false;
  694. Assert.AreEqual (false, t.Modified, "modified-3");
  695. t.Text = "TEXT";
  696. Assert.AreEqual (false, t.Modified, "modified-4");
  697. }
  698. void TextBox_TextChanged (object sender, EventArgs e)
  699. {
  700. _changed++;
  701. }
  702. void TextBox_Invalidated (object sender, InvalidateEventArgs e)
  703. {
  704. _invalidated++;
  705. }
  706. void TextBox_Paint (object sender, PaintEventArgs e)
  707. {
  708. _paint++;
  709. }
  710. void Reset ()
  711. {
  712. _changed = 0;
  713. _invalidated = 0;
  714. _paint = 0;
  715. }
  716. [Test]
  717. public void MethodIsInputChar ()
  718. {
  719. // Basically, show that this method always returns true
  720. InputCharControl m = new InputCharControl ();
  721. bool result = true;
  722. for (int i = 0; i < 256; i++)
  723. result &= m.PublicIsInputChar ((char)i);
  724. Assert.AreEqual (true, result, "I1");
  725. }
  726. private class InputCharControl : TextBox
  727. {
  728. public bool PublicIsInputChar (char charCode)
  729. {
  730. return base.IsInputChar (charCode);
  731. }
  732. }
  733. private class TextBoxPublic : TextBox
  734. {
  735. public bool GetStylePublic (ControlStyles flag) {
  736. return GetStyle (flag);
  737. }
  738. }
  739. }
  740. #if NET_2_0
  741. [TestFixture]
  742. public class TextBoxAutoCompleteSourceConverterTest : TestHelper
  743. {
  744. [Test]
  745. public void One()
  746. {
  747. PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(TextBox))["AutoCompleteSource"];
  748. TypeConverter converter = pd.Converter;
  749. Assert.AreEqual("System.Windows.Forms.TextBoxAutoCompleteSourceConverter",
  750. converter.GetType().FullName, "setup--converter type");
  751. Assert.IsTrue(converter.GetStandardValuesSupported(), "GetStandardValuesSupported");
  752. Assert.IsTrue(converter.GetStandardValuesExclusive(), "GetStandardValuesExclusive");
  753. //
  754. global::System.Collections.ICollection list = converter.GetStandardValues();
  755. Assert.AreEqual(8, list.Count, "count");
  756. Object[] arr = new Object[list.Count];
  757. list.CopyTo(arr, 0);
  758. Assert.AreEqual(AutoCompleteSource.FileSystem, arr[0], "item0");
  759. Assert.AreEqual(AutoCompleteSource.HistoryList, arr[1], "item1");
  760. Assert.AreEqual(AutoCompleteSource.RecentlyUsedList, arr[2], "item2");
  761. Assert.AreEqual(AutoCompleteSource.AllUrl, arr[3], "item3");
  762. Assert.AreEqual(AutoCompleteSource.AllSystemSources, arr[4], "item4");
  763. Assert.AreEqual(AutoCompleteSource.FileSystemDirectories, arr[5], "item5");
  764. Assert.AreEqual(AutoCompleteSource.CustomSource, arr[6], "item6");
  765. Assert.AreEqual(AutoCompleteSource.None, arr[7], "item7");
  766. // And NOT AutoCompleteSource.ListItems.
  767. }
  768. }
  769. #endif
  770. }