BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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 // Copyright (c) 2006 Matt Hargett
25 //
26 // Authors:
27 //      Ritvik Mayank <mritvik@novell.com>
28 //      Jordi Mas i Hernandez <jordi@ximian.com>
29 //      Matt Hargett  <matt@use.net>
30
31 using System;
32 using System.Data;
33 using System.IO;
34 using System.Collections;
35 using System.ComponentModel;
36 using System.Drawing;
37 using System.Globalization;
38 using System.Reflection;
39 using System.Threading;
40 using System.Windows.Forms;
41
42 using NUnit.Framework;
43 using CategoryAttribute=NUnit.Framework.CategoryAttribute;
44
45 namespace MonoTests.System.Windows.Forms
46 {
47         [TestFixture]
48         public class ComboBoxTest : TestHelper
49         {
50                 private CultureInfo _originalCulture;
51
52                 [SetUp]
53                 protected override void SetUp ()
54                 {
55                         _originalCulture = Thread.CurrentThread.CurrentCulture;
56                         base.SetUp ();
57                 }
58
59                 [TearDown]
60                 protected override void TearDown ()
61                 {
62                         Thread.CurrentThread.CurrentCulture = _originalCulture;
63                         base.TearDown ();
64                 }
65
66                 [Test] // bug 660294
67                 public void TestNullSelectedText ()
68                 {
69                         ComboBox comboBox = new ComboBox ();
70                         string text = "abc";
71                         comboBox.Items.Add (text);
72                         comboBox.SelectedIndex = 0;
73                         comboBox.Select (0, text.Length);
74                         comboBox.SelectedText = null;
75
76                         Assert.AreEqual (String.Empty, comboBox.SelectedText);
77                 }
78
79                 [Test] // bug #331144
80                 public void DropDownStyle ()
81                 {
82                         ComboBox comboBox = new ComboBox ();
83                         comboBox.Items.Add ("abc");
84
85                         Form form = new Form ();
86                         form.Controls.Add (comboBox);
87                         form.Show ();
88
89                         Assert.AreEqual (ComboBoxStyle.DropDown, comboBox.DropDownStyle, "#1");
90                         comboBox.DropDownStyle = ComboBoxStyle.Simple;
91                         Assert.AreEqual (ComboBoxStyle.Simple, comboBox.DropDownStyle, "#2");
92                         comboBox.DropDownStyle = ComboBoxStyle.DropDown;
93                         Assert.AreEqual (ComboBoxStyle.DropDown, comboBox.DropDownStyle, "#3");
94
95                         form.Dispose ();
96                 }
97
98                 [Test] // bug 81610
99                 [Category ("NotWorking")]
100                 public void InitialBoundValue ()
101                 {
102                         ComboBox comboBox1;
103                         using (Form f = new Form ()) {
104                                 f.ShowInTaskbar = false;
105
106                                 DataTable t = new DataTable ();
107                                 t.Columns.Add ("displaymember");
108                                 t.Columns.Add ("valuemember");
109                                 t.Rows.Add (new object [] {"lower", "a"});
110                                 t.Rows.Add (new object [] {"upper", "A"});
111
112                                 // This test seems to prove that the SelectedItem is updated when:
113                                 // - We have a parent
114                                 // - We're setting either DisplayMember, ValueMember or DataSource.
115
116                                 comboBox1 = new ComboBox ();
117                                 comboBox1.DisplayMember = "displaymember";
118                                 comboBox1.ValueMember = "valuemember";
119                                 comboBox1.DataSource = t;
120                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
121                                 f.Controls.Add (comboBox1);
122                                 Assert.AreEqual (string.Empty, comboBox1.Text,  "#??");
123                                 
124                                 comboBox1 = new ComboBox ();
125                                 f.Controls.AddRange (new Control [] { comboBox1 });
126                                 comboBox1.DisplayMember = "displaymember";
127                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
128                                 comboBox1.ValueMember = "valuemember";
129                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
130                                 comboBox1.DataSource = t;
131                                 Assert.AreEqual ("lower", comboBox1.Text, "#A02");
132                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
133                                 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
134
135                                 comboBox1 = new ComboBox ();
136                                 comboBox1.DisplayMember = "displaymember";
137                                 comboBox1.ValueMember = "valuemember";
138                                 comboBox1.DataSource = t;
139                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A01");
140                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
141                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A04");
142                                 f.Controls.AddRange (new Control [] { comboBox1 });
143                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A04");
144
145                                 comboBox1 = new ComboBox ();
146                                 f.Controls.AddRange (new Control [] { comboBox1 });
147                                 comboBox1.DisplayMember = "displaymember";
148                                 comboBox1.ValueMember = "valuemember";
149                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02");
150                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
151                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
152                                 comboBox1.DataSource = t;
153                                 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
154
155                                 comboBox1 = new ComboBox ();
156                                 f.Controls.AddRange (new Control [] { comboBox1 });
157                                 comboBox1.DataSource = t;
158                                 Assert.AreEqual ("System.Data.DataRowView", comboBox1.Text, "#A03");
159                                 comboBox1.DisplayMember = "displaymember";
160                                 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
161                                 comboBox1.ValueMember = "valuemember";
162                                 Assert.AreEqual ("lower", comboBox1.Text, "#A02");
163                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
164                                 Assert.AreEqual ("lower", comboBox1.Text, "#A02-1");
165
166
167                                 comboBox1 = new ComboBox ();
168                                 comboBox1.DisplayMember = "displaymember";
169                                 comboBox1.ValueMember = "valuemember";
170                                 Assert.AreEqual ("", comboBox1.Text, "#A02");
171                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
172                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
173                                 comboBox1.DataSource = t;
174                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A03");
175                                 f.Controls.AddRange (new Control [] { comboBox1 });
176                                 Assert.AreEqual (string.Empty, comboBox1.Text, "#A03");
177
178                                 comboBox1 = new ComboBox ();
179                                 comboBox1.DataSource = t;
180                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
181                                 comboBox1.DisplayMember = "displaymember";
182                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
183                                 comboBox1.ValueMember = "valuemember";
184                                 Assert.AreEqual ("", comboBox1.Text, "#A02");
185                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
186                                 Assert.AreEqual ("", comboBox1.Text, "#A02-1");
187                                 f.Controls.AddRange (new Control [] { comboBox1 });
188                                 Assert.AreEqual ("", comboBox1.Text, "#A02-1");
189
190
191                                 comboBox1 = new ComboBox ();
192                                 f.Controls.AddRange (new Control [] { comboBox1 });
193                                 comboBox1.DisplayMember = "displaymember";
194                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
195                                 comboBox1.ValueMember = "valuemember";
196                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
197                                 comboBox1.DataSource = t;
198                                 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
199                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
200                                 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
201
202                                 comboBox1 = new ComboBox ();
203                                 f.Controls.AddRange (new Control [] { comboBox1 });
204                                 comboBox1.DisplayMember = "displaymember";
205                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
206                                 comboBox1.ValueMember = "valuemember";
207                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
208                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
209                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
210                                 comboBox1.DataSource = t;
211                                 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
212                                                                 
213                                 comboBox1 = new ComboBox ();
214                                 f.Controls.AddRange (new Control [] { comboBox1 });
215                                 comboBox1.ValueMember = "valuemember";
216                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
217                                 comboBox1.DataSource = t;
218                                 Assert.AreEqual ("a", comboBox1.Text, "#A03");
219                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
220                                 Assert.AreEqual ("a", comboBox1.Text, "#A03");
221                                 comboBox1.DisplayMember = "displaymember";
222                                 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
223
224                                 comboBox1 = new ComboBox ();
225                                 comboBox1.DataSource = t;
226                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
227                                 comboBox1.DisplayMember = "displaymember";
228                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
229                                 comboBox1.ValueMember = "valuemember";
230                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
231                                 f.Controls.AddRange (new Control [] { comboBox1 });
232                                 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
233                                 InitialBoundValue_dummy e = new InitialBoundValue_dummy ();
234                                 e.controlsrc = "c";
235                                 comboBox1.DataBindings.Add ("SelectedValue", e, "controlsrc");
236                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
237                                 f.Show ();
238                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
239                                 
240                                 // The real failing test
241                                 comboBox1 = new ComboBox ();
242                                 comboBox1.DisplayMember = "displaymember";
243                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
244                                 comboBox1.ValueMember = "valuemember";
245                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
246                                 comboBox1.DataSource = t;
247                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
248                                 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
249                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
250                                 f.Controls.AddRange (new Control [] { comboBox1 });
251                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
252                                 f.Show ();
253                                 Assert.AreEqual ("", comboBox1.Text, "#A03");
254
255                                 Assert.AreEqual ("", comboBox1.Text, "#bug");
256
257                                 string table =
258 @"<?xml version=""1.0"" standalone=""yes""?>
259 <DOK>
260 <DOK>
261 <klient>287</klient>
262 </DOK>
263 </DOK>
264 ";
265                                 string lookup =
266         @"<?xml version=""1.0"" standalone=""yes""?>
267 <klient>
268 <klient>
269 <nimi>FAILED</nimi>
270 <kood>316</kood>
271 </klient>
272 <klient>
273 <nimi>SUCCESS</nimi>
274 <kood>287</kood>
275 </klient>
276 </klient>";
277
278                                 using (Form frm = new Form ()) {
279                                         frm.ShowInTaskbar = false;
280                                         DataSet dsTable = new DataSet ();
281                                         dsTable.ReadXml (new StringReader (table));
282                                         DataSet dsLookup = new DataSet ();
283                                         dsLookup.ReadXml (new StringReader (lookup));
284                                         ComboBox cb = new ComboBox ();
285                                         cb.DataSource = dsLookup.Tables [0];
286                                         cb.DisplayMember = "nimi";
287                                         cb.ValueMember = "kood";
288                                         InitialBoundValue_dummy d = new InitialBoundValue_dummy();
289                                         d.controlsrc = "287";
290                                         cb.DataBindings.Add ("SelectedValue", d, "controlsrc");
291                                         frm.Controls.Add (cb);
292                                         Assert.AreEqual ("", cb.Text, "#01");
293                                         frm.Show ();
294                                         Assert.AreEqual ("SUCCESS", cb.Text, "#02");
295                                 }
296                         }
297                 }
298
299                 class InitialBoundValue_dummy
300                 {
301                         string t;
302                         public string controlsrc
303                         {
304                                 get { return t; }
305                                 set { t = value; }
306                         }
307                 }
308
309                 [Test]
310                 public void ContextMenuTest ()
311                 {
312                         ComboBox cmb = new ComboBox ();
313                         ContextMenu cm = new ContextMenu ();
314                         
315                         Assert.IsNull (cmb.ContextMenu, "#1");
316                         cmb.ContextMenu = cm;
317                         Assert.AreSame (cmb.ContextMenu, cm, "#2");
318                         cmb.DropDownStyle = ComboBoxStyle.DropDown;
319                         Assert.AreSame (cmb.ContextMenu, cm, "#3");
320                         cmb.DropDownStyle = ComboBoxStyle.DropDownList;
321                         Assert.AreSame (cmb.ContextMenu, cm, "#4");
322                         cmb.DropDownStyle = ComboBoxStyle.Simple;
323                         Assert.AreSame (cmb.ContextMenu, cm, "#5");
324                         
325                 }
326                 
327                 [Test] // bug 80794
328                 public void DataBindingTest ()
329                 {
330                         string table = 
331 @"<?xml version=""1.0"" standalone=""yes""?>
332 <DOK>
333 <DOK>
334 <klient>287</klient>
335 </DOK>
336 </DOK>
337 ";
338                         string lookup = 
339 @"<?xml version=""1.0"" standalone=""yes""?>
340 <klient>
341 <klient>
342 <nimi>FAILED</nimi>
343 <kood>316</kood>
344 </klient>
345 <klient>
346 <nimi>SUCCESS</nimi>
347 <kood>287</kood>
348 </klient>
349 </klient>";
350                         
351                         using (Form frm = new Form ()) {
352                                 frm.ShowInTaskbar = false;
353                                 DataSet dsTable = new DataSet ();
354                                 dsTable.ReadXml (new StringReader (table));
355                                 DataSet dsLookup = new DataSet ();
356                                 dsLookup.ReadXml (new StringReader (lookup));
357                                 ComboBox cb = new ComboBox ();
358                                 cb.DataSource = dsLookup.Tables [0];
359                                 cb.DisplayMember = "nimi";
360                                 cb.ValueMember = "kood";
361                                 cb.DataBindings.Add ("SelectedValue", dsTable.Tables [0], "klient");
362                                 frm.Controls.Add (cb);
363                                 Assert.AreEqual ("", cb.Text, "#01");
364                                 frm.Show ();
365                                 Assert.AreEqual ("SUCCESS", cb.Text, "#02");
366                         }
367                 }
368
369                 [Test] // bug #82069
370                 public void DataBindingTest2 ()
371                 {
372                         ComboBox cmb = new ComboBox ();
373                         cmb.Items.Add (new MockItem ("Foo", 9));
374                         cmb.Items.Add (new MockItem ("Bar", 6));
375                         cmb.ValueMember = "Value";
376                         cmb.DisplayMember = "Text";
377
378                         Form form = new Form ();
379                         form.Controls.Add (cmb);
380                         form.Show ();
381
382                         cmb.SelectedIndex = 0;
383                         Assert.AreEqual ("Foo", cmb.Text, "#1");
384                         cmb.SelectedIndex = 1;
385                         Assert.AreEqual ("Bar", cmb.Text, "#2");
386
387                         form.Dispose ();
388                 }
389                 
390                 [Test]
391                 public void ComboBoxPropertyTest ()
392                 {
393                         ComboBox mycmbbox = new ComboBox ();
394                         Assert.AreEqual (DrawMode.Normal, mycmbbox.DrawMode, "#1");
395                         Assert.AreEqual (ComboBoxStyle.DropDown, mycmbbox.DropDownStyle, "#2");
396                         Assert.AreEqual (false, mycmbbox.DroppedDown, "#4");
397                         Assert.AreEqual (true, mycmbbox.IntegralHeight, "#5");
398                         Assert.AreEqual (0, mycmbbox.Items.Count, "#6");
399                         //Assert.AreEqual (15, mycmbbox.ItemHeight, "#7");      // Note: Item height depends on the current font.
400                         Assert.AreEqual (8, mycmbbox.MaxDropDownItems, "#8");
401                         Assert.AreEqual (0, mycmbbox.MaxLength, "#9");
402                         //Assert.AreEqual (20, mycmbbox.PreferredHeight, "#10");
403                         // Note: Item height depends on the current font.
404                         Assert.AreEqual (-1, mycmbbox.SelectedIndex, "#11");
405                         Assert.AreEqual (null, mycmbbox.SelectedItem, "#12");
406                         Assert.AreEqual ("", mycmbbox.SelectedText, "#13");
407                         Assert.AreEqual (0, mycmbbox.SelectionLength, "#14");
408                         Assert.AreEqual (0, mycmbbox.SelectionStart, "#15");
409                         Assert.AreEqual (false, mycmbbox.Sorted, "#16");
410                         Assert.AreEqual ("", mycmbbox.Text, "#17");
411 #if NET_2_0
412                         Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#18");
413                         Assert.AreEqual (AutoCompleteMode.None, mycmbbox.AutoCompleteMode, "#19");
414                         Assert.AreEqual (AutoCompleteSource.None, mycmbbox.AutoCompleteSource, "#20");
415
416                         mycmbbox.AutoCompleteCustomSource = null;
417                         Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#21");
418                         
419                         Assert.AreEqual (ImageLayout.Tile, mycmbbox.BackgroundImageLayout, "#22");
420                         Assert.AreEqual (null, mycmbbox.DataSource, "#23");
421                         Assert.AreEqual (106, mycmbbox.DropDownHeight, "#24");
422                         Assert.AreEqual (FlatStyle.Standard, mycmbbox.FlatStyle, "#25");
423                         Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MaximumSize.ToString (), "#26");
424                         Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MinimumSize.ToString (), "#27");
425                         Assert.AreEqual ("{Left=0,Top=0,Right=0,Bottom=0}", mycmbbox.Padding.ToString (), "#28");
426                         
427 #endif
428                 }
429
430                 [Test]
431                 public void PreferredHeight ()
432                 {
433                         // PreferredHeight is not tied to ItemHeight like we had it
434                         ComboBox cb = new ComboBox ();
435                         
436                         int h = cb.PreferredHeight;
437                         
438                         cb.ItemHeight = 10;
439                         Assert.AreEqual (h, cb.PreferredHeight, "A2");
440
441                         cb.ItemHeight = 30;
442                         Assert.AreEqual (h, cb.PreferredHeight, "A3");
443                 }
444                 
445 #if NET_2_0
446                 [Test]
447                 public void ResetTextTest ()
448                 {
449                         ComboBox cmbbox = new ComboBox ();
450                         Assert.AreEqual ("", cmbbox.Text, "#01");
451                         cmbbox.Text = "abc";
452                         Assert.AreEqual ("abc", cmbbox.Text, "#02");
453                         cmbbox.ResetText ();
454                         Assert.AreEqual ("", cmbbox.Text, "#03");
455                 }
456                 
457                 [Test]
458                 public void BackgroundImageLayoutTest ()
459                 {
460                         ComboBox cmbbox = new ComboBox ();
461                         cmbbox.BackgroundImageLayout = ImageLayout.Stretch;
462                         Assert.AreEqual (ImageLayout.Stretch, cmbbox.BackgroundImageLayout, "#01");
463                 }
464                 
465                 [Test]
466                 public void DropDownHeightTest ()
467                 {
468                         ComboBox cmbbox = new ComboBox ();
469                         cmbbox.DropDownHeight = 225;
470                         Assert.AreEqual (225, cmbbox.DropDownHeight, "#01");
471                         cmbbox.DropDownHeight = 1;
472                         Assert.AreEqual (1, cmbbox.DropDownHeight, "#02");
473                 }
474
475                 [Test]
476                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
477                 public void DropDownHeightExceptionTest1 ()
478                 {
479                         ComboBox cmbbox = new ComboBox ();
480                         cmbbox.DropDownHeight = -225;   
481                 }
482
483                 [Test]
484                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
485                 public void DropDownHeightExceptionTest2 ()
486                 {
487                         ComboBox cmbbox = new ComboBox ();
488                         cmbbox.DropDownHeight = 0;
489                 }
490                 
491                 [Test]
492                 public void FlatStyleTest ()
493                 {
494                         ComboBox cmbbox = new ComboBox ();
495                         cmbbox.FlatStyle = FlatStyle.Popup;
496                         Assert.AreEqual (FlatStyle.Popup, cmbbox.FlatStyle, "#01");
497                 }
498                 
499                 [Test]
500                 [ExpectedException (typeof (InvalidEnumArgumentException))]
501                 public void FlatStyleExceptionTest ()
502                 {
503                         ComboBox cmbbox = new ComboBox ();
504                         cmbbox.FlatStyle = (FlatStyle) (-123);
505                 }
506         
507                 [Test]
508                 public void MaximumSizeTest ()
509                 {
510                         ComboBox cmbbox = new ComboBox ();
511                         cmbbox.MaximumSize = new Size (25, 25);
512                         Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MaximumSize.ToString (), "#01");
513                         cmbbox.MaximumSize = new Size (50, 75);
514                         Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MaximumSize.ToString (), "#02");
515                 }
516         
517                 [Test]
518                 public void MinumumSizeTest ()
519                 {
520                         ComboBox cmbbox = new ComboBox ();
521                         cmbbox.MinimumSize = new Size (25, 25);
522                         Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MinimumSize.ToString (), "#1");
523                         cmbbox.MinimumSize = new Size (50, 75);
524                         Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MinimumSize.ToString (), "#2"); 
525                 }
526                 
527                 [Test]
528                 public void PaddingTest ()
529                 {
530                         ComboBox cmbbox = new ComboBox ();
531                         cmbbox.Padding = new Padding (21);
532                         Assert.AreEqual ("{Left=21,Top=21,Right=21,Bottom=21}", cmbbox.Padding.ToString (), "#01");
533                 }
534 #endif
535
536                 [Test]
537                 public void BeginEndUpdateTest ()
538                 {
539                         Form myform = new Form ();
540                         myform.ShowInTaskbar = false;
541                         myform.Visible = true;
542                         ComboBox cmbbox = new ComboBox ();
543                         cmbbox.Items.Add ("A");
544                         cmbbox.Visible = true;
545                         myform.Controls.Add (cmbbox);
546                         cmbbox.BeginUpdate ();
547                         for (int x = 1 ; x < 5000 ; x++) {
548                                 cmbbox.Items.Add ("Item " + x.ToString ());
549                         }
550                         cmbbox.EndUpdate ();
551                         myform.Dispose ();
552                 }
553
554                 [Test]
555                 public void FindString ()
556                 {
557                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
558
559                         ComboBox cmbbox = new ComboBox ();
560                         Assert.AreEqual (-1, cmbbox.FindString ("Hello"), "#A1");
561                         Assert.AreEqual (-1, cmbbox.FindString (string.Empty), "#A2");
562                         Assert.AreEqual (-1, cmbbox.FindString (null), "#A3");
563                         Assert.AreEqual (-1, cmbbox.FindString ("Hola", -5), "#A4");
564                         Assert.AreEqual (-1, cmbbox.FindString ("Hola", 40), "#A5");
565
566                         cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
567                         Assert.AreEqual (2, cmbbox.FindString ("I"), "#B1");
568                         Assert.AreEqual (0, cmbbox.FindString ("in"), "#B2");
569                         Assert.AreEqual (1, cmbbox.FindString ("BAD"), "#B3");
570                         Assert.AreEqual (1, cmbbox.FindString ("Bad"), "#B4");
571                         Assert.AreEqual (1, cmbbox.FindString ("b"), "#B5");
572                         Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#B6");
573                         Assert.AreEqual (-1, cmbbox.FindString (null), "#B7");
574
575                         Assert.AreEqual (3, cmbbox.FindString ("b", 2), "#C1");
576                         Assert.AreEqual (5, cmbbox.FindString ("I", 3), "#C2");
577                         Assert.AreEqual (4, cmbbox.FindString ("B", 3), "#C3");
578                         Assert.AreEqual (1, cmbbox.FindString ("B", 4), "#C4");
579                         Assert.AreEqual (5, cmbbox.FindString ("I", 4), "#C5");
580                         Assert.AreEqual (4, cmbbox.FindString ("BA", 3), "#C6");
581                         Assert.AreEqual (0, cmbbox.FindString ("i", -1), "#C7");
582                         Assert.AreEqual (3, cmbbox.FindString (string.Empty, 2), "#C8");
583                         Assert.AreEqual (-1, cmbbox.FindString (null, 1), "#C9");
584
585                         cmbbox.Items.Add (string.Empty);
586                         Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#D1");
587                         Assert.AreEqual (-1, cmbbox.FindString (null), "#D2");
588
589                         Assert.AreEqual (4, cmbbox.FindString (string.Empty, 3), "#E1");
590                         Assert.AreEqual (-1, cmbbox.FindString (null, 99), "#E2");
591                         Assert.AreEqual (-1, cmbbox.FindString (null, -5), "#E3");
592                 }
593
594                 [Test]
595                 public void FindString_StartIndex_ItemCount ()
596                 {
597                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
598
599                         ComboBox cmbbox = new ComboBox ();
600                         cmbbox.Items.AddRange (new object [] { "BA", "BB" });
601 #if NET_2_0
602                         Assert.AreEqual (0, cmbbox.FindString ("b", 1));
603 #else
604                         try {
605                                 cmbbox.FindString ("b", 1);
606                                 Assert.Fail ("#1");
607                         } catch (ArgumentOutOfRangeException ex) {
608                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
609                                 Assert.IsNull (ex.InnerException, "#3");
610                                 Assert.IsNotNull (ex.Message, "#4");
611                                 Assert.IsNotNull (ex.ParamName, "#5");
612                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
613                         }
614 #endif
615                 }
616
617                 [Test]
618                 public void FindString_StartIndex_Min ()
619                 {
620                         ComboBox cmbbox = new ComboBox ();
621                         cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
622                         try {
623                                 cmbbox.FindString ("Hola", -2);
624                                 Assert.Fail ("#1");
625                         } catch (ArgumentOutOfRangeException ex) {
626                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
627                                 Assert.IsNull (ex.InnerException, "#3");
628                                 Assert.IsNotNull (ex.Message, "#4");
629                                 Assert.IsNotNull (ex.ParamName, "#5");
630                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
631                         }
632                 }
633
634                 [Test]
635                 public void FindString_StartIndex_Max ()
636                 {
637                         ComboBox cmbbox = new ComboBox ();
638                         cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
639                         try {
640                                 cmbbox.FindString ("Hola", 4);
641                                 Assert.Fail ("#1");
642                         } catch (ArgumentOutOfRangeException ex) {
643                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
644                                 Assert.IsNull (ex.InnerException, "#3");
645                                 Assert.IsNotNull (ex.Message, "#4");
646                                 Assert.IsNotNull (ex.ParamName, "#5");
647                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
648                         }
649                 }
650
651                 [Test]
652                 public void FindStringExact ()
653                 {
654                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
655
656                         ComboBox cmbbox = new ComboBox ();
657                         Assert.AreEqual (-1, cmbbox.FindStringExact ("Hello"), "#A1");
658                         Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#A2");
659                         Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#A3");
660                         Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", -5), "#A4");
661                         Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", 40), "#A5");
662
663                         cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
664                         Assert.AreEqual (2, cmbbox.FindStringExact ("IN"), "#B1");
665                         Assert.AreEqual (0, cmbbox.FindStringExact ("in"), "#B2");
666                         Assert.AreEqual (3, cmbbox.FindStringExact ("BAD"), "#B3");
667                         Assert.AreEqual (3, cmbbox.FindStringExact ("bad"), "#B4");
668                         Assert.AreEqual (-1, cmbbox.FindStringExact ("B"), "#B5");
669                         Assert.AreEqual (-1, cmbbox.FindStringExact ("NonExistant"), "#B6");
670                         Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#B7");
671                         Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#B8");
672
673                         Assert.AreEqual (2, cmbbox.FindStringExact ("In", 1), "#C1");
674                         Assert.AreEqual (5, cmbbox.FindStringExact ("In", 2), "#C2");
675                         Assert.AreEqual (4, cmbbox.FindStringExact ("BaD", 3), "#C3");
676                         Assert.AreEqual (3, cmbbox.FindStringExact ("bad", -1), "#C4");
677                         Assert.AreEqual (5, cmbbox.FindStringExact ("In", 4), "#C5");
678                         Assert.AreEqual (3, cmbbox.FindStringExact ("bad", 4), "#C6");
679                         Assert.AreEqual (-1, cmbbox.FindStringExact ("B", 4), "#C7");
680                         Assert.AreEqual (-1, cmbbox.FindStringExact ("BADNOT", 0), "#C8");
681                         Assert.AreEqual (-1, cmbbox.FindStringExact ("i", -1), "#C9");
682                         Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty, 2), "#C10");
683                         Assert.AreEqual (-1, cmbbox.FindStringExact (null, 1), "#C11");
684
685                         cmbbox.Items.Add (string.Empty);
686                         Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty), "#D1");
687                         Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#D2");
688
689                         Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty, 3), "#E1");
690                         Assert.AreEqual (-1, cmbbox.FindStringExact (null, 99), "#E2");
691                         Assert.AreEqual (-1, cmbbox.FindStringExact (null, -5), "#E3");
692                 }
693
694                 [Test]
695                 public void FindStringExact_StartIndex_ItemCount ()
696                 {
697                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
698
699                         ComboBox cmbbox = new ComboBox ();
700                         cmbbox.Items.AddRange (new object [] { "AB", "BA", "AB", "BA" });
701 #if NET_2_0
702                         Assert.AreEqual (1, cmbbox.FindStringExact ("BA", 3));
703 #else
704                         try {
705                                 cmbbox.FindString ("BA", 3);
706                                 Assert.Fail ("#1");
707                         } catch (ArgumentOutOfRangeException ex) {
708                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
709                                 Assert.IsNull (ex.InnerException, "#3");
710                                 Assert.IsNotNull (ex.Message, "#4");
711                                 Assert.IsNotNull (ex.ParamName, "#5");
712                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
713                         }
714 #endif
715                 }
716
717                 [Test]
718                 public void FindStringExact_StartIndex_Min ()
719                 {
720                         ComboBox cmbbox = new ComboBox ();
721                         cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
722                         try {
723                                 cmbbox.FindStringExact ("Hola", -2);
724                                 Assert.Fail ("#1");
725                         } catch (ArgumentOutOfRangeException ex) {
726                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
727                                 Assert.IsNull (ex.InnerException, "#3");
728                                 Assert.IsNotNull (ex.Message, "#4");
729                                 Assert.IsNotNull (ex.ParamName, "#5");
730                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
731                         }
732                 }
733
734                 [Test]
735                 public void FindStringExact_StartIndex_Max ()
736                 {
737                         ComboBox cmbbox = new ComboBox ();
738                         cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
739                         try {
740                                 cmbbox.FindStringExact ("Hola", 4);
741                                 Assert.Fail ("#1");
742                         } catch (ArgumentOutOfRangeException ex) {
743                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
744                                 Assert.IsNull (ex.InnerException, "#3");
745                                 Assert.IsNotNull (ex.Message, "#4");
746                                 Assert.IsNotNull (ex.ParamName, "#5");
747                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
748                         }
749                 }
750
751                 [Test]
752                 public void GetItemHeightTest ()
753                 {
754                         ComboBox cmbbox = new ComboBox ();
755                         cmbbox.Items.Add ("ABC");
756                         cmbbox.Items.Add ("BCD");
757                         cmbbox.Items.Add ("DEF");
758                         int x = -1;
759                         x = cmbbox.GetItemHeight (x);
760                         Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
761                 }
762
763                 //
764                 // Exceptions
765                 //
766
767                 [Test]
768                 [ExpectedException (typeof (InvalidEnumArgumentException))]
769                 public void DropDownStyleException ()
770                 {
771                         ComboBox cmbbox = new ComboBox ();
772                         cmbbox.DropDownStyle = (ComboBoxStyle) 10;
773                 }
774
775                 [Test]
776                 [ExpectedException (typeof (InvalidEnumArgumentException))]
777                 public void DrawModeException ()
778                 {
779                         ComboBox cmbbox = new ComboBox ();
780                         cmbbox.DrawMode = (DrawMode) 10;
781                 }
782
783                 [Test]
784                 public void DropDownWidth ()
785                 {
786                         ComboBox cmbbox = new ComboBox ();
787                         Assert.AreEqual (121, cmbbox.DropDownWidth, "#A1");
788                         cmbbox.DropDownWidth = 1;
789                         Assert.AreEqual (1, cmbbox.DropDownWidth, "#A2");
790
791                         try {
792                                 cmbbox.DropDownWidth = 0;
793                                 Assert.Fail ("#B1");
794                         }
795 #if NET_2_0
796                         catch (ArgumentOutOfRangeException ex) {
797                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
798                                 Assert.IsNotNull (ex.Message, "#B3");
799                                 Assert.IsNotNull (ex.ParamName, "#B4");
800                                 Assert.AreEqual ("DropDownWidth", ex.ParamName, "#B5");
801                                 Assert.IsNull (ex.InnerException, "#B6");
802                         }
803 #else
804                         catch (ArgumentException ex) {
805                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
806                                 Assert.IsNotNull (ex.Message, "#B3");
807                                 Assert.IsNull (ex.ParamName, "#B4");
808                                 Assert.IsNull (ex.InnerException, "#B5");
809                         }
810 #endif
811                 }
812
813                 [Test]
814                 public void ItemHeight ()
815                 {
816                         ComboBox cmbbox = new ComboBox ();
817                         Assert.IsTrue (cmbbox.ItemHeight >= 1, "#A1");
818                         cmbbox.ItemHeight = 1;
819                         Assert.AreEqual (1, cmbbox.ItemHeight, "#A2");
820
821                         try {
822                                 cmbbox.ItemHeight = 0;
823                                 Assert.Fail ("#B1");
824                         }
825 #if NET_2_0
826                         catch (ArgumentOutOfRangeException ex) {
827                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
828                                 Assert.IsNotNull (ex.Message, "#B3");
829                                 Assert.IsNotNull (ex.ParamName, "#B4");
830                                 Assert.AreEqual ("ItemHeight", ex.ParamName, "#B5");
831                                 Assert.IsNull (ex.InnerException, "#B6");
832                         }
833 #else
834                         catch (ArgumentException ex) {
835                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
836                                 Assert.IsNotNull (ex.Message, "#B3");
837                                 Assert.IsNull (ex.ParamName, "#B4");
838                                 Assert.IsNull (ex.InnerException, "#B5");
839                         }
840 #endif
841                 }
842
843                 [Test]
844                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
845                 public void SelectedIndexException ()
846                 {
847                         ComboBox cmbbox = new ComboBox ();
848                         cmbbox.SelectedIndex = -2;
849                 }
850
851                 //
852                 // Events
853                 //
854                 private bool eventFired;
855                 private DrawItemEventArgs drawItemsArgs;
856                 private void DrawItemEventH (object sender,  DrawItemEventArgs e)
857                 {
858                         eventFired = true;
859                         drawItemsArgs = e;
860                 }
861
862                 private void GenericHandler (object sender,  EventArgs e)
863                 {
864                         eventFired = true;
865                 }
866
867                 [Ignore ("Bugs in X11 prevent this test to run properly")]
868                 public void DrawItemEventTest ()
869                 {
870                         eventFired = false;
871                         drawItemsArgs = null;
872                         Form myform = new Form ();
873                         myform.ShowInTaskbar = false;
874                         ComboBox cmbbox = new ComboBox ();
875                         cmbbox.DropDownStyle = ComboBoxStyle.Simple;
876                         cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
877                         cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
878
879                         myform.Controls.Add (cmbbox);
880                         cmbbox.Items.AddRange(new object[] {"Item1"});
881
882                         myform.Visible = true;
883                         cmbbox.Visible = true;
884                         cmbbox.Refresh ();
885
886                         Assert.AreEqual (true, eventFired, "DW1");
887                         Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
888                         myform.Dispose ();
889                 }
890
891                 [Test]
892                 public void DropDownStyleEventTest ()
893                 {
894                         eventFired = false;
895                         ComboBox cmbbox = new ComboBox ();
896                         cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
897                         cmbbox.DropDownStyle = ComboBoxStyle.Simple;
898
899                         Assert.AreEqual (true, eventFired, "DI1");
900                 }
901
902                 [Test]
903                 public void SelectedIndexTest ()
904                 {
905                         eventFired = false;
906                         ComboBox cmbbox = new ComboBox ();
907                         cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
908                         cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
909                         cmbbox.SelectedIndex = 1;
910                         Assert.AreEqual (true, eventFired, "SI1");
911                 }
912
913                 [Test]
914                 public void SelectionWithAdd()
915                 {
916                         ComboBox cb = new ComboBox();
917                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
918                         cb.Items.Add("Item 1");
919                         cb.Items.Add("Item 3");
920                         cb.SelectedIndex = 1;
921                         eventFired = false;
922                         cb.Items.Add("Item 4");
923                         Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
924                         Assert.AreEqual(false, eventFired, "SWA2");
925                         cb.Sorted = true;
926                         cb.SelectedIndex = 1;
927                         eventFired = false;
928                         cb.Items.Add("Item 5");
929                         Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
930                         Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
931                         Assert.AreEqual(false, eventFired, "SWA5");
932                         cb.SelectedIndex = 1;
933                         eventFired = false;
934                         cb.Items.Add("Item 2");
935                         Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
936                         Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
937                         Assert.AreEqual(false, eventFired, "SWA8");
938                 }
939
940                 [Test]
941                 public void SelectionWithInsert()
942                 {
943                         ComboBox cb = new ComboBox();
944                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
945                         cb.Items.Add("Item 1");
946                         cb.SelectedIndex = 0;
947                         eventFired = false;
948                         cb.Items.Insert(0, "Item 2");
949                         Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
950                         Assert.AreEqual(false, eventFired, "SWI2");
951                 }
952
953                 [Test]
954                 public void SelectionWithClear()
955                 {
956                         ComboBox cb = new ComboBox();
957                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
958                         cb.Items.Add("Item 1");
959                         cb.SelectedIndex = 0;
960                         eventFired = false;
961                         cb.Items.Clear();
962                         Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
963                         Assert.AreEqual(false, eventFired, "SWC2");
964                 }
965
966                 // Bug #333750 - DisplayMember assignation 
967                 // when ComboBox already has a BindingContext BUT
968                 // handle hasn't been created yet.
969                 [Test]
970                 public void SelectedTextWithBindingTest ()
971                 {
972                         Form form = new Form ();
973                         form.ShowInTaskbar = false;
974                         ComboBox cb = new ComboBox ();
975                         cb.Parent = form;
976
977                         MockItem [] items = new MockItem [] {
978                                 new MockItem ("A", 0),
979                                 new MockItem ("B", 1),
980                                 new MockItem ("C", 2)
981                         };
982
983                         cb.DataSource = items;
984                         cb.DisplayMember = "Text";
985
986                         form.Show ();
987
988                         Assert.AreEqual ("A", cb.SelectedText, "#A1");
989
990                         form.Dispose ();
991                 }
992
993                 [Test]
994                 public void SortedTest()
995                 {
996                         ComboBox mycb = new ComboBox();
997                         Assert.AreEqual(false, mycb.Sorted, "#1");
998                         mycb.Items.Add("Item 2");
999                         mycb.Items.Add("Item 1");
1000                         Assert.AreEqual("Item 2", mycb.Items[0], "#2");
1001                         Assert.AreEqual("Item 1", mycb.Items[1], "#3");
1002                         mycb.Sorted = true;
1003                         Assert.AreEqual(true, mycb.Sorted, "#4");
1004                         Assert.AreEqual("Item 1", mycb.Items[0], "#5");
1005                         Assert.AreEqual("Item 2", mycb.Items[1], "#6");
1006                         mycb.Sorted = false;
1007                         Assert.AreEqual(false, mycb.Sorted, "#7");
1008                         Assert.AreEqual("Item 1", mycb.Items[0], "#8");
1009                         Assert.AreEqual("Item 2", mycb.Items[1], "#9");
1010                 }
1011
1012                 [Test]
1013                 public void SortedAddTest()
1014                 {
1015                         ComboBox mycb = new ComboBox();
1016                         mycb.Items.Add("Item 2");
1017                         mycb.Items.Add("Item 1");
1018                         mycb.Sorted = true;
1019                         Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
1020                         Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
1021                 }
1022
1023                 [Test]
1024                 public void SortedInsertTest()
1025                 {
1026                         ComboBox mycb = new ComboBox();
1027                         mycb.Items.Add("Item 2");
1028                         mycb.Items.Add("Item 1");
1029                         mycb.Sorted = true;
1030                         mycb.Items.Insert (0, "Item 3");
1031                         Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
1032                         Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
1033                         Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
1034                 }
1035
1036                 [Test]
1037                 public void SortedSelectionInteractions()
1038                 {
1039                         ComboBox cb = new ComboBox();
1040                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
1041                         cb.Items.Add("Item 1");
1042                         cb.Items.Add("Item 2");
1043                         cb.Items.Add("Item 3");
1044                         cb.SelectedIndex = 1;
1045                         eventFired = false;
1046                         cb.Sorted = true;
1047                         Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
1048                         Assert.AreEqual(true, eventFired, "#SSI2");
1049                         cb.SelectedIndex = 1;
1050                         eventFired = false;
1051                         cb.Sorted = true;
1052                         Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
1053                         Assert.AreEqual(false, eventFired, "#SSI4");
1054                         cb.SelectedIndex = 1;
1055                         eventFired = false;
1056                         cb.Sorted = false;
1057                         Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
1058                         Assert.AreEqual(true, eventFired, "#SSI6");
1059                 }
1060
1061                 [Test]
1062                 public void Text_DropDown ()
1063                 {
1064                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
1065
1066                         ComboBox cmbbox = new ComboBox ();
1067                         Assert.IsNotNull (cmbbox.Text, "#A1");
1068                         Assert.AreEqual (string.Empty, cmbbox.Text, "#A2");
1069                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#A3");
1070
1071                         cmbbox.Items.Add ("Another");
1072                         cmbbox.Items.Add ("Bad");
1073                         cmbbox.Items.Add ("IN");
1074                         cmbbox.Items.Add ("Combobox");
1075                         cmbbox.Items.Add ("BAD");
1076                         cmbbox.Items.Add ("iN");
1077                         cmbbox.Items.Add ("Bad");
1078
1079                         Assert.IsNotNull (cmbbox.Text, "#B1");
1080                         Assert.AreEqual (string.Empty, cmbbox.Text, "#B2");
1081                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#B3");
1082
1083                         cmbbox.SelectedIndex = 3;
1084                         Assert.IsNotNull (cmbbox.Text, "#C1");
1085                         Assert.AreEqual ("Combobox", cmbbox.Text, "#C2");
1086                         Assert.AreEqual (3, cmbbox.SelectedIndex, "#C3");
1087
1088                         cmbbox.Text = string.Empty;
1089                         Assert.IsNotNull (cmbbox.Text, "#D1");
1090                         Assert.AreEqual (string.Empty, cmbbox.Text, "#D2");
1091                         Assert.AreEqual (3, cmbbox.SelectedIndex, "#D3");
1092
1093                         cmbbox.SelectedIndex = 1;
1094                         Assert.IsNotNull (cmbbox.Text, "#E1");
1095                         Assert.AreEqual ("Bad", cmbbox.Text, "#E2");
1096                         Assert.AreEqual (1, cmbbox.SelectedIndex, "#E3");
1097
1098                         cmbbox.Text = null;
1099                         Assert.IsNotNull (cmbbox.Text, "#F1");
1100                         Assert.AreEqual (string.Empty, cmbbox.Text, "#F2");
1101                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#F3");
1102
1103                         cmbbox.SelectedIndex = 0;
1104                         cmbbox.Text = "Q";
1105                         Assert.IsNotNull (cmbbox.Text, "#G1");
1106                         Assert.AreEqual ("Q", cmbbox.Text, "#G2");
1107                         Assert.AreEqual (0, cmbbox.SelectedIndex, "#G3");
1108
1109                         cmbbox.Text = "B";
1110                         Assert.IsNotNull (cmbbox.Text, "#H1");
1111                         Assert.AreEqual ("B", cmbbox.Text, "#H2");
1112                         Assert.AreEqual (0, cmbbox.SelectedIndex, "#H3");
1113
1114                         cmbbox.Text = "BAD";
1115                         Assert.IsNotNull (cmbbox.Text, "#I1");
1116                         Assert.AreEqual ("BAD", cmbbox.Text, "#I2");
1117                         Assert.AreEqual (4, cmbbox.SelectedIndex, "#I3");
1118
1119                         cmbbox.Text = "BAD";
1120                         Assert.IsNotNull (cmbbox.Text, "#J1");
1121                         Assert.AreEqual ("BAD", cmbbox.Text, "#J2");
1122                         Assert.AreEqual (4, cmbbox.SelectedIndex, "#J3");
1123
1124                         cmbbox.Text = "baD";
1125                         Assert.IsNotNull (cmbbox.Text, "#K1");
1126                         Assert.AreEqual ("Bad", cmbbox.Text, "#K2");
1127                         Assert.AreEqual (1, cmbbox.SelectedIndex, "#K3");
1128
1129                         cmbbox.SelectedIndex = -1;
1130                         cmbbox.Text = "E";
1131                         Assert.IsNotNull (cmbbox.Text, "#L1");
1132                         Assert.AreEqual ("E", cmbbox.Text, "#L2");
1133                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#L3");
1134
1135                         cmbbox.Text = "iN";
1136                         Assert.IsNotNull (cmbbox.Text, "#M1");
1137                         Assert.AreEqual ("iN", cmbbox.Text, "#M2");
1138                         Assert.AreEqual (5, cmbbox.SelectedIndex, "#M3");
1139
1140                         cmbbox.Text = "In";
1141                         Assert.IsNotNull (cmbbox.Text, "#N1");
1142                         Assert.AreEqual ("IN", cmbbox.Text, "#N2");
1143                         Assert.AreEqual (2, cmbbox.SelectedIndex, "#N3");
1144
1145                         cmbbox.Text = "Badd";
1146                         Assert.IsNotNull (cmbbox.Text, "#O1");
1147                         Assert.AreEqual ("Badd", cmbbox.Text, "#O2");
1148                         Assert.AreEqual (2, cmbbox.SelectedIndex, "#O3");
1149
1150                         cmbbox.SelectedIndex = -1;
1151                         Assert.IsNotNull (cmbbox.Text, "#P1");
1152                         Assert.AreEqual (string.Empty, cmbbox.Text, "#P2");
1153                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#P3");
1154
1155                         cmbbox.Text = "Something";
1156                         Assert.IsNotNull (cmbbox.Text, "#Q1");
1157                         Assert.AreEqual ("Something", cmbbox.Text, "#Q2");
1158                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#Q3");
1159
1160                         cmbbox.SelectedIndex = -1;
1161                         Assert.IsNotNull (cmbbox.Text, "#R1");
1162                         Assert.AreEqual ("Something", cmbbox.Text, "#R2");
1163                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#R3");
1164
1165                         cmbbox.Text = null;
1166                         Assert.IsNotNull (cmbbox.Text, "#S1");
1167                         Assert.AreEqual (string.Empty, cmbbox.Text, "#S2");
1168                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#S3");
1169                 }
1170
1171                 [Test]
1172                 public void Text_DataBinding ()
1173                 {
1174                         ArrayList objects = new ArrayList ();
1175                         objects.Add (new MockItem ("A", 0));
1176                         objects.Add (new MockItem ("B", 1));
1177
1178                         ComboBox cb = new ComboBox ();
1179                         cb.BindingContext = new BindingContext ();
1180                         cb.DataSource = objects;
1181                         cb.DisplayMember = "Text";
1182
1183                         Assert.AreEqual ("A", cb.Text, "#A1");
1184                         Assert.AreEqual ("A", ((MockItem)cb.SelectedItem).Text, "#B2");
1185                         Assert.AreEqual (0, ((MockItem)cb.SelectedItem).Value, "#B3");
1186
1187                         cb.Text = "B";
1188                         Assert.AreEqual ("B", cb.Text, "#B1");
1189                         Assert.AreEqual ("B", ((MockItem)cb.SelectedItem).Text, "#B2");
1190                         Assert.AreEqual (1, ((MockItem)cb.SelectedItem).Value, "#B3");
1191
1192                         // the text will change graphically, but Text and
1193                         // SelectedItem will keep their previous valid values.
1194                         cb.Text = "dontexist";
1195                         Assert.AreEqual ("B", ((MockItem)cb.SelectedItem).Text, "#C2");
1196                         Assert.AreEqual (1, ((MockItem)cb.SelectedItem).Value, "#C3");
1197                 }
1198
1199                 [Test]  // bug 360862
1200                 public void SizeChangesAtCreateHandle ()
1201                 {
1202                         ComboBox cb = new ComboBox ();
1203                         cb.Font = new Font ("Arial", 24f);
1204                         
1205                         int original = cb.Height;
1206                         
1207                         IntPtr h = cb.Handle;
1208                         
1209                         Assert.IsTrue (cb.Height > original, string.Format ("ComboBox height ({0}) should be bigger than original ({1})", cb.Height, original));
1210                 }
1211                 
1212                 [Test]
1213                 public void Bug424270 ()
1214                 {
1215                         ComboBox cb = new ComboBox ();
1216                         cb.Items.Add ("ab");
1217                         
1218                         cb.SelectedIndex = 0;
1219
1220                         Assert.AreEqual (0, cb.SelectedIndex, "A1");
1221                         Assert.AreEqual ("ab", cb.SelectedItem, "A2");
1222                         
1223                         cb.SelectedItem = null;
1224                         
1225                         Assert.AreEqual (-1, cb.SelectedIndex, "A3");
1226                         Assert.AreEqual (null, cb.SelectedItem, "A4");
1227                 }
1228                 
1229 #if NET_2_0
1230                 [Test]
1231                 public void BehaviorAutoSize ()
1232                 {
1233                         if (TestHelper.RunningOnUnix)
1234                                 Assert.Ignore ("Depends on font measurements, corresponds to windows");
1235
1236                         Form f = new Form ();
1237                         f.ShowInTaskbar = false;
1238
1239                         f.Show ();
1240
1241                         Image i = new Bitmap (20, 20);
1242                         String s = "My test string";
1243
1244                         CheckBox b = new CheckBox ();
1245                         Size s_size = TextRenderer.MeasureText (s, b.Font);
1246
1247                         b.UseCompatibleTextRendering = false;
1248                         b.Size = new Size (5, 5);
1249                         b.AutoSize = true;
1250                         b.Text = s;
1251                         f.Controls.Add (b);
1252
1253                         // Text only
1254                         b.TextImageRelation = TextImageRelation.Overlay;
1255                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A1");
1256                         b.TextImageRelation = TextImageRelation.ImageAboveText;
1257                         b.Size = new Size (5, 5);
1258                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A2");
1259                         b.TextImageRelation = TextImageRelation.ImageBeforeText;
1260                         b.Size = new Size (5, 5);
1261                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A3");
1262                         b.TextImageRelation = TextImageRelation.TextAboveImage;
1263                         b.Size = new Size (5, 5);
1264                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A4");
1265                         b.TextImageRelation = TextImageRelation.TextBeforeImage;
1266                         b.Size = new Size (5, 5);
1267                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A5");
1268
1269                         // Text and Image
1270                         b.Image = i;
1271                         b.TextImageRelation = TextImageRelation.Overlay;
1272                         b.Size = new Size (5, 5);
1273                         Assert.AreEqual (new Size (s_size.Width + 19, i.Height), b.Size, "A6");
1274                         b.TextImageRelation = TextImageRelation.ImageAboveText;
1275                         b.Size = new Size (5, 5);
1276                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A7");
1277                         b.TextImageRelation = TextImageRelation.ImageBeforeText;
1278                         b.Size = new Size (5, 5);
1279                         Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A8");
1280                         b.TextImageRelation = TextImageRelation.TextAboveImage;
1281                         b.Size = new Size (5, 5);
1282                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A9");
1283                         b.TextImageRelation = TextImageRelation.TextBeforeImage;
1284                         b.Size = new Size (5, 5);
1285                         Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A10");
1286
1287                         // Image only
1288                         b.Text = string.Empty;
1289                         b.TextImageRelation = TextImageRelation.Overlay;
1290                         b.Size = new Size (5, 5);
1291                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A11");
1292                         b.TextImageRelation = TextImageRelation.ImageAboveText;
1293                         b.Size = new Size (5, 5);
1294                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A12");
1295                         b.TextImageRelation = TextImageRelation.ImageBeforeText;
1296                         b.Size = new Size (5, 5);
1297                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A13");
1298                         b.TextImageRelation = TextImageRelation.TextAboveImage;
1299                         b.Size = new Size (5, 5);
1300                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A14");
1301                         b.TextImageRelation = TextImageRelation.TextBeforeImage;
1302                         b.Size = new Size (5, 5);
1303                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A15");
1304
1305                         // Neither
1306                         b.Image = null;
1307                         b.TextImageRelation = TextImageRelation.Overlay;
1308                         b.Size = new Size (5, 5);
1309                         Assert.AreEqual (new Size (15, 14), b.Size, "A16");
1310                         b.TextImageRelation = TextImageRelation.ImageAboveText;
1311                         b.Size = new Size (5, 5);
1312                         Assert.AreEqual (new Size (15, 14), b.Size, "A17");
1313                         b.TextImageRelation = TextImageRelation.ImageBeforeText;
1314                         b.Size = new Size (5, 5);
1315                         Assert.AreEqual (new Size (15, 14), b.Size, "A18");
1316                         b.TextImageRelation = TextImageRelation.TextAboveImage;
1317                         b.Size = new Size (5, 5);
1318                         Assert.AreEqual (new Size (15, 14), b.Size, "A19");
1319                         b.TextImageRelation = TextImageRelation.TextBeforeImage;
1320                         b.Size = new Size (5, 5);
1321                         Assert.AreEqual (new Size (15, 14), b.Size, "A20");
1322
1323                         // Padding
1324                         b.Padding = new Padding (5, 10, 15, 20);
1325                         Assert.AreEqual (new Size (15 + b.Padding.Horizontal, 14 + b.Padding.Vertical), b.Size, "A21");
1326
1327                         f.Dispose ();
1328                 }
1329
1330                 [Test]
1331                 public void MethodScaleControl ()
1332                 {
1333                         Form f = new Form ();
1334                         f.ShowInTaskbar = false;
1335
1336                         f.Show ();
1337
1338                         PublicComboBox gb = new PublicComboBox ();
1339                         gb.Location = new Point (5, 10);
1340                         f.Controls.Add (gb);
1341
1342                         Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A1");
1343
1344                         gb.PublicScaleControl (new SizeF (2.0f, 2.0f), BoundsSpecified.All);
1345                         Assert.AreEqual (new Rectangle (10, 20, 238, gb.PreferredHeight), gb.Bounds, "A2");
1346
1347                         gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Location);
1348                         Assert.AreEqual (new Rectangle (5, 10, 238, gb.PreferredHeight), gb.Bounds, "A3");
1349
1350                         gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Size);
1351                         Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A4");
1352
1353                         gb.PublicScaleControl (new SizeF (3.5f, 3.5f), BoundsSpecified.Size);
1354                         Assert.AreEqual (new Rectangle (5, 10, 414, gb.PreferredHeight), gb.Bounds, "A5");
1355
1356                         gb.PublicScaleControl (new SizeF (2.5f, 2.5f), BoundsSpecified.Size);
1357                         Assert.AreEqual (new Rectangle (5, 10, 1029, gb.PreferredHeight), gb.Bounds, "A6");
1358
1359                         gb.PublicScaleControl (new SizeF (.2f, .2f), BoundsSpecified.Size);
1360                         Assert.AreEqual (new Rectangle (5, 10, 209, gb.PreferredHeight), gb.Bounds, "A7");
1361
1362                         f.Dispose ();
1363                 }
1364
1365                 private class PublicComboBox : ComboBox
1366                 {
1367                         public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
1368                         {
1369                                 base.ScaleControl (factor, specified);
1370                         }
1371                 }
1372 #endif
1373
1374                 private struct ComboVal
1375                 {
1376                         public string text;
1377
1378                         public ComboVal (string t)
1379                         {
1380                                 text = t;
1381                         }
1382
1383                         public override string ToString ()
1384                         {
1385                                 return text;
1386                         }
1387                 }
1388                 
1389                 [Test]
1390                 public void SortTest ()
1391                 {
1392                         // Test sorting of objects with no IComparer
1393                         // should not crash
1394
1395                         ComboBox cmb = new ComboBox ();
1396                         cmb.Items.Add (new ComboVal ("B"));
1397                         cmb.Items.Add (new ComboVal ("A"));
1398                         cmb.Sorted = true;
1399                 }
1400         }
1401
1402         [TestFixture]
1403         public class ComboBoxObjectCollectionTest : TestHelper
1404         {
1405                 [Test]
1406                 public void ComboBoxObjectCollectionPropertyTest ()
1407                 {
1408                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1409                         Assert.AreEqual (false, col.IsReadOnly, "#B1");
1410                         Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
1411                         Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
1412                         Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
1413                 }
1414
1415                 [Test]
1416                 public void AddTest ()
1417                 {
1418                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1419                         col.Add ("Item1");
1420                         col.Add ("Item2");
1421                         Assert.AreEqual (2, col.Count, "#C1");
1422                 }
1423
1424                 [Test]
1425                 public void Add_Null ()
1426                 {
1427                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1428                         try {
1429                                 col.Add (null);
1430                                 Assert.Fail ("#1");
1431                         } catch (ArgumentNullException ex) {
1432                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1433                                 Assert.IsNull (ex.InnerException, "#3");
1434                                 Assert.IsNotNull (ex.Message, "#4");
1435                                 Assert.IsNotNull (ex.ParamName, "#5");
1436                                 Assert.AreEqual ("item", ex.ParamName, "#6");
1437                         }
1438                 }
1439
1440                 [Test]
1441                 public void AddRange_Null ()
1442                 {
1443                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1444                         try {
1445                                 col.AddRange (null);
1446                                 Assert.Fail ("#1");
1447                         } catch (ArgumentNullException ex) {
1448                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1449                                 Assert.IsNull (ex.InnerException, "#3");
1450                                 Assert.IsNotNull (ex.Message, "#4");
1451                                 Assert.IsNotNull (ex.ParamName, "#5");
1452                                 Assert.AreEqual ("items", ex.ParamName, "#6");
1453                         }
1454                 }
1455
1456                 [Test]
1457                 public void ClearTest ()
1458                 {
1459                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1460                         col.Add ("Item1");
1461                         col.Add ("Item2");
1462                         col.Clear ();
1463                         Assert.AreEqual (0, col.Count, "#D1");
1464                 }
1465
1466                 [Test]
1467                 public void ContainsTest ()
1468                 {
1469                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1470                         object obj = "Item1";
1471                         col.Add (obj);
1472                         Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
1473                         Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
1474                 }
1475
1476                 [Test]
1477                 public void Contains_Null ()
1478                 {
1479                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1480                         try {
1481                                 col.Contains (null);
1482                                 Assert.Fail ("#1");
1483                         } catch (ArgumentNullException ex) {
1484                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1485                                 Assert.IsNull (ex.InnerException, "#3");
1486                                 Assert.IsNotNull (ex.Message, "#4");
1487                                 Assert.IsNotNull (ex.ParamName, "#5");
1488 #if NET_2_0
1489                                 Assert.AreEqual ("value", ex.ParamName, "#6");
1490 #endif
1491                         }
1492                 }
1493
1494                 [Test]
1495                 public void IndexOfTest ()
1496                 {
1497                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1498                         col.Add ("Item1");
1499                         col.Add ("Item2");
1500                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
1501                 }
1502
1503                 [Test]
1504                 public void IndexOf_Null ()
1505                 {
1506                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1507                         try {
1508                                 col.IndexOf (null);
1509                                 Assert.Fail ("#1");
1510                         } catch (ArgumentNullException ex) {
1511                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1512                                 Assert.IsNull (ex.InnerException, "#3");
1513                                 Assert.IsNotNull (ex.Message, "#4");
1514                                 Assert.IsNotNull (ex.ParamName, "#5");
1515 #if NET_2_0
1516                                 Assert.AreEqual ("value", ex.ParamName, "#6");
1517 #endif
1518                         }
1519                 }
1520
1521                 [Test]
1522                 public void Insert_Null ()
1523                 {
1524                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1525                         col.Add ("Item1");
1526                         try {
1527                                 col.Insert (0, null);
1528                                 Assert.Fail ("#1");
1529                         } catch (ArgumentNullException ex) {
1530                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1531                                 Assert.IsNull (ex.InnerException, "#3");
1532                                 Assert.IsNotNull (ex.Message, "#4");
1533                                 Assert.IsNotNull (ex.ParamName, "#5");
1534                                 Assert.AreEqual ("item", ex.ParamName, "#6");
1535                         }
1536                 }
1537
1538                 [Test]
1539                 public void RemoveTest ()
1540                 {
1541                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1542                         col.Add ("Item1");
1543                         col.Add ("Item2");
1544                         col.Remove ("Item1");
1545                         Assert.AreEqual (1, col.Count, "#1");
1546                         col.Remove (null);
1547                         Assert.AreEqual (1, col.Count, "#2");
1548                 }
1549
1550                 [Test]
1551                 public void RemoveAtTest ()
1552                 {
1553                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1554                         col.Add ("Item1");
1555                         col.Add ("Item2");
1556                         col.RemoveAt (0);
1557                         Assert.AreEqual (1, col.Count, "#H1");
1558                         Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
1559                 }
1560
1561                 [Test]
1562                 public void Indexer_Null ()
1563                 {
1564                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1565                         col.Add ("Item1");
1566                         try {
1567                                 col [0] = null;
1568                                 Assert.Fail ("#1");
1569                         } catch (ArgumentNullException ex) {
1570                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1571                                 Assert.IsNull (ex.InnerException, "#3");
1572                                 Assert.IsNotNull (ex.Message, "#4");
1573                                 Assert.IsNotNull (ex.ParamName, "#5");
1574                                 Assert.AreEqual ("value", ex.ParamName, "#6");
1575                         }
1576                 }
1577         }
1578         [TestFixture]
1579         public class ComboBoxTests : TestHelper
1580         {
1581                 ComboBox comboBox;
1582                 bool textChanged, layoutUpdated;
1583
1584                 [SetUp]
1585                 protected override void SetUp () {
1586                         comboBox = new ComboBox ();
1587                         textChanged = false;
1588                         layoutUpdated = false;
1589                         comboBox.TextChanged += new EventHandler (textChangedEventHandler);
1590                         comboBox.Layout += new LayoutEventHandler (layoutEventHandler);
1591                         base.SetUp ();
1592                 }
1593
1594                 private void textChangedEventHandler (object sender, EventArgs e)
1595                 {
1596                         textChanged = true;
1597                 }
1598
1599                 private void layoutEventHandler (object sender, LayoutEventArgs e)
1600                 {
1601                         layoutUpdated = true;
1602                 }
1603
1604                 [Test]
1605                 public void InitialPropertyValues ()
1606                 {
1607
1608                         Assert.AreEqual (String.Empty, comboBox.Text);
1609                         Assert.AreEqual (-1, comboBox.SelectedIndex);
1610                         Assert.IsNull (comboBox.SelectedItem);
1611                         Assert.AreEqual (121, comboBox.Size.Width);
1612                         //Note: it is environment dependent
1613                         //Assert.AreEqual(20, comboBox.Size.Height);
1614                         Assert.IsFalse (textChanged);
1615                         Assert.IsFalse (layoutUpdated);
1616                 }
1617
1618                 [Test]
1619                 public void SetNegativeOneSelectedIndex ()
1620                 {
1621                         comboBox.SelectedIndex = -1;
1622                         Assert.AreEqual (String.Empty, comboBox.Text);
1623                         Assert.IsFalse (textChanged);
1624                 }
1625
1626                 [Test]
1627                 public void SetDifferentText ()
1628                 {
1629                         comboBox.Text = "foooooooooooooooooooooooooo";
1630                         Assert.IsTrue (textChanged);
1631                         Assert.IsFalse (layoutUpdated);
1632                 }
1633
1634                 [Test]
1635                 public void SetSameText ()
1636                 {
1637                         comboBox.Text = String.Empty;
1638                         Assert.IsFalse (textChanged);
1639                         Assert.IsFalse (layoutUpdated);
1640                 }
1641
1642                 [Test] // bug #79812
1643                 public void Add_Item_NonString ()
1644                 {
1645                         comboBox.Sorted = true;
1646                         comboBox.Items.Add (new Person ("B"));
1647                         comboBox.Items.Add (new Person ("A"));
1648                         comboBox.Items.Add (new Person ("C"));
1649                         Assert.AreEqual (string.Empty, comboBox.Text, "#1");
1650                         comboBox.SelectedIndex = 0;
1651                         Assert.AreEqual ("A", comboBox.Text, "#2");
1652                         comboBox.SelectedIndex = 2;
1653                         Assert.AreEqual ("C", comboBox.Text, "#3");
1654                 }
1655
1656                 [Test]
1657                 [Category ("NotWorking")]
1658                 public void SelectedText ()
1659                 {
1660                         Form form = new Form ();
1661                         form.ShowInTaskbar = false;
1662                         form.Visible = false;
1663                         form.Controls.Add (comboBox);
1664
1665                         comboBox.Items.Add ("Bar");
1666                         comboBox.Items.Add ("Foo");
1667                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#A1");
1668                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#A2");
1669                         comboBox.SelectedIndex = 0;
1670                         Assert.AreEqual (0, comboBox.SelectedIndex, "#B1");
1671                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#B2");
1672                         form.Show ();
1673                         Assert.AreEqual (0, comboBox.SelectedIndex, "#C1");
1674                         Assert.AreEqual ("Bar", comboBox.SelectedText, "#C2");
1675                         comboBox.SelectedIndex = 1;
1676                         Assert.AreEqual (1, comboBox.SelectedIndex, "#D1");
1677                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#D2");
1678                         comboBox.SelectedText = "Ba";
1679                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#E1");
1680                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#E2");
1681                         comboBox.SelectedText = "Bar";
1682                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#F1");
1683                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#F2");
1684                         comboBox.SelectedText = "doesnotexist";
1685                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#G1");
1686                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#G2");
1687                         comboBox.SelectedIndex = 0;
1688                         Assert.AreEqual (0, comboBox.SelectedIndex, "#H1");
1689                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#H2");
1690                         comboBox.SelectedText = "Foo";
1691                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#I1");
1692                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#I2");
1693                 }
1694
1695                 [Test]
1696                 public void SortedSelectedItem ()
1697                 {
1698                         using (Form form = new Form ()) {
1699                                 ComboBox box = new ComboBox ();
1700                                 box.Sorted = true;
1701
1702                                 form.Controls.Add (box);
1703                                 form.ShowInTaskbar = false;
1704                                 form.Show ();
1705
1706                                 box.Items.Add ("English");
1707                                 box.SelectedItem = "English";
1708                                 
1709                                 box.Items.Add ("Danish");
1710                                 Assert.AreEqual ("English", box.SelectedItem, "A1");
1711                                 
1712                                 box.Items.Add ("French");
1713                                 Assert.AreEqual ("English", box.SelectedItem, "A2");
1714                                 
1715                                 box.Items.Add ("Brazilian Portuguese");
1716                                 box.Items.Add ("Arabic");
1717                                 Assert.AreEqual ("English", box.SelectedItem, "A3");
1718                         }
1719                 }
1720                 
1721                 public class Person
1722                 {
1723                         private readonly string _name;
1724
1725                         public Person (string name)
1726                         {
1727                                 _name = name;
1728                         }
1729
1730                         public string Name
1731                         {
1732                                 get
1733                                 {
1734                                         return _name;
1735                                 }
1736                         }
1737
1738                         public override string ToString ()
1739                         {
1740                                 return Name;
1741                         }
1742                 }
1743         }
1744 }