ComboBoxTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. #if ONLY_1_1
  217. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  218. #endif
  219. public void FindStringExactMaxException ()
  220. {
  221. ComboBox cmbbox = new ComboBox ();
  222. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  223. cmbbox.FindStringExact ("Hola", 3);
  224. }
  225. //
  226. // Events
  227. //
  228. private bool eventFired;
  229. private DrawItemEventArgs drawItemsArgs;
  230. private void DrawItemEventH (object sender, DrawItemEventArgs e)
  231. {
  232. eventFired = true;
  233. drawItemsArgs = e;
  234. }
  235. private void GenericHandler (object sender, EventArgs e)
  236. {
  237. eventFired = true;
  238. }
  239. [Ignore ("Bugs in X11 prevent this test to run properly")]
  240. public void DrawItemEventTest ()
  241. {
  242. eventFired = false;
  243. drawItemsArgs = null;
  244. Form myform = new Form ();
  245. myform.ShowInTaskbar = false;
  246. ComboBox cmbbox = new ComboBox ();
  247. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  248. cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
  249. cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
  250. myform.Controls.Add (cmbbox);
  251. cmbbox.Items.AddRange(new object[] {"Item1"});
  252. myform.Visible = true;
  253. cmbbox.Visible = true;
  254. cmbbox.Refresh ();
  255. Assert.AreEqual (true, eventFired, "DW1");
  256. Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
  257. myform.Dispose ();
  258. }
  259. [Test]
  260. public void DropDownStyleEventTest ()
  261. {
  262. eventFired = false;
  263. ComboBox cmbbox = new ComboBox ();
  264. cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
  265. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  266. Assert.AreEqual (true, eventFired, "DI1");
  267. }
  268. [Test]
  269. public void SelectedIndextTest ()
  270. {
  271. eventFired = false;
  272. ComboBox cmbbox = new ComboBox ();
  273. cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
  274. cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
  275. cmbbox.SelectedIndex = 1;
  276. Assert.AreEqual (true, eventFired, "SI1");
  277. }
  278. [Test]
  279. public void SelectionWithAdd()
  280. {
  281. ComboBox cb = new ComboBox();
  282. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  283. cb.Items.Add("Item 1");
  284. cb.Items.Add("Item 3");
  285. cb.SelectedIndex = 1;
  286. eventFired = false;
  287. cb.Items.Add("Item 4");
  288. Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
  289. Assert.AreEqual(false, eventFired, "SWA2");
  290. cb.Sorted = true;
  291. cb.SelectedIndex = 1;
  292. eventFired = false;
  293. cb.Items.Add("Item 5");
  294. Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
  295. Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
  296. Assert.AreEqual(false, eventFired, "SWA5");
  297. cb.SelectedIndex = 1;
  298. eventFired = false;
  299. cb.Items.Add("Item 2");
  300. Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
  301. Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
  302. Assert.AreEqual(false, eventFired, "SWA8");
  303. }
  304. [Test]
  305. public void SelectionWithInsert()
  306. {
  307. ComboBox cb = new ComboBox();
  308. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  309. cb.Items.Add("Item 1");
  310. cb.SelectedIndex = 0;
  311. eventFired = false;
  312. cb.Items.Insert(0, "Item 2");
  313. Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
  314. Assert.AreEqual(false, eventFired, "SWI2");
  315. }
  316. [Test]
  317. public void SelectionWithClear()
  318. {
  319. ComboBox cb = new ComboBox();
  320. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  321. cb.Items.Add("Item 1");
  322. cb.SelectedIndex = 0;
  323. eventFired = false;
  324. cb.Items.Clear();
  325. Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
  326. Assert.AreEqual(false, eventFired, "SWC2");
  327. }
  328. [Test]
  329. public void SortedTest()
  330. {
  331. ComboBox mycb = new ComboBox();
  332. Assert.AreEqual(false, mycb.Sorted, "#1");
  333. mycb.Items.Add("Item 2");
  334. mycb.Items.Add("Item 1");
  335. Assert.AreEqual("Item 2", mycb.Items[0], "#2");
  336. Assert.AreEqual("Item 1", mycb.Items[1], "#3");
  337. mycb.Sorted = true;
  338. Assert.AreEqual(true, mycb.Sorted, "#4");
  339. Assert.AreEqual("Item 1", mycb.Items[0], "#5");
  340. Assert.AreEqual("Item 2", mycb.Items[1], "#6");
  341. mycb.Sorted = false;
  342. Assert.AreEqual(false, mycb.Sorted, "#7");
  343. Assert.AreEqual("Item 1", mycb.Items[0], "#8");
  344. Assert.AreEqual("Item 2", mycb.Items[1], "#9");
  345. }
  346. [Test]
  347. public void SortedAddTest()
  348. {
  349. ComboBox mycb = new ComboBox();
  350. mycb.Items.Add("Item 2");
  351. mycb.Items.Add("Item 1");
  352. mycb.Sorted = true;
  353. Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
  354. Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
  355. }
  356. [Test]
  357. public void SortedInsertTest()
  358. {
  359. ComboBox mycb = new ComboBox();
  360. mycb.Items.Add("Item 2");
  361. mycb.Items.Add("Item 1");
  362. mycb.Sorted = true;
  363. mycb.Items.Insert (0, "Item 3");
  364. Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
  365. Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
  366. Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
  367. }
  368. [Test]
  369. public void SortedSelectionInteractions()
  370. {
  371. ComboBox cb = new ComboBox();
  372. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  373. cb.Items.Add("Item 1");
  374. cb.Items.Add("Item 2");
  375. cb.Items.Add("Item 3");
  376. cb.SelectedIndex = 1;
  377. eventFired = false;
  378. cb.Sorted = true;
  379. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
  380. Assert.AreEqual(true, eventFired, "#SSI2");
  381. cb.SelectedIndex = 1;
  382. eventFired = false;
  383. cb.Sorted = true;
  384. Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
  385. Assert.AreEqual(false, eventFired, "#SSI4");
  386. cb.SelectedIndex = 1;
  387. eventFired = false;
  388. cb.Sorted = false;
  389. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
  390. Assert.AreEqual(true, eventFired, "#SSI6");
  391. }
  392. }
  393. [TestFixture]
  394. public class ComboBoxObjectCollectionTest
  395. {
  396. [Test]
  397. public void ComboBoxObjectCollectionPropertyTest ()
  398. {
  399. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  400. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  401. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  402. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  403. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  404. }
  405. [Test]
  406. public void AddTest ()
  407. {
  408. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  409. col.Add ("Item1");
  410. col.Add ("Item2");
  411. Assert.AreEqual (2, col.Count, "#C1");
  412. }
  413. [Test]
  414. public void ClearTest ()
  415. {
  416. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  417. col.Add ("Item1");
  418. col.Add ("Item2");
  419. col.Clear ();
  420. Assert.AreEqual (0, col.Count, "#D1");
  421. }
  422. [Test]
  423. public void ContainsTest ()
  424. {
  425. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  426. object obj = "Item1";
  427. col.Add (obj);
  428. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  429. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  430. }
  431. [Test]
  432. public void IndexOfTest ()
  433. {
  434. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  435. col.Add ("Item1");
  436. col.Add ("Item2");
  437. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  438. }
  439. [Test]
  440. public void RemoveTest ()
  441. {
  442. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  443. col.Add ("Item1");
  444. col.Add ("Item2");
  445. col.Remove ("Item1");
  446. Assert.AreEqual (1, col.Count, "#G1");
  447. }
  448. [Test]
  449. public void RemoveAtTest ()
  450. {
  451. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  452. col.Add ("Item1");
  453. col.Add ("Item2");
  454. col.RemoveAt (0);
  455. Assert.AreEqual (1, col.Count, "#H1");
  456. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  457. }
  458. [Test]
  459. [ExpectedException (typeof (ArgumentNullException))]
  460. public void AddNullTest ()
  461. {
  462. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  463. col.Add (null);
  464. }
  465. [Test]
  466. [ExpectedException (typeof (ArgumentNullException))]
  467. public void AddRangeNullTest ()
  468. {
  469. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  470. col.AddRange (null);
  471. }
  472. [Test]
  473. [ExpectedException (typeof (ArgumentNullException))]
  474. public void ContainsNullTest ()
  475. {
  476. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  477. col.Contains (null);
  478. }
  479. [Test]
  480. [ExpectedException (typeof (ArgumentNullException))]
  481. public void IndexOfNullTest ()
  482. {
  483. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  484. col.IndexOf (null);
  485. }
  486. [Test]
  487. [ExpectedException (typeof (ArgumentNullException))]
  488. public void InsertNullTest ()
  489. {
  490. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  491. col.Add ("Item1");
  492. col.Insert (0, null);
  493. }
  494. [Test]
  495. [ExpectedException (typeof (ArgumentNullException))]
  496. public void IndexerNullTest ()
  497. {
  498. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  499. col.Add ("Item1");
  500. col [0] = null;
  501. }
  502. }
  503. }