2006-12-19 Daniel Nauck <dna@mono-project.de>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ComboBoxTests.cs
1 //\r
2 // ComboBoxTests.cs: Test cases for ComboBox.\r
3 //\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
11 //\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
14 //\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
22 //\r
23 // Copyright (c) 2006 Matt Hargett\r
24 //                      \r
25 // Authors:             \r
26 //      Matt Hargett  <matt@use.net>\r
27 //\r
28 \r
29 using System;\r
30 using System.Windows.Forms;\r
31 \r
32 using NUnit.Framework;\r
33 \r
34 namespace MonoTests.System.Windows.Forms\r
35 {\r
36         [TestFixture]\r
37         public class ComboBoxTests\r
38         {\r
39                 ComboBox comboBox;\r
40                 bool textChanged, layoutUpdated;\r
41 \r
42                 [SetUp]\r
43                 public void SetUp ()\r
44                 {\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
50                 }\r
51 \r
52                 private void textChangedEventHandler (object sender, EventArgs e)\r
53                 {\r
54                         textChanged = true;\r
55                 }\r
56 \r
57                 private void layoutEventHandler (object sender, LayoutEventArgs e)\r
58                 {\r
59                         layoutUpdated = true;\r
60                 }\r
61 \r
62                 [Test]\r
63                 public void InitialPropertyValues ()\r
64                 {\r
65 \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
74                 }\r
75 \r
76                 [Test]\r
77                 public void SetNegativeOneSelectedIndex ()\r
78                 {\r
79                         comboBox.SelectedIndex = -1;\r
80                         Assert.AreEqual (String.Empty, comboBox.Text);\r
81                         Assert.IsFalse (textChanged);\r
82                 }\r
83 \r
84                 [Test]\r
85                 public void SetDifferentText ()\r
86                 {\r
87                         comboBox.Text = "foooooooooooooooooooooooooo";\r
88                         Assert.IsTrue (textChanged);\r
89                         Assert.IsFalse (layoutUpdated);\r
90                 }\r
91 \r
92                 [Test]\r
93                 public void SetSameText ()\r
94                 {\r
95                         comboBox.Text = String.Empty;\r
96                         Assert.IsFalse (textChanged);\r
97                         Assert.IsFalse (layoutUpdated);\r
98                 }\r
99 \r
100                 [Test] // bug #79812\r
101                 public void Add_Item_NonString ()\r
102                 {\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
112                 }\r
113 \r
114                 [Test]\r
115                 [Category ("NotWorking")]\r
116                 public void SelectedText ()\r
117                 {\r
118                         Form form = new Form ();\r
119                         form.ShowInTaskbar = false;\r
120                         form.Visible = false;\r
121                         form.Controls.Add (comboBox);\r
122 \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
130                         form.Show ();\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
151                 }\r
152 \r
153                 public class Person\r
154                 {\r
155                         private readonly string _name;\r
156 \r
157                         public Person (string name)\r
158                         {\r
159                                 _name = name;\r
160                         }\r
161 \r
162                         public string Name\r
163                         {\r
164                                 get\r
165                                 {\r
166                                         return _name;\r
167                                 }\r
168                         }\r
169 \r
170                         public override string ToString ()\r
171                         {\r
172                                 return Name;\r
173                         }\r
174                 }\r
175         }\r
176 }\r