ListBoxTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. [Ignore ("It depends on user system settings")]
  114. public void GetItemHeightTest ()
  115. {
  116. listBox.Visible = true;
  117. form.Controls.Add (listBox);
  118. listBox.Items.Add ("A");
  119. Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
  120. }
  121. [Ignore ("It depends on user system settings")]
  122. public void GetItemRectangleTest ()
  123. {
  124. form.Visible = true;
  125. listBox.Visible = true;
  126. form.Controls.Add (listBox);
  127. listBox.Items.Add ("A");
  128. Assert.AreEqual (new Rectangle(0,0,116,13), listBox.GetItemRectangle (0), "#29");
  129. }
  130. [Test]
  131. public void GetSelectedTest ()
  132. {
  133. listBox.Items.Add ("A");
  134. listBox.Items.Add ("B");
  135. listBox.Items.Add ("C");
  136. listBox.Items.Add ("D");
  137. listBox.Sorted = true;
  138. listBox.SetSelected (0,true);
  139. listBox.SetSelected (2,true);
  140. listBox.TopIndex=0;
  141. Assert.AreEqual (true, listBox.GetSelected (0), "#30");
  142. listBox.SetSelected (2,false);
  143. Assert.AreEqual (false, listBox.GetSelected (2), "#31");
  144. }
  145. [Test]
  146. public void IndexFromPointTest ()
  147. {
  148. listBox.Items.Add ("A");
  149. Point pt = new Point (100,100);
  150. listBox.IndexFromPoint (pt);
  151. Assert.AreEqual (-1, listBox.IndexFromPoint (100,100), "#32");
  152. }
  153. [Test]
  154. public void FindStringTest ()
  155. {
  156. listBox.FindString ("Hola", -5); // No exception, it's empty
  157. int x = listBox.FindString ("Hello");
  158. Assert.AreEqual (-1, x, "#19");
  159. listBox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  160. String myString = "ABC";
  161. x = listBox.FindString (myString);
  162. Assert.AreEqual (3, x, "#191");
  163. x = listBox.FindString (string.Empty);
  164. Assert.AreEqual (0, x, "#192");
  165. x = listBox.FindString ("NonExistant");
  166. Assert.AreEqual (-1, x, "#193");
  167. }
  168. [Test]
  169. public void FindStringExactTest ()
  170. {
  171. listBox.FindStringExact ("Hola", -5); // No exception, it's empty
  172. int x = listBox.FindStringExact ("Hello");
  173. Assert.AreEqual (-1, x, "#20");
  174. listBox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
  175. String myString = "ABC";
  176. x = listBox.FindStringExact (myString);
  177. Assert.AreEqual (1, x, "#201");
  178. x = listBox.FindStringExact (string.Empty);
  179. Assert.AreEqual (-1, x, "#202");
  180. x = listBox.FindStringExact ("NonExistant");
  181. Assert.AreEqual (-1, x, "#203");
  182. }
  183. #if NET_2_0
  184. [Test]
  185. public void AllowSelection ()
  186. {
  187. MockListBox lb = new MockListBox ();
  188. lb.SelectionMode = SelectionMode.None;
  189. Assert.IsFalse (lb.allow_selection, "#1");
  190. lb.SelectionMode = SelectionMode.One;
  191. Assert.IsTrue (lb.allow_selection, "#2");
  192. }
  193. #endif
  194. //
  195. // Exceptions
  196. //
  197. [Test]
  198. [ExpectedException (typeof (InvalidEnumArgumentException))]
  199. public void BorderStyleException ()
  200. {
  201. listBox.BorderStyle = (BorderStyle) 10;
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentException))]
  205. public void ColumnWidthException ()
  206. {
  207. listBox.ColumnWidth = -1;
  208. }
  209. [Test]
  210. [ExpectedException (typeof (InvalidEnumArgumentException))]
  211. public void DrawModeException ()
  212. {
  213. listBox.DrawMode = (DrawMode) 10;
  214. }
  215. [Test]
  216. [ExpectedException (typeof (ArgumentException))]
  217. public void DrawModeAndMultiColumnException ()
  218. {
  219. listBox.MultiColumn = true;
  220. listBox.DrawMode = DrawMode.OwnerDrawVariable;
  221. }
  222. [Test]
  223. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  224. public void ItemHeightException ()
  225. {
  226. listBox.ItemHeight = 256;
  227. }
  228. [Test] // bug #80696
  229. [NUnit.Framework.Category ("NotWorking")]
  230. public void SelectedIndex_Created ()
  231. {
  232. Form form = new Form ();
  233. ListBox listBox = new ListBox ();
  234. listBox.Items.Add ("A");
  235. listBox.Items.Add ("B");
  236. form.Controls.Add (listBox);
  237. form.Show ();
  238. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  239. listBox.SelectedIndex = 0;
  240. Assert.AreEqual (0, listBox.SelectedIndex, "#2");
  241. listBox.SelectedIndex = -1;
  242. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  243. listBox.SelectedIndex = 1;
  244. Assert.AreEqual (1, listBox.SelectedIndex, "#4");
  245. }
  246. [Test] // bug #80753
  247. [NUnit.Framework.Category ("NotWorking")]
  248. public void SelectedIndex_NotCreated ()
  249. {
  250. ListBox listBox = new ListBox ();
  251. listBox.Items.Add ("A");
  252. listBox.Items.Add ("B");
  253. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  254. listBox.SelectedIndex = 0;
  255. Assert.AreEqual (0, listBox.SelectedIndex, "#2");
  256. listBox.SelectedIndex = -1;
  257. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  258. listBox.SelectedIndex = 1;
  259. Assert.AreEqual (1, listBox.SelectedIndex, "#4");
  260. }
  261. [Test]
  262. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  263. public void SelectedIndexException ()
  264. {
  265. listBox.SelectedIndex = -2;
  266. }
  267. [Test]
  268. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  269. public void SelectedIndexException2 ()
  270. {
  271. listBox.SelectedIndex = listBox.Items.Count;
  272. }
  273. [Test]
  274. [ExpectedException (typeof (ArgumentException))]
  275. public void SelectedIndexModeNoneException ()
  276. {
  277. listBox.SelectionMode = SelectionMode.None;
  278. listBox.SelectedIndex = -1;
  279. }
  280. [Test]
  281. [ExpectedException (typeof (InvalidEnumArgumentException))]
  282. public void SelectionModeException ()
  283. {
  284. listBox.SelectionMode = (SelectionMode) 10;
  285. }
  286. [Test]
  287. public void SelectedValueNull()
  288. {
  289. listBox.Items.Clear ();
  290. listBox.Items.Add ("A");
  291. listBox.Items.Add ("B");
  292. listBox.Items.Add ("C");
  293. listBox.Items.Add ("D");
  294. listBox.SelectedIndex = 2;
  295. listBox.SelectedValue = null;
  296. Assert.AreEqual (listBox.SelectedIndex, 2);
  297. }
  298. [Test]
  299. public void SelectedValueEmptyString()
  300. {
  301. listBox.Items.Clear ();
  302. listBox.Items.Add ("A");
  303. listBox.Items.Add ("B");
  304. listBox.Items.Add ("C");
  305. listBox.Items.Add ("D");
  306. listBox.SelectedIndex = 2;
  307. listBox.SelectedValue = null;
  308. Assert.AreEqual (listBox.SelectedIndex, 2);
  309. }
  310. //
  311. // Events
  312. //
  313. //private bool eventFired;
  314. //private void GenericHandler (object sender, EventArgs e)
  315. //{
  316. // eventFired = true;
  317. //}
  318. public class MockListBox : ListBox
  319. {
  320. #if NET_2_0
  321. public bool allow_selection {
  322. get { return base.AllowSelection; }
  323. }
  324. #endif
  325. }
  326. }
  327. [TestFixture]
  328. public class ListBoxObjectCollectionTest
  329. {
  330. ListBox.ObjectCollection col;
  331. [SetUp]
  332. public void SetUp()
  333. {
  334. col = new ListBox.ObjectCollection (new ListBox ());
  335. }
  336. [Test]
  337. public void DefaultProperties ()
  338. {
  339. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  340. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  341. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  342. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  343. Assert.AreEqual (0, col.Count);
  344. }
  345. [Test]
  346. public void AddTest ()
  347. {
  348. col.Add ("Item1");
  349. col.Add ("Item2");
  350. Assert.AreEqual (2, col.Count, "#C1");
  351. }
  352. [Test]
  353. public void ClearTest ()
  354. {
  355. col.Add ("Item1");
  356. col.Add ("Item2");
  357. col.Clear ();
  358. Assert.AreEqual (0, col.Count, "#D1");
  359. }
  360. [Test]
  361. public void ContainsTest ()
  362. {
  363. object obj = "Item1";
  364. col.Add (obj);
  365. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  366. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  367. }
  368. [Test]
  369. public void IndexOfTest ()
  370. {
  371. col.Add ("Item1");
  372. col.Add ("Item2");
  373. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  374. }
  375. [Test]
  376. public void RemoveTest ()
  377. {
  378. col.Add ("Item1");
  379. col.Add ("Item2");
  380. col.Remove ("Item1");
  381. Assert.AreEqual (1, col.Count, "#G1");
  382. }
  383. [Test]
  384. public void RemoveAtTest ()
  385. {
  386. col.Add ("Item1");
  387. col.Add ("Item2");
  388. col.RemoveAt (0);
  389. Assert.AreEqual (1, col.Count, "#H1");
  390. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  391. }
  392. [Test]
  393. [ExpectedException (typeof (ArgumentNullException))]
  394. public void IndexerNullTest ()
  395. {
  396. col.Add ("Item1");
  397. col [0] = null;
  398. }
  399. [Test]
  400. [ExpectedException (typeof (ArgumentNullException))]
  401. public void AddNullTest ()
  402. {
  403. col.Add (null);
  404. }
  405. }
  406. }