Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ComboBoxTest.cs
index 60b77c8d1fa73c930b2792c9072c3029f24bf284..f7ccad8c39946e36f8a700fe44246bba2616d310 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2006 Matt Hargett
 //
 // Authors:
 //     Ritvik Mayank <mritvik@novell.com>
 //     Jordi Mas i Hernandez <jordi@ximian.com>
+//      Matt Hargett  <matt@use.net>
 
 using System;
 using System.Data;
@@ -38,24 +40,288 @@ using System.Threading;
 using System.Windows.Forms;
 
 using NUnit.Framework;
+using CategoryAttribute=NUnit.Framework.CategoryAttribute;
 
 namespace MonoTests.System.Windows.Forms
 {
        [TestFixture]
-       public class ComboBoxTest
+       public class ComboBoxTest : TestHelper
        {
                private CultureInfo _originalCulture;
 
                [SetUp]
-               public void SetUp ()
+               protected override void SetUp ()
                {
                        _originalCulture = Thread.CurrentThread.CurrentCulture;
+                       base.SetUp ();
                }
 
                [TearDown]
-               public void TearDown ()
+               protected override void TearDown ()
                {
                        Thread.CurrentThread.CurrentCulture = _originalCulture;
+                       base.TearDown ();
+               }
+
+               [Test] // bug 660294
+               public void TestNullSelectedText ()
+               {
+                       ComboBox comboBox = new ComboBox ();
+                       string text = "abc";
+                       comboBox.Items.Add (text);
+                       comboBox.SelectedIndex = 0;
+                       comboBox.Select (0, text.Length);
+                       comboBox.SelectedText = null;
+
+                       Assert.AreEqual (String.Empty, comboBox.SelectedText);
+               }
+
+               [Test] // bug #331144
+               public void DropDownStyle ()
+               {
+                       ComboBox comboBox = new ComboBox ();
+                       comboBox.Items.Add ("abc");
+
+                       Form form = new Form ();
+                       form.Controls.Add (comboBox);
+                       form.Show ();
+
+                       Assert.AreEqual (ComboBoxStyle.DropDown, comboBox.DropDownStyle, "#1");
+                       comboBox.DropDownStyle = ComboBoxStyle.Simple;
+                       Assert.AreEqual (ComboBoxStyle.Simple, comboBox.DropDownStyle, "#2");
+                       comboBox.DropDownStyle = ComboBoxStyle.DropDown;
+                       Assert.AreEqual (ComboBoxStyle.DropDown, comboBox.DropDownStyle, "#3");
+
+                       form.Dispose ();
+               }
+
+               [Test] // bug 81610
+               [Category ("NotWorking")]
+               public void InitialBoundValue ()
+               {
+                       ComboBox comboBox1;
+                       using (Form f = new Form ()) {
+                               f.ShowInTaskbar = false;
+
+                               DataTable t = new DataTable ();
+                               t.Columns.Add ("displaymember");
+                               t.Columns.Add ("valuemember");
+                               t.Rows.Add (new object [] {"lower", "a"});
+                               t.Rows.Add (new object [] {"upper", "A"});
+
+                               // This test seems to prove that the SelectedItem is updated when:
+                               // - We have a parent
+                               // - We're setting either DisplayMember, ValueMember or DataSource.
+
+                               comboBox1 = new ComboBox ();
+                               comboBox1.DisplayMember = "displaymember";
+                               comboBox1.ValueMember = "valuemember";
+                               comboBox1.DataSource = t;
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               f.Controls.Add (comboBox1);
+                               Assert.AreEqual (string.Empty, comboBox1.Text,  "#??");
+                               
+                               comboBox1 = new ComboBox ();
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               comboBox1.DisplayMember = "displaymember";
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A02");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A03");
+
+                               comboBox1 = new ComboBox ();
+                               comboBox1.DisplayMember = "displaymember";
+                               comboBox1.ValueMember = "valuemember";
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A01");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A04");
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A04");
+
+                               comboBox1 = new ComboBox ();
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               comboBox1.DisplayMember = "displaymember";
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A02");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A03");
+
+                               comboBox1 = new ComboBox ();
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("System.Data.DataRowView", comboBox1.Text, "#A03");
+                               comboBox1.DisplayMember = "displaymember";
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A03");
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A02");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A02-1");
+
+
+                               comboBox1 = new ComboBox ();
+                               comboBox1.DisplayMember = "displaymember";
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A02");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A03");
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               Assert.AreEqual (string.Empty, comboBox1.Text, "#A03");
+
+                               comboBox1 = new ComboBox ();
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.DisplayMember = "displaymember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A02");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual ("", comboBox1.Text, "#A02-1");
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               Assert.AreEqual ("", comboBox1.Text, "#A02-1");
+
+
+                               comboBox1 = new ComboBox ();
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               comboBox1.DisplayMember = "displaymember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A03");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A03");
+
+                               comboBox1 = new ComboBox ();
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               comboBox1.DisplayMember = "displaymember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A03");
+                                                               
+                               comboBox1 = new ComboBox ();
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("a", comboBox1.Text, "#A03");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual ("a", comboBox1.Text, "#A03");
+                               comboBox1.DisplayMember = "displaymember";
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A03");
+
+                               comboBox1 = new ComboBox ();
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.DisplayMember = "displaymember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               Assert.AreEqual ("lower", comboBox1.Text, "#A03");
+                               InitialBoundValue_dummy e = new InitialBoundValue_dummy ();
+                               e.controlsrc = "c";
+                               comboBox1.DataBindings.Add ("SelectedValue", e, "controlsrc");
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               f.Show ();
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               
+                               // The real failing test
+                               comboBox1 = new ComboBox ();
+                               comboBox1.DisplayMember = "displaymember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.ValueMember = "valuemember";
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.DataSource = t;
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               f.Controls.AddRange (new Control [] { comboBox1 });
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+                               f.Show ();
+                               Assert.AreEqual ("", comboBox1.Text, "#A03");
+
+                               Assert.AreEqual ("", comboBox1.Text, "#bug");
+
+                               string table =
+@"<?xml version=""1.0"" standalone=""yes""?>
+<DOK>
+<DOK>
+<klient>287</klient>
+</DOK>
+</DOK>
+";
+                               string lookup =
+       @"<?xml version=""1.0"" standalone=""yes""?>
+<klient>
+<klient>
+<nimi>FAILED</nimi>
+<kood>316</kood>
+</klient>
+<klient>
+<nimi>SUCCESS</nimi>
+<kood>287</kood>
+</klient>
+</klient>";
+
+                               using (Form frm = new Form ()) {
+                                       frm.ShowInTaskbar = false;
+                                       DataSet dsTable = new DataSet ();
+                                       dsTable.ReadXml (new StringReader (table));
+                                       DataSet dsLookup = new DataSet ();
+                                       dsLookup.ReadXml (new StringReader (lookup));
+                                       ComboBox cb = new ComboBox ();
+                                       cb.DataSource = dsLookup.Tables [0];
+                                       cb.DisplayMember = "nimi";
+                                       cb.ValueMember = "kood";
+                                       InitialBoundValue_dummy d = new InitialBoundValue_dummy();
+                                       d.controlsrc = "287";
+                                       cb.DataBindings.Add ("SelectedValue", d, "controlsrc");
+                                       frm.Controls.Add (cb);
+                                       Assert.AreEqual ("", cb.Text, "#01");
+                                       frm.Show ();
+                                       Assert.AreEqual ("SUCCESS", cb.Text, "#02");
+                               }
+                       }
+               }
+
+               class InitialBoundValue_dummy
+               {
+                       string t;
+                       public string controlsrc
+                       {
+                               get { return t; }
+                               set { t = value; }
+                       }
+               }
+
+               [Test]
+               public void ContextMenuTest ()
+               {
+                       ComboBox cmb = new ComboBox ();
+                       ContextMenu cm = new ContextMenu ();
+                       
+                       Assert.IsNull (cmb.ContextMenu, "#1");
+                       cmb.ContextMenu = cm;
+                       Assert.AreSame (cmb.ContextMenu, cm, "#2");
+                       cmb.DropDownStyle = ComboBoxStyle.DropDown;
+                       Assert.AreSame (cmb.ContextMenu, cm, "#3");
+                       cmb.DropDownStyle = ComboBoxStyle.DropDownList;
+                       Assert.AreSame (cmb.ContextMenu, cm, "#4");
+                       cmb.DropDownStyle = ComboBoxStyle.Simple;
+                       Assert.AreSame (cmb.ContextMenu, cm, "#5");
+                       
                }
                
                [Test] // bug 80794
