ListBoxTest.cs 9.7 KB

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