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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Jordi Mas i Hernandez <jordi@ximian.com>
29 using System.Collections;
30 using System.ComponentModel;
32 using System.Windows.Forms;
34 using NUnit.Framework;
36 namespace MonoTests.System.Windows.Forms
39 public class GridColumnStylesCollectionTest : TestHelper
41 private bool eventhandled;
42 private object Element;
43 private CollectionChangeAction Action;
48 public void TestDefaultValues ()
50 DataGridTableStyle ts = new DataGridTableStyle ();
51 GridColumnStylesCollection sc = ts.GridColumnStyles;
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");
61 public void TestAdd ()
63 DataGridTableStyle ts = new DataGridTableStyle ();
64 GridColumnStylesCollection sc = ts.GridColumnStyles;
65 sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
69 DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
70 col1.MappingName = "Column1";
72 Assert.AreEqual (true, eventhandled);
73 Assert.AreEqual (col1, Element);
74 Assert.AreEqual (CollectionChangeAction.Add, Action);
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");
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.
92 public void TestAddWithBindingContext ()
94 DataGrid datagrid = new DataGrid ();
95 datagrid.BindingContext = new BindingContext ();
96 DataTable table = new DataTable ();
97 datagrid.DataSource = table;
99 DataGridTableStyle ts = new DataGridTableStyle ();
100 datagrid.TableStyles.Add (ts);
102 DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
103 col1.MappingName = "Column1"; // Not valid mapping
104 ts.GridColumnStyles.Add (col1);
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");
112 public void TestAddRange ()
114 DataGridTableStyle ts = new DataGridTableStyle ();
115 GridColumnStylesCollection sc = ts.GridColumnStyles;
116 sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
119 DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
120 col1.MappingName = "Column1";
122 DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
123 col2.MappingName = "Column2";
124 sc.AddRange (new DataGridColumnStyle[] {col1, col2});
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");
133 public void TestRemove ()
135 DataGridTableStyle ts = new DataGridTableStyle ();
136 GridColumnStylesCollection sc = ts.GridColumnStyles;
137 sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
140 DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
141 col1.MappingName = "Column1";
144 DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
145 col2.MappingName = "Column2";
148 DataGridTextBoxColumn col3 = new DataGridTextBoxColumn ();
149 col3.MappingName = "Column3";
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");
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");
168 Assert.AreEqual (null, Element, "A8");
169 Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A9");
174 public void TestIndexContains ()
176 DataGridTableStyle ts = new DataGridTableStyle ();
177 GridColumnStylesCollection sc = ts.GridColumnStyles;
178 sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
181 DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
182 col1.MappingName = "Column1";
185 DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
186 col2.MappingName = "Column2";
189 DataGridTextBoxColumn col3 = new DataGridTextBoxColumn ();
190 col3.MappingName = "Column3";
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");
200 private void ResetEventData ()
203 eventhandled = false;
205 Action = (CollectionChangeAction) 0;
208 //private void OnEventHandler (object sender, EventArgs e)
210 // eventhandled = true;
213 private void OnCollectionEventHandler (object sender, CollectionChangeEventArgs e)