2 // ComboBoxTests.cs: Test cases for ComboBox.
\r
4 // Permission is hereby granted, free of charge, to any person obtaining
\r
5 // a copy of this software and associated documentation files (the
\r
6 // "Software"), to deal in the Software without restriction, including
\r
7 // without limitation the rights to use, copy, modify, merge, publish,
\r
8 // distribute, sublicense, and/or sell copies of the Software, and to
\r
9 // permit persons to whom the Software is furnished to do so, subject to
\r
10 // the following conditions:
\r
12 // The above copyright notice and this permission notice shall be
\r
13 // included in all copies or substantial portions of the Software.
\r
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 // Copyright (c) 2006 Matt Hargett
\r
26 // Matt Hargett <matt@use.net>
\r
30 using System.Windows.Forms;
\r
32 using NUnit.Framework;
\r
34 namespace MonoTests.System.Windows.Forms
\r
37 public class ComboBoxTests
\r
40 bool textChanged, layoutUpdated;
\r
43 public void SetUp ()
\r
45 comboBox = new ComboBox ();
\r
46 textChanged = false;
\r
47 layoutUpdated = false;
\r
48 comboBox.TextChanged += new EventHandler (textChangedEventHandler);
\r
49 comboBox.Layout += new LayoutEventHandler (layoutEventHandler);
\r
52 private void textChangedEventHandler (object sender, EventArgs e)
\r
57 private void layoutEventHandler (object sender, LayoutEventArgs e)
\r
59 layoutUpdated = true;
\r
63 public void InitialPropertyValues ()
\r
66 Assert.AreEqual (String.Empty, comboBox.Text);
\r
67 Assert.AreEqual (-1, comboBox.SelectedIndex);
\r
68 Assert.IsNull (comboBox.SelectedItem);
\r
69 Assert.AreEqual (121, comboBox.Size.Width);
\r
70 //Note: it is environment dependent
\r
71 //Assert.AreEqual(20, comboBox.Size.Height);
\r
72 Assert.IsFalse (textChanged);
\r
73 Assert.IsFalse (layoutUpdated);
\r
77 public void SetNegativeOneSelectedIndex ()
\r
79 comboBox.SelectedIndex = -1;
\r
80 Assert.AreEqual (String.Empty, comboBox.Text);
\r
81 Assert.IsFalse (textChanged);
\r
85 public void SetDifferentText ()
\r
87 comboBox.Text = "foooooooooooooooooooooooooo";
\r
88 Assert.IsTrue (textChanged);
\r
89 Assert.IsFalse (layoutUpdated);
\r
93 public void SetSameText ()
\r
95 comboBox.Text = String.Empty;
\r
96 Assert.IsFalse (textChanged);
\r
97 Assert.IsFalse (layoutUpdated);
\r
100 [Test] // bug #79812
\r
101 public void Add_Item_NonString ()
\r
103 comboBox.Sorted = true;
\r
104 comboBox.Items.Add (new Person ("B"));
\r
105 comboBox.Items.Add (new Person ("A"));
\r
106 comboBox.Items.Add (new Person ("C"));
\r
107 Assert.AreEqual (string.Empty, comboBox.Text, "#1");
\r
108 comboBox.SelectedIndex = 0;
\r
109 Assert.AreEqual ("A", comboBox.Text, "#2");
\r
110 comboBox.SelectedIndex = 2;
\r
111 Assert.AreEqual ("C", comboBox.Text, "#3");
\r
115 [Category ("NotWorking")]
\r
116 public void SelectedText ()
\r
118 Form form = new Form ();
\r
119 form.ShowInTaskbar = false;
\r
120 form.Visible = false;
\r
121 form.Controls.Add (comboBox);
\r
123 comboBox.Items.Add ("Bar");
\r
124 comboBox.Items.Add ("Foo");
\r
125 Assert.AreEqual (-1, comboBox.SelectedIndex, "#A1");
\r
126 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#A2");
\r
127 comboBox.SelectedIndex = 0;
\r
128 Assert.AreEqual (0, comboBox.SelectedIndex, "#B1");
\r
129 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#B2");
\r
131 Assert.AreEqual (0, comboBox.SelectedIndex, "#C1");
\r
132 Assert.AreEqual ("Bar", comboBox.SelectedText, "#C2");
\r
133 comboBox.SelectedIndex = 1;
\r
134 Assert.AreEqual (1, comboBox.SelectedIndex, "#D1");
\r
135 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#D2");
\r
136 comboBox.SelectedText = "Ba";
\r
137 Assert.AreEqual (-1, comboBox.SelectedIndex, "#E1");
\r
138 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#E2");
\r
139 comboBox.SelectedText = "Bar";
\r
140 Assert.AreEqual (-1, comboBox.SelectedIndex, "#F1");
\r
141 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#F2");
\r
142 comboBox.SelectedText = "doesnotexist";
\r
143 Assert.AreEqual (-1, comboBox.SelectedIndex, "#G1");
\r
144 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#G2");
\r
145 comboBox.SelectedIndex = 0;
\r
146 Assert.AreEqual (0, comboBox.SelectedIndex, "#H1");
\r
147 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#H2");
\r
148 comboBox.SelectedText = "Foo";
\r
149 Assert.AreEqual (-1, comboBox.SelectedIndex, "#I1");
\r
150 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#I2");
\r
153 public class Person
\r
155 private readonly string _name;
\r
157 public Person (string name)
\r
170 public override string ToString ()
\r