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();
52 form.ShowInTaskbar = false;
56 public void TearDown()
62 public void ListBoxPropertyTest ()
64 Assert.AreEqual (0, listBox.ColumnWidth, "#1");
65 Assert.AreEqual (DrawMode.Normal, listBox.DrawMode, "#2");
66 Assert.AreEqual (0, listBox.HorizontalExtent, "#3");
67 Assert.AreEqual (false, listBox.HorizontalScrollbar, "#4");
68 Assert.AreEqual (true, listBox.IntegralHeight, "#5");
69 //Assert.AreEqual (13, listBox.ItemHeight, "#6"); // Note: Item height depends on the current font.
70 listBox.Items.Add ("a");
71 listBox.Items.Add ("b");
72 listBox.Items.Add ("c");
73 Assert.AreEqual (3, listBox.Items.Count, "#7");
74 Assert.AreEqual (false, listBox.MultiColumn, "#8");
75 //Assert.AreEqual (46, listBox.PreferredHeight, "#9"); // Note: Item height depends on the current font.
76 //Assert.AreEqual (RightToLeft.No , listBox.RightToLeft, "#10"); // Depends on Windows version
77 Assert.AreEqual (false, listBox.ScrollAlwaysVisible, "#11");
78 Assert.AreEqual (-1, listBox.SelectedIndex, "#12");
79 listBox.SetSelected (2,true);
80 Assert.AreEqual (2, listBox.SelectedIndices[0], "#13");
81 Assert.AreEqual ("c", listBox.SelectedItem, "#14");
82 Assert.AreEqual ("c", listBox.SelectedItems[0], "#15");
83 Assert.AreEqual (SelectionMode.One, listBox.SelectionMode, "#16");
84 listBox.SetSelected (2,false);
85 Assert.AreEqual (false, listBox.Sorted, "#17");
86 Assert.AreEqual ("", listBox.Text, "#18");
87 Assert.AreEqual (0, listBox.TopIndex, "#19");
88 Assert.AreEqual (true, listBox.UseTabStops, "#20");
92 public void BeginEndUpdateTest ()
95 listBox.Items.Add ("A");
96 listBox.Visible = true;
97 form.Controls.Add (listBox);
98 listBox.BeginUpdate ();
99 for (int x = 1; x < 5000; x++)
101 listBox.Items.Add ("Item " + x.ToString ());
103 listBox.EndUpdate ();
104 listBox.SetSelected (1, true);
105 listBox.SetSelected (3, true);
106 Assert.AreEqual (true, listBox.SelectedItems.Contains ("Item 3"), "#21");
110 public void ClearSelectedTest ()
113 listBox.Items.Add ("A");
114 listBox.Visible = true;
115 form.Controls.Add (listBox);
116 listBox.SetSelected (0, true);
117 Assert.AreEqual ("A", listBox.SelectedItems [0].ToString (),"#22");
118 listBox.ClearSelected ();
119 Assert.AreEqual (0, listBox.SelectedItems.Count,"#23");
122 [Ignore ("It depends on user system settings")]
123 public void GetItemHeightTest ()
125 listBox.Visible = true;
126 form.Controls.Add (listBox);
127 listBox.Items.Add ("A");
128 Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
131 [Ignore ("It depends on user system settings")]
132 public void GetItemRectangleTest ()
135 listBox.Visible = true;
136 form.Controls.Add (listBox);
137 listBox.Items.Add ("A");
138 Assert.AreEqual (new Rectangle(0,0,116,13), listBox.GetItemRectangle (0), "#29");
142 public void GetSelectedTest ()
144 listBox.Items.Add ("A");
145 listBox.Items.Add ("B");
146 listBox.Items.Add ("C");
147 listBox.Items.Add ("D");
148 listBox.Sorted = true;
149 listBox.SetSelected (0,true);
150 listBox.SetSelected (2,true);
152 Assert.AreEqual (true, listBox.GetSelected (0), "#30");
153 listBox.SetSelected (2,false);
154 Assert.AreEqual (false, listBox.GetSelected (2), "#31");
158 public void IndexFromPointTest ()
160 listBox.Items.Add ("A");
161 Point pt = new Point (100,100);
162 listBox.IndexFromPoint (pt);
163 Assert.AreEqual (-1, listBox.IndexFromPoint (100,100), "#32");
167 public void FindStringTest ()
169 listBox.FindString ("Hola", -5); // No exception, it's empty
170 int x = listBox.FindString ("Hello");
171 Assert.AreEqual (-1, x, "#19");
172 listBox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
173 String myString = "ABC";
174 x = listBox.FindString (myString);
175 Assert.AreEqual (3, x, "#191");
176 x = listBox.FindString (string.Empty);
177 Assert.AreEqual (0, x, "#192");
178 x = listBox.FindString ("NonExistant");
179 Assert.AreEqual (-1, x, "#193");
183 public void FindStringExactTest ()
185 listBox.FindStringExact ("Hola", -5); // No exception, it's empty
186 int x = listBox.FindStringExact ("Hello");
187 Assert.AreEqual (-1, x, "#20");
188 listBox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
189 String myString = "ABC";
190 x = listBox.FindStringExact (myString);
191 Assert.AreEqual (1, x, "#201");
192 x = listBox.FindStringExact (string.Empty);
193 Assert.AreEqual (-1, x, "#202");
194 x = listBox.FindStringExact ("NonExistant");
195 Assert.AreEqual (-1, x, "#203");
203 [ExpectedException (typeof (InvalidEnumArgumentException))]
204 public void BorderStyleException ()
206 listBox.BorderStyle = (BorderStyle) 10;
210 [ExpectedException (typeof (ArgumentException))]
211 public void ColumnWidthException ()
213 listBox.ColumnWidth = -1;
217 [ExpectedException (typeof (InvalidEnumArgumentException))]
218 public void DrawModeException ()
220 listBox.DrawMode = (DrawMode) 10;
224 [ExpectedException (typeof (ArgumentException))]
225 public void DrawModeAndMultiColumnException ()
227 listBox.MultiColumn = true;
228 listBox.DrawMode = DrawMode.OwnerDrawVariable;
232 [ExpectedException (typeof (ArgumentOutOfRangeException))]
233 public void ItemHeightException ()
235 listBox.ItemHeight = 256;
239 [ExpectedException (typeof (ArgumentOutOfRangeException))]
240 public void SelectedIndexException ()
242 listBox.SelectedIndex = -2;
246 [ExpectedException (typeof (ArgumentOutOfRangeException))]
247 public void SelectedIndexException2 ()
249 listBox.SelectedIndex = listBox.Items.Count;
253 [ExpectedException (typeof (ArgumentException))]
254 public void SelectedIndexModeNoneException ()
256 listBox.SelectionMode = SelectionMode.None;
257 listBox.SelectedIndex = -1;
261 [ExpectedException (typeof (InvalidEnumArgumentException))]
262 public void SelectionModeException ()
264 listBox.SelectionMode = (SelectionMode) 10;
268 public void SelectedValueNull()
270 listBox.Items.Clear ();
272 listBox.Items.Add ("A");
273 listBox.Items.Add ("B");
274 listBox.Items.Add ("C");
275 listBox.Items.Add ("D");
277 listBox.SelectedIndex = 2;
278 listBox.SelectedValue = null;
279 Assert.AreEqual (listBox.SelectedIndex, 2);
283 public void SelectedValueEmptyString()
285 listBox.Items.Clear ();
287 listBox.Items.Add ("A");
288 listBox.Items.Add ("B");
289 listBox.Items.Add ("C");
290 listBox.Items.Add ("D");
292 listBox.SelectedIndex = 2;
293 listBox.SelectedValue = null;
294 Assert.AreEqual (listBox.SelectedIndex, 2);
300 private bool eventFired;
302 private void GenericHandler (object sender, EventArgs e)
311 public class ListBoxObjectCollectionTest
313 ListBox.ObjectCollection col;
318 col = new ListBox.ObjectCollection (new ListBox ());
322 public void DefaultProperties ()
324 Assert.AreEqual (false, col.IsReadOnly, "#B1");
325 Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
326 Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
327 Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
328 Assert.AreEqual (0, col.Count);
332 public void AddTest ()
336 Assert.AreEqual (2, col.Count, "#C1");
340 public void ClearTest ()
345 Assert.AreEqual (0, col.Count, "#D1");
349 public void ContainsTest ()
351 object obj = "Item1";
353 Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
354 Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
358 public void IndexOfTest ()
362 Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
366 public void RemoveTest ()
370 col.Remove ("Item1");
371 Assert.AreEqual (1, col.Count, "#G1");
375 public void RemoveAtTest ()
380 Assert.AreEqual (1, col.Count, "#H1");
381 Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
385 [ExpectedException (typeof (ArgumentNullException))]
386 public void IndexerNullTest ()
393 [ExpectedException (typeof (ArgumentNullException))]
394 public void AddNullTest ()