ComboBoxTest.cs 9.2 KB

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