| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633 |
- //
- // Tests for System.Web.UI.WebControls.EditCommandColumn.cs
- //
- // Author:
- // Peter Dennis Bartok ([email protected])
- //
- //
- // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using NUnit.Framework;
- using System;
- using System.Collections;
- using System.Data;
- using System.IO;
- using System.Globalization;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using MonoTests.stand_alone.WebHarness;
- using MonoTests.SystemWeb.Framework;
- namespace MonoTests.System.Web.UI.WebControls
- {
-
-
- [TestFixture]
- public class EditCommandColumnTest {
- private class DataGridTest : DataGrid {
- public ArrayList CreateColumns (PagedDataSource data_source, bool use_data_source) {
- return CreateColumnSet (data_source, use_data_source);
- }
- public void CreateControls (bool use_data_source) {
- CreateControlHierarchy (use_data_source);
- }
- }
-
- [Test]
- public void Defaults ()
- {
- EditCommandColumn e;
- e = new EditCommandColumn();
- Assert.AreEqual(ButtonColumnType.LinkButton, e.ButtonType, "D1");
- Assert.AreEqual(string.Empty, e.CancelText, "D2");
- Assert.AreEqual(string.Empty, e.EditText, "D3");
- Assert.AreEqual(string.Empty, e.UpdateText, "D4");
- Assert.AreEqual (true, e.CausesValidation, "CausesValidation");
- Assert.AreEqual (string.Empty, e.ValidationGroup, "ValidationGroup");
- }
- [Test]
- public void Properties () {
- EditCommandColumn e;
- e = new EditCommandColumn();
- e.ButtonType = ButtonColumnType.PushButton;
- Assert.AreEqual(ButtonColumnType.PushButton, e.ButtonType, "P1");
- e.CancelText = "Cancel this!";
- Assert.AreEqual("Cancel this!", e.CancelText, "D2");
- e.EditText = "Edit me good";
- Assert.AreEqual("Edit me good", e.EditText, "D3");
- e.UpdateText = "Update? What update?";
- Assert.AreEqual("Update? What update?", e.UpdateText, "D4");
- e.CausesValidation = false;
- Assert.AreEqual (false, e.CausesValidation, "CausesValidation");
- e.ValidationGroup = "test";
- Assert.AreEqual ("test", e.ValidationGroup, "ValidationGroup");
- }
- private string ControlMarkup(Control c) {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
- c.RenderControl (tw);
- return sw.ToString ();
- }
- private void ShowControlsRecursive (Control c, int depth) {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
- c.RenderControl (tw);
- Console.WriteLine (sw.ToString ());
- Console.WriteLine (c);
- foreach (Control child in c.Controls)
- ShowControlsRecursive (child, depth + 5);
- }
- [Test]
- public void InitializeCell ()
- {
- 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>";
- DataGridTest p = new DataGridTest ();
- DataTable table = new DataTable ();
- EditCommandColumn e;
- string markup;
- e = new EditCommandColumn();
- e.ButtonType = ButtonColumnType.LinkButton;
- e.CancelText = "Cancel";
- e.EditText = "Edit";
- e.UpdateText = "Update";
- table.Columns.Add (new DataColumn ("one", typeof (string)));
- table.Columns.Add (new DataColumn ("two", typeof (string)));
- table.Columns.Add (new DataColumn ("three", typeof (string)));
- table.Rows.Add (new object [] { "1", "2", "3" });
- p.DataSource = new DataView (table);
- p.Columns.Add(e);
- e = new EditCommandColumn();
- e.ButtonType = ButtonColumnType.PushButton;
- e.CancelText = "Abbrechen";
- e.EditText = "Bearbeiten";
- e.UpdateText = "Refresh";
- p.Columns.Add(e);
- // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
- p.CreateControls (true);
- p.ID = "sucker";
- Assert.AreEqual (2, p.Columns.Count, "I1");
- markup = ControlMarkup(p.Controls[0]);
- markup = markup.Replace("\t", "");
- markup = markup.Replace ("\r", "");
- markup = markup.Replace ("\n", "");
- HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
- //ShowControlsRecursive (p.Controls [0], 1);
- }
- [Test]
- public void ThisIsADGTest ()
- {
- 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>";
- DataGridTest p = new DataGridTest ();
- DataTable table = new DataTable ();
- EditCommandColumn e;
- string markup;
- e = new EditCommandColumn();
- e.ButtonType = ButtonColumnType.LinkButton;
- e.CancelText = "Cancel";
- e.EditText = "Edit";
- e.UpdateText = "Update";
- table.Columns.Add (new DataColumn ("one", typeof (string)));
- table.Columns.Add (new DataColumn ("two", typeof (string)));
- table.Columns.Add (new DataColumn ("three", typeof (string)));
- table.Rows.Add (new object [] { "1", "2", "3" });
- p.DataSource = new DataView (table);
- p.Columns.Add(e);
- e = new EditCommandColumn();
- e.ButtonType = ButtonColumnType.PushButton;
- e.CancelText = "Abbrechen";
- e.EditText = "Bearbeiten";
- e.UpdateText = "Refresh";
- p.Columns.Add(e);
- p.CreateControls (true);
- // This is the test we want to run: setting the ID of the table created by
- // the datagrid overrides the using the ID of the datagrid itself and uses
- // the table ClientID instead.
- p.ID = "sucker";
- p.Controls [0].ID = "tbl";
- Assert.AreEqual (2, p.Columns.Count, "I1");
- markup = ControlMarkup(p.Controls[0]);
- markup = markup.Replace("\t", "");
- markup = markup.Replace ("\r", "");
- markup = markup.Replace ("\n", "");
-
- HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
- }
- static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
- {
- foreach (Control c in coll) {
- sb.AppendFormat ("{0}{1}\n", new string (' ', 2 * level), c.GetType ());
- GetHierarchy (c.Controls, level + 1, sb);
- }
- }
- [Test]
- public void InitializeEditCell ()
- {
- 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>";
- DataGridTest p = new DataGridTest ();
- DataTable table = new DataTable ();
- EditCommandColumn e;
- string markup;
- e = new EditCommandColumn();
- e.ButtonType = ButtonColumnType.LinkButton;
- e.CancelText = "Cancel";
- e.EditText = "Edit";
- e.UpdateText = "Update";
- table.Columns.Add (new DataColumn ("one", typeof (string)));
- table.Columns.Add (new DataColumn ("two", typeof (string)));
- table.Columns.Add (new DataColumn ("three", typeof (string)));
- table.Rows.Add (new object [] { "1", "2", "3" });
- p.DataSource = new DataView (table);
- p.Columns.Add(e);
- e = new EditCommandColumn();
- e.ButtonType = ButtonColumnType.PushButton;
- e.CancelText = "Abbrechen";
- e.EditText = "Bearbeiten";
- e.UpdateText = "Refresh";
- p.Columns.Add(e);
- // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
- p.EditItemIndex = 0;
- // This will trigger EditCommandColumn.InitializeCell
- p.CreateControls (true);
- p.ID = "sucker";
- StringBuilder sb = new StringBuilder ();
- GetHierarchy (p.Controls, 0, sb);
- string h = sb.ToString ();
- int x = h.IndexOf (".TextBox");
- // These are from the BoundColumns
- Assert.IsTrue (x != -1, "textbox1");
- x = h.IndexOf (".TextBox", x + 1);
- Assert.IsTrue (x != -1, "textbox2");
- x = h.IndexOf (".TextBox", x + 1);
- Assert.IsTrue (x != -1, "textbox3");
- x = h.IndexOf (".TextBox", x + 1);
- Assert.IsTrue (x == -1, "textbox-end");
- markup = ControlMarkup (p.Controls[0]);
- markup = markup.Replace ("\t", "");
- markup = markup.Replace ("\r", "");
- markup = markup.Replace ("\n", "");
- //Console.WriteLine("Markup:>{0}<", markup);
- Assert.AreEqual (2, p.Columns.Count, "I1");
- HtmlDiff.AssertAreEqual (origHtml, markup, "I2");
- }
- [Test]
- [Ignore("Unfinished")]
- public void InitializeReadOnlyEditCell ()
- {
- DataGridTest p = new DataGridTest ();
- DataTable table = new DataTable ();
- EditCommandColumn e;
- string markup;
- e = new EditCommandColumn ();
- e.ButtonType = ButtonColumnType.LinkButton;
- e.CancelText = "Cancel";
- e.EditText = "Edit";
- e.UpdateText = "Update";
- table.Columns.Add (new DataColumn ("one", typeof (string)));
- table.Columns.Add (new DataColumn ("two", typeof (string)));
- table.Columns.Add (new DataColumn ("three", typeof (string)));
- table.Rows.Add (new object[] { "1", "2", "3" });
- p.DataSource = new DataView (table);
- p.Columns.Add (e);
- e = new EditCommandColumn ();
- e.ButtonType = ButtonColumnType.PushButton;
-
- e.CancelText = "Abbrechen";
- e.EditText = "Bearbeiten";
- e.UpdateText = "Refresh";
- p.Columns.Add (e);
- // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
- p.EditItemIndex = 0;
- // This will trigger EditCommandColumn.InitializeCell
- p.CreateControls (true);
- p.ID = "sucker";
- markup = ControlMarkup (p.Controls[0]);
- markup = markup.Replace ("\t", "");
- markup = markup.Replace ("\r", "");
- markup = markup.Replace ("\n", "");
- Assert.AreEqual (2, p.Columns.Count, "I1");
- Assert.AreEqual (
- "<table border=\"0\" id=\"sucker\"><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 type=\"submit\" value=\"Refresh\" /> " +
- "<input name value=\"Abbrechen\" type=\"submit\" /></td>" +
- "<td><input name=\"_ctl2:_ctl0\" type=\"text\" value=\"1\" /></td>" +
- "<td><input name=\"_ctl2:_ctl1\" type=\"text\" value=\"2\" /></td>" +
- "<td><input name=\"_ctl2:_ctl2\" type=\"text\" value=\"3\" /></td>" +
- "</tr><tr><td> </td><td> </td><td> </td><td> </td><td> </td>" +
- "</tr></table>", markup, "I2");
- }
- [Test]
- [Category ("NunitWeb")]
- public void Validation_ValidatingValid ()
- {
- WebTest t = new WebTest ();
- PageDelegates pd = new PageDelegates ();
- pd.Load = Validation_Load;
- pd.PreRender = Validation_PreRender;
- t.Invoker = new PageInvoker (pd);
- t.UserData = "ValidatingValid";
- string html = t.Run ();
- FormRequest fr = new FormRequest (t.Response, "form1");
- fr.Controls.Add ("__EVENTTARGET");
- fr.Controls.Add ("__EVENTARGUMENT");
- fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
- fr.Controls ["__EVENTARGUMENT"].Value = "";
- t.Request = fr;
- t.UserData = "ValidatingValid";
-
- html = t.Run ();
- }
- [Test]
- [Category ("NunitWeb")]
- [Ignore ("Possibly incorrectly constructed test - conflicts with fix for bug #471305")]
- public void Validation_ValidatingInvalid () {
- WebTest t = new WebTest ();
- PageDelegates pd = new PageDelegates ();
- pd.Load = Validation_Load;
- pd.PreRender = Validation_PreRender;
- t.Invoker = new PageInvoker (pd);
- t.UserData = "ValidatingInvalid";
- string html = t.Run ();
- FormRequest fr = new FormRequest (t.Response, "form1");
- fr.Controls.Add ("__EVENTTARGET");
- fr.Controls.Add ("__EVENTARGUMENT");
- fr.Controls ["__EVENTTARGET"].Value = (string)t.UserData;
- fr.Controls ["__EVENTARGUMENT"].Value = "";
- t.Request = fr;
- t.UserData = "ValidatingInvalid";
- html = t.Run ();
- }
- [Test]
- [Category ("NunitWeb")]
- public void Validation_NotValidatingInvalid () {
- WebTest t = new WebTest ();
- PageDelegates pd = new PageDelegates ();
- pd.Load = Validation_Load;
- pd.PreRender = Validation_PreRender;
- t.Invoker = new PageInvoker (pd);
- t.UserData = "NotValidatingInvalid";
- string html = t.Run ();
- FormRequest fr = new FormRequest (t.Response, "form1");
- fr.Controls.Add ("__EVENTTARGET");
- fr.Controls.Add ("__EVENTARGUMENT");
- fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
- fr.Controls ["__EVENTARGUMENT"].Value = "";
- t.Request = fr;
- t.UserData = "NotValidatingInvalid";
- html = t.Run ();
- }
- [Test]
- [Category ("NunitWeb")]
- [Ignore ("Possibly incorrectly constructed test - conflicts with fix for bug #471305")]
- public void Validation_ValidationGroupIncluded () {
- WebTest t = new WebTest ();
- PageDelegates pd = new PageDelegates ();
- pd.Load = Validation_Load;
- pd.PreRender = Validation_PreRender;
- t.Invoker = new PageInvoker (pd);
- t.UserData = "ValidationGroupIncluded";
- string html = t.Run ();
- FormRequest fr = new FormRequest (t.Response, "form1");
- fr.Controls.Add ("__EVENTTARGET");
- fr.Controls.Add ("__EVENTARGUMENT");
- fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
- fr.Controls ["__EVENTARGUMENT"].Value = "";
- t.Request = fr;
- t.UserData = "ValidationGroupIncluded";
- html = t.Run ();
- }
- [Test]
- [Category ("NunitWeb")]
- public void Validation_ValidationGroupNotIncluded () {
- WebTest t = new WebTest ();
- PageDelegates pd = new PageDelegates ();
- pd.Load = Validation_Load;
- pd.PreRender = Validation_PreRender;
- t.Invoker = new PageInvoker (pd);
- t.UserData = "ValidationGroupNotIncluded";
- string html = t.Run ();
- FormRequest fr = new FormRequest (t.Response, "form1");
- fr.Controls.Add ("__EVENTTARGET");
- fr.Controls.Add ("__EVENTARGUMENT");
- fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
- fr.Controls ["__EVENTARGUMENT"].Value = "";
- t.Request = fr;
- t.UserData = "ValidationGroupNotIncluded";
- html = t.Run ();
- }
- public static void Validation_Load (Page p)
- {
- string testType = (string)WebTest.CurrentTest.UserData;
- DataGridTest dg = new DataGridTest ();
- dg.ID = "mygrid";
- EditCommandColumn e;
- e = new EditCommandColumn ();
- e.ButtonType = ButtonColumnType.LinkButton;
- e.CancelText = "Cancel";
- e.EditText = "Edit";
- e.UpdateText = "Update";
- switch (testType) {
- case "ValidatingValid":
- case "ValidatingInvalid":
- case "ValidationGroupIncluded":
- case "ValidationGroupNotIncluded":
- e.CausesValidation = true;
- break;
- case "NotValidatingInvalid":
- e.CausesValidation = false;
- break;
- }
- switch (testType) {
- case "ValidationGroupIncluded":
- case "ValidationGroupNotIncluded":
- e.ValidationGroup = "Group1";
- break;
- default:
- e.ValidationGroup = "";
- break;
- }
- dg.Columns.Add (e);
- TextBox tb = new TextBox ();
- tb.ID = "Text1";
- switch (testType) {
- case "ValidatingValid":
- tb.Text = "111";
- break;
- case "ValidatingInvalid":
- case "NotValidatingInvalid":
- case "ValidationGroupIncluded":
- case "ValidationGroupNotIncluded":
- tb.Text = "";
- break;
- }
- RequiredFieldValidator v = new RequiredFieldValidator ();
- v.ControlToValidate = "Text1";
- switch (testType) {
- case "ValidationGroupIncluded":
- v.ValidationGroup = "Group1";
- break;
- case "ValidationGroupNotIncluded":
- v.ValidationGroup = "NotGroup1";
- break;
- default:
- v.ValidationGroup = "";
- break;
- }
- TemplateColumn tc = new TemplateColumn ();
- tc.EditItemTemplate = new ValidatingEditTemplate (tb, v);
- dg.Columns.Add (tc);
- ObjectDataSource ods = new ObjectDataSource ("MyObjectDS", "Select");
- ods.UpdateMethod = "Update";
- ods.DataObjectTypeName = "MyObjectDS";
- ods.ID = "MyDS";
- p.Form.Controls.Add (ods);
- dg.DataSource = ods;
- //dg.DataKeyField = "i";
- //DataTable table = new DataTable ();
- //table.Columns.Add (new DataColumn ("one", typeof (string)));
- //table.Columns.Add (new DataColumn ("two", typeof (string)));
- //table.Columns.Add (new DataColumn ("three", typeof (string)));
- //table.Rows.Add (new object [] { "1", "2", "3" });
- //dg.DataSource = new DataView (table);
- dg.EditItemIndex = 0;
- p.Form.Controls.Add (dg);
- dg.DataBind ();
- if (!p.IsPostBack) {
- WebTest.CurrentTest.UserData = dg.Items [0].Cells [0].Controls [0].UniqueID;
- }
- }
- public static void Validation_PreRender (Page p)
- {
- string testType = (string) WebTest.CurrentTest.UserData;
- if (p.IsPostBack) {
- switch (testType) {
- case "ValidatingValid":
- case "ValidationGroupNotIncluded":
- Assert.AreEqual (true, p.IsValid, "ValidatingValid");
- break;
- case "ValidatingInvalid":
- case "ValidationGroupIncluded":
- Assert.AreEqual (false, p.IsValid, "ValidatingInvalid");
- break;
- case "NotValidatingInvalid":
- bool isValidated = true;
- try {
- if (p.IsValid) {
- Assert.Fail ("NotValidatingInvalid IsValid == true");
- }
- }
- catch (HttpException httpException) {
- isValidated = false;
- }
- Assert.AreEqual(false, isValidated, "NotValidatingInvalid");
- break;
- }
- }
- }
- public class ValidatingEditTemplate : ITemplate
- {
- public ValidatingEditTemplate (params Control [] templateControls)
- {
- this.templateControls = new Control[templateControls.Length];
- templateControls.CopyTo (this.templateControls, 0);
- }
- #region ITemplate Members
- public void InstantiateIn (Control container)
- {
- foreach (Control c in templateControls) {
- container.Controls.Add (c);
- }
- }
- #endregion
- private Control[] templateControls;
- }
- }
- }
- #region MyObjectDS
- public class MyObjectDS
- {
- public MyObjectDS () {
- _i = 0;
- }
- public MyObjectDS (int value) {
- _i = value;
- }
- private int _i;
- public int i {
- get { return _i; }
- set { _i = value; }
- }
- static MyObjectDS [] myData = null;
- public static IList Select () {
- if (myData == null) {
- myData = new MyObjectDS [] { new MyObjectDS (1), new MyObjectDS (2), new MyObjectDS (3) };
- }
- return myData;
- }
- public static void Update (MyObjectDS instance) {
- }
- }
- #endregion
|