ComboBoxTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. Assert.AreEqual (ImageLayout.Tile, mycmbbox.BackgroundImageLayout, "#22");
  68. Assert.AreEqual (null, mycmbbox.DataSource, "#23");
  69. Assert.AreEqual (106, mycmbbox.DropDownHeight, "#24");
  70. Assert.AreEqual (FlatStyle.Standard, mycmbbox.FlatStyle, "#25");
  71. Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MaximumSize.ToString (), "#26");
  72. Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MinimumSize.ToString (), "#27");
  73. Assert.AreEqual ("{Left=0,Top=0,Right=0,Bottom=0}", mycmbbox.Padding.ToString (), "#28");
  74. #endif
  75. }
  76. #if NET_2_0
  77. [Test]
  78. public void ResetTextTest ()
  79. {
  80. ComboBox cmbbox = new ComboBox ();
  81. Assert.AreEqual ("", cmbbox.Text, "#01");
  82. cmbbox.Text = "abc";
  83. Assert.AreEqual ("abc", cmbbox.Text, "#02");
  84. cmbbox.ResetText ();
  85. Assert.AreEqual ("", cmbbox.Text, "#03");
  86. }
  87. [Test]
  88. public void BackgroundImageLayoutTest ()
  89. {
  90. ComboBox cmbbox = new ComboBox ();
  91. cmbbox.BackgroundImageLayout = ImageLayout.Stretch;
  92. Assert.AreEqual (ImageLayout.Stretch, cmbbox.BackgroundImageLayout, "#01");
  93. }
  94. [Test]
  95. public void DropDownHeightTest ()
  96. {
  97. ComboBox cmbbox = new ComboBox ();
  98. cmbbox.DropDownHeight = 225;
  99. Assert.AreEqual (225, cmbbox.DropDownHeight, "#01");
  100. cmbbox.DropDownHeight = 1;
  101. Assert.AreEqual (1, cmbbox.DropDownHeight, "#02");
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  105. public void DropDownHeightExceptionTest1 ()
  106. {
  107. ComboBox cmbbox = new ComboBox ();
  108. cmbbox.DropDownHeight = -225;
  109. }
  110. [Test]
  111. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  112. public void DropDownHeightExceptionTest2 ()
  113. {
  114. ComboBox cmbbox = new ComboBox ();
  115. cmbbox.DropDownHeight = 0;
  116. }
  117. [Test]
  118. public void FlatStyleTest ()
  119. {
  120. ComboBox cmbbox = new ComboBox ();
  121. cmbbox.FlatStyle = FlatStyle.Popup;
  122. Assert.AreEqual (FlatStyle.Popup, cmbbox.FlatStyle, "#01");
  123. }
  124. [Test]
  125. [ExpectedException (typeof (InvalidEnumArgumentException))]
  126. public void FlatStyleExceptionTest ()
  127. {
  128. ComboBox cmbbox = new ComboBox ();
  129. cmbbox.FlatStyle = (FlatStyle) (-123);
  130. }
  131. [Test]
  132. public void MaximumSizeTest ()
  133. {
  134. ComboBox cmbbox = new ComboBox ();
  135. cmbbox.MaximumSize = new Size (25, 25);
  136. Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MaximumSize.ToString (), "#01");
  137. cmbbox.MaximumSize = new Size (50, 75);
  138. Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MaximumSize.ToString (), "#02");
  139. }
  140. [Test]
  141. public void MinumumSizeTest ()
  142. {
  143. ComboBox cmbbox = new ComboBox ();
  144. cmbbox.MinimumSize = new Size (25, 25);
  145. Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MinimumSize.ToString (), "#1");
  146. cmbbox.MinimumSize = new Size (50, 75);
  147. Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MinimumSize.ToString (), "#2");
  148. }
  149. [Test]
  150. public void PaddingTest ()
  151. {
  152. ComboBox cmbbox = new ComboBox ();
  153. cmbbox.Padding = new Padding (21);
  154. Assert.AreEqual ("{Left=21,Top=21,Right=21,Bottom=21}", cmbbox.Padding.ToString (), "#01");
  155. }
  156. #endif
  157. [Test]
  158. public void BeginEndUpdateTest ()
  159. {
  160. Form myform = new Form ();
  161. myform.ShowInTaskbar = false;
  162. myform.Visible = true;
  163. ComboBox cmbbox = new ComboBox ();
  164. cmbbox.Items.Add ("A");
  165. cmbbox.Visible = true;
  166. myform.Controls.Add (cmbbox);
  167. cmbbox.BeginUpdate ();
  168. for (int x = 1 ; x < 5000 ; x++) {
  169. cmbbox.Items.Add ("Item " + x.ToString ());
  170. }
  171. cmbbox.EndUpdate ();
  172. myform.Dispose ();
  173. }
  174. [Test]
  175. public void FindStringTest ()
  176. {
  177. ComboBox cmbbox = new ComboBox ();
  178. cmbbox.FindString ("Hola", -5); // No exception, it's empty
  179. int x = cmbbox.FindString ("Hello");
  180. Assert.AreEqual (-1, x, "#19");
  181. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  182. String myString = "ABC";
  183. x = cmbbox.FindString (myString);
  184. Assert.AreEqual (3, x, "#191");
  185. x = cmbbox.FindString (string.Empty);
  186. Assert.AreEqual (0, x, "#192");
  187. x = cmbbox.FindString ("NonExistant");
  188. Assert.AreEqual (-1, x, "#193");
  189. }
  190. [Test]
  191. public void FindStringExactTest ()
  192. {
  193. ComboBox cmbbox = new ComboBox ();
  194. cmbbox.FindStringExact ("Hola", -5); // No exception, it's empty
  195. int x = cmbbox.FindStringExact ("Hello");
  196. Assert.AreEqual (-1, x, "#20");
  197. cmbbox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
  198. String myString = "ABC";
  199. x = cmbbox.FindStringExact (myString);
  200. Assert.AreEqual (1, x, "#201");
  201. x = cmbbox.FindStringExact (string.Empty);
  202. Assert.AreEqual (-1, x, "#202");
  203. x = cmbbox.FindStringExact ("NonExistant");
  204. Assert.AreEqual (-1, x, "#203");
  205. }
  206. [Test]
  207. public void GetItemHeightTest ()
  208. {
  209. ComboBox cmbbox = new ComboBox ();
  210. cmbbox.Items.Add ("ABC");
  211. cmbbox.Items.Add ("BCD");
  212. cmbbox.Items.Add ("DEF");
  213. int x = -1;
  214. x = cmbbox.GetItemHeight (x);
  215. Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
  216. }
  217. //
  218. // Exceptions
  219. //
  220. [Test]
  221. [ExpectedException (typeof (InvalidEnumArgumentException))]
  222. public void DropDownStyleException ()
  223. {
  224. ComboBox cmbbox = new ComboBox ();
  225. cmbbox.DropDownStyle = (ComboBoxStyle) 10;
  226. }
  227. [Test]
  228. [ExpectedException (typeof (InvalidEnumArgumentException))]
  229. public void DrawModeException ()
  230. {
  231. ComboBox cmbbox = new ComboBox ();
  232. cmbbox.DrawMode = (DrawMode) 10;
  233. }
  234. [Test]
  235. public void DropDownWidth ()
  236. {
  237. ComboBox cmbbox = new ComboBox ();
  238. Assert.AreEqual (121, cmbbox.DropDownWidth, "#A1");
  239. cmbbox.DropDownWidth = 1;
  240. Assert.AreEqual (1, cmbbox.DropDownWidth, "#A2");
  241. try {
  242. cmbbox.DropDownWidth = 0;
  243. Assert.Fail ("#B1");
  244. #if NET_2_0
  245. } catch (ArgumentOutOfRangeException ex) {
  246. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  247. Assert.IsNotNull (ex.Message, "#B3");
  248. Assert.IsNotNull (ex.ParamName, "#B4");
  249. Assert.AreEqual ("DropDownWidth", ex.ParamName, "#B5");
  250. Assert.IsNull (ex.InnerException, "#B6");
  251. }
  252. #else
  253. } catch (ArgumentException ex) {
  254. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  255. Assert.IsNotNull (ex.Message, "#B3");
  256. Assert.IsNull (ex.ParamName, "#B4");
  257. Assert.IsNull (ex.InnerException, "#B5");
  258. }
  259. #endif
  260. }
  261. [Test]
  262. public void ItemHeight ()
  263. {
  264. ComboBox cmbbox = new ComboBox ();
  265. Assert.IsTrue (cmbbox.ItemHeight >= 1, "#A1");
  266. cmbbox.ItemHeight = 1;
  267. Assert.AreEqual (1, cmbbox.ItemHeight, "#A2");
  268. try {
  269. cmbbox.ItemHeight = 0;
  270. Assert.Fail ("#B1");
  271. #if NET_2_0
  272. } catch (ArgumentOutOfRangeException ex) {
  273. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  274. Assert.IsNotNull (ex.Message, "#B3");
  275. Assert.IsNotNull (ex.ParamName, "#B4");
  276. Assert.AreEqual ("ItemHeight", ex.ParamName, "#B5");
  277. Assert.IsNull (ex.InnerException, "#B6");
  278. }
  279. #else
  280. } catch (ArgumentException ex) {
  281. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  282. Assert.IsNotNull (ex.Message, "#B3");
  283. Assert.IsNull (ex.ParamName, "#B4");
  284. Assert.IsNull (ex.InnerException, "#B5");
  285. }
  286. #endif
  287. }
  288. [Test]
  289. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  290. public void SelectedIndexException ()
  291. {
  292. ComboBox cmbbox = new ComboBox ();
  293. cmbbox.SelectedIndex = -2;
  294. }
  295. [Test]
  296. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  297. public void FindStringExactMinException ()
  298. {
  299. ComboBox cmbbox = new ComboBox ();
  300. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  301. cmbbox.FindStringExact ("Hola", -2);
  302. }
  303. [Test]
  304. #if ONLY_1_1
  305. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  306. #endif
  307. public void FindStringExactMaxException ()
  308. {
  309. ComboBox cmbbox = new ComboBox ();
  310. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  311. cmbbox.FindStringExact ("Hola", 3);
  312. }
  313. // 2.0 doesn't throw an exception until Item.Count instead of Item.Count - 1 like docs say
  314. [Test]
  315. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  316. public void FindStringExactMaxExceptionNet20 ()
  317. {
  318. ComboBox cmbbox = new ComboBox ();
  319. cmbbox.Items.AddRange (new object[] { "ACBD", "ABDC", "ACBD", "ABCD" });
  320. cmbbox.FindStringExact ("Hola", 4);
  321. }
  322. //
  323. // Events
  324. //
  325. private bool eventFired;
  326. private DrawItemEventArgs drawItemsArgs;
  327. private void DrawItemEventH (object sender, DrawItemEventArgs e)
  328. {
  329. eventFired = true;
  330. drawItemsArgs = e;
  331. }
  332. private void GenericHandler (object sender, EventArgs e)
  333. {
  334. eventFired = true;
  335. }
  336. [Ignore ("Bugs in X11 prevent this test to run properly")]
  337. public void DrawItemEventTest ()
  338. {
  339. eventFired = false;
  340. drawItemsArgs = null;
  341. Form myform = new Form ();
  342. myform.ShowInTaskbar = false;
  343. ComboBox cmbbox = new ComboBox ();
  344. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  345. cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
  346. cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
  347. myform.Controls.Add (cmbbox);
  348. cmbbox.Items.AddRange(new object[] {"Item1"});
  349. myform.Visible = true;
  350. cmbbox.Visible = true;
  351. cmbbox.Refresh ();
  352. Assert.AreEqual (true, eventFired, "DW1");
  353. Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
  354. myform.Dispose ();
  355. }
  356. [Test]
  357. public void DropDownStyleEventTest ()
  358. {
  359. eventFired = false;
  360. ComboBox cmbbox = new ComboBox ();
  361. cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
  362. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  363. Assert.AreEqual (true, eventFired, "DI1");
  364. }
  365. [Test]
  366. public void SelectedIndextTest ()
  367. {
  368. eventFired = false;
  369. ComboBox cmbbox = new ComboBox ();
  370. cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
  371. cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
  372. cmbbox.SelectedIndex = 1;
  373. Assert.AreEqual (true, eventFired, "SI1");
  374. }
  375. [Test]
  376. public void SelectionWithAdd()
  377. {
  378. ComboBox cb = new ComboBox();
  379. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  380. cb.Items.Add("Item 1");
  381. cb.Items.Add("Item 3");
  382. cb.SelectedIndex = 1;
  383. eventFired = false;
  384. cb.Items.Add("Item 4");
  385. Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
  386. Assert.AreEqual(false, eventFired, "SWA2");
  387. cb.Sorted = true;
  388. cb.SelectedIndex = 1;
  389. eventFired = false;
  390. cb.Items.Add("Item 5");
  391. Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
  392. Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
  393. Assert.AreEqual(false, eventFired, "SWA5");
  394. cb.SelectedIndex = 1;
  395. eventFired = false;
  396. cb.Items.Add("Item 2");
  397. Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
  398. Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
  399. Assert.AreEqual(false, eventFired, "SWA8");
  400. }
  401. [Test]
  402. public void SelectionWithInsert()
  403. {
  404. ComboBox cb = new ComboBox();
  405. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  406. cb.Items.Add("Item 1");
  407. cb.SelectedIndex = 0;
  408. eventFired = false;
  409. cb.Items.Insert(0, "Item 2");
  410. Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
  411. Assert.AreEqual(false, eventFired, "SWI2");
  412. }
  413. [Test]
  414. public void SelectionWithClear()
  415. {
  416. ComboBox cb = new ComboBox();
  417. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  418. cb.Items.Add("Item 1");
  419. cb.SelectedIndex = 0;
  420. eventFired = false;
  421. cb.Items.Clear();
  422. Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
  423. Assert.AreEqual(false, eventFired, "SWC2");
  424. }
  425. [Test]
  426. public void SortedTest()
  427. {
  428. ComboBox mycb = new ComboBox();
  429. Assert.AreEqual(false, mycb.Sorted, "#1");
  430. mycb.Items.Add("Item 2");
  431. mycb.Items.Add("Item 1");
  432. Assert.AreEqual("Item 2", mycb.Items[0], "#2");
  433. Assert.AreEqual("Item 1", mycb.Items[1], "#3");
  434. mycb.Sorted = true;
  435. Assert.AreEqual(true, mycb.Sorted, "#4");
  436. Assert.AreEqual("Item 1", mycb.Items[0], "#5");
  437. Assert.AreEqual("Item 2", mycb.Items[1], "#6");
  438. mycb.Sorted = false;
  439. Assert.AreEqual(false, mycb.Sorted, "#7");
  440. Assert.AreEqual("Item 1", mycb.Items[0], "#8");
  441. Assert.AreEqual("Item 2", mycb.Items[1], "#9");
  442. }
  443. [Test]
  444. public void SortedAddTest()
  445. {
  446. ComboBox mycb = new ComboBox();
  447. mycb.Items.Add("Item 2");
  448. mycb.Items.Add("Item 1");
  449. mycb.Sorted = true;
  450. Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
  451. Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
  452. }
  453. [Test]
  454. public void SortedInsertTest()
  455. {
  456. ComboBox mycb = new ComboBox();
  457. mycb.Items.Add("Item 2");
  458. mycb.Items.Add("Item 1");
  459. mycb.Sorted = true;
  460. mycb.Items.Insert (0, "Item 3");
  461. Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
  462. Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
  463. Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
  464. }
  465. [Test]
  466. public void SortedSelectionInteractions()
  467. {
  468. ComboBox cb = new ComboBox();
  469. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  470. cb.Items.Add("Item 1");
  471. cb.Items.Add("Item 2");
  472. cb.Items.Add("Item 3");
  473. cb.SelectedIndex = 1;
  474. eventFired = false;
  475. cb.Sorted = true;
  476. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
  477. Assert.AreEqual(true, eventFired, "#SSI2");
  478. cb.SelectedIndex = 1;
  479. eventFired = false;
  480. cb.Sorted = true;
  481. Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
  482. Assert.AreEqual(false, eventFired, "#SSI4");
  483. cb.SelectedIndex = 1;
  484. eventFired = false;
  485. cb.Sorted = false;
  486. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
  487. Assert.AreEqual(true, eventFired, "#SSI6");
  488. }
  489. }
  490. [TestFixture]
  491. public class ComboBoxObjectCollectionTest
  492. {
  493. [Test]
  494. public void ComboBoxObjectCollectionPropertyTest ()
  495. {
  496. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  497. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  498. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  499. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  500. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  501. }
  502. [Test]
  503. public void AddTest ()
  504. {
  505. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  506. col.Add ("Item1");
  507. col.Add ("Item2");
  508. Assert.AreEqual (2, col.Count, "#C1");
  509. }
  510. [Test]
  511. public void ClearTest ()
  512. {
  513. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  514. col.Add ("Item1");
  515. col.Add ("Item2");
  516. col.Clear ();
  517. Assert.AreEqual (0, col.Count, "#D1");
  518. }
  519. [Test]
  520. public void ContainsTest ()
  521. {
  522. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  523. object obj = "Item1";
  524. col.Add (obj);
  525. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  526. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  527. }
  528. [Test]
  529. public void IndexOfTest ()
  530. {
  531. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  532. col.Add ("Item1");
  533. col.Add ("Item2");
  534. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  535. }
  536. [Test]
  537. public void RemoveTest ()
  538. {
  539. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  540. col.Add ("Item1");
  541. col.Add ("Item2");
  542. col.Remove ("Item1");
  543. Assert.AreEqual (1, col.Count, "#G1");
  544. }
  545. [Test]
  546. public void RemoveAtTest ()
  547. {
  548. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  549. col.Add ("Item1");
  550. col.Add ("Item2");
  551. col.RemoveAt (0);
  552. Assert.AreEqual (1, col.Count, "#H1");
  553. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  554. }
  555. [Test]
  556. [ExpectedException (typeof (ArgumentNullException))]
  557. public void AddNullTest ()
  558. {
  559. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  560. col.Add (null);
  561. }
  562. [Test]
  563. [ExpectedException (typeof (ArgumentNullException))]
  564. public void AddRangeNullTest ()
  565. {
  566. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  567. col.AddRange (null);
  568. }
  569. [Test]
  570. [ExpectedException (typeof (ArgumentNullException))]
  571. public void ContainsNullTest ()
  572. {
  573. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  574. col.Contains (null);
  575. }
  576. [Test]
  577. [ExpectedException (typeof (ArgumentNullException))]
  578. public void IndexOfNullTest ()
  579. {
  580. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  581. col.IndexOf (null);
  582. }
  583. [Test]
  584. [ExpectedException (typeof (ArgumentNullException))]
  585. public void InsertNullTest ()
  586. {
  587. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  588. col.Add ("Item1");
  589. col.Insert (0, null);
  590. }
  591. [Test]
  592. [ExpectedException (typeof (ArgumentNullException))]
  593. public void IndexerNullTest ()
  594. {
  595. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  596. col.Add ("Item1");
  597. col [0] = null;
  598. }
  599. }
  600. }