ListBoxTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. //
  184. // Exceptions
  185. //
  186. [Test]
  187. [ExpectedException (typeof (InvalidEnumArgumentException))]
  188. public void BorderStyleException ()
  189. {
  190. listBox.BorderStyle = (BorderStyle) 10;
  191. }
  192. [Test]
  193. [ExpectedException (typeof (ArgumentException))]
  194. public void ColumnWidthException ()
  195. {
  196. listBox.ColumnWidth = -1;
  197. }
  198. [Test]
  199. [ExpectedException (typeof (InvalidEnumArgumentException))]
  200. public void DrawModeException ()
  201. {
  202. listBox.DrawMode = (DrawMode) 10;
  203. }
  204. [Test]
  205. [ExpectedException (typeof (ArgumentException))]
  206. public void DrawModeAndMultiColumnException ()
  207. {
  208. listBox.MultiColumn = true;
  209. listBox.DrawMode = DrawMode.OwnerDrawVariable;
  210. }
  211. [Test]
  212. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  213. public void ItemHeightException ()
  214. {
  215. listBox.ItemHeight = 256;
  216. }
  217. [Test]
  218. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  219. public void SelectedIndexException ()
  220. {
  221. listBox.SelectedIndex = -2;
  222. }
  223. [Test]
  224. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  225. public void SelectedIndexException2 ()
  226. {
  227. listBox.SelectedIndex = listBox.Items.Count;
  228. }
  229. [Test]
  230. [ExpectedException (typeof (ArgumentException))]
  231. public void SelectedIndexModeNoneException ()
  232. {
  233. listBox.SelectionMode = SelectionMode.None;
  234. listBox.SelectedIndex = -1;
  235. }
  236. [Test]
  237. [ExpectedException (typeof (InvalidEnumArgumentException))]
  238. public void SelectionModeException ()
  239. {
  240. listBox.SelectionMode = (SelectionMode) 10;
  241. }
  242. [Test]
  243. public void SelectedValueNull()
  244. {
  245. listBox.Items.Clear ();
  246. listBox.Items.Add ("A");
  247. listBox.Items.Add ("B");
  248. listBox.Items.Add ("C");
  249. listBox.Items.Add ("D");
  250. listBox.SelectedIndex = 2;
  251. listBox.SelectedValue = null;
  252. Assert.AreEqual (listBox.SelectedIndex, 2);
  253. }
  254. [Test]
  255. public void SelectedValueEmptyString()
  256. {
  257. listBox.Items.Clear ();
  258. listBox.Items.Add ("A");
  259. listBox.Items.Add ("B");
  260. listBox.Items.Add ("C");
  261. listBox.Items.Add ("D");
  262. listBox.SelectedIndex = 2;
  263. listBox.SelectedValue = null;
  264. Assert.AreEqual (listBox.SelectedIndex, 2);
  265. }
  266. //
  267. // Events
  268. //
  269. private bool eventFired;
  270. private void GenericHandler (object sender, EventArgs e)
  271. {
  272. eventFired = true;
  273. }
  274. }
  275. [TestFixture]
  276. public class ListBoxObjectCollectionTest
  277. {
  278. ListBox.ObjectCollection col;
  279. [SetUp]
  280. public void SetUp()
  281. {
  282. col = new ListBox.ObjectCollection (new ListBox ());
  283. }
  284. [Test]
  285. public void DefaultProperties ()
  286. {
  287. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  288. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  289. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  290. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  291. Assert.AreEqual (0, col.Count);
  292. }
  293. [Test]
  294. public void AddTest ()
  295. {
  296. col.Add ("Item1");
  297. col.Add ("Item2");
  298. Assert.AreEqual (2, col.Count, "#C1");
  299. }
  300. [Test]
  301. public void ClearTest ()
  302. {
  303. col.Add ("Item1");
  304. col.Add ("Item2");
  305. col.Clear ();
  306. Assert.AreEqual (0, col.Count, "#D1");
  307. }
  308. [Test]
  309. public void ContainsTest ()
  310. {
  311. object obj = "Item1";
  312. col.Add (obj);
  313. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  314. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  315. }
  316. [Test]
  317. public void IndexOfTest ()
  318. {
  319. col.Add ("Item1");
  320. col.Add ("Item2");
  321. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  322. }
  323. [Test]
  324. public void RemoveTest ()
  325. {
  326. col.Add ("Item1");
  327. col.Add ("Item2");
  328. col.Remove ("Item1");
  329. Assert.AreEqual (1, col.Count, "#G1");
  330. }
  331. [Test]
  332. public void RemoveAtTest ()
  333. {
  334. col.Add ("Item1");
  335. col.Add ("Item2");
  336. col.RemoveAt (0);
  337. Assert.AreEqual (1, col.Count, "#H1");
  338. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  339. }
  340. [Test]
  341. [ExpectedException (typeof (ArgumentNullException))]
  342. public void IndexerNullTest ()
  343. {
  344. col.Add ("Item1");
  345. col [0] = null;
  346. }
  347. [Test]
  348. [ExpectedException (typeof (ArgumentNullException))]
  349. public void AddNullTest ()
  350. {
  351. col.Add (null);
  352. }
  353. }
  354. }