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>
32 using System.Windows.Forms;
34 using System.Reflection;
35 using NUnit.Framework;
36 using System.Collections;
37 using System.ComponentModel;
39 namespace MonoTests.System.Windows.Forms
42 public class ListBoxTest
45 public void ListBoxPropertyTest ()
47 ListBox lb1 = new ListBox ();
48 Assert.AreEqual (0, lb1.ColumnWidth, "#1");
49 Assert.AreEqual (DrawMode.Normal, lb1.DrawMode, "#2");
50 Assert.AreEqual (0, lb1.HorizontalExtent, "#3");
51 Assert.AreEqual (false, lb1.HorizontalScrollbar, "#4");
52 Assert.AreEqual (true, lb1.IntegralHeight, "#5");
53 //Assert.AreEqual (13, lb1.ItemHeight, "#6"); // Note: Item height depends on the current font.
57 Assert.AreEqual (3, lb1.Items.Count, "#7");
58 Assert.AreEqual (false, lb1.MultiColumn, "#8");
59 //Assert.AreEqual (46, lb1.PreferredHeight, "#9"); // Note: Item height depends on the current font.
60 //Assert.AreEqual (RightToLeft.No , lb1.RightToLeft, "#10"); // Depends on Windows version
61 Assert.AreEqual (false, lb1.ScrollAlwaysVisible, "#11");
62 Assert.AreEqual (-1, lb1.SelectedIndex, "#12");
63 lb1.SetSelected (2,true);
64 Assert.AreEqual (2, lb1.SelectedIndices[0], "#13");
65 Assert.AreEqual ("c", lb1.SelectedItem, "#14");
66 Assert.AreEqual ("c", lb1.SelectedItems[0], "#15");
67 Assert.AreEqual (SelectionMode.One, lb1.SelectionMode, "#16");
68 lb1.SetSelected (2,false);
69 Assert.AreEqual (false, lb1.Sorted, "#17");
70 Assert.AreEqual ("", lb1.Text, "#18");
71 Assert.AreEqual (0, lb1.TopIndex, "#19");
72 Assert.AreEqual (true, lb1.UseTabStops, "#20");
76 public void BeginEndUpdateTest ()
80 ListBox lb1 = new ListBox ();
85 for (int x = 1; x < 5000; x++)
87 lb1.Items.Add ("Item " + x.ToString ());
90 lb1.SetSelected (1, true);
91 lb1.SetSelected (3, true);
92 Assert.AreEqual (true, lb1.SelectedItems.Contains ("Item 3"), "#21");
96 public void ClearSelectedTest ()
100 ListBox lb1 = new ListBox ();
103 f.Controls.Add (lb1);
104 lb1.SetSelected (0, true);
105 Assert.AreEqual ("A", lb1.SelectedItems [0].ToString (),"#22");
106 lb1.ClearSelected ();
107 Assert.AreEqual (0, lb1.SelectedItems.Count,"#23");
110 [Ignore ("It depends on user system settings")]
111 public void GetItemHeightTest ()
113 Form f = new Form ();
114 ListBox lb1 = new ListBox ();
116 f.Controls.Add (lb1);
118 Assert.AreEqual (13, lb1.GetItemHeight (0) , "#28");
121 [Ignore ("It depends on user system settings")]
122 public void GetItemRectangleTest ()
124 Form f = new Form ();
126 ListBox lb1 = new ListBox ();
128 f.Controls.Add (lb1);
130 Assert.AreEqual (new Rectangle(0,0,116,13), lb1.GetItemRectangle (0), "#29");
134 public void GetSelectedTest ()
136 ListBox lb1 = new ListBox ();
142 lb1.SetSelected (0,true);
143 lb1.SetSelected (2,true);
145 Assert.AreEqual (true, lb1.GetSelected (0), "#30");
146 lb1.SetSelected (2,false);
147 Assert.AreEqual (false, lb1.GetSelected (2), "#31");
151 public void IndexFromPointTest ()
153 ListBox lb1 = new ListBox ();
155 Point pt = new Point (100,100);
156 lb1.IndexFromPoint (pt);
157 Assert.AreEqual (-1, lb1.IndexFromPoint (100,100), "#32");
161 public void FindStringTest ()
163 ListBox cmbbox = new ListBox ();
164 cmbbox.FindString ("Hola", -5); // No exception, it's empty
165 int x = cmbbox.FindString ("Hello");
166 Assert.AreEqual (-1, x, "#19");
167 cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
168 String myString = "ABC";
169 x = cmbbox.FindString (myString);
170 Assert.AreEqual (3, x, "#191");
171 x = cmbbox.FindString (string.Empty);
172 Assert.AreEqual (0, x, "#192");
173 x = cmbbox.FindString ("NonExistant");
174 Assert.AreEqual (-1, x, "#193");
178 public void FindStringExactTest ()
180 ListBox cmbbox = new ListBox ();
181 cmbbox.FindStringExact ("Hola", -5); // No exception, it's empty
182 int x = cmbbox.FindStringExact ("Hello");
183 Assert.AreEqual (-1, x, "#20");
184 cmbbox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
185 String myString = "ABC";
186 x = cmbbox.FindStringExact (myString);
187 Assert.AreEqual (1, x, "#201");
188 x = cmbbox.FindStringExact (string.Empty);
189 Assert.AreEqual (-1, x, "#202");
190 x = cmbbox.FindStringExact ("NonExistant");
191 Assert.AreEqual (-1, x, "#203");
199 [ExpectedException (typeof (InvalidEnumArgumentException))]
200 public void BorderStyleException ()
202 ListBox lstbox = new ListBox ();
203 lstbox.BorderStyle = (BorderStyle) 10;
207 [ExpectedException (typeof (ArgumentException))]
208 public void ColumnWidthException ()
210 ListBox lstbox = new ListBox ();
211 lstbox.ColumnWidth = -1;
215 [ExpectedException (typeof (InvalidEnumArgumentException))]
216 public void DrawModeException ()
218 ListBox lstbox = new ListBox ();
219 lstbox.DrawMode = (DrawMode) 10;
223 [ExpectedException (typeof (ArgumentException))]
224 public void DrawModeAndMultiColumnException ()
226 ListBox lstbox = new ListBox ();
227 lstbox.MultiColumn = true;
228 lstbox.DrawMode = DrawMode.OwnerDrawVariable;
232 [ExpectedException (typeof (ArgumentOutOfRangeException))]
233 public void ItemHeightException ()
235 ListBox lstbox = new ListBox ();
236 lstbox.ItemHeight = 256;
240 [ExpectedException (typeof (ArgumentOutOfRangeException))]
241 public void SelectedIndexException ()
243 ListBox lstbox = new ListBox ();
244 lstbox.SelectedIndex = -2;
248 [ExpectedException (typeof (ArgumentException))]
249 public void SelectedIndexModeNoneException ()
251 ListBox lstbox = new ListBox ();
252 lstbox.SelectionMode = SelectionMode.None;
253 lstbox.SelectedIndex = -1;
257 [ExpectedException (typeof (InvalidEnumArgumentException))]
258 public void SelectionModeException ()
260 ListBox lstbox = new ListBox ();
261 lstbox.SelectionMode = (SelectionMode) 10;
267 private bool eventFired;
269 private void GenericHandler (object sender, EventArgs e)
278 public class ListBoxObjectCollectionTest
281 public void ComboBoxObjectCollectionPropertyTest ()
283 ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
284 Assert.AreEqual (false, col.IsReadOnly, "#B1");
285 Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
286 Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
287 Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
291 public void AddTest ()
293 ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
296 Assert.AreEqual (2, col.Count, "#C1");
300 public void ClearTest ()
302 ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
306 Assert.AreEqual (0, col.Count, "#D1");
310 public void ContainsTest ()
312 ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
313 object obj = "Item1";
315 Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
316 Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
320 public void IndexOfTest ()
322 ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
325 Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
329 public void RemoveTest ()
331 ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
334 col.Remove ("Item1");
335 Assert.AreEqual (1, col.Count, "#G1");
339 public void RemoveAtTest ()
341 ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
345 Assert.AreEqual (1, col.Count, "#H1");
346 Assert.AreEqual (true, col.Contains ("Item2"), "#H1");