context: //
  // ListViewTest.cs: Test cases for ListView.
  //
  // Author:
  //   Ritvik Mayank (mritvik@novell.com)
  //
  // (C) 2005 Novell, Inc. (http://www.novell.com)
  //
  
  using System;
  using System.Collections;
  using System.Drawing;
  using System.Reflection;
  using System.Windows.Forms;
  
  using NUnit.Framework;
  
  namespace MonoTests.System.Windows.Forms
  {
  	[TestFixture]
  	public class ListViewTest : TestHelper
  	{
  		[Test]
  		public void ListViewPropertyTest ()
  		{
  			ListView mylistview = new ListView ();
  			Assert.AreEqual (ItemActivation.Standard, mylistview.Activation, "#1");
  			Assert.AreEqual (ListViewAlignment.Top, mylistview.Alignment, "#2");
  			Assert.AreEqual (false, mylistview.AllowColumnReorder, "#3");
  			Assert.AreEqual (true, mylistview.AutoArrange, "#4");
  			Assert.AreEqual (BorderStyle.Fixed3D , mylistview.BorderStyle, "#5");
  			Assert.AreEqual (false, mylistview.CheckBoxes, "#6");
  			Assert.AreEqual (0, mylistview.CheckedIndices.Count, "#7");
  			Assert.AreEqual (0, mylistview.CheckedItems.Count, "#8");
  			Assert.AreEqual (0, mylistview.Columns.Count, "#9");
  			Assert.AreEqual (null, mylistview.FocusedItem, "#10");
  			Assert.AreEqual (false, mylistview.FullRowSelect, "#11");
  			Assert.AreEqual (false, mylistview.GridLines, "#12");
  			Assert.AreEqual (ColumnHeaderStyle.Clickable, mylistview.HeaderStyle, "#13");
  			Assert.AreEqual (true, mylistview.HideSelection, "#14");
  			Assert.AreEqual (false, mylistview.HoverSelection, "#15");
  			ListViewItem item1 = new ListViewItem ("A", -1);
  			mylistview.Items.Add (item1);
  			Assert.AreEqual (1, mylistview.Items.Count, "#16");
  			Assert.AreEqual (false, mylistview.LabelEdit, "#17");
  			Assert.AreEqual (true, mylistview.LabelWrap, "#18");
  			Assert.AreEqual (null, mylistview.LargeImageList, "#19");
  			Assert.AreEqual (null, mylistview.ListViewItemSorter, "#20");
  			Assert.AreEqual (true, mylistview.MultiSelect, "#21");
  			Assert.AreEqual (true, mylistview.Scrollable, "#22");
  			Assert.AreEqual (0, mylistview.SelectedIndices.Count, "#23");
  			Assert.AreEqual (0, mylistview.SelectedItems.Count, "#24");
  			Assert.AreEqual (null, mylistview.SmallImageList, "#25");
  			Assert.AreEqual (null, mylistview.LargeImageList, "#26");
  			Assert.AreEqual (SortOrder.None, mylistview.Sorting, "#27");
  			Assert.AreEqual (null, mylistview.StateImageList, "#28");
  			Assert.AreEqual (View.LargeIcon, mylistview.View, "#29");
  			mylistview.View = View.List;
  			Assert.AreEqual (false, mylistview.TopItem.Checked, "#30");
  			Assert.AreEqual (false, mylistview.ShowItemToolTips, "#31");
  			Assert.AreEqual (false, mylistview.HotTracking, "#31");
  		}
  
  		[Test]
  		public void ArrangeIconsTest ()
  		{
  			Form myform = new Form ();
  			myform.ShowInTaskbar = false;
  			ListView mylistview = new ListView ();
  			myform.Controls.Add (mylistview);
  			mylistview.Items.Add ("Item 1");
  			mylistview.Items.Add ("Item 2");
  			mylistview.View = View.LargeIcon;
  			mylistview.ArrangeIcons ();
  			myform.Dispose ();
  		}
  
  		// Hey
  		[Test]
  		public void BeginEndUpdateTest ()
  		{
  			Form myform = new Form ();
  			myform.ShowInTaskbar = false;
  			myform.Visible = true;
  			ListView mylistview = new ListView();
  			mylistview.Items.Add ("A");
  			mylistview.Visible = true;
  			myform.Controls.Add (mylistview);
  			mylistview.BeginUpdate ();
  			for(int x = 1 ; x < 5000 ; x++){
  				mylistview.Items.Add ("Item " + x.ToString());   
  			}
  			mylistview.EndUpdate ();
  			myform.Dispose ();
  		}	
  
  		[Test]
  		public void CheckBoxes ()
  		{
  			Form form = new Form ();
  			form.ShowInTaskbar = false;
  			ListView lvw = new ListView ();
  			form.Controls.Add (lvw);
  			lvw.Items.Add ("A");
  			ListViewItem itemB = lvw.Items.Add ("B");
  			lvw.Items.Add ("C");
  			itemB.Checked = true;
  
  			Assert.AreEqual (0, lvw.CheckedItems.Count, "#A1");
  			Assert.AreEqual (0, lvw.CheckedIndices.Count, "#A2");
  
  			lvw.CheckBoxes = true;
  
  			Assert.AreEqual (1, lvw.CheckedItems.Count, "#B1");
  			Assert.AreSame (itemB, lvw.CheckedItems [0], "#B2");
  			Assert.AreEqual (1, lvw.CheckedIndices.Count, "#B3");
  			Assert.AreEqual (1, lvw.CheckedIndices [0], "#B4");
  
  			form.Show ();
  
  			Assert.AreEqual (1, lvw.CheckedItems.Count, "#C1");
  			Assert.AreSame (itemB, lvw.CheckedItems [0], "#C2");
  			Assert.AreEqual (1, lvw.CheckedIndices.Count, "#C3");
  			Assert.AreEqual (1, lvw.CheckedIndices [0], "#C4");
  
  			lvw.CheckBoxes = false;
  
  			Assert.AreEqual (0, lvw.CheckedItems.Count, "#D1");
  			Assert.AreEqual (0, lvw.CheckedIndices.Count, "#D2");
  
  			lvw.CheckBoxes = true;
  
  			Assert.AreEqual (1, lvw.CheckedItems.Count, "#E1");
  			Assert.AreSame (itemB, lvw.CheckedItems [0], "#E2");
  			Assert.AreEqual (1, lvw.CheckedIndices.Count, "#E3");
  			Assert.AreEqual (1, lvw.CheckedIndices [0], "#E4");
  			form.Dispose ();
  		}
  
  		[Test]
  		public void ClearTest ()
  		{
  			Form myform = new Form ();
  			myform.ShowInTaskbar = false;
  			myform.Visible = true;
  			ListView mylistview = new ListView ();
  			ListViewItem itemA = mylistview.Items.Add ("A");
  			ColumnHeader colA = mylistview.Columns.Add ("Item Column", -2, HorizontalAlignment.Left);
  			Assert.AreSame (mylistview, itemA.ListView, "#1");
  			Assert.AreSame (mylistview, colA.ListView, "#2");
  			mylistview.Visible = true;
  			myform.Controls.Add (mylistview);
  			Assert.AreEqual (1, mylistview.Columns.Count, "#3");
  			Assert.AreEqual (1, mylistview.Items.Count, "#4");
  			mylistview.Clear ();
  			Assert.AreEqual (0, mylistview.Columns.Count, "#5");
  			Assert.AreEqual (0, mylistview.Items.Count, "#6");
  			Assert.IsNull (itemA.ListView, "#7");
  			Assert.IsNull (colA.ListView, "#8");
  			myform.Dispose ();
  		}
  
  		[Test] // bug #80620
  		public void ClientRectangle_Borders ()
  		{
  			ListView lv = new ListView ();
  			lv.CreateControl ();
  			Assert.AreEqual (lv.ClientRectangle, new ListView ().ClientRectangle);
  		}
  
  		[Test]
  		public void DisposeTest ()
  		{
  			ListView lv = new ListView ();
  			lv.View = View.Details;
  
  			lv.LargeImageList = new ImageList ();
  			lv.SmallImageList = new ImageList ();
  
  			ListViewItem lvi = new ListViewItem ();
  			lv.Items.Add (lvi);
  
  			ColumnHeader col = new ColumnHeader ();
  			lv.Columns.Add (col);
  
  			lv.Dispose ();
  
  			Assert.IsNull (lvi.ListView, "#A1");
  			Assert.IsNull (col.ListView, "#A2");
  
  			Assert.IsNull (lv.LargeImageList, "#B1");
  			Assert.IsNull (lv.SmallImageList, "#B2");
  			Assert.IsNull(lv.StateImageList, "#B3");
  		}
  
  		string dispose_log;
  
  		[Test]
  		public void DisposeLayoutTest ()
  		{
  			Form f = new Form ();
  			ListView lv = new ListView ();
  			f.Controls.Add (lv);
  			f.Show ();
  
  			dispose_log = String.Empty;
  			lv.Layout += DisposeOnLayout;
  			lv.Dispose (); // just to be sure.
  			f.Dispose ();
  
  			Assert.AreEqual (0, dispose_log.Length, "#A0");
  		}
  
  		void DisposeOnLayout (object o, LayoutEventArgs args)
  		{
  			dispose_log = "OnLayout";
  		}
  
  		// Hey
  		//[Test]
  		public void EnsureVisibleTest ()
  		{
  			Form myform = new Form ();
  			myform.ShowInTaskbar = false;
  			myform.Visible = true;
  			ListView mylistview = new ListView ();
  			mylistview.Items.Add ("A");
  			myform.Controls.Add (mylistview);
  			mylistview.BeginUpdate ();
  			for(int x = 1 ; x < 5000 ; x++) {
  				mylistview.Items.Add ("Item " + x.ToString());   
  			}
  			mylistview.EndUpdate ();
  			mylistview.EnsureVisible (4999);
  			myform.Dispose ();
  		}
  
  		[Test]
  		public void GetItemRectTest ()
  		{
  			ListView mylistview = new ListView ();
  			mylistview.Items.Add ("Item 1");
  			mylistview.Items.Add ("Item 2");
  			Rectangle r = mylistview.GetItemRect (1);
  			Assert.AreEqual (0, r.Top, "#35a");
  			Assert.IsTrue (r.Bottom > 0, "#35b");
  			Assert.IsTrue (r.Right > 0, "#35c");
  			Assert.IsTrue (r.Left > 0, "#35d");
  			Assert.IsTrue (r.Height > 0, "#35e");
  			Assert.IsTrue (r.Width > 0, "#35f");
  		}
  
  		[Test]
  		public void bug79076 ()
  		{
  			ListView entryList = new ListView ();
  			entryList.Sorting = SortOrder.Descending;
  
  			entryList.BeginUpdate ();
  			entryList.Columns.Add ("Type", 100, HorizontalAlignment.Left);
  
  			ListViewItem item = new ListViewItem (new string [] { "A" });
  			entryList.Items.Add (item);
  			item = new ListViewItem (new string [] { "B" });
  			entryList.Items.Add (item);
  		}
  
  		[Test] // bug #79416
  		public void MultiSelect ()
  		{
  			Form form = new Form ();
  			form.ShowInTaskbar = false;
  			ListView lvw = CreateListView (View.Details);
  			form.Controls.Add (lvw);
  			lvw.MultiSelect = true;
  			lvw.Items [0].Selected = true;
  			lvw.Items [2].Selected = true;
  
  			Assert.AreEqual (0, lvw.SelectedItems.Count, "#A1");
  			Assert.AreEqual (0, lvw.SelectedIndices.Count, "#A2");
  
  			lvw.Items [0].Selected = false;
  
  			Assert.AreEqual (0, lvw.SelectedItems.Count, "#B1");
  			Assert.AreEqual (0, lvw.SelectedIndices.Count, "#B2");
  
  			lvw.Items [0].Selected = true;
  
  			Assert.AreEqual (0, lvw.SelectedItems.Count, "#C1");
  			Assert.AreEqual (0, lvw.SelectedIndices.Count, "#C2");
  
  			form.Show ();
  
  			Assert.AreEqual (2, lvw.SelectedItems.Count, "#D1");
  			Assert.AreEqual ("B", lvw.SelectedItems [0].Text, "#D2");
  			Assert.AreEqual ("C", lvw.SelectedItems [1].Text, "#D3");
  			Assert.AreEqual (2, lvw.SelectedIndices.Count, "#D4");
  			Assert.AreEqual (0, lvw.SelectedIndices [0], "#D5");
  			Assert.AreEqual (2, lvw.SelectedIndices [1], "#D6");
  
  			// de-select an item
  			lvw.Items [2].Selected = false;
  
  			Assert.AreEqual (1, lvw.SelectedItems.Count, "#E1");
  			Assert.AreEqual ("B", lvw.SelectedItems [0].Text, "#E2");
  			Assert.AreEqual (1, lvw.SelectedIndices.Count, "#E3");
  			Assert.AreEqual (0, lvw.SelectedIndices [0], "#E4");
  
  			// re-select that item
  			lvw.Items [2].Selected = true;
  
  			Assert.AreEqual (2, lvw.SelectedItems.Count, "#F1");
  			Assert.AreEqual ("B", lvw.SelectedItems [0].Text, "#F2");
  			Assert.AreEqual ("C", lvw.SelectedItems [1].Text, "#F3");
  			Assert.AreEqual (2, lvw.SelectedIndices.Count, "#F4");
  			Assert.AreEqual (0, lvw.SelectedIndices [0], "#F5");
  			Assert.AreEqual (2, lvw.SelectedIndices [1], "#F6");
  
  			// dis-allow selection of multiple items
  			lvw.MultiSelect = false;
  
  			// setting MultiSelect to false when multiple items have been
  			// selected does not deselect items
  			Assert.AreEqual (2, lvw.SelectedItems.Count, "#G1");
  			Assert.AreEqual ("B", lvw.SelectedItems [0].Text, "#G2");
  			Assert.AreEqual ("C", lvw.SelectedItems [1].Text, "#G3");
  			Assert.AreEqual (2, lvw.SelectedIndices.Count, "#G4");
  			Assert.AreEqual (0, lvw.SelectedIndices [0], "#G5");
  			Assert.AreEqual (2, lvw.SelectedIndices [1], "#G6");
  
  			// de-select that item again
  			lvw.Items [2].Selected = false;
  
  			Assert.AreEqual (1, lvw.SelectedItems.Count, "#H1");
  			Assert.AreEqual ("B", lvw.SelectedItems [0].Text, "#H2");
  			Assert.AreEqual (1, lvw.SelectedIndices.Count, "#H3");
  			Assert.AreEqual (0, lvw.SelectedIndices [0], "#H4");
  
  			// re-select that item again
  			lvw.Items [2].Selected = true;
  
  			// when MultiSelect is false, and you attempt to select more than
  			// one item, then all items will first be de-selected and then
  			// the item in question is selected
  			Assert.AreEqual (1, lvw.SelectedItems.Count, "#I1");
  			Assert.AreEqual ("C", lvw.SelectedItems [0].Text, "#I2");
  			Assert.AreEqual (1, lvw.SelectedIndices.Count, "#I3");
  			Assert.AreEqual (2, lvw.SelectedIndices [0], "#I4");
  			form.Dispose ();
  		}
  
  		[Test]
  		public void TopItem ()
  		{
  			ListView lvw = CreateListView (View.List);
  
  			lvw.TopItem = null;
  			Assert.AreEqual (lvw.Items [0], lvw.TopItem, "#A1");
  
  			lvw.TopItem = new ListViewItem ();
  			Assert.AreEqual (lvw.Items [0], lvw.TopItem, "#A2");
  		}
  
  		[Test]
  		public void TopItem_Exceptions ()
  		{
  			ListView lvw = CreateListView (View.LargeIcon);
  			ListViewItem item = null;
  
  			try {
  				lvw.TopItem = lvw.Items [2];
  				Assert.Fail ("#A1");
  			} catch (InvalidOperationException) {
  			}
  
  			try {
  				item = lvw.TopItem;
  				Assert.Fail ("#A2");
  			} catch (InvalidOperationException) {
  			}
  
  			lvw.View = View.SmallIcon;
  			try {
  				lvw.TopItem = lvw.Items [2];
  				Assert.Fail ("#A3");
  			} catch (InvalidOperationException) {
  			}
  
  			try {
  				item = lvw.TopItem;
  				Assert.Fail ("#A4");
  			} catch (InvalidOperationException) {
  			}
  
  			lvw.View = View.Tile;
  			try {
  				lvw.TopItem = lvw.Items [2];
  				Assert.Fail ("#A5");
  			} catch (InvalidOperationException) {
  			}
  
  			try {
  				item = lvw.TopItem;
  				Assert.Fail ("#A6");
  			} catch (InvalidOperationException) {
  			}
  		}
  
  		[Test]
  		public void Selected ()
  		{
  			Form form = new Form ();
  			form.ShowInTaskbar = false;
  			ListView lvw = CreateListView (View.Details);
  			form.Controls.Add (lvw);
  			lvw.MultiSelect = true;
  			lvw.Items [0].Selected = true;
  			lvw.Items [2].Selected = true;
  
  			Assert.AreEqual (0, lvw.SelectedItems.Count, "#A1");
  			Assert.AreEqual (0, lvw.SelectedIndices.Count, "#A2");
  
  			form.Show ();
  
  			Assert.AreEqual (2, lvw.SelectedItems.Count, "#C1");
  			Assert.AreEqual ("B", lvw.SelectedItems [0].Text, "#C2");
  			Assert.AreEqual ("C", lvw.SelectedItems [1].Text, "#C3");
  			Assert.AreEqual (2, lvw.SelectedIndices.Count, "#C4");
  			Assert.AreEqual (0, lvw.SelectedIndices [0], "#C5");
  			Assert.AreEqual (2, lvw.SelectedIndices [1], "#C6");
  			form.Dispose ();
  		}
  
  		[Test]
  		public void FindItemWithText ()
  		{
  			ListView lvw = new ListView();
  			ListViewItem lvi1 = new ListViewItem (String.Empty);
  			ListViewItem lvi2 = new ListViewItem ("angle bracket");
  			ListViewItem lvi3 = new ListViewItem ("bracket holder");
  			ListViewItem lvi4 = new ListViewItem ("bracket");
  			lvw.Items.AddRange (new ListViewItem [] { lvi1, lvi2, lvi3, lvi4 });
  
  			Assert.AreEqual (lvi1, lvw.FindItemWithText (String.Empty), "#A1");
  			Assert.AreEqual (lvi3, lvw.FindItemWithText ("bracket"), "#A2");
  			Assert.AreEqual (lvi3, lvw.FindItemWithText ("BrackeT"), "#A3");
  			Assert.IsNull (lvw.FindItemWithText ("holder"), "#A5");
  
  			Assert.AreEqual (lvw.Items [3], lvw.FindItemWithText ("bracket", true, 3), "#B1");
  
  			Assert.AreEqual (lvw.Items [2], lvw.FindItemWithText ("bracket", true, 0, true), "#C1");
  			Assert.AreEqual (lvw.Items [3], lvw.FindItemWithText ("bracket", true, 0, false), "#C2");
  			Assert.AreEqual(lvw.Items [3], lvw.FindItemWithText("BrackeT", true, 0, false), "#C3");
  			Assert.IsNull (lvw.FindItemWithText ("brack", true, 0, false), "#C4");
  
  			// Sub item search tests
  			lvw.Items.Clear ();
  
  			lvi1.Text = "A";
  			lvi1.SubItems.Add ("car bracket");
  			lvi1.SubItems.Add ("C");
  
  			lvi2.Text = "B";
  			lvi2.SubItems.Add ("car");
  
  			lvi3.Text = "C";
  
  			lvw.Items.AddRange (new ListViewItem [] { lvi1, lvi2, lvi3 });
  
  			Assert.AreEqual (lvi1, lvw.FindItemWithText ("car", true, 0), "#D1");
  			Assert.AreEqual (lvi3, lvw.FindItemWithText ("C", true, 0), "#D2");
  			Assert.AreEqual (lvi2, lvw.FindItemWithText ("car", true, 1), "#D3");
  			Assert.IsNull (lvw.FindItemWithText ("car", false, 0), "#D4");
  
  			Assert.AreEqual (lvi1, lvw.FindItemWithText ("car", true, 0, true), "#E1");
  			Assert.AreEqual (lvi2, lvw.FindItemWithText ("car", true, 0, false), "#E2");
  			Assert.AreEqual (lvi2, lvw.FindItemWithText ("CaR", true, 0, false), "#E3");
  		}
  
  		[Test]
  		public void FindItemWithText_Exceptions ()
  		{
  			ListView lvw = new ListView ();
  
  			// Shouldn't throw any exception
  			lvw.FindItemWithText (null);
  
  			try {
  				lvw.FindItemWithText (null, false, 0);
  				Assert.Fail ("#A1");
  			} catch (ArgumentOutOfRangeException) {
  			}
  
  			try {
  				lvw.FindItemWithText (null, false, lvw.Items.Count);
  				Assert.Fail ("#A2");
  			} catch (ArgumentOutOfRangeException) {
  			}
  
  			// Add a single item
  			lvw.Items.Add ("bracket");
  
  			try {
  				lvw.FindItemWithText (null);
  				Assert.Fail ("#A3");
  			} catch (ArgumentNullException) {
  			}
  
  			try {
  				lvw.FindItemWithText ("bracket", false, -1);
  				Assert.Fail ("#A4");
  			} catch (ArgumentOutOfRangeException) {
  			}
  
  			try {
  				lvw.FindItemWithText ("bracket", false, lvw.Items.Count);
  				Assert.Fail ("#A5");
  			} catch (ArgumentOutOfRangeException) {
  			}
  		}
  
  		[Test]
  		public void FindNearestItem_Exceptions ()
  		{
  			ListView lvw = new ListView ();
  			lvw.Items.Add ("A");
  			lvw.Items.Add ("B");
  
  			lvw.View = View.Details;
  			try {
  				lvw.FindNearestItem (SearchDirectionHint.Down, 0, 0);
  				Assert.Fail ("#A1");
  			} catch (InvalidOperationException) {
  			}
  
  			lvw.View = View.List;
  			try {
  				lvw.FindNearestItem (SearchDirectionHint.Down, 0, 0);
  				Assert.Fail ("#A2");
  			} catch (InvalidOperationException) {
  			}
  
  			lvw.View = View.Tile;
  			try {
  				lvw.FindNearestItem (SearchDirectionHint.Down, 0, 0);
  				Assert.Fail ("#A3");
  			} catch (InvalidOperationException) {
  			}
  
  			lvw.View = View.LargeIcon;
  			try {
  				lvw.FindNearestItem ((SearchDirectionHint)666, 0, 0);
  			} catch (ArgumentOutOfRangeException) {
  			}
  		}
  
  		[Test]
  		public void FocusedItem ()
  		{
  			ListView lvw = CreateListView (View.LargeIcon);
  			Form form = new Form ();
  			lvw.Parent = form;
  
  			// FocusedItem setter ignores the value until form is shown
  			lvw.FocusedItem = lvw.Items [2];
  			Assert.AreEqual (null, lvw.FocusedItem, "#A1");
  
  			// It's not enough to create the ListView control
  			form.Show ();
  
  			lvw.FocusedItem = lvw.Items [2];
  			Assert.AreEqual (lvw.Items [2], lvw.FocusedItem, "#A2");
  
  			lvw.FocusedItem = new ListViewItem ();
  			Assert.AreEqual (lvw.Items [2], lvw.FocusedItem, "#A3");
  
  			lvw.FocusedItem = null;
  			Assert.AreEqual (lvw.Items [2], lvw.FocusedItem, "#A4");
  
  			form.Dispose ();
  		}
  
  		[Test]
  		public void HotTracking ()
  		{
  			ListView lvw = new ListView ();
  
  			lvw.HotTracking = true;
  			Assert.AreEqual (true, lvw.HotTracking, "#A1");
  			Assert.AreEqual (true, lvw.HoverSelection, "#A2");
  			Assert.AreEqual (ItemActivation.OneClick, lvw.Activation, "#A3");
  
  			// HoverSelection and Activation keep the previous value
  			lvw.HotTracking = false;
  			Assert.AreEqual (false, lvw.HotTracking, "#B1");
  			Assert.AreEqual (true, lvw.HoverSelection, "#B2");
  			Assert.AreEqual (ItemActivation.OneClick, lvw.Activation, "#B3");
  
  			lvw.HotTracking = true;
  			try {
  				lvw.HoverSelection = false;
  				Assert.Fail ("#C1");
  			} catch (ArgumentException) {
  			}
  
  			try {
  				lvw.Activation = ItemActivation.Standard;
  				Assert.Fail ("#C2");
  			} catch (ArgumentException) {
  			}
  		}
  
  		[Test]
  		public void RedrawItems_Exceptions ()
  		{
  			ListView lvw = new ListView ();
  			lvw.Items.Add ("A");
  			lvw.Items.Add ("B");
  
  			try {
  				lvw.RedrawItems (-1, 1, true);
  				Assert.Fail ("#A1");
  			} catch (ArgumentOutOfRangeException) {
  			}
  
  			try {
  				lvw.RedrawItems (0, -1, true);
  				Assert.Fail ("#A2");
  			} catch (ArgumentOutOfRangeException) {
  			}
  
  			try {
  				lvw.RedrawItems (lvw.Items.Count, 1, true);
  				Assert.Fail ("#A3");
  			} catch (ArgumentOutOfRangeException) {
  			}
  
  			try {
  				lvw.RedrawItems (0, lvw.Items.Count, true);
  				Assert.Fail ("#A4");
  			} catch (ArgumentOutOfRangeException) {
  			}
  
  			try {
  				lvw.RedrawItems (1, 0, true);
  				Assert.Fail ("#A5");
  			} catch (ArgumentException) {
  			}
  		}
  
  		const int item_count = 3;
  		ListViewItem [] items = new ListViewItem [item_count];
  
  		[Test]
  		public void VirtualMode ()
  		{
  			ListView lvw = new ListView ();
  			lvw.VirtualListSize = item_count;
  			lvw.RetrieveVirtualItem += ListViewRetrieveVirtualItemHandler;
  			lvw.VirtualMode = true;
  
  			CreateListViewItems (item_count);
  
  			Assert.AreEqual (item_count, lvw.Items.Count, "#A1");
  			Assert.AreEqual (true, lvw.VirtualMode, "#A2");
  
  			Assert.AreEqual (items [0], lvw.Items [0], "#B1");
  			Assert.AreEqual (items [1], lvw.Items [1], "#B2");
  			Assert.AreEqual (items [2], lvw.Items [2], "#B3");
  			Assert.AreEqual (0, lvw.Items [0].Index, "#B4");
  			Assert.AreEqual (1, lvw.Items [1].Index, "#B5");
  			Assert.AreEqual (2, lvw.Items [2].Index, "#B6");
  			Assert.AreEqual (lvw, lvw.Items [0].ListView, "#B7");
  			Assert.AreEqual (lvw, lvw.Items [1].ListView, "#B8");
  			Assert.AreEqual (lvw, lvw.Items [2].ListView, "#B9");
  
  			// force to re-create the items, because we need a blank state
  			// for our items
  			CreateListViewItems (item_count);
  			items [0].Name = "A";
  			items [1].Name = "B";
  			items [2].Name = "C";
  
  			Assert.AreEqual (items [0], lvw.Items ["A"], "#C1");
  			Assert.AreEqual (items [1], lvw.Items ["B"], "#C2");
  			Assert.AreEqual (items [2], lvw.Items ["C"], "#C3");
  			Assert.AreEqual (0, lvw.Items ["A"].Index, "#C4");
  			Assert.AreEqual (1, lvw.Items ["B"].Index, "#C5");
  			Assert.AreEqual (2, lvw.Items ["C"].Index, "#C6");
  			Assert.AreEqual (lvw, lvw.Items ["A"].ListView, "#C7");
  			Assert.AreEqual (lvw, lvw.Items ["B"].ListView, "#C8");
  			Assert.AreEqual (lvw, lvw.Items ["C"].ListView, "#C9");
  			Assert.IsNull (lvw.Items ["Invalid key"], "#C10");
  			Assert.IsNull (lvw.Items [String.Empty], "#C11");
  
  			Assert.AreEqual (false, lvw.Items.ContainsKey (String.Empty), "#D1");
  			Assert.AreEqual (false, lvw.Items.ContainsKey (null), "#D2");
  			Assert.AreEqual (true, lvw.Items.ContainsKey ("A"), "#D3");
  			Assert.AreEqual (true, lvw.Items.ContainsKey ("a"), "#D4");
  			Assert.AreEqual (true, lvw.Items.ContainsKey ("B"), "#D5");
  		}
  
  		void ListViewRetrieveVirtualItemHandler (object o, RetrieveVirtualItemEventArgs args)
  		{
  			args.Item = items [args.ItemIndex];
  		}
  
  		void CreateListViewItems(int count)
  		{
  			items = new ListViewItem [count];
  
  			for (int i = 0; i < count; i++)
  				items [i] = new ListViewItem (String.Empty);
  		}
  
  		[Test]
  		public void VirtualMode_Exceptions()
  		{
  			ListView lvw = new ListView ();
  
  			lvw.Items.Add ("Simple item");
  			try {
  				lvw.VirtualMode = true;
  				Assert.Fail ("#A1");
  			} catch (InvalidOperationException) {
  			}
  
  			lvw.Items.Clear();
  			lvw.VirtualMode = true;
  			lvw.VirtualListSize = 1;
  
  			lvw.RetrieveVirtualItem += ListViewRetrieveVirtualItemHandler;
  			CreateListViewItems (1);
  
  			try {
  				lvw.Sort ();
  				Assert.Fail ("#A3");
  			} catch (InvalidOperationException) {
  			}
  		}
  
  		[Test]
  		public void VirtualListSize ()
  		{
  			ListView lvw = new ListView ();
  
  			lvw.VirtualListSize = item_count;
  			Assert.AreEqual (item_count, lvw.VirtualListSize, "#A1");
  			Assert.AreEqual (0, lvw.Items.Count, "#A2");
  
  			lvw.VirtualMode = true;
  			Assert.AreEqual (item_count, lvw.VirtualListSize, "#B1");
  			Assert.AreEqual (item_count, lvw.Items.Count, "#B2");
  
  			lvw.VirtualMode = false;
  			Assert.AreEqual (item_count, lvw.VirtualListSize, "#C1");
  			Assert.AreEqual (0, lvw.Items.Count, "#C2");
  		}
  
  		[Test]
  		public void VirtualListSize_Exceptions ()
  		{
  			ListView lvw = new ListView ();
  			try {
  				lvw.VirtualListSize = -1;
  				Assert.Fail ("#A1");
  			} catch (ArgumentException) {
  			}
  		}
  
  		[Test]
  		public void Sort_Details_Checked ()
  		{
  			AssertSort_Checked (View.Details);
  		}
  
  		[Test]
  		public void Sort_Details_Created ()
  		{
  			AssertSortNoIcon_Created (View.Details);
  		}
  
  		[Test]
  		public void Sort_Details_NotCreated ()
  		{
  			AssertSortNoIcon_NotCreated (View.Details);
  		}
  
  		[Test]
  		public void Sort_Details_Selected ()
  		{
  			AssertSort_Selected (View.Details);
  		}
  
  		[Test]
  		public void Sort_LargeIcon_Checked ()
  		{
  			AssertSort_Checked (View.LargeIcon);
  		}
  
  		[Test]
  		public void Sort_LargeIcon_Created ()
  		{
  			AssertSortIcon_Created (View.LargeIcon);
  		}
  
  		[Test]
  		public void Sort_LargeIcon_NotCreated ()
  		{
  			AssertSortIcon_NotCreated (View.LargeIcon);
  		}
  
  		[Test]
  		public void Sort_LargeIcon_Selected ()
  		{
  			AssertSort_Selected (View.LargeIcon);
  		}
  
  		[Test]
  		public void Sort_List_Checked ()
  		{
  			AssertSort_Checked (View.List);
  		}
  
  		[Test]
  		public void Sort_List_Created ()
  		{
  			AssertSortNoIcon_Created (View.List);
  		}
  
  		[Test]
  		public void Sort_List_NotCreated ()
  		{
  			AssertSortNoIcon_NotCreated (View.List);
  		}
  
  		[Test]
  		public void Sort_List_Selection ()
  		{
  			AssertSort_Selected (View.List);
  		}
  
  		[Test]
  		public void Sort_SmallIcon_Checked ()
  		{
  			AssertSort_Checked (View.SmallIcon);
  		}
  
  		[Test]
  		public void Sort_SmallIcon_Created ()
  		{
  			AssertSortIcon_Created (View.SmallIcon);
  		}
  
  		[Test]
  		public void Sort_SmallIcon_NotCreated ()
  		{
  			AssertSortIcon_NotCreated (View.SmallIcon);
  		}
  
  		[Test]
  		public void Sort_SmallIcon_Selection ()
  		{
  			AssertSort_Selected (View.SmallIcon);
  		}
  
  		[Test]
  		[ExpectedException (typeof (NotSupportedException))]
  		public void Sort_Tile_Checked ()
  		{
  			AssertSort_Checked (View.Tile);
  		}
  
  		[Test]
  		public void Sort_Tile_Created ()
  		{
  			AssertSortNoIcon_Created (View.Tile);
  		}
  
  		[Test]
  		public void Sort_Tile_NotCreated ()
  		{
  			AssertSortNoIcon_NotCreated (View.Tile);
  		}
  
  		[Test]
  		public void Sort_Tile_Selection ()
  		{
  			AssertSort_Selected (View.Tile);
  		}
  
  		private void AssertSortIcon_Created (View view)
  		{
  			int compareCount = 0;
  
  			Form form = new Form ();
  			form.ShowInTaskbar = false;
  			ListView lvw = CreateListView (view);
  			form.Controls.Add (lvw);
  			Assert.IsNull (lvw.ListViewItemSorter, "#A");
  
  			form.Show ();
  
  			Assert.IsNull (lvw.ListViewItemSorter, "#B1");
  			Assert.AreEqual ("B", lvw.Items [0].Text, "#B2");
  			Assert.AreEqual ("A", lvw.Items [1].Text, "#B3");
  			Assert.AreEqual ("C", lvw.Items [2].Text, "#B4");
  
  			lvw.Sorting = SortOrder.None;
  			Assert.IsNull (lvw.ListViewItemSorter, "#C1");
  			Assert.AreEqual ("B", lvw.Items [0].Text, "#C2");
  			Assert.AreEqual ("A", lvw.Items [1].Text, "#C3");
  			Assert.AreEqual ("C", lvw.Items [2].Text, "#C4");
  
  			lvw.Sorting = SortOrder.Descending;
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#D1");
  			Assert.AreEqual ("C", lvw.Items [0].Text, "#D2");
  			Assert.AreEqual ("B", lvw.Items [1].Text, "#D3");
  			Assert.AreEqual ("A", lvw.Items [2].Text, "#D4");
  
  			lvw.Sorting = SortOrder.Ascending;
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#E1");
  			Assert.AreEqual ("A", lvw.Items [0].Text, "#E2");
  			Assert.AreEqual ("B", lvw.Items [1].Text, "#E3");
  			Assert.AreEqual ("C", lvw.Items [2].Text, "#E4");
  
  			lvw.Sorting = SortOrder.None;
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#F1");
  			Assert.AreEqual ("A", lvw.Items [0].Text, "#F2");
  			Assert.AreEqual ("B", lvw.Items [1].Text, "#F3");
  			Assert.AreEqual ("C", lvw.Items [2].Text, "#F4");
  
  			lvw.Sorting = SortOrder.Ascending;
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#G1");
  			Assert.AreEqual ("A", lvw.Items [0].Text, "#G2");
  			Assert.AreEqual ("B", lvw.Items [1].Text, "#G3");
  			Assert.AreEqual ("C", lvw.Items [2].Text, "#G4");
  
  			lvw.Sorting = SortOrder.Descending;
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#G1");
  			Assert.AreEqual ("C", lvw.Items [0].Text, "#G2");
  			Assert.AreEqual ("B", lvw.Items [1].Text, "#G3");
  			Assert.AreEqual ("A", lvw.Items [2].Text, "#G4");
  
  			lvw.Sorting = SortOrder.None;
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#H1");
  			Assert.AreEqual ("C", lvw.Items [0].Text, "#H2");
  			Assert.AreEqual ("B", lvw.Items [1].Text, "#H3");
  			Assert.AreEqual ("A", lvw.Items [2].Text, "#H4");
  
  			// when Sorting is None and a new item is added, the collection is
  			// sorted using the previous Sorting value
  			lvw.Items.Add ("BB");
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#I1");
  			Assert.AreEqual ("C", lvw.Items [0].Text, "#I2");
  			Assert.AreEqual ("BB", lvw.Items [1].Text, "#I3");
  			Assert.AreEqual ("B", lvw.Items [2].Text, "#I4");
  			Assert.AreEqual ("A", lvw.Items [3].Text, "#I5");
  
  			lvw.Sorting = SortOrder.Ascending;
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#J1");
  			Assert.AreEqual ("A", lvw.Items [0].Text, "#J2");
  			Assert.AreEqual ("B", lvw.Items [1].Text, "#J3");
  			Assert.AreEqual ("BB", lvw.Items [2].Text, "#J4");
  			Assert.AreEqual ("C", lvw.Items [3].Text, "#J5");
  
  			// when Sorting is not None and a new item is added, the
  			// collection is re-sorted automatically
  			lvw.Items.Add ("BA");
  			Assert.IsNotNull (lvw.ListViewItemSorter, "#K1");
  			Assert.AreEqual ("A", lvw.Items [0].Text, "#K2");
  			Assert.AreEqual ("B", lvw.Items [1].Text, "#K3");
  			Assert.AreEqual ("BA", lvw.Items [2].Text, "#K4");
  			Assert.AreEqual ("BB", lvw.Items [3].Text, "#K5");
  			Assert.AreEqual ("C", lvw.Items [4].Text, "#K6");
  
  			// assign a custom comparer
  
gt: 			MockComparer mc = new MockComparer (false);