merge -r 58060:58217
[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                 [Test]
45                 public void ListBoxPropertyTest ()
46                 {
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.
54                         lb1.Items.Add ("a");
55                         lb1.Items.Add ("b");
56                         lb1.Items.Add ("c");
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");
73                 }
74
75                 [Test]
76                 public void BeginEndUpdateTest ()
77                 {
78                         Form f = new Form ();
79                         f.Visible = true;
80                         ListBox lb1 = new ListBox ();
81                         lb1.Items.Add ("A");
82                         lb1.Visible = true;
83                         f.Controls.Add (lb1);
84                         lb1.BeginUpdate ();
85                         for (int x = 1; x < 5000; x++)
86                         {
87                                 lb1.Items.Add ("Item " + x.ToString ());
88                         }
89                         lb1.EndUpdate ();
90                         lb1.SetSelected (1, true);
91                         lb1.SetSelected (3, true);
92                         Assert.AreEqual (true, lb1.SelectedItems.Contains ("Item 3"), "#21");
93                 }
94
95                 [Test]
96                 public void ClearSelectedTest ()
97                 {
98                         Form f = new Form ();
99                         f.Visible = true;
100                         ListBox lb1 = new ListBox ();
101                         lb1.Items.Add ("A");
102                         lb1.Visible = true;
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");
108                 }
109
110                 [Ignore ("It depends on user system settings")]
111                 public void GetItemHeightTest ()
112                 {
113                         Form f = new Form ();
114                         ListBox lb1 = new ListBox ();
115                         lb1.Visible = true;
116                         f.Controls.Add (lb1);
117                         lb1.Items.Add ("A");
118                         Assert.AreEqual (13, lb1.GetItemHeight (0) , "#28");
119                 }
120
121                 [Ignore ("It depends on user system settings")]
122                 public void GetItemRectangleTest ()
123                 {
124                         Form f = new Form ();
125                         f.Visible = true;
126                         ListBox lb1 = new ListBox ();
127                         lb1.Visible = true;
128                         f.Controls.Add (lb1);
129                         lb1.Items.Add ("A");
130                         Assert.AreEqual (new Rectangle(0,0,116,13), lb1.GetItemRectangle (0), "#29");
131                 }
132
133                 [Test]
134                 public void GetSelectedTest ()
135                 {
136                         ListBox lb1 = new ListBox ();
137                         lb1.Items.Add ("A");
138                         lb1.Items.Add ("B");
139                         lb1.Items.Add ("C");
140                         lb1.Items.Add ("D");
141                         lb1.Sorted = true;
142                         lb1.SetSelected (0,true);
143                         lb1.SetSelected (2,true);
144                         lb1.TopIndex=0;
145                         Assert.AreEqual (true, lb1.GetSelected (0), "#30");
146                         lb1.SetSelected (2,false);
147                         Assert.AreEqual (false, lb1.GetSelected (2), "#31");
148                 }
149
150                 [Test]
151                 public void IndexFromPointTest ()
152                 {
153                         ListBox lb1 = new ListBox ();
154                         lb1.Items.Add ("A");
155                         Point pt = new Point (100,100);
156                                 lb1.IndexFromPoint (pt);
157                         Assert.AreEqual (-1, lb1.IndexFromPoint (100,100), "#32");
158                 }
159
160                 [Test]
161                 public void FindStringTest ()
162                 {
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");
175                 }
176
177                 [Test]
178                 public void FindStringExactTest ()
179                 {
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");
192                 }
193
194                 //
195                 // Exceptions
196                 //
197
198                 [Test]
199                 [ExpectedException (typeof (InvalidEnumArgumentException))]
200                 public void BorderStyleException ()
201                 {
202                         ListBox lstbox = new ListBox ();
203                         lstbox.BorderStyle = (BorderStyle) 10;
204                 }
205
206                 [Test]
207                 [ExpectedException (typeof (ArgumentException))]
208                 public void ColumnWidthException ()
209                 {
210                         ListBox lstbox = new ListBox ();
211                         lstbox.ColumnWidth = -1;
212                 }
213
214                 [Test]
215                 [ExpectedException (typeof (InvalidEnumArgumentException))]
216                 public void DrawModeException ()
217                 {
218                         ListBox lstbox = new ListBox ();
219                         lstbox.DrawMode = (DrawMode) 10;
220                 }
221
222                 [Test]
223                 [ExpectedException (typeof (ArgumentException))]
224                 public void DrawModeAndMultiColumnException ()
225                 {
226                         ListBox lstbox = new ListBox ();
227                         lstbox.MultiColumn = true;
228                         lstbox.DrawMode = DrawMode.OwnerDrawVariable;
229                 }
230
231                 [Test]
232                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
233                 public void ItemHeightException ()
234                 {
235                         ListBox lstbox = new ListBox ();
236                         lstbox.ItemHeight = 256;
237                 }
238
239                 [Test]
240                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
241                 public void SelectedIndexException ()
242                 {
243                         ListBox lstbox = new ListBox ();
244                         lstbox.SelectedIndex = -2;
245                 }
246
247                 [Test]
248                 [ExpectedException (typeof (ArgumentException))]
249                 public void SelectedIndexModeNoneException ()
250                 {
251                         ListBox lstbox = new ListBox ();
252                         lstbox.SelectionMode = SelectionMode.None;
253                         lstbox.SelectedIndex = -1;
254                 }
255
256                 [Test]
257                 [ExpectedException (typeof (InvalidEnumArgumentException))]
258                 public void SelectionModeException ()
259                 {
260                         ListBox lstbox = new ListBox ();
261                         lstbox.SelectionMode = (SelectionMode) 10;
262                 }
263
264                 //
265                 // Events
266                 //
267                 private bool eventFired;
268
269                 private void GenericHandler (object sender,  EventArgs e)
270                 {
271                         eventFired = true;
272                 }
273
274
275         }
276
277         [TestFixture]
278         public class ListBoxObjectCollectionTest
279         {
280                 [Test]
281                 public void ComboBoxObjectCollectionPropertyTest ()
282                 {
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");
288                 }
289
290                 [Test]
291                 public void AddTest ()
292                 {
293                         ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
294                         col.Add ("Item1");
295                         col.Add ("Item2");
296                         Assert.AreEqual (2, col.Count, "#C1");
297                 }
298
299                 [Test]
300                 public void ClearTest ()
301                 {
302                         ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
303                         col.Add ("Item1");
304                         col.Add ("Item2");
305                         col.Clear ();
306                         Assert.AreEqual (0, col.Count, "#D1");
307                 }
308
309                 [Test]
310                 public void ContainsTest ()
311                 {
312                         ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
313                         object obj = "Item1";
314                         col.Add (obj);
315                         Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
316                         Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
317                 }
318
319                 [Test]
320                 public void IndexOfTest ()
321                 {
322                         ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
323                         col.Add ("Item1");
324                         col.Add ("Item2");
325                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
326                 }
327
328                 [Test]
329                 public void RemoveTest ()
330                 {
331                         ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
332                         col.Add ("Item1");
333                         col.Add ("Item2");
334                         col.Remove ("Item1");
335                         Assert.AreEqual (1, col.Count, "#G1");
336                 }
337
338                 [Test]
339                 public void RemoveAtTest ()
340                 {
341                         ListBox.ObjectCollection col = new ListBox.ObjectCollection (new ListBox ());
342                         col.Add ("Item1");
343                         col.Add ("Item2");
344                         col.RemoveAt (0);
345                         Assert.AreEqual (1, col.Count, "#H1");
346                         Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
347                 }
348
349
350         }
351 }