2006-12-19 Daniel Nauck <dna@mono-project.de>
[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                         form.ShowInTaskbar = false;
53                 }
54
55                 [TearDown]
56                 public void TearDown()
57                 {
58                         form.Dispose ();
59                 }
60
61                 [Test]
62                 public void ListBoxPropertyTest ()
63                 {
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");
89                 }
90
91                 [Test]
92                 public void BeginEndUpdateTest ()
93                 {
94                         form.Visible = true;
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++)
100                         {
101                                 listBox.Items.Add ("Item " + x.ToString ());
102                         }
103                         listBox.EndUpdate ();
104                         listBox.SetSelected (1, true);
105                         listBox.SetSelected (3, true);
106                         Assert.AreEqual (true, listBox.SelectedItems.Contains ("Item 3"), "#21");
107                 }
108
109                 [Test]
110                 public void ClearSelectedTest ()
111                 {
112                         form.Visible = true;
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");
120                 }
121
122                 [Ignore ("It depends on user system settings")]
123                 public void GetItemHeightTest ()
124                 {
125                         listBox.Visible = true;
126                         form.Controls.Add (listBox);
127                         listBox.Items.Add ("A");
128                         Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
129                 }
130
131                 [Ignore ("It depends on user system settings")]
132                 public void GetItemRectangleTest ()
133                 {
134                         form.Visible = true;
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");
139                 }
140
141                 [Test]
142                 public void GetSelectedTest ()
143                 {
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);
151                         listBox.TopIndex=0;
152                         Assert.AreEqual (true, listBox.GetSelected (0), "#30");
153                         listBox.SetSelected (2,false);
154                         Assert.AreEqual (false, listBox.GetSelected (2), "#31");
155                 }
156
157                 [Test]
158                 public void IndexFromPointTest ()
159                 {
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");
164                 }
165
166                 [Test]
167                 public void FindStringTest ()
168                 {
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");
180                 }
181
182                 [Test]
183                 public void FindStringExactTest ()
184                 {
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");
196                 }
197
198                 //
199                 // Exceptions
200                 //
201
202                 [Test]
203                 [ExpectedException (typeof (InvalidEnumArgumentException))]
204                 public void BorderStyleException ()
205                 {
206                         listBox.BorderStyle = (BorderStyle) 10;
207                 }
208
209                 [Test]
210                 [ExpectedException (typeof (ArgumentException))]
211                 public void ColumnWidthException ()
212                 {
213                         listBox.ColumnWidth = -1;
214                 }
215
216                 [Test]
217                 [ExpectedException (typeof (InvalidEnumArgumentException))]
218                 public void DrawModeException ()
219                 {
220                         listBox.DrawMode = (DrawMode) 10;
221                 }
222
223                 [Test]
224                 [ExpectedException (typeof (ArgumentException))]
225                 public void DrawModeAndMultiColumnException ()
226                 {
227                         listBox.MultiColumn = true;
228                         listBox.DrawMode = DrawMode.OwnerDrawVariable;
229                 }
230
231                 [Test]
232                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
233                 public void ItemHeightException ()
234                 {
235                         listBox.ItemHeight = 256;
236                 }
237
238                 [Test]
239                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
240                 public void SelectedIndexException ()
241                 {
242                         listBox.SelectedIndex = -2;
243                 }
244
245                 [Test]
246                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
247                 public void SelectedIndexException2 ()
248                 {
249                         listBox.SelectedIndex = listBox.Items.Count;
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (ArgumentException))]
254                 public void SelectedIndexModeNoneException ()
255                 {
256                         listBox.SelectionMode = SelectionMode.None;
257                         listBox.SelectedIndex = -1;
258                 }
259
260                 [Test]
261                 [ExpectedException (typeof (InvalidEnumArgumentException))]
262                 public void SelectionModeException ()
263                 {
264                         listBox.SelectionMode = (SelectionMode) 10;
265                 }
266
267                 [Test]
268                 public void SelectedValueNull()
269                 {
270                         listBox.Items.Clear ();
271
272                         listBox.Items.Add ("A");
273                         listBox.Items.Add ("B");
274                         listBox.Items.Add ("C");
275                         listBox.Items.Add ("D");
276
277                         listBox.SelectedIndex = 2;
278                         listBox.SelectedValue = null;
279                         Assert.AreEqual (listBox.SelectedIndex, 2);
280                 }
281
282                 [Test]
283                 public void SelectedValueEmptyString()
284                 {
285                         listBox.Items.Clear ();
286
287                         listBox.Items.Add ("A");
288                         listBox.Items.Add ("B");
289                         listBox.Items.Add ("C");
290                         listBox.Items.Add ("D");
291
292                         listBox.SelectedIndex = 2;
293                         listBox.SelectedValue = null;
294                         Assert.AreEqual (listBox.SelectedIndex, 2);
295                 }
296
297                 //
298                 // Events
299                 //
300                 private bool eventFired;
301
302                 private void GenericHandler (object sender,  EventArgs e)
303                 {
304                         eventFired = true;
305                 }
306
307
308         }
309
310         [TestFixture]
311         public class ListBoxObjectCollectionTest
312         {
313                 ListBox.ObjectCollection col;
314
315                 [SetUp]
316                 public void SetUp()
317                 {
318                         col = new ListBox.ObjectCollection (new ListBox ());
319                 }
320
321                 [Test]
322                 public void DefaultProperties ()
323                 {
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);
329                 }
330
331                 [Test]
332                 public void AddTest ()
333                 {
334                         col.Add ("Item1");
335                         col.Add ("Item2");
336                         Assert.AreEqual (2, col.Count, "#C1");
337                 }
338
339                 [Test]
340                 public void ClearTest ()
341                 {
342                         col.Add ("Item1");
343                         col.Add ("Item2");
344                         col.Clear ();
345                         Assert.AreEqual (0, col.Count, "#D1");
346                 }
347
348                 [Test]
349                 public void ContainsTest ()
350                 {
351                         object obj = "Item1";
352                         col.Add (obj);
353                         Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
354                         Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
355                 }
356
357                 [Test]
358                 public void IndexOfTest ()
359                 {
360                         col.Add ("Item1");
361                         col.Add ("Item2");
362                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
363                 }
364
365                 [Test]
366                 public void RemoveTest ()
367                 {
368                         col.Add ("Item1");
369                         col.Add ("Item2");
370                         col.Remove ("Item1");
371                         Assert.AreEqual (1, col.Count, "#G1");
372                 }
373
374                 [Test]
375                 public void RemoveAtTest ()
376                 {
377                         col.Add ("Item1");
378                         col.Add ("Item2");
379                         col.RemoveAt (0);
380                         Assert.AreEqual (1, col.Count, "#H1");
381                         Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
382                 }
383
384                 [Test]
385                 [ExpectedException (typeof (ArgumentNullException))]
386                 public void IndexerNullTest ()
387                 {
388                         col.Add ("Item1");
389                         col [0] = null;
390                 }
391
392                 [Test]
393                 [ExpectedException (typeof (ArgumentNullException))]
394                 public void AddNullTest ()
395                 {
396                         col.Add (null);
397                 }
398
399
400         }
401 }