@@ -99,6 +365,27 @@ namespace MonoTests.System.Windows.Forms
                                Assert.AreEqual ("SUCCESS", cb.Text, "#02");
                        }
                }
+
+               [Test] // bug #82069
+               public void DataBindingTest2 ()
+               {
+                       ComboBox cmb = new ComboBox ();
+                       cmb.Items.Add (new MockItem ("Foo", 9));
+                       cmb.Items.Add (new MockItem ("Bar", 6));
+                       cmb.ValueMember = "Value";
+                       cmb.DisplayMember = "Text";
+
+                       Form form = new Form ();
+                       form.Controls.Add (cmb);
+                       form.Show ();
+
+                       cmb.SelectedIndex = 0;
+                       Assert.AreEqual ("Foo", cmb.Text, "#1");
+                       cmb.SelectedIndex = 1;
+                       Assert.AreEqual ("Bar", cmb.Text, "#2");
+
+                       form.Dispose ();
+               }
                
                [Test]
                public void ComboBoxPropertyTest ()
@@ -140,6 +427,21 @@ namespace MonoTests.System.Windows.Forms
 #endif
                }
 
+               [Test]
+               public void PreferredHeight ()
+               {
+                       // PreferredHeight is not tied to ItemHeight like we had it
+                       ComboBox cb = new ComboBox ();
+                       
+                       int h = cb.PreferredHeight;
+                       
+                       cb.ItemHeight = 10;
+                       Assert.AreEqual (h, cb.PreferredHeight, "A2");
+
+                       cb.ItemHeight = 30;
+                       Assert.AreEqual (h, cb.PreferredHeight, "A3");
+               }
+               
 #if NET_2_0
                [Test]
                public void ResetTextTest ()
