New test.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / EditCommandColumnTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.EditCommandColumn.cs 
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Collections;
34 using System.Data;
35 using System.IO;
36 using System.Globalization;
37 using System.Text;
38 using System.Web;
39 using System.Web.UI;
40 using System.Web.UI.WebControls;
41
42 namespace MonoTests.System.Web.UI.WebControls
43 {
44
45         
46         
47         [TestFixture]   
48         public class EditCommandColumnTest {
49
50                 private class DataGridTest : DataGrid {
51                         public ArrayList CreateColumns (PagedDataSource data_source, bool use_data_source) {
52                                 return CreateColumnSet (data_source, use_data_source);
53                         }
54
55                         public void CreateControls (bool use_data_source) {
56                                 CreateControlHierarchy (use_data_source);
57                         }
58                 }
59                 
60                 [Test]
61                 [Category("NotWorking")]
62                 public void Defaults ()
63                 {
64                         EditCommandColumn       e;
65
66                         e = new EditCommandColumn();
67
68                         Assert.AreEqual(ButtonColumnType.LinkButton, e.ButtonType, "D1");
69                         Assert.AreEqual(string.Empty, e.CancelText, "D2");
70                         Assert.AreEqual(string.Empty, e.EditText, "D3");
71                         Assert.AreEqual(string.Empty, e.UpdateText, "D4");
72 #if NET_2_0
73                         // MONO BUG: CausesValidation and ValidationGroup have no definition
74                         //Assert.AreEqual (true, e.CausesValidation, "CausesValidation");
75                         //Assert.AreEqual (string.Empty, e.ValidationGroup, "ValidationGroup");
76                         Assert.Fail("CausesValidation and ValidationGroup have no definition");
77 #endif
78                 }
79
80                 [Test]
81                 [Category ("NotWorking")]
82                 public void Properties () {
83                         EditCommandColumn       e;
84
85                         e = new EditCommandColumn();
86
87                         e.ButtonType = ButtonColumnType.PushButton;
88                         Assert.AreEqual(ButtonColumnType.PushButton, e.ButtonType, "P1");
89
90                         e.CancelText = "Cancel this!";
91                         Assert.AreEqual("Cancel this!", e.CancelText, "D2");
92
93                         e.EditText = "Edit me good";
94                         Assert.AreEqual("Edit me good", e.EditText, "D3");
95
96                         e.UpdateText = "Update? What update?";
97                         Assert.AreEqual("Update? What update?", e.UpdateText, "D4");
98 #if NET_2_0
99                         // MONO BUG: CausesValidation and ValidationGroup have no definition
100                         //e.CausesValidation = false;
101                         //Assert.AreEqual (false, e.CausesValidation, "CausesValidation");
102                         //e.ValidationGroup = "test";
103                         //Assert.AreEqual ("test", e.ValidationGroup, "ValidationGroup");
104                         Assert.Fail ("CausesValidation and ValidationGroup have no definition");
105 #endif
106
107                 }
108
109                 private string ControlMarkup(Control c) {
110                         StringWriter sw = new StringWriter ();
111                         HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
112
113                         c.RenderControl (tw);
114                         return sw.ToString ();
115                 }
116
117                 private void ShowControlsRecursive (Control c, int depth) {
118                          StringWriter sw = new StringWriter ();
119                          HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
120
121                          c.RenderControl (tw);
122                          Console.WriteLine (sw.ToString ());
123
124                         Console.WriteLine (c);
125
126                         foreach (Control child in c.Controls)
127                                 ShowControlsRecursive (child, depth + 5);
128                 }
129
130                 [Test]
131                 [Category ("NotDotNet")]
132                 [Category ("NotWorking")]
133                 public void InitializeCell () {
134                         DataGridTest    p = new DataGridTest ();
135                         DataTable       table = new DataTable ();
136                         EditCommandColumn       e;
137                         string                  markup;
138
139                         e = new EditCommandColumn();
140                         e.ButtonType = ButtonColumnType.LinkButton;
141                         e.CancelText = "Cancel";
142                         e.EditText = "Edit";
143                         e.UpdateText = "Update";                        
144
145                         table.Columns.Add (new DataColumn ("one", typeof (string)));
146                         table.Columns.Add (new DataColumn ("two", typeof (string)));
147                         table.Columns.Add (new DataColumn ("three", typeof (string)));
148                         table.Rows.Add (new object [] { "1", "2", "3" });
149
150                         p.DataSource = new DataView (table);
151                         p.Columns.Add(e);
152
153                         e = new EditCommandColumn();
154                         e.ButtonType = ButtonColumnType.PushButton;
155                         e.CancelText = "Abbrechen";
156                         e.EditText = "Bearbeiten";
157                         e.UpdateText = "Refresh";                       
158                         p.Columns.Add(e);
159
160                         // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
161                         p.CreateControls (true);
162                         p.ID = "sucker";
163
164                         Assert.AreEqual (2, p.Columns.Count, "I1");
165                         markup = ControlMarkup(p.Controls[0]);
166                         markup = markup.Replace("\t", "");
167                         markup = markup.Replace ("\r", "");
168                         markup = markup.Replace ("\n", "");
169
170 #if NET_2_0
171                         Assert.AreEqual (
172                                 "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
173                                 "</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>" +
174                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
175                                 "</tr></table>", markup, "I2");
176 #else
177                         Assert.AreEqual (
178                                 "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
179                                 "</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>" + 
180                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
181                                 "</tr></table>", markup, "I2");
182 #endif
183                         //ShowControlsRecursive (p.Controls [0], 1);
184                 }
185
186                 [Test]
187                 [Category ("NotDotNet")]
188                 [Category("NotWorking")]
189                 public void ThisIsADGTest () {
190                         DataGridTest    p = new DataGridTest ();
191                         DataTable       table = new DataTable ();
192                         EditCommandColumn       e;
193                         string                  markup;
194
195                         e = new EditCommandColumn();
196                         e.ButtonType = ButtonColumnType.LinkButton;
197                         e.CancelText = "Cancel";
198                         e.EditText = "Edit";
199                         e.UpdateText = "Update";                        
200
201                         table.Columns.Add (new DataColumn ("one", typeof (string)));
202                         table.Columns.Add (new DataColumn ("two", typeof (string)));
203                         table.Columns.Add (new DataColumn ("three", typeof (string)));
204                         table.Rows.Add (new object [] { "1", "2", "3" });
205
206                         p.DataSource = new DataView (table);
207                         p.Columns.Add(e);
208
209                         e = new EditCommandColumn();
210                         e.ButtonType = ButtonColumnType.PushButton;
211                         e.CancelText = "Abbrechen";
212                         e.EditText = "Bearbeiten";
213                         e.UpdateText = "Refresh";                       
214                         p.Columns.Add(e);
215
216                         p.CreateControls (true);
217                         // This is the test we want to run: setting the ID of the table created by
218                         // the datagrid overrides the using the ID of the datagrid itself and uses
219                         // the table ClientID instead.
220                         p.ID = "sucker";
221                         p.Controls [0].ID = "tbl";
222
223                         Assert.AreEqual (2, p.Columns.Count, "I1");
224                         markup = ControlMarkup(p.Controls[0]);
225                         markup = markup.Replace("\t", "");
226                         markup = markup.Replace ("\r", "");
227                         markup = markup.Replace ("\n", "");
228
229 #if NET_2_0
230                         Assert.AreEqual (
231                                 "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
232                                 "</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>" +
233                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
234                                 "</tr></table>", markup, "I2");
235 #else
236                         Assert.AreEqual (
237                                 "<table border=\"0\" id=\"sucker_tbl\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
238                                 "</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>" + 
239                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
240                                 "</tr></table>", markup, "I2");
241 #endif
242                 }
243
244                 static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
245                 {
246                         foreach (Control c in coll) {
247                                 sb.AppendFormat ("{0}{1}\n", new string (' ', 2 * level), c.GetType ());
248                                 GetHierarchy (c.Controls, level + 1, sb);
249                         }
250                 }
251
252                 [Test]
253                 [Category ("NotDotNet")]
254                 [Category("NotWorking")]
255                 public void InitializeEditCell () {
256                         DataGridTest    p = new DataGridTest ();
257                         DataTable       table = new DataTable ();
258                         EditCommandColumn       e;
259                         string                  markup;
260
261                         e = new EditCommandColumn();
262                         e.ButtonType = ButtonColumnType.LinkButton;
263                         e.CancelText = "Cancel";
264                         e.EditText = "Edit";
265                         e.UpdateText = "Update";                        
266
267                         table.Columns.Add (new DataColumn ("one", typeof (string)));
268                         table.Columns.Add (new DataColumn ("two", typeof (string)));
269                         table.Columns.Add (new DataColumn ("three", typeof (string)));
270                         table.Rows.Add (new object [] { "1", "2", "3" });
271
272                         p.DataSource = new DataView (table);
273                         p.Columns.Add(e);
274
275                         e = new EditCommandColumn();
276                         e.ButtonType = ButtonColumnType.PushButton;
277                         e.CancelText = "Abbrechen";
278                         e.EditText = "Bearbeiten";
279                         e.UpdateText = "Refresh";                       
280                         p.Columns.Add(e);
281
282                         // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
283                         p.EditItemIndex = 0;
284
285                         // This will trigger EditCommandColumn.InitializeCell
286                         p.CreateControls (true);
287                         p.ID = "sucker";
288
289                         StringBuilder sb = new StringBuilder ();
290                         GetHierarchy (p.Controls, 0, sb);
291                         string h = sb.ToString ();
292                         int x = h.IndexOf (".TextBox");
293                         // These are from the BoundColumns
294                         Assert.IsTrue (x != -1, "textbox1");
295                         x = h.IndexOf (".TextBox", x + 1);
296                         Assert.IsTrue (x != -1, "textbox2");
297                         x = h.IndexOf (".TextBox", x + 1);
298                         Assert.IsTrue (x != -1, "textbox3");
299                         x = h.IndexOf (".TextBox", x + 1);
300                         Assert.IsTrue (x == -1, "textbox-end");
301
302                         markup = ControlMarkup (p.Controls[0]);
303                         markup = markup.Replace ("\t", "");
304                         markup = markup.Replace ("\r", "");
305                         markup = markup.Replace ("\n", "");
306
307 //Console.WriteLine("Markup:>{0}<", markup);
308                         Assert.AreEqual (2, p.Columns.Count, "I1");
309
310 #if NET_2_0
311                         Assert.AreEqual (
312                                 "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" + 
313                                 "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" />&nbsp;" + 
314                                 "<input name=\"sucker$ctl02$ctl01\" type=\"submit\" value=\"Abbrechen\" /></td>" + 
315                                 "<td><input name=\"sucker$ctl02$ctl02\" type=\"text\" value=\"1\" /></td>" + 
316                                 "<td><input name=\"sucker$ctl02$ctl03\" type=\"text\" value=\"2\" /></td>" + 
317                                 "<td><input name=\"sucker$ctl02$ctl04\" type=\"text\" value=\"3\" /></td>" + 
318                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
319                                 "</tr></table>", markup, "I2");
320 #else
321                         Assert.AreEqual (
322                                 "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
323                                 "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Refresh\" />&nbsp;" +
324                                 "<input name=\"sucker:_ctl1:_ctl1\" type=\"submit\" value=\"Abbrechen\" /></td>" +
325                                 "<td><input name=\"sucker:_ctl1:_ctl2\" type=\"text\" value=\"1\" /></td>" +
326                                 "<td><input name=\"sucker:_ctl1:_ctl3\" type=\"text\" value=\"2\" /></td>" +
327                                 "<td><input name=\"sucker:_ctl1:_ctl4\" type=\"text\" value=\"3\" /></td>" + 
328                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
329                                 "</tr></table>", markup, "I2");
330 #endif
331                 }
332
333                 [Test]
334                 [Ignore("Unfinished")]
335                 public void InitializeReadOnlyEditCell ()
336                 {
337                         DataGridTest p = new DataGridTest ();
338                         DataTable table = new DataTable ();
339                         EditCommandColumn e;
340                         string markup;
341
342                         e = new EditCommandColumn ();
343                         e.ButtonType = ButtonColumnType.LinkButton;
344                         e.CancelText = "Cancel";
345                         e.EditText = "Edit";
346                         e.UpdateText = "Update";
347
348                         table.Columns.Add (new DataColumn ("one", typeof (string)));
349                         table.Columns.Add (new DataColumn ("two", typeof (string)));
350                         table.Columns.Add (new DataColumn ("three", typeof (string)));
351                         table.Rows.Add (new object[] { "1", "2", "3" });
352
353                         p.DataSource = new DataView (table);
354                         p.Columns.Add (e);
355
356                         e = new EditCommandColumn ();
357                         e.ButtonType = ButtonColumnType.PushButton;
358                         
359                         e.CancelText = "Abbrechen";
360                         e.EditText = "Bearbeiten";
361                         e.UpdateText = "Refresh";
362                         p.Columns.Add (e);
363
364                         // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
365                         p.EditItemIndex = 0;
366
367                         // This will trigger EditCommandColumn.InitializeCell
368                         p.CreateControls (true);
369                         p.ID = "sucker";
370
371                         markup = ControlMarkup (p.Controls[0]);
372                         markup = markup.Replace ("\t", "");
373                         markup = markup.Replace ("\r", "");
374                         markup = markup.Replace ("\n", "");
375
376                         Assert.AreEqual (2, p.Columns.Count, "I1");
377                         Assert.AreEqual (
378                                 "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
379                                 "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name type=\"submit\" value=\"Refresh\" />&nbsp;" +
380                                 "<input name value=\"Abbrechen\" type=\"submit\" /></td>" +
381                                 "<td><input name=\"_ctl2:_ctl0\" type=\"text\" value=\"1\" /></td>" +
382                                 "<td><input name=\"_ctl2:_ctl1\" type=\"text\" value=\"2\" /></td>" +
383                                 "<td><input name=\"_ctl2:_ctl2\" type=\"text\" value=\"3\" /></td>" +
384                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
385                                 "</tr></table>", markup, "I2");
386                 }
387         }
388 }