EditCommandColumnTest.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. DataGridTest p = new DataGridTest ();
  104. DataTable table = new DataTable ();
  105. EditCommandColumn e;
  106. string markup;
  107. e = new EditCommandColumn();
  108. e.ButtonType = ButtonColumnType.LinkButton;
  109. e.CancelText = "Cancel";
  110. e.EditText = "Edit";
  111. e.UpdateText = "Update";
  112. table.Columns.Add (new DataColumn ("one", typeof (string)));
  113. table.Columns.Add (new DataColumn ("two", typeof (string)));
  114. table.Columns.Add (new DataColumn ("three", typeof (string)));
  115. table.Rows.Add (new object [] { "1", "2", "3" });
  116. p.DataSource = new DataView (table);
  117. p.Columns.Add(e);
  118. e = new EditCommandColumn();
  119. e.ButtonType = ButtonColumnType.PushButton;
  120. e.CancelText = "Abbrechen";
  121. e.EditText = "Bearbeiten";
  122. e.UpdateText = "Refresh";
  123. p.Columns.Add(e);
  124. // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
  125. p.CreateControls (true);
  126. p.ID = "sucker";
  127. Assert.AreEqual (2, p.Columns.Count, "I1");
  128. markup = ControlMarkup(p.Controls[0]);
  129. markup = markup.Replace("\t", "");
  130. markup = markup.Replace ("\r", "");
  131. markup = markup.Replace ("\n", "");
  132. #if NET_2_0
  133. HtmlDiff.AssertAreEqual (
  134. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  135. "</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>" +
  136. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  137. "</tr></table>", markup, "I2");
  138. #else
  139. Assert.AreEqual (
  140. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  141. "</tr><tr><td><a>Edit</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td>" +
  142. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  143. "</tr></table>", markup, "I2");
  144. #endif
  145. //ShowControlsRecursive (p.Controls [0], 1);
  146. }
  147. [Test]
  148. public void ThisIsADGTest () {
  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. #if NET_2_0
  182. HtmlDiff.AssertAreEqual (
  183. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  184. "</tr><tr><td><a>Edit</a></td><td><input name=\"sucker_tbl$ctl02$ctl00\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td>" +
  185. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  186. "</tr></table>", markup, "I2");
  187. #else
  188. Assert.AreEqual (
  189. "<table border=\"0\" id=\"sucker_tbl\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  190. "</tr><tr><td><a>Edit</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td>" +
  191. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  192. "</tr></table>", markup, "I2");
  193. #endif
  194. }
  195. static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
  196. {
  197. foreach (Control c in coll) {
  198. sb.AppendFormat ("{0}{1}\n", new string (' ', 2 * level), c.GetType ());
  199. GetHierarchy (c.Controls, level + 1, sb);
  200. }
  201. }
  202. [Test]
  203. public void InitializeEditCell () {
  204. DataGridTest p = new DataGridTest ();
  205. DataTable table = new DataTable ();
  206. EditCommandColumn e;
  207. string markup;
  208. e = new EditCommandColumn();
  209. e.ButtonType = ButtonColumnType.LinkButton;
  210. e.CancelText = "Cancel";
  211. e.EditText = "Edit";
  212. e.UpdateText = "Update";
  213. table.Columns.Add (new DataColumn ("one", typeof (string)));
  214. table.Columns.Add (new DataColumn ("two", typeof (string)));
  215. table.Columns.Add (new DataColumn ("three", typeof (string)));
  216. table.Rows.Add (new object [] { "1", "2", "3" });
  217. p.DataSource = new DataView (table);
  218. p.Columns.Add(e);
  219. e = new EditCommandColumn();
  220. e.ButtonType = ButtonColumnType.PushButton;
  221. e.CancelText = "Abbrechen";
  222. e.EditText = "Bearbeiten";
  223. e.UpdateText = "Refresh";
  224. p.Columns.Add(e);
  225. // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
  226. p.EditItemIndex = 0;
  227. // This will trigger EditCommandColumn.InitializeCell
  228. p.CreateControls (true);
  229. p.ID = "sucker";
  230. StringBuilder sb = new StringBuilder ();
  231. GetHierarchy (p.Controls, 0, sb);
  232. string h = sb.ToString ();
  233. int x = h.IndexOf (".TextBox");
  234. // These are from the BoundColumns
  235. Assert.IsTrue (x != -1, "textbox1");
  236. x = h.IndexOf (".TextBox", x + 1);
  237. Assert.IsTrue (x != -1, "textbox2");
  238. x = h.IndexOf (".TextBox", x + 1);
  239. Assert.IsTrue (x != -1, "textbox3");
  240. x = h.IndexOf (".TextBox", x + 1);
  241. Assert.IsTrue (x == -1, "textbox-end");
  242. markup = ControlMarkup (p.Controls[0]);
  243. markup = markup.Replace ("\t", "");
  244. markup = markup.Replace ("\r", "");
  245. markup = markup.Replace ("\n", "");
  246. //Console.WriteLine("Markup:>{0}<", markup);
  247. Assert.AreEqual (2, p.Columns.Count, "I1");
  248. #if NET_2_0
  249. HtmlDiff.AssertAreEqual (
  250. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  251. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" />&nbsp;" +
  252. "<input name=\"sucker$ctl02$ctl01\" type=\"submit\" value=\"Abbrechen\" /></td>" +
  253. "<td><input name=\"sucker$ctl02$ctl02\" type=\"text\" value=\"1\" /></td>" +
  254. "<td><input name=\"sucker$ctl02$ctl03\" type=\"text\" value=\"2\" /></td>" +
  255. "<td><input name=\"sucker$ctl02$ctl04\" type=\"text\" value=\"3\" /></td>" +
  256. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  257. "</tr></table>", markup, "I2");
  258. #else
  259. Assert.AreEqual (
  260. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  261. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Refresh\" />&nbsp;" +
  262. "<input name=\"sucker:_ctl1:_ctl1\" type=\"submit\" value=\"Abbrechen\" /></td>" +
  263. "<td><input name=\"sucker:_ctl1:_ctl2\" type=\"text\" value=\"1\" /></td>" +
  264. "<td><input name=\"sucker:_ctl1:_ctl3\" type=\"text\" value=\"2\" /></td>" +
  265. "<td><input name=\"sucker:_ctl1:_ctl4\" type=\"text\" value=\"3\" /></td>" +
  266. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  267. "</tr></table>", markup, "I2");
  268. #endif
  269. }
  270. [Test]
  271. [Ignore("Unfinished")]
  272. public void InitializeReadOnlyEditCell ()
  273. {
  274. DataGridTest p = new DataGridTest ();
  275. DataTable table = new DataTable ();
  276. EditCommandColumn e;
  277. string markup;
  278. e = new EditCommandColumn ();
  279. e.ButtonType = ButtonColumnType.LinkButton;
  280. e.CancelText = "Cancel";
  281. e.EditText = "Edit";
  282. e.UpdateText = "Update";
  283. table.Columns.Add (new DataColumn ("one", typeof (string)));
  284. table.Columns.Add (new DataColumn ("two", typeof (string)));
  285. table.Columns.Add (new DataColumn ("three", typeof (string)));
  286. table.Rows.Add (new object[] { "1", "2", "3" });
  287. p.DataSource = new DataView (table);
  288. p.Columns.Add (e);
  289. e = new EditCommandColumn ();
  290. e.ButtonType = ButtonColumnType.PushButton;
  291. e.CancelText = "Abbrechen";
  292. e.EditText = "Bearbeiten";
  293. e.UpdateText = "Refresh";
  294. p.Columns.Add (e);
  295. // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
  296. p.EditItemIndex = 0;
  297. // This will trigger EditCommandColumn.InitializeCell
  298. p.CreateControls (true);
  299. p.ID = "sucker";
  300. markup = ControlMarkup (p.Controls[0]);
  301. markup = markup.Replace ("\t", "");
  302. markup = markup.Replace ("\r", "");
  303. markup = markup.Replace ("\n", "");
  304. Assert.AreEqual (2, p.Columns.Count, "I1");
  305. Assert.AreEqual (
  306. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  307. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name type=\"submit\" value=\"Refresh\" />&nbsp;" +
  308. "<input name value=\"Abbrechen\" type=\"submit\" /></td>" +
  309. "<td><input name=\"_ctl2:_ctl0\" type=\"text\" value=\"1\" /></td>" +
  310. "<td><input name=\"_ctl2:_ctl1\" type=\"text\" value=\"2\" /></td>" +
  311. "<td><input name=\"_ctl2:_ctl2\" type=\"text\" value=\"3\" /></td>" +
  312. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  313. "</tr></table>", markup, "I2");
  314. }
  315. #if NET_2_0
  316. [Test]
  317. [Category ("NunitWeb")]
  318. public void Validation_ValidatingValid ()
  319. {
  320. WebTest t = new WebTest ();
  321. PageDelegates pd = new PageDelegates ();
  322. pd.Load = Validation_Load;
  323. pd.PreRender = Validation_PreRender;
  324. t.Invoker = new PageInvoker (pd);
  325. t.UserData = "ValidatingValid";
  326. string html = t.Run ();
  327. FormRequest fr = new FormRequest (t.Response, "form1");
  328. fr.Controls.Add ("__EVENTTARGET");
  329. fr.Controls.Add ("__EVENTARGUMENT");
  330. fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
  331. fr.Controls ["__EVENTARGUMENT"].Value = "";
  332. t.Request = fr;
  333. t.UserData = "ValidatingValid";
  334. html = t.Run ();
  335. }
  336. [Test]
  337. [Category ("NunitWeb")]
  338. [Ignore ("Possibly incorrectly constructed test - conflicts with fix for bug #471305")]
  339. public void Validation_ValidatingInvalid () {
  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 = "ValidatingInvalid";
  346. string html = t.Run ();
  347. FormRequest fr = new FormRequest (t.Response, "form1");
  348. fr.Controls.Add ("__EVENTTARGET");
  349. fr.Controls.Add ("__EVENTARGUMENT");
  350. fr.Controls ["__EVENTTARGET"].Value = (string)t.UserData;
  351. fr.Controls ["__EVENTARGUMENT"].Value = "";
  352. t.Request = fr;
  353. t.UserData = "ValidatingInvalid";
  354. html = t.Run ();
  355. }
  356. [Test]
  357. [Category ("NunitWeb")]
  358. public void Validation_NotValidatingInvalid () {
  359. WebTest t = new WebTest ();
  360. PageDelegates pd = new PageDelegates ();
  361. pd.Load = Validation_Load;
  362. pd.PreRender = Validation_PreRender;
  363. t.Invoker = new PageInvoker (pd);
  364. t.UserData = "NotValidatingInvalid";
  365. string html = t.Run ();
  366. FormRequest fr = new FormRequest (t.Response, "form1");
  367. fr.Controls.Add ("__EVENTTARGET");
  368. fr.Controls.Add ("__EVENTARGUMENT");
  369. fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
  370. fr.Controls ["__EVENTARGUMENT"].Value = "";
  371. t.Request = fr;
  372. t.UserData = "NotValidatingInvalid";
  373. html = t.Run ();
  374. }
  375. [Test]
  376. [Category ("NunitWeb")]
  377. [Ignore ("Possibly incorrectly constructed test - conflicts with fix for bug #471305")]
  378. public void Validation_ValidationGroupIncluded () {
  379. WebTest t = new WebTest ();
  380. PageDelegates pd = new PageDelegates ();
  381. pd.Load = Validation_Load;
  382. pd.PreRender = Validation_PreRender;
  383. t.Invoker = new PageInvoker (pd);
  384. t.UserData = "ValidationGroupIncluded";
  385. string html = t.Run ();
  386. FormRequest fr = new FormRequest (t.Response, "form1");
  387. fr.Controls.Add ("__EVENTTARGET");
  388. fr.Controls.Add ("__EVENTARGUMENT");
  389. fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
  390. fr.Controls ["__EVENTARGUMENT"].Value = "";
  391. t.Request = fr;
  392. t.UserData = "ValidationGroupIncluded";
  393. html = t.Run ();
  394. }
  395. [Test]
  396. [Category ("NunitWeb")]
  397. public void Validation_ValidationGroupNotIncluded () {
  398. WebTest t = new WebTest ();
  399. PageDelegates pd = new PageDelegates ();
  400. pd.Load = Validation_Load;
  401. pd.PreRender = Validation_PreRender;
  402. t.Invoker = new PageInvoker (pd);
  403. t.UserData = "ValidationGroupNotIncluded";
  404. string html = t.Run ();
  405. 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 = "";
  410. t.Request = fr;
  411. t.UserData = "ValidationGroupNotIncluded";
  412. html = t.Run ();
  413. }
  414. public static void Validation_Load (Page p)
  415. {
  416. string testType = (string)WebTest.CurrentTest.UserData;
  417. DataGridTest dg = new DataGridTest ();
  418. dg.ID = "mygrid";
  419. EditCommandColumn e;
  420. e = new EditCommandColumn ();
  421. e.ButtonType = ButtonColumnType.LinkButton;
  422. e.CancelText = "Cancel";
  423. e.EditText = "Edit";
  424. e.UpdateText = "Update";
  425. switch (testType) {
  426. case "ValidatingValid":
  427. case "ValidatingInvalid":
  428. case "ValidationGroupIncluded":
  429. case "ValidationGroupNotIncluded":
  430. e.CausesValidation = true;
  431. break;
  432. case "NotValidatingInvalid":
  433. e.CausesValidation = false;
  434. break;
  435. }
  436. switch (testType) {
  437. case "ValidationGroupIncluded":
  438. case "ValidationGroupNotIncluded":
  439. e.ValidationGroup = "Group1";
  440. break;
  441. default:
  442. e.ValidationGroup = "";
  443. break;
  444. }
  445. dg.Columns.Add (e);
  446. TextBox tb = new TextBox ();
  447. tb.ID = "Text1";
  448. switch (testType) {
  449. case "ValidatingValid":
  450. tb.Text = "111";
  451. break;
  452. case "ValidatingInvalid":
  453. case "NotValidatingInvalid":
  454. case "ValidationGroupIncluded":
  455. case "ValidationGroupNotIncluded":
  456. tb.Text = "";
  457. break;
  458. }
  459. RequiredFieldValidator v = new RequiredFieldValidator ();
  460. v.ControlToValidate = "Text1";
  461. switch (testType) {
  462. case "ValidationGroupIncluded":
  463. v.ValidationGroup = "Group1";
  464. break;
  465. case "ValidationGroupNotIncluded":
  466. v.ValidationGroup = "NotGroup1";
  467. break;
  468. default:
  469. v.ValidationGroup = "";
  470. break;
  471. }
  472. TemplateColumn tc = new TemplateColumn ();
  473. tc.EditItemTemplate = new ValidatingEditTemplate (tb, v);
  474. dg.Columns.Add (tc);
  475. ObjectDataSource ods = new ObjectDataSource ("MyObjectDS", "Select");
  476. ods.UpdateMethod = "Update";
  477. ods.DataObjectTypeName = "MyObjectDS";
  478. ods.ID = "MyDS";
  479. p.Form.Controls.Add (ods);
  480. dg.DataSource = ods;
  481. //dg.DataKeyField = "i";
  482. //DataTable table = new DataTable ();
  483. //table.Columns.Add (new DataColumn ("one", typeof (string)));
  484. //table.Columns.Add (new DataColumn ("two", typeof (string)));
  485. //table.Columns.Add (new DataColumn ("three", typeof (string)));
  486. //table.Rows.Add (new object [] { "1", "2", "3" });
  487. //dg.DataSource = new DataView (table);
  488. dg.EditItemIndex = 0;
  489. p.Form.Controls.Add (dg);
  490. dg.DataBind ();
  491. if (!p.IsPostBack) {
  492. WebTest.CurrentTest.UserData = dg.Items [0].Cells [0].Controls [0].UniqueID;
  493. }
  494. }
  495. public static void Validation_PreRender (Page p)
  496. {
  497. string testType = (string) WebTest.CurrentTest.UserData;
  498. if (p.IsPostBack) {
  499. switch (testType) {
  500. case "ValidatingValid":
  501. case "ValidationGroupNotIncluded":
  502. Assert.AreEqual (true, p.IsValid, "ValidatingValid");
  503. break;
  504. case "ValidatingInvalid":
  505. case "ValidationGroupIncluded":
  506. Assert.AreEqual (false, p.IsValid, "ValidatingInvalid");
  507. break;
  508. case "NotValidatingInvalid":
  509. bool isValidated = true;
  510. try {
  511. if (p.IsValid) {
  512. Assert.Fail ("NotValidatingInvalid IsValid == true");
  513. }
  514. }
  515. catch (HttpException httpException) {
  516. isValidated = false;
  517. }
  518. Assert.AreEqual(false, isValidated, "NotValidatingInvalid");
  519. break;
  520. }
  521. }
  522. }
  523. public class ValidatingEditTemplate : ITemplate
  524. {
  525. public ValidatingEditTemplate (params Control [] templateControls)
  526. {
  527. this.templateControls = new Control[templateControls.Length];
  528. templateControls.CopyTo (this.templateControls, 0);
  529. }
  530. #region ITemplate Members
  531. public void InstantiateIn (Control container)
  532. {
  533. foreach (Control c in templateControls) {
  534. container.Controls.Add (c);
  535. }
  536. }
  537. #endregion
  538. private Control[] templateControls;
  539. }
  540. #endif
  541. }
  542. }
  543. #if NET_2_0
  544. #region MyObjectDS
  545. public class MyObjectDS
  546. {
  547. public MyObjectDS () {
  548. _i = 0;
  549. }
  550. public MyObjectDS (int value) {
  551. _i = value;
  552. }
  553. private int _i;
  554. public int i {
  555. get { return _i; }
  556. set { _i = value; }
  557. }
  558. static MyObjectDS [] myData = null;
  559. public static IList Select () {
  560. if (myData == null) {
  561. myData = new MyObjectDS [] { new MyObjectDS (1), new MyObjectDS (2), new MyObjectDS (3) };
  562. }
  563. return myData;
  564. }
  565. public static void Update (MyObjectDS instance) {
  566. }
  567. }
  568. #endregion
  569. #endif