[bcl] Remove more NET_2_0 checks from class libs
[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                         Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#18");
412                         Assert.AreEqual (AutoCompleteMode.None, mycmbbox.AutoCompleteMode, "#19");
413                         Assert.AreEqual (AutoCompleteSource.None, mycmbbox.AutoCompleteSource, "#20");
414
415                         mycmbbox.AutoCompleteCustomSource = null;
416                         Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#21");
417                         
418                         Assert.AreEqual (ImageLayout.Tile, mycmbbox.BackgroundImageLayout, "#22");
419                         Assert.AreEqual (null, mycmbbox.DataSource, "#23");
420                         Assert.AreEqual (106, mycmbbox.DropDownHeight, "#24");
421                         Assert.AreEqual (FlatStyle.Standard, mycmbbox.FlatStyle, "#25");
422                         Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MaximumSize.ToString (), "#26");
423                         Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MinimumSize.ToString (), "#27");
424                         Assert.AreEqual ("{Left=0,Top=0,Right=0,Bottom=0}", mycmbbox.Padding.ToString (), "#28");
425                         
426                 }
427
428                 [Test]
429                 public void PreferredHeight ()
430                 {
431                         // PreferredHeight is not tied to ItemHeight like we had it
432                         ComboBox cb = new ComboBox ();
433                         
434                         int h = cb.PreferredHeight;
435                         
436                         cb.ItemHeight = 10;
437                         Assert.AreEqual (h, cb.PreferredHeight, "A2");
438
439                         cb.ItemHeight = 30;
440                         Assert.AreEqual (h, cb.PreferredHeight, "A3");
441                 }
442                 
443                 [Test]
444                 public void ResetTextTest ()
445                 {
446                         ComboBox cmbbox = new ComboBox ();
447                         Assert.AreEqual ("", cmbbox.Text, "#01");
448                         cmbbox.Text = "abc";
449                         Assert.AreEqual ("abc", cmbbox.Text, "#02");
450                         cmbbox.ResetText ();
451                         Assert.AreEqual ("", cmbbox.Text, "#03");
452                 }
453                 
454                 [Test]
455                 public void BackgroundImageLayoutTest ()
456                 {
457                         ComboBox cmbbox = new ComboBox ();
458                         cmbbox.BackgroundImageLayout = ImageLayout.Stretch;
459                         Assert.AreEqual (ImageLayout.Stretch, cmbbox.BackgroundImageLayout, "#01");
460                 }
461                 
462                 [Test]
463                 public void DropDownHeightTest ()
464                 {
465                         ComboBox cmbbox = new ComboBox ();
466                         cmbbox.DropDownHeight = 225;
467                         Assert.AreEqual (225, cmbbox.DropDownHeight, "#01");
468                         cmbbox.DropDownHeight = 1;
469                         Assert.AreEqual (1, cmbbox.DropDownHeight, "#02");
470                 }
471
472                 [Test]
473                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
474                 public void DropDownHeightExceptionTest1 ()
475                 {
476                         ComboBox cmbbox = new ComboBox ();
477                         cmbbox.DropDownHeight = -225;   
478                 }
479
480                 [Test]
481                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
482                 public void DropDownHeightExceptionTest2 ()
483                 {
484                         ComboBox cmbbox = new ComboBox ();
485                         cmbbox.DropDownHeight = 0;
486                 }
487                 
488                 [Test]
489                 public void FlatStyleTest ()
490                 {
491                         ComboBox cmbbox = new ComboBox ();
492                         cmbbox.FlatStyle = FlatStyle.Popup;
493                         Assert.AreEqual (FlatStyle.Popup, cmbbox.FlatStyle, "#01");
494                 }
495                 
496                 [Test]
497                 [ExpectedException (typeof (InvalidEnumArgumentException))]
498                 public void FlatStyleExceptionTest ()
499                 {
500                         ComboBox cmbbox = new ComboBox ();
501                         cmbbox.FlatStyle = (FlatStyle) (-123);
502                 }
503         
504                 [Test]
505                 public void MaximumSizeTest ()
506                 {
507                         ComboBox cmbbox = new ComboBox ();
508                         cmbbox.MaximumSize = new Size (25, 25);
509                         Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MaximumSize.ToString (), "#01");
510                         cmbbox.MaximumSize = new Size (50, 75);
511                         Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MaximumSize.ToString (), "#02");
512                 }
513         
514                 [Test]
515                 public void MinumumSizeTest ()
516                 {
517                         ComboBox cmbbox = new ComboBox ();
518                         cmbbox.MinimumSize = new Size (25, 25);
519                         Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MinimumSize.ToString (), "#1");
520                         cmbbox.MinimumSize = new Size (50, 75);
521                         Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MinimumSize.ToString (), "#2"); 
522                 }
523                 
524                 [Test]
525                 public void PaddingTest ()
526                 {
527                         ComboBox cmbbox = new ComboBox ();
528                         cmbbox.Padding = new Padding (21);
529                         Assert.AreEqual ("{Left=21,Top=21,Right=21,Bottom=21}", cmbbox.Padding.ToString (), "#01");
530                 }
531
532                 [Test]
533                 public void BeginEndUpdateTest ()
534                 {
535                         Form myform = new Form ();
536                         myform.ShowInTaskbar = false;
537                         myform.Visible = true;
538                         ComboBox cmbbox = new ComboBox ();
539                         cmbbox.Items.Add ("A");
540                         cmbbox.Visible = true;
541                         myform.Controls.Add (cmbbox);
542                         cmbbox.BeginUpdate ();
543                         for (int x = 1 ; x < 5000 ; x++) {
544                                 cmbbox.Items.Add ("Item " + x.ToString ());
545                         }
546                         cmbbox.EndUpdate ();
547                         myform.Dispose ();
548                 }
549
550                 [Test]
551                 public void FindString ()
552                 {
553                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
554
555                         ComboBox cmbbox = new ComboBox ();
556                         Assert.AreEqual (-1, cmbbox.FindString ("Hello"), "#A1");
557                         Assert.AreEqual (-1, cmbbox.FindString (string.Empty), "#A2");
558                         Assert.AreEqual (-1, cmbbox.FindString (null), "#A3");
559                         Assert.AreEqual (-1, cmbbox.FindString ("Hola", -5), "#A4");
560                         Assert.AreEqual (-1, cmbbox.FindString ("Hola", 40), "#A5");
561
562                         cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
563                         Assert.AreEqual (2, cmbbox.FindString ("I"), "#B1");
564                         Assert.AreEqual (0, cmbbox.FindString ("in"), "#B2");
565                         Assert.AreEqual (1, cmbbox.FindString ("BAD"), "#B3");
566                         Assert.AreEqual (1, cmbbox.FindString ("Bad"), "#B4");
567                         Assert.AreEqual (1, cmbbox.FindString ("b"), "#B5");
568                         Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#B6");
569                         Assert.AreEqual (-1, cmbbox.FindString (null), "#B7");
570
571                         Assert.AreEqual (3, cmbbox.FindString ("b", 2), "#C1");
572                         Assert.AreEqual (5, cmbbox.FindString ("I", 3), "#C2");
573                         Assert.AreEqual (4, cmbbox.FindString ("B", 3), "#C3");
574                         Assert.AreEqual (1, cmbbox.FindString ("B", 4), "#C4");
575                         Assert.AreEqual (5, cmbbox.FindString ("I", 4), "#C5");
576                         Assert.AreEqual (4, cmbbox.FindString ("BA", 3), "#C6");
577                         Assert.AreEqual (0, cmbbox.FindString ("i", -1), "#C7");
578                         Assert.AreEqual (3, cmbbox.FindString (string.Empty, 2), "#C8");
579                         Assert.AreEqual (-1, cmbbox.FindString (null, 1), "#C9");
580
581                         cmbbox.Items.Add (string.Empty);
582                         Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#D1");
583                         Assert.AreEqual (-1, cmbbox.FindString (null), "#D2");
584
585                         Assert.AreEqual (4, cmbbox.FindString (string.Empty, 3), "#E1");
586                         Assert.AreEqual (-1, cmbbox.FindString (null, 99), "#E2");
587                         Assert.AreEqual (-1, cmbbox.FindString (null, -5), "#E3");
588                 }
589
590                 [Test]
591                 public void FindString_StartIndex_ItemCount ()
592                 {
593                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
594
595                         ComboBox cmbbox = new ComboBox ();
596                         cmbbox.Items.AddRange (new object [] { "BA", "BB" });
597                         Assert.AreEqual (0, cmbbox.FindString ("b", 1));
598                 }
599
600                 [Test]
601                 public void FindString_StartIndex_Min ()
602                 {
603                         ComboBox cmbbox = new ComboBox ();
604                         cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
605                         try {
606                                 cmbbox.FindString ("Hola", -2);
607                                 Assert.Fail ("#1");
608                         } catch (ArgumentOutOfRangeException ex) {
609                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
610                                 Assert.IsNull (ex.InnerException, "#3");
611                                 Assert.IsNotNull (ex.Message, "#4");
612                                 Assert.IsNotNull (ex.ParamName, "#5");
613                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
614                         }
615                 }
616
617                 [Test]
618                 public void FindString_StartIndex_Max ()
619                 {
620                         ComboBox cmbbox = new ComboBox ();
621                         cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
622                         try {
623                                 cmbbox.FindString ("Hola", 4);
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 FindStringExact ()
636                 {
637                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
638
639                         ComboBox cmbbox = new ComboBox ();
640                         Assert.AreEqual (-1, cmbbox.FindStringExact ("Hello"), "#A1");
641                         Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#A2");
642                         Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#A3");
643                         Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", -5), "#A4");
644                         Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", 40), "#A5");
645
646                         cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
647                         Assert.AreEqual (2, cmbbox.FindStringExact ("IN"), "#B1");
648                         Assert.AreEqual (0, cmbbox.FindStringExact ("in"), "#B2");
649                         Assert.AreEqual (3, cmbbox.FindStringExact ("BAD"), "#B3");
650                         Assert.AreEqual (3, cmbbox.FindStringExact ("bad"), "#B4");
651                         Assert.AreEqual (-1, cmbbox.FindStringExact ("B"), "#B5");
652                         Assert.AreEqual (-1, cmbbox.FindStringExact ("NonExistant"), "#B6");
653                         Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#B7");
654                         Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#B8");
655
656                         Assert.AreEqual (2, cmbbox.FindStringExact ("In", 1), "#C1");
657                         Assert.AreEqual (5, cmbbox.FindStringExact ("In", 2), "#C2");
658                         Assert.AreEqual (4, cmbbox.FindStringExact ("BaD", 3), "#C3");
659                         Assert.AreEqual (3, cmbbox.FindStringExact ("bad", -1), "#C4");
660                         Assert.AreEqual (5, cmbbox.FindStringExact ("In", 4), "#C5");
661                         Assert.AreEqual (3, cmbbox.FindStringExact ("bad", 4), "#C6");
662                         Assert.AreEqual (-1, cmbbox.FindStringExact ("B", 4), "#C7");
663                         Assert.AreEqual (-1, cmbbox.FindStringExact ("BADNOT", 0), "#C8");
664                         Assert.AreEqual (-1, cmbbox.FindStringExact ("i", -1), "#C9");
665                         Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty, 2), "#C10");
666                         Assert.AreEqual (-1, cmbbox.FindStringExact (null, 1), "#C11");
667
668                         cmbbox.Items.Add (string.Empty);
669                         Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty), "#D1");
670                         Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#D2");
671
672                         Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty, 3), "#E1");
673                         Assert.AreEqual (-1, cmbbox.FindStringExact (null, 99), "#E2");
674                         Assert.AreEqual (-1, cmbbox.FindStringExact (null, -5), "#E3");
675                 }
676
677                 [Test]
678                 public void FindStringExact_StartIndex_ItemCount ()
679                 {
680                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
681
682                         ComboBox cmbbox = new ComboBox ();
683                         cmbbox.Items.AddRange (new object [] { "AB", "BA", "AB", "BA" });
684                         Assert.AreEqual (1, cmbbox.FindStringExact ("BA", 3));
685                 }
686
687                 [Test]
688                 public void FindStringExact_StartIndex_Min ()
689                 {
690                         ComboBox cmbbox = new ComboBox ();
691                         cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
692                         try {
693                                 cmbbox.FindStringExact ("Hola", -2);
694                                 Assert.Fail ("#1");
695                         } catch (ArgumentOutOfRangeException ex) {
696                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
697                                 Assert.IsNull (ex.InnerException, "#3");
698                                 Assert.IsNotNull (ex.Message, "#4");
699                                 Assert.IsNotNull (ex.ParamName, "#5");
700                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
701                         }
702                 }
703
704                 [Test]
705                 public void FindStringExact_StartIndex_Max ()
706                 {
707                         ComboBox cmbbox = new ComboBox ();
708                         cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
709                         try {
710                                 cmbbox.FindStringExact ("Hola", 4);
711                                 Assert.Fail ("#1");
712                         } catch (ArgumentOutOfRangeException ex) {
713                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
714                                 Assert.IsNull (ex.InnerException, "#3");
715                                 Assert.IsNotNull (ex.Message, "#4");
716                                 Assert.IsNotNull (ex.ParamName, "#5");
717                                 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
718                         }
719                 }
720
721                 [Test]
722                 public void GetItemHeightTest ()
723                 {
724                         ComboBox cmbbox = new ComboBox ();
725                         cmbbox.Items.Add ("ABC");
726                         cmbbox.Items.Add ("BCD");
727                         cmbbox.Items.Add ("DEF");
728                         int x = -1;
729                         x = cmbbox.GetItemHeight (x);
730                         Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
731                 }
732
733                 //
734                 // Exceptions
735                 //
736
737                 [Test]
738                 [ExpectedException (typeof (InvalidEnumArgumentException))]
739                 public void DropDownStyleException ()
740                 {
741                         ComboBox cmbbox = new ComboBox ();
742                         cmbbox.DropDownStyle = (ComboBoxStyle) 10;
743                 }
744
745                 [Test]
746                 [ExpectedException (typeof (InvalidEnumArgumentException))]
747                 public void DrawModeException ()
748                 {
749                         ComboBox cmbbox = new ComboBox ();
750                         cmbbox.DrawMode = (DrawMode) 10;
751                 }
752
753                 [Test]
754                 public void DropDownWidth ()
755                 {
756                         ComboBox cmbbox = new ComboBox ();
757                         Assert.AreEqual (121, cmbbox.DropDownWidth, "#A1");
758                         cmbbox.DropDownWidth = 1;
759                         Assert.AreEqual (1, cmbbox.DropDownWidth, "#A2");
760
761                         try {
762                                 cmbbox.DropDownWidth = 0;
763                                 Assert.Fail ("#B1");
764                         }
765                         catch (ArgumentOutOfRangeException ex) {
766                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
767                                 Assert.IsNotNull (ex.Message, "#B3");
768                                 Assert.IsNotNull (ex.ParamName, "#B4");
769                                 Assert.AreEqual ("DropDownWidth", ex.ParamName, "#B5");
770                                 Assert.IsNull (ex.InnerException, "#B6");
771                         }
772                 }
773
774                 [Test]
775                 public void ItemHeight ()
776                 {
777                         ComboBox cmbbox = new ComboBox ();
778                         Assert.IsTrue (cmbbox.ItemHeight >= 1, "#A1");
779                         cmbbox.ItemHeight = 1;
780                         Assert.AreEqual (1, cmbbox.ItemHeight, "#A2");
781
782                         try {
783                                 cmbbox.ItemHeight = 0;
784                                 Assert.Fail ("#B1");
785                         }
786                         catch (ArgumentOutOfRangeException ex) {
787                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
788                                 Assert.IsNotNull (ex.Message, "#B3");
789                                 Assert.IsNotNull (ex.ParamName, "#B4");
790                                 Assert.AreEqual ("ItemHeight", ex.ParamName, "#B5");
791                                 Assert.IsNull (ex.InnerException, "#B6");
792                         }
793                 }
794
795                 [Test]
796                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
797                 public void SelectedIndexException ()
798                 {
799                         ComboBox cmbbox = new ComboBox ();
800                         cmbbox.SelectedIndex = -2;
801                 }
802                 
803                 //Bug 2234 (Xamarin) : Test 1
804                 [Test]
805                 public void VerifyNoExceptions2234()
806                 {
807                         using (Form form = new Form ()){
808                                 ComboBox cmb = new ComboBox();
809                                 form.Controls.Add (cmb);
810                                 form.Show ();
811                                 eventFired=false;  //for sanity
812                         
813                                 //Primary failure: if exception is raised when
814                                 //   DataSource changes.  We should "eat" the 
815                                 //   exception under this circumstance before 
816                                 //   it gets here.
817                                 cmb.SelectedIndexChanged += 
818                                         new EventHandler(GenericHandlerWithException);
819                                 cmb.DataSource=new string[]{"One","Two","Three"};
820                                 Assert.IsTrue(eventFired);
821                         }
822                 }
823                 
824                 //Bug 2234 (Xamarin) : Test 2
825                 [Test]
826                 [ExpectedException (typeof (Exception))]
827                 public void VerifyException2234()
828                 {
829                         using (Form form = new Form ())
830                         {
831                                 ComboBox cmb = new ComboBox();
832                                 form.Controls.Add (cmb);
833                                 form.Show ();
834                                 cmb.SelectedIndexChanged +=     
835                                         new EventHandler(GenericHandlerWithException);
836                                 cmb.DataSource=new string[]{"One","Two","Three"};
837                         
838                                 //Here's where Exception Should raise normally
839                                 cmb.SelectedIndex=2;
840                         }       
841                 }
842
843
844                 //
845                 // Events
846                 //
847                 private bool eventFired;
848                 private DrawItemEventArgs drawItemsArgs;
849                 private void DrawItemEventH (object sender,  DrawItemEventArgs e)
850                 {
851                         eventFired = true;
852                         drawItemsArgs = e;
853                 }
854
855                 private void GenericHandler (object sender,  EventArgs e)
856                 {
857                         eventFired = true;
858                 }
859
860                 private void GenericHandlerWithException (object sender,  EventArgs e)
861                 {
862                         eventFired = true;
863                         throw new Exception("Crash!");
864                 }
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                 // Xamarin bug 5595
955                 // https://bugzilla.xamarin.com/show_bug.cgi?id=5595
956                 public void SelectionWithDeletion()
957                 {
958                         Form form = null;
959
960                         try
961                         {
962                                 // Create a form with a combo box.
963                                 form = new Form ();
964                                 form.ShowInTaskbar = false;
965                                 ComboBox cb = new ComboBox ();
966                                 cb.DropDownStyle = ComboBoxStyle.DropDownList;
967                                 cb.Parent = form;
968                                 form.Show ();
969
970                                 // Add some items to the combo box.
971                                 cb.Items.Add ("Item 0");
972                                 cb.Items.Add ("Item 1");
973                                 cb.Items.Add ("Item 2");
974
975                                 // Select the last item.
976                                 cb.SelectedIndex = 2;
977                                 Assert.AreEqual(2, cb.SelectedIndex, "SWD1");
978
979                                 // Show the combo box's dropdown.
980                                 cb.DroppedDown = true;
981
982                                 // Display the results.
983                                 Application.DoEvents();
984
985                                 // Hide the combo box's dropdown.
986                                 cb.DroppedDown = false;
987
988                                 // Display the results.
989                                 Application.DoEvents();
990
991                                 // Delete an item before the selection.
992                                 // That should move the selection down.
993                                 // Before the bug fix, it would remain 2.
994                                 cb.Items.RemoveAt (1);
995                                 Assert.AreEqual(1, cb.SelectedIndex, "SWD2");
996
997                                 // Show the combo box's dropdown.
998                                 // Before the bug fix, this would throw an
999                                 // ArgumentOutOfRangeException, because the
1000                                 // selected index was still 2, and hence
1001                                 // invalid.)
1002                                 cb.DroppedDown = true;
1003                                 Assert.AreEqual(1, cb.SelectedIndex, "SWD3");
1004
1005                                 // Display the results.
1006                                 Application.DoEvents();
1007                         }
1008                         finally
1009                         {
1010                                 // Get rid of the form.
1011                                 if (form != null)
1012                                         form.Dispose ();
1013                         }
1014                 }
1015
1016                 [Test]
1017                 public void SelectionWithClear()
1018                 {
1019                         ComboBox cb = new ComboBox();
1020                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
1021                         cb.Items.Add("Item 1");
1022                         cb.SelectedIndex = 0;
1023                         eventFired = false;
1024                         cb.Items.Clear();
1025                         Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
1026                         Assert.AreEqual(false, eventFired, "SWC2");
1027                 }
1028
1029                 [Test]
1030                 public void SelectionWithModification()
1031                 {
1032                         // ComboBox's this[int].set() was dealing with the
1033                         // current index badly.  This reproduces the old bug and
1034                         // makes sure it's fixed.
1035                         ComboBox cb = new ComboBox ();
1036                         cb.DropDownStyle = ComboBoxStyle.DropDown;
1037
1038                         // Add a menu item.
1039                         string strItemText = "Item text";
1040                         cb.Items.Add (strItemText);
1041
1042                         // Select the menu item.
1043                         cb.SelectedIndex = 0;
1044
1045                         // Move the selection to the end of the text.
1046                         cb.SelectionLength = 0;
1047                         cb.SelectionStart = strItemText.Length;
1048
1049                         // Now set the menu item's text to the empty string and
1050                         // back again.
1051                         cb.Items[0] = string.Empty;
1052                         cb.Items[0] = strItemText;
1053
1054                         // Make sure the text didn't get duplicated (i.e. the
1055                         // bugged code would produce "Item textItem text").
1056                         Assert.AreEqual (strItemText, cb.Text, "A1");
1057                 }
1058
1059                 // Bug #333750 - DisplayMember assignation 
1060                 // when ComboBox already has a BindingContext BUT
1061                 // handle hasn't been created yet.
1062                 [Test]
1063                 public void SelectedTextWithBindingTest ()
1064                 {
1065                         Form form = new Form ();
1066                         form.ShowInTaskbar = false;
1067                         ComboBox cb = new ComboBox ();
1068                         cb.Parent = form;
1069
1070                         MockItem [] items = new MockItem [] {
1071                                 new MockItem ("A", 0),
1072                                 new MockItem ("B", 1),
1073                                 new MockItem ("C", 2)
1074                         };
1075
1076                         cb.DataSource = items;
1077                         cb.DisplayMember = "Text";
1078
1079                         form.Show ();
1080
1081                         Assert.AreEqual ("A", cb.SelectedText, "#A1");
1082
1083                         form.Dispose ();
1084                 }
1085
1086                 [Test]
1087                 public void SortedTest()
1088                 {
1089                         ComboBox mycb = new ComboBox();
1090                         Assert.AreEqual(false, mycb.Sorted, "#1");
1091                         mycb.Items.Add("Item 2");
1092                         mycb.Items.Add("Item 1");
1093                         Assert.AreEqual("Item 2", mycb.Items[0], "#2");
1094                         Assert.AreEqual("Item 1", mycb.Items[1], "#3");
1095                         mycb.Sorted = true;
1096                         Assert.AreEqual(true, mycb.Sorted, "#4");
1097                         Assert.AreEqual("Item 1", mycb.Items[0], "#5");
1098                         Assert.AreEqual("Item 2", mycb.Items[1], "#6");
1099                         mycb.Sorted = false;
1100                         Assert.AreEqual(false, mycb.Sorted, "#7");
1101                         Assert.AreEqual("Item 1", mycb.Items[0], "#8");
1102                         Assert.AreEqual("Item 2", mycb.Items[1], "#9");
1103                 }
1104
1105                 [Test]
1106                 public void SortedAddTest()
1107                 {
1108                         ComboBox mycb = new ComboBox();
1109                         mycb.Items.Add("Item 2");
1110                         mycb.Items.Add("Item 1");
1111                         mycb.Sorted = true;
1112                         Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
1113                         Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
1114                 }
1115
1116                 [Test]
1117                 public void SortedInsertTest()
1118                 {
1119                         ComboBox mycb = new ComboBox();
1120                         mycb.Items.Add("Item 2");
1121                         mycb.Items.Add("Item 1");
1122                         mycb.Sorted = true;
1123                         mycb.Items.Insert (0, "Item 3");
1124                         Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
1125                         Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
1126                         Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
1127                 }
1128
1129                 [Test]
1130                 public void SortedSelectionInteractions()
1131                 {
1132                         ComboBox cb = new ComboBox();
1133                         cb.SelectedIndexChanged += new EventHandler(GenericHandler);
1134                         cb.Items.Add("Item 1");
1135                         cb.Items.Add("Item 2");
1136                         cb.Items.Add("Item 3");
1137                         cb.SelectedIndex = 1;
1138                         eventFired = false;
1139                         cb.Sorted = true;
1140                         Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
1141                         Assert.AreEqual(true, eventFired, "#SSI2");
1142                         cb.SelectedIndex = 1;
1143                         eventFired = false;
1144                         cb.Sorted = true;
1145                         Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
1146                         Assert.AreEqual(false, eventFired, "#SSI4");
1147                         cb.SelectedIndex = 1;
1148                         eventFired = false;
1149                         cb.Sorted = false;
1150                         Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
1151                         Assert.AreEqual(true, eventFired, "#SSI6");
1152                 }
1153
1154                 [Test]
1155                 public void Text_DropDown ()
1156                 {
1157                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
1158
1159                         ComboBox cmbbox = new ComboBox ();
1160                         Assert.IsNotNull (cmbbox.Text, "#A1");
1161                         Assert.AreEqual (string.Empty, cmbbox.Text, "#A2");
1162                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#A3");
1163
1164                         cmbbox.Items.Add ("Another");
1165                         cmbbox.Items.Add ("Bad");
1166                         cmbbox.Items.Add ("IN");
1167                         cmbbox.Items.Add ("Combobox");
1168                         cmbbox.Items.Add ("BAD");
1169                         cmbbox.Items.Add ("iN");
1170                         cmbbox.Items.Add ("Bad");
1171
1172                         Assert.IsNotNull (cmbbox.Text, "#B1");
1173                         Assert.AreEqual (string.Empty, cmbbox.Text, "#B2");
1174                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#B3");
1175
1176                         cmbbox.SelectedIndex = 3;
1177                         Assert.IsNotNull (cmbbox.Text, "#C1");
1178                         Assert.AreEqual ("Combobox", cmbbox.Text, "#C2");
1179                         Assert.AreEqual (3, cmbbox.SelectedIndex, "#C3");
1180
1181                         cmbbox.Text = string.Empty;
1182                         Assert.IsNotNull (cmbbox.Text, "#D1");
1183                         Assert.AreEqual (string.Empty, cmbbox.Text, "#D2");
1184                         Assert.AreEqual (3, cmbbox.SelectedIndex, "#D3");
1185
1186                         cmbbox.SelectedIndex = 1;
1187                         Assert.IsNotNull (cmbbox.Text, "#E1");
1188                         Assert.AreEqual ("Bad", cmbbox.Text, "#E2");
1189                         Assert.AreEqual (1, cmbbox.SelectedIndex, "#E3");
1190
1191                         cmbbox.Text = null;
1192                         Assert.IsNotNull (cmbbox.Text, "#F1");
1193                         Assert.AreEqual (string.Empty, cmbbox.Text, "#F2");
1194                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#F3");
1195
1196                         cmbbox.SelectedIndex = 0;
1197                         cmbbox.Text = "Q";
1198                         Assert.IsNotNull (cmbbox.Text, "#G1");
1199                         Assert.AreEqual ("Q", cmbbox.Text, "#G2");
1200                         Assert.AreEqual (0, cmbbox.SelectedIndex, "#G3");
1201
1202                         cmbbox.Text = "B";
1203                         Assert.IsNotNull (cmbbox.Text, "#H1");
1204                         Assert.AreEqual ("B", cmbbox.Text, "#H2");
1205                         Assert.AreEqual (0, cmbbox.SelectedIndex, "#H3");
1206
1207                         cmbbox.Text = "BAD";
1208                         Assert.IsNotNull (cmbbox.Text, "#I1");
1209                         Assert.AreEqual ("BAD", cmbbox.Text, "#I2");
1210                         Assert.AreEqual (4, cmbbox.SelectedIndex, "#I3");
1211
1212                         cmbbox.Text = "BAD";
1213                         Assert.IsNotNull (cmbbox.Text, "#J1");
1214                         Assert.AreEqual ("BAD", cmbbox.Text, "#J2");
1215                         Assert.AreEqual (4, cmbbox.SelectedIndex, "#J3");
1216
1217                         cmbbox.Text = "Something";
1218                         Assert.IsNotNull (cmbbox.Text, "#T1");
1219                         Assert.AreEqual ("Something", cmbbox.Text, "#T2");
1220                         Assert.AreEqual (4, cmbbox.SelectedIndex, "#T3");
1221
1222                         cmbbox.Text = "BAD";
1223                         Assert.IsNotNull (cmbbox.Text, "#U1");
1224                         Assert.AreEqual ("BAD", cmbbox.Text, "#U2");
1225                         Assert.AreEqual (4, cmbbox.SelectedIndex, "#U3");
1226
1227                         cmbbox.Text = "baD";
1228                         Assert.IsNotNull (cmbbox.Text, "#K1");
1229                         Assert.AreEqual ("Bad", cmbbox.Text, "#K2");
1230                         Assert.AreEqual (1, cmbbox.SelectedIndex, "#K3");
1231
1232                         cmbbox.SelectedIndex = -1;
1233                         cmbbox.Text = "E";
1234                         Assert.IsNotNull (cmbbox.Text, "#L1");
1235                         Assert.AreEqual ("E", cmbbox.Text, "#L2");
1236                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#L3");
1237
1238                         cmbbox.Text = "iN";
1239                         Assert.IsNotNull (cmbbox.Text, "#M1");
1240                         Assert.AreEqual ("iN", cmbbox.Text, "#M2");
1241                         Assert.AreEqual (5, cmbbox.SelectedIndex, "#M3");
1242
1243                         cmbbox.Text = "In";
1244                         Assert.IsNotNull (cmbbox.Text, "#N1");
1245                         Assert.AreEqual ("IN", cmbbox.Text, "#N2");
1246                         Assert.AreEqual (2, cmbbox.SelectedIndex, "#N3");
1247
1248                         cmbbox.Text = "Badd";
1249                         Assert.IsNotNull (cmbbox.Text, "#O1");
1250                         Assert.AreEqual ("Badd", cmbbox.Text, "#O2");
1251                         Assert.AreEqual (2, cmbbox.SelectedIndex, "#O3");
1252
1253                         cmbbox.SelectedIndex = -1;
1254                         Assert.IsNotNull (cmbbox.Text, "#P1");
1255                         Assert.AreEqual (string.Empty, cmbbox.Text, "#P2");
1256                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#P3");
1257
1258                         cmbbox.Text = "Something";
1259                         Assert.IsNotNull (cmbbox.Text, "#Q1");
1260                         Assert.AreEqual ("Something", cmbbox.Text, "#Q2");
1261                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#Q3");
1262
1263                         cmbbox.SelectedIndex = -1;
1264                         Assert.IsNotNull (cmbbox.Text, "#R1");
1265                         Assert.AreEqual ("Something", cmbbox.Text, "#R2");
1266                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#R3");
1267
1268                         cmbbox.Text = null;
1269                         Assert.IsNotNull (cmbbox.Text, "#S1");
1270                         Assert.AreEqual (string.Empty, cmbbox.Text, "#S2");
1271                         Assert.AreEqual (-1, cmbbox.SelectedIndex, "#S3");
1272                 }
1273
1274                 [Test]
1275                 public void Text_DataBinding ()
1276                 {
1277                         ArrayList objects = new ArrayList ();
1278                         objects.Add (new MockItem ("A", 0));
1279                         objects.Add (new MockItem ("B", 1));
1280
1281                         ComboBox cb = new ComboBox ();
1282                         cb.BindingContext = new BindingContext ();
1283                         cb.DataSource = objects;
1284                         cb.DisplayMember = "Text";
1285
1286                         Assert.AreEqual ("A", cb.Text, "#A1");
1287                         Assert.AreEqual ("A", ((MockItem)cb.SelectedItem).Text, "#B2");
1288                         Assert.AreEqual (0, ((MockItem)cb.SelectedItem).Value, "#B3");
1289
1290                         cb.Text = "B";
1291                         Assert.AreEqual ("B", cb.Text, "#B1");
1292                         Assert.AreEqual ("B", ((MockItem)cb.SelectedItem).Text, "#B2");
1293                         Assert.AreEqual (1, ((MockItem)cb.SelectedItem).Value, "#B3");
1294
1295                         // the text will change graphically, but Text and
1296                         // SelectedItem will keep their previous valid values.
1297                         cb.Text = "dontexist";
1298                         Assert.AreEqual ("B", ((MockItem)cb.SelectedItem).Text, "#C2");
1299                         Assert.AreEqual (1, ((MockItem)cb.SelectedItem).Value, "#C3");
1300                 }
1301
1302                 [Test]  // bug 360862
1303                 public void SizeChangesAtCreateHandle ()
1304                 {
1305                         ComboBox cb = new ComboBox ();
1306                         cb.Font = new Font ("Arial", 24f);
1307                         
1308                         int original = cb.Height;
1309                         
1310                         IntPtr h = cb.Handle;
1311                         
1312                         Assert.IsTrue (cb.Height > original, string.Format ("ComboBox height ({0}) should be bigger than original ({1})", cb.Height, original));
1313                 }
1314                 
1315                 [Test]
1316                 public void Bug424270 ()
1317                 {
1318                         ComboBox cb = new ComboBox ();
1319                         cb.Items.Add ("ab");
1320                         
1321                         cb.SelectedIndex = 0;
1322
1323                         Assert.AreEqual (0, cb.SelectedIndex, "A1");
1324                         Assert.AreEqual ("ab", cb.SelectedItem, "A2");
1325                         
1326                         cb.SelectedItem = null;
1327                         
1328                         Assert.AreEqual (-1, cb.SelectedIndex, "A3");
1329                         Assert.AreEqual (null, cb.SelectedItem, "A4");
1330                 }
1331                 
1332                 [Test]
1333                 public void BehaviorAutoSize ()
1334                 {
1335                         if (TestHelper.RunningOnUnix)
1336                                 Assert.Ignore ("Depends on font measurements, corresponds to windows");
1337
1338                         Form f = new Form ();
1339                         f.ShowInTaskbar = false;
1340
1341                         f.Show ();
1342
1343                         Image i = new Bitmap (20, 20);
1344                         String s = "My test string";
1345
1346                         CheckBox b = new CheckBox ();
1347                         Size s_size = TextRenderer.MeasureText (s, b.Font);
1348
1349                         b.UseCompatibleTextRendering = false;
1350                         b.Size = new Size (5, 5);
1351                         b.AutoSize = true;
1352                         b.Text = s;
1353                         f.Controls.Add (b);
1354
1355                         // Text only
1356                         b.TextImageRelation = TextImageRelation.Overlay;
1357                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A1");
1358                         b.TextImageRelation = TextImageRelation.ImageAboveText;
1359                         b.Size = new Size (5, 5);
1360                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A2");
1361                         b.TextImageRelation = TextImageRelation.ImageBeforeText;
1362                         b.Size = new Size (5, 5);
1363                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A3");
1364                         b.TextImageRelation = TextImageRelation.TextAboveImage;
1365                         b.Size = new Size (5, 5);
1366                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A4");
1367                         b.TextImageRelation = TextImageRelation.TextBeforeImage;
1368                         b.Size = new Size (5, 5);
1369                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A5");
1370
1371                         // Text and Image
1372                         b.Image = i;
1373                         b.TextImageRelation = TextImageRelation.Overlay;
1374                         b.Size = new Size (5, 5);
1375                         Assert.AreEqual (new Size (s_size.Width + 19, i.Height), b.Size, "A6");
1376                         b.TextImageRelation = TextImageRelation.ImageAboveText;
1377                         b.Size = new Size (5, 5);
1378                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A7");
1379                         b.TextImageRelation = TextImageRelation.ImageBeforeText;
1380                         b.Size = new Size (5, 5);
1381                         Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A8");
1382                         b.TextImageRelation = TextImageRelation.TextAboveImage;
1383                         b.Size = new Size (5, 5);
1384                         Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A9");
1385                         b.TextImageRelation = TextImageRelation.TextBeforeImage;
1386                         b.Size = new Size (5, 5);
1387                         Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A10");
1388
1389                         // Image only
1390                         b.Text = string.Empty;
1391                         b.TextImageRelation = TextImageRelation.Overlay;
1392                         b.Size = new Size (5, 5);
1393                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A11");
1394                         b.TextImageRelation = TextImageRelation.ImageAboveText;
1395                         b.Size = new Size (5, 5);
1396                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A12");
1397                         b.TextImageRelation = TextImageRelation.ImageBeforeText;
1398                         b.Size = new Size (5, 5);
1399                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A13");
1400                         b.TextImageRelation = TextImageRelation.TextAboveImage;
1401                         b.Size = new Size (5, 5);
1402                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A14");
1403                         b.TextImageRelation = TextImageRelation.TextBeforeImage;
1404                         b.Size = new Size (5, 5);
1405                         Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A15");
1406
1407                         // Neither
1408                         b.Image = null;
1409                         b.TextImageRelation = TextImageRelation.Overlay;
1410                         b.Size = new Size (5, 5);
1411                         Assert.AreEqual (new Size (15, 14), b.Size, "A16");
1412                         b.TextImageRelation = TextImageRelation.ImageAboveText;
1413                         b.Size = new Size (5, 5);
1414                         Assert.AreEqual (new Size (15, 14), b.Size, "A17");
1415                         b.TextImageRelation = TextImageRelation.ImageBeforeText;
1416                         b.Size = new Size (5, 5);
1417                         Assert.AreEqual (new Size (15, 14), b.Size, "A18");
1418                         b.TextImageRelation = TextImageRelation.TextAboveImage;
1419                         b.Size = new Size (5, 5);
1420                         Assert.AreEqual (new Size (15, 14), b.Size, "A19");
1421                         b.TextImageRelation = TextImageRelation.TextBeforeImage;
1422                         b.Size = new Size (5, 5);
1423                         Assert.AreEqual (new Size (15, 14), b.Size, "A20");
1424
1425                         // Padding
1426                         b.Padding = new Padding (5, 10, 15, 20);
1427                         Assert.AreEqual (new Size (15 + b.Padding.Horizontal, 14 + b.Padding.Vertical), b.Size, "A21");
1428
1429                         f.Dispose ();
1430                 }
1431
1432                 [Test]
1433                 public void MethodScaleControl ()
1434                 {
1435                         Form f = new Form ();
1436                         f.ShowInTaskbar = false;
1437
1438                         f.Show ();
1439
1440                         PublicComboBox gb = new PublicComboBox ();
1441                         gb.Location = new Point (5, 10);
1442                         f.Controls.Add (gb);
1443
1444                         Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A1");
1445
1446                         gb.PublicScaleControl (new SizeF (2.0f, 2.0f), BoundsSpecified.All);
1447                         Assert.AreEqual (new Rectangle (10, 20, 238, gb.PreferredHeight), gb.Bounds, "A2");
1448
1449                         gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Location);
1450                         Assert.AreEqual (new Rectangle (5, 10, 238, gb.PreferredHeight), gb.Bounds, "A3");
1451
1452                         gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Size);
1453                         Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A4");
1454
1455                         gb.PublicScaleControl (new SizeF (3.5f, 3.5f), BoundsSpecified.Size);
1456                         Assert.AreEqual (new Rectangle (5, 10, 414, gb.PreferredHeight), gb.Bounds, "A5");
1457
1458                         gb.PublicScaleControl (new SizeF (2.5f, 2.5f), BoundsSpecified.Size);
1459                         Assert.AreEqual (new Rectangle (5, 10, 1029, gb.PreferredHeight), gb.Bounds, "A6");
1460
1461                         gb.PublicScaleControl (new SizeF (.2f, .2f), BoundsSpecified.Size);
1462                         Assert.AreEqual (new Rectangle (5, 10, 209, gb.PreferredHeight), gb.Bounds, "A7");
1463
1464                         f.Dispose ();
1465                 }
1466
1467                 private class PublicComboBox : ComboBox
1468                 {
1469                         public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
1470                         {
1471                                 base.ScaleControl (factor, specified);
1472                         }
1473                 }
1474
1475                 private struct ComboVal
1476                 {
1477                         public string text;
1478
1479                         public ComboVal (string t)
1480                         {
1481                                 text = t;
1482                         }
1483
1484                         public override string ToString ()
1485                         {
1486                                 return text;
1487                         }
1488                 }
1489                 
1490                 [Test]
1491                 public void SortTest ()
1492                 {
1493                         // Test sorting of objects with no IComparer
1494                         // should not crash
1495
1496                         ComboBox cmb = new ComboBox ();
1497                         cmb.Items.Add (new ComboVal ("B"));
1498                         cmb.Items.Add (new ComboVal ("A"));
1499                         cmb.Sorted = true;
1500                 }
1501         }
1502
1503         [TestFixture]
1504         public class ComboBoxObjectCollectionTest : TestHelper
1505         {
1506                 [Test]
1507                 public void ComboBoxObjectCollectionPropertyTest ()
1508                 {
1509                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1510                         Assert.AreEqual (false, col.IsReadOnly, "#B1");
1511                         Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
1512                         Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
1513                         Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
1514                 }
1515
1516                 [Test]
1517                 public void AddTest ()
1518                 {
1519                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1520                         col.Add ("Item1");
1521                         col.Add ("Item2");
1522                         Assert.AreEqual (2, col.Count, "#C1");
1523                 }
1524
1525                 [Test]
1526                 public void Add_Null ()
1527                 {
1528                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1529                         try {
1530                                 col.Add (null);
1531                                 Assert.Fail ("#1");
1532                         } catch (ArgumentNullException ex) {
1533                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1534                                 Assert.IsNull (ex.InnerException, "#3");
1535                                 Assert.IsNotNull (ex.Message, "#4");
1536                                 Assert.IsNotNull (ex.ParamName, "#5");
1537                                 Assert.AreEqual ("item", ex.ParamName, "#6");
1538                         }
1539                 }
1540
1541                 [Test]
1542                 public void AddRange_Null ()
1543                 {
1544                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1545                         try {
1546                                 col.AddRange (null);
1547                                 Assert.Fail ("#1");
1548                         } catch (ArgumentNullException ex) {
1549                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1550                                 Assert.IsNull (ex.InnerException, "#3");
1551                                 Assert.IsNotNull (ex.Message, "#4");
1552                                 Assert.IsNotNull (ex.ParamName, "#5");
1553                                 Assert.AreEqual ("items", ex.ParamName, "#6");
1554                         }
1555                 }
1556
1557                 [Test]
1558                 public void ClearTest ()
1559                 {
1560                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1561                         col.Add ("Item1");
1562                         col.Add ("Item2");
1563                         col.Clear ();
1564                         Assert.AreEqual (0, col.Count, "#D1");
1565                 }
1566
1567                 [Test]
1568                 public void ContainsTest ()
1569                 {
1570                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1571                         object obj = "Item1";
1572                         col.Add (obj);
1573                         Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
1574                         Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
1575                 }
1576
1577                 [Test]
1578                 public void Contains_Null ()
1579                 {
1580                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1581                         try {
1582                                 col.Contains (null);
1583                                 Assert.Fail ("#1");
1584                         } catch (ArgumentNullException ex) {
1585                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1586                                 Assert.IsNull (ex.InnerException, "#3");
1587                                 Assert.IsNotNull (ex.Message, "#4");
1588                                 Assert.IsNotNull (ex.ParamName, "#5");
1589                                 Assert.AreEqual ("value", ex.ParamName, "#6");
1590                         }
1591                 }
1592
1593                 [Test]
1594                 public void IndexOfTest ()
1595                 {
1596                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1597                         col.Add ("Item1");
1598                         col.Add ("Item2");
1599                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
1600                 }
1601
1602                 [Test]
1603                 public void IndexOf_Null ()
1604                 {
1605                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1606                         try {
1607                                 col.IndexOf (null);
1608                                 Assert.Fail ("#1");
1609                         } catch (ArgumentNullException ex) {
1610                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1611                                 Assert.IsNull (ex.InnerException, "#3");
1612                                 Assert.IsNotNull (ex.Message, "#4");
1613                                 Assert.IsNotNull (ex.ParamName, "#5");
1614                                 Assert.AreEqual ("value", ex.ParamName, "#6");
1615                         }
1616                 }
1617
1618                 [Test]
1619                 public void Insert_Null ()
1620                 {
1621                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1622                         col.Add ("Item1");
1623                         try {
1624                                 col.Insert (0, null);
1625                                 Assert.Fail ("#1");
1626                         } catch (ArgumentNullException ex) {
1627                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1628                                 Assert.IsNull (ex.InnerException, "#3");
1629                                 Assert.IsNotNull (ex.Message, "#4");
1630                                 Assert.IsNotNull (ex.ParamName, "#5");
1631                                 Assert.AreEqual ("item", ex.ParamName, "#6");
1632                         }
1633                 }
1634
1635                 [Test]
1636                 public void RemoveTest ()
1637                 {
1638                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1639                         col.Add ("Item1");
1640                         col.Add ("Item2");
1641                         col.Remove ("Item1");
1642                         Assert.AreEqual (1, col.Count, "#1");
1643                         col.Remove (null);
1644                         Assert.AreEqual (1, col.Count, "#2");
1645                         col.Remove ("Item3");
1646                         Assert.AreEqual (1, col.Count, "#3");
1647                 }
1648
1649                 [Test]
1650                 public void RemoveAtTest ()
1651                 {
1652                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1653                         col.Add ("Item1");
1654                         col.Add ("Item2");
1655                         col.RemoveAt (0);
1656                         Assert.AreEqual (1, col.Count, "#H1");
1657                         Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
1658                 }
1659
1660                 [Test]
1661                 public void Indexer_Null ()
1662                 {
1663                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1664                         col.Add ("Item1");
1665                         try {
1666                                 col [0] = null;
1667                                 Assert.Fail ("#1");
1668                         } catch (ArgumentNullException ex) {
1669                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1670                                 Assert.IsNull (ex.InnerException, "#3");
1671                                 Assert.IsNotNull (ex.Message, "#4");
1672                                 Assert.IsNotNull (ex.ParamName, "#5");
1673                                 Assert.AreEqual ("value", ex.ParamName, "#6");
1674                         }
1675                 }
1676         }
1677         [TestFixture]
1678         public class ComboBoxTests : TestHelper
1679         {
1680                 ComboBox comboBox;
1681                 bool textChanged, layoutUpdated;
1682
1683                 [SetUp]
1684                 protected override void SetUp () {
1685                         comboBox = new ComboBox ();
1686                         textChanged = false;
1687                         layoutUpdated = false;
1688                         comboBox.TextChanged += new EventHandler (textChangedEventHandler);
1689                         comboBox.Layout += new LayoutEventHandler (layoutEventHandler);
1690                         base.SetUp ();
1691                 }
1692
1693                 private void textChangedEventHandler (object sender, EventArgs e)
1694                 {
1695                         textChanged = true;
1696                 }
1697
1698                 private void layoutEventHandler (object sender, LayoutEventArgs e)
1699                 {
1700                         layoutUpdated = true;
1701                 }
1702
1703                 [Test]
1704                 public void InitialPropertyValues ()
1705                 {
1706
1707                         Assert.AreEqual (String.Empty, comboBox.Text);
1708                         Assert.AreEqual (-1, comboBox.SelectedIndex);
1709                         Assert.IsNull (comboBox.SelectedItem);
1710                         Assert.AreEqual (121, comboBox.Size.Width);
1711                         //Note: it is environment dependent
1712                         //Assert.AreEqual(20, comboBox.Size.Height);
1713                         Assert.IsFalse (textChanged);
1714                         Assert.IsFalse (layoutUpdated);
1715                 }
1716
1717                 [Test]
1718                 public void SetNegativeOneSelectedIndex ()
1719                 {
1720                         comboBox.SelectedIndex = -1;
1721                         Assert.AreEqual (String.Empty, comboBox.Text);
1722                         Assert.IsFalse (textChanged);
1723                 }
1724
1725                 [Test]
1726                 public void SetDifferentText ()
1727                 {
1728                         comboBox.Text = "foooooooooooooooooooooooooo";
1729                         Assert.IsTrue (textChanged);
1730                         Assert.IsFalse (layoutUpdated);
1731                 }
1732
1733                 [Test]
1734                 public void SetSameText ()
1735                 {
1736                         comboBox.Text = String.Empty;
1737                         Assert.IsFalse (textChanged);
1738                         Assert.IsFalse (layoutUpdated);
1739                 }
1740
1741                 [Test] // bug #79812
1742                 public void Add_Item_NonString ()
1743                 {
1744                         comboBox.Sorted = true;
1745                         comboBox.Items.Add (new Person ("B"));
1746                         comboBox.Items.Add (new Person ("A"));
1747                         comboBox.Items.Add (new Person ("C"));
1748                         Assert.AreEqual (string.Empty, comboBox.Text, "#1");
1749                         comboBox.SelectedIndex = 0;
1750                         Assert.AreEqual ("A", comboBox.Text, "#2");
1751                         comboBox.SelectedIndex = 2;
1752                         Assert.AreEqual ("C", comboBox.Text, "#3");
1753                 }
1754
1755                 [Test]
1756                 [Category ("NotWorking")]
1757                 public void SelectedText ()
1758                 {
1759                         Form form = new Form ();
1760                         form.ShowInTaskbar = false;
1761                         form.Visible = false;
1762                         form.Controls.Add (comboBox);
1763
1764                         comboBox.Items.Add ("Bar");
1765                         comboBox.Items.Add ("Foo");
1766                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#A1");
1767                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#A2");
1768                         comboBox.SelectedIndex = 0;
1769                         Assert.AreEqual (0, comboBox.SelectedIndex, "#B1");
1770                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#B2");
1771                         form.Show ();
1772                         Assert.AreEqual (0, comboBox.SelectedIndex, "#C1");
1773                         Assert.AreEqual ("Bar", comboBox.SelectedText, "#C2");
1774                         comboBox.SelectedIndex = 1;
1775                         Assert.AreEqual (1, comboBox.SelectedIndex, "#D1");
1776                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#D2");
1777                         comboBox.SelectedText = "Ba";
1778                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#E1");
1779                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#E2");
1780                         comboBox.SelectedText = "Bar";
1781                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#F1");
1782                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#F2");
1783                         comboBox.SelectedText = "doesnotexist";
1784                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#G1");
1785                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#G2");
1786                         comboBox.SelectedIndex = 0;
1787                         Assert.AreEqual (0, comboBox.SelectedIndex, "#H1");
1788                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#H2");
1789                         comboBox.SelectedText = "Foo";
1790                         Assert.AreEqual (-1, comboBox.SelectedIndex, "#I1");
1791                         Assert.AreEqual (string.Empty, comboBox.SelectedText, "#I2");
1792                 }
1793
1794                 [Test]
1795                 public void SortedSelectedItem ()
1796                 {
1797                         using (Form form = new Form ()) {
1798                                 ComboBox box = new ComboBox ();
1799                                 box.Sorted = true;
1800
1801                                 form.Controls.Add (box);
1802                                 form.ShowInTaskbar = false;
1803                                 form.Show ();
1804
1805                                 box.Items.Add ("English");
1806                                 box.SelectedItem = "English";
1807                                 
1808                                 box.Items.Add ("Danish");
1809                                 Assert.AreEqual ("English", box.SelectedItem, "A1");
1810                                 
1811                                 box.Items.Add ("French");
1812                                 Assert.AreEqual ("English", box.SelectedItem, "A2");
1813                                 
1814                                 box.Items.Add ("Brazilian Portuguese");
1815                                 box.Items.Add ("Arabic");
1816                                 Assert.AreEqual ("English", box.SelectedItem, "A3");
1817                         }
1818                 }
1819                 
1820                 public class Person
1821                 {
1822                         private readonly string _name;
1823
1824                         public Person (string name)
1825                         {
1826                                 _name = name;
1827                         }
1828
1829                         public string Name
1830                         {
1831                                 get
1832                                 {
1833                                         return _name;
1834                                 }
1835                         }
1836
1837                         public override string ToString ()
1838                         {
1839                                 return Name;
1840                         }
1841                 }
1842         }
1843 }