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
50 listBox = new ListBox();
55 public void ListBoxPropertyTest ()
57 Assert.AreEqual (0, listBox.ColumnWidth, "#1");
58 Assert.AreEqual (DrawMode.Normal, listBox.DrawMode, "#2");
59 Assert.AreEqual (0, listBox.HorizontalExtent, "#3");
60 Assert.AreEqual (false, listBox.HorizontalScrollbar, "#4");
61 Assert.AreEqual (true, listBox.IntegralHeight, "#5");
62 //Assert.AreEqual (13, listBox.ItemHeight, "#6"); // Note: Item height depends on the current font.
63 listBox.Items.Add ("a");
64 listBox.Items.Add ("b");
65 listBox.Items.Add ("c");
66 Assert.AreEqual (3, listBox.Items.Count, "#7");
67 Assert.AreEqual (false, listBox.MultiColumn, "#8");
68 //Assert.AreEqual (46, listBox.PreferredHeight, "#9"); // Note: Item height depends on the current font.
69 //Assert.AreEqual (RightToLeft.No , listBox.RightToLeft, "#10"); // Depends on Windows version
70 Assert.AreEqual (false, listBox.ScrollAlwaysVisible, "#11");
71 Assert.AreEqual (-1, listBox.SelectedIndex, "#12");
72 listBox.SetSelected (2,true);
73 Assert.AreEqual (2, listBox.SelectedIndices[0], "#13");
74 Assert.AreEqual ("c", listBox.SelectedItem, "#14");
75 Assert.AreEqual ("c", listBox.SelectedItems[0], "#15");
76 Assert.AreEqual (SelectionMode.One, listBox.SelectionMode, "#16");
77 listBox.SetSelected (2,false);
78 Assert.AreEqual (false, listBox.Sorted, "#17");
79 Assert.AreEqual ("", listBox.Text, "#18");
80 Assert.AreEqual (0, listBox.TopIndex, "#19");
81 Assert.AreEqual (true, listBox.UseTabStops, "#20");
85 public void BeginEndUpdateTest ()
88 listBox.Items.Add ("A");
89 listBox.Visible = true;
90 form.Controls.Add (listBox);
91 listBox.BeginUpdate ();
92 for (int x = 1; x < 5000; x++)
94 listBox.Items.Add ("Item " + x.ToString ());
97 listBox.SetSelected (1, true);
98 listBox.SetSelected (3, true);
99 Assert.AreEqual (true, listBox.SelectedItems.Contains ("Item 3"), "#21");
103 public void ClearSelectedTest ()
106 listBox.Items.Add ("A");
107 listBox.Visible = true;
108 form.Controls.Add (listBox);
109 listBox.SetSelected (0, true);
110 Assert.AreEqual ("A", listBox.SelectedItems [0].ToString (),"#22");
111 listBox.ClearSelected ();
112 Assert.AreEqual (0, listBox.SelectedItems.Count,"#23");
115 [Ignore ("It depends on user system settings")]
116 public void GetItemHeightTest ()
118 listBox.Visible = true;
119 form.Controls.Add (listBox);
120 listBox.Items.Add ("A");
121 Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
124 [Ignore ("It depends on user system settings")]
125 public void GetItemRectangleTest ()
128 listBox.Visible = true;
129 form.Controls.Add (listBox);
130 listBox.Items.Add ("A");
131 Assert.AreEqual (new Rectangle(0,0,116,13), listBox.GetItemRectangle (0), "#29");
135 public void GetSelectedTest ()
137 listBox.Items.Add ("A");
138 listBox.Items.Add ("B");
139 listBox.Items.Add ("C");
140 listBox.Items.Add ("D");
141 listBox.Sorted = true;
142 listBox.SetSelected (0,true);
143 listBox.SetSelected (2,true);
145 Assert.AreEqual (true, listBox.GetSelected (0), "#30");
146 listBox.SetSelected (2,false);
147 Assert.AreEqual (false, listBox.GetSelected (2), "#31");
151 public void IndexFromPointTest ()
153 listBox.Items.Add ("A");
154 Point pt = new Point (100,100);
155 listBox.IndexFromPoint (pt);
156 Assert.AreEqual (-1, listBox.IndexFromPoint (100,100), "#32");
160 public void FindStringTest ()
162 listBox.FindString ("Hola", -5); // No exception, it's empty
163 int x = listBox.FindString ("Hello");
164 Assert.AreEqual (-1, x, "#19");
165 listBox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
166 String myString = "ABC";
167 x = listBox.FindString (myString);
168 Assert.AreEqual (3, x, "#191");
169 x = listBox.FindString (string.Empty);
170 Assert.AreEqual (0, x, "#192");
171 x = listBox.FindString ("NonExistant");
172 Assert.AreEqual (-1, x, "#193");
176 public void FindStringExactTest ()
178 listBox.FindStringExact ("Hola", -5); // No exception, it's empty
179 int x = listBox.FindStringExact ("Hello");
180 Assert.AreEqual (-1, x, "#20");
181 listBox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
182 String myString = "ABC";
183 x = listBox.FindStringExact (myString);
184 Assert.AreEqual (1, x, "#201");
185 x = listBox.FindStringExact (string.Empty);
186 Assert.AreEqual (-1, x, "#202");
187 x = listBox.FindStringExact ("NonExistant");
188 Assert.AreEqual (-1, x, "#203");
196 [ExpectedException (typeof (InvalidEnumArgumentException))]
197 public void BorderStyleException ()
199 listBox.BorderStyle = (BorderStyle) 10;
203 [ExpectedException (typeof (ArgumentException))]
204 public void ColumnWidthException ()
206 listBox.ColumnWidth = -1;
210 [ExpectedException (typeof (InvalidEnumArgumentException))]
211 public void DrawModeException ()
213 listBox.DrawMode = (DrawMode) 10;
217 [ExpectedException (typeof (ArgumentException))]
218 public void DrawModeAndMultiColumnException ()
220 listBox.MultiColumn = true;
221 listBox.DrawMode = DrawMode.OwnerDrawVariable;
225 [ExpectedException (typeof (ArgumentOutOfRangeException))]
226 public void ItemHeightException ()
228 listBox.ItemHeight = 256;
232 [ExpectedException (typeof (ArgumentOutOfRangeException))]
233 public void SelectedIndexException ()
235 listBox.SelectedIndex = -2;
239 [ExpectedException (typeof (ArgumentException))]
240 public void SelectedIndexModeNoneException ()
242 listBox.SelectionMode = SelectionMode.None;
243 listBox.SelectedIndex = -1;
247 [ExpectedException (typeof (InvalidEnumArgumentException))]
248 public void SelectionModeException ()
250 listBox.SelectionMode = (SelectionMode) 10;
254 public void SelectedValueNull()
256 listBox.SelectedValue = null;
260 public void SelectedValueEmptyString()
262 listBox.SelectedValue = String.Empty;
268 private bool eventFired;
270 private void GenericHandler (object sender, EventArgs e)
279 public class ListBoxObjectCollectionTest
281 ListBox.ObjectCollection col;
286 col = new ListBox.ObjectCollection (new ListBox ());
290 public void DefaultProperties ()
292 Assert.AreEqual (false, col.IsReadOnly, "#B1");
293 Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
294 Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
295 Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
296 Assert.AreEqual (0, col.Count);
300 public void AddTest ()
304 Assert.AreEqual (2, col.Count, "#C1");
308 public void ClearTest ()
313 Assert.AreEqual (0, col.Count, "#D1");
317 public void ContainsTest ()
319 object obj = "Item1";
321 Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
322 Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
326 public void IndexOfTest ()
330 Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
334 public void RemoveTest ()
338 col.Remove ("Item1");
339 Assert.AreEqual (1, col.Count, "#G1");
343 public void RemoveAtTest ()
348 Assert.AreEqual (1, col.Count, "#H1");
349 Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
353 [ExpectedException (typeof (ArgumentNullException))]
354 public void IndexerNullTest ()
361 [ExpectedException (typeof (ArgumentNullException))]
362 public void AddNullTest ()