2007-01-10 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ComboBoxTests.cs
blob60d05b6dbc5fb0de26c7d358b735591bbc74f2a9
1 //
2 // ComboBoxTests.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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Matt Hargett
24 //
25 // Authors:
26 // Matt Hargett <matt@use.net>
29 using System;
30 using System.Windows.Forms;
32 using NUnit.Framework;
34 namespace MonoTests.System.Windows.Forms
36 [TestFixture]
37 public class ComboBoxTests
39 ComboBox comboBox;
40 bool textChanged, layoutUpdated;
42 [SetUp]
43 public void SetUp ()
45 comboBox = new ComboBox ();
46 textChanged = false;
47 layoutUpdated = false;
48 comboBox.TextChanged += new EventHandler (textChangedEventHandler);
49 comboBox.Layout += new LayoutEventHandler (layoutEventHandler);
52 private void textChangedEventHandler (object sender, EventArgs e)
54 textChanged = true;
57 private void layoutEventHandler (object sender, LayoutEventArgs e)
59 layoutUpdated = true;
62 [Test]
63 public void InitialPropertyValues ()
66 Assert.AreEqual (String.Empty, comboBox.Text);
67 Assert.AreEqual (-1, comboBox.SelectedIndex);
68 Assert.IsNull (comboBox.SelectedItem);
69 Assert.AreEqual (121, comboBox.Size.Width);
70 //Note: it is environment dependent
71 //Assert.AreEqual(20, comboBox.Size.Height);
72 Assert.IsFalse (textChanged);
73 Assert.IsFalse (layoutUpdated);
76 [Test]
77 public void SetNegativeOneSelectedIndex ()
79 comboBox.SelectedIndex = -1;
80 Assert.AreEqual (String.Empty, comboBox.Text);
81 Assert.IsFalse (textChanged);
84 [Test]
85 public void SetDifferentText ()
87 comboBox.Text = "foooooooooooooooooooooooooo";
88 Assert.IsTrue (textChanged);
89 Assert.IsFalse (layoutUpdated);
92 [Test]
93 public void SetSameText ()
95 comboBox.Text = String.Empty;
96 Assert.IsFalse (textChanged);
97 Assert.IsFalse (layoutUpdated);
100 [Test] // bug #79812
101 public void Add_Item_NonString ()
103 comboBox.Sorted = true;
104 comboBox.Items.Add (new Person ("B"));
105 comboBox.Items.Add (new Person ("A"));
106 comboBox.Items.Add (new Person ("C"));
107 Assert.AreEqual (string.Empty, comboBox.Text, "#1");
108 comboBox.SelectedIndex = 0;
109 Assert.AreEqual ("A", comboBox.Text, "#2");
110 comboBox.SelectedIndex = 2;
111 Assert.AreEqual ("C", comboBox.Text, "#3");
114 [Test]
115 [Category ("NotWorking")]
116 public void SelectedText ()
118 Form form = new Form ();
119 form.ShowInTaskbar = false;
120 form.Visible = false;
121 form.Controls.Add (comboBox);
123 comboBox.Items.Add ("Bar");
124 comboBox.Items.Add ("Foo");
125 Assert.AreEqual (-1, comboBox.SelectedIndex, "#A1");
126 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#A2");
127 comboBox.SelectedIndex = 0;
128 Assert.AreEqual (0, comboBox.SelectedIndex, "#B1");
129 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#B2");
130 form.Show ();
131 Assert.AreEqual (0, comboBox.SelectedIndex, "#C1");
132 Assert.AreEqual ("Bar", comboBox.SelectedText, "#C2");
133 comboBox.SelectedIndex = 1;
134 Assert.AreEqual (1, comboBox.SelectedIndex, "#D1");
135 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#D2");
136 comboBox.SelectedText = "Ba";
137 Assert.AreEqual (-1, comboBox.SelectedIndex, "#E1");
138 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#E2");
139 comboBox.SelectedText = "Bar";
140 Assert.AreEqual (-1, comboBox.SelectedIndex, "#F1");
141 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#F2");
142 comboBox.SelectedText = "doesnotexist";
143 Assert.AreEqual (-1, comboBox.SelectedIndex, "#G1");
144 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#G2");
145 comboBox.SelectedIndex = 0;
146 Assert.AreEqual (0, comboBox.SelectedIndex, "#H1");
147 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#H2");
148 comboBox.SelectedText = "Foo";
149 Assert.AreEqual (-1, comboBox.SelectedIndex, "#I1");
150 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#I2");
153 public class Person
155 private readonly string _name;
157 public Person (string name)
159 _name = name;
162 public string Name
166 return _name;
170 public override string ToString ()
172 return Name;