ComboBoxTest.cs 14 KB

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