ComboBoxTest.cs 16 KB

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