ComboBoxTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. using System;
  29. using System.Windows.Forms;
  30. using System.Drawing;
  31. using System.Reflection;
  32. using NUnit.Framework;
  33. using System.Collections;
  34. using System.ComponentModel;
  35. namespace MonoTests.System.Windows.Forms
  36. {
  37. [TestFixture]
  38. public class ComboBoxTest
  39. {
  40. [Test]
  41. public void ComboBoxPropertyTest ()
  42. {
  43. ComboBox mycmbbox = new ComboBox ();
  44. Assert.AreEqual (DrawMode.Normal, mycmbbox.DrawMode, "#1");
  45. Assert.AreEqual (ComboBoxStyle.DropDown, mycmbbox.DropDownStyle, "#2");
  46. Assert.AreEqual (121, mycmbbox.DropDownWidth, "#3");
  47. Assert.AreEqual (false, mycmbbox.DroppedDown, "#4");
  48. Assert.AreEqual (true, mycmbbox.IntegralHeight, "#5");
  49. Assert.AreEqual (0, mycmbbox.Items.Count, "#6");
  50. //Assert.AreEqual (15, mycmbbox.ItemHeight, "#7"); // Note: Item height depends on the current font.
  51. Assert.AreEqual (8, mycmbbox.MaxDropDownItems, "#8");
  52. Assert.AreEqual (0, mycmbbox.MaxLength, "#9");
  53. //Assert.AreEqual (20, mycmbbox.PreferredHeight, "#10");
  54. // Note: Item height depends on the current font.
  55. Assert.AreEqual (-1, mycmbbox.SelectedIndex, "#11");
  56. Assert.AreEqual (null, mycmbbox.SelectedItem, "#12");
  57. Assert.AreEqual ("", mycmbbox.SelectedText, "#13");
  58. Assert.AreEqual (0, mycmbbox.SelectionLength, "#14");
  59. Assert.AreEqual (0, mycmbbox.SelectionStart, "#15");
  60. Assert.AreEqual (false, mycmbbox.Sorted, "#16");
  61. Assert.AreEqual ("", mycmbbox.Text, "#17");
  62. }
  63. [Test]
  64. public void BeginEndUpdateTest ()
  65. {
  66. Form myform = new Form ();
  67. myform.Visible = true;
  68. ComboBox cmbbox = new ComboBox ();
  69. cmbbox.Items.Add ("A");
  70. cmbbox.Visible = true;
  71. myform.Controls.Add (cmbbox);
  72. cmbbox.BeginUpdate ();
  73. for (int x = 1 ; x < 5000 ; x++) {
  74. cmbbox.Items.Add ("Item " + x.ToString ());
  75. }
  76. cmbbox.EndUpdate ();
  77. myform.Dispose ();
  78. }
  79. [Test]
  80. public void FindStringTest ()
  81. {
  82. ComboBox cmbbox = new ComboBox ();
  83. cmbbox.FindString ("Hola", -5); // No exception, it's empty
  84. int x = cmbbox.FindString ("Hello");
  85. Assert.AreEqual (-1, x, "#19");
  86. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  87. String myString = "ABC";
  88. x = cmbbox.FindString (myString);
  89. Assert.AreEqual (3, x, "#191");
  90. x = cmbbox.FindString (string.Empty);
  91. Assert.AreEqual (0, x, "#192");
  92. x = cmbbox.FindString ("NonExistant");
  93. Assert.AreEqual (-1, x, "#193");
  94. }
  95. [Test]
  96. public void FindStringExactTest ()
  97. {
  98. ComboBox cmbbox = new ComboBox ();
  99. cmbbox.FindStringExact ("Hola", -5); // No exception, it's empty
  100. int x = cmbbox.FindStringExact ("Hello");
  101. Assert.AreEqual (-1, x, "#20");
  102. cmbbox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
  103. String myString = "ABC";
  104. x = cmbbox.FindStringExact (myString);
  105. Assert.AreEqual (1, x, "#201");
  106. x = cmbbox.FindStringExact (string.Empty);
  107. Assert.AreEqual (-1, x, "#202");
  108. x = cmbbox.FindStringExact ("NonExistant");
  109. Assert.AreEqual (-1, x, "#203");
  110. }
  111. [Test]
  112. public void GetItemHeightTest ()
  113. {
  114. ComboBox cmbbox = new ComboBox ();
  115. cmbbox.Items.Add ("ABC");
  116. cmbbox.Items.Add ("BCD");
  117. cmbbox.Items.Add ("DEF");
  118. int x = -1;
  119. x = cmbbox.GetItemHeight (x);
  120. Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
  121. }
  122. //
  123. // Exceptions
  124. //
  125. [Test]
  126. [ExpectedException (typeof (InvalidEnumArgumentException))]
  127. public void DropDownStyleException ()
  128. {
  129. ComboBox cmbbox = new ComboBox ();
  130. cmbbox.DropDownStyle = (ComboBoxStyle) 10;
  131. }
  132. [Test]
  133. [ExpectedException (typeof (InvalidEnumArgumentException))]
  134. public void DrawModeException ()
  135. {
  136. ComboBox cmbbox = new ComboBox ();
  137. cmbbox.DrawMode = (DrawMode) 10;
  138. }
  139. [Test]
  140. #if NET_2_0
  141. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  142. #else
  143. [ExpectedException (typeof (ArgumentException))]
  144. #endif
  145. public void DropDownWidthException ()
  146. {
  147. ComboBox cmbbox = new ComboBox ();
  148. cmbbox.DropDownWidth = 0;
  149. }
  150. [Test]
  151. #if NET_2_0
  152. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  153. #else
  154. [ExpectedException (typeof (ArgumentException))]
  155. #endif
  156. public void ItemHeightException ()
  157. {
  158. ComboBox cmbbox = new ComboBox ();
  159. cmbbox.ItemHeight = -1;
  160. }
  161. [Test]
  162. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  163. public void SelectedIndexException ()
  164. {
  165. ComboBox cmbbox = new ComboBox ();
  166. cmbbox.SelectedIndex = -2;
  167. }
  168. [Test]
  169. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  170. public void FindStringExactMinException ()
  171. {
  172. ComboBox cmbbox = new ComboBox ();
  173. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  174. cmbbox.FindStringExact ("Hola", -2);
  175. }
  176. [Test]
  177. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  178. public void FindStringExactMaxException ()
  179. {
  180. ComboBox cmbbox = new ComboBox ();
  181. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  182. cmbbox.FindStringExact ("Hola", 3);
  183. }
  184. //
  185. // Events
  186. //
  187. private bool eventFired;
  188. private DrawItemEventArgs drawItemsArgs;
  189. private void DrawItemEventH (object sender, DrawItemEventArgs e)
  190. {
  191. eventFired = true;
  192. drawItemsArgs = e;
  193. }
  194. private void GenericHandler (object sender, EventArgs e)
  195. {
  196. eventFired = true;
  197. }
  198. [Ignore ("Bugs in X11 prevent this test to run properly")]
  199. public void DrawItemEventTest ()
  200. {
  201. eventFired = false;
  202. drawItemsArgs = null;
  203. Form myform = new Form ();
  204. ComboBox cmbbox = new ComboBox ();
  205. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  206. cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
  207. cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
  208. myform.Controls.Add (cmbbox);
  209. cmbbox.Items.AddRange(new object[] {"Item1"});
  210. myform.Visible = true;
  211. cmbbox.Visible = true;
  212. cmbbox.Refresh ();
  213. Assert.AreEqual (true, eventFired, "DW1");
  214. Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
  215. myform.Dispose ();
  216. }
  217. [Test]
  218. public void DropDownStyleEventTest ()
  219. {
  220. eventFired = false;
  221. ComboBox cmbbox = new ComboBox ();
  222. cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
  223. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  224. Assert.AreEqual (true, eventFired, "DI1");
  225. }
  226. [Test]
  227. public void SelectedIndextTest ()
  228. {
  229. eventFired = false;
  230. ComboBox cmbbox = new ComboBox ();
  231. cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
  232. cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
  233. cmbbox.SelectedIndex = 1;
  234. Assert.AreEqual (true, eventFired, "SI1");
  235. }
  236. [Test]
  237. public void SelectionWithAdd()
  238. {
  239. ComboBox cb = new ComboBox();
  240. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  241. cb.Items.Add("Item 1");
  242. cb.Items.Add("Item 3");
  243. cb.SelectedIndex = 1;
  244. eventFired = false;
  245. cb.Items.Add("Item 4");
  246. Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
  247. Assert.AreEqual(false, eventFired, "SWA2");
  248. cb.Sorted = true;
  249. cb.SelectedIndex = 1;
  250. eventFired = false;
  251. cb.Items.Add("Item 5");
  252. Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
  253. Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
  254. Assert.AreEqual(false, eventFired, "SWA5");
  255. cb.SelectedIndex = 1;
  256. eventFired = false;
  257. cb.Items.Add("Item 2");
  258. Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
  259. Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
  260. Assert.AreEqual(false, eventFired, "SWA8");
  261. }
  262. [Test]
  263. public void SelectionWithInsert()
  264. {
  265. ComboBox cb = new ComboBox();
  266. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  267. cb.Items.Add("Item 1");
  268. cb.SelectedIndex = 0;
  269. eventFired = false;
  270. cb.Items.Insert(0, "Item 2");
  271. Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
  272. Assert.AreEqual(false, eventFired, "SWI2");
  273. }
  274. [Test]
  275. public void SelectionWithClear()
  276. {
  277. ComboBox cb = new ComboBox();
  278. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  279. cb.Items.Add("Item 1");
  280. cb.SelectedIndex = 0;
  281. eventFired = false;
  282. cb.Items.Clear();
  283. Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
  284. Assert.AreEqual(false, eventFired, "SWC2");
  285. }
  286. [Test]
  287. public void SortedTest()
  288. {
  289. ComboBox mycb = new ComboBox();
  290. Assert.AreEqual(false, mycb.Sorted, "#1");
  291. mycb.Items.Add("Item 2");
  292. mycb.Items.Add("Item 1");
  293. Assert.AreEqual("Item 2", mycb.Items[0], "#2");
  294. Assert.AreEqual("Item 1", mycb.Items[1], "#3");
  295. mycb.Sorted = true;
  296. Assert.AreEqual(true, mycb.Sorted, "#4");
  297. Assert.AreEqual("Item 1", mycb.Items[0], "#5");
  298. Assert.AreEqual("Item 2", mycb.Items[1], "#6");
  299. mycb.Sorted = false;
  300. Assert.AreEqual(false, mycb.Sorted, "#7");
  301. Assert.AreEqual("Item 1", mycb.Items[0], "#8");
  302. Assert.AreEqual("Item 2", mycb.Items[1], "#9");
  303. }
  304. [Test]
  305. public void SortedAddTest()
  306. {
  307. ComboBox mycb = new ComboBox();
  308. mycb.Items.Add("Item 2");
  309. mycb.Items.Add("Item 1");
  310. mycb.Sorted = true;
  311. Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
  312. Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
  313. }
  314. [Test]
  315. public void SortedInsertTest()
  316. {
  317. ComboBox mycb = new ComboBox();
  318. mycb.Items.Add("Item 2");
  319. mycb.Items.Add("Item 1");
  320. mycb.Sorted = true;
  321. mycb.Items.Insert (0, "Item 3");
  322. Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
  323. Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
  324. Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
  325. }
  326. [Test]
  327. public void SortedSelectionInteractions()
  328. {
  329. ComboBox cb = new ComboBox();
  330. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  331. cb.Items.Add("Item 1");
  332. cb.Items.Add("Item 2");
  333. cb.Items.Add("Item 3");
  334. cb.SelectedIndex = 1;
  335. eventFired = false;
  336. cb.Sorted = true;
  337. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
  338. Assert.AreEqual(true, eventFired, "#SSI2");
  339. cb.SelectedIndex = 1;
  340. eventFired = false;
  341. cb.Sorted = true;
  342. Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
  343. Assert.AreEqual(false, eventFired, "#SSI4");
  344. cb.SelectedIndex = 1;
  345. eventFired = false;
  346. cb.Sorted = false;
  347. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
  348. Assert.AreEqual(true, eventFired, "#SSI6");
  349. }
  350. }
  351. [TestFixture]
  352. public class ComboBoxObjectCollectionTest
  353. {
  354. [Test]
  355. public void ComboBoxObjectCollectionPropertyTest ()
  356. {
  357. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  358. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  359. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  360. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  361. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  362. }
  363. [Test]
  364. public void AddTest ()
  365. {
  366. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  367. col.Add ("Item1");
  368. col.Add ("Item2");
  369. Assert.AreEqual (2, col.Count, "#C1");
  370. }
  371. [Test]
  372. public void ClearTest ()
  373. {
  374. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  375. col.Add ("Item1");
  376. col.Add ("Item2");
  377. col.Clear ();
  378. Assert.AreEqual (0, col.Count, "#D1");
  379. }
  380. [Test]
  381. public void ContainsTest ()
  382. {
  383. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  384. object obj = "Item1";
  385. col.Add (obj);
  386. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  387. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  388. }
  389. [Test]
  390. public void IndexOfTest ()
  391. {
  392. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  393. col.Add ("Item1");
  394. col.Add ("Item2");
  395. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  396. }
  397. [Test]
  398. public void RemoveTest ()
  399. {
  400. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  401. col.Add ("Item1");
  402. col.Add ("Item2");
  403. col.Remove ("Item1");
  404. Assert.AreEqual (1, col.Count, "#G1");
  405. }
  406. [Test]
  407. public void RemoveAtTest ()
  408. {
  409. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  410. col.Add ("Item1");
  411. col.Add ("Item2");
  412. col.RemoveAt (0);
  413. Assert.AreEqual (1, col.Count, "#H1");
  414. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  415. }
  416. [Test]
  417. [ExpectedException (typeof (ArgumentNullException))]
  418. public void AddNullTest ()
  419. {
  420. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  421. col.Add (null);
  422. }
  423. [Test]
  424. [ExpectedException (typeof (ArgumentNullException))]
  425. public void AddRangeNullTest ()
  426. {
  427. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  428. col.AddRange (null);
  429. }
  430. [Test]
  431. [ExpectedException (typeof (ArgumentNullException))]
  432. public void ContainsNullTest ()
  433. {
  434. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  435. col.Contains (null);
  436. }
  437. [Test]
  438. [ExpectedException (typeof (ArgumentNullException))]
  439. public void IndexOfNullTest ()
  440. {
  441. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  442. col.IndexOf (null);
  443. }
  444. [Test]
  445. [ExpectedException (typeof (ArgumentNullException))]
  446. public void InsertNullTest ()
  447. {
  448. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  449. col.Add ("Item1");
  450. col.Insert (0, null);
  451. }
  452. [Test]
  453. [ExpectedException (typeof (ArgumentNullException))]
  454. public void IndexerNullTest ()
  455. {
  456. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  457. col.Add ("Item1");
  458. col [0] = null;
  459. }
  460. }
  461. }