@@ -489,8 +791,9 @@ namespace MonoTests.System.Windows.Forms
                        try {
                                cmbbox.DropDownWidth = 0;
                                Assert.Fail ("#B1");
+                       }
 #if NET_2_0
-                       catch (ArgumentOutOfRangeException ex) {
+                       catch (ArgumentOutOfRangeException ex) {
                                Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
                                Assert.IsNotNull (ex.Message, "#B3");
                                Assert.IsNotNull (ex.ParamName, "#B4");
@@ -498,7 +801,7 @@ namespace MonoTests.System.Windows.Forms
                                Assert.IsNull (ex.InnerException, "#B6");
                        }
 #else
-                       catch (ArgumentException ex) {
+                       catch (ArgumentException ex) {
                                Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
                                Assert.IsNotNull (ex.Message, "#B3");
                                Assert.IsNull (ex.ParamName, "#B4");
@@ -518,8 +821,9 @@ namespace MonoTests.System.Windows.Forms
                        try {
                                cmbbox.ItemHeight = 0;
                                Assert.Fail ("#B1");
+                       }
 #if NET_2_0
-                       catch (ArgumentOutOfRangeException ex) {
+                       catch (ArgumentOutOfRangeException ex) {
                                Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
                                Assert.IsNotNull (ex.Message, "#B3");
                                Assert.IsNotNull (ex.ParamName, "#B4");
@@ -527,7 +831,7 @@ namespace MonoTests.System.Windows.Forms
                                Assert.IsNull (ex.InnerException, "#B6");
                        }
 #else
-                       catch (ArgumentException ex) {
+                       catch (ArgumentException ex) {
                                Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
                                Assert.IsNotNull (ex.Message, "#B3");
                                Assert.IsNull (ex.ParamName, "#B4");
@@ -543,6 +847,47 @@ namespace MonoTests.System.Windows.Forms
                        ComboBox cmbbox = new ComboBox ();
                        cmbbox.SelectedIndex = -2;
                }
+               
+               //Bug 2234 (Xamarin) : Test 1
+               [Test]
+               public void VerifyNoExceptions2234()
+               {
+                       using (Form form = new Form ()){
+                               ComboBox cmb = new ComboBox();
+                               form.Controls.Add (cmb);
+                               form.Show ();
+                               eventFired=false;  //for sanity
+                        
+                               //Primary failure: if exception is raised when
+                               //   DataSource changes.  We should "eat" the 
+                               //   exception under this circumstance before 
+                               //   it gets here.
+                               cmb.SelectedIndexChanged += 
+                                       new EventHandler(GenericHandlerWithException);
+                               cmb.DataSource=new string[]{"One","Two","Three"};
+                               Assert.IsTrue(eventFired);
+                       }
+               }
+               
+               //Bug 2234 (Xamarin) : Test 2
+               [Test]
+               [ExpectedException (typeof (Exception))]
+               public void VerifyException2234()
+               {
+                       using (Form form = new Form ())
+                       {
+                               ComboBox cmb = new ComboBox();
+                               form.Controls.Add (cmb);
+                               form.Show ();
+                               cmb.SelectedIndexChanged +=     
+                                       new EventHandler(GenericHandlerWithException);
+                               cmb.DataSource=new string[]{"One","Two","Three"};
+                        
+                               //Here's where Exception Should raise normally
+                               cmb.SelectedIndex=2;
+                       }       
+               }
+
 
                //
                // Events
@@ -560,6 +905,13 @@ namespace MonoTests.System.Windows.Forms
                        eventFired = true;
                }
 
+               private void GenericHandlerWithException (object sender,  EventArgs e)
+               {
+                       eventFired = true;
+                       throw new Exception("Crash!");
+               }
+
+
                [Ignore ("Bugs in X11 prevent this test to run properly")]
                public void DrawItemEventTest ()
                {
@@ -646,6 +998,69 @@ namespace MonoTests.System.Windows.Forms
                        Assert.AreEqual(false, eventFired, "SWI2");
                }
 
+               [Test]
+               // Xamarin bug 5595
+               // https://bugzilla.xamarin.com/show_bug.cgi?id=5595
+               public void SelectionWithDeletion()
+               {
+                       Form form = null;
+
+                       try
+                       {
+                               // Create a form with a combo box.
+                               form = new Form ();
+                               form.ShowInTaskbar = false;
+                               ComboBox cb = new ComboBox ();
+                               cb.DropDownStyle = ComboBoxStyle.DropDownList;
+                               cb.Parent = form;
+                               form.Show ();
+
+                               // Add some items to the combo box.
+                               cb.Items.Add ("Item 0");
+                               cb.Items.Add ("Item 1");
+                               cb.Items.Add ("Item 2");
+
+                               // Select the last item.
+                               cb.SelectedIndex = 2;
+                               Assert.AreEqual(2, cb.SelectedIndex, "SWD1");
+
+                               // Show the combo box's dropdown.
+                               cb.DroppedDown = true;
+
+                               // Display the results.
+                               Application.DoEvents();
+
+                               // Hide the combo box's dropdown.
+                               cb.DroppedDown = false;
+
+                               // Display the results.
+                               Application.DoEvents();
+
+                               // Delete an item before the selection.
+                               // That should move the selection down.
+                               // Before the bug fix, it would remain 2.
+                               cb.Items.RemoveAt (1);
+                               Assert.AreEqual(1, cb.SelectedIndex, "SWD2");
+
+                               // Show the combo box's dropdown.
+                               // Before the bug fix, this would throw an
+                               // ArgumentOutOfRangeException, because the
+                               // selected index was still 2, and hence
+                               // invalid.)
+                               cb.DroppedDown = true;
+                               Assert.AreEqual(1, cb.SelectedIndex, "SWD3");
+
+                               // Display the results.
+                               Application.DoEvents();
+                       }
+                       finally
+                       {
+                               // Get rid of the form.
+                               if (form != null)
+                                       form.Dispose ();
+                       }
+               }
+
                [Test]
                public void SelectionWithClear()
                {
@@ -659,6 +1074,63 @@ namespace MonoTests.System.Windows.Forms
                        Assert.AreEqual(false, eventFired, "SWC2");
                }
 
+               [Test]
+               public void SelectionWithModification()
+               {
+                       // ComboBox's this[int].set() was dealing with the
+                       // current index badly.  This reproduces the old bug and
+                       // makes sure it's fixed.
+                       ComboBox cb = new ComboBox ();
+                       cb.DropDownStyle = ComboBoxStyle.DropDown;
+
+                       // Add a menu item.
+                       string strItemText = "Item text";
+                       cb.Items.Add (strItemText);
+
+                       // Select the menu item.
+                       cb.SelectedIndex = 0;
+
+                       // Move the selection to the end of the text.
+                       cb.SelectionLength = 0;
+                       cb.SelectionStart = strItemText.Length;
+
+                       // Now set the menu item's text to the empty string and
+                       // back again.
+                       cb.Items[0] = string.Empty;
+                       cb.Items[0] = strItemText;
+
+                       // Make sure the text didn't get duplicated (i.e. the
+                       // bugged code would produce "Item textItem text").
+                       Assert.AreEqual (strItemText, cb.Text, "A1");
+               }
+
+               // Bug #333750 - DisplayMember assignation 
+               // when ComboBox already has a BindingContext BUT
+               // handle hasn't been created yet.
+               [Test]
+               public void SelectedTextWithBindingTest ()
+               {
+                       Form form = new Form ();
+                       form.ShowInTaskbar = false;
+                       ComboBox cb = new ComboBox ();
+                       cb.Parent = form;
+
+                       MockItem [] items = new MockItem [] {
+                               new MockItem ("A", 0),
+                               new MockItem ("B", 1),
+                               new MockItem ("C", 2)
+                       };
+
+                       cb.DataSource = items;
+                       cb.DisplayMember = "Text";
+
+                       form.Show ();
+
+                       Assert.AreEqual ("A", cb.SelectedText, "#A1");
+
+                       form.Dispose ();
+               }
+
                [Test]
                public void SortedTest()
                {
@@ -790,6 +1262,16 @@ namespace MonoTests.System.Windows.Forms
                        Assert.AreEqual ("BAD", cmbbox.Text, "#J2");
                        Assert.AreEqual (4, cmbbox.SelectedIndex, "#J3");
 
+                       cmbbox.Text = "Something";
+                       Assert.IsNotNull (cmbbox.Text, "#T1");
+                       Assert.AreEqual ("Something", cmbbox.Text, "#T2");
+                       Assert.AreEqual (4, cmbbox.SelectedIndex, "#T3");
+
+                       cmbbox.Text = "BAD";
+                       Assert.IsNotNull (cmbbox.Text, "#U1");
+                       Assert.AreEqual ("BAD", cmbbox.Text, "#U2");
+                       Assert.AreEqual (4, cmbbox.SelectedIndex, "#U3");
+
                        cmbbox.Text = "baD";
                        Assert.IsNotNull (cmbbox.Text, "#K1");
                        Assert.AreEqual ("Bad", cmbbox.Text, "#K2");
@@ -836,10 +1318,240 @@ namespace MonoTests.System.Windows.Forms
                        Assert.AreEqual (string.Empty, cmbbox.Text, "#S2");
                        Assert.AreEqual (-1, cmbbox.SelectedIndex, "#S3");
                }
+
+               [Test]
+               public void Text_DataBinding ()
+               {
+                       ArrayList objects = new ArrayList ();
+                       objects.Add (new MockItem ("A", 0));
+                       objects.Add (new MockItem ("B", 1));
+
+                       ComboBox cb = new ComboBox ();
+                       cb.BindingContext = new BindingContext ();
+                       cb.DataSource = objects;
+                       cb.DisplayMember = "Text";
+
+                       Assert.AreEqual ("A", cb.Text, "#A1");
+                       Assert.AreEqual ("A", ((MockItem)cb.SelectedItem).Text, "#B2");
+                       Assert.AreEqual (0, ((MockItem)cb.SelectedItem).Value, "#B3");
+
+                       cb.Text = "B";
+                       Assert.AreEqual ("B", cb.Text, "#B1");
+                       Assert.AreEqual ("B", ((MockItem)cb.SelectedItem).Text, "#B2");
+                       Assert.AreEqual (1, ((MockItem)cb.SelectedItem).Value, "#B3");
+
+                       // the text will change graphically, but Text and
+                       // SelectedItem will keep their previous valid values.
+                       cb.Text = "dontexist";
+                       Assert.AreEqual ("B", ((MockItem)cb.SelectedItem).Text, "#C2");
+                       Assert.AreEqual (1, ((MockItem)cb.SelectedItem).Value, "#C3");
+               }
+
+               [Test]  // bug 360862
+               public void SizeChangesAtCreateHandle ()
+               {
+                       ComboBox cb = new ComboBox ();
+                       cb.Font = new Font ("Arial", 24f);
+                       
+                       int original = cb.Height;
+                       
+                       IntPtr h = cb.Handle;
+                       
+                       Assert.IsTrue (cb.Height > original, string.Format ("ComboBox height ({0}) should be bigger than original ({1})", cb.Height, original));
+               }
+               
+               [Test]
+               public void Bug424270 ()
+               {
+                       ComboBox cb = new ComboBox ();
+                       cb.Items.Add ("ab");
+                       
+                       cb.SelectedIndex = 0;
+
+                       Assert.AreEqual (0, cb.SelectedIndex, "A1");
+                       Assert.AreEqual ("ab", cb.SelectedItem, "A2");
+                       
+                       cb.SelectedItem = null;
+                       
+                       Assert.AreEqual (-1, cb.SelectedIndex, "A3");
+                       Assert.AreEqual (null, cb.SelectedItem, "A4");
+               }
+               
+#if NET_2_0
+               [Test]
+               public void BehaviorAutoSize ()
+               {
+                       if (TestHelper.RunningOnUnix)
+                               Assert.Ignore ("Depends on font measurements, corresponds to windows");
+
+                       Form f = new Form ();
+                       f.ShowInTaskbar = false;
+
+                       f.Show ();
+
+                       Image i = new Bitmap (20, 20);
+                       String s = "My test string";
+
+                       CheckBox b = new CheckBox ();
+                       Size s_size = TextRenderer.MeasureText (s, b.Font);
+
+                       b.UseCompatibleTextRendering = false;
+                       b.Size = new Size (5, 5);
+                       b.AutoSize = true;
+                       b.Text = s;
+                       f.Controls.Add (b);
+
+                       // Text only
+                       b.TextImageRelation = TextImageRelation.Overlay;
+                       Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A1");
+                       b.TextImageRelation = TextImageRelation.ImageAboveText;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A2");
+                       b.TextImageRelation = TextImageRelation.ImageBeforeText;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A3");
+                       b.TextImageRelation = TextImageRelation.TextAboveImage;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A4");
+                       b.TextImageRelation = TextImageRelation.TextBeforeImage;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A5");
+
+                       // Text and Image
+                       b.Image = i;
+                       b.TextImageRelation = TextImageRelation.Overlay;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + 19, i.Height), b.Size, "A6");
+                       b.TextImageRelation = TextImageRelation.ImageAboveText;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A7");
+                       b.TextImageRelation = TextImageRelation.ImageBeforeText;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A8");
+                       b.TextImageRelation = TextImageRelation.TextAboveImage;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A9");
+                       b.TextImageRelation = TextImageRelation.TextBeforeImage;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A10");
+
+                       // Image only
+                       b.Text = string.Empty;
+                       b.TextImageRelation = TextImageRelation.Overlay;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A11");
+                       b.TextImageRelation = TextImageRelation.ImageAboveText;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A12");
+                       b.TextImageRelation = TextImageRelation.ImageBeforeText;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A13");
+                       b.TextImageRelation = TextImageRelation.TextAboveImage;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A14");
+                       b.TextImageRelation = TextImageRelation.TextBeforeImage;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A15");
+
+                       // Neither
+                       b.Image = null;
+                       b.TextImageRelation = TextImageRelation.Overlay;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (15, 14), b.Size, "A16");
+                       b.TextImageRelation = TextImageRelation.ImageAboveText;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (15, 14), b.Size, "A17");
+                       b.TextImageRelation = TextImageRelation.ImageBeforeText;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (15, 14), b.Size, "A18");
+                       b.TextImageRelation = TextImageRelation.TextAboveImage;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (15, 14), b.Size, "A19");
+                       b.TextImageRelation = TextImageRelation.TextBeforeImage;
+                       b.Size = new Size (5, 5);
+                       Assert.AreEqual (new Size (15, 14), b.Size, "A20");
+
+                       // Padding
+                       b.Padding = new Padding (5, 10, 15, 20);
+                       Assert.AreEqual (new Size (15 + b.Padding.Horizontal, 14 + b.Padding.Vertical), b.Size, "A21");
+
+                       f.Dispose ();
+               }
+
+               [Test]
+               public void MethodScaleControl ()
+               {
+                       Form f = new Form ();
+                       f.ShowInTaskbar = false;
+
+                       f.Show ();
+
+                       PublicComboBox gb = new PublicComboBox ();
+                       gb.Location = new Point (5, 10);
+                       f.Controls.Add (gb);
+
+                       Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A1");
+
+                       gb.PublicScaleControl (new SizeF (2.0f, 2.0f), BoundsSpecified.All);
+                       Assert.AreEqual (new Rectangle (10, 20, 238, gb.PreferredHeight), gb.Bounds, "A2");
+
+                       gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Location);
+                       Assert.AreEqual (new Rectangle (5, 10, 238, gb.PreferredHeight), gb.Bounds, "A3");
+
+                       gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Size);
+                       Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A4");
+
+                       gb.PublicScaleControl (new SizeF (3.5f, 3.5f), BoundsSpecified.Size);
+                       Assert.AreEqual (new Rectangle (5, 10, 414, gb.PreferredHeight), gb.Bounds, "A5");
+
+                       gb.PublicScaleControl (new SizeF (2.5f, 2.5f), BoundsSpecified.Size);
+                       Assert.AreEqual (new Rectangle (5, 10, 1029, gb.PreferredHeight), gb.Bounds, "A6");
+
+                       gb.PublicScaleControl (new SizeF (.2f, .2f), BoundsSpecified.Size);
+                       Assert.AreEqual (new Rectangle (5, 10, 209, gb.PreferredHeight), gb.Bounds, "A7");
+
+                       f.Dispose ();
+               }
+
+               private class PublicComboBox : ComboBox
+               {
+                       public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
+                       {
+                               base.ScaleControl (factor, specified);
+                       }
+               }
+#endif
+
+               private struct ComboVal
+               {
+                       public string text;
+
+                       public ComboVal (string t)
+                       {
+                               text = t;
+                       }
+
+                       public override string ToString ()
+                       {
+                               return text;
+                       }
+               }
+               
+               [Test]
+               public void SortTest ()
+               {
+                       // Test sorting of objects with no IComparer
+                       // should not crash
+
+                       ComboBox cmb = new ComboBox ();
+                       cmb.Items.Add (new ComboVal ("B"));
+                       cmb.Items.Add (new ComboVal ("A"));
+                       cmb.Sorted = true;
+               }
        }
 
        [TestFixture]
