2006-12-19 Daniel Nauck <dna@mono-project.de>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ComboBoxTest.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 using System;
31 using System.Windows.Forms;
32 using System.Drawing;
33 using System.Reflection;
34 using NUnit.Framework;
35 using System.Collections;
36 using System.ComponentModel;
37
38 namespace MonoTests.System.Windows.Forms
39 {
40         [TestFixture]
41         public class ComboBoxTest
42         {
43                 [Test]
44                 public void ComboBoxPropertyTest ()
45                 {
46                         ComboBox mycmbbox = new ComboBox ();
47                         Assert.AreEqual (DrawMode.Normal, mycmbbox.DrawMode, "#1");
48                         Assert.AreEqual (ComboBoxStyle.DropDown, mycmbbox.DropDownStyle, "#2");
49                         Assert.AreEqual (121, mycmbbox.DropDownWidth, "#3");
50                         Assert.AreEqual (false, mycmbbox.DroppedDown, "#4");
51                         Assert.AreEqual (true, mycmbbox.IntegralHeight, "#5");
52                         Assert.AreEqual (0, mycmbbox.Items.Count, "#6");
53                         //Assert.AreEqual (15, mycmbbox.ItemHeight, "#7");      // Note: Item height depends on the current font.
54                         Assert.AreEqual (8, mycmbbox.MaxDropDownItems, "#8");
55                         Assert.AreEqual (0, mycmbbox.MaxLength, "#9");
56                         //Assert.AreEqual (20, mycmbbox.PreferredHeight, "#10");
57 // Note: Item height depends on the current font.
58                         Assert.AreEqual (-1, mycmbbox.SelectedIndex, "#11");
59                         Assert.AreEqual (null, mycmbbox.SelectedItem, "#12");
60                         Assert.AreEqual ("", mycmbbox.SelectedText, "#13");
61                         Assert.AreEqual (0, mycmbbox.SelectionLength, "#14");
62                         Assert.AreEqual (0, mycmbbox.SelectionStart, "#15");
63                         Assert.AreEqual (false, mycmbbox.Sorted, "#16");
64                         Assert.AreEqual ("", mycmbbox.Text, "#17");
65                 }
66
67                 [Test]
68                 public void BeginEndUpdateTest ()
69                 {
70                         Form myform = new Form ();
71                         myform.ShowInTaskbar = false;
72                         myform.Visible = true;
73                         ComboBox cmbbox = new ComboBox ();
74                         cmbbox.Items.Add ("A");
75                         cmbbox.Visible = true;
76                         myform.Controls.Add (cmbbox);
77                         cmbbox.BeginUpdate ();
78                         for (int x = 1 ; x < 5000 ; x++) {
79                                 cmbbox.Items.Add ("Item " + x.ToString ());
80                         }
81                         cmbbox.EndUpdate ();
82                         myform.Dispose ();
83                 }
84
85                 [Test]
86                 public void FindStringTest ()
87                 {
88                         ComboBox cmbbox = new ComboBox ();
89                         cmbbox.FindString ("Hola", -5); // No exception, it's empty
90                         int x = cmbbox.FindString ("Hello");
91                         Assert.AreEqual (-1, x, "#19");
92                         cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
93                         String myString = "ABC";
94                         x = cmbbox.FindString (myString);
95                         Assert.AreEqual (3, x, "#191");
96                         x = cmbbox.FindString (string.Empty);
97                         Assert.AreEqual (0, x, "#192");
98                         x = cmbbox.FindString ("NonExistant");
99                         Assert.AreEqual (-1, x, "#193");
100                 }
101
102                 [Test]
103                 public void FindStringExactTest ()
104                 {
105                         ComboBox cmbbox = new ComboBox ();
106                         cmbbox.FindStringExact ("Hola", -5); // No exception, it's empty
107                         int x = cmbbox.FindStringExact ("Hello");
108                         Assert.AreEqual (-1, x, "#20");
109                         cmbbox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
110                         String myString = "ABC";
111                         x = cmbbox.FindStringExact (myString);
112                         Assert.AreEqual (1, x, "#201");
113                         x = cmbbox.FindStringExact (string.Empty);
114                         Assert.AreEqual (-1, x, "#202");
115                         x = cmbbox.FindStringExact ("NonExistant");
116                         Assert.AreEqual (-1, x, "#203");
117                 }
118
119                 [Test]
120                 public void GetItemHeightTest ()
121                 {
122                         ComboBox cmbbox = new ComboBox ();
123                         cmbbox.Items.Add ("ABC");
124                         cmbbox.Items.Add ("BCD");
125                         cmbbox.Items.Add ("DEF");
126                         int x = -1;
127                         x = cmbbox.GetItemHeight (x);
128                         Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
129                 }
130
131
132                 //
133                 // Exceptions
134                 //
135
136                 [Test]
137                 [ExpectedException (typeof (InvalidEnumArgumentException))]
138                 public void DropDownStyleException ()
139                 {
140                         ComboBox cmbbox = new ComboBox ();
141                         cmbbox.DropDownStyle = (ComboBoxStyle) 10;
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (InvalidEnumArgumentException))]
146                 public void DrawModeException ()
147                 {
148                         ComboBox cmbbox = new ComboBox ();
149                         cmbbox.DrawMode = (DrawMode) 10;
150                 }
151
152                 [Test]
153 #if NET_2_0
154                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
155 #else
156                 [ExpectedException (typeof (ArgumentException))]
157 #endif
158                 public void DropDownWidthException ()
159                 {
160                         ComboBox cmbbox = new ComboBox ();
161                         cmbbox.DropDownWidth = 0;
162                 }
163
164                 [Test]
165 #if NET_2_0
166                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
167 #else
168                 [ExpectedException (typeof (ArgumentException))]
169 #endif
170                 public void ItemHeightException ()
171                 {
172                         ComboBox cmbbox = new ComboBox ();
173                         cmbbox.ItemHeight = -1;
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
178                 public void SelectedIndexException ()
179                 {
180                         ComboBox cmbbox = new ComboBox ();
181                         cmbbox.SelectedIndex = -2;
182                 }
183
184                 [Test]
185                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
186                 public void FindStringExactMinException ()
187                 {
188                         ComboBox cmbbox = new ComboBox ();
189                         cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
190                         cmbbox.FindStringExact ("Hola", -2);
191                 }
192
193                 [Test]
194                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
195                 public void FindStringExactMaxException ()
196                 {
197                         ComboBox cmbbox = new ComboBox ();
198                         cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
199                         cmbbox.FindStringExact ("Hola", 3);
200                 }
201
202                 //
203                 // Events
204                 //
205                 private bool eventFired;
206                 private DrawItemEventArgs drawItemsArgs;
207                 private void DrawItemEventH (object sender,  DrawItemEventArgs e)
208                 {
209                         eventFired = true;
210                         drawItemsArgs = e;
211                 }
212
213                 private void GenericHandler (object sender,  EventArgs e)
214                 {
215                         eventFired = true;
216                 }
217
218                 [Ignore ("Bugs in X11 prevent this test to run properly")]
219                 public void DrawItemEventTest ()
220                 {
221                         eventFired = false;
222                         drawItemsArgs = null;
223                         Form myform = new Form ();
224                         myform.ShowInTaskbar = false;
225                         ComboBox cmbbox = new ComboBox ();
226                         cmbbox.DropDownStyle = ComboBoxStyle.Simple;
227                         cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
228                         cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
229
230                         myform.Controls.Add (cmbbox);
231                         cmbbox.Items.AddRange(new object[] {"Item1"});
232
233                         myform.Visible = true;
234                         cmbbox.Visible = true;
235                         cmbbox.Refresh ();
236
237                         Assert.AreEqual (true, eventFired, "DW1");
238                         Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
239                         myform.Dispose ();
240                 }
241
242                 [Test]
243                 public void DropDownStyleEventTest ()
244                 {
245                         eventFired = false;
246                         ComboBox cmbbox = new ComboBox ();
247                         cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
248                         cmbbox.DropDownStyle = ComboBoxStyle.Simple;
249
250                         Assert.AreEqual (true, eventFired, "DI1");
251                 }
252
253                 [Test]
254                 public void SelectedIndextTest ()
255                 {
256                         eventFired = false;
257                         ComboBox cmbbox = new ComboBox ();
258                         cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
259                         cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
260                         cmbbox.SelectedIndex = 1;
261                         Assert.AreEqual (true, eventFired, "SI1");
262                 }
263
264                 [Test]
265                 public void SelectionWithAdd()
266                 {
267                         ComboBox cb = new ComboBox();
268                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
269                         cb.Items.Add("Item 1");
270                         cb.Items.Add("Item 3");
271                         cb.SelectedIndex = 1;
272                         eventFired = false;
273                         cb.Items.Add("Item 4");
274                         Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
275                         Assert.AreEqual(false, eventFired, "SWA2");
276                         cb.Sorted = true;
277                         cb.SelectedIndex = 1;
278                         eventFired = false;
279                         cb.Items.Add("Item 5");
280                         Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
281                         Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
282                         Assert.AreEqual(false, eventFired, "SWA5");
283                         cb.SelectedIndex = 1;
284                         eventFired = false;
285                         cb.Items.Add("Item 2");
286                         Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
287                         Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
288                         Assert.AreEqual(false, eventFired, "SWA8");
289                 }
290
291                 [Test]
292                 public void SelectionWithInsert()
293                 {
294                         ComboBox cb = new ComboBox();
295                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
296                         cb.Items.Add("Item 1");
297                         cb.SelectedIndex = 0;
298                         eventFired = false;
299                         cb.Items.Insert(0, "Item 2");
300                         Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
301                         Assert.AreEqual(false, eventFired, "SWI2");
302                 }
303
304                 [Test]
305                 public void SelectionWithClear()
306                 {
307                         ComboBox cb = new ComboBox();
308                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
309                         cb.Items.Add("Item 1");
310                         cb.SelectedIndex = 0;
311                         eventFired = false;
312                         cb.Items.Clear();
313                         Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
314                         Assert.AreEqual(false, eventFired, "SWC2");
315                 }
316
317                 [Test]
318                 public void SortedTest()
319                 {
320                         ComboBox mycb = new ComboBox();
321                         Assert.AreEqual(false, mycb.Sorted, "#1");
322                         mycb.Items.Add("Item 2");
323                         mycb.Items.Add("Item 1");
324                         Assert.AreEqual("Item 2", mycb.Items[0], "#2");
325                         Assert.AreEqual("Item 1", mycb.Items[1], "#3");
326                         mycb.Sorted = true;
327                         Assert.AreEqual(true, mycb.Sorted, "#4");
328                         Assert.AreEqual("Item 1", mycb.Items[0], "#5");
329                         Assert.AreEqual("Item 2", mycb.Items[1], "#6");
330                         mycb.Sorted = false;
331                         Assert.AreEqual(false, mycb.Sorted, "#7");
332                         Assert.AreEqual("Item 1", mycb.Items[0], "#8");
333                         Assert.AreEqual("Item 2", mycb.Items[1], "#9");
334                 }
335
336                 [Test]
337                 public void SortedAddTest()
338                 {
339                         ComboBox mycb = new ComboBox();
340                         mycb.Items.Add("Item 2");
341                         mycb.Items.Add("Item 1");
342                         mycb.Sorted = true;
343                         Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
344                         Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
345                 }
346
347                 [Test]
348                 public void SortedInsertTest()
349                 {
350                         ComboBox mycb = new ComboBox();
351                         mycb.Items.Add("Item 2");
352                         mycb.Items.Add("Item 1");
353                         mycb.Sorted = true;
354                         mycb.Items.Insert (0, "Item 3");
355                         Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
356                         Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
357                         Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
358                 }
359
360                 [Test]
361                 public void SortedSelectionInteractions()
362                 {
363                         ComboBox cb = new ComboBox();
364                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
365                         cb.Items.Add("Item 1");
366                         cb.Items.Add("Item 2");
367                         cb.Items.Add("Item 3");
368                         cb.SelectedIndex = 1;
369                         eventFired = false;
370                         cb.Sorted = true;
371                         Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
372                         Assert.AreEqual(true, eventFired, "#SSI2");
373                         cb.SelectedIndex = 1;
374                         eventFired = false;
375                         cb.Sorted = true;
376                         Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
377                         Assert.AreEqual(false, eventFired, "#SSI4");
378                         cb.SelectedIndex = 1;
379                         eventFired = false;
380                         cb.Sorted = false;
381                         Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
382                         Assert.AreEqual(true, eventFired, "#SSI6");
383                 }
384         }
385  
386         [TestFixture]
387         public class ComboBoxObjectCollectionTest
388         {
389                 [Test]
390                 public void ComboBoxObjectCollectionPropertyTest ()
391                 {
392                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
393                         Assert.AreEqual (false, col.IsReadOnly, "#B1");
394                         Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
395                         Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
396                         Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
397                 }
398
399                 [Test]
400                 public void AddTest ()
401                 {
402                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
403                         col.Add ("Item1");
404                         col.Add ("Item2");
405                         Assert.AreEqual (2, col.Count, "#C1");
406                 }
407
408                 [Test]
409                 public void ClearTest ()
410                 {
411                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
412                         col.Add ("Item1");
413                         col.Add ("Item2");
414                         col.Clear ();
415                         Assert.AreEqual (0, col.Count, "#D1");
416                 }
417
418                 [Test]
419                 public void ContainsTest ()
420                 {
421                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
422                         object obj = "Item1";
423                         col.Add (obj);
424                         Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
425                         Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
426                 }
427
428                 [Test]
429                 public void IndexOfTest ()
430                 {
431                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
432                         col.Add ("Item1");
433                         col.Add ("Item2");
434                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
435                 }
436
437                 [Test]
438                 public void RemoveTest ()
439                 {
440                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
441                         col.Add ("Item1");
442                         col.Add ("Item2");
443                         col.Remove ("Item1");
444                         Assert.AreEqual (1, col.Count, "#G1");
445                 }
446
447                 [Test]
448                 public void RemoveAtTest ()
449                 {
450                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
451                         col.Add ("Item1");
452                         col.Add ("Item2");
453                         col.RemoveAt (0);
454                         Assert.AreEqual (1, col.Count, "#H1");
455                         Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
456                 }
457
458                 [Test]
459                 [ExpectedException (typeof (ArgumentNullException))]
460                 public void AddNullTest ()
461                 {
462                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
463                         col.Add (null);
464                 }
465
466                 [Test]
467                 [ExpectedException (typeof (ArgumentNullException))]
468                 public void AddRangeNullTest ()
469                 {
470                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
471                         col.AddRange (null);
472                 }
473
474                 [Test]
475                 [ExpectedException (typeof (ArgumentNullException))]
476                 public void ContainsNullTest ()
477                 {
478                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
479                         col.Contains (null);
480                 }
481
482                 [Test]
483                 [ExpectedException (typeof (ArgumentNullException))]
484                 public void IndexOfNullTest ()
485                 {
486                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
487                         col.IndexOf (null);
488                 }
489
490                 [Test]
491                 [ExpectedException (typeof (ArgumentNullException))]
492                 public void InsertNullTest ()
493                 {
494                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
495                         col.Add ("Item1");
496                         col.Insert (0, null);
497                 }
498
499                 [Test]
500                 [ExpectedException (typeof (ArgumentNullException))]
501                 public void IndexerNullTest ()
502                 {
503                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
504                         col.Add ("Item1");
505                         col [0] = null;
506                 }
507         }
508
509 }