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>
28 using System.Collections;
29 using System.ComponentModel;
31 using System.Windows.Forms;
33 using NUnit.Framework;
36 namespace MonoTests.System.Windows.Forms
40 class TestDataGrid : DataGrid
42 public TestDataGrid ()
47 public CurrencyManager Manager {
55 public class DataGridTest : TestHelper
57 private bool eventhandled;
61 public void TestDefaultValues ()
63 DataGrid dg = new DataGrid ();
65 Assert.AreEqual (true, dg.AllowNavigation, "AllowNavigation property");
66 Assert.AreEqual (true, dg.AllowSorting, "AllowSorting property");
67 Assert.AreEqual (BorderStyle.Fixed3D, dg.BorderStyle, "BorderStyle property");
68 Assert.AreEqual (string.Empty, dg.CaptionText, "CaptionText property");
69 Assert.AreEqual (true, dg.CaptionVisible, "CaptionVisible property");
70 Assert.AreEqual (true, dg.ColumnHeadersVisible, "ColumnHeadersVisible property");
71 Assert.AreEqual (new DataGridCell (), dg.CurrentCell, "CurrentCell property");
72 Assert.AreEqual (-1, dg.CurrentRowIndex, "CurrentRowIndex property");
73 Assert.AreEqual (string.Empty, dg.DataMember, "DataMember property");
74 Assert.AreEqual (null, dg.DataSource, "DataSource property");
75 Assert.AreEqual (0, dg.FirstVisibleColumn, "FirstVisibleColumn property");
76 Assert.AreEqual (false, dg.FlatMode, "FlatMode property");
77 Assert.AreEqual (DataGridLineStyle.Solid, dg.GridLineStyle, "GridLineStyle property");
78 Assert.AreEqual (DataGridParentRowsLabelStyle.Both, dg.ParentRowsLabelStyle, "ParentRowsLabelStyle property");
79 Assert.AreEqual (true, dg.ParentRowsVisible, "ParentRowsVisible property");
80 Assert.AreEqual (75, dg.PreferredColumnWidth, "PreferredColumnWidth property");
81 //Assert.AreEqual (16, dg.PreferredRowHeight, "PreferredRowHeight property");
82 Assert.AreEqual (false, dg.ReadOnly, "ReadOnly property");
83 Assert.AreEqual (true, dg.RowHeadersVisible, "RowHeadersVisible property");
84 Assert.AreEqual (35, dg.RowHeaderWidth, "RowHeaderWidth property");
85 Assert.AreEqual (null, dg.Site, "Site property");
86 Assert.AreEqual (string.Empty, dg.Text, "Text property");
87 Assert.AreEqual (0, dg.VisibleColumnCount, "VisibleColumnCount property");
90 Assert.IsFalse (dg.Font.Bold, "Font Bold");
92 Assert.IsTrue (dg.Font.IsSystemFont, "Font IsSystemFont");
94 Assert.IsFalse (dg.Font.Italic, "Font Italic");
95 Assert.IsFalse (dg.Font.Strikeout, "Font Strikeout");
96 Assert.IsFalse (dg.Font.Underline, "Font Underline");
100 public void TestAllowNavigationChangedEvent ()
102 DataGrid dg = new DataGrid ();
103 eventhandled = false;
104 dg.AllowNavigationChanged += new EventHandler (OnEventHandler);
105 dg.AllowNavigation = !dg.AllowNavigation;
106 Assert.AreEqual (true, eventhandled, "A1");
110 public void TestBackgroundColorChangedEvent ()
112 DataGrid dg = new DataGrid ();
113 eventhandled = false;
114 dg.BackgroundColorChanged += new EventHandler (OnEventHandler);
115 dg.BackgroundColor = Color.Red;
116 Assert.AreEqual (true, eventhandled, "A1");
120 public void TestBorderStyleChangedEvent ()
122 DataGrid dg = new DataGrid ();
123 eventhandled = false;
124 dg.BorderStyleChanged += new EventHandler (OnEventHandler);
125 dg.BorderStyle = BorderStyle.None;
126 Assert.AreEqual (true, eventhandled, "A1");
130 public void TestCaptionVisibleChangedEvent ()
132 DataGrid dg = new DataGrid ();
133 eventhandled = false;
134 dg.CaptionVisibleChanged += new EventHandler (OnEventHandler);
135 dg.CaptionVisible = !dg.CaptionVisible;
136 Assert.AreEqual (true, eventhandled, "A1");
140 public void TestFlatModeChangedEvent ()
142 DataGrid dg = new DataGrid ();
143 eventhandled = false;
144 dg.FlatModeChanged += new EventHandler (OnEventHandler);
145 dg.FlatMode = !dg.FlatMode;
146 Assert.AreEqual (true, eventhandled, "A1");
150 public void CaptionFont ()
152 DataGrid dg = new DataGrid ();
155 Assert.IsTrue (dg.CaptionFont.Bold, "#A1");
156 Assert.AreEqual (dg.CaptionFont.FontFamily, dg.Font.FontFamily, "#A2");
157 Assert.AreEqual (dg.CaptionFont.Height, dg.Font.Height, "#A3");
159 Assert.IsFalse(dg.CaptionFont.IsSystemFont, "#A4");
161 Assert.AreEqual (dg.CaptionFont.Italic, dg.Font.Italic, "#A5");
162 Assert.AreEqual (dg.CaptionFont.Name, dg.Font.Name, "#A6");
163 Assert.AreEqual (dg.CaptionFont.Size, dg.Font.Size, "#A7");
164 Assert.AreEqual (dg.CaptionFont.SizeInPoints, dg.Font.SizeInPoints, "#A8");
165 Assert.AreEqual (dg.CaptionFont.Strikeout, dg.Font.Strikeout, "#A9");
166 Assert.AreEqual (dg.CaptionFont.Underline, dg.Font.Underline, "#A10");
167 Assert.AreEqual (dg.CaptionFont.Unit, dg.Font.Unit, "#A11");
169 // modifying Font affects CaptionFont, except for FontStyle
170 dg.Font = new Font (dg.Font.FontFamily, 3, FontStyle.Italic);
171 Assert.IsTrue (dg.CaptionFont.Bold, "#B1");
172 Assert.IsFalse (dg.Font.Bold, "#B2");
173 Assert.IsFalse (dg.CaptionFont.Italic, "#B3");
174 Assert.IsTrue (dg.Font.Italic, "#B4");
175 Assert.AreEqual (3, dg.Font.SizeInPoints, "#B5");
176 Assert.AreEqual (dg.CaptionFont.SizeInPoints, dg.Font.SizeInPoints, "#B6");
178 // explicitly setting CaptionFont removes link between CaptionFont
180 dg.CaptionFont = dg.Font;
181 Assert.AreSame (dg.CaptionFont, dg.Font, "#C1");
182 dg.Font = new Font (dg.Font.FontFamily, 7, FontStyle.Bold);
183 Assert.IsFalse (dg.CaptionFont.Bold, "#C2");
184 Assert.IsTrue (dg.Font.Bold, "#C3");
185 Assert.AreEqual (7, dg.Font.SizeInPoints, "#C4");
186 Assert.AreEqual (3, dg.CaptionFont.SizeInPoints, "#C5");
190 public void HeaderFont ()
192 DataGrid dg = new DataGrid ();
193 dg.Font = new Font (dg.Font, FontStyle.Italic);
194 Assert.AreSame (dg.HeaderFont, dg.Font, "#1");
196 dg.HeaderFont = dg.Font;
197 Assert.AreSame (dg.HeaderFont, dg.Font, "#2");
199 dg.Font = new Font (dg.Font, FontStyle.Regular);
200 Assert.IsTrue (dg.HeaderFont.Italic, "#3");
201 Assert.IsFalse (dg.Font.Italic, "#4");
203 dg.ResetHeaderFont ();
204 Assert.AreSame (dg.HeaderFont, dg.Font, "#5");
209 public void TestParentRowsLabelStyleChangedEvent ()
211 DataGrid dg = new DataGrid ();
212 eventhandled = false;
213 dg.ParentRowsLabelStyleChanged += new EventHandler (OnEventHandler);
214 dg.ParentRowsLabelStyle = DataGridParentRowsLabelStyle.None;
215 Assert.AreEqual (true, eventhandled, "A1");
219 public void TestParentRowsVisibleChangedEvent ()
221 DataGrid dg = new DataGrid ();
222 eventhandled = false;
223 dg.ParentRowsVisibleChanged += new EventHandler (OnEventHandler);
224 dg.ParentRowsVisible = !dg.ParentRowsVisible;
225 Assert.AreEqual (true, eventhandled, "A1");
229 public void TestReadOnlyChangedEvent ()
231 DataGrid dg = new DataGrid ();
232 eventhandled = false;
233 dg.ReadOnlyChanged += new EventHandler (OnEventHandler);
234 dg.ReadOnly = !dg.ReadOnly;
235 Assert.AreEqual (true, eventhandled, "A1");
238 public void OnEventHandler (object sender, EventArgs e)
243 // Property exceptions
246 [ExpectedException (typeof (ArgumentException))]
247 public void GridLineColorException ()
249 DataGrid dg = new DataGrid ();
250 dg.GridLineColor = Color.Empty;
254 [ExpectedException (typeof (ArgumentException))]
255 public void HeaderBackColorException ()
257 DataGrid dg = new DataGrid ();
258 dg.HeaderBackColor = Color.Empty;
262 [ExpectedException (typeof (ArgumentException))]
263 public void PreferredColumnWidthException ()
265 DataGrid dg = new DataGrid ();
266 dg.PreferredColumnWidth = -1;
270 public void ResetAlternatingBackColor ()
272 DataGrid dg = new DataGrid ();
273 DataGrid dg2 = new DataGrid ();
274 dg2.AlternatingBackColor = Color.Red;
275 dg2.ResetAlternatingBackColor ();
276 Assert.AreEqual (dg.AlternatingBackColor, dg2.AlternatingBackColor, "A1");
279 // Test reset colour methods
281 public void ResetBackColorMethod ()
283 DataGrid dg = new DataGrid ();
284 DataGrid dg2 = new DataGrid ();
285 dg2.BackColor = Color.Red;
286 dg2.ResetBackColor ();
287 Assert.AreEqual (dg.BackColor, dg2.BackColor, "A1");
291 public void ResetForeColorMethod ()
293 DataGrid dg = new DataGrid ();
294 DataGrid dg2 = new DataGrid ();
295 dg2.ForeColor = Color.Red;
296 dg2.ResetForeColor ();
297 Assert.AreEqual (dg.ForeColor, dg2.ForeColor, "A1");
301 public void ResetGridLineColorMethod ()
303 DataGrid dg = new DataGrid ();
304 DataGrid dg2 = new DataGrid ();
305 dg2.GridLineColor = Color.Red;
306 dg2.ResetGridLineColor ();
307 Assert.AreEqual (dg.GridLineColor, dg2.GridLineColor, "A1");
311 public void ResetHeaderBackColorMethod ()
313 DataGrid dg = new DataGrid ();
314 DataGrid dg2 = new DataGrid ();
315 dg2.HeaderBackColor = Color.Red;
316 dg2.ResetHeaderBackColor ();
317 Assert.AreEqual (dg.HeaderBackColor, dg2.HeaderBackColor, "A1");
321 public void ResetHeaderFontMethod ()
326 public void ResetHeaderForeColorMethod ()
328 DataGrid dg = new DataGrid ();
329 DataGrid dg2 = new DataGrid ();
330 dg2.HeaderForeColor = Color.Red;
331 dg2.ResetHeaderForeColor ();
332 Assert.AreEqual (dg.HeaderForeColor, dg2.HeaderForeColor, "A1");
336 public void ResetLinkColorMethod ()
338 DataGrid dg = new DataGrid ();
339 DataGrid dg2 = new DataGrid ();
340 dg2.LinkColor = Color.Red;
341 dg2.ResetLinkColor ();
342 Assert.AreEqual (dg.LinkColor, dg2.LinkColor, "A1");
346 public void ResetLinkHoverColor ()
348 DataGrid dg = new DataGrid ();
349 DataGrid dg2 = new DataGrid ();
350 dg2.LinkHoverColor = Color.Red;
351 dg2.ResetLinkHoverColor ();
352 Assert.AreEqual (dg.LinkHoverColor, dg2.LinkHoverColor, "A1");
356 public void ResetSelectionBackColor ()
358 DataGrid dg = new DataGrid ();
359 DataGrid dg2 = new DataGrid ();
360 dg2.SelectionBackColor = Color.Red;
361 dg2.ResetSelectionBackColor ();
362 Assert.AreEqual (dg.SelectionBackColor, dg2.SelectionBackColor, "A1");
366 public void ResetSelectionForeColor ()
368 DataGrid dg = new DataGrid ();
369 DataGrid dg2 = new DataGrid ();
370 dg2.SelectionForeColor = Color.Red;
371 dg2.ResetSelectionForeColor ();
372 Assert.AreEqual (dg.SelectionForeColor, dg2.SelectionForeColor, "A1");
376 public void TestSetDataBinding ()
378 DataGrid dg = new DataGrid ();
379 DataSet ds = new DataSet ("DataSet");
380 DataTable dt = new DataTable ("DataTable");
383 dg.SetDataBinding (ds, "DataTable");
386 int data_source_changed_count = 0;
387 void OnDataSourceChanged (object sender, EventArgs e)
389 data_source_changed_count ++;
393 public void TestManager1 ()
395 TestDataGrid dg = new TestDataGrid ();
397 data_source_changed_count = 0;
398 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
400 /* make sure everything is fine to start with */
401 Assert.IsNull (dg.Manager, "A1");
402 Assert.IsNull (dg.DataSource, "A2");
403 Assert.AreEqual (dg.DataMember, "", "A3");
404 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A4");
408 public void TestManagerSetDataMember ()
410 TestDataGrid dg = new TestDataGrid ();
412 data_source_changed_count = 0;
413 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
415 /* set the datamember to something */
416 dg.DataMember = "hi there";
417 Assert.IsNull (dg.Manager, "A1");
418 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A2");
422 public void TestManagerSetDataSource ()
424 TestDataGrid dg = new TestDataGrid ();
426 data_source_changed_count = 0;
427 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
429 /* set our datasource to something */
430 dg = new TestDataGrid ();
431 DataSet ds = new DataSet ("DataSet");
432 DataTable dt = new DataTable ("DataTable");
436 Assert.IsNull (dg.Manager, "A1");
438 /* set the datamember to something as well.. anything yet? */
439 dg.DataMember = "DataTable";
440 Assert.IsNull (dg.Manager, "A2");
441 Assert.AreEqual (0, data_source_changed_count, "A3");
445 public void TestManagerCreateHandle ()
447 TestDataGrid dg = new TestDataGrid ();
449 data_source_changed_count = 0;
450 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
452 /* set our datasource to something */
453 dg = new TestDataGrid ();
454 DataSet ds = new DataSet ("DataSet");
455 DataTable dt = new DataTable ("DataTable");
460 /* cause the control to create its handle and
461 * see if that does anything */
462 Assert.IsNotNull (dg.Handle, "A1");
463 Assert.IsNull (dg.Manager, "A2");
464 Assert.AreEqual (0, data_source_changed_count, "A3");
468 public void TestManagerSetBindingContext ()
470 TestDataGrid dg = new TestDataGrid ();
472 data_source_changed_count = 0;
473 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
475 /* set our datasource to something */
476 dg = new TestDataGrid ();
477 DataSet ds = new DataSet ("DataSet");
478 DataTable dt = new DataTable ("DataTable");
482 dg.DataMember = "DataTable";
484 /* now set the BindingContext and see if something changes */
485 dg.BindingContext = new BindingContext ();
486 Assert.IsNotNull (dg.Manager, "A1");
487 Assert.AreEqual (0, data_source_changed_count, "A2");
491 public void TestManagerAfterSetBindingContext ()
493 TestDataGrid dg = new TestDataGrid ();
495 data_source_changed_count = 0;
496 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
498 dg.BindingContext = new BindingContext ();
500 /* set our datasource to something */
501 dg = new TestDataGrid ();
502 DataSet ds = new DataSet ("DataSet");
503 DataTable dt = new DataTable ("DataTable");
507 Assert.IsNull (dg.Manager, "A1");
509 dg.DataMember = "DataTable";
510 Assert.IsNull (dg.Manager, "A2");
512 dg.BindingContext = new BindingContext ();
513 Assert.IsNotNull (dg.Manager, "A3");
514 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A4");
518 public void TestManagerSetDataMemberAfterSetBindingContext ()
520 TestDataGrid dg = new TestDataGrid ();
522 data_source_changed_count = 0;
523 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
525 /* set our datasource to something */
526 dg = new TestDataGrid ();
527 DataSet ds = new DataSet ("DataSet");
528 DataTable dt = new DataTable ("DataTable");
533 dg.BindingContext = new BindingContext ();
534 Assert.AreEqual (0, data_source_changed_count, "A1");
536 CurrencyManager mgr = dg.Manager;
538 dg.DataMember = "DataTable";
539 Assert.IsNotNull (dg.Manager, "A2");
540 Assert.IsTrue (mgr != dg.Manager, "A3");
541 Assert.AreEqual (0, data_source_changed_count, "A4");
545 public void TestManagerSetDataSourceAfterSetBindingContext ()
547 TestDataGrid dg = new TestDataGrid ();
549 data_source_changed_count = 0;
550 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
552 /* set our datasource to something */
553 dg = new TestDataGrid ();
554 DataSet ds = new DataSet ("DataSet");
555 DataTable dt = new DataTable ("DataTable");
558 dg.DataMember = "DataTable";
560 dg.BindingContext = new BindingContext ();
561 Assert.AreEqual (0, data_source_changed_count, "A1");
563 CurrencyManager mgr = dg.Manager;
566 Assert.IsNotNull (dg.Manager, "A2");
567 Assert.IsTrue (mgr != dg.Manager, "A3");
568 Assert.AreEqual (0, data_source_changed_count, "A4");
572 public void TestManagerSetDataSourceAfterSetBindingContextWithHandle ()
574 TestDataGrid dg = new TestDataGrid ();
576 data_source_changed_count = 0;
577 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
579 /* set our datasource to something */
580 dg = new TestDataGrid ();
581 DataSet ds = new DataSet ("DataSet");
582 DataTable dt = new DataTable ("DataTable");
585 /* cause the control to create its handle and
586 * see if that does anything */
587 Assert.IsNotNull (dg.Handle, "A1");
589 dg.DataSource = new ArrayList ();
591 dg.BindingContext = new BindingContext ();
592 Assert.AreEqual (0, data_source_changed_count, "A2");
595 Assert.AreEqual (0, data_source_changed_count, "A3");
599 public void TestManagerSetDataSourceWithEmptyStyle ()
601 TestDataGrid dg = new TestDataGrid ();
602 dg.BindingContext = new BindingContext ();
604 DataSet ds = new DataSet ("DataSet");
605 DataTable dt = new DataTable ("MyTable");
606 dt.Columns.Add ("A", typeof (string));
610 // Add the style for the table we have, but leave it empty
611 // - this is, no column styles
612 DataGridTableStyle table_style = new DataGridTableStyle ();
613 table_style.MappingName = "MyTable";
614 dg.TableStyles.Add (table_style);
616 Assert.AreEqual (0, table_style.GridColumnStyles.Count, "#A1");
620 Assert.AreEqual (1, table_style.GridColumnStyles.Count, "#B1");