Merge pull request #303 from ermshiperete/5278
[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 using System.Xml;
36
37 namespace MonoTests.System.Windows.Forms
38 {
39         // Helper classes
40
41         class TestDataGrid : DataGrid 
42         {
43                 public TestDataGrid () 
44                 {
45
46                 }
47
48                 public CurrencyManager Manager {
49                         get {
50                                 return ListManager;
51                         }
52                 }       
53         }
54
55         [TestFixture]
56         public class DataGridTest : TestHelper
57         {
58                 private bool eventhandled;
59
60
61                 [Test]
62                 public void TestDefaultValues ()
63                 {
64                         DataGrid dg = new DataGrid ();
65
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");
89
90                         // Font
91                         Assert.IsFalse (dg.Font.Bold, "Font Bold");
92 #if NET_2_0
93                         Assert.IsTrue (dg.Font.IsSystemFont, "Font IsSystemFont");
94 #endif
95                         Assert.IsFalse (dg.Font.Italic, "Font Italic");
96                         Assert.IsFalse (dg.Font.Strikeout, "Font Strikeout");
97                         Assert.IsFalse (dg.Font.Underline, "Font Underline");
98                 }
99
100                 [Test]
101                 public void TestAllowNavigationChangedEvent ()
102                 {
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");
108                 }
109
110                 [Test]
111                 public void TestBackgroundColorChangedEvent ()
112                 {
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");
118                 }
119
120                 [Test]
121                 public void TestBorderStyleChangedEvent ()
122                 {
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");
128                 }
129
130                 [Test]
131                 public void TestCaptionVisibleChangedEvent ()
132                 {
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");
138                 }
139
140                 [Test]
141                 public void TestFlatModeChangedEvent ()
142                 {
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");
148                 }
149
150                 [Test]
151                 public void CaptionFont ()
152                 {
153                         DataGrid dg = new DataGrid ();
154
155                         // default values
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");
159 #if NET_2_0
160                         Assert.IsFalse(dg.CaptionFont.IsSystemFont, "#A4");
161 #endif
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");
169
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");
178
179                         // explicitly setting CaptionFont removes link between CaptionFont
180                         // and Font
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");
188                 }
189
190                 [Test]
191                 public void HeaderFont ()
192                 {
193                         DataGrid dg = new DataGrid ();
194                         dg.Font = new Font (dg.Font, FontStyle.Italic);
195                         Assert.AreSame (dg.HeaderFont, dg.Font, "#1");
196
197                         dg.HeaderFont = dg.Font;
198                         Assert.AreSame (dg.HeaderFont, dg.Font, "#2");
199
200                         dg.Font = new Font (dg.Font, FontStyle.Regular);
201                         Assert.IsTrue (dg.HeaderFont.Italic, "#3");
202                         Assert.IsFalse (dg.Font.Italic, "#4");
203
204                         dg.ResetHeaderFont ();
205                         Assert.AreSame (dg.HeaderFont, dg.Font, "#5");
206                 }
207
208
209                 [Test]
210                 public void TestParentRowsLabelStyleChangedEvent ()
211                 {
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");
217                 }
218
219                 [Test]
220                 public void TestParentRowsVisibleChangedEvent ()
221                 {
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");
227                 }
228                 
229                 [Test]
230                 public void TestReadOnlyChangedEvent ()
231                 {
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");
237                 }
238
239                 public void OnEventHandler (object sender, EventArgs e)
240                 {
241                         eventhandled = true;
242                 }
243
244                 // Property exceptions
245
246                 [Test]
247                 [ExpectedException (typeof (ArgumentException))]
248                 public void GridLineColorException ()
249                 {
250                         DataGrid dg = new DataGrid ();
251                         dg.GridLineColor = Color.Empty;
252                 }
253
254                 [Test]
255                 [ExpectedException (typeof (ArgumentException))]
256                 public void HeaderBackColorException ()
257                 {
258                         DataGrid dg = new DataGrid ();
259                         dg.HeaderBackColor = Color.Empty;
260                 }
261
262                 [Test]
263                 [ExpectedException (typeof (ArgumentException))]
264                 public void PreferredColumnWidthException ()
265                 {
266                         DataGrid dg = new DataGrid ();
267                         dg.PreferredColumnWidth = -1;
268                 }
269                 
270                 [Test]
271                 public void ResetAlternatingBackColor ()
272                 {
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");
278                 }
279                 
280                 // Test reset colour methods
281                 [Test]
282                 public void ResetBackColorMethod ()
283                 {
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");
289                 }
290
291                 [Test]
292                 public void ResetForeColorMethod ()
293                 {
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");
299                 }
300
301                 [Test]
302                 public void ResetGridLineColorMethod ()
303                 {                       
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");
309                 }
310
311                 [Test]
312                 public void ResetHeaderBackColorMethod ()
313                 {
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");
319                 }
320
321                 [Test]
322                 public void ResetHeaderFontMethod ()
323                 {
324                 }
325
326                 [Test]
327                 public void ResetHeaderForeColorMethod ()
328                 {
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");
334                 }
335
336                 [Test]
337                 public void ResetLinkColorMethod ()
338                 {
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");
344                 }
345
346                 [Test]
347                 public void ResetLinkHoverColor ()
348                 {
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");
354                 }
355
356                 [Test]          
357                 public void ResetSelectionBackColor ()
358                 {                       
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");
364                 }
365
366                 [Test]
367                 public void ResetSelectionForeColor ()
368                 {
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");
374                 }
375
376                 [Test]
377                 public void TestSetDataBinding ()
378                 {
379                         DataGrid dg = new DataGrid ();
380                         DataSet ds = new DataSet ("DataSet");
381                         DataTable dt = new DataTable ("DataTable");
382                         ds.Tables.Add (dt);
383
384                         dg.SetDataBinding (ds, "DataTable");
385                 }
386
387                 int data_source_changed_count = 0;
388                 void OnDataSourceChanged (object sender, EventArgs e)
389                 {
390                         data_source_changed_count ++;
391                 }
392
393                 [Test]
394                 public void TestManager1 ()
395                 {
396                         TestDataGrid dg = new TestDataGrid ();
397
398                         data_source_changed_count = 0;
399                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
400
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");
406                 }
407
408                 [Test]
409                 public void TestManagerSetDataMember ()
410                 {
411                         TestDataGrid dg = new TestDataGrid ();
412
413                         data_source_changed_count = 0;
414                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
415
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");
420                 }
421
422                 [Test]
423                 public void TestManagerSetDataSource ()
424                 {
425                         TestDataGrid dg = new TestDataGrid ();
426
427                         data_source_changed_count = 0;
428                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
429
430                         /* set our datasource to something */
431                         dg = new TestDataGrid ();
432                         DataSet ds = new DataSet ("DataSet");
433                         DataTable dt = new DataTable ("DataTable");
434                         ds.Tables.Add (dt);
435
436                         dg.DataSource = ds;
437                         Assert.IsNull (dg.Manager, "A1");
438
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");
443                 }
444
445                 [Test]
446                 public void TestManagerCreateHandle ()
447                 {
448                         TestDataGrid dg = new TestDataGrid ();
449
450                         data_source_changed_count = 0;
451                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
452
453                         /* set our datasource to something */
454                         dg = new TestDataGrid ();
455                         DataSet ds = new DataSet ("DataSet");
456                         DataTable dt = new DataTable ("DataTable");
457                         ds.Tables.Add (dt);
458
459                         dg.DataSource = ds;
460
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");
466                 }
467
468                 [Test]
469                 public void TestManagerSetBindingContext ()
470                 {
471                         TestDataGrid dg = new TestDataGrid ();
472
473                         data_source_changed_count = 0;
474                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
475
476                         /* set our datasource to something */
477                         dg = new TestDataGrid ();
478                         DataSet ds = new DataSet ("DataSet");
479                         DataTable dt = new DataTable ("DataTable");
480                         ds.Tables.Add (dt);
481
482                         dg.DataSource = ds;
483                         dg.DataMember = "DataTable";
484
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");
489                 }
490
491                 [Test]
492                 public void TestManagerAfterSetBindingContext ()
493                 {
494                         TestDataGrid dg = new TestDataGrid ();
495
496                         data_source_changed_count = 0;
497                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
498
499                         dg.BindingContext = new BindingContext ();
500
501                         /* set our datasource to something */
502                         dg = new TestDataGrid ();
503                         DataSet ds = new DataSet ("DataSet");
504                         DataTable dt = new DataTable ("DataTable");
505                         ds.Tables.Add (dt);
506
507                         dg.DataSource = ds;
508                         Assert.IsNull (dg.Manager, "A1");
509
510                         dg.DataMember = "DataTable";
511                         Assert.IsNull (dg.Manager, "A2");
512
513                         dg.BindingContext = new BindingContext ();
514                         Assert.IsNotNull (dg.Manager, "A3");
515                         // NotWorking Assert.AreEqual (0, data_source_changed_count, "A4");
516                 }
517
518                 [Test]
519                 public void TestManagerSetDataMemberAfterSetBindingContext ()
520                 {
521                         TestDataGrid dg = new TestDataGrid ();
522
523                         data_source_changed_count = 0;
524                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
525
526                         /* set our datasource to something */
527                         dg = new TestDataGrid ();
528                         DataSet ds = new DataSet ("DataSet");
529                         DataTable dt = new DataTable ("DataTable");
530                         ds.Tables.Add (dt);
531
532                         dg.DataSource = ds;
533
534                         dg.BindingContext = new BindingContext ();
535                         Assert.AreEqual (0, data_source_changed_count, "A1");
536
537                         CurrencyManager mgr = dg.Manager;
538
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");
543                 }
544
545                 [Test]
546                 public void TestManagerSetDataSourceAfterSetBindingContext ()
547                 {
548                         TestDataGrid dg = new TestDataGrid ();
549
550                         data_source_changed_count = 0;
551                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
552
553                         /* set our datasource to something */
554                         dg = new TestDataGrid ();
555                         DataSet ds = new DataSet ("DataSet");
556                         DataTable dt = new DataTable ("DataTable");
557                         ds.Tables.Add (dt);
558
559                         dg.DataMember = "DataTable";
560
561                         dg.BindingContext = new BindingContext ();
562                         Assert.AreEqual (0, data_source_changed_count, "A1");
563
564                         CurrencyManager mgr = dg.Manager;
565
566                         dg.DataSource = ds;
567                         Assert.IsNotNull (dg.Manager, "A2");
568                         Assert.IsTrue (mgr != dg.Manager, "A3");
569                         Assert.AreEqual (0, data_source_changed_count, "A4");
570                 }
571
572                 [Test]
573                 public void TestManagerSetDataSourceAfterSetBindingContextWithHandle ()
574                 {
575                         TestDataGrid dg = new TestDataGrid ();
576
577                         data_source_changed_count = 0;
578                         dg.DataSourceChanged += new EventHandler (OnDataSourceChanged);
579
580                         /* set our datasource to something */
581                         dg = new TestDataGrid ();
582                         DataSet ds = new DataSet ("DataSet");
583                         DataTable dt = new DataTable ("DataTable");
584                         ds.Tables.Add (dt);
585
586                         /* cause the control to create its handle and
587                          * see if that does anything */
588                         Assert.IsNotNull (dg.Handle, "A1");
589
590                         dg.DataSource = new ArrayList ();
591
592                         dg.BindingContext = new BindingContext ();
593                         Assert.AreEqual (0, data_source_changed_count, "A2");
594
595                         dg.DataSource = ds;
596                         Assert.AreEqual (0, data_source_changed_count, "A3");
597                 }
598
599                 [Test]
600                 public void TestManagerSetDataSourceWithEmptyStyle ()
601                 {
602                         TestDataGrid dg = new TestDataGrid ();
603                         dg.BindingContext = new BindingContext ();
604
605                         DataSet ds = new DataSet ("DataSet");
606                         DataTable dt = new DataTable ("MyTable");
607                         dt.Columns.Add ("A", typeof (string));
608                         dt.NewRow ();
609                         ds.Tables.Add (dt);
610
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);
616
617                         Assert.AreEqual (0, table_style.GridColumnStyles.Count, "#A1");
618
619                         dg.DataSource = dt;
620
621                         Assert.AreEqual (1, table_style.GridColumnStyles.Count, "#B1");
622                 }
623
624                 public class ClickableDataGrid : DataGrid
625                 {
626                         public void ClickGrid (int X, int Y)
627                         {
628                                 MouseEventArgs me = new MouseEventArgs (
629                                         MouseButtons.Left,
630                                         1, /*# of clicks*/
631                                         X, Y, 0);
632                                 OnMouseDown (me);
633                                 OnClick (me);
634                                 OnMouseUp (me);
635                         }
636                 }
637
638                 public class Form5487 : Form
639                 {
640                         private ClickableDataGrid dataGrid1;
641                         private Container components = null;
642
643                         public Form5487 ()
644                         {
645                                 InitializeComponent ();
646                         }
647
648                         protected override void Dispose (bool disposing)
649                         {
650                                 if (disposing) {
651                                         if (components != null) {
652                                                 components.Dispose ();
653                                         }
654                                 }
655                                 base.Dispose (disposing);
656                         }
657
658                         private void InitializeComponent ()
659                         {
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);
672                                 this.Name = "Form1";
673                                 this.Text = "Form1";
674                                 this.Shown += new EventHandler (this.Form1_Load);
675                                 ((ISupportInitialize)(this.dataGrid1)).EndInit ();
676                                 this.ResumeLayout (false);
677
678                         }
679
680                         private void Form1_Load (object sender, EventArgs e)
681                         {
682                                 DataSet ds = new DataSet ();
683                                 String XMLString = "";
684                                 XmlTextReader XMLTR;
685
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 ();
698                                 ds.ReadXml (XMLTR);
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 ();
705                                 Close ();
706                         }
707                 }
708                 [Test]
709                 public void Bug5487AndRelated ()
710                 {
711                         //this should crash on fail
712                         Application.Run (new Form5487 ());
713                 }
714         }
715 }