Unit test for bug #821.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / GridColumnStylesCollectionTest.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) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Jordi Mas i Hernandez <jordi@ximian.com>
24 //
25 //
26
27 using System;
28 using System.Data;
29 using System.Collections;
30 using System.ComponentModel;
31 using System.Drawing;
32 using System.Windows.Forms;
33 using System.Xml;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Windows.Forms
37 {
38         [TestFixture]
39         public class GridColumnStylesCollectionTest : TestHelper
40         {
41                 private bool eventhandled;
42                 private object Element;
43                 private CollectionChangeAction Action;
44                 private int times;
45
46
47                 [Test]
48                 public void TestDefaultValues ()
49                 {
50                         DataGridTableStyle ts = new DataGridTableStyle ();
51                         GridColumnStylesCollection sc = ts.GridColumnStyles;
52
53                         Assert.AreEqual (false, sc.IsSynchronized, "IsSynchronized property");
54                         Assert.AreEqual (0, sc.Count, "Count");
55                         Assert.AreEqual (sc, sc.SyncRoot, "SyncRoot property");
56                         Assert.AreEqual (false, ((IList)sc).IsFixedSize, "IsFixedSize property");
57                         Assert.AreEqual (false, sc.IsReadOnly, "IsReadOnly property");
58                 }
59
60                 [Test]
61                 public void TestAdd ()
62                 {
63                         DataGridTableStyle ts = new DataGridTableStyle ();
64                         GridColumnStylesCollection sc = ts.GridColumnStyles;                    
65                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
66
67                         // Add single
68                         ResetEventData ();
69                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
70                         col1.MappingName = "Column1";
71                         sc.Add (col1);
72                         Assert.AreEqual (true, eventhandled);
73                         Assert.AreEqual (col1, Element);
74                         Assert.AreEqual (CollectionChangeAction.Add, Action);
75
76                         // Add multiple
77                         ResetEventData ();
78                         DataGridTextBoxColumn elem1 = new DataGridTextBoxColumn ();
79                         DataGridTextBoxColumn elem2 = new DataGridTextBoxColumn ();
80                         sc.AddRange (new DataGridTextBoxColumn [] {elem1, elem2});
81                         Assert.AreEqual (true, eventhandled, "A1");
82                         Assert.AreEqual (CollectionChangeAction.Add, Action, "A2");
83                         Assert.AreEqual (elem2, Element, "A3");
84                         
85                 }
86
87                 // The idea of this test is to have a complete DataGrid system
88                 // and assert that the Add method of the colleciton is _not_ forcing a 
89                 // call to the protected CheckValidSource method in the new column style class.
90                 // Bug #465019.
91                 [Test]
92                 public void TestAddWithBindingContext ()
93                 {
94                         DataGrid datagrid = new DataGrid ();
95                         datagrid.BindingContext = new BindingContext ();
96                         DataTable table = new DataTable ();
97                         datagrid.DataSource = table;
98
99                         DataGridTableStyle ts = new DataGridTableStyle ();
100                         datagrid.TableStyles.Add (ts);
101
102                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
103                         col1.MappingName = "Column1"; // Not valid mapping
104                         ts.GridColumnStyles.Add (col1);
105
106                         // More important: we should _not_ throw an exc here.
107                         Assert.AreEqual (ts, col1.DataGridTableStyle, "#A1");
108                         Assert.AreEqual (1, ts.GridColumnStyles.Count, "#A2");
109                 }
110
111                 [Test]
112                 public void TestAddRange ()
113                 {
114                         DataGridTableStyle ts = new DataGridTableStyle ();
115                         GridColumnStylesCollection sc = ts.GridColumnStyles;                    
116                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
117
118                         ResetEventData ();
119                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
120                         col1.MappingName = "Column1";
121
122                         DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
123                         col2.MappingName = "Column2";
124                         sc.AddRange (new DataGridColumnStyle[] {col1, col2});
125
126                         Assert.AreEqual (true, eventhandled, "A1");
127                         Assert.AreEqual (col2, Element, "A2");
128                         Assert.AreEqual (CollectionChangeAction.Add, Action, "A3");
129                         Assert.AreEqual (2, times, "A4");
130                 }
131
132                 [Test]
133                 public void TestRemove ()
134                 {
135                         DataGridTableStyle ts = new DataGridTableStyle ();
136                         GridColumnStylesCollection sc = ts.GridColumnStyles;                    
137                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
138
139                         // Add single                   
140                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
141                         col1.MappingName = "Column1";
142                         sc.Add (col1);
143                         
144                         DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
145                         col2.MappingName = "Column2";
146                         sc.Add (col2);
147                         
148                         DataGridTextBoxColumn col3 = new DataGridTextBoxColumn ();
149                         col3.MappingName = "Column3";
150                         sc.Add (col3);
151
152                         ResetEventData ();
153                         sc.Remove (col2);
154                         Assert.AreEqual (true, eventhandled, "A1");
155                         Assert.AreEqual (col2, Element, "A2");
156                         Assert.AreEqual (CollectionChangeAction.Remove, Action, "A3");
157                         Assert.AreEqual (2, sc.Count, "A4");
158
159                         ResetEventData ();
160                         sc.RemoveAt (0);
161                         Assert.AreEqual (true, eventhandled, "A5");
162                         Assert.AreEqual (col1, Element, "A6");
163                         Assert.AreEqual (CollectionChangeAction.Remove, Action, "A6");
164                         Assert.AreEqual (1, sc.Count, "A7");
165
166                         ResetEventData ();
167                         sc.Clear ();
168                         Assert.AreEqual (null, Element, "A8");
169                         Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A9");
170
171                 }
172
173                 [Test]
174                 public void TestIndexContains ()
175                 {
176                         DataGridTableStyle ts = new DataGridTableStyle ();
177                         GridColumnStylesCollection sc = ts.GridColumnStyles;                    
178                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
179
180                         // Add single                   
181                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
182                         col1.MappingName = "Column1";
183                         sc.Add (col1);
184                         
185                         DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
186                         col2.MappingName = "Column2";
187                         sc.Add (col2);
188                         
189                         DataGridTextBoxColumn col3 = new DataGridTextBoxColumn ();
190                         col3.MappingName = "Column3";
191                         sc.Add (col3);
192
193                         ResetEventData ();
194                         IList ilist = (IList) sc;
195                         Assert.AreEqual (1, ilist.IndexOf (col2), "A1");
196                         Assert.AreEqual (false, sc.Contains ("nothing"), "A2");
197                         Assert.AreEqual (true, sc.Contains (col3), "A3");
198                 }
199
200                 private void ResetEventData ()
201                 {
202                         times = 0;
203                         eventhandled = false;
204                         Element = null;
205                         Action = (CollectionChangeAction) 0;
206                 }
207
208                 //private void OnEventHandler (object sender, EventArgs e)
209                 //{
210                 //        eventhandled = true;
211                 //}
212
213                 private void OnCollectionEventHandler (object sender, CollectionChangeEventArgs e)
214                 {                       
215                         eventhandled = true;
216                         Element = e.Element;
217                         Action = e.Action;
218                         times++;
219                 }
220
221                 
222         }
223
224 }