-       public class ComboBoxObjectCollectionTest
+       public class ComboBoxObjectCollectionTest : TestHelper
        {
                [Test]
                public void ComboBoxObjectCollectionPropertyTest ()
@@ -984,6 +1696,8 @@ namespace MonoTests.System.Windows.Forms
                        Assert.AreEqual (1, col.Count, "#1");
                        col.Remove (null);
                        Assert.AreEqual (1, col.Count, "#2");
+                       col.Remove ("Item3");
+                       Assert.AreEqual (1, col.Count, "#3");
                }
 
                [Test]
@@ -1014,5 +1728,170 @@ namespace MonoTests.System.Windows.Forms
                        }
                }
        }
+       [TestFixture]
+       public class ComboBoxTests : TestHelper
+       {
+               ComboBox comboBox;
+               bool textChanged, layoutUpdated;
+
+               [SetUp]
+               protected override void SetUp () {
+                       comboBox = new ComboBox ();
+                       textChanged = false;
+                       layoutUpdated = false;
+                       comboBox.TextChanged += new EventHandler (textChangedEventHandler);
+                       comboBox.Layout += new LayoutEventHandler (layoutEventHandler);
+                       base.SetUp ();
+               }
+
+               private void textChangedEventHandler (object sender, EventArgs e)
+               {
+                       textChanged = true;
+               }
+
+               private void layoutEventHandler (object sender, LayoutEventArgs e)
+               {
+                       layoutUpdated = true;
+               }
+
+               [Test]
+               public void InitialPropertyValues ()
+               {
+
+                       Assert.AreEqual (String.Empty, comboBox.Text);
+                       Assert.AreEqual (-1, comboBox.SelectedIndex);
+                       Assert.IsNull (comboBox.SelectedItem);
+                       Assert.AreEqual (121, comboBox.Size.Width);
+                       //Note: it is environment dependent
+                       //Assert.AreEqual(20, comboBox.Size.Height);
+                       Assert.IsFalse (textChanged);
+                       Assert.IsFalse (layoutUpdated);
+               }
+
+               [Test]
+               public void SetNegativeOneSelectedIndex ()
+               {
+                       comboBox.SelectedIndex = -1;
+                       Assert.AreEqual (String.Empty, comboBox.Text);
+                       Assert.IsFalse (textChanged);
+               }
+
+               [Test]
+               public void SetDifferentText ()
+               {
+                       comboBox.Text = "foooooooooooooooooooooooooo";
+                       Assert.IsTrue (textChanged);
+                       Assert.IsFalse (layoutUpdated);
+               }
 
+               [Test]
+               public void SetSameText ()
+               {
+                       comboBox.Text = String.Empty;
+                       Assert.IsFalse (textChanged);
+                       Assert.IsFalse (layoutUpdated);
+               }
+
+               [Test] // bug #79812
+               public void Add_Item_NonString ()
+               {
+                       comboBox.Sorted = true;
+                       comboBox.Items.Add (new Person ("B"));
+                       comboBox.Items.Add (new Person ("A"));
+                       comboBox.Items.Add (new Person ("C"));
+                       Assert.AreEqual (string.Empty, comboBox.Text, "#1");
+                       comboBox.SelectedIndex = 0;
+                       Assert.AreEqual ("A", comboBox.Text, "#2");
+                       comboBox.SelectedIndex = 2;
+                       Assert.AreEqual ("C", comboBox.Text, "#3");
+               }
+
+               [Test]
+               [Category ("NotWorking")]
+               public void SelectedText ()
+               {
+                       Form form = new Form ();
+                       form.ShowInTaskbar = false;
+                       form.Visible = false;
+                       form.Controls.Add (comboBox);
+
+                       comboBox.Items.Add ("Bar");
+                       comboBox.Items.Add ("Foo");
+                       Assert.AreEqual (-1, comboBox.SelectedIndex, "#A1");
+                       Assert.AreEqual (string.Empty, comboBox.SelectedText, "#A2");
+                       comboBox.SelectedIndex = 0;
+                       Assert.AreEqual (0, comboBox.SelectedIndex, "#B1");
+                       Assert.AreEqual (string.Empty, comboBox.SelectedText, "#B2");
+                       form.Show ();
+                       Assert.AreEqual (0, comboBox.SelectedIndex, "#C1");
+                       Assert.AreEqual ("Bar", comboBox.SelectedText, "#C2");
+                       comboBox.SelectedIndex = 1;
+                       Assert.AreEqual (1, comboBox.SelectedIndex, "#D1");
+                       Assert.AreEqual (string.Empty, comboBox.SelectedText, "#D2");
+                       comboBox.SelectedText = "Ba";
+                       Assert.AreEqual (-1, comboBox.SelectedIndex, "#E1");
+                       Assert.AreEqual (string.Empty, comboBox.SelectedText, "#E2");
+                       comboBox.SelectedText = "Bar";
+                       Assert.AreEqual (-1, comboBox.SelectedIndex, "#F1");
+                       Assert.AreEqual (string.Empty, comboBox.SelectedText, "#F2");
+                       comboBox.SelectedText = "doesnotexist";
+                       Assert.AreEqual (-1, comboBox.SelectedIndex, "#G1");
+                       Assert.AreEqual (string.Empty, comboBox.SelectedText, "#G2");
+                       comboBox.SelectedIndex = 0;
+                       Assert.AreEqual (0, comboBox.SelectedIndex, "#H1");
+                       Assert.AreEqual (string.Empty, comboBox.SelectedText, "#H2");
+                       comboBox.SelectedText = "Foo";
+                       Assert.AreEqual (-1, comboBox.SelectedIndex, "#I1");
+                       Assert.AreEqual (string.Empty, comboBox.SelectedText, "#I2");
+               }
+
+               [Test]
+               public void SortedSelectedItem ()
+               {
+                       using (Form form = new Form ()) {
+                               ComboBox box = new ComboBox ();
+                               box.Sorted = true;
+
+                               form.Controls.Add (box);
+                               form.ShowInTaskbar = false;
+                               form.Show ();
+
+                               box.Items.Add ("English");
+                               box.SelectedItem = "English";
+                               
+                               box.Items.Add ("Danish");
+                               Assert.AreEqual ("English", box.SelectedItem, "A1");
+                               
+                               box.Items.Add ("French");
+                               Assert.AreEqual ("English", box.SelectedItem, "A2");
+                               
+                               box.Items.Add ("Brazilian Portuguese");
+                               box.Items.Add ("Arabic");
+                               Assert.AreEqual ("English", box.SelectedItem, "A3");
+                       }
+               }
+               
+               public class Person
+               {
+                       private readonly string _name;
+
+                       public Person (string name)
+                       {
+                               _name = name;
+                       }
+
+                       public string Name
+                       {
+                               get
+                               {
+                                       return _name;
+                               }
+                       }
+
+                       public override string ToString ()
+                       {
+                               return Name;
+                       }
+               }
+       }
 }