ListBoxTest.cs 9.3 KB

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