Some tests for recent CurrencyManager work.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ComboBoxTest.cs
1 //
2 // ComboBoxTest.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:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
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.
22 //
23 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
24 //
25 // Authors:
26 //      Ritvik Mayank <mritvik@novell.com>
27 //      Jordi Mas i Hernandez <jordi@ximian.com>
28
29
30 using System;
31 using System.Windows.Forms;
32 using System.Drawing;
33 using System.Reflection;
34 using NUnit.Framework;
35 using System.Collections;
36 using System.ComponentModel;
37
38 namespace MonoTests.System.Windows.Forms
39 {
40         [TestFixture]
41         public class ComboBoxTest
42         {
43                 [Test]
44                 public void ComboBoxPropertyTest ()
45                 {
46                         ComboBox mycmbbox = new ComboBox ();
47                         Assert.AreEqual (DrawMode.Normal, mycmbbox.DrawMode, "#1");
48                         Assert.AreEqual (ComboBoxStyle.DropDown, mycmbbox.DropDownStyle, "#2");
49                         Assert.AreEqual (121, mycmbbox.DropDownWidth, "#3");
50                         Assert.AreEqual (false, mycmbbox.DroppedDown, "#4");
51                         Assert.AreEqual (true, mycmbbox.IntegralHeight, "#5");
52                         Assert.AreEqual (0, mycmbbox.Items.Count, "#6");
53                         //Assert.AreEqual (15, mycmbbox.ItemHeight, "#7");      // Note: Item height depends on the current font.
54                         Assert.AreEqual (8, mycmbbox.MaxDropDownItems, "#8");
55                         Assert.AreEqual (0, mycmbbox.MaxLength, "#9");
56                         //Assert.AreEqual (20, mycmbbox.PreferredHeight, "#10");
57 // Note: Item height depends on the current font.
58                         Assert.AreEqual (-1, mycmbbox.SelectedIndex, "#11");
59                         Assert.AreEqual (null, mycmbbox.SelectedItem, "#12");
60                         Assert.AreEqual ("", mycmbbox.SelectedText, "#13");
61                         Assert.AreEqual (0, mycmbbox.SelectionLength, "#14");
62                         Assert.AreEqual (0, mycmbbox.SelectionStart, "#15");
63                         Assert.AreEqual (false, mycmbbox.Sorted, "#16");
64                         Assert.AreEqual ("", mycmbbox.Text, "#17");
65                 }
66
67                 [Test]
68                 public void BeginEndUpdateTest ()
69                 {
70                         Form myform = new Form ();
71                         myform.Visible = true;
72                         ComboBox cmbbox = new ComboBox ();
73                         cmbbox.Items.Add ("A");
74                         cmbbox.Visible = true;
75                         myform.Controls.Add (cmbbox);
76                         cmbbox.BeginUpdate ();
77                         for (int x = 1 ; x < 5000 ; x++) {
78                                 cmbbox.Items.Add ("Item " + x.ToString ());
79                         }
80                         cmbbox.EndUpdate ();
81                 }
82
83                 [Test]
84                 public void FindStringTest ()
85                 {
86                         ComboBox cmbbox = new ComboBox ();
87                         cmbbox.FindString ("Hola", -5); // No exception, it's empty
88                         int x = cmbbox.FindString ("Hello");
89                         Assert.AreEqual (-1, x, "#19");
90                         cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
91                         String myString = "ABC";
92                         x = cmbbox.FindString (myString);
93                         Assert.AreEqual (3, x, "#191");
94                         x = cmbbox.FindString (string.Empty);
95                         Assert.AreEqual (0, x, "#192");
96                         x = cmbbox.FindString ("NonExistant");
97                         Assert.AreEqual (-1, x, "#193");
98                 }
99
100                 [Test]
101                 public void FindStringExactTest ()
102                 {
103                         ComboBox cmbbox = new ComboBox ();
104                         cmbbox.FindStringExact ("Hola", -5); // No exception, it's empty
105                         int x = cmbbox.FindStringExact ("Hello");
106                         Assert.AreEqual (-1, x, "#20");
107                         cmbbox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
108                         String myString = "ABC";
109                         x = cmbbox.FindStringExact (myString);
110                         Assert.AreEqual (1, x, "#201");
111                         x = cmbbox.FindStringExact (string.Empty);
112                         Assert.AreEqual (-1, x, "#202");
113                         x = cmbbox.FindStringExact ("NonExistant");
114                         Assert.AreEqual (-1, x, "#203");
115                 }
116
117                 [Test]
118                 public void GetItemHeightTest ()
119                 {
120                         ComboBox cmbbox = new ComboBox ();
121                         cmbbox.Items.Add ("ABC");
122                         cmbbox.Items.Add ("BCD");
123                         cmbbox.Items.Add ("DEF");
124                         int x = -1;
125                         x = cmbbox.GetItemHeight (x);
126                         Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
127                 }
128
129
130                 //
131                 // Exceptions
132                 //
133
134                 [Test]
135                 [ExpectedException (typeof (InvalidEnumArgumentException))]
136                 public void DropDownStyleException ()
137                 {
138                         ComboBox cmbbox = new ComboBox ();
139                         cmbbox.DropDownStyle = (ComboBoxStyle) 10;
140                 }
141
142                 [Test]
143                 [ExpectedException (typeof (InvalidEnumArgumentException))]
144                 public void DrawModeException ()
145                 {
146                         ComboBox cmbbox = new ComboBox ();
147                         cmbbox.DrawMode = (DrawMode) 10;
148                 }
149
150                 [Test]
151                 [ExpectedException (typeof (ArgumentException))]
152                 public void DropDownWidthException ()
153                 {
154                         ComboBox cmbbox = new ComboBox ();
155                         cmbbox.DropDownWidth = 0;
156                 }
157
158                 [Test]
159                 [ExpectedException (typeof (ArgumentException))]
160                 public void ItemHeightException ()
161                 {
162                         ComboBox cmbbox = new ComboBox ();
163                         cmbbox.ItemHeight = -1;
164                 }
165
166                 [Test]
167                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
168                 public void SelectedIndexException ()
169                 {
170                         ComboBox cmbbox = new ComboBox ();
171                         cmbbox.SelectedIndex = -2;
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
176                 public void FindStringExactMinException ()
177                 {
178                         ComboBox cmbbox = new ComboBox ();
179                         cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
180                         cmbbox.FindStringExact ("Hola", -2);
181                 }
182
183                 [Test]
184                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
185                 public void FindStringExactMaxException ()
186                 {
187                         ComboBox cmbbox = new ComboBox ();
188                         cmbbox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
189                         cmbbox.FindStringExact ("Hola", 3);
190                 }
191
192                 //
193                 // Events
194                 //
195                 private bool eventFired;
196                 private DrawItemEventArgs drawItemsArgs;
197                 private void DrawItemEventH (object sender,  DrawItemEventArgs e)
198                 {
199                         eventFired = true;
200                         drawItemsArgs = e;
201                 }
202
203                 private void GenericHandler (object sender,  EventArgs e)
204                 {
205                         eventFired = true;
206                 }
207
208                 [Ignore ("Bugs in X11 prevent this test to run properly")]
209                 public void DrawItemEventTest ()
210                 {
211                         eventFired = false;
212                         drawItemsArgs = null;
213                         Form myform = new Form ();
214                         ComboBox cmbbox = new ComboBox ();
215                         cmbbox.DropDownStyle = ComboBoxStyle.Simple;
216                         cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
217                         cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
218
219                         myform.Controls.Add (cmbbox);
220                         cmbbox.Items.AddRange(new object[] {"Item1"});
221
222                         myform.Visible = true;
223                         cmbbox.Visible = true;
224                         cmbbox.Refresh ();
225
226                         Assert.AreEqual (true, eventFired, "DW1");
227                         Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
228                 }
229
230                 [Test]
231                 public void DropDownStyleEventTest ()
232                 {
233                         eventFired = false;
234                         ComboBox cmbbox = new ComboBox ();
235                         cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
236                         cmbbox.DropDownStyle = ComboBoxStyle.Simple;
237
238                         Assert.AreEqual (true, eventFired, "DI1");
239                 }
240
241                 [Test]
242                 public void SelectedIndextTest ()
243                 {
244                         eventFired = false;
245                         ComboBox cmbbox = new ComboBox ();
246                         cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
247                         cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
248                         cmbbox.SelectedIndex = 1;
249                         Assert.AreEqual (true, eventFired, "SI1");
250                 }
251
252         }
253
254         [TestFixture]
255         public class ComboBoxObjectCollectionTest
256         {
257                 [Test]
258                 public void ComboBoxObjectCollectionPropertyTest ()
259                 {
260                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
261                         Assert.AreEqual (false, col.IsReadOnly, "#B1");
262                         Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
263                         Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
264                         Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
265                 }
266
267                 [Test]
268                 public void AddTest ()
269                 {
270                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
271                         col.Add ("Item1");
272                         col.Add ("Item2");
273                         Assert.AreEqual (2, col.Count, "#C1");
274                 }
275
276                 [Test]
277                 public void ClearTest ()
278                 {
279                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
280                         col.Add ("Item1");
281                         col.Add ("Item2");
282                         col.Clear ();
283                         Assert.AreEqual (0, col.Count, "#D1");
284                 }
285
286                 [Test]
287                 public void ContainsTest ()
288                 {
289                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
290                         object obj = "Item1";
291                         col.Add (obj);
292                         Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
293                         Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
294                 }
295
296                 [Test]
297                 public void IndexOfTest ()
298                 {
299                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
300                         col.Add ("Item1");
301                         col.Add ("Item2");
302                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
303                 }
304
305                 [Test]
306                 public void RemoveTest ()
307                 {
308                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
309                         col.Add ("Item1");
310                         col.Add ("Item2");
311                         col.Remove ("Item1");
312                         Assert.AreEqual (1, col.Count, "#G1");
313                 }
314
315                 [Test]
316                 public void RemoveAtTest ()
317                 {
318                         ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
319                         col.Add ("Item1");
320                         col.Add ("Item2");
321                         col.RemoveAt (0);
322                         Assert.AreEqual (1, col.Count, "#H1");
323                         Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
324                 }
325         }
326
327 }