ListBoxTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. public void SelectedIndex_Created ()
  230. {
  231. Form form = new Form ();
  232. ListBox listBox = new ListBox ();
  233. listBox.Items.Add ("A");
  234. listBox.Items.Add ("B");
  235. form.Controls.Add (listBox);
  236. form.Show ();
  237. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  238. listBox.SelectedIndex = 0;
  239. Assert.AreEqual (0, listBox.SelectedIndex, "#2");
  240. listBox.SelectedIndex = -1;
  241. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  242. listBox.SelectedIndex = 1;
  243. Assert.AreEqual (1, listBox.SelectedIndex, "#4");
  244. }
  245. [Test] // bug #80753
  246. public void SelectedIndex_NotCreated ()
  247. {
  248. ListBox listBox = new ListBox ();
  249. listBox.Items.Add ("A");
  250. listBox.Items.Add ("B");
  251. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  252. listBox.SelectedIndex = 0;
  253. Assert.AreEqual (0, listBox.SelectedIndex, "#2");
  254. listBox.SelectedIndex = -1;
  255. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  256. listBox.SelectedIndex = 1;
  257. Assert.AreEqual (1, listBox.SelectedIndex, "#4");
  258. }
  259. [Test]
  260. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  261. public void SelectedIndexException ()
  262. {
  263. listBox.SelectedIndex = -2;
  264. }
  265. [Test]
  266. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  267. public void SelectedIndexException2 ()
  268. {
  269. listBox.SelectedIndex = listBox.Items.Count;
  270. }
  271. [Test]
  272. [ExpectedException (typeof (ArgumentException))]
  273. public void SelectedIndexModeNoneException ()
  274. {
  275. listBox.SelectionMode = SelectionMode.None;
  276. listBox.SelectedIndex = -1;
  277. }
  278. [Test]
  279. [ExpectedException (typeof (InvalidEnumArgumentException))]
  280. public void SelectionModeException ()
  281. {
  282. listBox.SelectionMode = (SelectionMode) 10;
  283. }
  284. [Test]
  285. public void SelectedValueNull()
  286. {
  287. listBox.Items.Clear ();
  288. listBox.Items.Add ("A");
  289. listBox.Items.Add ("B");
  290. listBox.Items.Add ("C");
  291. listBox.Items.Add ("D");
  292. listBox.SelectedIndex = 2;
  293. listBox.SelectedValue = null;
  294. Assert.AreEqual (listBox.SelectedIndex, 2);
  295. }
  296. [Test]
  297. public void SelectedValueEmptyString()
  298. {
  299. listBox.Items.Clear ();
  300. listBox.Items.Add ("A");
  301. listBox.Items.Add ("B");
  302. listBox.Items.Add ("C");
  303. listBox.Items.Add ("D");
  304. listBox.SelectedIndex = 2;
  305. listBox.SelectedValue = null;
  306. Assert.AreEqual (listBox.SelectedIndex, 2);
  307. }
  308. //
  309. // Events
  310. //
  311. //private bool eventFired;
  312. //private void GenericHandler (object sender, EventArgs e)
  313. //{
  314. // eventFired = true;
  315. //}
  316. public class MockListBox : ListBox
  317. {
  318. #if NET_2_0
  319. public bool allow_selection {
  320. get { return base.AllowSelection; }
  321. }
  322. #endif
  323. }
  324. }
  325. [TestFixture]
  326. public class ListBoxObjectCollectionTest
  327. {
  328. ListBox.ObjectCollection col;
  329. [SetUp]
  330. public void SetUp()
  331. {
  332. col = new ListBox.ObjectCollection (new ListBox ());
  333. }
  334. [Test]
  335. public void DefaultProperties ()
  336. {
  337. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  338. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  339. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  340. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  341. Assert.AreEqual (0, col.Count);
  342. }
  343. [Test]
  344. public void AddTest ()
  345. {
  346. col.Add ("Item1");
  347. col.Add ("Item2");
  348. Assert.AreEqual (2, col.Count, "#C1");
  349. }
  350. [Test]
  351. public void ClearTest ()
  352. {
  353. col.Add ("Item1");
  354. col.Add ("Item2");
  355. col.Clear ();
  356. Assert.AreEqual (0, col.Count, "#D1");
  357. }
  358. [Test]
  359. public void ContainsTest ()
  360. {
  361. object obj = "Item1";
  362. col.Add (obj);
  363. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  364. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  365. }
  366. [Test]
  367. public void IndexOfTest ()
  368. {
  369. col.Add ("Item1");
  370. col.Add ("Item2");
  371. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  372. }
  373. [Test]
  374. public void RemoveTest ()
  375. {
  376. col.Add ("Item1");
  377. col.Add ("Item2");
  378. col.Remove ("Item1");
  379. Assert.AreEqual (1, col.Count, "#G1");
  380. }
  381. [Test]
  382. public void RemoveAtTest ()
  383. {
  384. col.Add ("Item1");
  385. col.Add ("Item2");
  386. col.RemoveAt (0);
  387. Assert.AreEqual (1, col.Count, "#H1");
  388. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  389. }
  390. [Test]
  391. [ExpectedException (typeof (ArgumentNullException))]
  392. public void IndexerNullTest ()
  393. {
  394. col.Add ("Item1");
  395. col [0] = null;
  396. }
  397. [Test]
  398. [ExpectedException (typeof (ArgumentNullException))]
  399. public void AddNullTest ()
  400. {
  401. col.Add (null);
  402. }
  403. }
  404. }