EditCommandColumnTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. namespace MonoTests.System.Web.UI.WebControls
  40. {
  41. [TestFixture]
  42. public class EditCommandColumnTest {
  43. private class DataGridTest : DataGrid {
  44. public ArrayList CreateColumns (PagedDataSource data_source, bool use_data_source) {
  45. return CreateColumnSet (data_source, use_data_source);
  46. }
  47. public void CreateControls (bool use_data_source) {
  48. CreateControlHierarchy (use_data_source);
  49. }
  50. }
  51. [Test]
  52. public void Defaults ()
  53. {
  54. EditCommandColumn e;
  55. e = new EditCommandColumn();
  56. Assert.AreEqual(ButtonColumnType.LinkButton, e.ButtonType, "D1");
  57. Assert.AreEqual(string.Empty, e.CancelText, "D2");
  58. Assert.AreEqual(string.Empty, e.EditText, "D3");
  59. Assert.AreEqual(string.Empty, e.UpdateText, "D4");
  60. }
  61. [Test]
  62. public void Properties () {
  63. EditCommandColumn e;
  64. e = new EditCommandColumn();
  65. e.ButtonType = ButtonColumnType.PushButton;
  66. Assert.AreEqual(ButtonColumnType.PushButton, e.ButtonType, "P1");
  67. e.CancelText = "Cancel this!";
  68. Assert.AreEqual("Cancel this!", e.CancelText, "D2");
  69. e.EditText = "Edit me good";
  70. Assert.AreEqual("Edit me good", e.EditText, "D3");
  71. e.UpdateText = "Update? What update?";
  72. Assert.AreEqual("Update? What update?", e.UpdateText, "D4");
  73. }
  74. private string ControlMarkup(Control c) {
  75. StringWriter sw = new StringWriter ();
  76. HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
  77. c.RenderControl (tw);
  78. return sw.ToString ();
  79. }
  80. private void ShowControlsRecursive (Control c, int depth) {
  81. StringWriter sw = new StringWriter ();
  82. HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
  83. c.RenderControl (tw);
  84. Console.WriteLine (sw.ToString ());
  85. Console.WriteLine (c);
  86. foreach (Control child in c.Controls)
  87. ShowControlsRecursive (child, depth + 5);
  88. }
  89. [Test]
  90. [Category ("NotDotNet")]
  91. [Category ("NotWorking")]
  92. public void InitializeCell () {
  93. DataGridTest p = new DataGridTest ();
  94. DataTable table = new DataTable ();
  95. EditCommandColumn e;
  96. string markup;
  97. e = new EditCommandColumn();
  98. e.ButtonType = ButtonColumnType.LinkButton;
  99. e.CancelText = "Cancel";
  100. e.EditText = "Edit";
  101. e.UpdateText = "Update";
  102. table.Columns.Add (new DataColumn ("one", typeof (string)));
  103. table.Columns.Add (new DataColumn ("two", typeof (string)));
  104. table.Columns.Add (new DataColumn ("three", typeof (string)));
  105. table.Rows.Add (new object [] { "1", "2", "3" });
  106. p.DataSource = new DataView (table);
  107. p.Columns.Add(e);
  108. e = new EditCommandColumn();
  109. e.ButtonType = ButtonColumnType.PushButton;
  110. e.CancelText = "Abbrechen";
  111. e.EditText = "Bearbeiten";
  112. e.UpdateText = "Refresh";
  113. p.Columns.Add(e);
  114. // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
  115. p.CreateControls (true);
  116. p.ID = "sucker";
  117. Assert.AreEqual (2, p.Columns.Count, "I1");
  118. markup = ControlMarkup(p.Controls[0]);
  119. markup = markup.Replace("\t", "");
  120. markup = markup.Replace ("\r", "");
  121. markup = markup.Replace ("\n", "");
  122. #if NET_2_0
  123. Assert.AreEqual (
  124. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  125. "</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>" +
  126. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  127. "</tr></table>", markup, "I2");
  128. #else
  129. Assert.AreEqual (
  130. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  131. "</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>" +
  132. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  133. "</tr></table>", markup, "I2");
  134. #endif
  135. //ShowControlsRecursive (p.Controls [0], 1);
  136. }
  137. [Test]
  138. [Category ("NotDotNet")]
  139. [Category("NotWorking")]
  140. public void ThisIsADGTest () {
  141. DataGridTest p = new DataGridTest ();
  142. DataTable table = new DataTable ();
  143. EditCommandColumn e;
  144. string markup;
  145. e = new EditCommandColumn();
  146. e.ButtonType = ButtonColumnType.LinkButton;
  147. e.CancelText = "Cancel";
  148. e.EditText = "Edit";
  149. e.UpdateText = "Update";
  150. table.Columns.Add (new DataColumn ("one", typeof (string)));
  151. table.Columns.Add (new DataColumn ("two", typeof (string)));
  152. table.Columns.Add (new DataColumn ("three", typeof (string)));
  153. table.Rows.Add (new object [] { "1", "2", "3" });
  154. p.DataSource = new DataView (table);
  155. p.Columns.Add(e);
  156. e = new EditCommandColumn();
  157. e.ButtonType = ButtonColumnType.PushButton;
  158. e.CancelText = "Abbrechen";
  159. e.EditText = "Bearbeiten";
  160. e.UpdateText = "Refresh";
  161. p.Columns.Add(e);
  162. p.CreateControls (true);
  163. // This is the test we want to run: setting the ID of the table created by
  164. // the datagrid overrides the using the ID of the datagrid itself and uses
  165. // the table ClientID instead.
  166. p.ID = "sucker";
  167. p.Controls [0].ID = "tbl";
  168. Assert.AreEqual (2, p.Columns.Count, "I1");
  169. markup = ControlMarkup(p.Controls[0]);
  170. markup = markup.Replace("\t", "");
  171. markup = markup.Replace ("\r", "");
  172. markup = markup.Replace ("\n", "");
  173. #if NET_2_0
  174. Assert.AreEqual (
  175. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  176. "</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>" +
  177. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  178. "</tr></table>", markup, "I2");
  179. #else
  180. Assert.AreEqual (
  181. "<table border=\"0\" id=\"sucker_tbl\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  182. "</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>" +
  183. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  184. "</tr></table>", markup, "I2");
  185. #endif
  186. }
  187. static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
  188. {
  189. foreach (Control c in coll) {
  190. sb.AppendFormat ("{0}{1}\n", new string (' ', 2 * level), c.GetType ());
  191. GetHierarchy (c.Controls, level + 1, sb);
  192. }
  193. }
  194. [Test]
  195. [Category ("NotDotNet")]
  196. [Category("NotWorking")]
  197. public void InitializeEditCell () {
  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. #if NET_2_0
  243. Assert.AreEqual (
  244. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  245. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" />&nbsp;" +
  246. "<input name=\"sucker$ctl02$ctl01\" type=\"submit\" value=\"Abbrechen\" /></td>" +
  247. "<td><input name=\"sucker$ctl02$ctl02\" type=\"text\" value=\"1\" /></td>" +
  248. "<td><input name=\"sucker$ctl02$ctl03\" type=\"text\" value=\"2\" /></td>" +
  249. "<td><input name=\"sucker$ctl02$ctl04\" type=\"text\" value=\"3\" /></td>" +
  250. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  251. "</tr></table>", markup, "I2");
  252. #else
  253. Assert.AreEqual (
  254. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  255. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Refresh\" />&nbsp;" +
  256. "<input name=\"sucker:_ctl1:_ctl1\" type=\"submit\" value=\"Abbrechen\" /></td>" +
  257. "<td><input name=\"sucker:_ctl1:_ctl2\" type=\"text\" value=\"1\" /></td>" +
  258. "<td><input name=\"sucker:_ctl1:_ctl3\" type=\"text\" value=\"2\" /></td>" +
  259. "<td><input name=\"sucker:_ctl1:_ctl4\" type=\"text\" value=\"3\" /></td>" +
  260. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  261. "</tr></table>", markup, "I2");
  262. #endif
  263. }
  264. [Test]
  265. [Ignore("Unfinished")]
  266. public void InitializeReadOnlyEditCell ()
  267. {
  268. DataGridTest p = new DataGridTest ();
  269. DataTable table = new DataTable ();
  270. EditCommandColumn e;
  271. string markup;
  272. e = new EditCommandColumn ();
  273. e.ButtonType = ButtonColumnType.LinkButton;
  274. e.CancelText = "Cancel";
  275. e.EditText = "Edit";
  276. e.UpdateText = "Update";
  277. table.Columns.Add (new DataColumn ("one", typeof (string)));
  278. table.Columns.Add (new DataColumn ("two", typeof (string)));
  279. table.Columns.Add (new DataColumn ("three", typeof (string)));
  280. table.Rows.Add (new object[] { "1", "2", "3" });
  281. p.DataSource = new DataView (table);
  282. p.Columns.Add (e);
  283. e = new EditCommandColumn ();
  284. e.ButtonType = ButtonColumnType.PushButton;
  285. e.CancelText = "Abbrechen";
  286. e.EditText = "Bearbeiten";
  287. e.UpdateText = "Refresh";
  288. p.Columns.Add (e);
  289. // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
  290. p.EditItemIndex = 0;
  291. // This will trigger EditCommandColumn.InitializeCell
  292. p.CreateControls (true);
  293. p.ID = "sucker";
  294. markup = ControlMarkup (p.Controls[0]);
  295. markup = markup.Replace ("\t", "");
  296. markup = markup.Replace ("\r", "");
  297. markup = markup.Replace ("\n", "");
  298. Assert.AreEqual (2, p.Columns.Count, "I1");
  299. Assert.AreEqual (
  300. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  301. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name type=\"submit\" value=\"Refresh\" />&nbsp;" +
  302. "<input name value=\"Abbrechen\" type=\"submit\" /></td>" +
  303. "<td><input name=\"_ctl2:_ctl0\" type=\"text\" value=\"1\" /></td>" +
  304. "<td><input name=\"_ctl2:_ctl1\" type=\"text\" value=\"2\" /></td>" +
  305. "<td><input name=\"_ctl2:_ctl2\" type=\"text\" value=\"3\" /></td>" +
  306. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  307. "</tr></table>", markup, "I2");
  308. }
  309. }
  310. }