Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ListBoxTest.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.Collections;
32 using System.ComponentModel;
33 using System.Drawing;
34 using System.Reflection;
35 using System.Windows.Forms;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Windows.Forms
40 {
41         [TestFixture]
42         public class ListBoxTest : TestHelper
43         {
44                 ListBox listBox;
45                 Form form;
46
47                 [SetUp]
48                 protected override void SetUp ()
49                 {
50                         listBox = new ListBox();
51                         form = new Form();
52                         form.ShowInTaskbar = false;
53                         base.SetUp ();
54                 }
55
56                 [TearDown]
57                 protected override void TearDown ()
58                 {
59                         form.Dispose ();
60                         base.TearDown ();
61                 }
62
63                 [Test] // bug #465422
64                 public void RemoveLast ()
65                 {
66                         listBox.Items.Clear ();
67
68                         for (int i = 0; i < 3; i++)
69                                 listBox.Items.Add (i.ToString ());
70
71                         // need to create control to actually test the invalidation
72                         listBox.CreateControl ();
73
74                         // select last - then remove an item that is *not* the last,
75                         // so basically the selection is invalidated implicitly
76                         listBox.SelectedIndex = 2;
77                         listBox.Items.RemoveAt (1);
78                         Assert.AreEqual (1, listBox.SelectedIndex, "#A1");
79
80                         listBox.SelectedIndex = 0;
81                         Assert.AreEqual (0, listBox.SelectedIndex, "#B1");
82
83                         // 
84                         // MultiSelection
85                         //
86                         listBox.ClearSelected ();
87                         listBox.Items.Clear ();
88                         for (int i = 0; i < 3; i++)
89                                 listBox.Items.Add (i.ToString ());
90
91                         listBox.SelectionMode = SelectionMode.MultiSimple;
92
93                         listBox.SetSelected (0, true);
94                         listBox.SetSelected (2, true);
95
96                         Assert.AreEqual (true, listBox.GetSelected (0), "#C1");
97                         Assert.AreEqual (false, listBox.GetSelected (1), "#C2");
98                         Assert.AreEqual (true, listBox.GetSelected (2), "#C3");
99
100                         listBox.Items.RemoveAt (2);
101                         Assert.AreEqual (true, listBox.GetSelected (0), "#D1");
102                         Assert.AreEqual (false, listBox.GetSelected (1), "#D2");
103                         Assert.AreEqual (1, listBox.SelectedIndices.Count, "#D3");
104                 }
105
106                 [Test]
107                 public void ListBoxPropertyTest ()
108                 {
109                         Assert.AreEqual (0, listBox.ColumnWidth, "#1");
110                         Assert.AreEqual (DrawMode.Normal, listBox.DrawMode, "#2");
111                         Assert.AreEqual (0, listBox.HorizontalExtent, "#3");
112                         Assert.AreEqual (false, listBox.HorizontalScrollbar, "#4");
113                         Assert.AreEqual (true, listBox.IntegralHeight, "#5");
114                         //Assert.AreEqual (13, listBox.ItemHeight, "#6"); // Note: Item height depends on the current font.
115                         listBox.Items.Add ("a");
116                         listBox.Items.Add ("b");
117                         listBox.Items.Add ("c");
118                         Assert.AreEqual (3,  listBox.Items.Count, "#7");
119                         Assert.AreEqual (false, listBox.MultiColumn, "#8");
120                         //Assert.AreEqual (46, listBox.PreferredHeight, "#9"); // Note: Item height depends on the current font.
121                         //Assert.AreEqual (RightToLeft.No , listBox.RightToLeft, "#10"); // Depends on Windows version
122                         Assert.AreEqual (false, listBox.ScrollAlwaysVisible, "#11");
123                         Assert.AreEqual (-1, listBox.SelectedIndex, "#12");
124                         listBox.SetSelected (2,true);
125                         Assert.AreEqual (2, listBox.SelectedIndices[0], "#13");
126                         Assert.AreEqual ("c", listBox.SelectedItem, "#14");
127                         Assert.AreEqual ("c", listBox.SelectedItems[0], "#15");
128                         Assert.AreEqual (SelectionMode.One, listBox.SelectionMode, "#16");
129                         listBox.SetSelected (2,false);
130                         Assert.AreEqual (false, listBox.Sorted, "#17");
131                         Assert.AreEqual ("", listBox.Text, "#18");
132                         Assert.AreEqual (0, listBox.TopIndex, "#19");
133                         Assert.AreEqual (true, listBox.UseTabStops, "#20");
134                 }
135
136                 [Test]
137                 public void BeginEndUpdateTest ()
138                 {
139                         form.Visible = true;
140                         listBox.Items.Add ("A");
141                         listBox.Visible = true;
142                         form.Controls.Add (listBox);
143                         listBox.BeginUpdate ();
144                         for (int x = 1; x < 5000; x++)
145                         {
146                                 listBox.Items.Add ("Item " + x.ToString ());
147                         }
148                         listBox.EndUpdate ();
149                         listBox.SetSelected (1, true);
150                         listBox.SetSelected (3, true);
151                         Assert.AreEqual (true, listBox.SelectedItems.Contains ("Item 3"), "#21");
152                 }
153
154                 [Test]
155                 public void ClearSelectedTest ()
156                 {
157                         form.Visible = true;
158                         listBox.Items.Add ("A");
159                         listBox.Visible = true;
160                         form.Controls.Add (listBox);
161                         listBox.SetSelected (0, true);
162                         Assert.AreEqual ("A", listBox.SelectedItems [0].ToString (),"#22");
163                         listBox.ClearSelected ();
164                         Assert.AreEqual (0, listBox.SelectedItems.Count,"#23");
165                 }
166
167                 [Test] // bug #80620
168                 [NUnit.Framework.Category ("NotWorking")]
169                 public void ClientRectangle_Borders ()
170                 {
171                         // This test is invalid because createcontrol forces .net to resize
172                         // the listbox using integralheight, which defaults to true.  This
173                         // will only hold for most font sizes.
174                         listBox.CreateControl ();
175                         Assert.AreEqual (listBox.ClientRectangle, new ListBox ().ClientRectangle);
176                 }
177
178                 [Ignore ("It depends on user system settings")]
179                 public void GetItemHeightTest ()
180                 {
181                         listBox.Visible = true;
182                         form.Controls.Add (listBox);
183                         listBox.Items.Add ("A");
184                         Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
185                 }
186
187                 [Ignore ("It depends on user system settings")]
188                 public void GetItemRectangleTest ()
189                 {
190                         form.Visible = true;
191                         listBox.Visible = true;
192                         form.Controls.Add (listBox);
193                         listBox.Items.Add ("A");
194                         Assert.AreEqual (new Rectangle(0,0,116,13), listBox.GetItemRectangle (0), "#29");
195                 }
196
197                 [Test]
198                 public void GetSelectedTest ()
199                 {
200                         listBox.Items.Add ("A");
201                         listBox.Items.Add ("B");
202                         listBox.Items.Add ("C");
203                         listBox.Items.Add ("D");
204                         listBox.SelectionMode = SelectionMode.MultiSimple;
205                         listBox.Sorted = true;
206                         listBox.SetSelected (0,true);
207                         listBox.SetSelected (2,true);
208                         listBox.TopIndex=0;
209                         Assert.AreEqual (true, listBox.GetSelected (0), "#30");
210                         listBox.SetSelected (2,false);
211                         Assert.AreEqual (false, listBox.GetSelected (2), "#31");
212                 }
213
214                 [Test]
215                 public void IndexFromPointTest ()
216                 {
217                         listBox.Items.Add ("A");
218                         Point pt = new Point (100,100);
219                                 listBox.IndexFromPoint (pt);
220                         Assert.AreEqual (-1, listBox.IndexFromPoint (100,100), "#32");
221                 }
222
223                 [Test]
224                 public void FindStringTest ()
225                 {
226                         listBox.FindString ("Hola", -5); // No exception, it's empty
227                         int x = listBox.FindString ("Hello");
228                         Assert.AreEqual (-1, x, "#19");
229                         listBox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
230                         String myString = "ABC";
231                         x = listBox.FindString (myString);
232                         Assert.AreEqual (3, x, "#191");
233                         x = listBox.FindString (string.Empty);
234                         Assert.AreEqual (0, x, "#192");
235                         x = listBox.FindString ("NonExistant");
236                         Assert.AreEqual (-1, x, "#193");
237
238                         x = listBox.FindString ("A", -1);
239                         Assert.AreEqual (0, x, "#194");
240                         x = listBox.FindString ("A", 0);
241                         Assert.AreEqual (1, x, "#195");
242                         x = listBox.FindString ("A", listBox.Items.Count - 1);
243                         Assert.AreEqual (0, x, "#196");
244                         x = listBox.FindString ("a", listBox.Items.Count - 1);
245                         Assert.AreEqual (0, x, "#197");
246                 }
247
248                 [Test]
249                 public void FindStringExactTest ()
250                 {
251                         listBox.FindStringExact ("Hola", -5); // No exception, it's empty
252                         int x = listBox.FindStringExact ("Hello");
253                         Assert.AreEqual (-1, x, "#20");
254                         listBox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
255                         String myString = "ABC";
256                         x = listBox.FindStringExact (myString);
257                         Assert.AreEqual (1, x, "#201");
258                         x = listBox.FindStringExact (string.Empty);
259                         Assert.AreEqual (-1, x, "#202");
260                         x = listBox.FindStringExact ("NonExistant");
261                         Assert.AreEqual (-1, x, "#203");
262
263                         x = listBox.FindStringExact ("ABCD", -1);
264                         Assert.AreEqual (0, x, "#204");
265                         x = listBox.FindStringExact ("ABC", 0);
266                         Assert.AreEqual (1, x, "#205");
267                         x = listBox.FindStringExact ("ABC", listBox.Items.Count - 1);
268                         Assert.AreEqual (1, x, "#206");
269                         x = listBox.FindStringExact ("abcd", listBox.Items.Count - 1);
270                         Assert.AreEqual (0, x, "#207");
271                 }
272
273 #if NET_2_0
274                 [Test]
275                 public void AllowSelection ()
276                 {
277                         MockListBox lb = new MockListBox ();
278                         lb.SelectionMode = SelectionMode.None;
279                         Assert.IsFalse (lb.allow_selection, "#1");
280                         lb.SelectionMode = SelectionMode.One;
281                         Assert.IsTrue (lb.allow_selection, "#2");
282                 }
283 #endif
284
285                 //
286                 // Exceptions
287                 //
288
289                 [Test]
290                 [ExpectedException (typeof (InvalidEnumArgumentException))]
291                 public void BorderStyleException ()
292                 {
293                         listBox.BorderStyle = (BorderStyle) 10;
294                 }
295
296                 [Test]
297                 [ExpectedException (typeof (ArgumentException))]
298                 public void ColumnWidthException ()
299                 {
300                         listBox.ColumnWidth = -1;
301                 }
302
303                 [Test]
304                 [ExpectedException (typeof (InvalidEnumArgumentException))]
305                 public void DrawModeException ()
306                 {
307                         listBox.DrawMode = (DrawMode) 10;
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (ArgumentException))]
312                 public void DrawModeAndMultiColumnException ()
313                 {
314                         listBox.MultiColumn = true;
315                         listBox.DrawMode = DrawMode.OwnerDrawVariable;
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
320                 public void ItemHeightException ()
321                 {
322                         listBox.ItemHeight = 256;
323                 }
324
325                 [Test] // bug #80696
326                 public void SelectedIndex_Created ()
327                 {
328                         Form form = new Form ();
329                         ListBox listBox = new ListBox ();
330                         listBox.Items.Add ("A");
331                         listBox.Items.Add ("B");
332                         form.Controls.Add (listBox);
333                         form.Show ();
334
335                         Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
336                         listBox.SelectedIndex = 0;
337                         Assert.AreEqual (0, listBox.SelectedIndex, "#2");
338                         listBox.SelectedIndex = -1;
339                         Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
340                         listBox.SelectedIndex = 1;
341                         Assert.AreEqual (1, listBox.SelectedIndex, "#4");
342                         
343                         form.Close ();
344                 }
345
346                 [Test] // bug #80753
347                 public void SelectedIndex_NotCreated ()
348                 {
349                         ListBox listBox = new ListBox ();
350                         listBox.Items.Add ("A");
351                         listBox.Items.Add ("B");
352                         Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
353                         listBox.SelectedIndex = 0;
354                         Assert.AreEqual (0, listBox.SelectedIndex, "#2");
355                         listBox.SelectedIndex = -1;
356                         Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
357                         listBox.SelectedIndex = 1;
358                         Assert.AreEqual (1, listBox.SelectedIndex, "#4");
359                 }
360
361 #if NET_2_0
362                 [Test]
363                 public void SelectedIndex_Removed ()
364                 {
365                         ListBox listBox = new ListBox ();
366                         listBox.Items.Add ("A");
367                         listBox.Items.Add ("B");
368                         listBox.Items.Add ("C");
369
370                         Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
371                         listBox.SelectedIndex = 2;
372                         Assert.AreEqual (2, listBox.SelectedIndex, "#2");
373                         listBox.Items.RemoveAt (2);
374                         Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
375
376                         listBox.SelectedIndex = 0;
377                         Assert.AreEqual (0, listBox.SelectedIndex, "#4");
378                         listBox.Items.RemoveAt (0);
379                         Assert.AreEqual (-1, listBox.SelectedIndex, "#5");
380                 }
381
382                 // This should also apply to MultiSimple selection mode
383                 [Test]
384                 public void Selection_MultiExtended ()
385                 {
386                         listBox.Items.Add ("A");
387                         listBox.Items.Add ("B");
388                         listBox.Items.Add ("C");
389                         listBox.Items.Add ("D");
390                         listBox.SelectionMode = SelectionMode.MultiExtended;
391
392                         //
393                         // First part: test the order of SelectedItems as well
394                         // as SelectedIndex when more than one item is selected
395                         //
396                         listBox.SelectedItems.Add ("D");
397                         listBox.SelectedItems.Add ("B");
398                         Assert.AreEqual (1, listBox.SelectedIndex, "#A1");
399                         Assert.AreEqual (2, listBox.SelectedItems.Count, "#A2");
400                         Assert.AreEqual ("B", listBox.SelectedItems [0], "#A3");
401                         Assert.AreEqual ("D", listBox.SelectedItems [1], "#A4");
402
403                         listBox.SelectedItems.Add ("C");
404                         Assert.AreEqual (1, listBox.SelectedIndex, "#B1");
405                         Assert.AreEqual (3, listBox.SelectedItems.Count, "#B2");
406                         Assert.AreEqual ("B", listBox.SelectedItems [0], "#B3");
407                         Assert.AreEqual ("C", listBox.SelectedItems [1], "#B4");
408                         Assert.AreEqual ("D", listBox.SelectedItems [2], "#B5");
409
410                         listBox.SelectedItems.Add ("A");
411                         Assert.AreEqual (0, listBox.SelectedIndex, "#C1");
412                         Assert.AreEqual (4, listBox.SelectedItems.Count, "#C2");
413                         Assert.AreEqual ("A", listBox.SelectedItems [0], "#C3");
414                         Assert.AreEqual ("B", listBox.SelectedItems [1], "#C4");
415                         Assert.AreEqual ("C", listBox.SelectedItems [2], "#C5");
416                         Assert.AreEqual ("D", listBox.SelectedItems [3], "#C6");
417
418                         // 
419                         // Second part: how does SelectedIndex setter work related
420                         // to SelectedItems
421                         //
422                         listBox.SelectedIndex = -1;
423                         Assert.AreEqual (-1, listBox.SelectedIndex, "#D1");
424                         Assert.AreEqual (0, listBox.SelectedItems.Count, "#D2");
425
426                         listBox.SelectedIndex = 3; // "D"
427                         Assert.AreEqual (3, listBox.SelectedIndex, "#E1");
428                         Assert.AreEqual (1, listBox.SelectedItems.Count, "#E2");
429                         Assert.AreEqual ("D", listBox.SelectedItems [0], "#E3");
430
431                         listBox.SelectedItems.Add ("B"); // index = 1
432                         Assert.AreEqual (1, listBox.SelectedIndex, "#F1");
433                         Assert.AreEqual (2, listBox.SelectedItems.Count, "#E3");
434                         Assert.AreEqual ("B", listBox.SelectedItems [0], "#E4");
435                         Assert.AreEqual ("D", listBox.SelectedItems [1], "#E5");
436
437                         listBox.SelectedIndex = 2;
438                         Assert.AreEqual (1, listBox.SelectedIndex, "#G1");
439                         Assert.AreEqual (3, listBox.SelectedItems.Count, "#G2");
440                         Assert.AreEqual ("B", listBox.SelectedItems [0], "#G3");
441                         Assert.AreEqual ("C", listBox.SelectedItems [1], "#G4");
442                         Assert.AreEqual ("D", listBox.SelectedItems [2], "#G5");
443
444                         listBox.SelectedIndex = 1; // already selected
445                         Assert.AreEqual (1, listBox.SelectedIndex, "#H1");
446                         Assert.AreEqual (3, listBox.SelectedItems.Count, "#H2");
447
448                         // NOTE: It seems that passing -1 does not affect the collection
449                         // in anyway (other wrong values generate an exception, however)
450                         listBox.SelectedIndices.Add (-1);
451                         Assert.AreEqual (3, listBox.SelectedItems.Count, "#J1");
452                 }
453
454                 [Test]
455                 public void Selection_One ()
456                 {
457                         listBox.Items.Add ("A");
458                         listBox.Items.Add ("B");
459                         listBox.Items.Add ("C");
460                         listBox.SelectionMode = SelectionMode.One;
461
462                         listBox.SelectedItems.Add ("B");
463                         Assert.AreEqual (1, listBox.SelectedIndex, "#A1");
464                         Assert.AreEqual (1, listBox.SelectedItems.Count, "#A2");
465                         Assert.AreEqual ("B", listBox.SelectedItems [0], "#A3");
466
467                         listBox.SelectedIndex = 2;
468                         Assert.AreEqual (2, listBox.SelectedIndex, "#B1");
469                         Assert.AreEqual (1, listBox.SelectedItems.Count, "#B2");
470                         Assert.AreEqual ("C", listBox.SelectedItems [0], "#B3");
471
472                         listBox.SelectedItems.Add ("A");
473                         Assert.AreEqual (0, listBox.SelectedIndex, "#C1");
474                         Assert.AreEqual (1, listBox.SelectedItems.Count, "#C2");
475                         Assert.AreEqual ("A", listBox.SelectedItems [0], "#C3");
476                 }
477
478                 [Test]
479                 public void Selection_None ()
480                 {
481                         listBox.Items.Add ("A");
482                         listBox.Items.Add ("B");
483                         listBox.SelectionMode = SelectionMode.None;
484
485                         try {
486                                 listBox.SelectedIndex = 0;
487                                 Assert.Fail ("#A");
488                         } catch (ArgumentException) {
489                         }
490
491                         try {
492                                 listBox.SelectedIndices.Add (0);
493                                 Assert.Fail ("#B");
494                         } catch (InvalidOperationException e) {
495                                 Console.WriteLine (e.Message);
496                         }
497
498                         try {
499                                 listBox.SelectedItems.Add ("A");
500                                 Assert.Fail ("#C");
501                         } catch (ArgumentException) {
502                         }
503                 }
504 #endif
505
506                 [Test]
507                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
508                 public void SelectedIndexException ()
509                 {
510                         listBox.SelectedIndex = -2;
511                 }
512
513                 [Test]
514                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
515                 public void SelectedIndexException2 ()
516                 {
517                         listBox.SelectedIndex = listBox.Items.Count;
518                 }
519
520                 [Test]
521                 [ExpectedException (typeof (ArgumentException))]
522                 public void SelectedIndexModeNoneException ()
523                 {
524                         listBox.SelectionMode = SelectionMode.None;
525                         listBox.SelectedIndex = -1;
526                 }
527
528                 [Test]
529                 [ExpectedException (typeof (InvalidEnumArgumentException))]
530                 public void SelectionModeException ()
531                 {
532                         listBox.SelectionMode = (SelectionMode) 10;
533                 }
534
535                 [Test]
536                 public void SelectedValueNull()
537                 {
538                         listBox.Items.Clear ();
539
540                         listBox.Items.Add ("A");
541                         listBox.Items.Add ("B");
542                         listBox.Items.Add ("C");
543                         listBox.Items.Add ("D");
544
545                         listBox.SelectedIndex = 2;
546                         listBox.SelectedValue = null;
547                         Assert.AreEqual (listBox.SelectedIndex, 2);
548                 }
549
550                 [Test]
551                 public void SelectedValueEmptyString()
552                 {
553                         listBox.Items.Clear ();
554
555                         listBox.Items.Add ("A");
556                         listBox.Items.Add ("B");
557                         listBox.Items.Add ("C");
558                         listBox.Items.Add ("D");
559
560                         listBox.SelectedIndex = 2;
561                         listBox.SelectedValue = null;
562                         Assert.AreEqual (listBox.SelectedIndex, 2);
563                 }
564
565                 [Test]
566                 public void SetItemsCore ()
567                 {
568                         MockListBox l = new MockListBox ();
569                         l.InvokeSetItemsCore (new object [] { "A", "B", "C" });
570                         Assert.AreEqual (3, l.Items.Count, "#1");
571                         Assert.AreEqual ("A", l.Items [0], "#2");
572                         Assert.AreEqual ("B", l.Items [1], "#3");
573                         Assert.AreEqual ("C", l.Items [2], "#4");
574                 }
575
576                 [Test]
577                 public void SetItemsCore_Item_Null ()
578                 {
579                         MockListBox l = new MockListBox ();
580                         try {
581                                 l.InvokeSetItemsCore (new object [] { "A", null, "B" });
582                                 Assert.Fail ("#1");
583                         } catch (ArgumentNullException ex) {
584                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
585                                 Assert.IsNull (ex.InnerException, "#3");
586                                 Assert.IsNotNull (ex.Message, "#4");
587                                 Assert.AreEqual ("item", ex.ParamName, "#5");
588                         }
589
590 #if NET_2_0
591                         Assert.AreEqual (1, l.Items.Count, "#6");
592                         Assert.AreEqual ("A", l.Items [0], "#7");
593 #else
594                         Assert.AreEqual (0, l.Items.Count, "#6");
595 #endif
596                 }
597
598                 [Test]
599                 public void SetItemsCore_Value_Null ()
600                 {
601                         MockListBox l = new MockListBox ();
602                         try {
603                                 l.InvokeSetItemsCore ((IList) null);
604                                 Assert.Fail ("#1");
605                         } catch (ArgumentNullException ex) {
606                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
607                                 Assert.IsNull (ex.InnerException, "#3");
608                                 Assert.IsNotNull (ex.Message, "#4");
609                                 Assert.AreEqual ("items", ex.ParamName, "#5");
610                         }
611                 }
612
613                 [Test]  // Bug #80466
614                 public void ListBoxHeight ()
615                 {
616                         ListBox l = new ListBox ();
617                         
618                         for (int h = 0; h < 100; h++) {
619                                 l.Height = h;
620                                 
621                                 if (l.Height != h)
622                                         Assert.Fail ("Set ListBox height of {0}, got back {1}.  Should be the same.", h, l.Height);
623                         }
624                 }
625
626 #if NET_2_0
627                 [Test]
628                 public void GetScaledBoundsTest ()
629                 {
630                         ScaleListBox c = new ScaleListBox ();
631
632                         Rectangle r = new Rectangle (100, 200, 300, 400);
633
634                         Assert.AreEqual (new Rectangle (200, 100, 596, 50), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A1");
635                         Assert.AreEqual (new Rectangle (200, 100, 300, 96), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A2");
636                         Assert.AreEqual (new Rectangle (100, 200, 596, 50), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A3");
637                         Assert.AreEqual (new Rectangle (100, 200, 300, 50), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A4");
638                         Assert.AreEqual (new Rectangle (200, 200, 300, 96), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A5");
639                         Assert.AreEqual (new Rectangle (100, 200, 300, 96), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A6");
640
641                         Assert.AreEqual (new Rectangle (100, 200, 300, 96), c.PublicGetScaledBounds (r, new SizeF (1f, 1f), BoundsSpecified.All), "A6-2");
642                         Assert.AreEqual (new Rectangle (200, 400, 596, 188), c.PublicGetScaledBounds (r, new SizeF (2f, 2f), BoundsSpecified.All), "A7");
643                         Assert.AreEqual (new Rectangle (300, 600, 892, 280), c.PublicGetScaledBounds (r, new SizeF (3f, 3f), BoundsSpecified.All), "A8");
644                         Assert.AreEqual (new Rectangle (400, 800, 1188, 372), c.PublicGetScaledBounds (r, new SizeF (4f, 4f), BoundsSpecified.All), "A9");
645                         Assert.AreEqual (new Rectangle (50, 100, 152, 50), c.PublicGetScaledBounds (r, new SizeF (.5f, .5f), BoundsSpecified.All), "A10");
646                 }
647
648                 [Test]
649                 [NUnit.Framework.Category ("NotWorking")]
650                 public void MethodScaleControl ()
651                 {
652                         Form f = new Form ();
653                         f.ShowInTaskbar = false;
654
655                         f.Show ();
656
657                         ScaleListBox gb = new ScaleListBox ();
658                         gb.Location = new Point (5, 10);
659                         f.Controls.Add (gb);
660
661                         Assert.AreEqual (new Rectangle (5, 10, 120, 95), gb.Bounds, "A1");
662
663                         gb.PublicScaleControl (new SizeF (2.0f, 2.0f), BoundsSpecified.All);
664                         Assert.AreEqual (new Rectangle (10, 20, 236, 186), gb.Bounds, "A2");
665
666                         gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Location);
667                         Assert.AreEqual (new Rectangle (5, 10, 236, 186), gb.Bounds, "A3");
668
669                         gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Size);
670                         Assert.AreEqual (new Rectangle (5, 10, 120, 95), gb.Bounds, "A4");
671
672                         gb.PublicScaleControl (new SizeF (3.5f, 3.5f), BoundsSpecified.Size);
673                         Assert.AreEqual (new Rectangle (5, 10, 410, 316), gb.Bounds, "A5");
674
675                         gb.PublicScaleControl (new SizeF (2.5f, 2.5f), BoundsSpecified.Size);
676                         Assert.AreEqual (new Rectangle (5, 10, 1019, 797), gb.Bounds, "A6");
677
678                         gb.PublicScaleControl (new SizeF (.2f, .2f), BoundsSpecified.Size);
679                         Assert.AreEqual (new Rectangle (5, 10, 207, 160), gb.Bounds, "A7");
680
681                         f.Dispose ();
682                 }
683
684                 private class ScaleListBox : ListBox
685                 {
686                         public Rectangle PublicGetScaledBounds (Rectangle bounds, SizeF factor, BoundsSpecified specified)
687                         {
688                                 return base.GetScaledBounds (bounds, factor, specified);
689                         }
690
691                         public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
692                         {
693                                 base.ScaleControl (factor, specified);
694                         }
695                 }
696 #endif
697
698                 [Test]
699                 public void MethodIsInputChar ()
700                 {
701                         // Basically, show that this method always returns true
702                         InputCharControl m = new InputCharControl ();
703                         bool result = true;
704
705                         for (int i = 0; i < 256; i++)
706                                 result &= m.PublicIsInputChar ((char)i);
707
708                         Assert.AreEqual (true, result, "I1");
709                 }
710
711                 private class InputCharControl : ListBox
712                 {
713                         public bool PublicIsInputChar (char charCode)
714                         {
715                                 return base.IsInputChar (charCode);
716                         }
717                 }
718
719                 [Test]
720                 public void HeightAndIntegralHeight ()
721                 {
722                         ListBox a = new ListBox();
723                         Size defaultSize = new Size(120, 96);
724                         Assert.AreEqual (defaultSize, a.Size, "A1");
725                         a.CreateControl();
726                         Assert.AreEqual (0, (a.ClientSize.Height % a.ItemHeight), "A2");
727                         a.IntegralHeight = false;
728                         Assert.AreEqual (a.Size, defaultSize, "A3");
729                         a.IntegralHeight = true;
730                         Assert.AreEqual (0, (a.ClientSize.Height % a.ItemHeight), "A4");
731
732                         Size clientSizeI = new Size(200, a.ItemHeight * 5);
733                         Size clientSize = clientSizeI + new Size(0, a.ItemHeight / 2);
734                         Size borderSize = new Size(a.Width - a.ClientSize.Width, a.Height - a.ClientSize.Height);
735                         Size totalSizeI = clientSizeI + borderSize;
736                         Size totalSize = clientSize + borderSize;
737
738                         a = new ListBox();
739                         a.ClientSize = clientSize;
740                         Assert.AreEqual (clientSize, a.ClientSize, "A5");
741                         Assert.AreEqual (totalSize, a.Size, "A6");
742                         a.IntegralHeight = false;
743                         a.IntegralHeight = true;
744                         Assert.AreEqual (clientSize, a.ClientSize, "A7");
745                         a.CreateControl();
746                         Assert.AreEqual (clientSizeI, a.ClientSize, "A8");
747                         Assert.AreEqual (totalSizeI, a.Size, "A9");
748                         a.IntegralHeight = false;
749                         Assert.AreEqual (clientSize, a.ClientSize, "A10");
750                         a.IntegralHeight = true;
751                         Assert.AreEqual (totalSizeI, a.Size, "A11");
752
753                         a = new ListBox();
754                         a.CreateControl();
755                         a.Size = totalSize;
756                         Assert.AreEqual (totalSizeI, a.Size, "A12");
757                         Assert.AreEqual (clientSizeI, a.ClientSize, "A13");
758                         a.IntegralHeight = false;
759                         Assert.AreEqual (totalSize, a.Size, "A14");
760                         Assert.AreEqual (clientSize, a.ClientSize, "A15");
761
762                         a = new ListBox();
763                         a.IntegralHeight = false;
764                         Assert.AreEqual (defaultSize, a.Size, "A16");
765                         a.CreateControl();
766                         Assert.AreEqual (defaultSize, a.Size, "A17");
767
768                         a = new ListBox();
769                         a.ClientSize = clientSize;
770                         a.IntegralHeight = false;
771                         Assert.AreEqual (clientSize, a.ClientSize, "A18");
772                         a.CreateControl();
773                         Assert.AreEqual (clientSize, a.ClientSize, "A19");
774                 }
775
776                 [Test]
777                 public void PropertyTopIndex ()
778                 {
779                         Form f = new Form ();
780                         f.ShowInTaskbar = false;
781                         f.Show ();
782                         
783                         ListBox l = new ListBox ();
784                         l.Height = 100;
785                         f.Controls.Add (l);
786                         
787                         l.Items.AddRange (new string[] { "A", "B", "C"});
788                         
789                         Assert.AreEqual (0, l.TopIndex, "A1");
790                         
791                         l.TopIndex = 2;
792                         Assert.AreEqual (0, l.TopIndex, "A2");
793
794                         l.Items.AddRange (new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" });
795                         Assert.AreEqual (0, l.TopIndex, "A3");
796
797                         l.TopIndex = 2;
798                         Assert.AreEqual (2, l.TopIndex, "A4");
799
800                         // There aren't items enough for 12 to be the top index, but
801                         // the actual value is font height dependent.
802                         l.TopIndex = 12;
803                         Assert.IsTrue (l.TopIndex < 12, "A5");
804                         
805                         f.Close ();
806                         f.Dispose ();
807                 }
808                 
809                 //
810                 // Events
811                 //
812                 //private bool eventFired;
813
814                 //private void GenericHandler (object sender,  EventArgs e)
815                 //{
816                 //        eventFired = true;
817                 //}
818
819                 public class MockListBox : ListBox
820                 {
821 #if NET_2_0
822                         public bool allow_selection {
823                                 get { return base.AllowSelection; }
824                         }
825 #endif
826
827                         public void InvokeSetItemsCore (IList value)
828                         {
829                                 base.SetItemsCore (value);
830                         }
831                 }
832
833                 [Test]
834                 public void AddToSorted ()
835                 {
836                         AddItems (true, CheckedListBoxTest.Items, CheckedListBoxTest.ExpectedAddPositionsSorted);
837                 }
838
839                 [Test]
840                 public void AddToUnsorted ()
841                 {
842                         AddItems (false, CheckedListBoxTest.Items, CheckedListBoxTest.ExpectedAddPositionsUnsorted);
843                 }
844
845                 void AddItems (bool sorted, object[] items, int[] expectedAddPositions)
846                 {
847                         ListBox clb = new ListBox ();
848                         clb.Sorted = sorted;
849                         ArrayList addedAtList = new ArrayList ();
850                         foreach (object cur in items)
851                         {
852                                 int idx = clb.Items.Add (cur);
853                                 addedAtList.Add (idx);
854                         }
855                         Assert.AreEqual ((Array)expectedAddPositions, (Array)addedAtList.ToArray (typeof (int)), "addedAtList");
856                 }
857
858                 [Test]
859                 public void SelectedIndexUpdated () // Xamarin bug 4921
860                 {
861                         using (Form f = new Form ()) {
862                                 f.ShowInTaskbar = false;
863
864                                 ListBox l = new ListBox ();
865                                 l.Sorted = true;
866                                 f.Controls.Add (l);
867
868                                 l.Items.Add ("B");
869                                 l.SelectedIndex = 0;
870
871                                 Assert.AreEqual (0, l.SelectedIndex);
872
873                                 l.Items.Add ("A");
874                                 Assert.AreEqual (1, l.SelectedIndex);
875                         }
876                 }
877
878                 [Test]
879                 public void SelectedIndexUpdated_MultiSelect () // Xamarin bug 4921
880                 {
881                         using (Form f = new Form ()) {
882                                 f.ShowInTaskbar = false;
883
884                                 ListBox l = new ListBox ();
885                                 l.Sorted = true;
886                                 l.SelectionMode = SelectionMode.MultiSimple;
887                                 f.Controls.Add (l);
888
889                                 l.Items.Add ("B");
890                                 l.Items.Add ("C");
891                                 l.SelectedIndex = 0;
892                                 l.SelectedIndex = 1;
893
894                                 Assert.AreEqual (2, l.SelectedIndices.Count);
895                                 Assert.AreEqual (0, l.SelectedIndices [0]);
896                                 Assert.AreEqual (1, l.SelectedIndices [1]);
897                                 Assert.AreEqual (2, l.SelectedItems.Count);
898                                 Assert.AreEqual ("B", l.SelectedItems [0]);
899                                 Assert.AreEqual ("C", l.SelectedItems [1]);
900
901                                 l.Items.Add ("A");
902                                 Assert.AreEqual (2, l.SelectedIndices.Count);
903                                 Assert.AreEqual (1, l.SelectedIndices[0]);
904                                 Assert.AreEqual (2, l.SelectedIndices[1]);
905                                 Assert.AreEqual (2, l.SelectedItems.Count);
906                                 Assert.AreEqual ("B", l.SelectedItems [0]);
907                                 Assert.AreEqual ("C", l.SelectedItems [1]);
908                         }
909                 }
910         }
911
912         [TestFixture]
913         public class ListBoxObjectCollectionTest : TestHelper
914         {
915                 ListBox.ObjectCollection col;
916
917                 [SetUp]
918                 protected override void SetUp ()
919                 {
920                         col = new ListBox.ObjectCollection (new ListBox ());
921                 }
922
923                 [Test]
924                 public void DefaultProperties ()
925                 {
926                         Assert.AreEqual (false, col.IsReadOnly, "#B1");
927                         Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
928                         Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
929                         Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
930                         Assert.AreEqual (0, col.Count);
931                 }
932
933                 [Test]
934                 public void Add ()
935                 {
936                         col.Add ("Item1");
937                         col.Add ("Item2");
938                         Assert.AreEqual (2, col.Count, "#1");
939                         Assert.AreEqual ("Item1", col [0], "#2");
940                         Assert.AreEqual ("Item2", col [1], "#3");
941                 }
942
943                 [Test]
944                 public void Add_Item_Null ()
945                 {
946                         try {
947                                 col.Add (null);
948                                 Assert.Fail ("#1");
949                         } catch (ArgumentNullException ex) {
950                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
951                                 Assert.IsNull (ex.InnerException, "#3");
952                                 Assert.IsNotNull (ex.Message, "#4");
953                                 Assert.AreEqual ("item", ex.ParamName, "#5");
954                         }
955                 }
956
957                 [Test] // AddRange (Object [])
958                 public void AddRange1_Items_Null ()
959                 {
960                         try {
961                                 col.AddRange ((object []) null);
962                                 Assert.Fail ("#1");
963                         } catch (ArgumentNullException ex) {
964                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
965                                 Assert.IsNull (ex.InnerException, "#3");
966                                 Assert.IsNotNull (ex.Message, "#4");
967                                 Assert.AreEqual ("items", ex.ParamName, "#5");
968                         }
969                 }
970
971                 [Test] // AddRange (ListBox.ObjectCollection)
972                 public void AddRange2_Value_Null ()
973                 {
974                         try {
975                                 col.AddRange ((ListBox.ObjectCollection) null);
976                                 Assert.Fail ("#1");
977                         } catch (ArgumentNullException ex) {
978                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
979                                 Assert.IsNull (ex.InnerException, "#3");
980                                 Assert.IsNotNull (ex.Message, "#4");
981                                 Assert.AreEqual ("items", ex.ParamName, "#5");
982                         }
983                 }
984
985                 [Test]
986                 public void Clear ()
987                 {
988                         col.Add ("Item1");
989                         col.Add ("Item2");
990                         col.Clear ();
991                         Assert.AreEqual (0, col.Count, "#D1");
992                 }
993
994                 [Test]
995                 public void Contains ()
996                 {
997                         object obj = "Item1";
998                         col.Add (obj);
999                         Assert.IsTrue (col.Contains ("Item1"), "#1");
1000                         Assert.IsFalse (col.Contains ("Item2"), "#2");
1001                 }
1002
1003                 [Test]
1004                 public void Contains_Value_Null ()
1005                 {
1006                         try {
1007                                 col.Contains (null);
1008                                 Assert.Fail ("#1");
1009                         } catch (ArgumentNullException ex) {
1010                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1011                                 Assert.IsNull (ex.InnerException, "#3");
1012                                 Assert.IsNotNull (ex.Message, "#4");
1013 #if NET_2_0
1014                                 Assert.AreEqual ("value", ex.ParamName, "#5");
1015 #else
1016                                 Assert.IsNotNull (ex.ParamName, "#5");
1017 #endif
1018                         }
1019                 }
1020
1021                 [Test]
1022                 public void Indexer_Value_Null ()
1023                 {
1024                         col.Add ("Item1");
1025                         try {
1026                                 col [0] = null;
1027                                 Assert.Fail ("#1");
1028                         } catch (ArgumentNullException ex) {
1029                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1030                                 Assert.IsNull (ex.InnerException, "#3");
1031                                 Assert.IsNotNull (ex.Message, "#4");
1032                                 Assert.AreEqual ("value", ex.ParamName, "#5");
1033                         }
1034                 }
1035
1036                 [Test]
1037                 public void IndexOf ()
1038                 {
1039                         col.Add ("Item1");
1040                         col.Add ("Item2");
1041                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#1");
1042                         Assert.AreEqual (0, col.IndexOf ("Item1"), "#2");
1043                         Assert.AreEqual (-1, col.IndexOf ("Item3"), "#3");
1044                 }
1045
1046                 [Test]
1047                 public void IndexOf_Value_Null ()
1048                 {
1049                         try {
1050                                 col.IndexOf (null);
1051                                 Assert.Fail ("#1");
1052                         } catch (ArgumentNullException ex) {
1053                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1054                                 Assert.IsNull (ex.InnerException, "#3");
1055                                 Assert.IsNotNull (ex.Message, "#4");
1056 #if NET_2_0
1057                                 Assert.AreEqual ("value", ex.ParamName, "#5");
1058 #else
1059                                 Assert.IsNotNull (ex.ParamName, "#5");
1060 #endif
1061                         }
1062                 }
1063
1064                 [Test]
1065 #if NET_2_0
1066                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363285
1067 #endif
1068                 public void Insert_Item_Null ()
1069                 {
1070                         col.Add ("Item1");
1071                         try {
1072                                 col.Insert (0, null);
1073                                 Assert.Fail ("#1");
1074                         } catch (ArgumentNullException ex) {
1075                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1076                                 Assert.IsNull (ex.InnerException, "#3");
1077                                 Assert.IsNotNull (ex.Message, "#4");
1078                                 Assert.AreEqual ("item", ex.ParamName, "#5");
1079                         }
1080                 }
1081
1082                 [Test]
1083                 public void Remove ()
1084                 {
1085                         col.Add ("Item1");
1086                         col.Add ("Item2");
1087                         col.Remove ("Item1");
1088                         Assert.AreEqual (1, col.Count, "#A1");
1089                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#A2");
1090                         Assert.AreEqual (0, col.IndexOf ("Item2"), "#A3");
1091                         col.Remove (null);
1092                         Assert.AreEqual (1, col.Count, "#B1");
1093                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#B2");
1094                         Assert.AreEqual (0, col.IndexOf ("Item2"), "#B3");
1095                         col.Remove ("Item3");
1096                         Assert.AreEqual (1, col.Count, "#C1");
1097                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#C2");
1098                         Assert.AreEqual (0, col.IndexOf ("Item2"), "#C3");
1099                         col.Remove ("Item2");
1100                         Assert.AreEqual (0, col.Count, "#D1");
1101                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#D2");
1102                         Assert.AreEqual (-1, col.IndexOf ("Item2"), "#D3");
1103                 }
1104
1105                 [Test]
1106                 public void RemoveAt ()
1107                 {
1108                         col.Add ("Item1");
1109                         col.Add ("Item2");
1110                         col.RemoveAt (0);
1111                         Assert.AreEqual (1, col.Count, "#1");
1112                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#2");
1113                         Assert.AreEqual (0, col.IndexOf ("Item2"), "#3");
1114                 }
1115         }
1116
1117 #if NET_2_0
1118         [TestFixture]
1119         public class ListBoxIntegerCollectionTest : TestHelper
1120         {
1121                 ListBox.IntegerCollection col;
1122                 ListBox listBox;
1123
1124                 [SetUp]
1125                 protected override void SetUp ()
1126                 {
1127                         listBox = new ListBox ();
1128                         col = new ListBox.IntegerCollection (listBox);
1129                 }
1130
1131                 [Test]
1132                 public void Add ()
1133                 {
1134                         col.Add (5);
1135                         Assert.AreEqual (1, col.Count, "#1");
1136                         col.Add (7);
1137                         Assert.AreEqual (2, col.Count, "#2");
1138                         col.Add (5);
1139                         Assert.AreEqual (2, col.Count, "#3");
1140                         col.Add (3);
1141                         Assert.AreEqual (3, col.Count, "#4");
1142                 }
1143
1144                 [Test] // AddRange (Int32 [])
1145                 public void AddRange1 ()
1146                 {
1147                         col.Add (5);
1148                         col.Add (3);
1149                         col.AddRange (new int [] { 3, 7, 9, 5, 4 });
1150                         Assert.AreEqual (5, col.Count, "#1");
1151                         Assert.AreEqual (3, col [0], "#2");
1152                         Assert.AreEqual (4, col [1], "#3");
1153                         Assert.AreEqual (5, col [2], "#4");
1154                         Assert.AreEqual (7, col [3], "#5");
1155                         Assert.AreEqual (9, col [4], "#6");
1156                 }
1157
1158                 [Test] // AddRange (Int32 [])
1159                 public void AddRange1_Items_Null ()
1160                 {
1161                         try {
1162                                 col.AddRange ((int []) null);
1163                                 Assert.Fail ("#1");
1164                         } catch (ArgumentNullException ex) {
1165                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1166                                 Assert.IsNull (ex.InnerException, "#3");
1167                                 Assert.IsNotNull (ex.Message, "#4");
1168                                 Assert.AreEqual ("items", ex.ParamName, "#5");
1169                         }
1170                 }
1171
1172                 [Test] // AddRange (ListBox.IntegerCollection)
1173                 public void AddRange2 ()
1174                 {
1175                         ListBox.IntegerCollection ints = new ListBox.IntegerCollection (
1176                                 listBox);
1177                         ints.Add (3);
1178                         ints.Add (1);
1179                         ints.Add (-5);
1180                         ints.Add (4);
1181                         ints.Add (2);
1182
1183                         col.Add (5);
1184                         col.Add (3);
1185                         col.Add (12);
1186                         col.AddRange (ints);
1187
1188                         Assert.AreEqual (7, col.Count, "#1");
1189                         Assert.AreEqual (-5, col [0], "#2");
1190                         Assert.AreEqual (1, col [1], "#3");
1191                         Assert.AreEqual (2, col [2], "#4");
1192                         Assert.AreEqual (3, col [3], "#5");
1193                         Assert.AreEqual (4, col [4], "#6");
1194                         Assert.AreEqual (5, col [5], "#7");
1195                 }
1196
1197                 [Test] // AddRange (ListBox.IntegerCollection)
1198                 public void AddRange2_Items_Null ()
1199                 {
1200                         try {
1201                                 col.AddRange ((ListBox.IntegerCollection) null);
1202                                 Assert.Fail ("#1");
1203                         } catch (ArgumentNullException ex) {
1204                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1205                                 Assert.IsNull (ex.InnerException, "#3");
1206                                 Assert.IsNotNull (ex.Message, "#4");
1207                                 Assert.AreEqual ("items", ex.ParamName, "#5");
1208                         }
1209                 }
1210
1211                 [Test]
1212                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363278
1213                 public void Clear ()
1214                 {
1215                         col.Add (5);
1216                         col.Add (3);
1217                         col.Clear ();
1218
1219                         Assert.AreEqual (0, col.Count, "#1");
1220                         Assert.AreEqual (-1, col.IndexOf (5), "#2");
1221                         Assert.AreEqual (-1, col.IndexOf (3), "#3");
1222                 }
1223
1224                 [Test]
1225                 public void Contains ()
1226                 {
1227                         col.Add (5);
1228                         col.Add (7);
1229                         Assert.IsTrue (col.Contains (5), "#1");
1230                         Assert.IsFalse (col.Contains (3), "#2");
1231                         Assert.IsTrue (col.Contains (7), "#3");
1232                         Assert.IsFalse (col.Contains (-5), "#4");
1233                 }
1234
1235                 [Test]
1236                 public void CopyTo ()
1237                 {
1238                         int [] copy = new int [5] { 9, 4, 6, 2, 8 };
1239                         
1240                         col.Add (3);
1241                         col.Add (7);
1242                         col.Add (5);
1243                         col.CopyTo (copy, 1);
1244
1245                         Assert.AreEqual (9, copy [0], "#1");
1246                         Assert.AreEqual (3, copy [1], "#2");
1247                         Assert.AreEqual (5, copy [2], "#3");
1248                         Assert.AreEqual (7, copy [3], "#4");
1249                         Assert.AreEqual (8, copy [4], "#5");
1250                 }
1251
1252                 [Test]
1253                 public void CopyTo_Destination_Invalid ()
1254                 {
1255                         string [] copy = new string [3] { "A", "B", "C" };
1256
1257                         col.CopyTo (copy, 1);
1258                         col.Add (3);
1259
1260                         try {
1261                                 col.CopyTo (copy, 1);
1262                                 Assert.Fail ("#1");
1263                         } catch (InvalidCastException) {
1264                         }
1265                 }
1266
1267                 [Test]
1268                 public void CopyTo_Destination_Null ()
1269                 {
1270                         col.CopyTo ((Array) null, 1);
1271                         col.Add (3);
1272
1273                         try {
1274                                 col.CopyTo ((Array) null, 1);
1275                                 Assert.Fail ("#1");
1276                         } catch (NullReferenceException) {
1277                         }
1278                 }
1279
1280                 [Test]
1281                 public void CopyTo_Index_Negative ()
1282                 {
1283                         int [] copy = new int [5] { 9, 4, 6, 2, 8 };
1284
1285                         col.CopyTo (copy, -5);
1286                         col.Add (3);
1287
1288                         try {
1289                                 col.CopyTo (copy, -5);
1290                                 Assert.Fail ("#1");
1291                         } catch (IndexOutOfRangeException ex) {
1292                                 // Index was outside the bounds of the array
1293                                 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
1294                                 Assert.IsNull (ex.InnerException, "#3");
1295                                 Assert.IsNotNull (ex.Message, "#4");
1296                         }
1297                 }
1298
1299                 [Test]
1300                 public void Count ()
1301                 {
1302                         Assert.AreEqual (0, col.Count, "#1");
1303                         col.Add (5);
1304                         Assert.AreEqual (1, col.Count, "#2");
1305                         col.Add (7);
1306                         Assert.AreEqual (2, col.Count, "#3");
1307                         col.Remove (7);
1308                         Assert.AreEqual (1, col.Count, "#4");
1309                 }
1310
1311                 [Test]
1312                 public void Indexer ()
1313                 {
1314                         col.Add (5);
1315                         col.Add (7);
1316                         Assert.AreEqual (7, col [1], "#1");
1317                         Assert.AreEqual (5, col [0], "#2");
1318                         col [0] = 3;
1319                         Assert.AreEqual (7, col [1], "#3");
1320                         Assert.AreEqual (3, col [0], "#4");
1321                 }
1322
1323                 [Test]
1324                 public void IndexOf ()
1325                 {
1326                         col.Add (5);
1327                         col.Add (7);
1328                         Assert.AreEqual (0, col.IndexOf (5), "#1");
1329                         Assert.AreEqual (-1, col.IndexOf (3), "#2");
1330                         Assert.AreEqual (1, col.IndexOf (7), "#3");
1331                         Assert.AreEqual (-1, col.IndexOf (-5), "#4");
1332                 }
1333
1334                 [Test]
1335                 public void Remove ()
1336                 {
1337                         col.Add (5);
1338                         col.Add (3);
1339                         col.Remove (5);
1340                         col.Remove (7);
1341
1342                         Assert.AreEqual (1, col.Count, "#1");
1343                         Assert.AreEqual (3, col [0], "#2");
1344                         col.Remove (3);
1345                         Assert.AreEqual (0, col.Count, "#3");
1346                         col.Remove (3);
1347                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363280
1348                         //Assert.AreEqual (0, col.Count, "#4");
1349                 }
1350
1351                 [Test]
1352                 public void RemoveAt ()
1353                 {
1354                         col.Add (5);
1355                         col.Add (3);
1356                         col.Add (7);
1357                         col.RemoveAt (1);
1358                         Assert.AreEqual (2, col.Count, "#A1");
1359                         Assert.AreEqual (3, col [0], "#A2");
1360                         Assert.AreEqual (7, col [1], "#A3");
1361                         col.RemoveAt (0);
1362                         Assert.AreEqual (1, col.Count, "#B1");
1363                         Assert.AreEqual (7, col [0], "#B2");
1364                         col.RemoveAt (0);
1365                         Assert.AreEqual (0, col.Count, "#C1");
1366                         Assert.AreEqual (-1, col.IndexOf (5), "#C2");
1367                         Assert.AreEqual (-1, col.IndexOf (3), "#C3");
1368                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363280
1369                         //Assert.AreEqual (-1, col.IndexOf (7), "#C4");
1370                 }
1371
1372                 [Test]
1373                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
1374                 public void RemoveAt_Index_Negative ()
1375                 {
1376                         col.Add (5);
1377
1378                         try {
1379                                 col.RemoveAt (-1);
1380                                 Assert.Fail ("#1");
1381                         } catch (IndexOutOfRangeException ex) {
1382                                 // Index was outside the bounds of the array
1383                                 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
1384                                 Assert.IsNull (ex.InnerException, "#3");
1385                                 Assert.IsNotNull (ex.Message, "#4");
1386                         }
1387
1388                         Assert.AreEqual (1, col.Count, "#5");
1389                 }
1390
1391                 [Test]
1392                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
1393                 public void RemoveAt_Index_Overflow ()
1394                 {
1395                         col.Add (5);
1396
1397                         try {
1398                                 col.RemoveAt (1);
1399                                 Assert.Fail ("#1");
1400                         } catch (ArgumentOutOfRangeException ex) {
1401                                 // Index was outside the bounds of the array
1402                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
1403                                 Assert.IsNull (ex.InnerException, "#3");
1404                                 Assert.IsNotNull (ex.Message, "#4");
1405                                 Assert.AreEqual ("index", ex.ParamName, "#5");
1406                         }
1407
1408                         Assert.AreEqual (1, col.Count, "#6");
1409                 }
1410
1411                 [Test]
1412                 public void ICollection_IsSynchronized ()
1413                 {
1414                         ICollection collection = (ICollection) col;
1415                         Assert.IsTrue (collection.IsSynchronized);
1416                 }
1417
1418                 [Test]
1419                 public void ICollection_SyncRoot ()
1420                 {
1421                         ICollection collection = (ICollection) col;
1422                         Assert.AreSame (collection, collection.SyncRoot);
1423                 }
1424
1425                 [Test]
1426                 public void IList_Add ()
1427                 {
1428                         IList list = (IList) col;
1429                         list.Add (5);
1430                         Assert.AreEqual (1, list.Count, "#1");
1431                         list.Add (7);
1432                         Assert.AreEqual (2, list.Count, "#2");
1433                         list.Add (5);
1434                         Assert.AreEqual (2, list.Count, "#3");
1435                         list.Add (3);
1436                         Assert.AreEqual (3, list.Count, "#4");
1437                 }
1438
1439                 [Test]
1440                 public void IList_Add_Item_Invalid ()
1441                 {
1442                         IList list = (IList) col;
1443
1444                         try {
1445                                 list.Add (null);
1446                                 Assert.Fail ("#A1");
1447                         } catch (ArgumentException ex) {
1448                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
1449                                 Assert.IsNull (ex.InnerException, "#A3");
1450                                 Assert.AreEqual ("item", ex.Message, "#A4");
1451                                 Assert.IsNull (ex.ParamName, "#A5");
1452                         }
1453
1454                         try {
1455                                 list.Add ("x");
1456                                 Assert.Fail ("#B1");
1457                         } catch (ArgumentException ex) {
1458                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1459                                 Assert.IsNull (ex.InnerException, "#B3");
1460                                 Assert.AreEqual ("item", ex.Message, "#B4");
1461                                 Assert.IsNull (ex.ParamName, "#B5");
1462                         }
1463                 }
1464
1465                 [Test]
1466                 public void IList_Clear ()
1467                 {
1468                         IList list = (IList) col;
1469                         list.Add (5);
1470                         list.Add (7);
1471                         list.Clear ();
1472                         Assert.AreEqual (0, list.Count);
1473                 }
1474
1475                 [Test]
1476                 public void IList_Contains ()
1477                 {
1478                         IList list = (IList) col;
1479                         list.Add (5);
1480                         list.Add (7);
1481                         Assert.IsTrue (list.Contains (5), "#1");
1482                         Assert.IsFalse (list.Contains (3), "#2");
1483                         Assert.IsTrue (list.Contains (7), "#3");
1484                         Assert.IsFalse (list.Contains (null), "#4");
1485                         Assert.IsFalse (list.Contains ("x"), "#5");
1486                 }
1487
1488                 [Test]
1489                 public void IList_Indexer ()
1490                 {
1491                         IList list = (IList) col;
1492                         list.Add (5);
1493                         list.Add (7);
1494                         Assert.AreEqual (7, list [1], "#1");
1495                         Assert.AreEqual (5, list [0], "#2");
1496                         list [0] = 3;
1497                         Assert.AreEqual (7, list [1], "#3");
1498                         Assert.AreEqual (3, list [0], "#4");
1499                 }
1500
1501                 [Test]
1502                 public void IList_IndexOf ()
1503                 {
1504                         IList list = (IList) col;
1505                         list.Add (5);
1506                         list.Add (7);
1507                         Assert.AreEqual (0, list.IndexOf (5), "#1");
1508                         Assert.AreEqual (-1, list.IndexOf (3), "#2");
1509                         Assert.AreEqual (1, list.IndexOf (7), "#3");
1510                         Assert.AreEqual (-1, list.IndexOf (null), "#4");
1511                         Assert.AreEqual (-1, list.IndexOf ("x"), "#5");
1512                 }
1513
1514                 [Test]
1515                 public void IList_Insert ()
1516                 {
1517                         IList list = (IList) col;
1518                         list.Add (5);
1519
1520                         try {
1521                                 list.Insert (0, 7);
1522                                 Assert.Fail ("#A1");
1523                         } catch (NotSupportedException ex) {
1524                                 // ListBox.IntegerCollection is sorted, and
1525                                 // items cannot be inserted into it
1526                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
1527                                 Assert.IsNull (ex.InnerException, "#A3");
1528                                 Assert.IsNotNull (ex.Message, "#A4");
1529                         }
1530
1531                         try {
1532                                 list.Insert (-5, null);
1533                                 Assert.Fail ("#B1");
1534                         } catch (NotSupportedException ex) {
1535                                 // ListBox.IntegerCollection is sorted, and
1536                                 // items cannot be inserted into it
1537                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
1538                                 Assert.IsNull (ex.InnerException, "#B3");
1539                                 Assert.IsNotNull (ex.Message, "#B4");
1540                         }
1541                 }
1542
1543                 [Test]
1544                 public void IList_IsFixedSize ()
1545                 {
1546                         IList list = (IList) col;
1547                         Assert.IsFalse (list.IsFixedSize);
1548                 }
1549
1550                 [Test]
1551                 public void IList_IsReadOnly ()
1552                 {
1553                         IList list = (IList) col;
1554                         Assert.IsFalse (list.IsReadOnly);
1555                 }
1556
1557                 [Test]
1558                 public void IList_Remove ()
1559                 {
1560                         IList list = (IList) col;
1561                         list.Add (5);
1562                         list.Add (3);
1563                         list.Remove (5);
1564                         list.Remove (7);
1565                         list.Remove (int.MinValue);
1566                         list.Remove (int.MaxValue);
1567
1568                         Assert.AreEqual (1, list.Count, "#1");
1569                         Assert.AreEqual (3, list [0], "#2");
1570                         list.Remove (3);
1571                         Assert.AreEqual (0, list.Count, "#3");
1572                 }
1573
1574                 [Test]
1575                 public void IList_Remove_Value_Invalid ()
1576                 {
1577                         IList list = (IList) col;
1578                         list.Add (5);
1579
1580                         try {
1581                                 list.Remove ("x");
1582                                 Assert.Fail ("#A1");
1583                         } catch (ArgumentException ex) {
1584                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
1585                                 Assert.IsNull (ex.InnerException, "#A3");
1586                                 Assert.AreEqual ("value", ex.Message, "#A4");
1587                                 Assert.IsNull (ex.ParamName, "#A5");
1588                         }
1589
1590                         try {
1591                                 list.Remove (null);
1592                                 Assert.Fail ("#B1");
1593                         } catch (ArgumentException ex) {
1594                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1595                                 Assert.IsNull (ex.InnerException, "#B3");
1596                                 Assert.AreEqual ("value", ex.Message, "#B4");
1597                                 Assert.IsNull (ex.ParamName, "#B5");
1598                         }
1599                 }
1600
1601                 [Test]
1602                 public void IList_RemoveAt ()
1603                 {
1604                         IList list = (IList) col;
1605                         list.Add (5);
1606                         list.Add (3);
1607                         list.Add (7);
1608                         list.RemoveAt (1);
1609                         Assert.AreEqual (2, list.Count, "#A1");
1610                         Assert.AreEqual (3, list [0], "#A2");
1611                         Assert.AreEqual (7, list [1], "#A3");
1612                         list.RemoveAt (0);
1613                         Assert.AreEqual (1, list.Count, "#B1");
1614                         Assert.AreEqual (7, list [0], "#B2");
1615                         list.RemoveAt (0);
1616                         Assert.AreEqual (0, list.Count, "#C");
1617                 }
1618
1619                 [Test]
1620                 public void IList_RemoveAt_Index_Negative ()
1621                 {
1622                         IList list = (IList) col;
1623                         list.Add (5);
1624
1625                         try {
1626                                 list.RemoveAt (-1);
1627                                 Assert.Fail ("#1");
1628                         } catch (IndexOutOfRangeException ex) {
1629                                 // Index was outside the bounds of the array
1630                                 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
1631                                 Assert.IsNull (ex.InnerException, "#3");
1632                                 Assert.IsNotNull (ex.Message, "#4");
1633                         }
1634
1635                         // // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
1636                         //Assert.AreEqual (1, list.Count, "#5");
1637                 }
1638
1639                 [Test]
1640                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
1641                 public void IList_RemoveAt_Index_Overflow ()
1642                 {
1643                         IList list = (IList) col;
1644                         list.Add (5);
1645
1646                         try {
1647                                 list.RemoveAt (1);
1648                                 Assert.Fail ("#1");
1649                         } catch (ArgumentOutOfRangeException ex) {
1650                                 // Index was outside the bounds of the array
1651                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
1652                                 Assert.IsNull (ex.InnerException, "#3");
1653                                 Assert.IsNotNull (ex.Message, "#4");
1654                                 Assert.AreEqual ("index", ex.ParamName, "#5");
1655                         }
1656
1657                         Assert.AreEqual (1, list.Count, "#6");
1658                 }
1659         }
1660 #endif
1661 }