2 // Tests for System.Web.UI.WebControls.EditCommandColumn.cs
5 // Peter Dennis Bartok (pbartok@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
33 using System.Collections;
36 using System.Globalization;
40 using System.Web.UI.WebControls;
41 using MonoTests.stand_alone.WebHarness;
42 using MonoTests.SystemWeb.Framework;
44 namespace MonoTests.System.Web.UI.WebControls
50 public class EditCommandColumnTest {
52 private class DataGridTest : DataGrid {
53 public ArrayList CreateColumns (PagedDataSource data_source, bool use_data_source) {
54 return CreateColumnSet (data_source, use_data_source);
57 public void CreateControls (bool use_data_source) {
58 CreateControlHierarchy (use_data_source);
63 public void Defaults ()
67 e = new EditCommandColumn();
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");
78 public void Properties () {
81 e = new EditCommandColumn();
83 e.ButtonType = ButtonColumnType.PushButton;
84 Assert.AreEqual(ButtonColumnType.PushButton, e.ButtonType, "P1");
86 e.CancelText = "Cancel this!";
87 Assert.AreEqual("Cancel this!", e.CancelText, "D2");
89 e.EditText = "Edit me good";
90 Assert.AreEqual("Edit me good", e.EditText, "D3");
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");
100 private string ControlMarkup(Control c) {
101 StringWriter sw = new StringWriter ();
102 HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
104 c.RenderControl (tw);
105 return sw.ToString ();
108 private void ShowControlsRecursive (Control c, int depth) {
109 StringWriter sw = new StringWriter ();
110 HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
112 c.RenderControl (tw);
113 Console.WriteLine (sw.ToString ());
115 Console.WriteLine (c);
117 foreach (Control child in c.Controls)
118 ShowControlsRecursive (child, depth + 5);
122 public void InitializeCell ()
125 string origHtml = "<table><tr><td> </td><td> </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> </td><td> </td><td> </td><td> </td><td> </td></tr></table>";
127 string origHtml = "<table border=\"0\"><tr><td> </td><td> </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> </td><td> </td><td> </td><td> </td><td> </td></tr></table>";
129 DataGridTest p = new DataGridTest ();
130 DataTable table = new DataTable ();
134 e = new EditCommandColumn();
135 e.ButtonType = ButtonColumnType.LinkButton;
136 e.CancelText = "Cancel";
138 e.UpdateText = "Update";
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" });
145 p.DataSource = new DataView (table);
148 e = new EditCommandColumn();
149 e.ButtonType = ButtonColumnType.PushButton;
150 e.CancelText = "Abbrechen";
151 e.EditText = "Bearbeiten";
152 e.UpdateText = "Refresh";
155 // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
156 p.CreateControls (true);
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", "");
165 HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
167 //ShowControlsRecursive (p.Controls [0], 1);
171 public void ThisIsADGTest ()
174 string origHtml = "<table id=\"sucker_tbl\"><tr><td> </td><td> </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> </td><td> </td><td> </td><td> </td><td> </td></tr></table>";
176 string origHtml = "<table border=\"0\" id=\"sucker_tbl\"><tr><td> </td><td> </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> </td><td> </td><td> </td><td> </td><td> </td></tr></table>";
178 DataGridTest p = new DataGridTest ();
179 DataTable table = new DataTable ();
183 e = new EditCommandColumn();
184 e.ButtonType = ButtonColumnType.LinkButton;
185 e.CancelText = "Cancel";
187 e.UpdateText = "Update";
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" });
194 p.DataSource = new DataView (table);
197 e = new EditCommandColumn();
198 e.ButtonType = ButtonColumnType.PushButton;
199 e.CancelText = "Abbrechen";
200 e.EditText = "Bearbeiten";
201 e.UpdateText = "Refresh";
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.
209 p.Controls [0].ID = "tbl";
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", "");
217 HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
220 static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
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);
229 public void InitializeEditCell ()
232 string origHtml = "<table><tr><td> </td><td> </td><td>one</td><td>two</td><td>three</td></tr><tr><td><a>Update</a> <a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" /> <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> </td><td> </td><td> </td><td> </td><td> </td></tr></table>";
234 string origHtml = "<table border=\"0\"><tr><td> </td><td> </td><td>one</td><td>two</td><td>three</td></tr><tr><td><a>Update</a> <a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" /> <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> </td><td> </td><td> </td><td> </td><td> </td></tr></table>";
236 DataGridTest p = new DataGridTest ();
237 DataTable table = new DataTable ();
241 e = new EditCommandColumn();
242 e.ButtonType = ButtonColumnType.LinkButton;
243 e.CancelText = "Cancel";
245 e.UpdateText = "Update";
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" });
252 p.DataSource = new DataView (table);
255 e = new EditCommandColumn();
256 e.ButtonType = ButtonColumnType.PushButton;
257 e.CancelText = "Abbrechen";
258 e.EditText = "Bearbeiten";
259 e.UpdateText = "Refresh";
262 // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
265 // This will trigger EditCommandColumn.InitializeCell
266 p.CreateControls (true);
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");
282 markup = ControlMarkup (p.Controls[0]);
283 markup = markup.Replace ("\t", "");
284 markup = markup.Replace ("\r", "");
285 markup = markup.Replace ("\n", "");
287 //Console.WriteLine("Markup:>{0}<", markup);
288 Assert.AreEqual (2, p.Columns.Count, "I1");
290 HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
294 [Ignore("Unfinished")]
295 public void InitializeReadOnlyEditCell ()
297 DataGridTest p = new DataGridTest ();
298 DataTable table = new DataTable ();
302 e = new EditCommandColumn ();
303 e.ButtonType = ButtonColumnType.LinkButton;
304 e.CancelText = "Cancel";
306 e.UpdateText = "Update";
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" });
313 p.DataSource = new DataView (table);
316 e = new EditCommandColumn ();
317 e.ButtonType = ButtonColumnType.PushButton;
319 e.CancelText = "Abbrechen";
320 e.EditText = "Bearbeiten";
321 e.UpdateText = "Refresh";
324 // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
327 // This will trigger EditCommandColumn.InitializeCell
328 p.CreateControls (true);
331 markup = ControlMarkup (p.Controls[0]);
332 markup = markup.Replace ("\t", "");
333 markup = markup.Replace ("\r", "");
334 markup = markup.Replace ("\n", "");
336 Assert.AreEqual (2, p.Columns.Count, "I1");
338 "<table border=\"0\" id=\"sucker\"><tr><td> </td><td> </td><td>one</td><td>two</td><td>three</td>" +
339 "</tr><tr><td><a>Update</a> <a>Cancel</a></td><td><input name type=\"submit\" value=\"Refresh\" /> " +
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> </td><td> </td><td> </td><td> </td><td> </td>" +
345 "</tr></table>", markup, "I2");
349 [Category ("NunitWeb")]
350 public void Validation_ValidatingValid ()
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");
361 fr.Controls.Add ("__EVENTTARGET");
362 fr.Controls.Add ("__EVENTARGUMENT");
363 fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
364 fr.Controls ["__EVENTARGUMENT"].Value = "";
366 t.UserData = "ValidatingValid";
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");
384 fr.Controls.Add ("__EVENTTARGET");
385 fr.Controls.Add ("__EVENTARGUMENT");
386 fr.Controls ["__EVENTTARGET"].Value = (string)t.UserData;
387 fr.Controls ["__EVENTARGUMENT"].Value = "";
389 t.UserData = "ValidatingInvalid";
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");
406 fr.Controls.Add ("__EVENTTARGET");
407 fr.Controls.Add ("__EVENTARGUMENT");
408 fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
409 fr.Controls ["__EVENTARGUMENT"].Value = "";
411 t.UserData = "NotValidatingInvalid";
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");
429 fr.Controls.Add ("__EVENTTARGET");
430 fr.Controls.Add ("__EVENTARGUMENT");
431 fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
432 fr.Controls ["__EVENTARGUMENT"].Value = "";
434 t.UserData = "ValidationGroupIncluded";
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");
451 fr.Controls.Add ("__EVENTTARGET");
452 fr.Controls.Add ("__EVENTARGUMENT");
453 fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
454 fr.Controls ["__EVENTARGUMENT"].Value = "";
456 t.UserData = "ValidationGroupNotIncluded";
461 public static void Validation_Load (Page p)
463 string testType = (string)WebTest.CurrentTest.UserData;
464 DataGridTest dg = new DataGridTest ();
468 e = new EditCommandColumn ();
469 e.ButtonType = ButtonColumnType.LinkButton;
470 e.CancelText = "Cancel";
472 e.UpdateText = "Update";
475 case "ValidatingValid":
476 case "ValidatingInvalid":
477 case "ValidationGroupIncluded":
478 case "ValidationGroupNotIncluded":
479 e.CausesValidation = true;
482 case "NotValidatingInvalid":
483 e.CausesValidation = false;
488 case "ValidationGroupIncluded":
489 case "ValidationGroupNotIncluded":
490 e.ValidationGroup = "Group1";
494 e.ValidationGroup = "";
500 TextBox tb = new TextBox ();
503 case "ValidatingValid":
507 case "ValidatingInvalid":
508 case "NotValidatingInvalid":
509 case "ValidationGroupIncluded":
510 case "ValidationGroupNotIncluded":
515 RequiredFieldValidator v = new RequiredFieldValidator ();
516 v.ControlToValidate = "Text1";
518 case "ValidationGroupIncluded":
519 v.ValidationGroup = "Group1";
522 case "ValidationGroupNotIncluded":
523 v.ValidationGroup = "NotGroup1";
527 v.ValidationGroup = "";
530 TemplateColumn tc = new TemplateColumn ();
531 tc.EditItemTemplate = new ValidatingEditTemplate (tb, v);
534 ObjectDataSource ods = new ObjectDataSource ("MyObjectDS", "Select");
535 ods.UpdateMethod = "Update";
536 ods.DataObjectTypeName = "MyObjectDS";
539 p.Form.Controls.Add (ods);
542 //dg.DataKeyField = "i";
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" });
550 //dg.DataSource = new DataView (table);
552 dg.EditItemIndex = 0;
553 p.Form.Controls.Add (dg);
557 WebTest.CurrentTest.UserData = dg.Items [0].Cells [0].Controls [0].UniqueID;
561 public static void Validation_PreRender (Page p)
563 string testType = (string) WebTest.CurrentTest.UserData;
567 case "ValidatingValid":
568 case "ValidationGroupNotIncluded":
569 Assert.AreEqual (true, p.IsValid, "ValidatingValid");
571 case "ValidatingInvalid":
572 case "ValidationGroupIncluded":
573 Assert.AreEqual (false, p.IsValid, "ValidatingInvalid");
576 case "NotValidatingInvalid":
577 bool isValidated = true;
580 Assert.Fail ("NotValidatingInvalid IsValid == true");
583 catch (HttpException httpException) {
586 Assert.AreEqual(false, isValidated, "NotValidatingInvalid");
592 public class ValidatingEditTemplate : ITemplate
594 public ValidatingEditTemplate (params Control [] templateControls)
596 this.templateControls = new Control[templateControls.Length];
597 templateControls.CopyTo (this.templateControls, 0);
600 #region ITemplate Members
602 public void InstantiateIn (Control container)
604 foreach (Control c in templateControls) {
605 container.Controls.Add (c);
611 private Control[] templateControls;
617 public class MyObjectDS
619 public MyObjectDS () {
623 public MyObjectDS (int value) {
633 static MyObjectDS [] myData = null;
635 public static IList Select () {
636 if (myData == null) {
637 myData = new MyObjectDS [] { new MyObjectDS (1), new MyObjectDS (2), new MyObjectDS (3) };
642 public static void Update (MyObjectDS instance) {