Make a copy of the old ZipLib
[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                 public void Defaults ()
62                 {
63                         EditCommandColumn       e;
64
65                         e = new EditCommandColumn();
66
67                         Assert.AreEqual(ButtonColumnType.LinkButton, e.ButtonType, "D1");
68                         Assert.AreEqual(string.Empty, e.CancelText, "D2");
69                         Assert.AreEqual(string.Empty, e.EditText, "D3");
70                         Assert.AreEqual(string.Empty, e.UpdateText, "D4");
71                 }
72
73                 [Test]
74                 public void Properties () {
75                         EditCommandColumn       e;
76
77                         e = new EditCommandColumn();
78
79                         e.ButtonType = ButtonColumnType.PushButton;
80                         Assert.AreEqual(ButtonColumnType.PushButton, e.ButtonType, "P1");
81
82                         e.CancelText = "Cancel this!";
83                         Assert.AreEqual("Cancel this!", e.CancelText, "D2");
84
85                         e.EditText = "Edit me good";
86                         Assert.AreEqual("Edit me good", e.EditText, "D3");
87
88                         e.UpdateText = "Update? What update?";
89                         Assert.AreEqual("Update? What update?", e.UpdateText, "D4");
90                 }
91
92                 private string ControlMarkup(Control c) {
93                         StringWriter sw = new StringWriter ();
94                         HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
95
96                         c.RenderControl (tw);
97                         return sw.ToString ();
98                 }
99
100                 private void ShowControlsRecursive (Control c, int depth) {
101                          StringWriter sw = new StringWriter ();
102                          HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
103
104                          c.RenderControl (tw);
105                          Console.WriteLine (sw.ToString ());
106
107                         Console.WriteLine (c);
108
109                         foreach (Control child in c.Controls)
110                                 ShowControlsRecursive (child, depth + 5);
111                 }
112
113                 [Test]
114                 [Category ("NotDotNet")]
115                 public void InitializeCell () {
116                         DataGridTest    p = new DataGridTest ();
117                         DataTable       table = new DataTable ();
118                         EditCommandColumn       e;
119                         string                  markup;
120
121                         e = new EditCommandColumn();
122                         e.ButtonType = ButtonColumnType.LinkButton;
123                         e.CancelText = "Cancel";
124                         e.EditText = "Edit";
125                         e.UpdateText = "Update";                        
126
127                         table.Columns.Add (new DataColumn ("one", typeof (string)));
128                         table.Columns.Add (new DataColumn ("two", typeof (string)));
129                         table.Columns.Add (new DataColumn ("three", typeof (string)));
130                         table.Rows.Add (new object [] { "1", "2", "3" });
131
132                         p.DataSource = new DataView (table);
133                         p.Columns.Add(e);
134
135                         e = new EditCommandColumn();
136                         e.ButtonType = ButtonColumnType.PushButton;
137                         e.CancelText = "Abbrechen";
138                         e.EditText = "Bearbeiten";
139                         e.UpdateText = "Refresh";                       
140                         p.Columns.Add(e);
141
142                         // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
143                         p.CreateControls (true);
144                         p.ID = "sucker";
145
146                         Assert.AreEqual (2, p.Columns.Count, "I1");
147                         markup = ControlMarkup(p.Controls[0]);
148                         markup = markup.Replace("\t", "");
149                         markup = markup.Replace ("\r", "");
150                         markup = markup.Replace ("\n", "");
151
152 #if NET_2_0
153                         Assert.AreEqual (
154                                 "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
155                                 "</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>" +
156                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
157                                 "</tr></table>", markup, "I2");
158 #else
159                         Assert.AreEqual (
160                                 "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
161                                 "</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>" + 
162                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
163                                 "</tr></table>", markup, "I2");
164 #endif
165                         //ShowControlsRecursive (p.Controls [0], 1);
166                 }
167
168                 [Test]
169                 [Category ("NotDotNet")]
170                 public void ThisIsADGTest () {
171                         DataGridTest    p = new DataGridTest ();
172                         DataTable       table = new DataTable ();
173                         EditCommandColumn       e;
174                         string                  markup;
175
176                         e = new EditCommandColumn();
177                         e.ButtonType = ButtonColumnType.LinkButton;
178                         e.CancelText = "Cancel";
179                         e.EditText = "Edit";
180                         e.UpdateText = "Update";                        
181
182                         table.Columns.Add (new DataColumn ("one", typeof (string)));
183                         table.Columns.Add (new DataColumn ("two", typeof (string)));
184                         table.Columns.Add (new DataColumn ("three", typeof (string)));
185                         table.Rows.Add (new object [] { "1", "2", "3" });
186
187                         p.DataSource = new DataView (table);
188                         p.Columns.Add(e);
189
190                         e = new EditCommandColumn();
191                         e.ButtonType = ButtonColumnType.PushButton;
192                         e.CancelText = "Abbrechen";
193                         e.EditText = "Bearbeiten";
194                         e.UpdateText = "Refresh";                       
195                         p.Columns.Add(e);
196
197                         p.CreateControls (true);
198                         // This is the test we want to run: setting the ID of the table created by
199                         // the datagrid overrides the using the ID of the datagrid itself and uses
200                         // the table ClientID instead.
201                         p.ID = "sucker";
202                         p.Controls [0].ID = "tbl";
203
204                         Assert.AreEqual (2, p.Columns.Count, "I1");
205                         markup = ControlMarkup(p.Controls[0]);
206                         markup = markup.Replace("\t", "");
207                         markup = markup.Replace ("\r", "");
208                         markup = markup.Replace ("\n", "");
209
210 #if NET_2_0
211                         Assert.AreEqual (
212                                 "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
213                                 "</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>" +
214                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
215                                 "</tr></table>", markup, "I2");
216 #else
217                         Assert.AreEqual (
218                                 "<table border=\"0\" id=\"sucker_tbl\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
219                                 "</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>" + 
220                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
221                                 "</tr></table>", markup, "I2");
222 #endif
223                 }
224
225                 static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
226                 {
227                         foreach (Control c in coll) {
228                                 sb.AppendFormat ("{0}{1}\n", new string (' ', 2 * level), c.GetType ());
229                                 GetHierarchy (c.Controls, level + 1, sb);
230                         }
231                 }
232
233                 [Test]
234                 [Category ("NotDotNet")]
235                 public void InitializeEditCell () {
236                         DataGridTest    p = new DataGridTest ();
237                         DataTable       table = new DataTable ();
238                         EditCommandColumn       e;
239                         string                  markup;
240
241                         e = new EditCommandColumn();
242                         e.ButtonType = ButtonColumnType.LinkButton;
243                         e.CancelText = "Cancel";
244                         e.EditText = "Edit";
245                         e.UpdateText = "Update";                        
246
247                         table.Columns.Add (new DataColumn ("one", typeof (string)));
248                         table.Columns.Add (new DataColumn ("two", typeof (string)));
249                         table.Columns.Add (new DataColumn ("three", typeof (string)));
250                         table.Rows.Add (new object [] { "1", "2", "3" });
251
252                         p.DataSource = new DataView (table);
253                         p.Columns.Add(e);
254
255                         e = new EditCommandColumn();
256                         e.ButtonType = ButtonColumnType.PushButton;
257                         e.CancelText = "Abbrechen";
258                         e.EditText = "Bearbeiten";
259                         e.UpdateText = "Refresh";                       
260                         p.Columns.Add(e);
261
262                         // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
263                         p.EditItemIndex = 0;
264
265                         // This will trigger EditCommandColumn.InitializeCell
266                         p.CreateControls (true);
267                         p.ID = "sucker";
268
269                         StringBuilder sb = new StringBuilder ();
270                         GetHierarchy (p.Controls, 0, sb);
271                         string h = sb.ToString ();
272                         int x = h.IndexOf (".TextBox");
273                         // These are from the BoundColumns
274                         Assert.IsTrue (x != -1, "textbox1");
275                         x = h.IndexOf (".TextBox", x + 1);
276                         Assert.IsTrue (x != -1, "textbox2");
277                         x = h.IndexOf (".TextBox", x + 1);
278                         Assert.IsTrue (x != -1, "textbox3");
279                         x = h.IndexOf (".TextBox", x + 1);
280                         Assert.IsTrue (x == -1, "textbox-end");
281
282                         markup = ControlMarkup (p.Controls[0]);
283                         markup = markup.Replace ("\t", "");
284                         markup = markup.Replace ("\r", "");
285                         markup = markup.Replace ("\n", "");
286
287 //Console.WriteLine("Markup:>{0}<", markup);
288                         Assert.AreEqual (2, p.Columns.Count, "I1");
289
290 #if NET_2_0
291                         Assert.AreEqual (
292                                 "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" + 
293                                 "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" />&nbsp;" + 
294                                 "<input name=\"sucker$ctl02$ctl01\" type=\"submit\" value=\"Abbrechen\" /></td>" + 
295                                 "<td><input name=\"sucker$ctl02$ctl02\" type=\"text\" value=\"1\" /></td>" + 
296                                 "<td><input name=\"sucker$ctl02$ctl03\" type=\"text\" value=\"2\" /></td>" + 
297                                 "<td><input name=\"sucker$ctl02$ctl04\" type=\"text\" value=\"3\" /></td>" + 
298                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
299                                 "</tr></table>", markup, "I2");
300 #else
301                         Assert.AreEqual (
302                                 "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
303                                 "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Refresh\" />&nbsp;" +
304                                 "<input name=\"sucker:_ctl1:_ctl1\" type=\"submit\" value=\"Abbrechen\" /></td>" +
305                                 "<td><input name=\"sucker:_ctl1:_ctl2\" type=\"text\" value=\"1\" /></td>" +
306                                 "<td><input name=\"sucker:_ctl1:_ctl3\" type=\"text\" value=\"2\" /></td>" +
307                                 "<td><input name=\"sucker:_ctl1:_ctl4\" type=\"text\" value=\"3\" /></td>" + 
308                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
309                                 "</tr></table>", markup, "I2");
310 #endif
311                 }
312
313                 [Test]
314                 [Ignore("Unfinished")]
315                 public void InitializeReadOnlyEditCell ()
316                 {
317                         DataGridTest p = new DataGridTest ();
318                         DataTable table = new DataTable ();
319                         EditCommandColumn e;
320                         string markup;
321
322                         e = new EditCommandColumn ();
323                         e.ButtonType = ButtonColumnType.LinkButton;
324                         e.CancelText = "Cancel";
325                         e.EditText = "Edit";
326                         e.UpdateText = "Update";
327
328                         table.Columns.Add (new DataColumn ("one", typeof (string)));
329                         table.Columns.Add (new DataColumn ("two", typeof (string)));
330                         table.Columns.Add (new DataColumn ("three", typeof (string)));
331                         table.Rows.Add (new object[] { "1", "2", "3" });
332
333                         p.DataSource = new DataView (table);
334                         p.Columns.Add (e);
335
336                         e = new EditCommandColumn ();
337                         e.ButtonType = ButtonColumnType.PushButton;
338                         
339                         e.CancelText = "Abbrechen";
340                         e.EditText = "Bearbeiten";
341                         e.UpdateText = "Refresh";
342                         p.Columns.Add (e);
343
344                         // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
345                         p.EditItemIndex = 0;
346
347                         // This will trigger EditCommandColumn.InitializeCell
348                         p.CreateControls (true);
349                         p.ID = "sucker";
350
351                         markup = ControlMarkup (p.Controls[0]);
352                         markup = markup.Replace ("\t", "");
353                         markup = markup.Replace ("\r", "");
354                         markup = markup.Replace ("\n", "");
355
356                         Assert.AreEqual (2, p.Columns.Count, "I1");
357                         Assert.AreEqual (
358                                 "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
359                                 "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name type=\"submit\" value=\"Refresh\" />&nbsp;" +
360                                 "<input name value=\"Abbrechen\" type=\"submit\" /></td>" +
361                                 "<td><input name=\"_ctl2:_ctl0\" type=\"text\" value=\"1\" /></td>" +
362                                 "<td><input name=\"_ctl2:_ctl1\" type=\"text\" value=\"2\" /></td>" +
363                                 "<td><input name=\"_ctl2:_ctl2\" type=\"text\" value=\"3\" /></td>" +
364                                 "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
365                                 "</tr></table>", markup, "I2");
366                 }
367         }
368 }