2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / DataGridViewColumnCollectionTest.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Rolf Bjarne Kvinge  (RKvinge@novell.com)
24 //
25
26 #if NET_2_0
27
28 using NUnit.Framework;
29 using System;
30 using System.Drawing;
31 using System.Windows.Forms;
32 using System.ComponentModel;
33 using System.Collections;
34
35 namespace MonoTests.System.Windows.Forms
36 {
37
38         [TestFixture]
39         public class DataGridViewColumnCollectionTest : TestHelper 
40         {
41                 [Test]
42                 [ExpectedException (typeof (InvalidOperationException), "Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.")]
43                 public void AddFullColumnSelect ()
44                 {
45                         DataGridView dgv = new DataGridView ();
46                         dgv.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
47                         dgv.Columns.Add ("A", "A");
48                 }
49                 
50                 [Test]
51                 public void Add ()
52                 {
53                         DataGridViewColumnCollection c;
54                         
55                         c = (new DataGridView ()).Columns;
56                         c.Add ("A", "B");
57                         
58                         DataGridViewColumn col = c [0];
59                         
60                         Assert.AreEqual ("DataGridViewTextBoxColumn { Name=A, Index=0 }", col.ToString (), "T3");
61                         Assert.AreEqual ("DataGridViewTextBoxColumn", col.GetType ().Name, "G2");
62                         
63                         Assert.AreEqual (DataGridViewAutoSizeColumnMode.NotSet, col.AutoSizeMode, "#A col.AutoSizeMode");
64                         Assert.IsNotNull (col.CellTemplate, "#A col.CellTemplate");
65                         Assert.IsNotNull (col.CellType, "#A col.CellType");
66                         Assert.IsNull (col.ContextMenuStrip, "#A col.ContextMenuStrip");
67                         Assert.IsNotNull (col.DataGridView, "#A col.DataGridView");
68                         Assert.AreEqual (@"", col.DataPropertyName, "#A col.DataPropertyName");
69                         Assert.IsNotNull (col.DefaultCellStyle, "#A col.DefaultCellStyle");
70                         Assert.IsNotNull (col.DefaultHeaderCellType, "#A col.DefaultHeaderCellType");
71                         Assert.AreEqual (false, col.Displayed, "#A col.Displayed");
72                         Assert.AreEqual (0, col.DisplayIndex, "#A col.DisplayIndex");
73                         Assert.AreEqual (0, col.DividerWidth, "#A col.DividerWidth");
74                         Assert.AreEqual (100, col.FillWeight, "#A col.FillWeight");
75                         Assert.AreEqual (false, col.Frozen, "#A col.Frozen");
76                         Assert.AreEqual (true, col.HasDefaultCellStyle, "#A col.HasDefaultCellStyle");
77                         Assert.IsNotNull (col.HeaderCell, "#A col.HeaderCell");
78                         Assert.AreEqual (@"B", col.HeaderText, "#A col.HeaderText");
79                         Assert.AreEqual (0, col.Index, "#A col.Index");
80                         Assert.AreEqual (DataGridViewAutoSizeColumnMode.None, col.InheritedAutoSizeMode, "#A col.InheritedAutoSizeMode");
81                         Assert.IsNotNull (col.InheritedStyle, "#A col.InheritedStyle");
82                         Assert.AreEqual (false, col.IsDataBound, "#A col.IsDataBound");
83                         Assert.AreEqual (5, col.MinimumWidth, "#A col.MinimumWidth");
84                         Assert.AreEqual (@"A", col.Name, "#A col.Name");
85                         Assert.AreEqual (false, col.ReadOnly, "#A col.ReadOnly");
86                         Assert.AreEqual (DataGridViewTriState.True, col.Resizable, "#A col.Resizable");
87                         Assert.AreEqual (false, col.Selected, "#A col.Selected");
88                         Assert.IsNull (col.Site, "#A col.Site");
89                         Assert.AreEqual (DataGridViewColumnSortMode.Automatic, col.SortMode, "#A col.SortMode");
90                         Assert.AreEqual (DataGridViewElementStates.Visible, col.State, "#A col.State");
91                         Assert.IsNull (col.Tag, "#A col.Tag");
92                         Assert.AreEqual (@"", col.ToolTipText, "#A col.ToolTipText");
93                         Assert.IsNull (col.ValueType, "#A col.ValueType");
94                         Assert.AreEqual (true, col.Visible, "#A col.Visible");
95                         Assert.AreEqual (100, col.Width, "#A col.Width");
96                         
97                 }
98
99                 [Test]
100                 public void IndexUpdatedOnColumnCollectionChange ()
101                 {
102                         DataGridView dgv = new DataGridView ();
103
104                         Form f = new Form ();
105                         f.Controls.Add (dgv);
106                         f.Show ();
107
108                         dgv.Columns.Add ("A1", "A1");
109                         Assert.AreEqual (0, dgv.Columns[0].Index, "A1");
110
111                         dgv.Columns.Add ("A2", "A2");
112                         Assert.AreEqual (0, dgv.Columns[0].Index, "A2");
113                         Assert.AreEqual (1, dgv.Columns[1].Index, "A3");
114
115                         dgv.Columns.Insert (0, new DataGridViewTextBoxColumn ());
116                         Assert.AreEqual (0, dgv.Columns[0].Index, "A4");
117                         Assert.AreEqual (1, dgv.Columns[1].Index, "A5");
118                         Assert.AreEqual (2, dgv.Columns[2].Index, "A6");
119
120                         dgv.Columns.RemoveAt (1);
121                         Assert.AreEqual (0, dgv.Columns[0].Index, "A7");
122                         Assert.AreEqual (1, dgv.Columns[1].Index, "A8");
123
124                         dgv.Columns.RemoveAt (0);
125                         Assert.AreEqual (0, dgv.Columns[0].Index, "A9");
126
127                         f.Close ();
128                         f.Dispose ();
129                 }
130         }
131 }
132 #endif