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;
37 namespace MonoTests.System.Windows.Forms
41 class TestDataGrid : DataGrid
43 public TestDataGrid ()
48 public CurrencyManager Manager {
56 public class DataGridTest : TestHelper
58 private bool eventhandled;
62 public void TestDefaultValues ()
64 DataGrid dg = new DataGrid ();
66 Assert.AreEqual (true, dg.AllowNavigation, "AllowNavigation property");
67 Assert.AreEqual (true, dg.AllowSorting, "AllowSorting property");
68 Assert.AreEqual (BorderStyle.Fixed3D, dg.BorderStyle, "BorderStyle property");
69 Assert.AreEqual (string.Empty, dg.CaptionText, "CaptionText property");
70 Assert.AreEqual (true, dg.CaptionVisible, "CaptionVisible property");
71 Assert.AreEqual (true, dg.ColumnHeadersVisible, "ColumnHeadersVisible property");
72 Assert.AreEqual (new DataGridCell (), dg.CurrentCell, "CurrentCell property");
73 Assert.AreEqual (-1, dg.CurrentRowIndex, "CurrentRowIndex property");
74 Assert.AreEqual (string.Empty, dg.DataMember, "DataMember property");
75 Assert.AreEqual (null, dg.DataSource, "DataSource property");
76 Assert.AreEqual (0, dg.FirstVisibleColumn, "FirstVisibleColumn property");
77 Assert.AreEqual (false, dg.FlatMode, "FlatMode property");
78 Assert.AreEqual (DataGridLineStyle.Solid, dg.GridLineStyle, "GridLineStyle property");
79 Assert.AreEqual (DataGridParentRowsLabelStyle.Both, dg.ParentRowsLabelStyle, "ParentRowsLabelStyle property");
80 Assert.AreEqual (true, dg.ParentRowsVisible, "ParentRowsVisible property");
81 Assert.AreEqual (75, dg.PreferredColumnWidth, "PreferredColumnWidth property");
82 //Assert.AreEqual (16, dg.PreferredRowHeight, "PreferredRowHeight property");
83 Assert.AreEqual (false, dg.ReadOnly, "ReadOnly property");
84 Assert.AreEqual (true, dg.RowHeadersVisible, "RowHeadersVisible property");
85 Assert.AreEqual (35, dg.RowHeaderWidth, "RowHeaderWidth property");
86 Assert.AreEqual (null, dg.Site, "Site property");
87 Assert.AreEqual (string.Empty, dg.Text, "Text property");
88 Assert.AreEqual (0, dg.VisibleColumnCount, "VisibleColumnCount property");
91 Assert.IsFalse (dg.Font.Bold, "Font Bold");
93 Assert.IsTrue (dg.Font.IsSystemFont, "Font IsSystemFont");
95 Assert.IsFalse (dg.Font.Italic, "Font Italic");
96 Assert.IsFalse (dg.Font.Strikeout, "Font Strikeout");
97 Assert.IsFalse (dg.Font.Underline, "Font Underline");
101 public void TestAllowNavigationChangedEvent ()
103 DataGrid dg = new DataGrid ();
104 eventhandled = false;
105 dg.AllowNavigationChanged += new EventHandler (OnEventHandler);
106 dg.AllowNavigation = !dg.AllowNavigation;
107 Assert.AreEqual (true, eventhandled, "A1");
111 public void TestBackgroundColorChangedEvent ()
113 DataGrid dg = new DataGrid ();
114 eventhandled = false;
115 dg.BackgroundColorChanged += new EventHandler (OnEventHandler);
116 dg.BackgroundColor = Color.Red;
117 Assert.AreEqual (true, eventhandled, "A1");
121 public void TestBorderStyleChangedEvent ()
123 DataGrid dg = new DataGrid ();
124 eventhandled = false;
125 dg.BorderStyleChanged += new EventHandler (OnEventHandler);
126 dg.BorderStyle = BorderStyle.None;
127 Assert.AreEqual (true, eventhandled, "A1");
131 public void TestCaptionVisibleChangedEvent ()
133 DataGrid dg = new DataGrid ();
134 eventhandled = false;
135 dg.CaptionVisibleChanged += new EventHandler (OnEventHandler);
136 dg.CaptionVisible = !dg.CaptionVisible;
137 Assert.AreEqual (true, eventhandled, "A1");
141 public void TestFlatModeChangedEvent ()
143 DataGrid dg = new DataGrid ();
144 eventhandled = false;
145 dg.FlatModeChanged += new EventHandler (OnEventHandler);
146 dg.FlatMode = !dg.FlatMode;
147 Assert.AreEqual (true, eventhandled, "A1");
151 public void CaptionFont ()
153 DataGrid dg = new DataGrid ();
156 Assert.IsTrue (dg.CaptionFont.Bold, "#A1");
157 Assert.AreEqual (dg.CaptionFont.FontFamily, dg.Font.FontFamily, "#A2");
158 Assert.AreEqual (dg.CaptionFont.Height, dg.Font.Height, "#A3");
160 Assert.IsFalse(dg.CaptionFont.IsSystemFont, "#A4");
162 Assert.AreEqual (dg.CaptionFont.Italic, dg.Font.Italic, "#A5");
163 Assert.AreEqual (dg.CaptionFont.Name, dg.Font.Name, "#A6");
164 Assert.AreEqual (dg.CaptionFont.Size, dg.Font.Size, "#A7");
165 Assert.AreEqual (dg.CaptionFont.SizeInPoints, dg.Font.SizeInPoints, "#A8");
166 Assert.AreEqual (dg.CaptionFont.Strikeout, dg.Font.Strikeout, "#A9");
167 Assert.AreEqual (dg.CaptionFont.Underline, dg.Font.Underline, "#A10");
168 Assert.AreEqual (dg.CaptionFont.Unit, dg.Font.Unit, "#A11");
170 // modifying Font affects CaptionFont, except for FontStyle
171 dg.Font = new Font (dg.Font.FontFamily, 3, FontStyle.Italic);
172 Assert.IsTrue (dg.CaptionFont.Bold, "#B1");
173 Assert.IsFalse (dg.Font.Bold, "#B2");
174 Assert.IsFalse (dg.CaptionFont.Italic, "#B3");
175 Assert.IsTrue (dg.Font.Italic, "#B4");
176 Assert.AreEqual (3, dg.Font.SizeInPoints, "#B5");
177 Assert.AreEqual (dg.CaptionFont.SizeInPoints, dg.Font.SizeInPoints, "#B6");
179 // explicitly setting CaptionFont removes link between CaptionFont
181 dg.CaptionFont = dg.Font;
182 Assert.AreSame (dg.CaptionFont, dg.Font, "#C1");
183 dg.Font = new Font (dg.Font.FontFamily, 7, FontStyle.Bold);
184 Assert.IsFalse (dg.CaptionFont.Bold, "#C2");
185 Assert.IsTrue (dg.Font.Bold, "#C3");
186 Assert.AreEqual (7, dg.Font.SizeInPoints, "#C4");
187 Assert.AreEqual (3, dg.CaptionFont.SizeInPoints, "#C5");
191 public void HeaderFont ()
193 DataGrid dg = new DataGrid ();
194 dg.Font = new Font (dg.Font, FontStyle.Italic);
195 Assert.AreSame (dg.HeaderFont, dg.Font, "#1");
197 dg.HeaderFont = dg.Font;
198 Assert.AreSame (dg.HeaderFont, dg.Font, "#2");
200 dg.Font = new Font (dg.Font, FontStyle.Regular);
201 Assert.IsTrue (dg.HeaderFont.Italic, "#3");
202 Assert.IsFalse (dg.Font.Italic, "#4");
204 dg.ResetHeaderFont ();
205 Assert.AreSame (dg.HeaderFont, dg.Font, "#5");
210 public void TestParentRowsLabelStyleChangedEvent ()
212 DataGrid dg = new DataGrid ();
213 eventhandled = false;
214 dg.ParentRowsLabelStyleChanged += new EventHandler (OnEventHandler);
215 dg.ParentRowsLabelStyle = DataGridParentRowsLabelStyle.None;
216 Assert.AreEqual (true, eventhandled, "A1");
220 public void TestParentRowsVisibleChangedEvent ()
222 DataGrid dg = new DataGrid ();
223 eventhandled = false;
224 dg.ParentRowsVisibleChanged += new EventHandler (OnEventHandler);
225 dg.ParentRowsVisible = !dg.ParentRowsVisible;
226 Assert.AreEqual (true, eventhandled, "A1");
230 public void TestReadOnlyChangedEvent ()
232 DataGrid dg = new DataGrid ();
233 eventhandled = false;
234 dg.ReadOnlyChanged += new EventHandler (OnEventHandler);
235 dg.ReadOnly = !dg.ReadOnly;
236 Assert.AreEqual (true, eventhandled, "A1");
239 public void OnEventHandler (object sender, EventArgs e)
244 // Property exceptions
247 [ExpectedException (typeof (ArgumentException))]
248 public void GridLineColorException ()
250 DataGrid dg = new DataGrid ();
251 dg.GridLineColor = Color.Empty;
255 [ExpectedException (typeof (ArgumentException))]
256 public void HeaderBackColorException ()
258 DataGrid dg = new DataGrid ();
259 dg.HeaderBackColor = Color.Empty;
263 [ExpectedException (typeof (ArgumentException))]
264 public void PreferredColumnWidthException ()
266 DataGrid dg = new DataGrid ();
267 dg.PreferredColumnWidth = -1;
271 public void ResetAlternatingBackColor ()
273 DataGrid dg = new DataGrid ();
274 DataGrid dg2 = new DataGrid ();
275 dg2.AlternatingBackColor = Color.Red;
276 dg2.ResetAlternatingBackColor ();
277 Assert.AreEqual (dg.AlternatingBackColor, dg2.AlternatingBackColor, "A1");
280 // Test reset colour methods
282 public void ResetBackColorMethod ()
284 DataGrid dg = new DataGrid ();
285 DataGrid dg2 = new DataGrid ();
286 dg2.BackColor = Color.Red;
287 dg2.ResetBackColor ();
288 Assert.AreEqual (dg.BackColor, dg2.BackColor, "A1");
292 public void ResetForeColorMethod ()
294 DataGrid dg = new DataGrid ();
295 DataGrid dg2 = new DataGrid ();
296 dg2.ForeColor = Color.Red;
297 dg2.ResetForeColor ();
298 Assert.AreEqual (dg.ForeColor, dg2.ForeColor, "A1");
302 public void ResetGridLineColorMethod ()
304 DataGrid dg = new DataGrid ();
305 DataGrid dg2 = new DataGrid ();
306 dg2.GridLineColor = Color.Red;
307 dg2.ResetGridLineColor ();
308 Assert.AreEqual (dg.GridLineColor, dg2.GridLineColor, "A1");
312 public void ResetHeaderBackColorMethod ()
314 DataGrid dg = new DataGrid ();
315 DataGrid dg2 = new DataGrid ();
316 dg2.HeaderBackColor = Color.Red;
317 dg2.ResetHeaderBackColor ();
318 Assert.AreEqual (dg.HeaderBackColor, dg2.HeaderBackColor, "A1");
322 public void ResetHeaderFontMethod ()
327 public void ResetHeaderForeColorMethod ()
329 DataGrid dg = new DataGrid ();
330 DataGrid dg2 = new DataGrid ();
331 dg2.HeaderForeColor = Color.Red;
332 dg2.ResetHeaderForeColor ();
333 Assert.AreEqual (dg.HeaderForeColor, dg2.HeaderForeColor, "A1");
337 public void ResetLinkColorMethod ()
339 DataGrid dg = new DataGrid ();
340 DataGrid dg2 = new DataGrid ();
341 dg2.LinkColor = Color.Red;
342 dg2.ResetLinkColor ();
343 Assert.AreEqual (dg.LinkColor, dg2.LinkColor, "A1");
347 public void ResetLinkHoverColor ()
349 DataGrid dg = new DataGrid ();
350 DataGrid dg2 = new DataGrid ();
351 dg2.LinkHoverColor = Color.Red;
352 dg2.ResetLinkHoverColor ();
353 Assert.AreEqual (dg.LinkHoverColor, dg2.LinkHoverColor, "A1");
357 public void ResetSelectionBackColor ()
359 DataGrid dg = new DataGrid ();
360 DataGrid dg2 = new DataGrid ();
361 dg2.SelectionBackColor = Color.Red;
362 dg2.ResetSelectionBackColor ();
363 Assert.AreEqual (dg.SelectionBackColor, dg2.SelectionBackColor, "A1");
367 public void ResetSelectionForeColor ()
369 DataGrid dg = new DataGrid ();
370 DataGrid dg2 = new DataGrid ();
371 dg2.SelectionForeColor = Color.Red;
372 dg2.ResetSelectionForeColor ();
373 Assert.AreEqual (dg.SelectionForeColor, dg2.SelectionForeColor, "A1");
377 public void TestSetDataBinding ()
379 DataGrid dg = new DataGrid ();
380 DataSet ds = new DataSet ("DataSet");
381 DataTable dt = new DataTable ("DataTable");
384 dg.SetDataBinding (ds, "DataTable");
387 int data_source_changed_count = 0;
388 void OnDataSourceChanged (object sender, EventArgs e)
390 data_source_changed_count ++;
394 public void TestManager1 ()
396 TestDataGrid dg = new TestDataGrid ();
398 data_source_changed_count = 0;
399 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
401 /* make sure everything is fine to start with */
402 Assert.IsNull (dg.Manager, "A1");
403 Assert.IsNull (dg.DataSource, "A2");
404 Assert.AreEqual (dg.DataMember, "", "A3");
405 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A4");
409 public void TestManagerSetDataMember ()
411 TestDataGrid dg = new TestDataGrid ();
413 data_source_changed_count = 0;
414 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
416 /* set the datamember to something */
417 dg.DataMember = "hi there";
418 Assert.IsNull (dg.Manager, "A1");
419 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A2");
423 public void TestManagerSetDataSource ()
425 TestDataGrid dg = new TestDataGrid ();
427 data_source_changed_count = 0;
428 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
430 /* set our datasource to something */
431 dg = new TestDataGrid ();
432 DataSet ds = new DataSet ("DataSet");
433 DataTable dt = new DataTable ("DataTable");
437 Assert.IsNull (dg.Manager, "A1");
439 /* set the datamember to something as well.. anything yet? */
440 dg.DataMember = "DataTable";
441 Assert.IsNull (dg.Manager, "A2");
442 Assert.AreEqual (0, data_source_changed_count, "A3");
446 public void TestManagerCreateHandle ()
448 TestDataGrid dg = new TestDataGrid ();
450 data_source_changed_count = 0;
451 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
453 /* set our datasource to something */
454 dg = new TestDataGrid ();
455 DataSet ds = new DataSet ("DataSet");
456 DataTable dt = new DataTable ("DataTable");
461 /* cause the control to create its handle and
462 * see if that does anything */
463 Assert.IsNotNull (dg.Handle, "A1");
464 Assert.IsNull (dg.Manager, "A2");
465 Assert.AreEqual (0, data_source_changed_count, "A3");
469 public void TestManagerSetBindingContext ()
471 TestDataGrid dg = new TestDataGrid ();
473 data_source_changed_count = 0;
474 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
476 /* set our datasource to something */
477 dg = new TestDataGrid ();
478 DataSet ds = new DataSet ("DataSet");
479 DataTable dt = new DataTable ("DataTable");
483 dg.DataMember = "DataTable";
485 /* now set the BindingContext and see if something changes */
486 dg.BindingContext = new BindingContext ();
487 Assert.IsNotNull (dg.Manager, "A1");
488 Assert.AreEqual (0, data_source_changed_count, "A2");
492 public void TestManagerAfterSetBindingContext ()
494 TestDataGrid dg = new TestDataGrid ();
496 data_source_changed_count = 0;
497 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
499 dg.BindingContext = new BindingContext ();
501 /* set our datasource to something */
502 dg = new TestDataGrid ();
503 DataSet ds = new DataSet ("DataSet");
504 DataTable dt = new DataTable ("DataTable");
508 Assert.IsNull (dg.Manager, "A1");
510 dg.DataMember = "DataTable";
511 Assert.IsNull (dg.Manager, "A2");
513 dg.BindingContext = new BindingContext ();
514 Assert.IsNotNull (dg.Manager, "A3");
515 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A4");
519 public void TestManagerSetDataMemberAfterSetBindingContext ()
521 TestDataGrid dg = new TestDataGrid ();
523 data_source_changed_count = 0;
524 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
526 /* set our datasource to something */
527 dg = new TestDataGrid ();
528 DataSet ds = new DataSet ("DataSet");
529 DataTable dt = new DataTable ("DataTable");
534 dg.BindingContext = new BindingContext ();
535 Assert.AreEqual (0, data_source_changed_count, "A1");
537 CurrencyManager mgr = dg.Manager;
539 dg.DataMember = "DataTable";
540 Assert.IsNotNull (dg.Manager, "A2");
541 Assert.IsTrue (mgr != dg.Manager, "A3");
542 Assert.AreEqual (0, data_source_changed_count, "A4");
546 public void TestManagerSetDataSourceAfterSetBindingContext ()
548 TestDataGrid dg = new TestDataGrid ();
550 data_source_changed_count = 0;
551 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
553 /* set our datasource to something */
554 dg = new TestDataGrid ();
555 DataSet ds = new DataSet ("DataSet");
556 DataTable dt = new DataTable ("DataTable");
559 dg.DataMember = "DataTable";
561 dg.BindingContext = new BindingContext ();
562 Assert.AreEqual (0, data_source_changed_count, "A1");
564 CurrencyManager mgr = dg.Manager;
567 Assert.IsNotNull (dg.Manager, "A2");
568 Assert.IsTrue (mgr != dg.Manager, "A3");
569 Assert.AreEqual (0, data_source_changed_count, "A4");
573 public void TestManagerSetDataSourceAfterSetBindingContextWithHandle ()
575 TestDataGrid dg = new TestDataGrid ();
577 data_source_changed_count = 0;
578 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
580 /* set our datasource to something */
581 dg = new TestDataGrid ();
582 DataSet ds = new DataSet ("DataSet");
583 DataTable dt = new DataTable ("DataTable");
586 /* cause the control to create its handle and
587 * see if that does anything */
588 Assert.IsNotNull (dg.Handle, "A1");
590 dg.DataSource = new ArrayList ();
592 dg.BindingContext = new BindingContext ();
593 Assert.AreEqual (0, data_source_changed_count, "A2");
596 Assert.AreEqual (0, data_source_changed_count, "A3");
600 public void TestManagerSetDataSourceWithEmptyStyle ()
602 TestDataGrid dg = new TestDataGrid ();
603 dg.BindingContext = new BindingContext ();
605 DataSet ds = new DataSet ("DataSet");
606 DataTable dt = new DataTable ("MyTable");
607 dt.Columns.Add ("A", typeof (string));
611 // Add the style for the table we have, but leave it empty
612 // - this is, no column styles
613 DataGridTableStyle table_style = new DataGridTableStyle ();
614 table_style.MappingName = "MyTable";
615 dg.TableStyles.Add (table_style);
617 Assert.AreEqual (0, table_style.GridColumnStyles.Count, "#A1");
621 Assert.AreEqual (1, table_style.GridColumnStyles.Count, "#B1");
624 public class ClickableDataGrid : DataGrid
626 public void ClickGrid (int X, int Y)
628 MouseEventArgs me = new MouseEventArgs (
638 public class Form5487 : Form
640 private ClickableDataGrid dataGrid1;
641 private Container components = null;
645 InitializeComponent ();
648 protected override void Dispose (bool disposing)
651 if (components != null) {
652 components.Dispose ();
655 base.Dispose (disposing);
658 private void InitializeComponent ()
660 this.dataGrid1 = new ClickableDataGrid ();
661 ((ISupportInitialize)(this.dataGrid1)).BeginInit ();
662 this.SuspendLayout ();
663 this.dataGrid1.DataMember = "";
664 this.dataGrid1.HeaderForeColor = SystemColors.ControlText;
665 this.dataGrid1.Location = new Point (16, 16);
666 this.dataGrid1.Name = "dataGrid1";
667 this.dataGrid1.Size = new Size (624, 440);
668 this.dataGrid1.TabIndex = 0;
669 this.AutoScaleBaseSize = new Size (5, 13);
670 this.ClientSize = new Size (656, 470);
671 this.Controls.Add (this.dataGrid1);
674 this.Shown += new EventHandler (this.Form1_Load);
675 ((ISupportInitialize)(this.dataGrid1)).EndInit ();
676 this.ResumeLayout (false);
680 private void Form1_Load (object sender, EventArgs e)
682 DataSet ds = new DataSet ();
683 String XMLString = "";
686 XMLString += "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
687 XMLString += "<pa><pb><pc><pd><id>1</id>";
688 XMLString += "</pd></pc><pc><pd><id>1</id>";
689 XMLString += "</pd><pd><id>1</id></pd><pd>";
690 XMLString += "<id>1</id></pd><pd><id>1</id>";
691 XMLString += "</pd><pd><id>1</id></pd><pd>";
692 XMLString += "<id>1</id></pd><pd><id>1</id>";
693 XMLString += "</pd><pd><id>1</id></pd></pc>";
694 XMLString += "</pb></pa>";
695 XMLTR = new XmlTextReader (XMLString,
696 XmlNodeType.Document, null);
697 XMLTR.ReadOuterXml ();
699 this.dataGrid1.DataSource = ds;
700 this.dataGrid1.ClickGrid (25, 45);
701 Application.DoEvents ();
702 this.dataGrid1.ClickGrid (46, 73);
703 Application.DoEvents ();
704 this.dataGrid1.NavigateBack ();
709 public void Bug5487AndRelated ()
711 //this should crash on fail
712 Application.Run (new Form5487 ());