2 // ComboBoxTest.cs: Test cases for ComboBox.
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
26 // Ritvik Mayank <mritvik@novell.com>
27 // Jordi Mas i Hernandez <jordi@ximian.com>
31 using System.Windows.Forms;
33 using System.Reflection;
34 using NUnit.Framework;
35 using System.Collections;
36 using System.ComponentModel;
38 namespace MonoTests.System.Windows.Forms
41 public class ComboBoxTest
44 public void ComboBoxPropertyTest ()
46 ComboBox mycmbbox = new ComboBox ();
47 Assert.AreEqual (DrawMode.Normal, mycmbbox.DrawMode, "#1");
48 Assert.AreEqual (ComboBoxStyle.DropDown, mycmbbox.DropDownStyle, "#2");
49 Assert.AreEqual (121, mycmbbox.DropDownWidth, "#3");
50 Assert.AreEqual (false, mycmbbox.DroppedDown, "#4");
51 Assert.AreEqual (true, mycmbbox.IntegralHeight, "#5");
52 Assert.AreEqual (0, mycmbbox.Items.Count, "#6");
53 //Assert.AreEqual (15, mycmbbox.ItemHeight, "#7"); // Note: Item height depends on the current font.
54 Assert.AreEqual (8, mycmbbox.MaxDropDownItems, "#8");
55 Assert.AreEqual (0, mycmbbox.MaxLength, "#9");
56 //Assert.AreEqual (20, mycmbbox.PreferredHeight, "#10");
57 // Note: Item height depends on the current font.
58 Assert.AreEqual (-1, mycmbbox.SelectedIndex, "#11");
59 Assert.AreEqual (null, mycmbbox.SelectedItem, "#12");
60 Assert.AreEqual ("", mycmbbox.SelectedText, "#13");
61 Assert.AreEqual (0, mycmbbox.SelectionLength, "#14");
62 Assert.AreEqual (0, mycmbbox.SelectionStart, "#15");
63 Assert.AreEqual (false, mycmbbox.Sorted, "#16");
64 Assert.AreEqual ("", mycmbbox.Text, "#17");
68 public void BeginEndUpdateTest ()
70 Form myform = new Form ();
71 myform.Visible = true;
72 ComboBox cmbbox = new ComboBox ();
73 cmbbox.Items.Add ("A");
74 cmbbox.Visible = true;
75 myform.Controls.Add (cmbbox);
76 cmbbox.BeginUpdate ();
77 for (int x = 1 ; x < 5000 ; x++) {
78 cmbbox.Items.Add ("Item " + x.ToString ());
84 public void FindStringTest ()
86 ComboBox cmbbox = new ComboBox ();
87 cmbbox.FindString ("Hola", -5); // No exception, it's empty
88 int x = cmbbox.FindString ("Hello");
89 Assert.AreEqual (-1, x, "#19");
90 cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
91 String myString = "ABC";
92 x = cmbbox.FindString (myString);
93 Assert.AreEqual (3, x, "#191");
94 x = cmbbox.FindString (string.Empty);
95 Assert.AreEqual (0, x, "#192");
96 x = cmbbox.FindString ("NonExistant");
97 Assert.AreEqual (-1, x, "#193");
101 public void FindStringExactTest ()
103 ComboBox cmbbox = new ComboBox ();
104 cmbbox.FindStringExact ("Hola", -5); // No exception, it's empty
105 int x = cmbbox.FindStringExact ("Hello");
106 Assert.AreEqual (-1, x, "#20");
107 cmbbox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
108 String myString = "ABC";
109 x = cmbbox.FindStringExact (myString);
110 Assert.AreEqual (1, x, "#201");
111 x = cmbbox.FindStringExact (string.Empty);
112 Assert.AreEqual (-1, x, "#202");
113 x = cmbbox.FindStringExact ("NonExistant");
114 Assert.AreEqual (-1, x, "#203");
118 public void GetItemHeightTest ()
120 ComboBox cmbbox = new ComboBox ();
121 cmbbox.Items.Add ("ABC");
122 cmbbox.Items.Add ("BCD");
123 cmbbox.Items.Add ("DEF");
125 x = cmbbox.GetItemHeight (x);
126 Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
135 [ExpectedException (typeof (InvalidEnumArgumentException))]
136 public void DropDownStyleException ()
138 ComboBox cmbbox = new ComboBox ();
139 cmbbox.DropDownStyle = (ComboBoxStyle) 10;
143 [ExpectedException (typeof (InvalidEnumArgumentException))]
144 public void DrawModeException ()
146 ComboBox cmbbox = new ComboBox ();
147 cmbbox.DrawMode = (DrawMode) 10;
151 [ExpectedException (typeof (ArgumentException))]
152 public void DropDownWidthException ()
154 ComboBox cmbbox = new ComboBox ();
155 cmbbox.DropDownWidth = 0;
159 [ExpectedException (typeof (ArgumentException))]
160 public void ItemHeightException ()
162 ComboBox cmbbox = new ComboBox ();
163 cmbbox.ItemHeight = -1;
167 [ExpectedException (typeof (ArgumentOutOfRangeException))]
168 public void SelectedIndexException ()
170 ComboBox cmbbox = new ComboBox ();
171 cmbbox.SelectedIndex = -2;
175 [ExpectedException (typeof (ArgumentOutOfRangeException))]
176 public void FindStringExactMinException ()
178 ComboBox cmbbox = new ComboBox ();
179 cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
180 cmbbox.FindStringExact ("Hola", -2);
184 [ExpectedException (typeof (ArgumentOutOfRangeException))]
185 public void FindStringExactMaxException ()
187 ComboBox cmbbox = new ComboBox ();
188 cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
189 cmbbox.FindStringExact ("Hola", 3);
195 private bool eventFired;
196 private DrawItemEventArgs drawItemsArgs;
197 private void DrawItemEventH (object sender, DrawItemEventArgs e)
203 private void GenericHandler (object sender, EventArgs e)
208 [Ignore ("Bugs in X11 prevent this test to run properly")]
209 public void DrawItemEventTest ()
212 drawItemsArgs = null;
213 Form myform = new Form ();
214 ComboBox cmbbox = new ComboBox ();
215 cmbbox.DropDownStyle = ComboBoxStyle.Simple;
216 cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
217 cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
219 myform.Controls.Add (cmbbox);
220 cmbbox.Items.AddRange(new object[] {"Item1"});
222 myform.Visible = true;
223 cmbbox.Visible = true;
226 Assert.AreEqual (true, eventFired, "DW1");
227 Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
231 public void DropDownStyleEventTest ()
234 ComboBox cmbbox = new ComboBox ();
235 cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
236 cmbbox.DropDownStyle = ComboBoxStyle.Simple;
238 Assert.AreEqual (true, eventFired, "DI1");
242 public void SelectedIndextTest ()
245 ComboBox cmbbox = new ComboBox ();
246 cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
247 cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
248 cmbbox.SelectedIndex = 1;
249 Assert.AreEqual (true, eventFired, "SI1");
255 public class ComboBoxObjectCollectionTest
258 public void ComboBoxObjectCollectionPropertyTest ()
260 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
261 Assert.AreEqual (false, col.IsReadOnly, "#B1");
262 Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
263 Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
264 Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
268 public void AddTest ()
270 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
273 Assert.AreEqual (2, col.Count, "#C1");
277 public void ClearTest ()
279 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
283 Assert.AreEqual (0, col.Count, "#D1");
287 public void ContainsTest ()
289 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
290 object obj = "Item1";
292 Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
293 Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
297 public void IndexOfTest ()
299 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
302 Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
306 public void RemoveTest ()
308 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
311 col.Remove ("Item1");
312 Assert.AreEqual (1, col.Count, "#G1");
316 public void RemoveAtTest ()
318 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
322 Assert.AreEqual (1, col.Count, "#H1");
323 Assert.AreEqual (true, col.Contains ("Item2"), "#H1");