New test for ListView.Dispose firing extra Layout events.
[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
859         [TestFixture]
860         public class ListBoxObjectCollectionTest : TestHelper
861         {
862                 ListBox.ObjectCollection col;
863
864                 [SetUp]
865                 protected override void SetUp ()
866                 {
867                         col = new ListBox.ObjectCollection (new ListBox ());
868                 }
869
870                 [Test]
871                 public void DefaultProperties ()
872                 {
873                         Assert.AreEqual (false, col.IsReadOnly, "#B1");
874                         Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
875                         Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
876                         Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
877                         Assert.AreEqual (0, col.Count);
878                 }
879
880                 [Test]
881                 public void Add ()
882                 {
883                         col.Add ("Item1");
884                         col.Add ("Item2");
885                         Assert.AreEqual (2, col.Count, "#1");
886                         Assert.AreEqual ("Item1", col [0], "#2");
887                         Assert.AreEqual ("Item2", col [1], "#3");
888                 }
889
890                 [Test]
891                 public void Add_Item_Null ()
892                 {
893                         try {
894                                 col.Add (null);
895                                 Assert.Fail ("#1");
896                         } catch (ArgumentNullException ex) {
897                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
898                                 Assert.IsNull (ex.InnerException, "#3");
899                                 Assert.IsNotNull (ex.Message, "#4");
900                                 Assert.AreEqual ("item", ex.ParamName, "#5");
901                         }
902                 }
903
904                 [Test] // AddRange (Object [])
905                 public void AddRange1_Items_Null ()
906                 {
907                         try {
908                                 col.AddRange ((object []) null);
909                                 Assert.Fail ("#1");
910                         } catch (ArgumentNullException ex) {
911                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
912                                 Assert.IsNull (ex.InnerException, "#3");
913                                 Assert.IsNotNull (ex.Message, "#4");
914                                 Assert.AreEqual ("items", ex.ParamName, "#5");
915                         }
916                 }
917
918                 [Test] // AddRange (ListBox.ObjectCollection)
919                 public void AddRange2_Value_Null ()
920                 {
921                         try {
922                                 col.AddRange ((ListBox.ObjectCollection) null);
923                                 Assert.Fail ("#1");
924                         } catch (ArgumentNullException ex) {
925                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
926                                 Assert.IsNull (ex.InnerException, "#3");
927                                 Assert.IsNotNull (ex.Message, "#4");
928                                 Assert.AreEqual ("items", ex.ParamName, "#5");
929                         }
930                 }
931
932                 [Test]
933                 public void Clear ()
934                 {
935                         col.Add ("Item1");
936                         col.Add ("Item2");
937                         col.Clear ();
938                         Assert.AreEqual (0, col.Count, "#D1");
939                 }
940
941                 [Test]
942                 public void Contains ()
943                 {
944                         object obj = "Item1";
945                         col.Add (obj);
946                         Assert.IsTrue (col.Contains ("Item1"), "#1");
947                         Assert.IsFalse (col.Contains ("Item2"), "#2");
948                 }
949
950                 [Test]
951                 public void Contains_Value_Null ()
952                 {
953                         try {
954                                 col.Contains (null);
955                                 Assert.Fail ("#1");
956                         } catch (ArgumentNullException ex) {
957                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
958                                 Assert.IsNull (ex.InnerException, "#3");
959                                 Assert.IsNotNull (ex.Message, "#4");
960 #if NET_2_0
961                                 Assert.AreEqual ("value", ex.ParamName, "#5");
962 #else
963                                 Assert.IsNotNull (ex.ParamName, "#5");
964 #endif
965                         }
966                 }
967
968                 [Test]
969                 public void Indexer_Value_Null ()
970                 {
971                         col.Add ("Item1");
972                         try {
973                                 col [0] = null;
974                                 Assert.Fail ("#1");
975                         } catch (ArgumentNullException ex) {
976                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
977                                 Assert.IsNull (ex.InnerException, "#3");
978                                 Assert.IsNotNull (ex.Message, "#4");
979                                 Assert.AreEqual ("value", ex.ParamName, "#5");
980                         }
981                 }
982
983                 [Test]
984                 public void IndexOf ()
985                 {
986                         col.Add ("Item1");
987                         col.Add ("Item2");
988                         Assert.AreEqual (1, col.IndexOf ("Item2"), "#1");
989                         Assert.AreEqual (0, col.IndexOf ("Item1"), "#2");
990                         Assert.AreEqual (-1, col.IndexOf ("Item3"), "#3");
991                 }
992
993                 [Test]
994                 public void IndexOf_Value_Null ()
995                 {
996                         try {
997                                 col.IndexOf (null);
998                                 Assert.Fail ("#1");
999                         } catch (ArgumentNullException ex) {
1000                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1001                                 Assert.IsNull (ex.InnerException, "#3");
1002                                 Assert.IsNotNull (ex.Message, "#4");
1003 #if NET_2_0
1004                                 Assert.AreEqual ("value", ex.ParamName, "#5");
1005 #else
1006                                 Assert.IsNotNull (ex.ParamName, "#5");
1007 #endif
1008                         }
1009                 }
1010
1011                 [Test]
1012 #if NET_2_0
1013                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363285
1014 #endif
1015                 public void Insert_Item_Null ()
1016                 {
1017                         col.Add ("Item1");
1018                         try {
1019                                 col.Insert (0, null);
1020                                 Assert.Fail ("#1");
1021                         } catch (ArgumentNullException ex) {
1022                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1023                                 Assert.IsNull (ex.InnerException, "#3");
1024                                 Assert.IsNotNull (ex.Message, "#4");
1025                                 Assert.AreEqual ("item", ex.ParamName, "#5");
1026                         }
1027                 }
1028
1029                 [Test]
1030                 public void Remove ()
1031                 {
1032                         col.Add ("Item1");
1033                         col.Add ("Item2");
1034                         col.Remove ("Item1");
1035                         Assert.AreEqual (1, col.Count, "#A1");
1036                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#A2");
1037                         Assert.AreEqual (0, col.IndexOf ("Item2"), "#A3");
1038                         col.Remove (null);
1039                         Assert.AreEqual (1, col.Count, "#B1");
1040                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#B2");
1041                         Assert.AreEqual (0, col.IndexOf ("Item2"), "#B3");
1042                         col.Remove ("Item3");
1043                         Assert.AreEqual (1, col.Count, "#C1");
1044                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#C2");
1045                         Assert.AreEqual (0, col.IndexOf ("Item2"), "#C3");
1046                         col.Remove ("Item2");
1047                         Assert.AreEqual (0, col.Count, "#D1");
1048                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#D2");
1049                         Assert.AreEqual (-1, col.IndexOf ("Item2"), "#D3");
1050                 }
1051
1052                 [Test]
1053                 public void RemoveAt ()
1054                 {
1055                         col.Add ("Item1");
1056                         col.Add ("Item2");
1057                         col.RemoveAt (0);
1058                         Assert.AreEqual (1, col.Count, "#1");
1059                         Assert.AreEqual (-1, col.IndexOf ("Item1"), "#2");
1060                         Assert.AreEqual (0, col.IndexOf ("Item2"), "#3");
1061                 }
1062         }
1063
1064 #if NET_2_0
1065         [TestFixture]
1066         public class ListBoxIntegerCollectionTest : TestHelper
1067         {
1068                 ListBox.IntegerCollection col;
1069                 ListBox listBox;
1070
1071                 [SetUp]
1072                 protected override void SetUp ()
1073                 {
1074                         listBox = new ListBox ();
1075                         col = new ListBox.IntegerCollection (listBox);
1076                 }
1077
1078                 [Test]
1079                 public void Add ()
1080                 {
1081                         col.Add (5);
1082                         Assert.AreEqual (1, col.Count, "#1");
1083                         col.Add (7);
1084                         Assert.AreEqual (2, col.Count, "#2");
1085                         col.Add (5);
1086                         Assert.AreEqual (2, col.Count, "#3");
1087                         col.Add (3);
1088                         Assert.AreEqual (3, col.Count, "#4");
1089                 }
1090
1091                 [Test] // AddRange (Int32 [])
1092                 public void AddRange1 ()
1093                 {
1094                         col.Add (5);
1095                         col.Add (3);
1096                         col.AddRange (new int [] { 3, 7, 9, 5, 4 });
1097                         Assert.AreEqual (5, col.Count, "#1");
1098                         Assert.AreEqual (3, col [0], "#2");
1099                         Assert.AreEqual (4, col [1], "#3");
1100                         Assert.AreEqual (5, col [2], "#4");
1101                         Assert.AreEqual (7, col [3], "#5");
1102                         Assert.AreEqual (9, col [4], "#6");
1103                 }
1104
1105                 [Test] // AddRange (Int32 [])
1106                 public void AddRange1_Items_Null ()
1107                 {
1108                         try {
1109                                 col.AddRange ((int []) null);
1110                                 Assert.Fail ("#1");
1111                         } catch (ArgumentNullException ex) {
1112                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1113                                 Assert.IsNull (ex.InnerException, "#3");
1114                                 Assert.IsNotNull (ex.Message, "#4");
1115                                 Assert.AreEqual ("items", ex.ParamName, "#5");
1116                         }
1117                 }
1118
1119                 [Test] // AddRange (ListBox.IntegerCollection)
1120                 public void AddRange2 ()
1121                 {
1122                         ListBox.IntegerCollection ints = new ListBox.IntegerCollection (
1123                                 listBox);
1124                         ints.Add (3);
1125                         ints.Add (1);
1126                         ints.Add (-5);
1127                         ints.Add (4);
1128                         ints.Add (2);
1129
1130                         col.Add (5);
1131                         col.Add (3);
1132                         col.Add (12);
1133                         col.AddRange (ints);
1134
1135                         Assert.AreEqual (7, col.Count, "#1");
1136                         Assert.AreEqual (-5, col [0], "#2");
1137                         Assert.AreEqual (1, col [1], "#3");
1138                         Assert.AreEqual (2, col [2], "#4");
1139                         Assert.AreEqual (3, col [3], "#5");
1140                         Assert.AreEqual (4, col [4], "#6");
1141                         Assert.AreEqual (5, col [5], "#7");
1142                 }
1143
1144                 [Test] // AddRange (ListBox.IntegerCollection)
1145                 public void AddRange2_Items_Null ()
1146                 {
1147                         try {
1148                                 col.AddRange ((ListBox.IntegerCollection) null);
1149                                 Assert.Fail ("#1");
1150                         } catch (ArgumentNullException ex) {
1151                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1152                                 Assert.IsNull (ex.InnerException, "#3");
1153                                 Assert.IsNotNull (ex.Message, "#4");
1154                                 Assert.AreEqual ("items", ex.ParamName, "#5");
1155                         }
1156                 }
1157
1158                 [Test]
1159                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363278
1160                 public void Clear ()
1161                 {
1162                         col.Add (5);
1163                         col.Add (3);
1164                         col.Clear ();
1165
1166                         Assert.AreEqual (0, col.Count, "#1");
1167                         Assert.AreEqual (-1, col.IndexOf (5), "#2");
1168                         Assert.AreEqual (-1, col.IndexOf (3), "#3");
1169                 }
1170
1171                 [Test]
1172                 public void Contains ()
1173                 {
1174                         col.Add (5);
1175                         col.Add (7);
1176                         Assert.IsTrue (col.Contains (5), "#1");
1177                         Assert.IsFalse (col.Contains (3), "#2");
1178                         Assert.IsTrue (col.Contains (7), "#3");
1179                         Assert.IsFalse (col.Contains (-5), "#4");
1180                 }
1181
1182                 [Test]
1183                 public void CopyTo ()
1184                 {
1185                         int [] copy = new int [5] { 9, 4, 6, 2, 8 };
1186                         
1187                         col.Add (3);
1188                         col.Add (7);
1189                         col.Add (5);
1190                         col.CopyTo (copy, 1);
1191
1192                         Assert.AreEqual (9, copy [0], "#1");
1193                         Assert.AreEqual (3, copy [1], "#2");
1194                         Assert.AreEqual (5, copy [2], "#3");
1195                         Assert.AreEqual (7, copy [3], "#4");
1196                         Assert.AreEqual (8, copy [4], "#5");
1197                 }
1198
1199                 [Test]
1200                 public void CopyTo_Destination_Invalid ()
1201                 {
1202                         string [] copy = new string [3] { "A", "B", "C" };
1203
1204                         col.CopyTo (copy, 1);
1205                         col.Add (3);
1206
1207                         try {
1208                                 col.CopyTo (copy, 1);
1209                                 Assert.Fail ("#1");
1210                         } catch (InvalidCastException) {
1211                         }
1212                 }
1213
1214                 [Test]
1215                 public void CopyTo_Destination_Null ()
1216                 {
1217                         col.CopyTo ((Array) null, 1);
1218                         col.Add (3);
1219
1220                         try {
1221                                 col.CopyTo ((Array) null, 1);
1222                                 Assert.Fail ("#1");
1223                         } catch (NullReferenceException) {
1224                         }
1225                 }
1226
1227                 [Test]
1228                 public void CopyTo_Index_Negative ()
1229                 {
1230                         int [] copy = new int [5] { 9, 4, 6, 2, 8 };
1231
1232                         col.CopyTo (copy, -5);
1233                         col.Add (3);
1234
1235                         try {
1236                                 col.CopyTo (copy, -5);
1237                                 Assert.Fail ("#1");
1238                         } catch (IndexOutOfRangeException ex) {
1239                                 // Index was outside the bounds of the array
1240                                 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
1241                                 Assert.IsNull (ex.InnerException, "#3");
1242                                 Assert.IsNotNull (ex.Message, "#4");
1243                         }
1244                 }
1245
1246                 [Test]
1247                 public void Count ()
1248                 {
1249                         Assert.AreEqual (0, col.Count, "#1");
1250                         col.Add (5);
1251                         Assert.AreEqual (1, col.Count, "#2");
1252                         col.Add (7);
1253                         Assert.AreEqual (2, col.Count, "#3");
1254                         col.Remove (7);
1255                         Assert.AreEqual (1, col.Count, "#4");
1256                 }
1257
1258                 [Test]
1259                 public void Indexer ()
1260                 {
1261                         col.Add (5);
1262                         col.Add (7);
1263                         Assert.AreEqual (7, col [1], "#1");
1264                         Assert.AreEqual (5, col [0], "#2");
1265                         col [0] = 3;
1266                         Assert.AreEqual (7, col [1], "#3");
1267                         Assert.AreEqual (3, col [0], "#4");
1268                 }
1269
1270                 [Test]
1271                 public void IndexOf ()
1272                 {
1273                         col.Add (5);
1274                         col.Add (7);
1275                         Assert.AreEqual (0, col.IndexOf (5), "#1");
1276                         Assert.AreEqual (-1, col.IndexOf (3), "#2");
1277                         Assert.AreEqual (1, col.IndexOf (7), "#3");
1278                         Assert.AreEqual (-1, col.IndexOf (-5), "#4");
1279                 }
1280
1281                 [Test]
1282                 public void Remove ()
1283                 {
1284                         col.Add (5);
1285                         col.Add (3);
1286                         col.Remove (5);
1287                         col.Remove (7);
1288
1289                         Assert.AreEqual (1, col.Count, "#1");
1290                         Assert.AreEqual (3, col [0], "#2");
1291                         col.Remove (3);
1292                         Assert.AreEqual (0, col.Count, "#3");
1293                         col.Remove (3);
1294                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363280
1295                         //Assert.AreEqual (0, col.Count, "#4");
1296                 }
1297
1298                 [Test]
1299                 public void RemoveAt ()
1300                 {
1301                         col.Add (5);
1302                         col.Add (3);
1303                         col.Add (7);
1304                         col.RemoveAt (1);
1305                         Assert.AreEqual (2, col.Count, "#A1");
1306                         Assert.AreEqual (3, col [0], "#A2");
1307                         Assert.AreEqual (7, col [1], "#A3");
1308                         col.RemoveAt (0);
1309                         Assert.AreEqual (1, col.Count, "#B1");
1310                         Assert.AreEqual (7, col [0], "#B2");
1311                         col.RemoveAt (0);
1312                         Assert.AreEqual (0, col.Count, "#C1");
1313                         Assert.AreEqual (-1, col.IndexOf (5), "#C2");
1314                         Assert.AreEqual (-1, col.IndexOf (3), "#C3");
1315                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363280
1316                         //Assert.AreEqual (-1, col.IndexOf (7), "#C4");
1317                 }
1318
1319                 [Test]
1320                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
1321                 public void RemoveAt_Index_Negative ()
1322                 {
1323                         col.Add (5);
1324
1325                         try {
1326                                 col.RemoveAt (-1);
1327                                 Assert.Fail ("#1");
1328                         } catch (IndexOutOfRangeException ex) {
1329                                 // Index was outside the bounds of the array
1330                                 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
1331                                 Assert.IsNull (ex.InnerException, "#3");
1332                                 Assert.IsNotNull (ex.Message, "#4");
1333                         }
1334
1335                         Assert.AreEqual (1, col.Count, "#5");
1336                 }
1337
1338                 [Test]
1339                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
1340                 public void RemoveAt_Index_Overflow ()
1341                 {
1342                         col.Add (5);
1343
1344                         try {
1345                                 col.RemoveAt (1);
1346                                 Assert.Fail ("#1");
1347                         } catch (ArgumentOutOfRangeException ex) {
1348                                 // Index was outside the bounds of the array
1349                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
1350                                 Assert.IsNull (ex.InnerException, "#3");
1351                                 Assert.IsNotNull (ex.Message, "#4");
1352                                 Assert.AreEqual ("index", ex.ParamName, "#5");
1353                         }
1354
1355                         Assert.AreEqual (1, col.Count, "#6");
1356                 }
1357
1358                 [Test]
1359                 public void ICollection_IsSynchronized ()
1360                 {
1361                         ICollection collection = (ICollection) col;
1362                         Assert.IsTrue (collection.IsSynchronized);
1363                 }
1364
1365                 [Test]
1366                 public void ICollection_SyncRoot ()
1367                 {
1368                         ICollection collection = (ICollection) col;
1369                         Assert.AreSame (collection, collection.SyncRoot);
1370                 }
1371
1372                 [Test]
1373                 public void IList_Add ()
1374                 {
1375                         IList list = (IList) col;
1376                         list.Add (5);
1377                         Assert.AreEqual (1, list.Count, "#1");
1378                         list.Add (7);
1379                         Assert.AreEqual (2, list.Count, "#2");
1380                         list.Add (5);
1381                         Assert.AreEqual (2, list.Count, "#3");
1382                         list.Add (3);
1383                         Assert.AreEqual (3, list.Count, "#4");
1384                 }
1385
1386                 [Test]
1387                 public void IList_Add_Item_Invalid ()
1388                 {
1389                         IList list = (IList) col;
1390
1391                         try {
1392                                 list.Add (null);
1393                                 Assert.Fail ("#A1");
1394                         } catch (ArgumentException ex) {
1395                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
1396                                 Assert.IsNull (ex.InnerException, "#A3");
1397                                 Assert.AreEqual ("item", ex.Message, "#A4");
1398                                 Assert.IsNull (ex.ParamName, "#A5");
1399                         }
1400
1401                         try {
1402                                 list.Add ("x");
1403                                 Assert.Fail ("#B1");
1404                         } catch (ArgumentException ex) {
1405                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1406                                 Assert.IsNull (ex.InnerException, "#B3");
1407                                 Assert.AreEqual ("item", ex.Message, "#B4");
1408                                 Assert.IsNull (ex.ParamName, "#B5");
1409                         }
1410                 }
1411
1412                 [Test]
1413                 public void IList_Clear ()
1414                 {
1415                         IList list = (IList) col;
1416                         list.Add (5);
1417                         list.Add (7);
1418                         list.Clear ();
1419                         Assert.AreEqual (0, list.Count);
1420                 }
1421
1422                 [Test]
1423                 public void IList_Contains ()
1424                 {
1425                         IList list = (IList) col;
1426                         list.Add (5);
1427                         list.Add (7);
1428                         Assert.IsTrue (list.Contains (5), "#1");
1429                         Assert.IsFalse (list.Contains (3), "#2");
1430                         Assert.IsTrue (list.Contains (7), "#3");
1431                         Assert.IsFalse (list.Contains (null), "#4");
1432                         Assert.IsFalse (list.Contains ("x"), "#5");
1433                 }
1434
1435                 [Test]
1436                 public void IList_Indexer ()
1437                 {
1438                         IList list = (IList) col;
1439                         list.Add (5);
1440                         list.Add (7);
1441                         Assert.AreEqual (7, list [1], "#1");
1442                         Assert.AreEqual (5, list [0], "#2");
1443                         list [0] = 3;
1444                         Assert.AreEqual (7, list [1], "#3");
1445                         Assert.AreEqual (3, list [0], "#4");
1446                 }
1447
1448                 [Test]
1449                 public void IList_IndexOf ()
1450                 {
1451                         IList list = (IList) col;
1452                         list.Add (5);
1453                         list.Add (7);
1454                         Assert.AreEqual (0, list.IndexOf (5), "#1");
1455                         Assert.AreEqual (-1, list.IndexOf (3), "#2");
1456                         Assert.AreEqual (1, list.IndexOf (7), "#3");
1457                         Assert.AreEqual (-1, list.IndexOf (null), "#4");
1458                         Assert.AreEqual (-1, list.IndexOf ("x"), "#5");
1459                 }
1460
1461                 [Test]
1462                 public void IList_Insert ()
1463                 {
1464                         IList list = (IList) col;
1465                         list.Add (5);
1466
1467                         try {
1468                                 list.Insert (0, 7);
1469                                 Assert.Fail ("#A1");
1470                         } catch (NotSupportedException ex) {
1471                                 // ListBox.IntegerCollection is sorted, and
1472                                 // items cannot be inserted into it
1473                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
1474                                 Assert.IsNull (ex.InnerException, "#A3");
1475                                 Assert.IsNotNull (ex.Message, "#A4");
1476                         }
1477
1478                         try {
1479                                 list.Insert (-5, null);
1480                                 Assert.Fail ("#B1");
1481                         } catch (NotSupportedException ex) {
1482                                 // ListBox.IntegerCollection is sorted, and
1483                                 // items cannot be inserted into it
1484                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
1485                                 Assert.IsNull (ex.InnerException, "#B3");
1486                                 Assert.IsNotNull (ex.Message, "#B4");
1487                         }
1488                 }
1489
1490                 [Test]
1491                 public void IList_IsFixedSize ()
1492                 {
1493                         IList list = (IList) col;
1494                         Assert.IsFalse (list.IsFixedSize);
1495                 }
1496
1497                 [Test]
1498                 public void IList_IsReadOnly ()
1499                 {
1500                         IList list = (IList) col;
1501                         Assert.IsFalse (list.IsReadOnly);
1502                 }
1503
1504                 [Test]
1505                 public void IList_Remove ()
1506                 {
1507                         IList list = (IList) col;
1508                         list.Add (5);
1509                         list.Add (3);
1510                         list.Remove (5);
1511                         list.Remove (7);
1512                         list.Remove (int.MinValue);
1513                         list.Remove (int.MaxValue);
1514
1515                         Assert.AreEqual (1, list.Count, "#1");
1516                         Assert.AreEqual (3, list [0], "#2");
1517                         list.Remove (3);
1518                         Assert.AreEqual (0, list.Count, "#3");
1519                 }
1520
1521                 [Test]
1522                 public void IList_Remove_Value_Invalid ()
1523                 {
1524                         IList list = (IList) col;
1525                         list.Add (5);
1526
1527                         try {
1528                                 list.Remove ("x");
1529                                 Assert.Fail ("#A1");
1530                         } catch (ArgumentException ex) {
1531                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
1532                                 Assert.IsNull (ex.InnerException, "#A3");
1533                                 Assert.AreEqual ("value", ex.Message, "#A4");
1534                                 Assert.IsNull (ex.ParamName, "#A5");
1535                         }
1536
1537                         try {
1538                                 list.Remove (null);
1539                                 Assert.Fail ("#B1");
1540                         } catch (ArgumentException ex) {
1541                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1542                                 Assert.IsNull (ex.InnerException, "#B3");
1543                                 Assert.AreEqual ("value", ex.Message, "#B4");
1544                                 Assert.IsNull (ex.ParamName, "#B5");
1545                         }
1546                 }
1547
1548                 [Test]
1549                 public void IList_RemoveAt ()
1550                 {
1551                         IList list = (IList) col;
1552                         list.Add (5);
1553                         list.Add (3);
1554                         list.Add (7);
1555                         list.RemoveAt (1);
1556                         Assert.AreEqual (2, list.Count, "#A1");
1557                         Assert.AreEqual (3, list [0], "#A2");
1558                         Assert.AreEqual (7, list [1], "#A3");
1559                         list.RemoveAt (0);
1560                         Assert.AreEqual (1, list.Count, "#B1");
1561                         Assert.AreEqual (7, list [0], "#B2");
1562                         list.RemoveAt (0);
1563                         Assert.AreEqual (0, list.Count, "#C");
1564                 }
1565
1566                 [Test]
1567                 public void IList_RemoveAt_Index_Negative ()
1568                 {
1569                         IList list = (IList) col;
1570                         list.Add (5);
1571
1572                         try {
1573                                 list.RemoveAt (-1);
1574                                 Assert.Fail ("#1");
1575                         } catch (IndexOutOfRangeException ex) {
1576                                 // Index was outside the bounds of the array
1577                                 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
1578                                 Assert.IsNull (ex.InnerException, "#3");
1579                                 Assert.IsNotNull (ex.Message, "#4");
1580                         }
1581
1582                         // // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
1583                         //Assert.AreEqual (1, list.Count, "#5");
1584                 }
1585
1586                 [Test]
1587                 [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
1588                 public void IList_RemoveAt_Index_Overflow ()
1589                 {
1590                         IList list = (IList) col;
1591                         list.Add (5);
1592
1593                         try {
1594                                 list.RemoveAt (1);
1595                                 Assert.Fail ("#1");
1596                         } catch (ArgumentOutOfRangeException ex) {
1597                                 // Index was outside the bounds of the array
1598                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
1599                                 Assert.IsNull (ex.InnerException, "#3");
1600                                 Assert.IsNotNull (ex.Message, "#4");
1601                                 Assert.AreEqual ("index", ex.ParamName, "#5");
1602                         }
1603
1604                         Assert.AreEqual (1, list.Count, "#6");
1605                 }
1606         }
1607 #endif
1608 }