ListBoxTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. }
  49. [TearDown]
  50. public void TearDown()
  51. {
  52. form.Dispose ();
  53. }
  54. [Test]
  55. public void ListBoxPropertyTest ()
  56. {
  57. Assert.AreEqual (0, listBox.ColumnWidth, "#1");
  58. Assert.AreEqual (DrawMode.Normal, listBox.DrawMode, "#2");
  59. Assert.AreEqual (0, listBox.HorizontalExtent, "#3");
  60. Assert.AreEqual (false, listBox.HorizontalScrollbar, "#4");
  61. Assert.AreEqual (true, listBox.IntegralHeight, "#5");
  62. //Assert.AreEqual (13, listBox.ItemHeight, "#6"); // Note: Item height depends on the current font.
  63. listBox.Items.Add ("a");
  64. listBox.Items.Add ("b");
  65. listBox.Items.Add ("c");
  66. Assert.AreEqual (3, listBox.Items.Count, "#7");
  67. Assert.AreEqual (false, listBox.MultiColumn, "#8");
  68. //Assert.AreEqual (46, listBox.PreferredHeight, "#9"); // Note: Item height depends on the current font.
  69. //Assert.AreEqual (RightToLeft.No , listBox.RightToLeft, "#10"); // Depends on Windows version
  70. Assert.AreEqual (false, listBox.ScrollAlwaysVisible, "#11");
  71. Assert.AreEqual (-1, listBox.SelectedIndex, "#12");
  72. listBox.SetSelected (2,true);
  73. Assert.AreEqual (2, listBox.SelectedIndices[0], "#13");
  74. Assert.AreEqual ("c", listBox.SelectedItem, "#14");
  75. Assert.AreEqual ("c", listBox.SelectedItems[0], "#15");
  76. Assert.AreEqual (SelectionMode.One, listBox.SelectionMode, "#16");
  77. listBox.SetSelected (2,false);
  78. Assert.AreEqual (false, listBox.Sorted, "#17");
  79. Assert.AreEqual ("", listBox.Text, "#18");
  80. Assert.AreEqual (0, listBox.TopIndex, "#19");
  81. Assert.AreEqual (true, listBox.UseTabStops, "#20");
  82. }
  83. [Test]
  84. public void BeginEndUpdateTest ()
  85. {
  86. form.Visible = true;
  87. listBox.Items.Add ("A");
  88. listBox.Visible = true;
  89. form.Controls.Add (listBox);
  90. listBox.BeginUpdate ();
  91. for (int x = 1; x < 5000; x++)
  92. {
  93. listBox.Items.Add ("Item " + x.ToString ());
  94. }
  95. listBox.EndUpdate ();
  96. listBox.SetSelected (1, true);
  97. listBox.SetSelected (3, true);
  98. Assert.AreEqual (true, listBox.SelectedItems.Contains ("Item 3"), "#21");
  99. }
  100. [Test]
  101. public void ClearSelectedTest ()
  102. {
  103. form.Visible = true;
  104. listBox.Items.Add ("A");
  105. listBox.Visible = true;
  106. form.Controls.Add (listBox);
  107. listBox.SetSelected (0, true);
  108. Assert.AreEqual ("A", listBox.SelectedItems [0].ToString (),"#22");
  109. listBox.ClearSelected ();
  110. Assert.AreEqual (0, listBox.SelectedItems.Count,"#23");
  111. }
  112. [Ignore ("It depends on user system settings")]
  113. public void GetItemHeightTest ()
  114. {
  115. listBox.Visible = true;
  116. form.Controls.Add (listBox);
  117. listBox.Items.Add ("A");
  118. Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
  119. }
  120. [Ignore ("It depends on user system settings")]
  121. public void GetItemRectangleTest ()
  122. {
  123. form.Visible = true;
  124. listBox.Visible = true;
  125. form.Controls.Add (listBox);
  126. listBox.Items.Add ("A");
  127. Assert.AreEqual (new Rectangle(0,0,116,13), listBox.GetItemRectangle (0), "#29");
  128. }
  129. [Test]
  130. public void GetSelectedTest ()
  131. {
  132. listBox.Items.Add ("A");
  133. listBox.Items.Add ("B");
  134. listBox.Items.Add ("C");
  135. listBox.Items.Add ("D");
  136. listBox.Sorted = true;
  137. listBox.SetSelected (0,true);
  138. listBox.SetSelected (2,true);
  139. listBox.TopIndex=0;
  140. Assert.AreEqual (true, listBox.GetSelected (0), "#30");
  141. listBox.SetSelected (2,false);
  142. Assert.AreEqual (false, listBox.GetSelected (2), "#31");
  143. }
  144. [Test]
  145. public void IndexFromPointTest ()
  146. {
  147. listBox.Items.Add ("A");
  148. Point pt = new Point (100,100);
  149. listBox.IndexFromPoint (pt);
  150. Assert.AreEqual (-1, listBox.IndexFromPoint (100,100), "#32");
  151. }
  152. [Test]
  153. public void FindStringTest ()
  154. {
  155. listBox.FindString ("Hola", -5); // No exception, it's empty
  156. int x = listBox.FindString ("Hello");
  157. Assert.AreEqual (-1, x, "#19");
  158. listBox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  159. String myString = "ABC";
  160. x = listBox.FindString (myString);
  161. Assert.AreEqual (3, x, "#191");
  162. x = listBox.FindString (string.Empty);
  163. Assert.AreEqual (0, x, "#192");
  164. x = listBox.FindString ("NonExistant");
  165. Assert.AreEqual (-1, x, "#193");
  166. }
  167. [Test]
  168. public void FindStringExactTest ()
  169. {
  170. listBox.FindStringExact ("Hola", -5); // No exception, it's empty
  171. int x = listBox.FindStringExact ("Hello");
  172. Assert.AreEqual (-1, x, "#20");
  173. listBox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
  174. String myString = "ABC";
  175. x = listBox.FindStringExact (myString);
  176. Assert.AreEqual (1, x, "#201");
  177. x = listBox.FindStringExact (string.Empty);
  178. Assert.AreEqual (-1, x, "#202");
  179. x = listBox.FindStringExact ("NonExistant");
  180. Assert.AreEqual (-1, x, "#203");
  181. }
  182. //
  183. // Exceptions
  184. //
  185. [Test]
  186. [ExpectedException (typeof (InvalidEnumArgumentException))]
  187. public void BorderStyleException ()
  188. {
  189. listBox.BorderStyle = (BorderStyle) 10;
  190. }
  191. [Test]
  192. [ExpectedException (typeof (ArgumentException))]
  193. public void ColumnWidthException ()
  194. {
  195. listBox.ColumnWidth = -1;
  196. }
  197. [Test]
  198. [ExpectedException (typeof (InvalidEnumArgumentException))]
  199. public void DrawModeException ()
  200. {
  201. listBox.DrawMode = (DrawMode) 10;
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentException))]
  205. public void DrawModeAndMultiColumnException ()
  206. {
  207. listBox.MultiColumn = true;
  208. listBox.DrawMode = DrawMode.OwnerDrawVariable;
  209. }
  210. [Test]
  211. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  212. public void ItemHeightException ()
  213. {
  214. listBox.ItemHeight = 256;
  215. }
  216. [Test]
  217. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  218. public void SelectedIndexException ()
  219. {
  220. listBox.SelectedIndex = -2;
  221. }
  222. [Test]
  223. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  224. public void SelectedIndexException2 ()
  225. {
  226. listBox.SelectedIndex = listBox.Items.Count;
  227. }
  228. [Test]
  229. [ExpectedException (typeof (ArgumentException))]
  230. public void SelectedIndexModeNoneException ()
  231. {
  232. listBox.SelectionMode = SelectionMode.None;
  233. listBox.SelectedIndex = -1;
  234. }
  235. [Test]
  236. [ExpectedException (typeof (InvalidEnumArgumentException))]
  237. public void SelectionModeException ()
  238. {
  239. listBox.SelectionMode = (SelectionMode) 10;
  240. }
  241. [Test]
  242. public void SelectedValueNull()
  243. {
  244. listBox.Items.Clear ();
  245. listBox.Items.Add ("A");
  246. listBox.Items.Add ("B");
  247. listBox.Items.Add ("C");
  248. listBox.Items.Add ("D");
  249. listBox.SelectedIndex = 2;
  250. listBox.SelectedValue = null;
  251. Assert.AreEqual (listBox.SelectedIndex, 2);
  252. }
  253. [Test]
  254. public void SelectedValueEmptyString()
  255. {
  256. listBox.Items.Clear ();
  257. listBox.Items.Add ("A");
  258. listBox.Items.Add ("B");
  259. listBox.Items.Add ("C");
  260. listBox.Items.Add ("D");
  261. listBox.SelectedIndex = 2;
  262. listBox.SelectedValue = null;
  263. Assert.AreEqual (listBox.SelectedIndex, 2);
  264. }
  265. //
  266. // Events
  267. //
  268. private bool eventFired;
  269. private void GenericHandler (object sender, EventArgs e)
  270. {
  271. eventFired = true;
  272. }
  273. }
  274. [TestFixture]
  275. public class ListBoxObjectCollectionTest
  276. {
  277. ListBox.ObjectCollection col;
  278. [SetUp]
  279. public void SetUp()
  280. {
  281. col = new ListBox.ObjectCollection (new ListBox ());
  282. }
  283. [Test]
  284. public void DefaultProperties ()
  285. {
  286. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  287. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  288. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  289. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  290. Assert.AreEqual (0, col.Count);
  291. }
  292. [Test]
  293. public void AddTest ()
  294. {
  295. col.Add ("Item1");
  296. col.Add ("Item2");
  297. Assert.AreEqual (2, col.Count, "#C1");
  298. }
  299. [Test]
  300. public void ClearTest ()
  301. {
  302. col.Add ("Item1");
  303. col.Add ("Item2");
  304. col.Clear ();
  305. Assert.AreEqual (0, col.Count, "#D1");
  306. }
  307. [Test]
  308. public void ContainsTest ()
  309. {
  310. object obj = "Item1";
  311. col.Add (obj);
  312. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  313. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  314. }
  315. [Test]
  316. public void IndexOfTest ()
  317. {
  318. col.Add ("Item1");
  319. col.Add ("Item2");
  320. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  321. }
  322. [Test]
  323. public void RemoveTest ()
  324. {
  325. col.Add ("Item1");
  326. col.Add ("Item2");
  327. col.Remove ("Item1");
  328. Assert.AreEqual (1, col.Count, "#G1");
  329. }
  330. [Test]
  331. public void RemoveAtTest ()
  332. {
  333. col.Add ("Item1");
  334. col.Add ("Item2");
  335. col.RemoveAt (0);
  336. Assert.AreEqual (1, col.Count, "#H1");
  337. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  338. }
  339. [Test]
  340. [ExpectedException (typeof (ArgumentNullException))]
  341. public void IndexerNullTest ()
  342. {
  343. col.Add ("Item1");
  344. col [0] = null;
  345. }
  346. [Test]
  347. [ExpectedException (typeof (ArgumentNullException))]
  348. public void AddNullTest ()
  349. {
  350. col.Add (null);
  351. }
  352. }
  353. }