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
57 private bool eventhandled;
60 public void Clean() {}
63 public void GetReady ()
68 public void TestDefaultValues ()
70 DataGrid dg = new DataGrid ();
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");
97 Assert.IsFalse (dg.Font.Bold, "Font Bold");
99 Assert.IsTrue (dg.Font.IsSystemFont, "Font IsSystemFont");
101 Assert.IsFalse (dg.Font.Italic, "Font Italic");
102 Assert.IsFalse (dg.Font.Strikeout, "Font Strikeout");
103 Assert.IsFalse (dg.Font.Underline, "Font Underline");
107 public void TestAllowNavigationChangedEvent ()
109 DataGrid dg = new DataGrid ();
110 eventhandled = false;
111 dg.AllowNavigationChanged += new EventHandler (OnEventHandler);
112 dg.AllowNavigation = !dg.AllowNavigation;
113 Assert.AreEqual (true, eventhandled, "A1");
117 public void TestBackgroundColorChangedEvent ()
119 DataGrid dg = new DataGrid ();
120 eventhandled = false;
121 dg.BackgroundColorChanged += new EventHandler (OnEventHandler);
122 dg.BackgroundColor = Color.Red;
123 Assert.AreEqual (true, eventhandled, "A1");
127 public void TestBorderStyleChangedEvent ()
129 DataGrid dg = new DataGrid ();
130 eventhandled = false;
131 dg.BorderStyleChanged += new EventHandler (OnEventHandler);
132 dg.BorderStyle = BorderStyle.None;
133 Assert.AreEqual (true, eventhandled, "A1");
137 public void TestCaptionVisibleChangedEvent ()
139 DataGrid dg = new DataGrid ();
140 eventhandled = false;
141 dg.CaptionVisibleChanged += new EventHandler (OnEventHandler);
142 dg.CaptionVisible = !dg.CaptionVisible;
143 Assert.AreEqual (true, eventhandled, "A1");
147 public void TestFlatModeChangedEvent ()
149 DataGrid dg = new DataGrid ();
150 eventhandled = false;
151 dg.FlatModeChanged += new EventHandler (OnEventHandler);
152 dg.FlatMode = !dg.FlatMode;
153 Assert.AreEqual (true, eventhandled, "A1");
157 public void CaptionFont ()
159 DataGrid dg = new DataGrid ();
162 Assert.IsTrue (dg.CaptionFont.Bold, "#A1");
163 Assert.AreEqual (dg.CaptionFont.FontFamily, dg.Font.FontFamily, "#A2");
164 Assert.AreEqual (dg.CaptionFont.Height, dg.Font.Height, "#A3");
166 Assert.AreEqual (dg.CaptionFont.IsSystemFont, dg.Font.IsSystemFont, "#A4");
168 Assert.AreEqual (dg.CaptionFont.Italic, dg.Font.Italic, "#A5");
169 Assert.AreEqual (dg.CaptionFont.Name, dg.Font.Name, "#A6");
170 Assert.AreEqual (dg.CaptionFont.Size, dg.Font.Size, "#A7");
171 Assert.AreEqual (dg.CaptionFont.SizeInPoints, dg.Font.SizeInPoints, "#A8");
172 Assert.AreEqual (dg.CaptionFont.Strikeout, dg.Font.Strikeout, "#A9");
173 Assert.AreEqual (dg.CaptionFont.Underline, dg.Font.Underline, "#A10");
174 Assert.AreEqual (dg.CaptionFont.Unit, dg.Font.Unit, "#A11");
176 // modifying Font affects CaptionFont, except for FontStyle
177 dg.Font = new Font (dg.Font.FontFamily, 3, FontStyle.Italic);
178 Assert.IsTrue (dg.CaptionFont.Bold, "#B1");
179 Assert.IsFalse (dg.Font.Bold, "#B2");
180 Assert.IsFalse (dg.CaptionFont.Italic, "#B3");
181 Assert.IsTrue (dg.Font.Italic, "#B4");
182 Assert.AreEqual (3, dg.Font.SizeInPoints, "#B5");
183 Assert.AreEqual (dg.CaptionFont.SizeInPoints, dg.Font.SizeInPoints, "#B6");
185 // explicitly setting CaptionFont removes link between CaptionFont
187 dg.CaptionFont = dg.Font;
188 Assert.AreSame (dg.CaptionFont, dg.Font, "#C1");
189 dg.Font = new Font (dg.Font.FontFamily, 7, FontStyle.Bold);
190 Assert.IsFalse (dg.CaptionFont.Bold, "#C2");
191 Assert.IsTrue (dg.Font.Bold, "#C3");
192 Assert.AreEqual (7, dg.Font.SizeInPoints, "#C4");
193 Assert.AreEqual (3, dg.CaptionFont.SizeInPoints, "#C5");
197 [NUnit.Framework.Category ("NotWorking")]
198 public void HeaderFont ()
200 DataGrid dg = new DataGrid ();
201 dg.Font = new Font (dg.Font, FontStyle.Italic);
202 Assert.AreSame (dg.HeaderFont, dg.Font, "#1");
204 // explicitly setting HeaderFont removes link between HeaderFont
206 dg.HeaderFont = dg.Font;
207 Assert.AreSame (dg.HeaderFont, dg.Font, "#2");
208 dg.Font = new Font (dg.Font, FontStyle.Regular);
209 Assert.IsTrue (dg.HeaderFont.Italic, "#3");
210 Assert.IsFalse (dg.Font.Italic, "#4");
214 public void TestParentRowsLabelStyleChangedEvent ()
216 DataGrid dg = new DataGrid ();
217 eventhandled = false;
218 dg.ParentRowsLabelStyleChanged += new EventHandler (OnEventHandler);
219 dg.ParentRowsLabelStyle = DataGridParentRowsLabelStyle.None;
220 Assert.AreEqual (true, eventhandled, "A1");
224 public void TestParentRowsVisibleChangedEvent ()
226 DataGrid dg = new DataGrid ();
227 eventhandled = false;
228 dg.ParentRowsVisibleChanged += new EventHandler (OnEventHandler);
229 dg.ParentRowsVisible = !dg.ParentRowsVisible;
230 Assert.AreEqual (true, eventhandled, "A1");
234 public void TestReadOnlyChangedEvent ()
236 DataGrid dg = new DataGrid ();
237 eventhandled = false;
238 dg.ReadOnlyChanged += new EventHandler (OnEventHandler);
239 dg.ReadOnly = !dg.ReadOnly;
240 Assert.AreEqual (true, eventhandled, "A1");
243 public void OnEventHandler (object sender, EventArgs e)
248 // Property exceptions
251 [ExpectedException (typeof (ArgumentException))]
252 public void GridLineColorException ()
254 DataGrid dg = new DataGrid ();
255 dg.GridLineColor = Color.Empty;
259 [ExpectedException (typeof (ArgumentException))]
260 public void HeaderBackColorException ()
262 DataGrid dg = new DataGrid ();
263 dg.HeaderBackColor = Color.Empty;
267 [ExpectedException (typeof (ArgumentException))]
268 public void PreferredColumnWidthException ()
270 DataGrid dg = new DataGrid ();
271 dg.PreferredColumnWidth = -1;
275 public void ResetAlternatingBackColor ()
277 DataGrid dg = new DataGrid ();
278 DataGrid dg2 = new DataGrid ();
279 dg2.AlternatingBackColor = Color.Red;
280 dg2.ResetAlternatingBackColor ();
281 Assert.AreEqual (dg.AlternatingBackColor, dg2.AlternatingBackColor, "A1");
284 // Test reset colour methods
286 public void ResetBackColorMethod ()
288 DataGrid dg = new DataGrid ();
289 DataGrid dg2 = new DataGrid ();
290 dg2.BackColor = Color.Red;
291 dg2.ResetBackColor ();
292 Assert.AreEqual (dg.BackColor, dg2.BackColor, "A1");
296 public void ResetForeColorMethod ()
298 DataGrid dg = new DataGrid ();
299 DataGrid dg2 = new DataGrid ();
300 dg2.ForeColor = Color.Red;
301 dg2.ResetForeColor ();
302 Assert.AreEqual (dg.ForeColor, dg2.ForeColor, "A1");
306 public void ResetGridLineColorMethod ()
308 DataGrid dg = new DataGrid ();
309 DataGrid dg2 = new DataGrid ();
310 dg2.GridLineColor = Color.Red;
311 dg2.ResetGridLineColor ();
312 Assert.AreEqual (dg.GridLineColor, dg2.GridLineColor, "A1");
316 public void ResetHeaderBackColorMethod ()
318 DataGrid dg = new DataGrid ();
319 DataGrid dg2 = new DataGrid ();
320 dg2.HeaderBackColor = Color.Red;
321 dg2.ResetHeaderBackColor ();
322 Assert.AreEqual (dg.HeaderBackColor, dg2.HeaderBackColor, "A1");
326 public void ResetHeaderFontMethod ()
331 public void ResetHeaderForeColorMethod ()
333 DataGrid dg = new DataGrid ();
334 DataGrid dg2 = new DataGrid ();
335 dg2.HeaderForeColor = Color.Red;
336 dg2.ResetHeaderForeColor ();
337 Assert.AreEqual (dg.HeaderForeColor, dg2.HeaderForeColor, "A1");
341 public void ResetLinkColorMethod ()
343 DataGrid dg = new DataGrid ();
344 DataGrid dg2 = new DataGrid ();
345 dg2.LinkColor = Color.Red;
346 dg2.ResetLinkColor ();
347 Assert.AreEqual (dg.LinkColor, dg2.LinkColor, "A1");
351 public void ResetLinkHoverColor ()
353 DataGrid dg = new DataGrid ();
354 DataGrid dg2 = new DataGrid ();
355 dg2.LinkHoverColor = Color.Red;
356 dg2.ResetLinkHoverColor ();
357 Assert.AreEqual (dg.LinkHoverColor, dg2.LinkHoverColor, "A1");
361 public void ResetSelectionBackColor ()
363 DataGrid dg = new DataGrid ();
364 DataGrid dg2 = new DataGrid ();
365 dg2.SelectionBackColor = Color.Red;
366 dg2.ResetSelectionBackColor ();
367 Assert.AreEqual (dg.SelectionBackColor, dg2.SelectionBackColor, "A1");
371 public void ResetSelectionForeColor ()
373 DataGrid dg = new DataGrid ();
374 DataGrid dg2 = new DataGrid ();
375 dg2.SelectionForeColor = Color.Red;
376 dg2.ResetSelectionForeColor ();
377 Assert.AreEqual (dg.SelectionForeColor, dg2.SelectionForeColor, "A1");
381 public void TestSetDataBinding ()
383 DataGrid dg = new DataGrid ();
384 DataSet ds = new DataSet ("DataSet");
385 DataTable dt = new DataTable ("DataTable");
386 DataColumn dc = new DataColumn ("C");
389 dg.SetDataBinding (ds, "DataTable");
392 int data_source_changed_count = 0;
393 void OnDataSourceChanged (object sender, EventArgs e)
395 data_source_changed_count ++;
399 public void TestManager1 ()
401 TestDataGrid dg = new TestDataGrid ();
403 data_source_changed_count = 0;
404 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
406 /* make sure everything is fine to start with */
407 Assert.IsNull (dg.Manager, "A1");
408 Assert.IsNull (dg.DataSource, "A2");
409 Assert.AreEqual (dg.DataMember, "", "A3");
410 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A4");
414 public void TestManagerSetDataMember ()
416 TestDataGrid dg = new TestDataGrid ();
418 data_source_changed_count = 0;
419 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
421 /* set the datamember to something */
422 dg.DataMember = "hi there";
423 Assert.IsNull (dg.Manager, "A1");
424 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A2");
428 public void TestManagerSetDataSource ()
430 TestDataGrid dg = new TestDataGrid ();
432 data_source_changed_count = 0;
433 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
435 /* set our datasource to something */
436 dg = new TestDataGrid ();
437 DataSet ds = new DataSet ("DataSet");
438 DataTable dt = new DataTable ("DataTable");
439 DataColumn dc = new DataColumn ("C");
443 Assert.IsNull (dg.Manager, "A1");
445 /* set the datamember to something as well.. anything yet? */
446 dg.DataMember = "DataTable";
447 Assert.IsNull (dg.Manager, "A2");
448 Assert.AreEqual (0, data_source_changed_count, "A3");
452 public void TestManagerCreateHandle ()
454 TestDataGrid dg = new TestDataGrid ();
456 data_source_changed_count = 0;
457 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
459 /* set our datasource to something */
460 dg = new TestDataGrid ();
461 DataSet ds = new DataSet ("DataSet");
462 DataTable dt = new DataTable ("DataTable");
463 DataColumn dc = new DataColumn ("C");
468 /* cause the control to create its handle and
469 * see if that does anything */
470 Assert.IsNotNull (dg.Handle, "A1");
471 Assert.IsNull (dg.Manager, "A2");
472 Assert.AreEqual (0, data_source_changed_count, "A3");
476 public void TestManagerSetBindingContext ()
478 TestDataGrid dg = new TestDataGrid ();
480 data_source_changed_count = 0;
481 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
483 /* set our datasource to something */
484 dg = new TestDataGrid ();
485 DataSet ds = new DataSet ("DataSet");
486 DataTable dt = new DataTable ("DataTable");
487 DataColumn dc = new DataColumn ("C");
491 dg.DataMember = "DataTable";
493 /* now set the BindingContext and see if something changes */
494 dg.BindingContext = new BindingContext ();
495 Assert.IsNotNull (dg.Manager, "A1");
496 Assert.AreEqual (0, data_source_changed_count, "A2");
500 public void TestManagerAfterSetBindingContext ()
502 TestDataGrid dg = new TestDataGrid ();
504 data_source_changed_count = 0;
505 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
507 dg.BindingContext = new BindingContext ();
509 /* set our datasource to something */
510 dg = new TestDataGrid ();
511 DataSet ds = new DataSet ("DataSet");
512 DataTable dt = new DataTable ("DataTable");
513 DataColumn dc = new DataColumn ("C");
517 Assert.IsNull (dg.Manager, "A1");
519 dg.DataMember = "DataTable";
520 Assert.IsNull (dg.Manager, "A2");
522 dg.BindingContext = new BindingContext ();
523 Assert.IsNotNull (dg.Manager, "A3");
524 // NotWorking Assert.AreEqual (0, data_source_changed_count, "A4");
528 public void TestManagerSetDataMemberAfterSetBindingContext ()
530 TestDataGrid dg = new TestDataGrid ();
532 data_source_changed_count = 0;
533 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
535 /* set our datasource to something */
536 dg = new TestDataGrid ();
537 DataSet ds = new DataSet ("DataSet");
538 DataTable dt = new DataTable ("DataTable");
539 DataColumn dc = new DataColumn ("C");
544 dg.BindingContext = new BindingContext ();
545 Assert.AreEqual (0, data_source_changed_count, "A1");
547 CurrencyManager mgr = dg.Manager;
549 dg.DataMember = "DataTable";
550 Assert.IsNotNull (dg.Manager, "A2");
551 Assert.IsTrue (mgr != dg.Manager, "A3");
552 Assert.AreEqual (0, data_source_changed_count, "A4");
556 public void TestManagerSetDataSourceAfterSetBindingContext ()
558 TestDataGrid dg = new TestDataGrid ();
560 data_source_changed_count = 0;
561 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
563 /* set our datasource to something */
564 dg = new TestDataGrid ();
565 DataSet ds = new DataSet ("DataSet");
566 DataTable dt = new DataTable ("DataTable");
567 DataColumn dc = new DataColumn ("C");
570 dg.DataMember = "DataTable";
572 dg.BindingContext = new BindingContext ();
573 Assert.AreEqual (0, data_source_changed_count, "A1");
575 CurrencyManager mgr = dg.Manager;
578 Assert.IsNotNull (dg.Manager, "A2");
579 Assert.IsTrue (mgr != dg.Manager, "A3");
580 Assert.AreEqual (0, data_source_changed_count, "A4");
584 public void TestManagerSetDataSourceAfterSetBindingContextWithHandle ()
586 TestDataGrid dg = new TestDataGrid ();
588 data_source_changed_count = 0;
589 dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
591 /* set our datasource to something */
592 dg = new TestDataGrid ();
593 DataSet ds = new DataSet ("DataSet");
594 DataTable dt = new DataTable ("DataTable");
595 DataColumn dc = new DataColumn ("C");
598 /* cause the control to create its handle and
599 * see if that does anything */
600 Assert.IsNotNull (dg.Handle, "A1");
602 dg.DataSource = new ArrayList ();
604 dg.BindingContext = new BindingContext ();
605 Assert.AreEqual (0, data_source_changed_count, "A2");
608 Assert.AreEqual (0, data_source_changed_count, "A3");