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.ShowInTaskbar = false;
72 myform.Visible = true;
73 ComboBox cmbbox = new ComboBox ();
74 cmbbox.Items.Add ("A");
75 cmbbox.Visible = true;
76 myform.Controls.Add (cmbbox);
77 cmbbox.BeginUpdate ();
78 for (int x = 1 ; x < 5000 ; x++) {
79 cmbbox.Items.Add ("Item " + x.ToString ());
86 public void FindStringTest ()
88 ComboBox cmbbox = new ComboBox ();
89 cmbbox.FindString ("Hola", -5); // No exception, it's empty
90 int x = cmbbox.FindString ("Hello");
91 Assert.AreEqual (-1, x, "#19");
92 cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
93 String myString = "ABC";
94 x = cmbbox.FindString (myString);
95 Assert.AreEqual (3, x, "#191");
96 x = cmbbox.FindString (string.Empty);
97 Assert.AreEqual (0, x, "#192");
98 x = cmbbox.FindString ("NonExistant");
99 Assert.AreEqual (-1, x, "#193");
103 public void FindStringExactTest ()
105 ComboBox cmbbox = new ComboBox ();
106 cmbbox.FindStringExact ("Hola", -5); // No exception, it's empty
107 int x = cmbbox.FindStringExact ("Hello");
108 Assert.AreEqual (-1, x, "#20");
109 cmbbox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
110 String myString = "ABC";
111 x = cmbbox.FindStringExact (myString);
112 Assert.AreEqual (1, x, "#201");
113 x = cmbbox.FindStringExact (string.Empty);
114 Assert.AreEqual (-1, x, "#202");
115 x = cmbbox.FindStringExact ("NonExistant");
116 Assert.AreEqual (-1, x, "#203");
120 public void GetItemHeightTest ()
122 ComboBox cmbbox = new ComboBox ();
123 cmbbox.Items.Add ("ABC");
124 cmbbox.Items.Add ("BCD");
125 cmbbox.Items.Add ("DEF");
127 x = cmbbox.GetItemHeight (x);
128 Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
137 [ExpectedException (typeof (InvalidEnumArgumentException))]
138 public void DropDownStyleException ()
140 ComboBox cmbbox = new ComboBox ();
141 cmbbox.DropDownStyle = (ComboBoxStyle) 10;
145 [ExpectedException (typeof (InvalidEnumArgumentException))]
146 public void DrawModeException ()
148 ComboBox cmbbox = new ComboBox ();
149 cmbbox.DrawMode = (DrawMode) 10;
154 [ExpectedException (typeof (ArgumentOutOfRangeException))]
156 [ExpectedException (typeof (ArgumentException))]
158 public void DropDownWidthException ()
160 ComboBox cmbbox = new ComboBox ();
161 cmbbox.DropDownWidth = 0;
166 [ExpectedException (typeof (ArgumentOutOfRangeException))]
168 [ExpectedException (typeof (ArgumentException))]
170 public void ItemHeightException ()
172 ComboBox cmbbox = new ComboBox ();
173 cmbbox.ItemHeight = -1;
177 [ExpectedException (typeof (ArgumentOutOfRangeException))]
178 public void SelectedIndexException ()
180 ComboBox cmbbox = new ComboBox ();
181 cmbbox.SelectedIndex = -2;
185 [ExpectedException (typeof (ArgumentOutOfRangeException))]
186 public void FindStringExactMinException ()
188 ComboBox cmbbox = new ComboBox ();
189 cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
190 cmbbox.FindStringExact ("Hola", -2);
194 [ExpectedException (typeof (ArgumentOutOfRangeException))]
195 public void FindStringExactMaxException ()
197 ComboBox cmbbox = new ComboBox ();
198 cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
199 cmbbox.FindStringExact ("Hola", 3);
205 private bool eventFired;
206 private DrawItemEventArgs drawItemsArgs;
207 private void DrawItemEventH (object sender, DrawItemEventArgs e)
213 private void GenericHandler (object sender, EventArgs e)
218 [Ignore ("Bugs in X11 prevent this test to run properly")]
219 public void DrawItemEventTest ()
222 drawItemsArgs = null;
223 Form myform = new Form ();
224 myform.ShowInTaskbar = false;
225 ComboBox cmbbox = new ComboBox ();
226 cmbbox.DropDownStyle = ComboBoxStyle.Simple;
227 cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
228 cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
230 myform.Controls.Add (cmbbox);
231 cmbbox.Items.AddRange(new object[] {"Item1"});
233 myform.Visible = true;
234 cmbbox.Visible = true;
237 Assert.AreEqual (true, eventFired, "DW1");
238 Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
243 public void DropDownStyleEventTest ()
246 ComboBox cmbbox = new ComboBox ();
247 cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
248 cmbbox.DropDownStyle = ComboBoxStyle.Simple;
250 Assert.AreEqual (true, eventFired, "DI1");
254 public void SelectedIndextTest ()
257 ComboBox cmbbox = new ComboBox ();
258 cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
259 cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
260 cmbbox.SelectedIndex = 1;
261 Assert.AreEqual (true, eventFired, "SI1");
265 public void SelectionWithAdd()
267 ComboBox cb = new ComboBox();
268 cb.SelectedIndexChanged += new EventHandler(GenericHandler);
269 cb.Items.Add("Item 1");
270 cb.Items.Add("Item 3");
271 cb.SelectedIndex = 1;
273 cb.Items.Add("Item 4");
274 Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
275 Assert.AreEqual(false, eventFired, "SWA2");
277 cb.SelectedIndex = 1;
279 cb.Items.Add("Item 5");
280 Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
281 Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
282 Assert.AreEqual(false, eventFired, "SWA5");
283 cb.SelectedIndex = 1;
285 cb.Items.Add("Item 2");
286 Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
287 Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
288 Assert.AreEqual(false, eventFired, "SWA8");
292 public void SelectionWithInsert()
294 ComboBox cb = new ComboBox();
295 cb.SelectedIndexChanged += new EventHandler(GenericHandler);
296 cb.Items.Add("Item 1");
297 cb.SelectedIndex = 0;
299 cb.Items.Insert(0, "Item 2");
300 Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
301 Assert.AreEqual(false, eventFired, "SWI2");
305 public void SelectionWithClear()
307 ComboBox cb = new ComboBox();
308 cb.SelectedIndexChanged += new EventHandler(GenericHandler);
309 cb.Items.Add("Item 1");
310 cb.SelectedIndex = 0;
313 Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
314 Assert.AreEqual(false, eventFired, "SWC2");
318 public void SortedTest()
320 ComboBox mycb = new ComboBox();
321 Assert.AreEqual(false, mycb.Sorted, "#1");
322 mycb.Items.Add("Item 2");
323 mycb.Items.Add("Item 1");
324 Assert.AreEqual("Item 2", mycb.Items[0], "#2");
325 Assert.AreEqual("Item 1", mycb.Items[1], "#3");
327 Assert.AreEqual(true, mycb.Sorted, "#4");
328 Assert.AreEqual("Item 1", mycb.Items[0], "#5");
329 Assert.AreEqual("Item 2", mycb.Items[1], "#6");
331 Assert.AreEqual(false, mycb.Sorted, "#7");
332 Assert.AreEqual("Item 1", mycb.Items[0], "#8");
333 Assert.AreEqual("Item 2", mycb.Items[1], "#9");
337 public void SortedAddTest()
339 ComboBox mycb = new ComboBox();
340 mycb.Items.Add("Item 2");
341 mycb.Items.Add("Item 1");
343 Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
344 Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
348 public void SortedInsertTest()
350 ComboBox mycb = new ComboBox();
351 mycb.Items.Add("Item 2");
352 mycb.Items.Add("Item 1");
354 mycb.Items.Insert (0, "Item 3");
355 Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
356 Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
357 Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
361 public void SortedSelectionInteractions()
363 ComboBox cb = new ComboBox();
364 cb.SelectedIndexChanged += new EventHandler(GenericHandler);
365 cb.Items.Add("Item 1");
366 cb.Items.Add("Item 2");
367 cb.Items.Add("Item 3");
368 cb.SelectedIndex = 1;
371 Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
372 Assert.AreEqual(true, eventFired, "#SSI2");
373 cb.SelectedIndex = 1;
376 Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
377 Assert.AreEqual(false, eventFired, "#SSI4");
378 cb.SelectedIndex = 1;
381 Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
382 Assert.AreEqual(true, eventFired, "#SSI6");
387 public class ComboBoxObjectCollectionTest
390 public void ComboBoxObjectCollectionPropertyTest ()
392 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
393 Assert.AreEqual (false, col.IsReadOnly, "#B1");
394 Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
395 Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
396 Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
400 public void AddTest ()
402 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
405 Assert.AreEqual (2, col.Count, "#C1");
409 public void ClearTest ()
411 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
415 Assert.AreEqual (0, col.Count, "#D1");
419 public void ContainsTest ()
421 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
422 object obj = "Item1";
424 Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
425 Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
429 public void IndexOfTest ()
431 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
434 Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
438 public void RemoveTest ()
440 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
443 col.Remove ("Item1");
444 Assert.AreEqual (1, col.Count, "#G1");
448 public void RemoveAtTest ()
450 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
454 Assert.AreEqual (1, col.Count, "#H1");
455 Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
459 [ExpectedException (typeof (ArgumentNullException))]
460 public void AddNullTest ()
462 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
467 [ExpectedException (typeof (ArgumentNullException))]
468 public void AddRangeNullTest ()
470 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
475 [ExpectedException (typeof (ArgumentNullException))]
476 public void ContainsNullTest ()
478 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
483 [ExpectedException (typeof (ArgumentNullException))]
484 public void IndexOfNullTest ()
486 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
491 [ExpectedException (typeof (ArgumentNullException))]
492 public void InsertNullTest ()
494 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
496 col.Insert (0, null);
500 [ExpectedException (typeof (ArgumentNullException))]
501 public void IndexerNullTest ()
503 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());