EditCommandColumnTest.cs 21 KB

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