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