[resgen] Implement conditional resources (#if/#ifdef)
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / EditCommandColumnTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.EditCommandColumn.cs 
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Collections;
34 using System.Data;
35 using System.IO;
36 using System.Globalization;
37 using System.Text;
38 using System.Web;
39 using System.Web.UI;
40 using System.Web.UI.WebControls;
41 using MonoTests.stand_alone.WebHarness;
42 using MonoTests.SystemWeb.Framework;
43
44 namespace MonoTests.System.Web.UI.WebControls
45 {
46
47         
48         
49         [TestFixture]   
50         public class EditCommandColumnTest {
51
52                 private class DataGridTest : DataGrid {
53                         public ArrayList CreateColumns (PagedDataSource data_source, bool use_data_source) {
54                                 return CreateColumnSet (data_source, use_data_source);
55                         }
56
57                         public void CreateControls (bool use_data_source) {
58                                 CreateControlHierarchy (use_data_source);
59                         }
60                 }
61                 
62                 [Test]
63                 public void Defaults ()
64                 {
65                         EditCommandColumn       e;
66
67                         e = new EditCommandColumn();
68
69                         Assert.AreEqual(ButtonColumnType.LinkButton, e.ButtonType, "D1");
70                         Assert.AreEqual(string.Empty, e.CancelText, "D2");
71                         Assert.AreEqual(string.Empty, e.EditText, "D3");
72                         Assert.AreEqual(string.Empty, e.UpdateText, "D4");
73                         Assert.AreEqual (true, e.CausesValidation, "CausesValidation");
74                         Assert.AreEqual (string.Empty, e.ValidationGroup, "ValidationGroup");
75                 }
76
77                 [Test]
78                 public void Properties () {
79                         EditCommandColumn       e;
80
81                         e = new EditCommandColumn();
82
83                         e.ButtonType = ButtonColumnType.PushButton;
84                         Assert.AreEqual(ButtonColumnType.PushButton, e.ButtonType, "P1");
85
86                         e.CancelText = "Cancel this!";
87                         Assert.AreEqual("Cancel this!", e.CancelText, "D2");
88
89                         e.EditText = "Edit me good";
90                         Assert.AreEqual("Edit me good", e.EditText, "D3");
91
92                         e.UpdateText = "Update? What update?";
93                         Assert.AreEqual("Update? What update?", e.UpdateText, "D4");
94                         e.CausesValidation = false;
95                         Assert.AreEqual (false, e.CausesValidation, "CausesValidation");
96                         e.ValidationGroup = "test";
97                         Assert.AreEqual ("test", e.ValidationGroup, "ValidationGroup");
98                 }
99
100                 private string ControlMarkup(Control c) {
101                         StringWriter sw = new StringWriter ();
102                         HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
103
104                         c.RenderControl (tw);
105                         return sw.ToString ();
106                 }
107
108                 private void ShowControlsRecursive (Control c, int depth) {
109                          StringWriter sw = new StringWriter ();
110                          HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
111
112                          c.RenderControl (tw);
113                          Console.WriteLine (sw.ToString ());
114
115                         Console.WriteLine (c);
116
117                         foreach (Control child in c.Controls)
118                                 ShowControlsRecursive (child, depth + 5);
119                 }
120
121                 [Test]
122                 public void InitializeCell () 
123                 {
124 #if NET_4_0
125                         string origHtml = "<table><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td></tr><tr><td><a>Edit</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>";
126 #else
127                         string origHtml = "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td></tr><tr><td><a>Edit</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>";
128 #endif
129                         DataGridTest    p = new DataGridTest ();
130                         DataTable       table = new DataTable ();
131                         EditCommandColumn       e;
132                         string                  markup;
133
134                         e = new EditCommandColumn();
135                         e.ButtonType = ButtonColumnType.LinkButton;
136                         e.CancelText = "Cancel";
137                         e.EditText = "Edit";
138                         e.UpdateText = "Update";                        
139
140                         table.Columns.Add (new DataColumn ("one", typeof (string)));
141                         table.Columns.Add (new DataColumn ("two", typeof (string)));
142                         table.Columns.Add (new DataColumn ("three", typeof (string)));
143                         table.Rows.Add (new object [] { "1", "2", "3" });
144
145                         p.DataSource = new DataView (table);
146                         p.Columns.Add(e);
147
148                         e = new EditCommandColumn();
149                         e.ButtonType = ButtonColumnType.PushButton;
150                         e.CancelText = "Abbrechen";
151                         e.EditText = "Bearbeiten";
152                         e.UpdateText = "Refresh";                       
153                         p.Columns.Add(e);
154
155                         // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
156                         p.CreateControls (true);
157                         p.ID = "sucker";
158
159                         Assert.AreEqual (2, p.Columns.Count, "I1");
160                         markup = ControlMarkup(p.Controls[0]);
161                         markup = markup.Replace("\t", "");
162                         markup = markup.Replace ("\r", "");
163                         markup = markup.Replace ("\n", "");
164
165                         HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
166
167                         //ShowControlsRecursive (p.Controls [0], 1);
168                 }
169
170                 [Test]
171                 public void ThisIsADGTest () 
172                 {
173 #if NET_4_0
174                         string origHtml = "<table id=\"sucker_tbl\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td></tr><tr><td><a>Edit</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>";
175 #else
176                         string origHtml = "<table border=\"0\" id=\"sucker_tbl\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td></tr><tr><td><a>Edit</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>";
177 #endif
178                         DataGridTest    p = new DataGridTest ();
179                         DataTable       table = new DataTable ();
180                         EditCommandColumn       e;
181                         string                  markup;
182
183                         e = new EditCommandColumn();
184                         e.ButtonType = ButtonColumnType.LinkButton;
185                         e.CancelText = "Cancel";
186                         e.EditText = "Edit";
187                         e.UpdateText = "Update";                        
188
189                         table.Columns.Add (new DataColumn ("one", typeof (string)));
190                         table.Columns.Add (new DataColumn ("two", typeof (string)));
191                         table.Columns.Add (new DataColumn ("three", typeof (string)));
192                         table.Rows.Add (new object [] { "1", "2", "3" });
193
194                         p.DataSource = new DataView (table);
195                         p.Columns.Add(e);
196
197                         e = new EditCommandColumn();
198                         e.ButtonType = ButtonColumnType.PushButton;
199                         e.CancelText = "Abbrechen";
200                         e.EditText = "Bearbeiten";
201                         e.UpdateText = "Refresh";                       
202                         p.Columns.Add(e);
203
204                         p.CreateControls (true);
205                         // This is the test we want to run: setting the ID of the table created by
206                         // the datagrid overrides the using the ID of the datagrid itself and uses
207                         // the table ClientID instead.
208                         p.ID = "sucker";
209                         p.Controls [0].ID = "tbl";
210
211                         Assert.AreEqual (2, p.Columns.Count, "I1");
212                         markup = ControlMarkup(p.Controls[0]);
213                         markup = markup.Replace("\t", "");
214                         markup = markup.Replace ("\r", "");
215                         markup = markup.Replace ("\n", "");
216                         
217                         HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
218                 }
219
220                 static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
221                 {
222                         foreach (Control c in coll) {
223                                 sb.AppendFormat ("{0}{1}\n", new string (' ', 2 * level), c.GetType ());
224                                 GetHierarchy (c.Controls, level + 1, sb);
225                         }
226                 }
227
228                 [Test]
229                 public void InitializeEditCell () 
230                 {
231 #if NET_4_0
232                         string origHtml = "<table><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td></tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" />&nbsp;<input name=\"sucker$ctl02$ctl01\" type=\"submit\" value=\"Abbrechen\" /></td><td><input name=\"sucker$ctl02$ctl02\" type=\"text\" value=\"1\" /></td><td><input name=\"sucker$ctl02$ctl03\" type=\"text\" value=\"2\" /></td><td><input name=\"sucker$ctl02$ctl04\" type=\"text\" value=\"3\" /></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>";
233 #else
234                         string origHtml = "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td></tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" />&nbsp;<input name=\"sucker$ctl02$ctl01\" type=\"submit\" value=\"Abbrechen\" /></td><td><input name=\"sucker$ctl02$ctl02\" type=\"text\" value=\"1\" /></td><td><input name=\"sucker$ctl02$ctl03\" type=\"text\" value=\"2\" /></td><td><input name=\"sucker$ctl02$ctl04\" type=\"text\" value=\"3\" /></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>";
235 #endif
236                         DataGridTest    p = new DataGridTest ();
237                         DataTable       table = new DataTable ();
238                         EditCommandColumn       e;
239                         string                  markup;
240
241                         e = new EditCommandColumn();
242                         e.ButtonType = ButtonColumnType.LinkButton;
243                         e.CancelText = "Cancel";
244                         e.EditText = "Edit";
245                         e.UpdateText = "Update";                        
246
247                         table.Columns.Add (new DataColumn ("one", typeof (string)));
248                         table.Columns.Add (new DataColumn ("two", typeof (string)));
249                         table.Columns.Add (new DataColumn ("three", typeof (string)));
250                         table.Rows.Add (new object [] { "1", "2", "3" });
251
252                         p.DataSource = new DataView (table);
253                         p.Columns.Add(e);
254
255                         e = new EditCommandColumn();
256                         e.ButtonType = ButtonColumnType.PushButton;
257                         e.CancelText = "Abbrechen";
258                         e.EditText = "Bearbeiten";
259                         e.UpdateText = "Refresh";                       
260                         p.Columns.Add(e);
261
262                         // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
263                         p.EditItemIndex = 0;
264
265                         // This will trigger EditCommandColumn.InitializeCell
266                         p.CreateControls (true);
267                         p.ID = "sucker";
268
269                         StringBuilder sb = new StringBuilder ();
270                         GetHierarchy (p.Controls, 0, sb);
271                         string h = sb.ToString ();
272                         int x = h.IndexOf (".TextBox");
273                         // These are from the BoundColumns
274                         Assert.IsTrue (x != -1, "textbox1");
275                         x = h.IndexOf (".TextBox", x + 1);
276                         Assert.IsTrue (x != -1, "textbox2");
277                         x = h.IndexOf (".TextBox", x + 1);
278                         Assert.IsTrue (x != -1, "textbox3");
279                         x = h.IndexOf (".TextBox", x + 1);
280                         Assert.IsTrue (x == -1, "textbox-end");
281
282                         markup = ControlMarkup (p.Controls[0]);
283                         markup = markup.Replace ("\t", "");
284                         markup = markup.Replace ("\r", "");
285                         markup = markup.Replace ("\n", "");
286
287 //Console.WriteLine("Markup:>{0}<", markup);
288                         Assert.AreEqual (2, p.Columns.Count, "I1");
289
290                         HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
291                 }
292
293                 [Test]
294                 [Ignore("Unfinished")]
295                 public void InitializeReadOnlyEditCell ()
296                 {
297                         DataGridTest p = new DataGridTest ();
298                         DataTable table = new DataTable ();
299                         EditCommandColumn e;
300                         string markup;
301
302                         e = new EditCommandColumn ();
303                         e.ButtonType = ButtonColumnType.LinkButton;
304                         e.CancelText = "Cancel";
305                         e.EditText = "Edit";
306                         e.UpdateText = "Update";
307
308                         table.Columns.Add (new DataColumn ("one", typeof (string)));
309                         table.Columns.Add (new DataColumn ("two", typeof (string)));
310                         table.Columns.Add (new DataColumn ("three", typeof (string)));
311                         table.Rows.Add (new object[] { "1", "2", "3" });
312
313                         p.DataSource = new DataView (table);
314                         p.Columns.Add (e);
315
316                         e = new EditCommandColumn ();
317                         e.ButtonType = ButtonColumnType.PushButton;
318                         
319                         e.CancelText = "Abbrechen";
320                         e.EditText = "Bearbeiten";
321                         e.UpdateText = "Refresh";
322                         p.Columns.Add (e);
323
324                         // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
325                         p.EditItemIndex = 0;
326
327                         // This will trigger EditCommandColumn.InitializeCell
328                         p.CreateControls (true);
329                         p.ID = "sucker";
330
331                         markup = ControlMarkup (p.Controls[0]);
332                         markup = markup.Replace ("\t", "");
333                         markup = markup.Replace ("\r", "");
334                         markup = markup.Replace ("\n", "");
335
336                         Assert.AreEqual (2, p.Columns.Count, "I1");
337                         Assert.AreEqual (
338                                 "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
339                                 "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name type=\"submit\" value=\"Refresh\" />&nbsp;" +
340                                 "<input name value=\"Abbrechen\" type=\"submit\" /></td>" +
341                                 "<td><input name=\"_ctl2:_ctl0\" type=\"text\" value=\"1\" /></td>" +
342                                 "<td><input name=\"_ctl2:_ctl1\" type=\"text\" value=\"2\" /></td>" +
343                                 "<td><input name=\"_ctl2:_ctl2\" type=\"text\" value=\"3\" /></td>" +
344                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
345                                 "</tr></table>", markup, "I2");
346                 }
347
348                 [Test]
349                 [Category ("NunitWeb")]
350                 public void Validation_ValidatingValid () 
351                 {
352                         WebTest t = new WebTest ();
353                         PageDelegates pd = new PageDelegates ();
354                         pd.Load = Validation_Load;
355                         pd.PreRender = Validation_PreRender;
356                         t.Invoker = new PageInvoker (pd);
357                         t.UserData = "ValidatingValid";
358                         string html = t.Run ();
359                         FormRequest fr = new FormRequest (t.Response, "form1");
360
361                         fr.Controls.Add ("__EVENTTARGET");
362                         fr.Controls.Add ("__EVENTARGUMENT");
363                         fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
364                         fr.Controls ["__EVENTARGUMENT"].Value = "";
365                         t.Request = fr;
366                         t.UserData = "ValidatingValid";
367                         
368                         html = t.Run ();
369                 }
370
371                 [Test]
372                 [Category ("NunitWeb")]
373                 [Ignore ("Possibly incorrectly constructed test - conflicts with fix for bug #471305")]
374                 public void Validation_ValidatingInvalid () {
375                         WebTest t = new WebTest ();
376                         PageDelegates pd = new PageDelegates ();
377                         pd.Load = Validation_Load;
378                         pd.PreRender = Validation_PreRender;
379                         t.Invoker = new PageInvoker (pd);
380                         t.UserData = "ValidatingInvalid";
381                         string html = t.Run ();
382                         FormRequest fr = new FormRequest (t.Response, "form1");
383
384                         fr.Controls.Add ("__EVENTTARGET");
385                         fr.Controls.Add ("__EVENTARGUMENT");
386                         fr.Controls ["__EVENTTARGET"].Value = (string)t.UserData;
387                         fr.Controls ["__EVENTARGUMENT"].Value = "";
388                         t.Request = fr;
389                         t.UserData = "ValidatingInvalid";
390
391                         html = t.Run ();
392                 }
393
394                 [Test]
395                 [Category ("NunitWeb")]
396                 public void Validation_NotValidatingInvalid () {
397                         WebTest t = new WebTest ();
398                         PageDelegates pd = new PageDelegates ();
399                         pd.Load = Validation_Load;
400                         pd.PreRender = Validation_PreRender;
401                         t.Invoker = new PageInvoker (pd);
402                         t.UserData = "NotValidatingInvalid";
403                         string html = t.Run ();
404                         FormRequest fr = new FormRequest (t.Response, "form1");
405
406                         fr.Controls.Add ("__EVENTTARGET");
407                         fr.Controls.Add ("__EVENTARGUMENT");
408                         fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
409                         fr.Controls ["__EVENTARGUMENT"].Value = "";
410                         t.Request = fr;
411                         t.UserData = "NotValidatingInvalid";
412
413                         html = t.Run ();
414                 }
415
416                 [Test]
417                 [Category ("NunitWeb")]
418                 [Ignore ("Possibly incorrectly constructed test - conflicts with fix for bug #471305")]
419                 public void Validation_ValidationGroupIncluded () {
420                         WebTest t = new WebTest ();
421                         PageDelegates pd = new PageDelegates ();
422                         pd.Load = Validation_Load;
423                         pd.PreRender = Validation_PreRender;
424                         t.Invoker = new PageInvoker (pd);
425                         t.UserData = "ValidationGroupIncluded";
426                         string html = t.Run ();
427                         FormRequest fr = new FormRequest (t.Response, "form1");
428
429                         fr.Controls.Add ("__EVENTTARGET");
430                         fr.Controls.Add ("__EVENTARGUMENT");
431                         fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
432                         fr.Controls ["__EVENTARGUMENT"].Value = "";
433                         t.Request = fr;
434                         t.UserData = "ValidationGroupIncluded";
435
436                         html = t.Run ();
437                 }
438
439                 [Test]
440                 [Category ("NunitWeb")]
441                 public void Validation_ValidationGroupNotIncluded () {
442                         WebTest t = new WebTest ();
443                         PageDelegates pd = new PageDelegates ();
444                         pd.Load = Validation_Load;
445                         pd.PreRender = Validation_PreRender;
446                         t.Invoker = new PageInvoker (pd);
447                         t.UserData = "ValidationGroupNotIncluded";
448                         string html = t.Run ();
449                         FormRequest fr = new FormRequest (t.Response, "form1");
450
451                         fr.Controls.Add ("__EVENTTARGET");
452                         fr.Controls.Add ("__EVENTARGUMENT");
453                         fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
454                         fr.Controls ["__EVENTARGUMENT"].Value = "";
455                         t.Request = fr;
456                         t.UserData = "ValidationGroupNotIncluded";
457
458                         html = t.Run ();
459                 }
460
461                 public static void Validation_Load (Page p) 
462                 {
463                         string testType = (string)WebTest.CurrentTest.UserData;
464                         DataGridTest dg = new DataGridTest ();
465                         dg.ID = "mygrid";
466                         EditCommandColumn e;
467
468                         e = new EditCommandColumn ();
469                         e.ButtonType = ButtonColumnType.LinkButton;
470                         e.CancelText = "Cancel";
471                         e.EditText = "Edit";
472                         e.UpdateText = "Update";
473
474                         switch (testType) {
475                         case "ValidatingValid":
476                         case "ValidatingInvalid":
477                         case "ValidationGroupIncluded":
478                         case "ValidationGroupNotIncluded":
479                                 e.CausesValidation = true;
480                                 break;
481
482                         case "NotValidatingInvalid":
483                                 e.CausesValidation = false;
484                                 break;
485                         }
486
487                         switch (testType) {
488                         case "ValidationGroupIncluded":
489                         case "ValidationGroupNotIncluded":
490                                 e.ValidationGroup = "Group1";
491                                 break;
492
493                         default:
494                                 e.ValidationGroup = "";
495                                 break;
496                         }
497
498                         dg.Columns.Add (e);
499
500                         TextBox tb = new TextBox ();
501                         tb.ID = "Text1";
502                         switch (testType) {
503                         case "ValidatingValid":
504                                 tb.Text = "111";
505                                 break;
506
507                         case "ValidatingInvalid":
508                         case "NotValidatingInvalid":
509                         case "ValidationGroupIncluded":
510                         case "ValidationGroupNotIncluded":
511                                 tb.Text = "";
512                                 break;
513                         }
514
515                         RequiredFieldValidator v = new RequiredFieldValidator ();
516                         v.ControlToValidate = "Text1";
517                         switch (testType) {
518                         case "ValidationGroupIncluded":
519                                 v.ValidationGroup = "Group1";
520                                 break;
521
522                         case "ValidationGroupNotIncluded":
523                                 v.ValidationGroup = "NotGroup1";
524                                 break;
525
526                         default:
527                                 v.ValidationGroup = "";
528                                 break;
529                         }
530                         TemplateColumn tc = new TemplateColumn ();
531                         tc.EditItemTemplate = new ValidatingEditTemplate (tb, v);
532                         dg.Columns.Add (tc);
533
534                         ObjectDataSource ods = new ObjectDataSource ("MyObjectDS", "Select");
535                         ods.UpdateMethod = "Update";
536                         ods.DataObjectTypeName = "MyObjectDS";
537                         ods.ID = "MyDS";
538
539                         p.Form.Controls.Add (ods);
540
541                         dg.DataSource = ods;
542                         //dg.DataKeyField = "i";
543
544                         //DataTable table = new DataTable ();
545                         //table.Columns.Add (new DataColumn ("one", typeof (string)));
546                         //table.Columns.Add (new DataColumn ("two", typeof (string)));
547                         //table.Columns.Add (new DataColumn ("three", typeof (string)));
548                         //table.Rows.Add (new object [] { "1", "2", "3" });
549
550                         //dg.DataSource = new DataView (table);
551
552                         dg.EditItemIndex = 0;
553                         p.Form.Controls.Add (dg);
554
555                         dg.DataBind ();
556                         if (!p.IsPostBack) {
557                                 WebTest.CurrentTest.UserData = dg.Items [0].Cells [0].Controls [0].UniqueID;
558                         }
559                 }
560
561                 public static void Validation_PreRender (Page p) 
562                 {
563                         string testType = (string) WebTest.CurrentTest.UserData;
564
565                         if (p.IsPostBack) {
566                                 switch (testType) {
567                                 case "ValidatingValid":
568                                 case "ValidationGroupNotIncluded":
569                                         Assert.AreEqual (true, p.IsValid, "ValidatingValid");
570                                         break;
571                                 case "ValidatingInvalid":
572                                 case "ValidationGroupIncluded":
573                                         Assert.AreEqual (false, p.IsValid, "ValidatingInvalid");
574                                         break;
575
576                                 case "NotValidatingInvalid":
577                                         bool isValidated = true;
578                                         try {
579                                                 if (p.IsValid) {
580                                                         Assert.Fail ("NotValidatingInvalid IsValid == true");
581                                                 }
582                                         }
583                                         catch (HttpException httpException) {
584                                                 isValidated = false;
585                                         }
586                                         Assert.AreEqual(false, isValidated, "NotValidatingInvalid");
587                                         break;
588                                 }
589                         }
590                 }
591
592                 public class ValidatingEditTemplate : ITemplate
593                 {
594                         public ValidatingEditTemplate (params Control [] templateControls) 
595                         {
596                                 this.templateControls = new Control[templateControls.Length];
597                                 templateControls.CopyTo (this.templateControls, 0);
598                         }
599
600                         #region ITemplate Members
601
602                         public void InstantiateIn (Control container) 
603                         {
604                                 foreach (Control c in templateControls) {
605                                         container.Controls.Add (c);
606                                 }
607                         }
608
609                         #endregion
610
611                         private Control[] templateControls;
612                 }
613         }
614 }
615
616 #region MyObjectDS
617 public class MyObjectDS
618 {
619         public MyObjectDS () {
620                 _i = 0;
621         }
622
623         public MyObjectDS (int value) {
624                 _i = value;
625         }
626
627         private int _i;
628         public int i {
629                 get { return _i; }
630                 set { _i = value; }
631         }
632
633         static MyObjectDS [] myData = null;
634
635         public static IList Select () {
636                 if (myData == null) {
637                         myData = new MyObjectDS [] { new MyObjectDS (1), new MyObjectDS (2), new MyObjectDS (3) };
638                 }
639                 return myData;
640         }
641
642         public static void Update (MyObjectDS instance) {
643         }
644 }
645 #endregion