[System] UriKind.RelativeOrAbsolute workaround.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / TableTest.cs
1 //
2 // TableTest.cs
3 //      - Unit tests for System.Web.UI.WebControls.Table
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
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
30 using System;
31 using System.IO;
32 using System.Web;
33 using System.Web.UI;
34 using System.Web.UI.WebControls;
35 using MonoTests.SystemWeb.Framework;
36
37 using NUnit.Framework;
38 using MonoTests.stand_alone.WebHarness;
39
40 namespace MonoTests.System.Web.UI.WebControls {
41
42         public class TestTable : Table {
43
44                 public string Tag {
45                         get { return base.TagName; }
46                 }
47
48                 public StateBag StateBag {
49                         get { return base.ViewState; }
50                 }
51
52                 public string Render ()
53                 {
54                         StringWriter sw = new StringWriter ();
55                         sw.NewLine = "\n";
56                         HtmlTextWriter writer = new HtmlTextWriter (sw);
57                         base.Render (writer);
58                         return writer.InnerWriter.ToString ();
59                 }
60
61                 public Style GetStyle ()
62                 {
63                         return base.CreateControlStyle ();
64                 }
65                 protected override void RaisePostBackEvent (string argument)
66                 {
67                         WebTest.CurrentTest.UserData = "RaisePostBackEvent";
68                         base.RaisePostBackEvent (argument);
69                 }
70
71         }
72
73         [TestFixture]
74         public class TableTest {
75
76                 private const string imageUrl = "http://www.mono-project.com/stylesheets/images.wiki.png";
77                 private const string localImageUrl = "foo.jpg";
78
79                 private HtmlTextWriter GetWriter ()
80                 {
81                         StringWriter sw = new StringWriter ();
82                         sw.NewLine = "\n";
83                         return new HtmlTextWriter (sw);
84                 }
85
86                 [Test]
87                 public void DefaultProperties ()
88                 {
89                         TestTable t = new TestTable ();
90                         Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
91                         Assert.AreEqual (0, t.StateBag.Count, "ViewState.Count");
92
93                         Assert.AreEqual (String.Empty, t.BackImageUrl, "BackImageUrl");
94                         Assert.AreEqual (String.Empty, t.Caption, "Caption");
95                         Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign");
96                         Assert.AreEqual (-1, t.CellPadding, "CellPadding");
97                         Assert.AreEqual (-1, t.CellSpacing, "CellSpacing");
98                         Assert.AreEqual (GridLines.None, t.GridLines, "GridLines");
99                         Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "HorizontalAlign");
100                         Assert.AreEqual (0, t.Rows.Count, "Rows.Count");
101
102                         Assert.AreEqual ("table", t.Tag, "TagName");
103                         Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count-2");
104                         Assert.AreEqual (0, t.StateBag.Count, "ViewState.Count-2");
105                         Assert.AreEqual (String.Empty, t.Caption, "Caption");
106                         Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign");
107
108                 }
109                 [Test]
110                 public void Caption ()
111                 {
112                         TestTable t = new TestTable ();
113                         t.Caption = "CaptionText";
114                         string html = t.Render ();
115 #if NET_4_0
116                         string orig = "<table>\n\t<caption>\n\t\tCaptionText\n\t</caption>\n</table>";
117 #else
118                         string orig = "<table border=\"0\">\n\t<caption>\n\t\tCaptionText\n\t</caption>\n</table>";
119 #endif
120                         HtmlDiff.AssertAreEqual (orig, html, "Caption");
121                 }
122
123                 [Test]
124                 public void CaptionAlign ()
125                 {
126                         TestTable t = new TestTable ();
127                         t.Caption = "CaptionText";
128                         t.CaptionAlign = TableCaptionAlign.Left; 
129                         string html = t.Render ();
130 #if NET_4_0
131                         string orig = "<table>\n\t<caption align=\"Left\">\n\t\tCaptionText\n\t</caption>\n</table>";
132 #else
133                         string orig = "<table border=\"0\">\n\t<caption align=\"Left\">\n\t\tCaptionText\n\t</caption>\n</table>";
134 #endif
135                         HtmlDiff.AssertAreEqual (orig, html, "CaptionAlign");
136                 }
137                 [Test]
138                 public void NullProperties ()
139                 {
140                         TestTable t = new TestTable ();
141                         t.BackImageUrl = String.Empty; // doesn't accept null, see specific test
142                         Assert.AreEqual (String.Empty, t.BackImageUrl, "BackImageUrl");
143                         t.Caption = null; // doesn't get added to ViewState
144                         Assert.AreEqual (String.Empty, t.Caption, "Caption");
145                         t.CaptionAlign = TableCaptionAlign.NotSet;
146                         Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign");
147                         t.CellPadding = -1;
148                         Assert.AreEqual (-1, t.CellPadding, "CellPadding");
149                         t.CellSpacing = -1;
150                         Assert.AreEqual (-1, t.CellSpacing, "CellSpacing");
151                         t.GridLines = GridLines.None;
152                         Assert.AreEqual (GridLines.None, t.GridLines, "GridLines");
153                         t.HorizontalAlign = HorizontalAlign.NotSet;
154                         Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "HorizontalAlign");
155
156                         Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
157                         Assert.AreEqual (6, t.StateBag.Count, "ViewState.Count-1");
158                 }
159
160                 [Test]
161                 public void CleanProperties ()
162                 {
163                         TestTable t = new TestTable ();
164                         t.BackImageUrl = imageUrl;
165                         Assert.AreEqual (imageUrl, t.BackImageUrl, "BackImageUrl");
166                         t.Caption = "Mono";
167                         Assert.AreEqual ("Mono", t.Caption, "Caption");
168                         t.CaptionAlign = TableCaptionAlign.Top;
169                         Assert.AreEqual (TableCaptionAlign.Top, t.CaptionAlign, "CaptionAlign");
170                         t.CellPadding = 1;
171                         Assert.AreEqual (1, t.CellPadding, "CellPadding");
172                         t.CellSpacing = 2;
173                         Assert.AreEqual (2, t.CellSpacing, "CellSpacing");
174                         t.GridLines = GridLines.Both;
175                         Assert.AreEqual (GridLines.Both, t.GridLines, "GridLines");
176                         t.HorizontalAlign = HorizontalAlign.Justify;
177                         Assert.AreEqual (HorizontalAlign.Justify, t.HorizontalAlign, "HorizontalAlign");
178                         Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
179                         Assert.AreEqual (7, t.StateBag.Count, "ViewState.Count");
180
181                         t.BackImageUrl = String.Empty; // doesn't accept null, see specific test
182                         Assert.AreEqual (String.Empty, t.BackImageUrl, "-BackImageUrl");
183                         t.Caption = null; // removed
184                         Assert.AreEqual (String.Empty, t.Caption, "-Caption");
185                         t.CaptionAlign = TableCaptionAlign.NotSet;
186                         Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "-CaptionAlign");
187                         t.CellPadding = -1;
188                         Assert.AreEqual (-1, t.CellPadding, "-CellPadding");
189                         t.CellSpacing = -1;
190                         Assert.AreEqual (-1, t.CellSpacing, "-CellSpacing");
191                         t.GridLines = GridLines.None;
192                         Assert.AreEqual (GridLines.None, t.GridLines, "-GridLines");
193                         t.HorizontalAlign = HorizontalAlign.NotSet;
194                         Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "-HorizontalAlign");
195
196                         Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count-1");
197                         Assert.AreEqual (6, t.StateBag.Count, "ViewState.Count-1");
198                 }
199
200                 [Test]
201                 // LAMESPEC: undocumented (all others property I've seen takes null as the default value)
202                 [ExpectedException (typeof (ArgumentNullException))]
203                 public void BackImageUrl_Null ()
204                 {
205                         Table t = new Table ();
206                         t.BackImageUrl = null;
207                 }
208
209                 [Test]
210                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
211                 public void CaptionAlign_Invalid ()
212                 {
213                         Table t = new Table ();
214                         t.CaptionAlign = (TableCaptionAlign)Int32.MinValue;
215                 }
216
217                 [Test]
218                 // LAMESPEC: undocumented exception but similar to Image
219                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
220                 public void GridLines_Invalid ()
221                 {
222                         Table t = new Table ();
223                         t.GridLines = (GridLines)Int32.MinValue;
224                 }
225
226                 [Test]
227                 // LAMESPEC: undocumented exception but similar to Image
228                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
229                 public void HorizontalAlign_Invalid ()
230                 {
231                         Table t = new Table ();
232                         t.HorizontalAlign = (HorizontalAlign)Int32.MinValue;
233                 }
234
235                 [Test]
236                 public void BorderWidth ()
237                 {
238                         Table t = new Table ();
239                         Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.None");
240                         t.GridLines = GridLines.Horizontal;
241                         Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Horizontal");
242                         t.GridLines = GridLines.Vertical;
243                         Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Vertical");
244                         t.GridLines = GridLines.Both;
245                         Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Both");
246                         // note: but border="1" when rendered
247                 }
248
249                 [Test]
250                 public void Render ()
251                 {
252                         TestTable t = new TestTable ();
253                         string s = t.Render ();
254 #if NET_4_0
255                         Assert.AreEqual ("<table>\n\n</table>", s, "empty/default");
256 #else
257                         Assert.AreEqual ("<table border=\"0\">\n\n</table>", s, "empty/default");
258 #endif
259
260                         t.CellPadding = 1;
261                         s = t.Render ();
262 #if NET_4_0
263                         Assert.AreEqual ("<table cellpadding=\"1\">\n\n</table>", s, "CellPadding");
264 #else
265                         Assert.AreEqual ("<table cellpadding=\"1\" border=\"0\">\n\n</table>", s, "CellPadding");
266 #endif
267                         t.CellPadding = -1;
268
269                         t.CellSpacing = 2;
270                         s = t.Render ();
271 #if NET_4_0
272                         Assert.AreEqual ("<table cellspacing=\"2\">\n\n</table>", s, "CellSpacing");
273 #else
274                         Assert.AreEqual ("<table cellspacing=\"2\" border=\"0\">\n\n</table>", s, "CellSpacing");
275 #endif
276                         t.CellSpacing = -1;
277
278                         t.GridLines = GridLines.Horizontal;
279                         s = t.Render ();
280                         Assert.AreEqual ("<table rules=\"rows\" border=\"1\">\n\n</table>", s, "GridLines.Horizontal");
281                         t.GridLines = GridLines.Vertical;
282                         s = t.Render ();
283                         Assert.AreEqual ("<table rules=\"cols\" border=\"1\">\n\n</table>", s, "GridLines.Vertical");
284                         t.GridLines = GridLines.Both;
285                         s = t.Render ();
286                         Assert.AreEqual ("<table rules=\"all\" border=\"1\">\n\n</table>", s, "GridLines.Both");
287                         t.GridLines = GridLines.None;
288
289                         t.BorderWidth = new Unit (2);
290                         s = t.Render ();
291 #if NET_4_0
292                         Assert.IsTrue ((s.IndexOf ("\"border-width:2px;border-style:solid;\"") > 0), "border=0/2");
293 #else
294                         Assert.IsTrue ((s.IndexOf ("border=\"0\"") > 0), "border=0/2");
295 #endif
296                         t.GridLines = GridLines.Horizontal;
297                         s = t.Render ();
298 #if NET_4_0
299                         Console.WriteLine (s);
300                         Assert.IsTrue ((s.IndexOf ("rules=\"rows\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Horizontal");
301 #else
302                         Assert.IsTrue ((s.IndexOf ("rules=\"rows\" border=\"2\"") > 0), "2/GridLines.Horizontal");
303 #endif
304                         t.GridLines = GridLines.Vertical;
305                         s = t.Render ();
306 #if NET_4_0
307                         Assert.IsTrue ((s.IndexOf ("rules=\"cols\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Vertical");
308 #else
309                         Assert.IsTrue ((s.IndexOf ("rules=\"cols\" border=\"2\"") > 0), "2/GridLines.Vertical");
310 #endif
311                         t.GridLines = GridLines.Both;
312                         s = t.Render ();
313 #if NET_4_0
314                         Assert.IsTrue ((s.IndexOf ("rules=\"all\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Both");
315 #else
316                         Assert.IsTrue ((s.IndexOf ("rules=\"all\" border=\"2\"") > 0), "2/GridLines.Both");
317 #endif
318                         t.GridLines = GridLines.None;
319                         t.BorderWidth = new Unit ();
320
321                         t.HorizontalAlign = HorizontalAlign.Left;
322                         s = t.Render ();
323 #if NET_4_0
324                         Assert.AreEqual ("<table align=\"left\">\n\n</table>", s.ToLower (), "HorizontalAlign.Left");
325 #else
326                         Assert.AreEqual ("<table align=\"left\" border=\"0\">\n\n</table>", s.ToLower (), "HorizontalAlign.Left");
327 #endif
328                         t.HorizontalAlign = HorizontalAlign.Center;
329                         s = t.Render ();
330 #if NET_4_0
331                         Assert.AreEqual ("<table align=\"center\">\n\n</table>", s.ToLower (), "HorizontalAlign.Center");
332 #else
333                         Assert.AreEqual ("<table align=\"center\" border=\"0\">\n\n</table>", s.ToLower (), "HorizontalAlign.Center");
334 #endif
335                         t.HorizontalAlign = HorizontalAlign.Right;
336                         s = t.Render ();
337 #if NET_4_0
338                         Assert.AreEqual ("<table align=\"right\">\n\n</table>", s.ToLower (), "HorizontalAlign.Right");
339 #else
340                         Assert.AreEqual ("<table align=\"right\" border=\"0\">\n\n</table>", s.ToLower (), "HorizontalAlign.Right");
341 #endif
342                         t.HorizontalAlign = HorizontalAlign.Justify;
343                         s = t.Render ();
344 #if NET_4_0
345                         Assert.AreEqual ("<table align=\"justify\">\n\n</table>", s.ToLower (), "HorizontalAlign.Justify");
346 #else
347                         Assert.AreEqual ("<table align=\"justify\" border=\"0\">\n\n</table>", s.ToLower (), "HorizontalAlign.Justify");
348 #endif
349                         t.HorizontalAlign = HorizontalAlign.NotSet;
350
351                         t.Caption = "mono";
352                         s = t.Render ();
353 #if NET_4_0
354                         Assert.AreEqual ("<table>\n\t<caption>\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption");
355 #else
356                         Assert.AreEqual ("<table border=\"0\">\n\t<caption>\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption");
357 #endif
358
359                         t.CaptionAlign = TableCaptionAlign.Top;
360                         s = t.Render ();
361 #if NET_4_0
362                         Assert.AreEqual ("<table>\n\t<caption align=\"top\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Top");
363 #else
364                         Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"top\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Top");
365 #endif
366                         t.CaptionAlign = TableCaptionAlign.Bottom;
367                         s = t.Render ();
368 #if NET_4_0
369                         Assert.AreEqual ("<table>\n\t<caption align=\"bottom\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Bottom");
370 #else
371                         Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"bottom\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Bottom");
372 #endif
373                         t.CaptionAlign = TableCaptionAlign.Right;
374                         s = t.Render ();
375 #if NET_4_0
376                         Assert.AreEqual ("<table>\n\t<caption align=\"right\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Right");
377 #else
378                         Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"right\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Right");
379 #endif
380                         t.CaptionAlign = TableCaptionAlign.Left;
381                         s = t.Render ();
382 #if NET_4_0
383                         Assert.AreEqual ("<table>\n\t<caption align=\"left\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Left");
384 #else
385                         Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"left\">\n\t\tmono\n\t</caption>\n</table>", s.ToLower (), "Caption/Left");
386 #endif
387                         t.Caption = null;
388                         s = t.Render ();
389 #if NET_4_0
390                         Assert.AreEqual ("<table>\n\n</table>", s, "CaptionAlign without Caption");
391 #else
392                         Assert.AreEqual ("<table border=\"0\">\n\n</table>", s, "CaptionAlign without Caption");
393 #endif
394                         t.CaptionAlign = TableCaptionAlign.NotSet;
395
396                         t.BackImageUrl = imageUrl;
397                         s = t.Render ();
398 #if NET_4_0
399                         Assert.AreEqual ("<table style=\"background-image:url(http://www.mono-project.com/stylesheets/images.wiki.png);\">\n\n</table>", s, "BackImageUrl");
400 #else
401                         Assert.AreEqual ("<table border=\"0\" style=\"background-image:url(http://www.mono-project.com/stylesheets/images.wiki.png);\">\n\n</table>", s, "BackImageUrl");
402 #endif
403                         t.BackImageUrl = localImageUrl;
404                         s = t.Render ();
405 #if NET_4_0
406                         Assert.AreEqual ("<table style=\"background-image:url(foo.jpg);\">\n\n</table>", s, "BackImageUrl");
407 #else
408                         Assert.AreEqual ("<table border=\"0\" style=\"background-image:url(foo.jpg);\">\n\n</table>", s, "BackImageUrl");
409 #endif
410                         t.BackImageUrl = String.Empty;
411                 }
412
413                 [Test]
414                 [Category ("NunitWeb")]
415                 public void RenderInAspxPage ()
416                 {
417                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RenderInAspxPage_OnLoad));
418                         string res = t.Run ();
419 #if NET_4_0
420                         Assert.IsTrue (res.IndexOf ("<table id=\"MagicID_A1C3\" style=\"background-image:url(foo.jpg);\"") != -1, res);
421 #else
422                         Assert.IsTrue (res.IndexOf ("<table id=\"MagicID_A1C3\" border=\"0\" style=\"background-image:url(foo.jpg);\"")!= -1, res);
423 #endif
424                 }
425
426                 public static void RenderInAspxPage_OnLoad (Page p)
427                 {
428                         Table t = new Table ();
429                         t.BackImageUrl = "foo.jpg";
430                         t.ID = "MagicID_A1C3";
431                         p.Form.Controls.Add (t);
432                         p.Controls.Add (t);
433                 }
434
435                 [Test]
436                 public void CreateControlStyle ()
437                 {
438                         TestTable t = new TestTable ();
439                         t.BackImageUrl = imageUrl;
440                         t.CellPadding = 1;
441                         t.CellSpacing = 2;
442                         t.GridLines = GridLines.Horizontal;
443                         t.HorizontalAlign = HorizontalAlign.Left;
444
445                         TableStyle ts = (TableStyle)t.GetStyle ();
446                         // is it live ?
447                         ts.BackImageUrl = "mono";
448                         Assert.AreEqual ("mono", t.BackImageUrl, "BackImageUrl-2");
449                         ts.CellPadding = Int32.MaxValue;
450                         Assert.AreEqual (Int32.MaxValue, t.CellPadding, "CellPadding-2");
451                         ts.CellSpacing = 0;
452                         Assert.AreEqual (0, t.CellSpacing, "CellSpacing-2");
453                         ts.GridLines = GridLines.Vertical;
454                         Assert.AreEqual (GridLines.Vertical, t.GridLines, "GridLines-2");
455                         ts.HorizontalAlign = HorizontalAlign.Right;
456                         Assert.AreEqual (HorizontalAlign.Right, t.HorizontalAlign, "HorizontalAlign-2");
457                 }
458
459                 private string Adjust (string s)
460                 {
461                         // right now Mono doesn't generate the exact same indentation/lines as MS implementation
462                         // and different fx versions have different casing for enums
463                         return s.Replace ("\n", "").Replace ("\t", "").ToLower ();
464                 }
465
466                 [Test]
467                 public void Rows ()
468                 {
469                         TestTable t = new TestTable ();
470                         Assert.AreEqual (0, t.Rows.Count, "0");
471                         TableRow tr = new TableRow ();
472
473                         t.Rows.Add (tr);
474                         Assert.AreEqual (1, t.Rows.Count, "r1");
475                         Assert.AreEqual (1, t.Controls.Count, "c1");
476                         string s = t.Render ();
477 #if NET_4_0
478                         Assert.AreEqual (Adjust ("<table>\n\t<tr>\n\n\t</tr>\n</table>"), Adjust (s), "tr-1");
479 #else
480                         Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr>\n\n\t</tr>\n</table>"), Adjust (s), "tr-1");
481 #endif
482
483                         // change instance properties
484                         tr.HorizontalAlign = HorizontalAlign.Justify;
485                         s = t.Render ();
486 #if NET_4_0
487                         Assert.AreEqual (Adjust ("<table>\n\t<tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-1j");
488 #else
489                         Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-1j");
490 #endif
491
492                         // add it again (same instance)
493                         t.Rows.Add (tr);
494                         Assert.AreEqual (1, t.Rows.Count, "t1bis");
495                         Assert.AreEqual (1, t.Controls.Count, "c1bis");
496                         s = t.Render ();
497 #if NET_4_0
498                         Assert.AreEqual (Adjust ("<table>\n\t<tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-1bis");
499 #else
500                         Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-1bis");
501 #endif
502                         tr.HorizontalAlign = HorizontalAlign.NotSet;
503
504                         tr = new TableRow ();
505                         tr.HorizontalAlign = HorizontalAlign.Justify;
506                         t.Rows.Add (tr);
507                         Assert.AreEqual (2, t.Rows.Count, "r2");
508                         Assert.AreEqual (2, t.Controls.Count, "c2");
509                         s = t.Render ();
510 #if NET_4_0
511                         Assert.AreEqual (Adjust ("<table>\n\t<tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-2");
512 #else
513                         Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-2");
514 #endif
515
516                         tr = new TableRow ();
517                         tr.VerticalAlign = VerticalAlign.Bottom;
518                         t.Controls.Add (tr);
519                         Assert.AreEqual (3, t.Rows.Count, "r3");
520                         Assert.AreEqual (3, t.Controls.Count, "c3");
521                         s = t.Render ();
522 #if NET_4_0
523                         Assert.AreEqual (Adjust ("<table>\n\t<tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr><tr valign=\"bottom\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-3");
524 #else
525                         Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr><tr valign=\"bottom\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-3");
526 #endif
527
528                         t.Caption = "caption";
529                         s = t.Render ();
530 #if NET_4_0
531                         Assert.AreEqual (Adjust ("<table>\n\t<caption>\n\t\tcaption\n\t</caption><tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr><tr valign=\"bottom\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-2c");
532 #else
533                         Assert.AreEqual (Adjust ("<table border=\"0\">\n\t<caption>\n\t\tcaption\n\t</caption><tr>\n\n\t</tr><tr align=\"justify\">\n\n\t</tr><tr valign=\"bottom\">\n\n\t</tr>\n</table>"), Adjust (s), "tr-2c");
534 #endif
535                 }
536
537                 [Test]
538                 [ExpectedException (typeof (NullReferenceException))]
539                 public void ControlsAdd_Null ()
540                 {
541                         new Table ().Controls.Add (null);
542                 }
543
544                 [Test]
545                 [ExpectedException (typeof (ArgumentException))]
546                 public void ControlsAdd_LiteralControl ()
547                 {
548                         new Table ().Controls.Add (new LiteralControl ("mono"));
549                 }
550
551                 [Test]
552                 public void ControlsAdd_TableRow ()
553                 {
554                         Table t = new Table ();
555                         t.Controls.Add (new TableRow ());
556                         Assert.AreEqual (1, t.Controls.Count, "Controls");
557                         Assert.AreEqual (1, t.Rows.Count, "Rows");
558                 }
559
560                 [Test]
561                 public void ControlsAdd_TestTableRow ()
562                 {
563                         Table t = new Table ();
564                         t.Controls.Add (new TestTableRow ());
565                         Assert.AreEqual (1, t.Controls.Count, "Controls");
566                         Assert.AreEqual (1, t.Rows.Count, "Rows");
567                 }
568
569                 [Test]
570                 [ExpectedException (typeof (NullReferenceException))]
571                 public void ControlsAddAt_Null ()
572                 {
573                         new Table ().Controls.AddAt (0, null);
574                 }
575
576                 [Test]
577                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
578                 public void ControlsAddAt_Negative ()
579                 {
580                         new Table ().Controls.AddAt (Int32.MinValue, new TableRow ());
581                 }
582
583                 [Test]
584                 [ExpectedException (typeof (ArgumentException))]
585                 public void ControlsAddAt_LiteralControl ()
586                 {
587                         new Table ().Controls.AddAt (0, new LiteralControl ("mono"));
588                 }
589
590                 [Test]
591                 public void ControlsAddAt_TableRow ()
592                 {
593                         Table t = new Table ();
594                         t.Controls.AddAt (0, new TableRow ());
595                         Assert.AreEqual (1, t.Controls.Count, "Controls");
596                         Assert.AreEqual (1, t.Rows.Count, "Rows");
597                 }
598
599                 [Test]
600                 public void ControlsAddAt_TestTableRow ()
601                 {
602                         Table t = new Table ();
603                         t.Controls.AddAt (0, new TestTableRow ());
604                         Assert.AreEqual (1, t.Controls.Count, "Controls");
605                         Assert.AreEqual (1, t.Rows.Count, "Rows");
606                 }
607
608                 [Test]
609                 [ExpectedException (typeof (NullReferenceException))]
610                 public void RenderBeginTag_Null ()
611                 {
612                         Table t = new Table ();
613                         t.RenderBeginTag (null);
614                 }
615
616                 [Test]
617                 public void RenderBeginTag_Empty ()
618                 {
619                         HtmlTextWriter writer = GetWriter ();
620                         Table t = new Table ();
621                         t.RenderBeginTag (writer);
622                         string s = writer.InnerWriter.ToString ();
623 #if NET_4_0
624                         Assert.AreEqual ("<table>\n", s, "empty");
625 #else
626                         Assert.AreEqual ("<table border=\"0\">\n", s, "empty");
627 #endif
628                 }
629
630                 [Test]
631                 public void RenderBeginTag_Attributes ()
632                 {
633                         HtmlTextWriter writer = GetWriter ();
634                         Table t = new Table ();
635                         t.CellPadding = 1;
636                         t.RenderBeginTag (writer);
637                         string s = writer.InnerWriter.ToString ();
638 #if NET_4_0
639                         Assert.AreEqual ("<table cellpadding=\"1\">\n", s, "CellPadding");
640 #else
641                         Assert.AreEqual ("<table cellpadding=\"1\" border=\"0\">\n", s, "CellPadding");
642 #endif
643                 }
644
645                 [Test]
646                 public void RenderBeginTag_Caption ()
647                 {
648                         HtmlTextWriter writer = GetWriter ();
649                         Table t = new Table ();
650                         t.Caption = "caption";
651                         t.RenderBeginTag (writer);
652                         string s = writer.InnerWriter.ToString ();
653 #if NET_4_0
654                         Assert.AreEqual ("<table>\n\t<caption>\n\t\tcaption\n\t</caption>", s, "caption");
655 #else
656                         Assert.AreEqual ("<table border=\"0\">\n\t<caption>\n\t\tcaption\n\t</caption>", s, "caption");
657 #endif
658                 }
659
660                 [Test]
661                 public void RenderBeginTag_Caption_Align ()
662                 {
663                         HtmlTextWriter writer = GetWriter ();
664                         Table t = new Table ();
665                         t.Caption = "caption";
666                         t.CaptionAlign = TableCaptionAlign.Top;
667                         t.RenderBeginTag (writer);
668                         string s = writer.InnerWriter.ToString ();
669 #if NET_4_0
670                         Assert.AreEqual ("<table>\n\t<caption align=\"top\">\n\t\tcaption\n\t</caption>", s.ToLower (), "caption");
671 #else
672                         Assert.AreEqual ("<table border=\"0\">\n\t<caption align=\"top\">\n\t\tcaption\n\t</caption>", s.ToLower (), "caption");
673 #endif
674                 }
675
676                 [Test]
677                 public void RenderBeginTag_Row ()
678                 {
679                         HtmlTextWriter writer = GetWriter ();
680                         Table t = new Table ();
681                         t.Rows.Add (new TableRow ());
682                         t.RenderBeginTag (writer);
683                         string s = writer.InnerWriter.ToString ();
684 #if NET_4_0
685                         Assert.AreEqual ("<table>\n", s, "tr");
686 #else
687                         Assert.AreEqual ("<table border=\"0\">\n", s, "tr");
688 #endif
689                 }
690
691                 [Test]
692                 [Category("NunitWeb")] // Note: No event fired , only flow been checked.
693                 public void RaisePostBackEvent ()
694                 {
695                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RaisePostBackEvent__Init));
696                         string str = t.Run ();
697                         FormRequest fr = new FormRequest (t.Response, "form1");
698                         fr.Controls.Add ("__EVENTTARGET");
699                         fr.Controls.Add ("__EVENTARGUMENT");
700                         fr.Controls["__EVENTTARGET"].Value = "Table";
701                         fr.Controls["__EVENTARGUMENT"].Value = "";
702                         t.Request = fr;
703                         t.Run ();
704                         Assert.AreEqual ("RaisePostBackEvent", (String) t.UserData, "RaisePostBackEvent");
705                 }
706
707                 public static void RaisePostBackEvent__Init (Page page)
708                 {
709                         TestTable t = new TestTable ();
710                         t.ID = "Table";
711                         page.Form.Controls.Add (t);
712                 }
713
714                 [TestFixtureTearDown]
715                 public void TearDown ()
716                 {
717                         WebTest.Unload ();
718                 }
719         }
720 }