2009-06-06 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ComboBoxTest.cs
index 5aa8abf6af53c5a8c6944f2e9ab01953d91e7f9a..0da81a7f8165e642987100ae4a0bd3427d5dc29d 100644 (file)
@@ -45,20 +45,41 @@ 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 #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
@@ -393,6 +414,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 ()
@@ -742,8 +778,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");
@@ -751,7 +788,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");
@@ -771,8 +808,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");
@@ -780,7 +818,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");
@@ -912,6 +950,33 @@ namespace MonoTests.System.Windows.Forms
                        Assert.AreEqual(false, eventFired, "SWC2");
                }
 
+               // 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()
                {
@@ -1090,6 +1155,36 @@ namespace MonoTests.System.Windows.Forms
                        Assert.AreEqual (-1, cmbbox.SelectedIndex, "#S3");
                }
 
+               [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 ()
@@ -1203,25 +1298,25 @@ namespace MonoTests.System.Windows.Forms
                        gb.Location = new Point (5, 10);
                        f.Controls.Add (gb);
 
-                       Assert.AreEqual (new Rectangle (5, 10, 121, 21), gb.Bounds, "A1");
+                       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, 21), gb.Bounds, "A2");
+                       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, 21), gb.Bounds, "A3");
+                       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, 21), gb.Bounds, "A4");
-                       Console.WriteLine ("DONE");
+                       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, 21), gb.Bounds, "A5");
+                       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, 21), gb.Bounds, "A6");
+                       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, 21), gb.Bounds, "A7");
+                       Assert.AreEqual (new Rectangle (5, 10, 209, gb.PreferredHeight), gb.Bounds, "A7");
 
                        f.Dispose ();
                }
@@ -1234,10 +1329,37 @@ namespace MonoTests.System.Windows.Forms
                        }
                }
 #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 ()
@@ -1413,19 +1535,19 @@ namespace MonoTests.System.Windows.Forms
                }
        }
        [TestFixture]
-       public class ComboBoxTests
+       public class ComboBoxTests : TestHelper
        {
                ComboBox comboBox;
                bool textChanged, layoutUpdated;
 
                [SetUp]
-               public void 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)
@@ -1529,6 +1651,32 @@ namespace MonoTests.System.Windows.Forms
                        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;
@@ -1552,4 +1700,4 @@ namespace MonoTests.System.Windows.Forms
                        }
                }
        }
-}
+}
\ No newline at end of file