ComboBoxTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.Visible = true;
  68. ComboBox cmbbox = new ComboBox ();
  69. cmbbox.Items.Add ("A");
  70. cmbbox.Visible = true;
  71. myform.Controls.Add (cmbbox);
  72. cmbbox.BeginUpdate ();
  73. for (int x = 1 ; x < 5000 ; x++) {
  74. cmbbox.Items.Add ("Item " + x.ToString ());
  75. }
  76. cmbbox.EndUpdate ();
  77. }
  78. [Test]
  79. public void FindStringTest ()
  80. {
  81. ComboBox cmbbox = new ComboBox ();
  82. cmbbox.FindString ("Hola", -5); // No exception, it's empty
  83. int x = cmbbox.FindString ("Hello");
  84. Assert.AreEqual (-1, x, "#19");
  85. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  86. String myString = "ABC";
  87. x = cmbbox.FindString (myString);
  88. Assert.AreEqual (3, x, "#191");
  89. x = cmbbox.FindString (string.Empty);
  90. Assert.AreEqual (0, x, "#192");
  91. x = cmbbox.FindString ("NonExistant");
  92. Assert.AreEqual (-1, x, "#193");
  93. }
  94. [Test]
  95. public void FindStringExactTest ()
  96. {
  97. ComboBox cmbbox = new ComboBox ();
  98. cmbbox.FindStringExact ("Hola", -5); // No exception, it's empty
  99. int x = cmbbox.FindStringExact ("Hello");
  100. Assert.AreEqual (-1, x, "#20");
  101. cmbbox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
  102. String myString = "ABC";
  103. x = cmbbox.FindStringExact (myString);
  104. Assert.AreEqual (1, x, "#201");
  105. x = cmbbox.FindStringExact (string.Empty);
  106. Assert.AreEqual (-1, x, "#202");
  107. x = cmbbox.FindStringExact ("NonExistant");
  108. Assert.AreEqual (-1, x, "#203");
  109. }
  110. [Test]
  111. public void GetItemHeightTest ()
  112. {
  113. ComboBox cmbbox = new ComboBox ();
  114. cmbbox.Items.Add ("ABC");
  115. cmbbox.Items.Add ("BCD");
  116. cmbbox.Items.Add ("DEF");
  117. int x = -1;
  118. x = cmbbox.GetItemHeight (x);
  119. Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
  120. }
  121. //
  122. // Exceptions
  123. //
  124. [Test]
  125. [ExpectedException (typeof (InvalidEnumArgumentException))]
  126. public void DropDownStyleException ()
  127. {
  128. ComboBox cmbbox = new ComboBox ();
  129. cmbbox.DropDownStyle = (ComboBoxStyle) 10;
  130. }
  131. [Test]
  132. [ExpectedException (typeof (InvalidEnumArgumentException))]
  133. public void DrawModeException ()
  134. {
  135. ComboBox cmbbox = new ComboBox ();
  136. cmbbox.DrawMode = (DrawMode) 10;
  137. }
  138. [Test]
  139. [ExpectedException (typeof (ArgumentException))]
  140. public void DropDownWidthException ()
  141. {
  142. ComboBox cmbbox = new ComboBox ();
  143. cmbbox.DropDownWidth = 0;
  144. }
  145. [Test]
  146. [ExpectedException (typeof (ArgumentException))]
  147. public void ItemHeightException ()
  148. {
  149. ComboBox cmbbox = new ComboBox ();
  150. cmbbox.ItemHeight = -1;
  151. }
  152. [Test]
  153. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  154. public void SelectedIndexException ()
  155. {
  156. ComboBox cmbbox = new ComboBox ();
  157. cmbbox.SelectedIndex = -2;
  158. }
  159. [Test]
  160. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  161. public void FindStringExactMinException ()
  162. {
  163. ComboBox cmbbox = new ComboBox ();
  164. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  165. cmbbox.FindStringExact ("Hola", -2);
  166. }
  167. [Test]
  168. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  169. public void FindStringExactMaxException ()
  170. {
  171. ComboBox cmbbox = new ComboBox ();
  172. cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  173. cmbbox.FindStringExact ("Hola", 3);
  174. }
  175. //
  176. // Events
  177. //
  178. private bool eventFired;
  179. private DrawItemEventArgs drawItemsArgs;
  180. private void DrawItemEventH (object sender, DrawItemEventArgs e)
  181. {
  182. eventFired = true;
  183. drawItemsArgs = e;
  184. }
  185. private void GenericHandler (object sender, EventArgs e)
  186. {
  187. eventFired = true;
  188. }
  189. [Ignore ("Bugs in X11 prevent this test to run properly")]
  190. public void DrawItemEventTest ()
  191. {
  192. eventFired = false;
  193. drawItemsArgs = null;
  194. Form myform = new Form ();
  195. ComboBox cmbbox = new ComboBox ();
  196. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  197. cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
  198. cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
  199. myform.Controls.Add (cmbbox);
  200. cmbbox.Items.AddRange(new object[] {"Item1"});
  201. myform.Visible = true;
  202. cmbbox.Visible = true;
  203. cmbbox.Refresh ();
  204. Assert.AreEqual (true, eventFired, "DW1");
  205. Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
  206. }
  207. [Test]
  208. public void DropDownStyleEventTest ()
  209. {
  210. eventFired = false;
  211. ComboBox cmbbox = new ComboBox ();
  212. cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
  213. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  214. Assert.AreEqual (true, eventFired, "DI1");
  215. }
  216. [Test]
  217. public void SelectedIndextTest ()
  218. {
  219. eventFired = false;
  220. ComboBox cmbbox = new ComboBox ();
  221. cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
  222. cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
  223. cmbbox.SelectedIndex = 1;
  224. Assert.AreEqual (true, eventFired, "SI1");
  225. }
  226. }
  227. [TestFixture]
  228. public class ComboBoxObjectCollectionTest
  229. {
  230. [Test]
  231. public void ComboBoxObjectCollectionPropertyTest ()
  232. {
  233. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  234. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  235. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  236. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  237. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  238. }
  239. [Test]
  240. public void AddTest ()
  241. {
  242. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  243. col.Add ("Item1");
  244. col.Add ("Item2");
  245. Assert.AreEqual (2, col.Count, "#C1");
  246. }
  247. [Test]
  248. public void ClearTest ()
  249. {
  250. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  251. col.Add ("Item1");
  252. col.Add ("Item2");
  253. col.Clear ();
  254. Assert.AreEqual (0, col.Count, "#D1");
  255. }
  256. [Test]
  257. public void ContainsTest ()
  258. {
  259. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  260. object obj = "Item1";
  261. col.Add (obj);
  262. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  263. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  264. }
  265. [Test]
  266. public void IndexOfTest ()
  267. {
  268. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  269. col.Add ("Item1");
  270. col.Add ("Item2");
  271. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  272. }
  273. [Test]
  274. public void RemoveTest ()
  275. {
  276. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  277. col.Add ("Item1");
  278. col.Add ("Item2");
  279. col.Remove ("Item1");
  280. Assert.AreEqual (1, col.Count, "#G1");
  281. }
  282. [Test]
  283. public void RemoveAtTest ()
  284. {
  285. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  286. col.Add ("Item1");
  287. col.Add ("Item2");
  288. col.RemoveAt (0);
  289. Assert.AreEqual (1, col.Count, "#H1");
  290. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  291. }
  292. [Test]
  293. [ExpectedException (typeof (ArgumentNullException))]
  294. public void AddNullTest ()
  295. {
  296. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  297. col.Add (null);
  298. }
  299. [Test]
  300. [ExpectedException (typeof (ArgumentNullException))]
  301. public void AddRangeNullTest ()
  302. {
  303. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  304. col.AddRange (null);
  305. }
  306. [Test]
  307. [ExpectedException (typeof (ArgumentNullException))]
  308. public void ContainsNullTest ()
  309. {
  310. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  311. col.Contains (null);
  312. }
  313. [Test]
  314. [ExpectedException (typeof (ArgumentNullException))]
  315. public void IndexOfNullTest ()
  316. {
  317. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  318. col.IndexOf (null);
  319. }
  320. [Test]
  321. [ExpectedException (typeof (ArgumentNullException))]
  322. public void InsertNullTest ()
  323. {
  324. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  325. col.Add ("Item1");
  326. col.Insert (0, null);
  327. }
  328. [Test]
  329. [ExpectedException (typeof (ArgumentNullException))]
  330. public void IndexerNullTest ()
  331. {
  332. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  333. col.Add ("Item1");
  334. col [0] = null;
  335. }
  336. }
  337. }