ListBoxTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. //
  2. // ComboBoxTest.cs: Test cases for ComboBox.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  24. //
  25. // Authors:
  26. // Ritvik Mayank <[email protected]>
  27. // Jordi Mas i Hernandez <[email protected]>
  28. //
  29. using System;
  30. using System.Windows.Forms;
  31. using System.Drawing;
  32. using System.Reflection;
  33. using NUnit.Framework;
  34. using System.Collections;
  35. using System.ComponentModel;
  36. namespace MonoTests.System.Windows.Forms
  37. {
  38. [TestFixture]
  39. public class ListBoxTest
  40. {
  41. ListBox listBox;
  42. Form form;
  43. [SetUp]
  44. public void SetUp()
  45. {
  46. listBox = new ListBox();
  47. form = new Form();
  48. form.ShowInTaskbar = false;
  49. }
  50. [TearDown]
  51. public void TearDown()
  52. {
  53. form.Dispose ();
  54. }
  55. [Test]
  56. public void ListBoxPropertyTest ()
  57. {
  58. Assert.AreEqual (0, listBox.ColumnWidth, "#1");
  59. Assert.AreEqual (DrawMode.Normal, listBox.DrawMode, "#2");
  60. Assert.AreEqual (0, listBox.HorizontalExtent, "#3");
  61. Assert.AreEqual (false, listBox.HorizontalScrollbar, "#4");
  62. Assert.AreEqual (true, listBox.IntegralHeight, "#5");
  63. //Assert.AreEqual (13, listBox.ItemHeight, "#6"); // Note: Item height depends on the current font.
  64. listBox.Items.Add ("a");
  65. listBox.Items.Add ("b");
  66. listBox.Items.Add ("c");
  67. Assert.AreEqual (3, listBox.Items.Count, "#7");
  68. Assert.AreEqual (false, listBox.MultiColumn, "#8");
  69. //Assert.AreEqual (46, listBox.PreferredHeight, "#9"); // Note: Item height depends on the current font.
  70. //Assert.AreEqual (RightToLeft.No , listBox.RightToLeft, "#10"); // Depends on Windows version
  71. Assert.AreEqual (false, listBox.ScrollAlwaysVisible, "#11");
  72. Assert.AreEqual (-1, listBox.SelectedIndex, "#12");
  73. listBox.SetSelected (2,true);
  74. Assert.AreEqual (2, listBox.SelectedIndices[0], "#13");
  75. Assert.AreEqual ("c", listBox.SelectedItem, "#14");
  76. Assert.AreEqual ("c", listBox.SelectedItems[0], "#15");
  77. Assert.AreEqual (SelectionMode.One, listBox.SelectionMode, "#16");
  78. listBox.SetSelected (2,false);
  79. Assert.AreEqual (false, listBox.Sorted, "#17");
  80. Assert.AreEqual ("", listBox.Text, "#18");
  81. Assert.AreEqual (0, listBox.TopIndex, "#19");
  82. Assert.AreEqual (true, listBox.UseTabStops, "#20");
  83. }
  84. [Test]
  85. public void BeginEndUpdateTest ()
  86. {
  87. form.Visible = true;
  88. listBox.Items.Add ("A");
  89. listBox.Visible = true;
  90. form.Controls.Add (listBox);
  91. listBox.BeginUpdate ();
  92. for (int x = 1; x < 5000; x++)
  93. {
  94. listBox.Items.Add ("Item " + x.ToString ());
  95. }
  96. listBox.EndUpdate ();
  97. listBox.SetSelected (1, true);
  98. listBox.SetSelected (3, true);
  99. Assert.AreEqual (true, listBox.SelectedItems.Contains ("Item 3"), "#21");
  100. }
  101. [Test]
  102. public void ClearSelectedTest ()
  103. {
  104. form.Visible = true;
  105. listBox.Items.Add ("A");
  106. listBox.Visible = true;
  107. form.Controls.Add (listBox);
  108. listBox.SetSelected (0, true);
  109. Assert.AreEqual ("A", listBox.SelectedItems [0].ToString (),"#22");
  110. listBox.ClearSelected ();
  111. Assert.AreEqual (0, listBox.SelectedItems.Count,"#23");
  112. }
  113. [Test] // bug #80620
  114. public void ClientRectangle_Borders ()
  115. {
  116. listBox.CreateControl ();
  117. Assert.AreEqual (listBox.ClientRectangle, new ListBox ().ClientRectangle);
  118. }
  119. [Ignore ("It depends on user system settings")]
  120. public void GetItemHeightTest ()
  121. {
  122. listBox.Visible = true;
  123. form.Controls.Add (listBox);
  124. listBox.Items.Add ("A");
  125. Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
  126. }
  127. [Ignore ("It depends on user system settings")]
  128. public void GetItemRectangleTest ()
  129. {
  130. form.Visible = true;
  131. listBox.Visible = true;
  132. form.Controls.Add (listBox);
  133. listBox.Items.Add ("A");
  134. Assert.AreEqual (new Rectangle(0,0,116,13), listBox.GetItemRectangle (0), "#29");
  135. }
  136. [Test]
  137. public void GetSelectedTest ()
  138. {
  139. listBox.Items.Add ("A");
  140. listBox.Items.Add ("B");
  141. listBox.Items.Add ("C");
  142. listBox.Items.Add ("D");
  143. listBox.Sorted = true;
  144. listBox.SetSelected (0,true);
  145. listBox.SetSelected (2,true);
  146. listBox.TopIndex=0;
  147. Assert.AreEqual (true, listBox.GetSelected (0), "#30");
  148. listBox.SetSelected (2,false);
  149. Assert.AreEqual (false, listBox.GetSelected (2), "#31");
  150. }
  151. [Test]
  152. public void IndexFromPointTest ()
  153. {
  154. listBox.Items.Add ("A");
  155. Point pt = new Point (100,100);
  156. listBox.IndexFromPoint (pt);
  157. Assert.AreEqual (-1, listBox.IndexFromPoint (100,100), "#32");
  158. }
  159. [Test]
  160. public void FindStringTest ()
  161. {
  162. listBox.FindString ("Hola", -5); // No exception, it's empty
  163. int x = listBox.FindString ("Hello");
  164. Assert.AreEqual (-1, x, "#19");
  165. listBox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  166. String myString = "ABC";
  167. x = listBox.FindString (myString);
  168. Assert.AreEqual (3, x, "#191");
  169. x = listBox.FindString (string.Empty);
  170. Assert.AreEqual (0, x, "#192");
  171. x = listBox.FindString ("NonExistant");
  172. Assert.AreEqual (-1, x, "#193");
  173. }
  174. [Test]
  175. public void FindStringExactTest ()
  176. {
  177. listBox.FindStringExact ("Hola", -5); // No exception, it's empty
  178. int x = listBox.FindStringExact ("Hello");
  179. Assert.AreEqual (-1, x, "#20");
  180. listBox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
  181. String myString = "ABC";
  182. x = listBox.FindStringExact (myString);
  183. Assert.AreEqual (1, x, "#201");
  184. x = listBox.FindStringExact (string.Empty);
  185. Assert.AreEqual (-1, x, "#202");
  186. x = listBox.FindStringExact ("NonExistant");
  187. Assert.AreEqual (-1, x, "#203");
  188. }
  189. #if NET_2_0
  190. [Test]
  191. public void AllowSelection ()
  192. {
  193. MockListBox lb = new MockListBox ();
  194. lb.SelectionMode = SelectionMode.None;
  195. Assert.IsFalse (lb.allow_selection, "#1");
  196. lb.SelectionMode = SelectionMode.One;
  197. Assert.IsTrue (lb.allow_selection, "#2");
  198. }
  199. #endif
  200. //
  201. // Exceptions
  202. //
  203. [Test]
  204. [ExpectedException (typeof (InvalidEnumArgumentException))]
  205. public void BorderStyleException ()
  206. {
  207. listBox.BorderStyle = (BorderStyle) 10;
  208. }
  209. [Test]
  210. [ExpectedException (typeof (ArgumentException))]
  211. public void ColumnWidthException ()
  212. {
  213. listBox.ColumnWidth = -1;
  214. }
  215. [Test]
  216. [ExpectedException (typeof (InvalidEnumArgumentException))]
  217. public void DrawModeException ()
  218. {
  219. listBox.DrawMode = (DrawMode) 10;
  220. }
  221. [Test]
  222. [ExpectedException (typeof (ArgumentException))]
  223. public void DrawModeAndMultiColumnException ()
  224. {
  225. listBox.MultiColumn = true;
  226. listBox.DrawMode = DrawMode.OwnerDrawVariable;
  227. }
  228. [Test]
  229. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  230. public void ItemHeightException ()
  231. {
  232. listBox.ItemHeight = 256;
  233. }
  234. [Test] // bug #80696
  235. public void SelectedIndex_Created ()
  236. {
  237. Form form = new Form ();
  238. ListBox listBox = new ListBox ();
  239. listBox.Items.Add ("A");
  240. listBox.Items.Add ("B");
  241. form.Controls.Add (listBox);
  242. form.Show ();
  243. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  244. listBox.SelectedIndex = 0;
  245. Assert.AreEqual (0, listBox.SelectedIndex, "#2");
  246. listBox.SelectedIndex = -1;
  247. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  248. listBox.SelectedIndex = 1;
  249. Assert.AreEqual (1, listBox.SelectedIndex, "#4");
  250. form.Close ();
  251. }
  252. [Test] // bug #80753
  253. public void SelectedIndex_NotCreated ()
  254. {
  255. ListBox listBox = new ListBox ();
  256. listBox.Items.Add ("A");
  257. listBox.Items.Add ("B");
  258. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  259. listBox.SelectedIndex = 0;
  260. Assert.AreEqual (0, listBox.SelectedIndex, "#2");
  261. listBox.SelectedIndex = -1;
  262. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  263. listBox.SelectedIndex = 1;
  264. Assert.AreEqual (1, listBox.SelectedIndex, "#4");
  265. }
  266. [Test]
  267. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  268. public void SelectedIndexException ()
  269. {
  270. listBox.SelectedIndex = -2;
  271. }
  272. [Test]
  273. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  274. public void SelectedIndexException2 ()
  275. {
  276. listBox.SelectedIndex = listBox.Items.Count;
  277. }
  278. [Test]
  279. [ExpectedException (typeof (ArgumentException))]
  280. public void SelectedIndexModeNoneException ()
  281. {
  282. listBox.SelectionMode = SelectionMode.None;
  283. listBox.SelectedIndex = -1;
  284. }
  285. [Test]
  286. [ExpectedException (typeof (InvalidEnumArgumentException))]
  287. public void SelectionModeException ()
  288. {
  289. listBox.SelectionMode = (SelectionMode) 10;
  290. }
  291. [Test]
  292. public void SelectedValueNull()
  293. {
  294. listBox.Items.Clear ();
  295. listBox.Items.Add ("A");
  296. listBox.Items.Add ("B");
  297. listBox.Items.Add ("C");
  298. listBox.Items.Add ("D");
  299. listBox.SelectedIndex = 2;
  300. listBox.SelectedValue = null;
  301. Assert.AreEqual (listBox.SelectedIndex, 2);
  302. }
  303. [Test]
  304. public void SelectedValueEmptyString()
  305. {
  306. listBox.Items.Clear ();
  307. listBox.Items.Add ("A");
  308. listBox.Items.Add ("B");
  309. listBox.Items.Add ("C");
  310. listBox.Items.Add ("D");
  311. listBox.SelectedIndex = 2;
  312. listBox.SelectedValue = null;
  313. Assert.AreEqual (listBox.SelectedIndex, 2);
  314. }
  315. //
  316. // Events
  317. //
  318. //private bool eventFired;
  319. //private void GenericHandler (object sender, EventArgs e)
  320. //{
  321. // eventFired = true;
  322. //}
  323. public class MockListBox : ListBox
  324. {
  325. #if NET_2_0
  326. public bool allow_selection {
  327. get { return base.AllowSelection; }
  328. }
  329. #endif
  330. }
  331. }
  332. [TestFixture]
  333. public class ListBoxObjectCollectionTest
  334. {
  335. ListBox.ObjectCollection col;
  336. [SetUp]
  337. public void SetUp()
  338. {
  339. col = new ListBox.ObjectCollection (new ListBox ());
  340. }
  341. [Test]
  342. public void DefaultProperties ()
  343. {
  344. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  345. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  346. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  347. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  348. Assert.AreEqual (0, col.Count);
  349. }
  350. [Test]
  351. public void AddTest ()
  352. {
  353. col.Add ("Item1");
  354. col.Add ("Item2");
  355. Assert.AreEqual (2, col.Count, "#C1");
  356. }
  357. [Test]
  358. public void ClearTest ()
  359. {
  360. col.Add ("Item1");
  361. col.Add ("Item2");
  362. col.Clear ();
  363. Assert.AreEqual (0, col.Count, "#D1");
  364. }
  365. [Test]
  366. public void ContainsTest ()
  367. {
  368. object obj = "Item1";
  369. col.Add (obj);
  370. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  371. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  372. }
  373. [Test]
  374. public void IndexOfTest ()
  375. {
  376. col.Add ("Item1");
  377. col.Add ("Item2");
  378. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  379. }
  380. [Test]
  381. public void RemoveTest ()
  382. {
  383. col.Add ("Item1");
  384. col.Add ("Item2");
  385. col.Remove ("Item1");
  386. Assert.AreEqual (1, col.Count, "#G1");
  387. }
  388. [Test]
  389. public void RemoveAtTest ()
  390. {
  391. col.Add ("Item1");
  392. col.Add ("Item2");
  393. col.RemoveAt (0);
  394. Assert.AreEqual (1, col.Count, "#H1");
  395. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  396. }
  397. [Test]
  398. [ExpectedException (typeof (ArgumentNullException))]
  399. public void AddRangeNullTest ()
  400. {
  401. col.AddRange ((object []) null);
  402. }
  403. [Test]
  404. [ExpectedException (typeof (ArgumentNullException))]
  405. public void AddRangeNullTest2 ()
  406. {
  407. col.AddRange ((ListBox.ObjectCollection) null);
  408. }
  409. [Test]
  410. [ExpectedException (typeof (ArgumentNullException))]
  411. public void ContainsNullTest ()
  412. {
  413. col.Contains (null);
  414. }
  415. [Test]
  416. [ExpectedException (typeof (ArgumentNullException))]
  417. public void IndexOfNullTest ()
  418. {
  419. col.IndexOf (null);
  420. }
  421. [Test]
  422. [ExpectedException (typeof (ArgumentNullException))]
  423. public void InsertNullTest ()
  424. {
  425. col.Add ("Item1");
  426. col.Insert (0, null);
  427. }
  428. [Test]
  429. [ExpectedException (typeof (ArgumentNullException))]
  430. public void IndexerNullTest ()
  431. {
  432. col.Add ("Item1");
  433. col [0] = null;
  434. }
  435. [Test]
  436. [ExpectedException (typeof (ArgumentNullException))]
  437. public void AddNullTest ()
  438. {
  439. col.Add (null);
  440. }
  441. }
  442. }