2 // ComboBoxTest.cs: Test cases for ComboBox.
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) 2005 Novell, Inc. (http://www.novell.com)
24 // Copyright (c) 2006 Matt Hargett
27 // Ritvik Mayank <mritvik@novell.com>
28 // Jordi Mas i Hernandez <jordi@ximian.com>
29 // Matt Hargett <matt@use.net>
34 using System.Collections;
35 using System.ComponentModel;
37 using System.Globalization;
38 using System.Reflection;
39 using System.Threading;
40 using System.Windows.Forms;
42 using NUnit.Framework;
43 using CategoryAttribute=NUnit.Framework.CategoryAttribute;
45 namespace MonoTests.System.Windows.Forms
48 public class ComboBoxTest : TestHelper
50 private CultureInfo _originalCulture;
53 protected override void SetUp ()
55 _originalCulture = Thread.CurrentThread.CurrentCulture;
60 protected override void TearDown ()
62 Thread.CurrentThread.CurrentCulture = _originalCulture;
67 public void TestNullSelectedText ()
69 ComboBox comboBox = new ComboBox ();
71 comboBox.Items.Add (text);
72 comboBox.SelectedIndex = 0;
73 comboBox.Select (0, text.Length);
74 comboBox.SelectedText = null;
76 Assert.AreEqual (String.Empty, comboBox.SelectedText);
80 public void DropDownStyle ()
82 ComboBox comboBox = new ComboBox ();
83 comboBox.Items.Add ("abc");
85 Form form = new Form ();
86 form.Controls.Add (comboBox);
89 Assert.AreEqual (ComboBoxStyle.DropDown, comboBox.DropDownStyle, "#1");
90 comboBox.DropDownStyle = ComboBoxStyle.Simple;
91 Assert.AreEqual (ComboBoxStyle.Simple, comboBox.DropDownStyle, "#2");
92 comboBox.DropDownStyle = ComboBoxStyle.DropDown;
93 Assert.AreEqual (ComboBoxStyle.DropDown, comboBox.DropDownStyle, "#3");
99 [Category ("NotWorking")]
100 public void InitialBoundValue ()
103 using (Form f = new Form ()) {
104 f.ShowInTaskbar = false;
106 DataTable t = new DataTable ();
107 t.Columns.Add ("displaymember");
108 t.Columns.Add ("valuemember");
109 t.Rows.Add (new object [] {"lower", "a"});
110 t.Rows.Add (new object [] {"upper", "A"});
112 // This test seems to prove that the SelectedItem is updated when:
113 // - We have a parent
114 // - We're setting either DisplayMember, ValueMember or DataSource.
116 comboBox1 = new ComboBox ();
117 comboBox1.DisplayMember = "displaymember";
118 comboBox1.ValueMember = "valuemember";
119 comboBox1.DataSource = t;
120 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
121 f.Controls.Add (comboBox1);
122 Assert.AreEqual (string.Empty, comboBox1.Text, "#??");
124 comboBox1 = new ComboBox ();
125 f.Controls.AddRange (new Control [] { comboBox1 });
126 comboBox1.DisplayMember = "displaymember";
127 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
128 comboBox1.ValueMember = "valuemember";
129 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
130 comboBox1.DataSource = t;
131 Assert.AreEqual ("lower", comboBox1.Text, "#A02");
132 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
133 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
135 comboBox1 = new ComboBox ();
136 comboBox1.DisplayMember = "displaymember";
137 comboBox1.ValueMember = "valuemember";
138 comboBox1.DataSource = t;
139 Assert.AreEqual (string.Empty, comboBox1.Text, "#A01");
140 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
141 Assert.AreEqual (string.Empty, comboBox1.Text, "#A04");
142 f.Controls.AddRange (new Control [] { comboBox1 });
143 Assert.AreEqual (string.Empty, comboBox1.Text, "#A04");
145 comboBox1 = new ComboBox ();
146 f.Controls.AddRange (new Control [] { comboBox1 });
147 comboBox1.DisplayMember = "displaymember";
148 comboBox1.ValueMember = "valuemember";
149 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02");
150 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
151 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
152 comboBox1.DataSource = t;
153 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
155 comboBox1 = new ComboBox ();
156 f.Controls.AddRange (new Control [] { comboBox1 });
157 comboBox1.DataSource = t;
158 Assert.AreEqual ("System.Data.DataRowView", comboBox1.Text, "#A03");
159 comboBox1.DisplayMember = "displaymember";
160 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
161 comboBox1.ValueMember = "valuemember";
162 Assert.AreEqual ("lower", comboBox1.Text, "#A02");
163 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
164 Assert.AreEqual ("lower", comboBox1.Text, "#A02-1");
167 comboBox1 = new ComboBox ();
168 comboBox1.DisplayMember = "displaymember";
169 comboBox1.ValueMember = "valuemember";
170 Assert.AreEqual ("", comboBox1.Text, "#A02");
171 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
172 Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
173 comboBox1.DataSource = t;
174 Assert.AreEqual (string.Empty, comboBox1.Text, "#A03");
175 f.Controls.AddRange (new Control [] { comboBox1 });
176 Assert.AreEqual (string.Empty, comboBox1.Text, "#A03");
178 comboBox1 = new ComboBox ();
179 comboBox1.DataSource = t;
180 Assert.AreEqual ("", comboBox1.Text, "#A03");
181 comboBox1.DisplayMember = "displaymember";
182 Assert.AreEqual ("", comboBox1.Text, "#A03");
183 comboBox1.ValueMember = "valuemember";
184 Assert.AreEqual ("", comboBox1.Text, "#A02");
185 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
186 Assert.AreEqual ("", comboBox1.Text, "#A02-1");
187 f.Controls.AddRange (new Control [] { comboBox1 });
188 Assert.AreEqual ("", comboBox1.Text, "#A02-1");
191 comboBox1 = new ComboBox ();
192 f.Controls.AddRange (new Control [] { comboBox1 });
193 comboBox1.DisplayMember = "displaymember";
194 Assert.AreEqual ("", comboBox1.Text, "#A03");
195 comboBox1.ValueMember = "valuemember";
196 Assert.AreEqual ("", comboBox1.Text, "#A03");
197 comboBox1.DataSource = t;
198 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
199 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
200 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
202 comboBox1 = new ComboBox ();
203 f.Controls.AddRange (new Control [] { comboBox1 });
204 comboBox1.DisplayMember = "displaymember";
205 Assert.AreEqual ("", comboBox1.Text, "#A03");
206 comboBox1.ValueMember = "valuemember";
207 Assert.AreEqual ("", comboBox1.Text, "#A03");
208 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
209 Assert.AreEqual ("", comboBox1.Text, "#A03");
210 comboBox1.DataSource = t;
211 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
213 comboBox1 = new ComboBox ();
214 f.Controls.AddRange (new Control [] { comboBox1 });
215 comboBox1.ValueMember = "valuemember";
216 Assert.AreEqual ("", comboBox1.Text, "#A03");
217 comboBox1.DataSource = t;
218 Assert.AreEqual ("a", comboBox1.Text, "#A03");
219 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
220 Assert.AreEqual ("a", comboBox1.Text, "#A03");
221 comboBox1.DisplayMember = "displaymember";
222 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
224 comboBox1 = new ComboBox ();
225 comboBox1.DataSource = t;
226 Assert.AreEqual ("", comboBox1.Text, "#A03");
227 comboBox1.DisplayMember = "displaymember";
228 Assert.AreEqual ("", comboBox1.Text, "#A03");
229 comboBox1.ValueMember = "valuemember";
230 Assert.AreEqual ("", comboBox1.Text, "#A03");
231 f.Controls.AddRange (new Control [] { comboBox1 });
232 Assert.AreEqual ("lower", comboBox1.Text, "#A03");
233 InitialBoundValue_dummy e = new InitialBoundValue_dummy ();
235 comboBox1.DataBindings.Add ("SelectedValue", e, "controlsrc");
236 Assert.AreEqual ("", comboBox1.Text, "#A03");
238 Assert.AreEqual ("", comboBox1.Text, "#A03");
240 // The real failing test
241 comboBox1 = new ComboBox ();
242 comboBox1.DisplayMember = "displaymember";
243 Assert.AreEqual ("", comboBox1.Text, "#A03");
244 comboBox1.ValueMember = "valuemember";
245 Assert.AreEqual ("", comboBox1.Text, "#A03");
246 comboBox1.DataSource = t;
247 Assert.AreEqual ("", comboBox1.Text, "#A03");
248 comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
249 Assert.AreEqual ("", comboBox1.Text, "#A03");
250 f.Controls.AddRange (new Control [] { comboBox1 });
251 Assert.AreEqual ("", comboBox1.Text, "#A03");
253 Assert.AreEqual ("", comboBox1.Text, "#A03");
255 Assert.AreEqual ("", comboBox1.Text, "#bug");
258 @"<?xml version=""1.0"" standalone=""yes""?>
266 @"<?xml version=""1.0"" standalone=""yes""?>
278 using (Form frm = new Form ()) {
279 frm.ShowInTaskbar = false;
280 DataSet dsTable = new DataSet ();
281 dsTable.ReadXml (new StringReader (table));
282 DataSet dsLookup = new DataSet ();
283 dsLookup.ReadXml (new StringReader (lookup));
284 ComboBox cb = new ComboBox ();
285 cb.DataSource = dsLookup.Tables [0];
286 cb.DisplayMember = "nimi";
287 cb.ValueMember = "kood";
288 InitialBoundValue_dummy d = new InitialBoundValue_dummy();
289 d.controlsrc = "287";
290 cb.DataBindings.Add ("SelectedValue", d, "controlsrc");
291 frm.Controls.Add (cb);
292 Assert.AreEqual ("", cb.Text, "#01");
294 Assert.AreEqual ("SUCCESS", cb.Text, "#02");
299 class InitialBoundValue_dummy
302 public string controlsrc
310 public void ContextMenuTest ()
312 ComboBox cmb = new ComboBox ();
313 ContextMenu cm = new ContextMenu ();
315 Assert.IsNull (cmb.ContextMenu, "#1");
316 cmb.ContextMenu = cm;
317 Assert.AreSame (cmb.ContextMenu, cm, "#2");
318 cmb.DropDownStyle = ComboBoxStyle.DropDown;
319 Assert.AreSame (cmb.ContextMenu, cm, "#3");
320 cmb.DropDownStyle = ComboBoxStyle.DropDownList;
321 Assert.AreSame (cmb.ContextMenu, cm, "#4");
322 cmb.DropDownStyle = ComboBoxStyle.Simple;
323 Assert.AreSame (cmb.ContextMenu, cm, "#5");
328 public void DataBindingTest ()
331 @"<?xml version=""1.0"" standalone=""yes""?>
339 @"<?xml version=""1.0"" standalone=""yes""?>
351 using (Form frm = new Form ()) {
352 frm.ShowInTaskbar = false;
353 DataSet dsTable = new DataSet ();
354 dsTable.ReadXml (new StringReader (table));
355 DataSet dsLookup = new DataSet ();
356 dsLookup.ReadXml (new StringReader (lookup));
357 ComboBox cb = new ComboBox ();
358 cb.DataSource = dsLookup.Tables [0];
359 cb.DisplayMember = "nimi";
360 cb.ValueMember = "kood";
361 cb.DataBindings.Add ("SelectedValue", dsTable.Tables [0], "klient");
362 frm.Controls.Add (cb);
363 Assert.AreEqual ("", cb.Text, "#01");
365 Assert.AreEqual ("SUCCESS", cb.Text, "#02");
370 public void DataBindingTest2 ()
372 ComboBox cmb = new ComboBox ();
373 cmb.Items.Add (new MockItem ("Foo", 9));
374 cmb.Items.Add (new MockItem ("Bar", 6));
375 cmb.ValueMember = "Value";
376 cmb.DisplayMember = "Text";
378 Form form = new Form ();
379 form.Controls.Add (cmb);
382 cmb.SelectedIndex = 0;
383 Assert.AreEqual ("Foo", cmb.Text, "#1");
384 cmb.SelectedIndex = 1;
385 Assert.AreEqual ("Bar", cmb.Text, "#2");
391 public void ComboBoxPropertyTest ()
393 ComboBox mycmbbox = new ComboBox ();
394 Assert.AreEqual (DrawMode.Normal, mycmbbox.DrawMode, "#1");
395 Assert.AreEqual (ComboBoxStyle.DropDown, mycmbbox.DropDownStyle, "#2");
396 Assert.AreEqual (false, mycmbbox.DroppedDown, "#4");
397 Assert.AreEqual (true, mycmbbox.IntegralHeight, "#5");
398 Assert.AreEqual (0, mycmbbox.Items.Count, "#6");
399 //Assert.AreEqual (15, mycmbbox.ItemHeight, "#7"); // Note: Item height depends on the current font.
400 Assert.AreEqual (8, mycmbbox.MaxDropDownItems, "#8");
401 Assert.AreEqual (0, mycmbbox.MaxLength, "#9");
402 //Assert.AreEqual (20, mycmbbox.PreferredHeight, "#10");
403 // Note: Item height depends on the current font.
404 Assert.AreEqual (-1, mycmbbox.SelectedIndex, "#11");
405 Assert.AreEqual (null, mycmbbox.SelectedItem, "#12");
406 Assert.AreEqual ("", mycmbbox.SelectedText, "#13");
407 Assert.AreEqual (0, mycmbbox.SelectionLength, "#14");
408 Assert.AreEqual (0, mycmbbox.SelectionStart, "#15");
409 Assert.AreEqual (false, mycmbbox.Sorted, "#16");
410 Assert.AreEqual ("", mycmbbox.Text, "#17");
411 Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#18");
412 Assert.AreEqual (AutoCompleteMode.None, mycmbbox.AutoCompleteMode, "#19");
413 Assert.AreEqual (AutoCompleteSource.None, mycmbbox.AutoCompleteSource, "#20");
415 mycmbbox.AutoCompleteCustomSource = null;
416 Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#21");
418 Assert.AreEqual (ImageLayout.Tile, mycmbbox.BackgroundImageLayout, "#22");
419 Assert.AreEqual (null, mycmbbox.DataSource, "#23");
420 Assert.AreEqual (106, mycmbbox.DropDownHeight, "#24");
421 Assert.AreEqual (FlatStyle.Standard, mycmbbox.FlatStyle, "#25");
422 Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MaximumSize.ToString (), "#26");
423 Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MinimumSize.ToString (), "#27");
424 Assert.AreEqual ("{Left=0,Top=0,Right=0,Bottom=0}", mycmbbox.Padding.ToString (), "#28");
429 public void PreferredHeight ()
431 // PreferredHeight is not tied to ItemHeight like we had it
432 ComboBox cb = new ComboBox ();
434 int h = cb.PreferredHeight;
437 Assert.AreEqual (h, cb.PreferredHeight, "A2");
440 Assert.AreEqual (h, cb.PreferredHeight, "A3");
444 public void ResetTextTest ()
446 ComboBox cmbbox = new ComboBox ();
447 Assert.AreEqual ("", cmbbox.Text, "#01");
449 Assert.AreEqual ("abc", cmbbox.Text, "#02");
451 Assert.AreEqual ("", cmbbox.Text, "#03");
455 public void BackgroundImageLayoutTest ()
457 ComboBox cmbbox = new ComboBox ();
458 cmbbox.BackgroundImageLayout = ImageLayout.Stretch;
459 Assert.AreEqual (ImageLayout.Stretch, cmbbox.BackgroundImageLayout, "#01");
463 public void DropDownHeightTest ()
465 ComboBox cmbbox = new ComboBox ();
466 cmbbox.DropDownHeight = 225;
467 Assert.AreEqual (225, cmbbox.DropDownHeight, "#01");
468 cmbbox.DropDownHeight = 1;
469 Assert.AreEqual (1, cmbbox.DropDownHeight, "#02");
473 [ExpectedException (typeof (ArgumentOutOfRangeException))]
474 public void DropDownHeightExceptionTest1 ()
476 ComboBox cmbbox = new ComboBox ();
477 cmbbox.DropDownHeight = -225;
481 [ExpectedException (typeof (ArgumentOutOfRangeException))]
482 public void DropDownHeightExceptionTest2 ()
484 ComboBox cmbbox = new ComboBox ();
485 cmbbox.DropDownHeight = 0;
489 public void FlatStyleTest ()
491 ComboBox cmbbox = new ComboBox ();
492 cmbbox.FlatStyle = FlatStyle.Popup;
493 Assert.AreEqual (FlatStyle.Popup, cmbbox.FlatStyle, "#01");
497 [ExpectedException (typeof (InvalidEnumArgumentException))]
498 public void FlatStyleExceptionTest ()
500 ComboBox cmbbox = new ComboBox ();
501 cmbbox.FlatStyle = (FlatStyle) (-123);
505 public void MaximumSizeTest ()
507 ComboBox cmbbox = new ComboBox ();
508 cmbbox.MaximumSize = new Size (25, 25);
509 Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MaximumSize.ToString (), "#01");
510 cmbbox.MaximumSize = new Size (50, 75);
511 Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MaximumSize.ToString (), "#02");
515 public void MinumumSizeTest ()
517 ComboBox cmbbox = new ComboBox ();
518 cmbbox.MinimumSize = new Size (25, 25);
519 Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MinimumSize.ToString (), "#1");
520 cmbbox.MinimumSize = new Size (50, 75);
521 Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MinimumSize.ToString (), "#2");
525 public void PaddingTest ()
527 ComboBox cmbbox = new ComboBox ();
528 cmbbox.Padding = new Padding (21);
529 Assert.AreEqual ("{Left=21,Top=21,Right=21,Bottom=21}", cmbbox.Padding.ToString (), "#01");
533 public void BeginEndUpdateTest ()
535 Form myform = new Form ();
536 myform.ShowInTaskbar = false;
537 myform.Visible = true;
538 ComboBox cmbbox = new ComboBox ();
539 cmbbox.Items.Add ("A");
540 cmbbox.Visible = true;
541 myform.Controls.Add (cmbbox);
542 cmbbox.BeginUpdate ();
543 for (int x = 1 ; x < 5000 ; x++) {
544 cmbbox.Items.Add ("Item " + x.ToString ());
551 public void FindString ()
553 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
555 ComboBox cmbbox = new ComboBox ();
556 Assert.AreEqual (-1, cmbbox.FindString ("Hello"), "#A1");
557 Assert.AreEqual (-1, cmbbox.FindString (string.Empty), "#A2");
558 Assert.AreEqual (-1, cmbbox.FindString (null), "#A3");
559 Assert.AreEqual (-1, cmbbox.FindString ("Hola", -5), "#A4");
560 Assert.AreEqual (-1, cmbbox.FindString ("Hola", 40), "#A5");
562 cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
563 Assert.AreEqual (2, cmbbox.FindString ("I"), "#B1");
564 Assert.AreEqual (0, cmbbox.FindString ("in"), "#B2");
565 Assert.AreEqual (1, cmbbox.FindString ("BAD"), "#B3");
566 Assert.AreEqual (1, cmbbox.FindString ("Bad"), "#B4");
567 Assert.AreEqual (1, cmbbox.FindString ("b"), "#B5");
568 Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#B6");
569 Assert.AreEqual (-1, cmbbox.FindString (null), "#B7");
571 Assert.AreEqual (3, cmbbox.FindString ("b", 2), "#C1");
572 Assert.AreEqual (5, cmbbox.FindString ("I", 3), "#C2");
573 Assert.AreEqual (4, cmbbox.FindString ("B", 3), "#C3");
574 Assert.AreEqual (1, cmbbox.FindString ("B", 4), "#C4");
575 Assert.AreEqual (5, cmbbox.FindString ("I", 4), "#C5");
576 Assert.AreEqual (4, cmbbox.FindString ("BA", 3), "#C6");
577 Assert.AreEqual (0, cmbbox.FindString ("i", -1), "#C7");
578 Assert.AreEqual (3, cmbbox.FindString (string.Empty, 2), "#C8");
579 Assert.AreEqual (-1, cmbbox.FindString (null, 1), "#C9");
581 cmbbox.Items.Add (string.Empty);
582 Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#D1");
583 Assert.AreEqual (-1, cmbbox.FindString (null), "#D2");
585 Assert.AreEqual (4, cmbbox.FindString (string.Empty, 3), "#E1");
586 Assert.AreEqual (-1, cmbbox.FindString (null, 99), "#E2");
587 Assert.AreEqual (-1, cmbbox.FindString (null, -5), "#E3");
591 public void FindString_StartIndex_ItemCount ()
593 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
595 ComboBox cmbbox = new ComboBox ();
596 cmbbox.Items.AddRange (new object [] { "BA", "BB" });
597 Assert.AreEqual (0, cmbbox.FindString ("b", 1));
601 public void FindString_StartIndex_Min ()
603 ComboBox cmbbox = new ComboBox ();
604 cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
606 cmbbox.FindString ("Hola", -2);
608 } catch (ArgumentOutOfRangeException ex) {
609 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
610 Assert.IsNull (ex.InnerException, "#3");
611 Assert.IsNotNull (ex.Message, "#4");
612 Assert.IsNotNull (ex.ParamName, "#5");
613 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
618 public void FindString_StartIndex_Max ()
620 ComboBox cmbbox = new ComboBox ();
621 cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
623 cmbbox.FindString ("Hola", 4);
625 } catch (ArgumentOutOfRangeException ex) {
626 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
627 Assert.IsNull (ex.InnerException, "#3");
628 Assert.IsNotNull (ex.Message, "#4");
629 Assert.IsNotNull (ex.ParamName, "#5");
630 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
635 public void FindStringExact ()
637 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
639 ComboBox cmbbox = new ComboBox ();
640 Assert.AreEqual (-1, cmbbox.FindStringExact ("Hello"), "#A1");
641 Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#A2");
642 Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#A3");
643 Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", -5), "#A4");
644 Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", 40), "#A5");
646 cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
647 Assert.AreEqual (2, cmbbox.FindStringExact ("IN"), "#B1");
648 Assert.AreEqual (0, cmbbox.FindStringExact ("in"), "#B2");
649 Assert.AreEqual (3, cmbbox.FindStringExact ("BAD"), "#B3");
650 Assert.AreEqual (3, cmbbox.FindStringExact ("bad"), "#B4");
651 Assert.AreEqual (-1, cmbbox.FindStringExact ("B"), "#B5");
652 Assert.AreEqual (-1, cmbbox.FindStringExact ("NonExistant"), "#B6");
653 Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#B7");
654 Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#B8");
656 Assert.AreEqual (2, cmbbox.FindStringExact ("In", 1), "#C1");
657 Assert.AreEqual (5, cmbbox.FindStringExact ("In", 2), "#C2");
658 Assert.AreEqual (4, cmbbox.FindStringExact ("BaD", 3), "#C3");
659 Assert.AreEqual (3, cmbbox.FindStringExact ("bad", -1), "#C4");
660 Assert.AreEqual (5, cmbbox.FindStringExact ("In", 4), "#C5");
661 Assert.AreEqual (3, cmbbox.FindStringExact ("bad", 4), "#C6");
662 Assert.AreEqual (-1, cmbbox.FindStringExact ("B", 4), "#C7");
663 Assert.AreEqual (-1, cmbbox.FindStringExact ("BADNOT", 0), "#C8");
664 Assert.AreEqual (-1, cmbbox.FindStringExact ("i", -1), "#C9");
665 Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty, 2), "#C10");
666 Assert.AreEqual (-1, cmbbox.FindStringExact (null, 1), "#C11");
668 cmbbox.Items.Add (string.Empty);
669 Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty), "#D1");
670 Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#D2");
672 Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty, 3), "#E1");
673 Assert.AreEqual (-1, cmbbox.FindStringExact (null, 99), "#E2");
674 Assert.AreEqual (-1, cmbbox.FindStringExact (null, -5), "#E3");
678 public void FindStringExact_StartIndex_ItemCount ()
680 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
682 ComboBox cmbbox = new ComboBox ();
683 cmbbox.Items.AddRange (new object [] { "AB", "BA", "AB", "BA" });
684 Assert.AreEqual (1, cmbbox.FindStringExact ("BA", 3));
688 public void FindStringExact_StartIndex_Min ()
690 ComboBox cmbbox = new ComboBox ();
691 cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
693 cmbbox.FindStringExact ("Hola", -2);
695 } catch (ArgumentOutOfRangeException ex) {
696 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
697 Assert.IsNull (ex.InnerException, "#3");
698 Assert.IsNotNull (ex.Message, "#4");
699 Assert.IsNotNull (ex.ParamName, "#5");
700 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
705 public void FindStringExact_StartIndex_Max ()
707 ComboBox cmbbox = new ComboBox ();
708 cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
710 cmbbox.FindStringExact ("Hola", 4);
712 } catch (ArgumentOutOfRangeException ex) {
713 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
714 Assert.IsNull (ex.InnerException, "#3");
715 Assert.IsNotNull (ex.Message, "#4");
716 Assert.IsNotNull (ex.ParamName, "#5");
717 Assert.AreEqual ("startIndex", ex.ParamName, "#6");
722 public void GetItemHeightTest ()
724 ComboBox cmbbox = new ComboBox ();
725 cmbbox.Items.Add ("ABC");
726 cmbbox.Items.Add ("BCD");
727 cmbbox.Items.Add ("DEF");
729 x = cmbbox.GetItemHeight (x);
730 Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
738 [ExpectedException (typeof (InvalidEnumArgumentException))]
739 public void DropDownStyleException ()
741 ComboBox cmbbox = new ComboBox ();
742 cmbbox.DropDownStyle = (ComboBoxStyle) 10;
746 [ExpectedException (typeof (InvalidEnumArgumentException))]
747 public void DrawModeException ()
749 ComboBox cmbbox = new ComboBox ();
750 cmbbox.DrawMode = (DrawMode) 10;
754 public void DropDownWidth ()
756 ComboBox cmbbox = new ComboBox ();
757 Assert.AreEqual (121, cmbbox.DropDownWidth, "#A1");
758 cmbbox.DropDownWidth = 1;
759 Assert.AreEqual (1, cmbbox.DropDownWidth, "#A2");
762 cmbbox.DropDownWidth = 0;
765 catch (ArgumentOutOfRangeException ex) {
766 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
767 Assert.IsNotNull (ex.Message, "#B3");
768 Assert.IsNotNull (ex.ParamName, "#B4");
769 Assert.AreEqual ("DropDownWidth", ex.ParamName, "#B5");
770 Assert.IsNull (ex.InnerException, "#B6");
775 public void ItemHeight ()
777 ComboBox cmbbox = new ComboBox ();
778 Assert.IsTrue (cmbbox.ItemHeight >= 1, "#A1");
779 cmbbox.ItemHeight = 1;
780 Assert.AreEqual (1, cmbbox.ItemHeight, "#A2");
783 cmbbox.ItemHeight = 0;
786 catch (ArgumentOutOfRangeException ex) {
787 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
788 Assert.IsNotNull (ex.Message, "#B3");
789 Assert.IsNotNull (ex.ParamName, "#B4");
790 Assert.AreEqual ("ItemHeight", ex.ParamName, "#B5");
791 Assert.IsNull (ex.InnerException, "#B6");
796 [ExpectedException (typeof (ArgumentOutOfRangeException))]
797 public void SelectedIndexException ()
799 ComboBox cmbbox = new ComboBox ();
800 cmbbox.SelectedIndex = -2;
803 //Bug 2234 (Xamarin) : Test 1
805 public void VerifyNoExceptions2234()
807 using (Form form = new Form ()){
808 ComboBox cmb = new ComboBox();
809 form.Controls.Add (cmb);
811 eventFired=false; //for sanity
813 //Primary failure: if exception is raised when
814 // DataSource changes. We should "eat" the
815 // exception under this circumstance before
817 cmb.SelectedIndexChanged +=
818 new EventHandler(GenericHandlerWithException);
819 cmb.DataSource=new string[]{"One","Two","Three"};
820 Assert.IsTrue(eventFired);
824 //Bug 2234 (Xamarin) : Test 2
826 [ExpectedException (typeof (Exception))]
827 public void VerifyException2234()
829 using (Form form = new Form ())
831 ComboBox cmb = new ComboBox();
832 form.Controls.Add (cmb);
834 cmb.SelectedIndexChanged +=
835 new EventHandler(GenericHandlerWithException);
836 cmb.DataSource=new string[]{"One","Two","Three"};
838 //Here's where Exception Should raise normally
847 private bool eventFired;
848 private DrawItemEventArgs drawItemsArgs;
849 private void DrawItemEventH (object sender, DrawItemEventArgs e)
855 private void GenericHandler (object sender, EventArgs e)
860 private void GenericHandlerWithException (object sender, EventArgs e)
863 throw new Exception("Crash!");
867 [Ignore ("Bugs in X11 prevent this test to run properly")]
868 public void DrawItemEventTest ()
871 drawItemsArgs = null;
872 Form myform = new Form ();
873 myform.ShowInTaskbar = false;
874 ComboBox cmbbox = new ComboBox ();
875 cmbbox.DropDownStyle = ComboBoxStyle.Simple;
876 cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
877 cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
879 myform.Controls.Add (cmbbox);
880 cmbbox.Items.AddRange(new object[] {"Item1"});
882 myform.Visible = true;
883 cmbbox.Visible = true;
886 Assert.AreEqual (true, eventFired, "DW1");
887 Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
892 public void DropDownStyleEventTest ()
895 ComboBox cmbbox = new ComboBox ();
896 cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
897 cmbbox.DropDownStyle = ComboBoxStyle.Simple;
899 Assert.AreEqual (true, eventFired, "DI1");
903 public void SelectedIndexTest ()
906 ComboBox cmbbox = new ComboBox ();
907 cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
908 cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
909 cmbbox.SelectedIndex = 1;
910 Assert.AreEqual (true, eventFired, "SI1");
914 public void SelectionWithAdd()
916 ComboBox cb = new ComboBox();
917 cb.SelectedIndexChanged += new EventHandler(GenericHandler);
918 cb.Items.Add("Item 1");
919 cb.Items.Add("Item 3");
920 cb.SelectedIndex = 1;
922 cb.Items.Add("Item 4");
923 Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
924 Assert.AreEqual(false, eventFired, "SWA2");
926 cb.SelectedIndex = 1;
928 cb.Items.Add("Item 5");
929 Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
930 Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
931 Assert.AreEqual(false, eventFired, "SWA5");
932 cb.SelectedIndex = 1;
934 cb.Items.Add("Item 2");
935 Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
936 Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
937 Assert.AreEqual(false, eventFired, "SWA8");
941 public void SelectionWithInsert()
943 ComboBox cb = new ComboBox();
944 cb.SelectedIndexChanged += new EventHandler(GenericHandler);
945 cb.Items.Add("Item 1");
946 cb.SelectedIndex = 0;
948 cb.Items.Insert(0, "Item 2");
949 Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
950 Assert.AreEqual(false, eventFired, "SWI2");
955 // https://bugzilla.xamarin.com/show_bug.cgi?id=5595
956 public void SelectionWithDeletion()
962 // Create a form with a combo box.
964 form.ShowInTaskbar = false;
965 ComboBox cb = new ComboBox ();
966 cb.DropDownStyle = ComboBoxStyle.DropDownList;
970 // Add some items to the combo box.
971 cb.Items.Add ("Item 0");
972 cb.Items.Add ("Item 1");
973 cb.Items.Add ("Item 2");
975 // Select the last item.
976 cb.SelectedIndex = 2;
977 Assert.AreEqual(2, cb.SelectedIndex, "SWD1");
979 // Show the combo box's dropdown.
980 cb.DroppedDown = true;
982 // Display the results.
983 Application.DoEvents();
985 // Hide the combo box's dropdown.
986 cb.DroppedDown = false;
988 // Display the results.
989 Application.DoEvents();
991 // Delete an item before the selection.
992 // That should move the selection down.
993 // Before the bug fix, it would remain 2.
994 cb.Items.RemoveAt (1);
995 Assert.AreEqual(1, cb.SelectedIndex, "SWD2");
997 // Show the combo box's dropdown.
998 // Before the bug fix, this would throw an
999 // ArgumentOutOfRangeException, because the
1000 // selected index was still 2, and hence
1002 cb.DroppedDown = true;
1003 Assert.AreEqual(1, cb.SelectedIndex, "SWD3");
1005 // Display the results.
1006 Application.DoEvents();
1010 // Get rid of the form.
1017 public void SelectionWithClear()
1019 ComboBox cb = new ComboBox();
1020 cb.SelectedIndexChanged += new EventHandler(GenericHandler);
1021 cb.Items.Add("Item 1");
1022 cb.SelectedIndex = 0;
1025 Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
1026 Assert.AreEqual(false, eventFired, "SWC2");
1030 public void SelectionWithModification()
1032 // ComboBox's this[int].set() was dealing with the
1033 // current index badly. This reproduces the old bug and
1034 // makes sure it's fixed.
1035 ComboBox cb = new ComboBox ();
1036 cb.DropDownStyle = ComboBoxStyle.DropDown;
1039 string strItemText = "Item text";
1040 cb.Items.Add (strItemText);
1042 // Select the menu item.
1043 cb.SelectedIndex = 0;
1045 // Move the selection to the end of the text.
1046 cb.SelectionLength = 0;
1047 cb.SelectionStart = strItemText.Length;
1049 // Now set the menu item's text to the empty string and
1051 cb.Items[0] = string.Empty;
1052 cb.Items[0] = strItemText;
1054 // Make sure the text didn't get duplicated (i.e. the
1055 // bugged code would produce "Item textItem text").
1056 Assert.AreEqual (strItemText, cb.Text, "A1");
1059 // Bug #333750 - DisplayMember assignation
1060 // when ComboBox already has a BindingContext BUT
1061 // handle hasn't been created yet.
1063 public void SelectedTextWithBindingTest ()
1065 Form form = new Form ();
1066 form.ShowInTaskbar = false;
1067 ComboBox cb = new ComboBox ();
1070 MockItem [] items = new MockItem [] {
1071 new MockItem ("A", 0),
1072 new MockItem ("B", 1),
1073 new MockItem ("C", 2)
1076 cb.DataSource = items;
1077 cb.DisplayMember = "Text";
1081 Assert.AreEqual ("A", cb.SelectedText, "#A1");
1087 public void SortedTest()
1089 ComboBox mycb = new ComboBox();
1090 Assert.AreEqual(false, mycb.Sorted, "#1");
1091 mycb.Items.Add("Item 2");
1092 mycb.Items.Add("Item 1");
1093 Assert.AreEqual("Item 2", mycb.Items[0], "#2");
1094 Assert.AreEqual("Item 1", mycb.Items[1], "#3");
1096 Assert.AreEqual(true, mycb.Sorted, "#4");
1097 Assert.AreEqual("Item 1", mycb.Items[0], "#5");
1098 Assert.AreEqual("Item 2", mycb.Items[1], "#6");
1099 mycb.Sorted = false;
1100 Assert.AreEqual(false, mycb.Sorted, "#7");
1101 Assert.AreEqual("Item 1", mycb.Items[0], "#8");
1102 Assert.AreEqual("Item 2", mycb.Items[1], "#9");
1106 public void SortedAddTest()
1108 ComboBox mycb = new ComboBox();
1109 mycb.Items.Add("Item 2");
1110 mycb.Items.Add("Item 1");
1112 Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
1113 Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
1117 public void SortedInsertTest()
1119 ComboBox mycb = new ComboBox();
1120 mycb.Items.Add("Item 2");
1121 mycb.Items.Add("Item 1");
1123 mycb.Items.Insert (0, "Item 3");
1124 Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
1125 Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
1126 Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
1130 public void SortedSelectionInteractions()
1132 ComboBox cb = new ComboBox();
1133 cb.SelectedIndexChanged += new EventHandler(GenericHandler);
1134 cb.Items.Add("Item 1");
1135 cb.Items.Add("Item 2");
1136 cb.Items.Add("Item 3");
1137 cb.SelectedIndex = 1;
1140 Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
1141 Assert.AreEqual(true, eventFired, "#SSI2");
1142 cb.SelectedIndex = 1;
1145 Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
1146 Assert.AreEqual(false, eventFired, "#SSI4");
1147 cb.SelectedIndex = 1;
1150 Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
1151 Assert.AreEqual(true, eventFired, "#SSI6");
1155 public void Text_DropDown ()
1157 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
1159 ComboBox cmbbox = new ComboBox ();
1160 Assert.IsNotNull (cmbbox.Text, "#A1");
1161 Assert.AreEqual (string.Empty, cmbbox.Text, "#A2");
1162 Assert.AreEqual (-1, cmbbox.SelectedIndex, "#A3");
1164 cmbbox.Items.Add ("Another");
1165 cmbbox.Items.Add ("Bad");
1166 cmbbox.Items.Add ("IN");
1167 cmbbox.Items.Add ("Combobox");
1168 cmbbox.Items.Add ("BAD");
1169 cmbbox.Items.Add ("iN");
1170 cmbbox.Items.Add ("Bad");
1172 Assert.IsNotNull (cmbbox.Text, "#B1");
1173 Assert.AreEqual (string.Empty, cmbbox.Text, "#B2");
1174 Assert.AreEqual (-1, cmbbox.SelectedIndex, "#B3");
1176 cmbbox.SelectedIndex = 3;
1177 Assert.IsNotNull (cmbbox.Text, "#C1");
1178 Assert.AreEqual ("Combobox", cmbbox.Text, "#C2");
1179 Assert.AreEqual (3, cmbbox.SelectedIndex, "#C3");
1181 cmbbox.Text = string.Empty;
1182 Assert.IsNotNull (cmbbox.Text, "#D1");
1183 Assert.AreEqual (string.Empty, cmbbox.Text, "#D2");
1184 Assert.AreEqual (3, cmbbox.SelectedIndex, "#D3");
1186 cmbbox.SelectedIndex = 1;
1187 Assert.IsNotNull (cmbbox.Text, "#E1");
1188 Assert.AreEqual ("Bad", cmbbox.Text, "#E2");
1189 Assert.AreEqual (1, cmbbox.SelectedIndex, "#E3");
1192 Assert.IsNotNull (cmbbox.Text, "#F1");
1193 Assert.AreEqual (string.Empty, cmbbox.Text, "#F2");
1194 Assert.AreEqual (-1, cmbbox.SelectedIndex, "#F3");
1196 cmbbox.SelectedIndex = 0;
1198 Assert.IsNotNull (cmbbox.Text, "#G1");
1199 Assert.AreEqual ("Q", cmbbox.Text, "#G2");
1200 Assert.AreEqual (0, cmbbox.SelectedIndex, "#G3");
1203 Assert.IsNotNull (cmbbox.Text, "#H1");
1204 Assert.AreEqual ("B", cmbbox.Text, "#H2");
1205 Assert.AreEqual (0, cmbbox.SelectedIndex, "#H3");
1207 cmbbox.Text = "BAD";
1208 Assert.IsNotNull (cmbbox.Text, "#I1");
1209 Assert.AreEqual ("BAD", cmbbox.Text, "#I2");
1210 Assert.AreEqual (4, cmbbox.SelectedIndex, "#I3");
1212 cmbbox.Text = "BAD";
1213 Assert.IsNotNull (cmbbox.Text, "#J1");
1214 Assert.AreEqual ("BAD", cmbbox.Text, "#J2");
1215 Assert.AreEqual (4, cmbbox.SelectedIndex, "#J3");
1217 cmbbox.Text = "Something";
1218 Assert.IsNotNull (cmbbox.Text, "#T1");
1219 Assert.AreEqual ("Something", cmbbox.Text, "#T2");
1220 Assert.AreEqual (4, cmbbox.SelectedIndex, "#T3");
1222 cmbbox.Text = "BAD";
1223 Assert.IsNotNull (cmbbox.Text, "#U1");
1224 Assert.AreEqual ("BAD", cmbbox.Text, "#U2");
1225 Assert.AreEqual (4, cmbbox.SelectedIndex, "#U3");
1227 cmbbox.Text = "baD";
1228 Assert.IsNotNull (cmbbox.Text, "#K1");
1229 Assert.AreEqual ("Bad", cmbbox.Text, "#K2");
1230 Assert.AreEqual (1, cmbbox.SelectedIndex, "#K3");
1232 cmbbox.SelectedIndex = -1;
1234 Assert.IsNotNull (cmbbox.Text, "#L1");
1235 Assert.AreEqual ("E", cmbbox.Text, "#L2");
1236 Assert.AreEqual (-1, cmbbox.SelectedIndex, "#L3");
1239 Assert.IsNotNull (cmbbox.Text, "#M1");
1240 Assert.AreEqual ("iN", cmbbox.Text, "#M2");
1241 Assert.AreEqual (5, cmbbox.SelectedIndex, "#M3");
1244 Assert.IsNotNull (cmbbox.Text, "#N1");
1245 Assert.AreEqual ("IN", cmbbox.Text, "#N2");
1246 Assert.AreEqual (2, cmbbox.SelectedIndex, "#N3");
1248 cmbbox.Text = "Badd";
1249 Assert.IsNotNull (cmbbox.Text, "#O1");
1250 Assert.AreEqual ("Badd", cmbbox.Text, "#O2");
1251 Assert.AreEqual (2, cmbbox.SelectedIndex, "#O3");
1253 cmbbox.SelectedIndex = -1;
1254 Assert.IsNotNull (cmbbox.Text, "#P1");
1255 Assert.AreEqual (string.Empty, cmbbox.Text, "#P2");
1256 Assert.AreEqual (-1, cmbbox.SelectedIndex, "#P3");
1258 cmbbox.Text = "Something";
1259 Assert.IsNotNull (cmbbox.Text, "#Q1");
1260 Assert.AreEqual ("Something", cmbbox.Text, "#Q2");
1261 Assert.AreEqual (-1, cmbbox.SelectedIndex, "#Q3");
1263 cmbbox.SelectedIndex = -1;
1264 Assert.IsNotNull (cmbbox.Text, "#R1");
1265 Assert.AreEqual ("Something", cmbbox.Text, "#R2");
1266 Assert.AreEqual (-1, cmbbox.SelectedIndex, "#R3");
1269 Assert.IsNotNull (cmbbox.Text, "#S1");
1270 Assert.AreEqual (string.Empty, cmbbox.Text, "#S2");
1271 Assert.AreEqual (-1, cmbbox.SelectedIndex, "#S3");
1275 public void Text_DataBinding ()
1277 ArrayList objects = new ArrayList ();
1278 objects.Add (new MockItem ("A", 0));
1279 objects.Add (new MockItem ("B", 1));
1281 ComboBox cb = new ComboBox ();
1282 cb.BindingContext = new BindingContext ();
1283 cb.DataSource = objects;
1284 cb.DisplayMember = "Text";
1286 Assert.AreEqual ("A", cb.Text, "#A1");
1287 Assert.AreEqual ("A", ((MockItem)cb.SelectedItem).Text, "#B2");
1288 Assert.AreEqual (0, ((MockItem)cb.SelectedItem).Value, "#B3");
1291 Assert.AreEqual ("B", cb.Text, "#B1");
1292 Assert.AreEqual ("B", ((MockItem)cb.SelectedItem).Text, "#B2");
1293 Assert.AreEqual (1, ((MockItem)cb.SelectedItem).Value, "#B3");
1295 // the text will change graphically, but Text and
1296 // SelectedItem will keep their previous valid values.
1297 cb.Text = "dontexist";
1298 Assert.AreEqual ("B", ((MockItem)cb.SelectedItem).Text, "#C2");
1299 Assert.AreEqual (1, ((MockItem)cb.SelectedItem).Value, "#C3");
1302 [Test] // bug 360862
1303 public void SizeChangesAtCreateHandle ()
1305 ComboBox cb = new ComboBox ();
1306 cb.Font = new Font ("Arial", 24f);
1308 int original = cb.Height;
1310 IntPtr h = cb.Handle;
1312 Assert.IsTrue (cb.Height > original, string.Format ("ComboBox height ({0}) should be bigger than original ({1})", cb.Height, original));
1316 public void Bug424270 ()
1318 ComboBox cb = new ComboBox ();
1319 cb.Items.Add ("ab");
1321 cb.SelectedIndex = 0;
1323 Assert.AreEqual (0, cb.SelectedIndex, "A1");
1324 Assert.AreEqual ("ab", cb.SelectedItem, "A2");
1326 cb.SelectedItem = null;
1328 Assert.AreEqual (-1, cb.SelectedIndex, "A3");
1329 Assert.AreEqual (null, cb.SelectedItem, "A4");
1333 public void BehaviorAutoSize ()
1335 if (TestHelper.RunningOnUnix)
1336 Assert.Ignore ("Depends on font measurements, corresponds to windows");
1338 Form f = new Form ();
1339 f.ShowInTaskbar = false;
1343 Image i = new Bitmap (20, 20);
1344 String s = "My test string";
1346 CheckBox b = new CheckBox ();
1347 Size s_size = TextRenderer.MeasureText (s, b.Font);
1349 b.UseCompatibleTextRendering = false;
1350 b.Size = new Size (5, 5);
1356 b.TextImageRelation = TextImageRelation.Overlay;
1357 Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A1");
1358 b.TextImageRelation = TextImageRelation.ImageAboveText;
1359 b.Size = new Size (5, 5);
1360 Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A2");
1361 b.TextImageRelation = TextImageRelation.ImageBeforeText;
1362 b.Size = new Size (5, 5);
1363 Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A3");
1364 b.TextImageRelation = TextImageRelation.TextAboveImage;
1365 b.Size = new Size (5, 5);
1366 Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A4");
1367 b.TextImageRelation = TextImageRelation.TextBeforeImage;
1368 b.Size = new Size (5, 5);
1369 Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A5");
1373 b.TextImageRelation = TextImageRelation.Overlay;
1374 b.Size = new Size (5, 5);
1375 Assert.AreEqual (new Size (s_size.Width + 19, i.Height), b.Size, "A6");
1376 b.TextImageRelation = TextImageRelation.ImageAboveText;
1377 b.Size = new Size (5, 5);
1378 Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A7");
1379 b.TextImageRelation = TextImageRelation.ImageBeforeText;
1380 b.Size = new Size (5, 5);
1381 Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A8");
1382 b.TextImageRelation = TextImageRelation.TextAboveImage;
1383 b.Size = new Size (5, 5);
1384 Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A9");
1385 b.TextImageRelation = TextImageRelation.TextBeforeImage;
1386 b.Size = new Size (5, 5);
1387 Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A10");
1390 b.Text = string.Empty;
1391 b.TextImageRelation = TextImageRelation.Overlay;
1392 b.Size = new Size (5, 5);
1393 Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A11");
1394 b.TextImageRelation = TextImageRelation.ImageAboveText;
1395 b.Size = new Size (5, 5);
1396 Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A12");
1397 b.TextImageRelation = TextImageRelation.ImageBeforeText;
1398 b.Size = new Size (5, 5);
1399 Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A13");
1400 b.TextImageRelation = TextImageRelation.TextAboveImage;
1401 b.Size = new Size (5, 5);
1402 Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A14");
1403 b.TextImageRelation = TextImageRelation.TextBeforeImage;
1404 b.Size = new Size (5, 5);
1405 Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A15");
1409 b.TextImageRelation = TextImageRelation.Overlay;
1410 b.Size = new Size (5, 5);
1411 Assert.AreEqual (new Size (15, 14), b.Size, "A16");
1412 b.TextImageRelation = TextImageRelation.ImageAboveText;
1413 b.Size = new Size (5, 5);
1414 Assert.AreEqual (new Size (15, 14), b.Size, "A17");
1415 b.TextImageRelation = TextImageRelation.ImageBeforeText;
1416 b.Size = new Size (5, 5);
1417 Assert.AreEqual (new Size (15, 14), b.Size, "A18");
1418 b.TextImageRelation = TextImageRelation.TextAboveImage;
1419 b.Size = new Size (5, 5);
1420 Assert.AreEqual (new Size (15, 14), b.Size, "A19");
1421 b.TextImageRelation = TextImageRelation.TextBeforeImage;
1422 b.Size = new Size (5, 5);
1423 Assert.AreEqual (new Size (15, 14), b.Size, "A20");
1426 b.Padding = new Padding (5, 10, 15, 20);
1427 Assert.AreEqual (new Size (15 + b.Padding.Horizontal, 14 + b.Padding.Vertical), b.Size, "A21");
1433 public void MethodScaleControl ()
1435 Form f = new Form ();
1436 f.ShowInTaskbar = false;
1440 PublicComboBox gb = new PublicComboBox ();
1441 gb.Location = new Point (5, 10);
1442 f.Controls.Add (gb);
1444 Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A1");
1446 gb.PublicScaleControl (new SizeF (2.0f, 2.0f), BoundsSpecified.All);
1447 Assert.AreEqual (new Rectangle (10, 20, 238, gb.PreferredHeight), gb.Bounds, "A2");
1449 gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Location);
1450 Assert.AreEqual (new Rectangle (5, 10, 238, gb.PreferredHeight), gb.Bounds, "A3");
1452 gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Size);
1453 Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A4");
1455 gb.PublicScaleControl (new SizeF (3.5f, 3.5f), BoundsSpecified.Size);
1456 Assert.AreEqual (new Rectangle (5, 10, 414, gb.PreferredHeight), gb.Bounds, "A5");
1458 gb.PublicScaleControl (new SizeF (2.5f, 2.5f), BoundsSpecified.Size);
1459 Assert.AreEqual (new Rectangle (5, 10, 1029, gb.PreferredHeight), gb.Bounds, "A6");
1461 gb.PublicScaleControl (new SizeF (.2f, .2f), BoundsSpecified.Size);
1462 Assert.AreEqual (new Rectangle (5, 10, 209, gb.PreferredHeight), gb.Bounds, "A7");
1467 private class PublicComboBox : ComboBox
1469 public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
1471 base.ScaleControl (factor, specified);
1475 private struct ComboVal
1479 public ComboVal (string t)
1484 public override string ToString ()
1491 public void SortTest ()
1493 // Test sorting of objects with no IComparer
1496 ComboBox cmb = new ComboBox ();
1497 cmb.Items.Add (new ComboVal ("B"));
1498 cmb.Items.Add (new ComboVal ("A"));
1504 public class ComboBoxObjectCollectionTest : TestHelper
1507 public void ComboBoxObjectCollectionPropertyTest ()
1509 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1510 Assert.AreEqual (false, col.IsReadOnly, "#B1");
1511 Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
1512 Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
1513 Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
1517 public void AddTest ()
1519 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1522 Assert.AreEqual (2, col.Count, "#C1");
1526 public void Add_Null ()
1528 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1532 } catch (ArgumentNullException ex) {
1533 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1534 Assert.IsNull (ex.InnerException, "#3");
1535 Assert.IsNotNull (ex.Message, "#4");
1536 Assert.IsNotNull (ex.ParamName, "#5");
1537 Assert.AreEqual ("item", ex.ParamName, "#6");
1542 public void AddRange_Null ()
1544 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1546 col.AddRange (null);
1548 } catch (ArgumentNullException ex) {
1549 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1550 Assert.IsNull (ex.InnerException, "#3");
1551 Assert.IsNotNull (ex.Message, "#4");
1552 Assert.IsNotNull (ex.ParamName, "#5");
1553 Assert.AreEqual ("items", ex.ParamName, "#6");
1558 public void ClearTest ()
1560 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1564 Assert.AreEqual (0, col.Count, "#D1");
1568 public void ContainsTest ()
1570 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1571 object obj = "Item1";
1573 Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
1574 Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
1578 public void Contains_Null ()
1580 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1582 col.Contains (null);
1584 } catch (ArgumentNullException ex) {
1585 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1586 Assert.IsNull (ex.InnerException, "#3");
1587 Assert.IsNotNull (ex.Message, "#4");
1588 Assert.IsNotNull (ex.ParamName, "#5");
1589 Assert.AreEqual ("value", ex.ParamName, "#6");
1594 public void IndexOfTest ()
1596 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1599 Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
1603 public void IndexOf_Null ()
1605 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1609 } catch (ArgumentNullException ex) {
1610 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1611 Assert.IsNull (ex.InnerException, "#3");
1612 Assert.IsNotNull (ex.Message, "#4");
1613 Assert.IsNotNull (ex.ParamName, "#5");
1614 Assert.AreEqual ("value", ex.ParamName, "#6");
1619 public void Insert_Null ()
1621 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1624 col.Insert (0, null);
1626 } catch (ArgumentNullException ex) {
1627 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1628 Assert.IsNull (ex.InnerException, "#3");
1629 Assert.IsNotNull (ex.Message, "#4");
1630 Assert.IsNotNull (ex.ParamName, "#5");
1631 Assert.AreEqual ("item", ex.ParamName, "#6");
1636 public void RemoveTest ()
1638 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1641 col.Remove ("Item1");
1642 Assert.AreEqual (1, col.Count, "#1");
1644 Assert.AreEqual (1, col.Count, "#2");
1645 col.Remove ("Item3");
1646 Assert.AreEqual (1, col.Count, "#3");
1650 public void RemoveAtTest ()
1652 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1656 Assert.AreEqual (1, col.Count, "#H1");
1657 Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
1661 public void Indexer_Null ()
1663 ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
1668 } catch (ArgumentNullException ex) {
1669 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1670 Assert.IsNull (ex.InnerException, "#3");
1671 Assert.IsNotNull (ex.Message, "#4");
1672 Assert.IsNotNull (ex.ParamName, "#5");
1673 Assert.AreEqual ("value", ex.ParamName, "#6");
1678 public class ComboBoxTests : TestHelper
1681 bool textChanged, layoutUpdated;
1684 protected override void SetUp () {
1685 comboBox = new ComboBox ();
1686 textChanged = false;
1687 layoutUpdated = false;
1688 comboBox.TextChanged += new EventHandler (textChangedEventHandler);
1689 comboBox.Layout += new LayoutEventHandler (layoutEventHandler);
1693 private void textChangedEventHandler (object sender, EventArgs e)
1698 private void layoutEventHandler (object sender, LayoutEventArgs e)
1700 layoutUpdated = true;
1704 public void InitialPropertyValues ()
1707 Assert.AreEqual (String.Empty, comboBox.Text);
1708 Assert.AreEqual (-1, comboBox.SelectedIndex);
1709 Assert.IsNull (comboBox.SelectedItem);
1710 Assert.AreEqual (121, comboBox.Size.Width);
1711 //Note: it is environment dependent
1712 //Assert.AreEqual(20, comboBox.Size.Height);
1713 Assert.IsFalse (textChanged);
1714 Assert.IsFalse (layoutUpdated);
1718 public void SetNegativeOneSelectedIndex ()
1720 comboBox.SelectedIndex = -1;
1721 Assert.AreEqual (String.Empty, comboBox.Text);
1722 Assert.IsFalse (textChanged);
1726 public void SetDifferentText ()
1728 comboBox.Text = "foooooooooooooooooooooooooo";
1729 Assert.IsTrue (textChanged);
1730 Assert.IsFalse (layoutUpdated);
1734 public void SetSameText ()
1736 comboBox.Text = String.Empty;
1737 Assert.IsFalse (textChanged);
1738 Assert.IsFalse (layoutUpdated);
1741 [Test] // bug #79812
1742 public void Add_Item_NonString ()
1744 comboBox.Sorted = true;
1745 comboBox.Items.Add (new Person ("B"));
1746 comboBox.Items.Add (new Person ("A"));
1747 comboBox.Items.Add (new Person ("C"));
1748 Assert.AreEqual (string.Empty, comboBox.Text, "#1");
1749 comboBox.SelectedIndex = 0;
1750 Assert.AreEqual ("A", comboBox.Text, "#2");
1751 comboBox.SelectedIndex = 2;
1752 Assert.AreEqual ("C", comboBox.Text, "#3");
1756 [Category ("NotWorking")]
1757 public void SelectedText ()
1759 Form form = new Form ();
1760 form.ShowInTaskbar = false;
1761 form.Visible = false;
1762 form.Controls.Add (comboBox);
1764 comboBox.Items.Add ("Bar");
1765 comboBox.Items.Add ("Foo");
1766 Assert.AreEqual (-1, comboBox.SelectedIndex, "#A1");
1767 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#A2");
1768 comboBox.SelectedIndex = 0;
1769 Assert.AreEqual (0, comboBox.SelectedIndex, "#B1");
1770 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#B2");
1772 Assert.AreEqual (0, comboBox.SelectedIndex, "#C1");
1773 Assert.AreEqual ("Bar", comboBox.SelectedText, "#C2");
1774 comboBox.SelectedIndex = 1;
1775 Assert.AreEqual (1, comboBox.SelectedIndex, "#D1");
1776 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#D2");
1777 comboBox.SelectedText = "Ba";
1778 Assert.AreEqual (-1, comboBox.SelectedIndex, "#E1");
1779 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#E2");
1780 comboBox.SelectedText = "Bar";
1781 Assert.AreEqual (-1, comboBox.SelectedIndex, "#F1");
1782 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#F2");
1783 comboBox.SelectedText = "doesnotexist";
1784 Assert.AreEqual (-1, comboBox.SelectedIndex, "#G1");
1785 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#G2");
1786 comboBox.SelectedIndex = 0;
1787 Assert.AreEqual (0, comboBox.SelectedIndex, "#H1");
1788 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#H2");
1789 comboBox.SelectedText = "Foo";
1790 Assert.AreEqual (-1, comboBox.SelectedIndex, "#I1");
1791 Assert.AreEqual (string.Empty, comboBox.SelectedText, "#I2");
1795 public void SortedSelectedItem ()
1797 using (Form form = new Form ()) {
1798 ComboBox box = new ComboBox ();
1801 form.Controls.Add (box);
1802 form.ShowInTaskbar = false;
1805 box.Items.Add ("English");
1806 box.SelectedItem = "English";
1808 box.Items.Add ("Danish");
1809 Assert.AreEqual ("English", box.SelectedItem, "A1");
1811 box.Items.Add ("French");
1812 Assert.AreEqual ("English", box.SelectedItem, "A2");
1814 box.Items.Add ("Brazilian Portuguese");
1815 box.Items.Add ("Arabic");
1816 Assert.AreEqual ("English", box.SelectedItem, "A3");
1822 private readonly string _name;
1824 public Person (string name)
1837 public override string ToString ()