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 ()
124 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>";
125 DataGridTest p = new DataGridTest ();
126 DataTable table = new DataTable ();
130 e = new EditCommandColumn();
131 e.ButtonType = ButtonColumnType.LinkButton;
132 e.CancelText = "Cancel";
134 e.UpdateText = "Update";
136 table.Columns.Add (new DataColumn ("one", typeof (string)));
137 table.Columns.Add (new DataColumn ("two", typeof (string)));
138 table.Columns.Add (new DataColumn ("three", typeof (string)));
139 table.Rows.Add (new object [] { "1", "2", "3" });
141 p.DataSource = new DataView (table);
144 e = new EditCommandColumn();
145 e.ButtonType = ButtonColumnType.PushButton;
146 e.CancelText = "Abbrechen";
147 e.EditText = "Bearbeiten";
148 e.UpdateText = "Refresh";
151 // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
152 p.CreateControls (true);
155 Assert.AreEqual (2, p.Columns.Count, "I1");
156 markup = ControlMarkup(p.Controls[0]);
157 markup = markup.Replace("\t", "");
158 markup = markup.Replace ("\r", "");
159 markup = markup.Replace ("\n", "");
161 HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
163 //ShowControlsRecursive (p.Controls [0], 1);
167 public void ThisIsADGTest ()
169 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>";
170 DataGridTest p = new DataGridTest ();
171 DataTable table = new DataTable ();
175 e = new EditCommandColumn();
176 e.ButtonType = ButtonColumnType.LinkButton;
177 e.CancelText = "Cancel";
179 e.UpdateText = "Update";
181 table.Columns.Add (new DataColumn ("one", typeof (string)));
182 table.Columns.Add (new DataColumn ("two", typeof (string)));
183 table.Columns.Add (new DataColumn ("three", typeof (string)));
184 table.Rows.Add (new object [] { "1", "2", "3" });
186 p.DataSource = new DataView (table);
189 e = new EditCommandColumn();
190 e.ButtonType = ButtonColumnType.PushButton;
191 e.CancelText = "Abbrechen";
192 e.EditText = "Bearbeiten";
193 e.UpdateText = "Refresh";
196 p.CreateControls (true);
197 // This is the test we want to run: setting the ID of the table created by
198 // the datagrid overrides the using the ID of the datagrid itself and uses
199 // the table ClientID instead.
201 p.Controls [0].ID = "tbl";
203 Assert.AreEqual (2, p.Columns.Count, "I1");
204 markup = ControlMarkup(p.Controls[0]);
205 markup = markup.Replace("\t", "");
206 markup = markup.Replace ("\r", "");
207 markup = markup.Replace ("\n", "");
209 HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
212 static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
214 foreach (Control c in coll) {
215 sb.AppendFormat ("{0}{1}\n", new string (' ', 2 * level), c.GetType ());
216 GetHierarchy (c.Controls, level + 1, sb);
221 public void InitializeEditCell ()
223 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>";
224 DataGridTest p = new DataGridTest ();
225 DataTable table = new DataTable ();
229 e = new EditCommandColumn();
230 e.ButtonType = ButtonColumnType.LinkButton;
231 e.CancelText = "Cancel";
233 e.UpdateText = "Update";
235 table.Columns.Add (new DataColumn ("one", typeof (string)));
236 table.Columns.Add (new DataColumn ("two", typeof (string)));
237 table.Columns.Add (new DataColumn ("three", typeof (string)));
238 table.Rows.Add (new object [] { "1", "2", "3" });
240 p.DataSource = new DataView (table);
243 e = new EditCommandColumn();
244 e.ButtonType = ButtonColumnType.PushButton;
245 e.CancelText = "Abbrechen";
246 e.EditText = "Bearbeiten";
247 e.UpdateText = "Refresh";
250 // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
253 // This will trigger EditCommandColumn.InitializeCell
254 p.CreateControls (true);
257 StringBuilder sb = new StringBuilder ();
258 GetHierarchy (p.Controls, 0, sb);
259 string h = sb.ToString ();
260 int x = h.IndexOf (".TextBox");
261 // These are from the BoundColumns
262 Assert.IsTrue (x != -1, "textbox1");
263 x = h.IndexOf (".TextBox", x + 1);
264 Assert.IsTrue (x != -1, "textbox2");
265 x = h.IndexOf (".TextBox", x + 1);
266 Assert.IsTrue (x != -1, "textbox3");
267 x = h.IndexOf (".TextBox", x + 1);
268 Assert.IsTrue (x == -1, "textbox-end");
270 markup = ControlMarkup (p.Controls[0]);
271 markup = markup.Replace ("\t", "");
272 markup = markup.Replace ("\r", "");
273 markup = markup.Replace ("\n", "");
275 //Console.WriteLine("Markup:>{0}<", markup);
276 Assert.AreEqual (2, p.Columns.Count, "I1");
278 HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
282 [Ignore("Unfinished")]
283 public void InitializeReadOnlyEditCell ()
285 DataGridTest p = new DataGridTest ();
286 DataTable table = new DataTable ();
290 e = new EditCommandColumn ();
291 e.ButtonType = ButtonColumnType.LinkButton;
292 e.CancelText = "Cancel";
294 e.UpdateText = "Update";
296 table.Columns.Add (new DataColumn ("one", typeof (string)));
297 table.Columns.Add (new DataColumn ("two", typeof (string)));
298 table.Columns.Add (new DataColumn ("three", typeof (string)));
299 table.Rows.Add (new object[] { "1", "2", "3" });
301 p.DataSource = new DataView (table);
304 e = new EditCommandColumn ();
305 e.ButtonType = ButtonColumnType.PushButton;
307 e.CancelText = "Abbrechen";
308 e.EditText = "Bearbeiten";
309 e.UpdateText = "Refresh";
312 // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
315 // This will trigger EditCommandColumn.InitializeCell
316 p.CreateControls (true);
319 markup = ControlMarkup (p.Controls[0]);
320 markup = markup.Replace ("\t", "");
321 markup = markup.Replace ("\r", "");
322 markup = markup.Replace ("\n", "");
324 Assert.AreEqual (2, p.Columns.Count, "I1");
326 "<table border=\"0\" id=\"sucker\"><tr><td> </td><td> </td><td>one</td><td>two</td><td>three</td>" +
327 "</tr><tr><td><a>Update</a> <a>Cancel</a></td><td><input name type=\"submit\" value=\"Refresh\" /> " +
328 "<input name value=\"Abbrechen\" type=\"submit\" /></td>" +
329 "<td><input name=\"_ctl2:_ctl0\" type=\"text\" value=\"1\" /></td>" +
330 "<td><input name=\"_ctl2:_ctl1\" type=\"text\" value=\"2\" /></td>" +
331 "<td><input name=\"_ctl2:_ctl2\" type=\"text\" value=\"3\" /></td>" +
332 "</tr><tr><td> </td><td> </td><td> </td><td> </td><td> </td>" +
333 "</tr></table>", markup, "I2");
337 [Category ("NunitWeb")]
338 public void Validation_ValidatingValid ()
340 WebTest t = new WebTest ();
341 PageDelegates pd = new PageDelegates ();
342 pd.Load = Validation_Load;
343 pd.PreRender = Validation_PreRender;
344 t.Invoker = new PageInvoker (pd);
345 t.UserData = "ValidatingValid";
346 string html = t.Run ();
347 FormRequest fr = new FormRequest (t.Response, "form1");
349 fr.Controls.Add ("__EVENTTARGET");
350 fr.Controls.Add ("__EVENTARGUMENT");
351 fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
352 fr.Controls ["__EVENTARGUMENT"].Value = "";
354 t.UserData = "ValidatingValid";
360 [Category ("NunitWeb")]
361 [Ignore ("Possibly incorrectly constructed test - conflicts with fix for bug #471305")]
362 public void Validation_ValidatingInvalid () {
363 WebTest t = new WebTest ();
364 PageDelegates pd = new PageDelegates ();
365 pd.Load = Validation_Load;
366 pd.PreRender = Validation_PreRender;
367 t.Invoker = new PageInvoker (pd);
368 t.UserData = "ValidatingInvalid";
369 string html = t.Run ();
370 FormRequest fr = new FormRequest (t.Response, "form1");
372 fr.Controls.Add ("__EVENTTARGET");
373 fr.Controls.Add ("__EVENTARGUMENT");
374 fr.Controls ["__EVENTTARGET"].Value = (string)t.UserData;
375 fr.Controls ["__EVENTARGUMENT"].Value = "";
377 t.UserData = "ValidatingInvalid";
383 [Category ("NunitWeb")]
384 public void Validation_NotValidatingInvalid () {
385 WebTest t = new WebTest ();
386 PageDelegates pd = new PageDelegates ();
387 pd.Load = Validation_Load;
388 pd.PreRender = Validation_PreRender;
389 t.Invoker = new PageInvoker (pd);
390 t.UserData = "NotValidatingInvalid";
391 string html = t.Run ();
392 FormRequest fr = new FormRequest (t.Response, "form1");
394 fr.Controls.Add ("__EVENTTARGET");
395 fr.Controls.Add ("__EVENTARGUMENT");
396 fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
397 fr.Controls ["__EVENTARGUMENT"].Value = "";
399 t.UserData = "NotValidatingInvalid";
405 [Category ("NunitWeb")]
406 [Ignore ("Possibly incorrectly constructed test - conflicts with fix for bug #471305")]
407 public void Validation_ValidationGroupIncluded () {
408 WebTest t = new WebTest ();
409 PageDelegates pd = new PageDelegates ();
410 pd.Load = Validation_Load;
411 pd.PreRender = Validation_PreRender;
412 t.Invoker = new PageInvoker (pd);
413 t.UserData = "ValidationGroupIncluded";
414 string html = t.Run ();
415 FormRequest fr = new FormRequest (t.Response, "form1");
417 fr.Controls.Add ("__EVENTTARGET");
418 fr.Controls.Add ("__EVENTARGUMENT");
419 fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
420 fr.Controls ["__EVENTARGUMENT"].Value = "";
422 t.UserData = "ValidationGroupIncluded";
428 [Category ("NunitWeb")]
429 public void Validation_ValidationGroupNotIncluded () {
430 WebTest t = new WebTest ();
431 PageDelegates pd = new PageDelegates ();
432 pd.Load = Validation_Load;
433 pd.PreRender = Validation_PreRender;
434 t.Invoker = new PageInvoker (pd);
435 t.UserData = "ValidationGroupNotIncluded";
436 string html = t.Run ();
437 FormRequest fr = new FormRequest (t.Response, "form1");
439 fr.Controls.Add ("__EVENTTARGET");
440 fr.Controls.Add ("__EVENTARGUMENT");
441 fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
442 fr.Controls ["__EVENTARGUMENT"].Value = "";
444 t.UserData = "ValidationGroupNotIncluded";
449 public static void Validation_Load (Page p)
451 string testType = (string)WebTest.CurrentTest.UserData;
452 DataGridTest dg = new DataGridTest ();
456 e = new EditCommandColumn ();
457 e.ButtonType = ButtonColumnType.LinkButton;
458 e.CancelText = "Cancel";
460 e.UpdateText = "Update";
463 case "ValidatingValid":
464 case "ValidatingInvalid":
465 case "ValidationGroupIncluded":
466 case "ValidationGroupNotIncluded":
467 e.CausesValidation = true;
470 case "NotValidatingInvalid":
471 e.CausesValidation = false;
476 case "ValidationGroupIncluded":
477 case "ValidationGroupNotIncluded":
478 e.ValidationGroup = "Group1";
482 e.ValidationGroup = "";
488 TextBox tb = new TextBox ();
491 case "ValidatingValid":
495 case "ValidatingInvalid":
496 case "NotValidatingInvalid":
497 case "ValidationGroupIncluded":
498 case "ValidationGroupNotIncluded":
503 RequiredFieldValidator v = new RequiredFieldValidator ();
504 v.ControlToValidate = "Text1";
506 case "ValidationGroupIncluded":
507 v.ValidationGroup = "Group1";
510 case "ValidationGroupNotIncluded":
511 v.ValidationGroup = "NotGroup1";
515 v.ValidationGroup = "";
518 TemplateColumn tc = new TemplateColumn ();
519 tc.EditItemTemplate = new ValidatingEditTemplate (tb, v);
522 ObjectDataSource ods = new ObjectDataSource ("MyObjectDS", "Select");
523 ods.UpdateMethod = "Update";
524 ods.DataObjectTypeName = "MyObjectDS";
527 p.Form.Controls.Add (ods);
530 //dg.DataKeyField = "i";
532 //DataTable table = new DataTable ();
533 //table.Columns.Add (new DataColumn ("one", typeof (string)));
534 //table.Columns.Add (new DataColumn ("two", typeof (string)));
535 //table.Columns.Add (new DataColumn ("three", typeof (string)));
536 //table.Rows.Add (new object [] { "1", "2", "3" });
538 //dg.DataSource = new DataView (table);
540 dg.EditItemIndex = 0;
541 p.Form.Controls.Add (dg);
545 WebTest.CurrentTest.UserData = dg.Items [0].Cells [0].Controls [0].UniqueID;
549 public static void Validation_PreRender (Page p)
551 string testType = (string) WebTest.CurrentTest.UserData;
555 case "ValidatingValid":
556 case "ValidationGroupNotIncluded":
557 Assert.AreEqual (true, p.IsValid, "ValidatingValid");
559 case "ValidatingInvalid":
560 case "ValidationGroupIncluded":
561 Assert.AreEqual (false, p.IsValid, "ValidatingInvalid");
564 case "NotValidatingInvalid":
565 bool isValidated = true;
568 Assert.Fail ("NotValidatingInvalid IsValid == true");
571 catch (HttpException httpException) {
574 Assert.AreEqual(false, isValidated, "NotValidatingInvalid");
580 public class ValidatingEditTemplate : ITemplate
582 public ValidatingEditTemplate (params Control [] templateControls)
584 this.templateControls = new Control[templateControls.Length];
585 templateControls.CopyTo (this.templateControls, 0);
588 #region ITemplate Members
590 public void InstantiateIn (Control container)
592 foreach (Control c in templateControls) {
593 container.Controls.Add (c);
599 private Control[] templateControls;
605 public class MyObjectDS
607 public MyObjectDS () {
611 public MyObjectDS (int value) {
621 static MyObjectDS [] myData = null;
623 public static IList Select () {
624 if (myData == null) {
625 myData = new MyObjectDS [] { new MyObjectDS (1), new MyObjectDS (2), new MyObjectDS (3) };
630 public static void Update (MyObjectDS instance) {