Things happen a little differently when control is created.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / DataGridTest.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.Collections;
29 using System.ComponentModel;
30 using System.Drawing;
31 using System.Windows.Forms;
32 using System.Xml;
33 using NUnit.Framework;
34 using System.Data;
35
36 namespace MonoTests.System.Windows.Forms
37 {
38         // Helper classes
39
40         class TestDataGrid : DataGrid 
41         {
42                 public TestDataGrid () 
43                 {
44
45                 }
46
47                 public CurrencyManager Manager {
48                         get {
49                                 return ListManager;
50                         }
51                 }       
52         }
53
54         [TestFixture]
55         class DataGridTest
56         {
57                 private bool eventhandled;
58
59                 [TearDown]
60                 public void Clean() {}
61
62                 [SetUp]
63                 public void GetReady ()
64                 {
65                 }
66
67                 [Test]
68                 public void TestDefaultValues ()
69                 {
70                         DataGrid dg = new DataGrid ();
71
72                         Assert.AreEqual (true, dg.AllowNavigation, "AllowNavigation property");
73                         Assert.AreEqual (true, dg.AllowSorting, "AllowSorting property");
74                         Assert.AreEqual (BorderStyle.Fixed3D, dg.BorderStyle, "BorderStyle property");
75                         Assert.AreEqual (string.Empty, dg.CaptionText, "CaptionText property");
76                         Assert.AreEqual (true, dg.CaptionVisible, "CaptionVisible property");
77                         Assert.AreEqual (true, dg.ColumnHeadersVisible, "ColumnHeadersVisible property");
78                         Assert.AreEqual (new DataGridCell (), dg.CurrentCell, "CurrentCell property");
79                         Assert.AreEqual (-1, dg.CurrentRowIndex, "CurrentRowIndex property");
80                         Assert.AreEqual (string.Empty, dg.DataMember, "DataMember property");
81                         Assert.AreEqual (null, dg.DataSource, "DataSource property");
82                         Assert.AreEqual (0, dg.FirstVisibleColumn, "FirstVisibleColumn property");
83                         Assert.AreEqual (false, dg.FlatMode, "FlatMode property");
84                         Assert.AreEqual (DataGridLineStyle.Solid, dg.GridLineStyle, "GridLineStyle property");
85                         Assert.AreEqual (DataGridParentRowsLabelStyle.Both, dg.ParentRowsLabelStyle, "ParentRowsLabelStyle property");
86                         Assert.AreEqual (true, dg.ParentRowsVisible, "ParentRowsVisible property");
87                         Assert.AreEqual (75, dg.PreferredColumnWidth, "PreferredColumnWidth property");
88                         //Assert.AreEqual (16, dg.PreferredRowHeight, "PreferredRowHeight property");
89                         Assert.AreEqual (false, dg.ReadOnly, "ReadOnly property");
90                         Assert.AreEqual (true, dg.RowHeadersVisible, "RowHeadersVisible property");
91                         Assert.AreEqual (35, dg.RowHeaderWidth, "RowHeaderWidth property");
92                         Assert.AreEqual (null, dg.Site, "Site property");
93                         Assert.AreEqual (string.Empty, dg.Text, "Text property");
94                         Assert.AreEqual (0, dg.VisibleColumnCount, "VisibleColumnCount property");
95                 }
96
97                 [Test]
98                 public void TestAllowNavigationChangedEvent ()
99                 {
100                         DataGrid dg = new DataGrid ();
101                         eventhandled = false;
102                         dg.AllowNavigationChanged += new EventHandler (OnEventHandler);
103                         dg.AllowNavigation = !dg.AllowNavigation;
104                         Assert.AreEqual (true, eventhandled, "A1");
105                 }
106
107                 [Test]
108                 public void TestBackgroundColorChangedEvent ()
109                 {
110                         DataGrid dg = new DataGrid ();
111                         eventhandled = false;
112                         dg.BackgroundColorChanged  += new EventHandler (OnEventHandler);
113                         dg.BackgroundColor = Color.Red;
114                         Assert.AreEqual (true, eventhandled, "A1");
115                 }
116
117                 [Test]
118                 public void TestBorderStyleChangedEvent ()
119                 {
120                         DataGrid dg = new DataGrid ();
121                         eventhandled = false;
122                         dg.BorderStyleChanged  += new EventHandler (OnEventHandler);
123                         dg.BorderStyle = BorderStyle.None;
124                         Assert.AreEqual (true, eventhandled, "A1");
125                 }
126
127                 [Test]
128                 public void TestCaptionVisibleChangedEvent ()
129                 {
130                         DataGrid dg = new DataGrid ();
131                         eventhandled = false;
132                         dg.CaptionVisibleChanged += new EventHandler (OnEventHandler);
133                         dg.CaptionVisible = !dg.CaptionVisible;
134                         Assert.AreEqual (true, eventhandled, "A1");
135                 }
136
137                 [Test]
138                 public void TestFlatModeChangedEvent ()
139                 {
140                         DataGrid dg = new DataGrid ();
141                         eventhandled = false;
142                         dg.FlatModeChanged += new EventHandler (OnEventHandler);
143                         dg.FlatMode = !dg.FlatMode;
144                         Assert.AreEqual (true, eventhandled, "A1");
145                 }
146
147                 [Test]
148                 public void TestParentRowsLabelStyleChangedEvent ()
149                 {
150                         DataGrid dg = new DataGrid ();
151                         eventhandled = false;
152                         dg.ParentRowsLabelStyleChanged  += new EventHandler (OnEventHandler);
153                         dg.ParentRowsLabelStyle = DataGridParentRowsLabelStyle.None;
154                         Assert.AreEqual (true, eventhandled, "A1");
155                 }
156
157                 [Test]
158                 public void TestParentRowsVisibleChangedEvent ()
159                 {
160                         DataGrid dg = new DataGrid ();
161                         eventhandled = false;
162                         dg.ParentRowsVisibleChanged  += new EventHandler (OnEventHandler);
163                         dg.ParentRowsVisible = !dg.ParentRowsVisible;
164                         Assert.AreEqual (true, eventhandled, "A1");
165                 }
166                 
167                 [Test]
168                 public void TestReadOnlyChangedEvent ()
169                 {
170                         DataGrid dg = new DataGrid ();
171                         eventhandled = false;
172                         dg.ReadOnlyChanged  += new EventHandler (OnEventHandler);
173                         dg.ReadOnly = !dg.ReadOnly;
174                         Assert.AreEqual (true, eventhandled, "A1");
175                 }
176
177                 public void OnEventHandler (object sender, EventArgs e)\r
178                 {\r
179                         eventhandled = true;\r
180                 }
181
182                 // Property exceptions
183
184                 [Test]
185                 [ExpectedException (typeof (ArgumentException))]
186                 public void GridLineColorException ()
187                 {
188                         DataGrid dg = new DataGrid ();
189                         dg.GridLineColor = Color.Empty;
190                 }
191
192                 [Test]
193                 [ExpectedException (typeof (ArgumentException))]
194                 public void HeaderBackColorException ()
195                 {
196                         DataGrid dg = new DataGrid ();
197                         dg.HeaderBackColor = Color.Empty;
198                 }
199
200                 [Test]
201                 [ExpectedException (typeof (ArgumentException))]
202                 public void PreferredColumnWidthException ()
203                 {
204                         DataGrid dg = new DataGrid ();
205                         dg.PreferredColumnWidth = -1;
206                 }
207                 
208                 [Test]
209                 public void ResetAlternatingBackColor ()
210                 {
211                         DataGrid dg = new DataGrid ();
212                         DataGrid dg2 = new DataGrid ();
213                         dg2.AlternatingBackColor = Color.Red;
214                         dg2.ResetAlternatingBackColor ();
215                         Assert.AreEqual (dg.AlternatingBackColor, dg2.AlternatingBackColor, "A1");
216                 }
217                 
218                 // Test reset colour methods
219                 [Test]
220                 public void ResetBackColorMethod ()
221                 {
222                         DataGrid dg = new DataGrid ();
223                         DataGrid dg2 = new DataGrid ();
224                         dg2.BackColor = Color.Red;
225                         dg2.ResetBackColor ();
226                         Assert.AreEqual (dg.BackColor, dg2.BackColor, "A1");
227                 }
228
229                 [Test]
230                 public void ResetForeColorMethod ()
231                 {
232                         DataGrid dg = new DataGrid ();
233                         DataGrid dg2 = new DataGrid ();
234                         dg2.ForeColor = Color.Red;
235                         dg2.ResetForeColor ();
236                         Assert.AreEqual (dg.ForeColor, dg2.ForeColor, "A1");
237                 }
238
239                 [Test]
240                 public void ResetGridLineColorMethod ()
241                 {                       
242                         DataGrid dg = new DataGrid ();
243                         DataGrid dg2 = new DataGrid ();
244                         dg2.GridLineColor = Color.Red;
245                         dg2.ResetGridLineColor ();
246                         Assert.AreEqual (dg.GridLineColor, dg2.GridLineColor, "A1");
247                 }
248
249                 [Test]
250                 public void ResetHeaderBackColorMethod ()
251                 {
252                         DataGrid dg = new DataGrid ();
253                         DataGrid dg2 = new DataGrid ();
254                         dg2.HeaderBackColor = Color.Red;
255                         dg2.ResetHeaderBackColor ();
256                         Assert.AreEqual (dg.HeaderBackColor, dg2.HeaderBackColor, "A1");
257                 }
258
259                 [Test]
260                 public void ResetHeaderFontMethod ()
261                 {                       
262                 }
263
264                 [Test]
265                 public void ResetHeaderForeColorMethod ()
266                 {
267                         DataGrid dg = new DataGrid ();
268                         DataGrid dg2 = new DataGrid ();
269                         dg2.HeaderForeColor = Color.Red;
270                         dg2.ResetHeaderForeColor ();
271                         Assert.AreEqual (dg.HeaderForeColor, dg2.HeaderForeColor, "A1");                        
272                 }
273
274                 [Test]
275                 public void ResetLinkColorMethod ()
276                 {                                               
277                         DataGrid dg = new DataGrid ();
278                         DataGrid dg2 = new DataGrid ();
279                         dg2.LinkColor = Color.Red;
280                         dg2.ResetLinkColor ();
281                         Assert.AreEqual (dg.LinkColor, dg2.LinkColor, "A1");
282                 }
283
284                 [Test]
285                 public void ResetLinkHoverColor ()
286                 {
287                         DataGrid dg = new DataGrid ();
288                         DataGrid dg2 = new DataGrid ();
289                         dg2.LinkHoverColor = Color.Red;
290                         dg2.ResetLinkHoverColor ();
291                         Assert.AreEqual (dg.LinkHoverColor, dg2.LinkHoverColor, "A1");
292                 }
293
294                 [Test]          
295                 public void ResetSelectionBackColor ()
296                 {                       
297                         DataGrid dg = new DataGrid ();
298                         DataGrid dg2 = new DataGrid ();
299                         dg2.SelectionBackColor = Color.Red;
300                         dg2.ResetSelectionBackColor ();
301                         Assert.AreEqual (dg.SelectionBackColor, dg2.SelectionBackColor, "A1");
302                 }
303
304                 [Test]
305                 public void ResetSelectionForeColor ()
306                 {
307                         DataGrid dg = new DataGrid ();
308                         DataGrid dg2 = new DataGrid ();
309                         dg2.SelectionForeColor = Color.Red;
310                         dg2.ResetSelectionForeColor ();
311                         Assert.AreEqual (dg.SelectionForeColor, dg2.SelectionForeColor, "A1");
312                 }
313
314                 [Test] public void TestSetDataBinding ()
315                 {
316                         DataGrid dg = new DataGrid ();
317                         DataSet ds = new DataSet ("DataSet");
318                         DataTable dt = new DataTable ("DataTable");
319                         DataColumn dc = new DataColumn ("C");
320                         ds.Tables.Add (dt);
321
322                         dg.SetDataBinding (ds, "DataTable");
323                         
324                 }
325         }
326 }