ComboBoxTest.cs 15 KB

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