Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / System.Windows.Forms / Test / System.Windows.Forms / DataGridTextBoxColumnTest.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.Data;
31 using System.Drawing;
32 using System.Globalization;
33 using System.Threading;
34 using System.Windows.Forms;
35 using System.Xml;
36 using NUnit.Framework;
37
38 // resolve the ambiguity between System.ComponentModel and NUnit.Framework
39 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
40
41 namespace MonoTests.System.Windows.Forms
42 {
43         [TestFixture]
44         public class DataGridTextBoxColumnTest : TestHelper
45         {
46                 private bool eventhandled;
47                 //private object Element;
48                 //private CollectionChangeAction Action;
49
50                 [Test]
51                 public void TestDefaultValues ()
52                 {
53                         DataGridTextBoxColumn col = new DataGridTextBoxColumn ();
54
55                         Assert.AreEqual (HorizontalAlignment.Left, col.Alignment, "HorizontalAlignment property");
56                         Assert.AreEqual ("", col.HeaderText, "HeaderText property");
57                         Assert.AreEqual ("", col.MappingName, "MappingName property");
58                         Assert.AreEqual ("(null)", col.NullText, "NullText property");
59                         Assert.AreEqual (false, col.ReadOnly, "ReadOnly property");
60                         Assert.AreEqual (-1, col.Width, "Width property");
61                         Assert.AreEqual ("", col.Format, "Format property");
62                         Assert.AreEqual (null, col.FormatInfo, "FormatInfo property");
63                 }
64
65                 [Test]
66                 public void TestMappingNameChangedEvent ()
67                 {
68                         DataGridTextBoxColumn col = new DataGridTextBoxColumn ();
69                         eventhandled = false;
70                         col.MappingNameChanged += new EventHandler (OnEventHandler);
71                         col.MappingName = "name1";
72                         Assert.AreEqual (true, eventhandled, "A1");
73                 }
74
75                 [Test]
76                 public void TestAlignmentChangedEvent ()
77                 {
78                         DataGridTextBoxColumn col = new DataGridTextBoxColumn ();
79                         eventhandled = false;
80                         col.AlignmentChanged += new EventHandler (OnEventHandler);
81                         col.Alignment = HorizontalAlignment.Center;
82                         Assert.AreEqual (true, eventhandled, "A1");
83                 }
84
85                 [Test]
86                 public void TestHeaderTextChangedEvent ()
87                 {
88                         DataGridTextBoxColumn col = new DataGridTextBoxColumn ();
89                         eventhandled = false;
90                         col.HeaderTextChanged += new EventHandler (OnEventHandler);
91                         col.HeaderText = "Header";
92                         Assert.AreEqual (true, eventhandled, "A1");
93                 }
94
95                 [Test]
96                 public void TestNullTextChangedEvent ()
97                 {
98                         DataGridTextBoxColumn col = new DataGridTextBoxColumn ();
99                         eventhandled = false;
100                         col.NullTextChanged += new EventHandler (OnEventHandler);
101                         col.NullText = "Null";
102                         Assert.AreEqual (true, eventhandled, "A1");
103                 }
104
105                 private void OnEventHandler (object sender, EventArgs e)
106                 {
107                         eventhandled = true;
108                 }
109
110                 DataTable table;
111                 DataView view;
112                 DataGridTableStyle tableStyle;
113                 ColumnPoker nameColumnStyle;
114                 ColumnPoker companyColumnStyle;
115
116                 public void MakeTable (bool readonly_name)
117                 {
118                         table = new DataTable ();
119                         view = table.DefaultView;
120                         table.Columns.Add (new DataColumn ("who"));
121                         table.Columns.Add (new DataColumn ("where"));
122                         DataRow row = table.NewRow ();
123                         row ["who"] = "Miguel";
124                         row ["where"] = null;
125                         table.Rows.Add (row);
126
127                         row = table.NewRow ();
128                         row ["who"] = "Toshok";
129                         row ["where"] = "Novell";
130                         table.Rows.Add (row);
131
132                         tableStyle = new DataGridTableStyle ();
133                         nameColumnStyle = new ColumnPoker ();
134                         nameColumnStyle.MappingName = "who";
135                         nameColumnStyle.ReadOnly = readonly_name;
136                         tableStyle.GridColumnStyles.Add (nameColumnStyle);
137                         companyColumnStyle = new ColumnPoker ();
138                         companyColumnStyle.HeaderText = "Company";
139                         companyColumnStyle.MappingName = "where";
140                         companyColumnStyle.NullText = "(not set)";
141                         tableStyle.GridColumnStyles.Add (companyColumnStyle);
142                 }
143
144                 class ColumnPoker : DataGridTextBoxColumn
145                 {
146                         public ColumnPoker ()
147                         {
148                         }
149
150                         public ColumnPoker (PropertyDescriptor prop) : base (prop)
151                         {
152                         }
153
154                         public void DoAbort (int rowNum)
155                         {
156                                 base.Abort (rowNum);
157                         }
158
159                         public bool DoCommit (CurrencyManager dataSource, int rowNum)
160                         {
161                                 return base.Commit (dataSource, rowNum);
162                         }
163
164                         public void DoConcedeFocus ()
165                         {
166                                 base.ConcedeFocus ();
167                         }
168
169                         public void DoEdit (CurrencyManager source, int rowNum,  Rectangle bounds,  bool _ro, string instantText, bool cellIsVisible)
170                         {
171                                 base.Edit (source, rowNum, bounds, _ro, instantText, cellIsVisible);
172                         }
173
174                         public void DoEndEdit ()
175                         {
176                                 base.EndEdit ();
177                         }
178
179                         public void DoEnterNullValue ()
180                         {
181                                 base.EnterNullValue ();
182                         }
183
184                         public void DoHideEditBox ()
185                         {
186                                 base.HideEditBox ();
187                         }
188
189                         public void DoReleaseHostedControl ()
190                         {
191                                 base.ReleaseHostedControl ();
192                         }
193
194                         public void DoSetDataGridInColumn (DataGrid value)
195                         {
196                                 base.SetDataGridInColumn (value);
197                         }
198
199                         public void DoUpdateUI (CurrencyManager source, int rowNum, string instantText)
200                         {
201                                 base.UpdateUI (source, rowNum, instantText);
202                         }
203                 }
204
205                 [Test]
206                 public void TestDoEdit ()
207                 {
208                         MakeTable (true);
209
210                         BindingContext bc = new BindingContext ();
211                         DataGrid dg = new DataGrid ();
212                         dg.BindingContext = bc;
213                         dg.TableStyles.Add (tableStyle);
214                         dg.DataSource = table;
215
216                         CurrencyManager cm = (CurrencyManager)bc[view];
217                         ColumnPoker column = nameColumnStyle;
218                         TextBox tb = nameColumnStyle.TextBox;
219
220                         Assert.IsNotNull (tb, "1");
221                         Assert.AreEqual (typeof (DataGridTextBox), tb.GetType(), "2");
222                         Assert.IsTrue (tb.Enabled, "3");
223                         Assert.IsFalse (tb.Visible, "4");
224                         Assert.AreEqual ("", tb.Text, "5");
225                         Assert.IsFalse (tb.ReadOnly, "6");
226
227                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
228                         Assert.IsTrue (tb.ReadOnly, "7");
229
230                         // since it's readonly
231                         Assert.AreEqual ("Miguel", tb.Text, "8");
232                 }
233
234                 [Test]
235                 public void TestDoEdit_NullInstantTest ()
236                 {
237                         MakeTable (false);
238
239                         BindingContext bc = new BindingContext ();
240                         DataGrid dg = new DataGrid ();
241                         dg.BindingContext = bc;
242                         dg.TableStyles.Add (tableStyle);
243                         dg.DataSource = table;
244
245                         CurrencyManager cm = (CurrencyManager)bc[view];
246                         ColumnPoker column = nameColumnStyle;
247                         TextBox tb = nameColumnStyle.TextBox;
248
249                         Assert.IsNotNull (tb, "1");
250                         Assert.AreEqual (typeof (DataGridTextBox), tb.GetType(), "2");
251                         Assert.IsTrue (tb.Enabled, "3");
252                         Assert.IsFalse (tb.Visible, "4");
253                         Assert.AreEqual ("", tb.Text, "5");
254                         Assert.IsFalse (tb.ReadOnly, "6");
255
256                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
257                         Assert.IsFalse (tb.ReadOnly, "7");
258
259                         // since it's readonly
260                         Assert.AreEqual ("Miguel", tb.Text, "8");
261                 }
262
263                 [Test]
264                 public void TestEndEdit ()
265                 {
266                         MakeTable (false);
267
268                         BindingContext bc = new BindingContext ();
269                         DataGrid dg = new DataGrid ();
270                         dg.BindingContext = bc;
271                         dg.TableStyles.Add (tableStyle);
272                         dg.DataSource = table;
273
274                         CurrencyManager cm = (CurrencyManager)bc[view];
275                         ColumnPoker column = nameColumnStyle;
276                         TextBox tb = column.TextBox;
277
278                         Assert.AreEqual ("", tb.Text, "1");
279                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
280                         Assert.AreEqual ("hi there", tb.Text, "2");
281
282                         tb.Text = "yo";
283
284                         column.DoEndEdit ();
285
286                         DataRowView v = (DataRowView)cm.Current;
287
288                         Assert.AreEqual ("Miguel", v[0], "3");
289                 }
290
291                 [Test]
292                 public void TestCommit ()
293                 {
294                         MakeTable (false);
295
296                         BindingContext bc = new BindingContext ();
297                         DataGrid dg = new DataGrid ();
298                         dg.BindingContext = bc;
299                         dg.TableStyles.Add (tableStyle);
300                         dg.DataSource = table;
301
302                         CurrencyManager cm = (CurrencyManager)bc[view];
303                         ColumnPoker column = nameColumnStyle;
304                         DataGridTextBox tb = (DataGridTextBox)column.TextBox;
305
306                         Assert.AreEqual ("", tb.Text, "1");
307                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "1.5");
308                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
309                         Assert.AreEqual ("hi there", tb.Text, "2");
310                         Assert.AreEqual (new Rectangle (new Point (2,2), new Size (97,97)), tb.Bounds, "3");
311                         Assert.IsFalse (tb.ReadOnly, "4");
312                         Assert.IsFalse (tb.IsInEditOrNavigateMode, "5");
313
314                         bool rv;
315                         rv = column.DoCommit (cm, cm.Position);
316                         column.DoEndEdit ();
317
318                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "6");
319                         Assert.IsTrue (rv, "7");
320                         DataRowView v = (DataRowView)cm.Current;
321                         Assert.AreEqual ("hi there", v[0], "8");
322                 }
323
324                 [Test]
325                 public void TestCommit2 ()
326                 {
327                         MakeTable (false);
328
329                         BindingContext bc = new BindingContext ();
330                         DataGrid dg = new DataGrid ();
331                         dg.BindingContext = bc;
332                         dg.TableStyles.Add (tableStyle);
333                         dg.DataSource = table;
334
335                         CurrencyManager cm = (CurrencyManager)bc[view];
336                         ColumnPoker column = nameColumnStyle;
337                         DataGridTextBox tb = (DataGridTextBox)column.TextBox;
338
339                         Assert.AreEqual ("", tb.Text, "1");
340                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "1.5");
341                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
342                         Assert.AreEqual ("hi there", tb.Text, "2");
343                         Assert.AreEqual (new Rectangle (new Point (2,2), new Size (97,97)), tb.Bounds, "3");
344                         Assert.IsFalse (tb.ReadOnly, "4");
345                         Assert.IsFalse (tb.IsInEditOrNavigateMode, "5");
346
347                         tb.Text = "yo";
348
349                         column.DoEndEdit ();
350
351                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "5.5");
352
353                         bool rv = column.DoCommit (cm, cm.Position);
354                         Assert.IsTrue (rv, "6");
355                         DataRowView v = (DataRowView)cm.Current;
356                         Assert.AreEqual ("Miguel", v[0], "7");
357
358                         /* try it again with the DoCommit before the DoEndEdit */
359                         cm.Position = 0;
360                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100,100)), false, "hi there", true);
361                         Assert.AreEqual ("hi there", tb.Text, "8");
362                         Assert.AreEqual (new Rectangle (new Point (2,2), new Size (97,97)), tb.Bounds, "9");
363                         Assert.IsFalse (tb.ReadOnly, "10");
364                         Assert.IsFalse (tb.IsInEditOrNavigateMode, "11");
365                         tb.Text = "yo";
366
367                         rv = column.DoCommit (cm, cm.Position);
368                         column.DoEndEdit ();
369                         Assert.IsTrue (rv, "12");
370                         v = (DataRowView)cm.Current;
371                         Assert.AreEqual ("yo", v[0], "13");
372                 }
373
374                 [Test]
375                 public void TestAbort ()
376                 {
377                         MakeTable (false);
378
379                         BindingContext bc = new BindingContext ();
380                         DataGrid dg = new DataGrid ();
381                         dg.BindingContext = bc;
382                         dg.TableStyles.Add (tableStyle);
383                         dg.DataSource = table;
384
385                         CurrencyManager cm = (CurrencyManager)bc[view];
386                         ColumnPoker column = nameColumnStyle;
387                         DataGridTextBox tb = (DataGridTextBox)column.TextBox;
388
389                         Assert.AreEqual ("", tb.Text, "1");
390                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "1.5");
391                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
392                         Assert.AreEqual ("hi there", tb.Text, "2");
393                         Assert.AreEqual (new Rectangle (new Point (2,2), new Size (97,97)), tb.Bounds, "3");
394                         Assert.IsFalse (tb.ReadOnly, "4");
395                         Assert.IsFalse (tb.IsInEditOrNavigateMode, "5");
396
397                         tb.Text = "yo";
398
399                         column.DoAbort (0);
400
401                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "6");
402                         DataRowView v = (DataRowView)cm.Current;
403                         Assert.AreEqual ("Miguel", v[0], "7");
404                 }
405
406                 [Test]
407                 public void TestAbort_DifferentRow ()
408                 {
409                         MakeTable (false);
410
411                         BindingContext bc = new BindingContext ();
412                         DataGrid dg = new DataGrid ();
413                         dg.BindingContext = bc;
414                         dg.TableStyles.Add (tableStyle);
415                         dg.DataSource = table;
416
417                         CurrencyManager cm = (CurrencyManager)bc[view];
418                         ColumnPoker column = nameColumnStyle;
419                         DataGridTextBox tb = (DataGridTextBox)column.TextBox;
420
421                         Assert.AreEqual ("", tb.Text, "1");
422                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "1.5");
423                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
424                         Assert.AreEqual ("hi there", tb.Text, "2");
425                         Assert.AreEqual (new Rectangle (new Point (2,2), new Size (97,97)), tb.Bounds, "3");
426                         Assert.IsFalse (tb.ReadOnly, "4");
427                         Assert.IsFalse (tb.IsInEditOrNavigateMode, "5");
428
429                         tb.Text = "yo";
430
431                         column.DoAbort (1);
432
433                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "6");
434                         DataRowView v = (DataRowView)cm.Current;
435                         Assert.AreEqual ("Miguel", v[0], "7");
436                 }
437
438                 [Test]
439                 [Category ("NotWorking")]
440                 public void TestUpdateUI ()
441                 {
442                         MakeTable (false);
443
444                         BindingContext bc = new BindingContext ();
445                         DataGrid dg = new DataGrid ();
446                         dg.BindingContext = bc;
447                         dg.TableStyles.Add (tableStyle);
448                         dg.DataSource = table;
449
450                         CurrencyManager cm = (CurrencyManager)bc[view];
451                         ColumnPoker column = nameColumnStyle;
452                         DataGridTextBox tb = (DataGridTextBox)column.TextBox;
453
454                         Assert.AreEqual ("", tb.Text, "1");
455                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "2");
456
457                         Assert.AreEqual (Point.Empty, tb.Location, "3");
458
459                         column.DoUpdateUI (cm, 0, "hi there");
460
461                         Assert.AreEqual (Point.Empty, tb.Location, "4");
462
463                         Assert.AreEqual ("hi there", tb.Text, "5");
464                         Assert.IsFalse (tb.ReadOnly, "6");
465                         Assert.IsTrue (tb.IsInEditOrNavigateMode, "7");
466
467                         DataRowView v = (DataRowView)cm.Current;
468                         Assert.AreEqual ("Miguel", v[0], "8");
469                 }
470
471                 [Test]
472                 public void TestReadOnly_InEditCall ()
473                 {
474                         MakeTable (false);
475
476                         BindingContext bc = new BindingContext ();
477                         DataGrid dg = new DataGrid ();
478                         dg.BindingContext = bc;
479                         dg.TableStyles.Add (tableStyle);
480                         dg.DataSource = table;
481
482                         CurrencyManager cm = (CurrencyManager)bc[view];
483                         ColumnPoker column = nameColumnStyle;
484                         TextBox tb = nameColumnStyle.TextBox;
485
486                         Assert.IsNotNull (tb, "1");
487                         Assert.AreEqual (typeof (DataGridTextBox), tb.GetType(), "2");
488                         Assert.IsTrue (tb.Enabled, "3");
489                         Assert.IsFalse (tb.Visible, "4");
490                         Assert.AreEqual ("", tb.Text, "5");
491                         Assert.IsFalse (tb.ReadOnly, "6");
492
493                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), true, "hi there", true);
494
495                         Assert.IsTrue (tb.ReadOnly, "7");
496
497                         // since it's readonly
498                         Assert.AreEqual ("Miguel", tb.Text, "8");
499                 }
500
501                 [Test]
502                 public void TestReadOnly_AfterEditCall ()
503                 {
504                         MakeTable (false);
505
506                         BindingContext bc = new BindingContext ();
507                         DataGrid dg = new DataGrid ();
508                         dg.BindingContext = bc;
509                         dg.TableStyles.Add (tableStyle);
510                         dg.DataSource = table;
511
512                         CurrencyManager cm = (CurrencyManager)bc[view];
513                         ColumnPoker column = nameColumnStyle;
514                         TextBox tb = nameColumnStyle.TextBox;
515
516                         Assert.IsNotNull (tb, "1");
517                         Assert.AreEqual (typeof (DataGridTextBox), tb.GetType(), "2");
518                         Assert.IsTrue (tb.Enabled, "3");
519                         Assert.IsFalse (tb.Visible, "4");
520                         Assert.AreEqual ("", tb.Text, "5");
521                         Assert.IsFalse (tb.ReadOnly, "6");
522
523                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
524                         column.ReadOnly = true;
525
526                         Assert.IsFalse (tb.ReadOnly, "7");
527
528                         Assert.AreEqual ("hi there", tb.Text, "8");
529
530                         bool rv;
531
532                         rv = column.DoCommit (cm, cm.Position);
533                         column.DoEndEdit ();
534                         Assert.IsTrue (rv, "9");
535                         DataRowView v = (DataRowView)cm.Current;
536                         Assert.AreEqual ("hi there", v[0], "10");
537                 }
538
539                 [Test]
540                 public void TestReadOnly_DataGrid ()
541                 {
542                         MakeTable (false);
543
544                         BindingContext bc = new BindingContext ();
545                         DataGrid dg = new DataGrid ();
546                         dg.BindingContext = bc;
547                         dg.TableStyles.Add (tableStyle);
548                         dg.DataSource = table;
549                         dg.ReadOnly = true;
550
551                         CurrencyManager cm = (CurrencyManager)bc[view];
552                         ColumnPoker column = nameColumnStyle;
553                         TextBox tb = nameColumnStyle.TextBox;
554
555                         Assert.IsNotNull (tb, "1");
556                         Assert.AreEqual (typeof (DataGridTextBox), tb.GetType(), "2");
557                         Assert.IsTrue (tb.Enabled, "3");
558                         Assert.IsFalse (tb.Visible, "4");
559                         Assert.AreEqual ("", tb.Text, "5");
560                         Assert.IsFalse (tb.ReadOnly, "6");
561
562                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
563
564                         Assert.IsFalse (tb.ReadOnly, "7");
565
566                         Assert.AreEqual ("hi there", tb.Text, "8");
567
568                         bool rv;
569
570                         rv = column.DoCommit (cm, cm.Position);
571                         column.DoEndEdit ();
572                         Assert.IsTrue (rv, "9");
573                         DataRowView v = (DataRowView)cm.Current;
574                         Assert.AreEqual ("hi there", v[0], "10");
575                 }
576
577                 [Test]
578                 public void TestReadOnly_TableStyle ()
579                 {
580                         MakeTable (false);
581
582                         BindingContext bc = new BindingContext ();
583                         DataGrid dg = new DataGrid ();
584                         dg.BindingContext = bc;
585                         dg.TableStyles.Add (tableStyle);
586                         dg.DataSource = table;
587
588                         tableStyle.ReadOnly = true;
589
590                         CurrencyManager cm = (CurrencyManager)bc[view];
591                         ColumnPoker column = nameColumnStyle;
592                         TextBox tb = nameColumnStyle.TextBox;
593
594                         Assert.IsNotNull (tb, "1");
595                         Assert.AreEqual (typeof (DataGridTextBox), tb.GetType(), "2");
596                         Assert.IsTrue (tb.Enabled, "3");
597                         Assert.IsFalse (tb.Visible, "4");
598                         Assert.AreEqual ("", tb.Text, "5");
599                         Assert.IsFalse (tb.ReadOnly, "6");
600
601                         column.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "hi there", true);
602
603                         Assert.IsTrue (tb.ReadOnly, "7");
604
605                         Assert.AreEqual ("Miguel", tb.Text, "8");
606                 }
607
608                 [Test]
609                 public void IFormattable ()
610                 {
611                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
612
613                         try {
614                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-GB");
615
616                                 table = new DataTable ();
617                                 view = table.DefaultView;
618                                 table.Columns.Add (new DataColumn ("Amount", typeof (MockNumeric)));
619
620                                 DataRow row = table.NewRow ();
621                                 row ["Amount"] = new MockNumericFormattable (1);
622                                 table.Rows.Add (row);
623
624                                 row = table.NewRow ();
625                                 row ["Amount"] = new MockNumericFormattable (2);
626                                 table.Rows.Add (row);
627
628                                 row = table.NewRow ();
629                                 row ["Amount"] = new MockNumeric (3);
630                                 table.Rows.Add (row);
631
632                                 tableStyle = new DataGridTableStyle ();
633                                 ColumnPoker amountColumnStyle = new ColumnPoker ();
634                                 amountColumnStyle.MappingName = "Amount";
635                                 tableStyle.GridColumnStyles.Add (amountColumnStyle);
636
637                                 BindingContext bc = new BindingContext ();
638                                 DataGrid dg = new DataGrid ();
639                                 dg.BindingContext = bc;
640                                 dg.TableStyles.Add (tableStyle);
641                                 dg.DataSource = table;
642
643                                 CurrencyManager cm = (CurrencyManager) bc [view];
644                                 TextBox tb = amountColumnStyle.TextBox;
645
646                                 Assert.IsNotNull (tb, "#A1");
647                                 Assert.AreEqual (string.Empty, tb.Text, "#A2");
648
649                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
650                                 Assert.AreEqual ("uno", tb.Text, "#B1");
651                                 Assert.AreEqual (new MockNumericFormattable (1), table.Rows [0] ["Amount"], "#B2");
652
653                                 amountColumnStyle.DoEdit (cm, 1, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
654                                 Assert.AreEqual ("dos", tb.Text, "#C1");
655                                 Assert.AreEqual (new MockNumericFormattable (2), table.Rows [1] ["Amount"], "#C2");
656
657                                 amountColumnStyle.DoEdit (cm, 2, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
658                                 Assert.AreEqual ("tres", tb.Text, "#D1");
659                                 Assert.AreEqual (new MockNumeric (3), table.Rows [2] ["Amount"], "#D2");
660
661                                 amountColumnStyle.Format = string.Empty;
662
663                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
664                                 Assert.AreEqual ("uno", tb.Text, "#E1");
665                                 Assert.AreEqual (new MockNumericFormattable (1), table.Rows [0] ["Amount"], "#E2");
666
667                                 amountColumnStyle.Format = "currency";
668
669                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
670                                 Assert.AreEqual ("#£1.00", tb.Text, "#F1");
671                                 Assert.AreEqual (new MockNumericFormattable (1), table.Rows [0] ["Amount"], "#F2");
672
673                                 amountColumnStyle.DoEdit (cm, 2, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
674                                 Assert.AreEqual ("tres", tb.Text, "#G1");
675                                 Assert.AreEqual (new MockNumeric (3), table.Rows [2] ["Amount"], "#G2");
676
677                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "#£5.00", true);
678                                 Assert.AreEqual ("#£5.00", tb.Text, "#H1");
679                                 Assert.AreEqual (new MockNumericFormattable (1), table.Rows [0] ["Amount"], "#H2");
680
681                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "INVALID", true);
682                                 Assert.IsFalse (amountColumnStyle.DoCommit (cm, 0), "#I1");
683                                 Assert.AreEqual ("INVALID", tb.Text, "#I2");
684                                 //Assert.AreEqual ("INVALID", table.Rows [0] ["Amount"], "#I3");
685
686                                 amountColumnStyle.FormatInfo = new CultureInfo ("en-US");
687
688                                 amountColumnStyle.DoEdit (cm, 1, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
689                                 Assert.AreEqual ("#$2.00", tb.Text, "#J1");
690                                 Assert.AreEqual (new MockNumericFormattable (2), table.Rows [1] ["Amount"], "#J2");
691                         } finally {
692                                 Thread.CurrentThread.CurrentCulture = originalCulture;
693                         }
694                 }
695
696                 [Test]
697                 [Category ("NotWorking")]
698                 public void IFormattable_DateTime ()
699                 {
700                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
701
702                         try {
703                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
704                                 DateTime today = DateTime.Today;
705                                 DateTime now = DateTime.Now;
706
707                                 table = new DataTable ();
708                                 view = table.DefaultView;
709                                 table.Columns.Add (new DataColumn ("Date", typeof (DateTime)));
710
711                                 DataRow row = table.NewRow ();
712                                 row ["Date"] = today;
713                                 table.Rows.Add (row);
714
715                                 row = table.NewRow ();
716                                 row ["Date"] = now;
717                                 table.Rows.Add (row);
718
719                                 tableStyle = new DataGridTableStyle ();
720                                 ColumnPoker dateColumnStyle = new ColumnPoker ();
721                                 dateColumnStyle.MappingName = "Date";
722                                 tableStyle.GridColumnStyles.Add (dateColumnStyle);
723
724                                 BindingContext bc = new BindingContext ();
725                                 DataGrid dg = new DataGrid ();
726                                 dg.BindingContext = bc;
727                                 dg.TableStyles.Add (tableStyle);
728                                 dg.DataSource = table;
729
730                                 CurrencyManager cm = (CurrencyManager) bc [view];
731                                 TextBox tb = dateColumnStyle.TextBox;
732                                 DateTimeConverter converter = new DateTimeConverter ();
733
734                                 Assert.IsNotNull (tb, "#A1");
735                                 Assert.AreEqual (string.Empty, tb.Text, "#A2");
736
737                                 dateColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
738                                 Assert.AreEqual (converter.ConvertTo (null, CultureInfo.CurrentCulture,
739                                         today, typeof (string)), tb.Text, "#B1");
740                                 Assert.AreEqual (today, table.Rows [0] ["Date"], "#B2");
741
742                                 dateColumnStyle.DoEdit (cm, 1, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
743                                 Assert.AreEqual (converter.ConvertTo (null, CultureInfo.CurrentCulture,
744                                         now, typeof (string)), tb.Text, "#C1");
745                                 Assert.AreEqual (now, table.Rows [1] ["Date"], "#C2");
746
747                                 dateColumnStyle.Format = "MM";
748
749                                 dateColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
750                                 Assert.AreEqual (today.ToString ("MM", CultureInfo.CurrentCulture), tb.Text, "#D1");
751                                 Assert.AreEqual (today, table.Rows [0] ["Date"], "#D2");
752
753                                 dateColumnStyle.DoEdit (cm, 1, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
754                                 Assert.AreEqual (now.ToString ("MM", CultureInfo.CurrentCulture), tb.Text, "#E1");
755                                 Assert.AreEqual (now, table.Rows [1] ["Date"], "#E2");
756
757                                 dateColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "INVALID", true);
758                                 Assert.IsFalse (dateColumnStyle.DoCommit (cm, 0), "#F1");
759                                 Assert.AreEqual ("INVALID", tb.Text, "#F2");
760                                 Assert.AreEqual (today, table.Rows [0] ["Date"], "#F3");
761
762                                 dateColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "12", true);
763                                 Assert.IsFalse (dateColumnStyle.DoCommit (cm, 0), "#G1");
764                                 Assert.AreEqual ("12", tb.Text, "#G2");
765                                 Assert.AreEqual (today, table.Rows [0] ["Date"], "#G3");
766
767                                 dateColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "07/09/2007", true);
768                                 Assert.IsTrue (dateColumnStyle.DoCommit (cm, 0), "#H1");
769                                 Assert.AreEqual (converter.ConvertTo (null, CultureInfo.CurrentCulture,
770                                         new DateTime (2007, 9, 7), typeof (string)), tb.Text, "#H2");
771                                 Assert.AreEqual (new DateTime (2007, 9, 7), table.Rows [0] ["Date"], "#H3");
772
773                                 dateColumnStyle.FormatInfo = CultureInfo.CurrentCulture;
774
775                                 dateColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "08/06/2005", true);
776                                 Assert.IsTrue (dateColumnStyle.DoCommit (cm, 0), "#I1");
777                                 Assert.AreEqual ("06", tb.Text, "#I2");
778                                 Assert.AreEqual (new DateTime (2005, 6, 8), table.Rows [0] ["Date"], "#I3");
779                         } finally {
780                                 Thread.CurrentThread.CurrentCulture = originalCulture;
781                         }
782                 }
783
784                 [Test]
785                 public void StringConverterTest ()
786                 {
787                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
788
789                         try {
790                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-GB");
791
792                                 table = new DataTable ();
793                                 view = table.DefaultView;
794                                 table.Columns.Add (new DataColumn ("Amount", typeof (MockNumericStringConvertable)));
795
796                                 DataRow row = table.NewRow ();
797                                 row ["Amount"] = new MockNumericStringConvertable (1);
798                                 table.Rows.Add (row);
799
800                                 row = table.NewRow ();
801                                 row ["Amount"] = new MockNumericStringConvertable (2);
802                                 table.Rows.Add (row);
803
804                                 tableStyle = new DataGridTableStyle ();
805                                 ColumnPoker amountColumnStyle = new ColumnPoker ();
806                                 amountColumnStyle.MappingName = "Amount";
807                                 tableStyle.GridColumnStyles.Add (amountColumnStyle);
808
809                                 BindingContext bc = new BindingContext ();
810                                 DataGrid dg = new DataGrid ();
811                                 dg.BindingContext = bc;
812                                 dg.TableStyles.Add (tableStyle);
813                                 dg.DataSource = table;
814
815                                 CurrencyManager cm = (CurrencyManager) bc [view];
816                                 DataGridTextBox tb = (DataGridTextBox) amountColumnStyle.TextBox;
817
818                                 Assert.IsNotNull (tb, "#A1");
819                                 Assert.AreEqual (string.Empty, tb.Text, "#A2");
820                                 Assert.IsTrue (tb.IsInEditOrNavigateMode, "#A3");
821
822                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
823                                 Assert.AreEqual ("£1.00", tb.Text, "#B1");
824                                 Assert.AreEqual (new MockNumericStringConvertable (1), table.Rows [0] ["Amount"], "#B2");
825                                 Assert.IsTrue (tb.IsInEditOrNavigateMode, "#B3");
826
827                                 amountColumnStyle.DoEdit (cm, 1, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
828                                 Assert.AreEqual ("£2.00", tb.Text, "#C1");
829                                 Assert.AreEqual (new MockNumericStringConvertable (2), table.Rows [1] ["Amount"], "#C2");
830                                 Assert.IsTrue (tb.IsInEditOrNavigateMode, "#C3");
831
832                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "£3.00", true);
833                                 Assert.AreEqual ("£3.00", tb.Text, "#D1");
834                                 Assert.AreEqual (new MockNumericStringConvertable (1), table.Rows [0] ["Amount"], "#D2");
835                                 Assert.IsFalse (tb.IsInEditOrNavigateMode, "#D3");
836
837                                 Assert.IsTrue (amountColumnStyle.DoCommit (cm, cm.Position), "#E1");
838                                 Assert.AreEqual ("£3.00", tb.Text, "#E2");
839                                 Assert.AreEqual (new MockNumericStringConvertable (3), table.Rows [0] ["Amount"], "#E3");
840                                 Assert.IsTrue (tb.IsInEditOrNavigateMode, "#E4");
841
842                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, "INVALID", true);
843                                 Assert.IsFalse (amountColumnStyle.DoCommit (cm, cm.Position), "#F1");
844                                 Assert.AreEqual ("INVALID", tb.Text, "#F2");
845                                 Assert.AreEqual (new MockNumericStringConvertable (3), table.Rows [0] ["Amount"], "#F3");
846                                 Assert.IsFalse (tb.IsInEditOrNavigateMode, "#F4");
847
848                                 amountColumnStyle.Format = "whatever";
849                                 amountColumnStyle.FormatInfo = new CultureInfo ("en-US");
850
851                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0,0), new Size (100, 100)), false, null, true);
852                                 Assert.AreEqual ("£3.00", tb.Text, "#G1");
853                                 Assert.AreEqual (new MockNumericStringConvertable (3), table.Rows [0] ["Amount"], "#G2");
854                                 Assert.IsFalse (tb.IsInEditOrNavigateMode, "#G3");
855
856                                 tb.Text = "5";
857                                 Assert.IsTrue (amountColumnStyle.DoCommit (cm, cm.Position), "#H1");
858                                 Assert.AreEqual ("£5.00", tb.Text, "#H2");
859                                 Assert.AreEqual (new MockNumericStringConvertable (5), table.Rows [0] ["Amount"], "#H3");
860                         } finally {
861                                 Thread.CurrentThread.CurrentCulture = originalCulture;
862                         }
863                 }
864
865                 [Test]
866                 public void NonStringConverterTest ()
867                 {
868                         CultureInfo originalCulture = CultureInfo.CurrentCulture;
869
870                         try {
871                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-GB");
872
873                                 table = new DataTable ();
874                                 view = table.DefaultView;
875                                 table.Columns.Add (new DataColumn ("Amount", typeof (MockNumericNonStringConvertable)));
876
877                                 DataRow row = table.NewRow ();
878                                 row ["Amount"] = new MockNumericNonStringConvertable (1);
879                                 table.Rows.Add (row);
880
881                                 row = table.NewRow ();
882                                 row ["Amount"] = new MockNumericNonStringConvertable (2);
883                                 table.Rows.Add (row);
884
885                                 tableStyle = new DataGridTableStyle ();
886                                 ColumnPoker amountColumnStyle = new ColumnPoker ();
887                                 amountColumnStyle.MappingName = "Amount";
888                                 tableStyle.GridColumnStyles.Add (amountColumnStyle);
889
890                                 BindingContext bc = new BindingContext ();
891                                 DataGrid dg = new DataGrid ();
892                                 dg.BindingContext = bc;
893                                 dg.TableStyles.Add (tableStyle);
894                                 dg.DataSource = table;
895
896                                 CurrencyManager cm = (CurrencyManager) bc [view];
897                                 TextBox tb = amountColumnStyle.TextBox;
898
899                                 Assert.IsNotNull (tb, "#A1");
900                                 Assert.AreEqual (string.Empty, tb.Text, "#A2");
901
902                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0, 0), new Size (100, 100)), false, null, true);
903                                 Assert.AreEqual ("uno", tb.Text, "#B1");
904                                 Assert.AreEqual (new MockNumericStringConvertable (1), table.Rows [0] ["Amount"], "#B2");
905
906                                 amountColumnStyle.DoEdit (cm, 1, new Rectangle (new Point (0, 0), new Size (100, 100)), false, null, true);
907                                 Assert.AreEqual ("dos", tb.Text, "#C1");
908                                 Assert.AreEqual (new MockNumericStringConvertable (2), table.Rows [1] ["Amount"], "#C2");
909
910                                 amountColumnStyle.DoEdit (cm, 0, new Rectangle (new Point (0, 0), new Size (100, 100)), false, "£3.00", true);
911                                 Assert.AreEqual ("£3.00", tb.Text, "#D1");
912                                 Assert.AreEqual (new MockNumericStringConvertable (1), table.Rows [0] ["Amount"], "#D2");
913
914                                 Assert.IsFalse (amountColumnStyle.DoCommit (cm, cm.Position), "#E1");
915                                 Assert.AreEqual ("£3.00", tb.Text, "#E2");
916                                 //Assert.AreEqual ("£3.00", table.Rows [0] ["Amount"], "#E3");
917                         } finally {
918                                 Thread.CurrentThread.CurrentCulture = originalCulture;
919                         }
920                 }
921
922                 class MockNumeric
923                 {
924                         public MockNumeric (int number)
925                         {
926                                 this.number = number;
927                         }
928
929                         public int Number {
930                                 get { return number; }
931                                 set { number = value; }
932                         }
933
934                         public override bool Equals (object obj)
935                         {
936                                 if (obj == null)
937                                         return false;
938
939                                 MockNumeric numeric = obj as MockNumeric;
940                                 return (numeric != null && numeric.Number == Number);
941                         }
942
943                         public override int GetHashCode ()
944                         {
945                                 return number.GetHashCode ();
946                         }
947
948                         public override string ToString ()
949                         {
950                                 switch (Number) {
951                                 case 1:
952                                         return "uno";
953                                 case 2:
954                                         return "dos";
955                                 case 3:
956                                         return "tres";
957                                 default:
958                                         return "bad";
959                                 }
960                         }
961
962                         private int number;
963                 }
964
965                 class MockNumericFormattable : MockNumeric, IFormattable
966                 {
967                         public MockNumericFormattable (int number) : base (number)
968                         {
969                         }
970
971                         string IFormattable.ToString (string format, IFormatProvider formatProvider)
972                         {
973                                 if (format == "currency") {
974                                         return "#" + Number.ToString ("c", formatProvider);
975                                 } else {
976                                         return "#" + Number.ToString (formatProvider);
977                                 }
978                         }
979                 }
980
981                 [TypeConverter (typeof (MockNumericStringConverter))]
982                 class MockNumericStringConvertable : MockNumeric
983                 {
984                         public MockNumericStringConvertable (int number) : base (number)
985                         {
986                         }
987                 }
988
989                 [TypeConverter (typeof (MockNumericNonStringConverter))]
990                 class MockNumericNonStringConvertable : MockNumeric
991                 {
992                         public MockNumericNonStringConvertable (int number) : base (number)
993                         {
994                         }
995                 }
996
997                 class MockNumericStringConverter : TypeConverter
998                 {
999                         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
1000                         {
1001                                 if (sourceType == typeof (string))
1002                                         return true;
1003                                 return false;
1004                         }
1005
1006                         public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
1007                         {
1008                                 if (destinationType == typeof (string))
1009                                         return true;
1010                                 return false;
1011                         }
1012
1013                         public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
1014                         {
1015                                 if (value == null)
1016                                         return value;
1017
1018                                 string val = value as string;
1019                                 if (val != null) {
1020                                         int number = int.Parse (val, NumberStyles.Currency, culture);
1021                                         return new MockNumericStringConvertable (number);
1022                                 }
1023
1024                                 return base.ConvertFrom (context, culture, value);
1025                         }
1026
1027                         public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
1028                         {
1029                                 if (value == null)
1030                                         return value;
1031
1032                                 if (destinationType == typeof (string)) {
1033                                         MockNumeric val = value as MockNumeric;
1034                                         return val.Number.ToString ("C", culture);
1035                                 }
1036
1037                                 return base.ConvertTo (context, culture, value, destinationType);
1038                         }
1039                 }
1040
1041                 class MockNumericNonStringConverter : TypeConverter
1042                 {
1043                         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
1044                         {
1045                                 return false;
1046                         }
1047
1048                         public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
1049                         {
1050                                 return false;
1051                         }
1052
1053                         public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
1054                         {
1055                                 throw new NotSupportedException ();
1056                         }
1057
1058                         public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
1059                         {
1060                                 throw new NotSupportedException ();
1061                         }
1062                 }
1063         }
1064 }