New test.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ListBoxTest.cs
1 //
2 // ComboBoxTest.cs: Test cases for ComboBox.
3 //
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:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
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.
22 //
23 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
24 //
25 // Authors:
26 //      Ritvik Mayank <mritvik@novell.com>
27 //      Jordi Mas i Hernandez <jordi@ximian.com>
28 //
29
30
31 using System;
32 using System.Windows.Forms;
33 using System.Drawing;
34 using System.Reflection;
35 using NUnit.Framework;
36 using System.Collections;
37 using System.ComponentModel;
38
39 namespace MonoTests.System.Windows.Forms
40 {
41         [TestFixture]
42         public class ListBoxTest
43         {
44                 ListBox listBox;
45                 Form form;
46
47                 [SetUp]
48                 public void SetUp()
49                 {
50                         listBox = new ListBox();
51                         form = new Form();
52                 }
53
54                 [Test]
55                 public void ListBoxPropertyTest ()
56                 {
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");
82                 }
83
84                 [Test]
85                 public void BeginEndUpdateTest ()
86                 {
87                         form.Visible = true;
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++)
93                         {
94                                 listBox.Items.Add ("Item " + x.ToString ());
95                         }
96                         listBox.EndUpdate ();
97                         listBox.SetSelected (1, true);
98                         listBox.SetSelected (3, true);
99                         Assert.AreEqual (true, listBox.SelectedItems.Contains ("Item 3"), "#21");
100                 }
101
102                 [Test]
103                 public void ClearSelectedTest ()
104                 {
105                         form.Visible = true;
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");
113                 }
114
115                 [Ignore ("It depends on user system settings")]
116                 public void GetItemHeightTest ()
117                 {
118                         listBox.Visible = true;
119                         form.Controls.Add (listBox);
120                         listBox.Items.Add ("A");
121                         Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
122                 }
123
124                 [Ignore ("It depends on user system settings")]
125                 public void GetItemRectangleTest ()
126                 {
127                         form.Visible = true;
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");
132                 }
133
134                 [Test]
135                 public void GetSelectedTest ()
136                 {
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);
144                         listBox.TopIndex=0;
145                         Assert.AreEqual (true, listBox.GetSelected (0), "#30");
146                         listBox.SetSelected (2,false);
147                         Assert.AreEqual (false, listBox.GetSelected (2), "#31");
148                 }
149
150                 [Test]
151                 public void IndexFromPointTest ()
152                 {
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");
157                 }
158
159                 [Test]
160                 public void FindStringTest ()
161                 {
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");
173                 }
174
175                 [Test]
176                 public void FindStringExactTest ()
177                 {
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");
189                 }
190
191                 //
192                 // Exceptions
193                 //
194
195                 [Test]
196                 [ExpectedException (typeof (InvalidEnumArgumentException))]
197                 public void BorderStyleException ()
198                 {
199                         listBox.BorderStyle = (BorderStyle) 10;
200                 }
201
202                 [Test]
203                 [ExpectedException (typeof (ArgumentException))]
204                 public void ColumnWidthException ()
205                 {
206                         listBox.ColumnWidth = -1;
207                 }
208
209                 [Test]
210                 [ExpectedException (typeof (InvalidEnumArgumentException))]
211                 public void DrawModeException ()
212                 {
213                         listBox.DrawMode = (DrawMode) 10;
214                 }
215
216                 [Test]
217                 [ExpectedException (typeof (ArgumentException))]
218                 public void DrawModeAndMultiColumnException ()
219                 {
220                         listBox.MultiColumn = true;
221                         listBox.DrawMode = DrawMode.OwnerDrawVariable;
222                 }
223
224                 [Test]
225                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
226                 public void ItemHeightException ()
227                 {
228                         listBox.ItemHeight = 256;
229                 }
230
231                 [Test]
232                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
233                 public void SelectedIndexException ()
234                 {
235                         listBox.SelectedIndex = -2;
236                 }
237
238                 [Test]
239                 [ExpectedException (typeof (ArgumentException))]
240                 public void SelectedIndexModeNoneException ()
241                 {
242                         listBox.SelectionMode = SelectionMode.None;
243                         listBox.SelectedIndex = -1;
244                 }
245
246                 [Test]
247                 [ExpectedException (typeof (InvalidEnumArgumentException))]
248                 public void SelectionModeException ()
249                 {
250                         listBox.SelectionMode = (SelectionMode) 10;
251                 }
252
253                 [Test]
254                 public void SelectedValueNull()
255                 {
256                         listBox.SelectedValue = null;
257                 }
258
259                 [Test]
260                 public void SelectedValueEmptyString()
261                 {
262                         listBox.SelectedValue = String.Empty;
263                 }
264
265                 //
266                 // Events
267                 //
268                 private bool eventFired;
269
270                 private void GenericHandler (object sender,  EventArgs e)
271                 {
272                         eventFired = true;
273                 }
274
275
276         }
277
278         [TestFixture]
279         public class ListBoxObjectCollectionTest
280         {
281                 ListBox.ObjectCollection col;
282
283                 [SetUp]
284                 public void SetUp()
285                 {
286                         col = new ListBox.ObjectCollection (new ListBox ());
287                 }
288
289                 [Test]
290                 public void DefaultProperties ()
291                 {
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);
297                 }
298
299                 [Test]
300                 public void AddTest ()
301                 {
302                         col.Add ("Item1");
303                         col.Add ("Item2");
304                         Assert.AreEqual (2, col.Count, "#C1");
305                 }
306
307                 [Test]
308                 public void ClearTest ()
309                 {
310                         col.Add ("Item1");
311                         col.Add ("Item2");
312                         col.Clear ();
313                         Assert.AreEqual (0, col.Count, "#D1");
314                 }
315
316                 [Test]
317                 public void ContainsTest ()
318                 {
319                         object obj = "Item1";
320                         col.Add (obj);
321                         Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
322                         Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
323                 }
324
325                 [Test]
326                 public void IndexOfTest ()
327                 {
328                         col.Add ("Item1");
329                         col.Add ("Item2");
330                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
331                 }
332
333                 [Test]
334                 public void RemoveTest ()
335                 {
336                         col.Add ("Item1");
337                         col.Add ("Item2");
338                         col.Remove ("Item1");
339                         Assert.AreEqual (1, col.Count, "#G1");
340                 }
341
342                 [Test]
343                 public void RemoveAtTest ()
344                 {
345                         col.Add ("Item1");
346                         col.Add ("Item2");
347                         col.RemoveAt (0);
348                         Assert.AreEqual (1, col.Count, "#H1");
349                         Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
350                 }
351
352                 [Test]
353                 [ExpectedException (typeof (ArgumentNullException))]
354                 public void IndexerNullTest ()
355                 {
356                         col.Add ("Item1");
357                         col [0] = null;
358                 }
359
360                 [Test]
361                 [ExpectedException (typeof (ArgumentNullException))]
362                 public void AddNullTest ()
363                 {
364                         col.Add (null);
365                 }
366
367
368         }
369 }