Tests
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / TableRowTest.cs
1 //
2 // TableRowTest.cs
3 //      - Unit tests for System.Web.UI.WebControls.TableRow
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.Drawing;
32 using System.IO;
33 using System.Web;
34 using System.Web.UI;
35 using System.Web.UI.WebControls;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Web.UI.WebControls {
40
41         public interface ITableRowTest {
42
43                 // testing
44                 string Tag { get; }
45                 StateBag StateBag { get; }
46                 string Render ();
47                 Style GetStyle ();
48
49                 // TableRow
50                 AttributeCollection Attributes { get; }
51                 Color BackColor { get; set; }
52                 ControlCollection Controls { get; }
53                 TableCellCollection Cells { get; }
54                 Style ControlStyle { get; }
55                 HorizontalAlign HorizontalAlign { get; set; }
56                 VerticalAlign VerticalAlign { get; set; }
57 #if NET_2_0
58                 TableRowSection TableSection { get; set; }
59 #endif
60         }
61
62         public class TestTableRow : TableRow, ITableRowTest {
63
64                 public string Tag {
65                         get { return base.TagName; }
66                 }
67
68                 public StateBag StateBag {
69                         get { return base.ViewState; }
70                 }
71
72                 public string Render ()
73                 {
74                         StringWriter sw = new StringWriter ();
75                         sw.NewLine = "\n";
76                         HtmlTextWriter writer = new HtmlTextWriter (sw);
77                         base.Render (writer);
78                         return writer.InnerWriter.ToString ();
79                 }
80
81                 public Style GetStyle ()
82                 {
83                         return base.CreateControlStyle ();
84                 }
85         }
86
87         [TestFixture]
88         public class TableRowTest {
89
90                 private HtmlTextWriter GetWriter ()
91                 {
92                         StringWriter sw = new StringWriter ();
93                         sw.NewLine = "\n";
94                         return new HtmlTextWriter (sw);
95                 }
96
97                 public virtual TableRow GetNewTableRow ()
98                 {
99                         return new TableRow ();
100                 }
101
102                 public virtual ITableRowTest GetNewTestTableRow ()
103                 {
104                         return new TestTableRow ();
105                 }
106
107                 [Test]
108                 public void DefaultProperties ()
109                 {
110                         ITableRowTest tr = GetNewTestTableRow ();
111                         Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
112                         Assert.AreEqual (0, tr.StateBag.Count, "ViewState.Count");
113
114                         Assert.AreEqual (0, tr.Cells.Count, "Cells.Count");
115                         Assert.AreEqual (HorizontalAlign.NotSet, tr.HorizontalAlign, "HorizontalAlign");
116                         Assert.AreEqual (VerticalAlign.NotSet, tr.VerticalAlign, "VerticalAlign");
117 #if NET_2_0
118                         Assert.AreEqual (TableRowSection.TableBody, tr.TableSection, "TableSection");
119 #endif
120                         Assert.AreEqual ("tr", tr.Tag, "TagName");
121                         Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count-2");
122                         Assert.AreEqual (0, tr.StateBag.Count, "ViewState.Count-2");
123                 }
124
125                 [Test]
126                 public void NullProperties ()
127                 {
128                         ITableRowTest tr = GetNewTestTableRow ();
129                         tr.HorizontalAlign = HorizontalAlign.NotSet;
130                         Assert.AreEqual (HorizontalAlign.NotSet, tr.HorizontalAlign, "HorizontalAlign");
131                         tr.VerticalAlign = VerticalAlign.NotSet;
132                         Assert.AreEqual (VerticalAlign.NotSet, tr.VerticalAlign, "VerticalAlign");
133 #if NET_2_0
134                         tr.TableSection = TableRowSection.TableBody;
135                         Assert.AreEqual (TableRowSection.TableBody, tr.TableSection, "TableSection");
136                         Assert.AreEqual (3, tr.StateBag.Count, "ViewState.Count-1");
137 #else
138                         Assert.AreEqual (2, tr.StateBag.Count, "ViewState.Count-1");
139 #endif
140                         Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
141                 }
142
143                 [Test]
144                 public void CleanProperties ()
145                 {
146                         ITableRowTest tr = GetNewTestTableRow ();
147                         tr.HorizontalAlign = HorizontalAlign.Justify;
148                         Assert.AreEqual (HorizontalAlign.Justify, tr.HorizontalAlign, "HorizontalAlign");
149                         tr.VerticalAlign = VerticalAlign.Bottom;
150                         Assert.AreEqual (VerticalAlign.Bottom, tr.VerticalAlign, "VerticalAlign");
151                         Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
152                         Assert.AreEqual (2, tr.StateBag.Count, "ViewState.Count");
153
154                         tr.HorizontalAlign = HorizontalAlign.NotSet;
155                         Assert.AreEqual (HorizontalAlign.NotSet, tr.HorizontalAlign, "HorizontalAlign");
156                         tr.VerticalAlign = VerticalAlign.NotSet;
157                         Assert.AreEqual (VerticalAlign.NotSet, tr.VerticalAlign, "VerticalAlign");
158 #if NET_2_0
159                         tr.TableSection = TableRowSection.TableFooter;
160                         Assert.AreEqual (TableRowSection.TableFooter, tr.TableSection, "TableFooter");
161                         tr.TableSection = TableRowSection.TableHeader;
162                         Assert.AreEqual (TableRowSection.TableHeader, tr.TableSection, "TableHeader");
163                         tr.TableSection = TableRowSection.TableBody;
164                         Assert.AreEqual (TableRowSection.TableBody, tr.TableSection, "TableBody");
165                         Assert.AreEqual (3, tr.StateBag.Count, "ViewState.Count-1");
166 #else
167                         Assert.AreEqual (2, tr.StateBag.Count, "ViewState.Count-1");
168 #endif
169                         Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
170                 }
171
172                 [Test]
173                 // LAMESPEC: undocumented exception but similar to Image
174                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
175                 public void HorizontalAlign_Invalid ()
176                 {
177                         TableRow tr = GetNewTableRow ();
178                         tr.HorizontalAlign = (HorizontalAlign)Int32.MinValue;
179                 }
180
181                 [Test]
182                 // LAMESPEC: undocumented exception but similar to Image
183                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
184                 public void VerticalAlign_Invalid ()
185                 {
186                         TableRow tr = GetNewTableRow ();
187                         tr.VerticalAlign = (VerticalAlign)Int32.MinValue;
188                 }
189 #if NET_2_0
190                 [Test]
191                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
192                 public void TableSection_Invalid ()
193                 {
194                         TableRow tr = GetNewTableRow ();
195                         tr.TableSection = (TableRowSection)Int32.MinValue;
196                 }
197 #endif
198                 [Test]
199                 public void Render ()
200                 {
201                         ITableRowTest tr = GetNewTestTableRow ();
202                         string s = tr.Render ();
203                         Assert.AreEqual ("<tr>\n\n</tr>", s, "empty/default");
204
205                         // case varies with fx versions
206                         tr.HorizontalAlign = HorizontalAlign.Left;
207                         s = tr.Render ();
208                         Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"left\"") > 0), "HorizontalAlign.Left");
209                         tr.HorizontalAlign = HorizontalAlign.Center;
210                         s = tr.Render ();
211                         Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"center\"") > 0), "HorizontalAlign.Center");
212                         tr.HorizontalAlign = HorizontalAlign.Right;
213                         s = tr.Render ();
214                         Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"right\"") > 0), "HorizontalAlign.Justify");
215                         tr.HorizontalAlign = HorizontalAlign.Justify;
216                         s = tr.Render ();
217                         Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"justify\"") > 0), "HorizontalAlign.Justify");
218                         tr.HorizontalAlign = HorizontalAlign.NotSet;
219
220                         tr.VerticalAlign = VerticalAlign.Top;
221                         s = tr.Render ();
222                         Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"top\"") > 0), "VerticalAlign.Top");
223                         tr.VerticalAlign = VerticalAlign.Middle;
224                         s = tr.Render ();
225                         Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"middle\"") > 0), "VerticalAlign.Middle");
226                         tr.VerticalAlign = VerticalAlign.Bottom;
227                         s = tr.Render ();
228                         Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"bottom\"") > 0), "VerticalAlign.Bottom");
229                         tr.VerticalAlign = VerticalAlign.NotSet;
230 #if NET_2_0
231                         // TableSection has no influence over the "row" rendering
232                         tr.TableSection = TableRowSection.TableFooter;
233                         s = tr.Render ();
234                         Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableFooter");
235                         tr.TableSection = TableRowSection.TableHeader;
236                         s = tr.Render ();
237                         Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableHeader");
238                         tr.TableSection = TableRowSection.TableBody;
239                         s = tr.Render ();
240                         Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableBody");
241 #endif
242                 }
243
244                 [Test]
245                 public void Render_Style ()
246                 {
247                         ITableRowTest tr = GetNewTestTableRow ();
248                         tr.BackColor = Color.Aqua;
249                         string s = tr.Render ();
250                         Assert.AreEqual ("<tr style=\"background-color:Aqua;\">\n\n</tr>", s, "direct");
251
252                         TableItemStyle tis = new TableItemStyle ();
253                         tis.BackColor = Color.Red;
254                         tr.ControlStyle.CopyFrom (tis);
255                         s = tr.Render ();
256                         Assert.AreEqual ("<tr style=\"background-color:Red;\">\n\n</tr>", s, "CopyFrom");
257                 }
258
259                 [Test]
260                 public void CreateControlStyle ()
261                 {
262                         ITableRowTest tr = GetNewTestTableRow ();
263                         tr.HorizontalAlign = HorizontalAlign.Left;
264                         tr.VerticalAlign = VerticalAlign.Bottom;
265
266                         TableItemStyle tis = (TableItemStyle)tr.GetStyle ();
267                         // is it live ?
268                         tis.HorizontalAlign = HorizontalAlign.Right;
269                         Assert.AreEqual (HorizontalAlign.Right, tr.HorizontalAlign, "HorizontalAlign-2");
270                         tis.VerticalAlign = VerticalAlign.Top;
271                         Assert.AreEqual (VerticalAlign.Top, tr.VerticalAlign, "VerticalAlign-2");
272                 }
273
274                 private string Adjust (string s)
275                 {
276                         // right now Mono doesn't generate the exact same indentation/lines as MS implementation
277                         // and different fx versions have different casing for enums
278                         return s.Replace ("\n", "").Replace ("\t", "").ToLower ();
279                 }
280
281                 [Test]
282                 public void Cells ()
283                 {
284                         ITableRowTest tr = GetNewTestTableRow ();
285                         Assert.AreEqual (0, tr.Cells.Count, "0");
286                         TableCell td = new TableCell ();
287
288                         tr.Cells.Add (td);
289                         Assert.AreEqual (1, tr.Cells.Count, "c1");
290                         Assert.AreEqual (1, tr.Controls.Count, "k1");
291                         string s = tr.Render ();
292                         Assert.AreEqual (Adjust ("<tr>\n\t<td></td>\n</tr>"), Adjust (s), "td-1");
293
294                         // change instance properties
295                         td.RowSpan = 1;
296                         s = tr.Render ();
297                         Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td>\n</tr>"), Adjust (s), "td-1r");
298
299                         // add it again (same instance)
300                         tr.Cells.Add (td);
301                         Assert.AreEqual (1, tr.Cells.Count, "c1bis");
302                         Assert.AreEqual (1, tr.Controls.Count, "k1bis");
303                         s = tr.Render ();
304                         Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td>\n</tr>"), Adjust (s), "tr-1bis");
305
306                         td = new TableCell ();
307                         td.VerticalAlign = VerticalAlign.Top;
308                         tr.Cells.Add (td);
309                         Assert.AreEqual (2, tr.Cells.Count, "c2");
310                         Assert.AreEqual (2, tr.Controls.Count, "k2");
311                         s = tr.Render ();
312                         Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td><td valign=\"top\"></td>\n</tr>"), Adjust (s), "tr-2");
313
314                         td = new TableCell ();
315                         td.HorizontalAlign = HorizontalAlign.Center;
316                         tr.Cells.Add (td);
317                         Assert.AreEqual (3, tr.Cells.Count, "c3");
318                         Assert.AreEqual (3, tr.Controls.Count, "k3");
319                         s = tr.Render ();
320                         Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td><td valign=\"top\"></td><td align=\"center\"></td>\n</tr>"), Adjust (s), "tr-3");
321
322                         tr.HorizontalAlign = HorizontalAlign.Right;
323                         s = tr.Render ();
324                         Assert.AreEqual (Adjust ("<tr align=\"right\">\n\t<td rowspan=\"1\"></td><td valign=\"top\"></td><td align=\"center\"></td>\n</tr>"), Adjust (s), "tr-3a");
325                 }
326
327                 [Test]
328                 [ExpectedException (typeof (NullReferenceException))]
329                 public void ControlsAdd_Null ()
330                 {
331                         GetNewTableRow ().Controls.Add (null);
332                 }
333
334                 [Test]
335                 [ExpectedException (typeof (ArgumentException))]
336                 public void ControlsAdd_LiteralControl ()
337                 {
338                         GetNewTableRow ().Controls.Add (new LiteralControl ("mono"));
339                 }
340
341                 [Test]
342                 public void ControlsAdd_TableCell ()
343                 {
344                         TableRow tr = GetNewTableRow ();
345                         tr.Controls.Add (new TableCell ());
346                         Assert.AreEqual (1, tr.Controls.Count, "Controls");
347                         Assert.AreEqual (1, tr.Cells.Count, "Cells");
348                 }
349
350                 [Test]
351                 public void ControlsAdd_TestTableRow ()
352                 {
353                         TableRow tr = GetNewTableRow ();
354                         tr.Controls.Add (new TestTableCell ());
355                         Assert.AreEqual (1, tr.Controls.Count, "Controls");
356                         Assert.AreEqual (1, tr.Cells.Count, "Cells");
357                 }
358
359                 [Test]
360                 [ExpectedException (typeof (NullReferenceException))]
361                 public void ControlsAddAt_Null ()
362                 {
363                         GetNewTableRow ().Controls.AddAt (0, null);
364                 }
365
366                 [Test]
367                 [ExpectedException (typeof (ArgumentException))]
368                 // note: for Table it's ArgumentOutOfRangeException
369                 public void ControlsAddAt_Negative ()
370                 {
371                         GetNewTableRow ().Controls.AddAt (Int32.MinValue, new TableRow ());
372                 }
373
374                 [Test]
375                 [ExpectedException (typeof (ArgumentException))]
376                 public void ControlsAddAt_LiteralControl ()
377                 {
378                         GetNewTableRow ().Controls.AddAt (0, new LiteralControl ("mono"));
379                 }
380
381                 [Test]
382                 public void ControlsAddAt_TableRow ()
383                 {
384                         TableRow tr = GetNewTableRow ();
385                         tr.Controls.AddAt (0, new TableCell ());
386                         Assert.AreEqual (1, tr.Controls.Count, "Controls");
387                         Assert.AreEqual (1, tr.Cells.Count, "Cells");
388                 }
389
390                 [Test]
391                 public void ControlsAddAt_TestTableRow ()
392                 {
393                         TableRow tr = GetNewTableRow ();
394                         tr.Controls.AddAt (0, new TestTableCell ());
395                         Assert.AreEqual (1, tr.Controls.Count, "Controls");
396                         Assert.AreEqual (1, tr.Cells.Count, "Cells");
397                 }
398
399                 [Test]
400                 [ExpectedException (typeof (NullReferenceException))]
401                 public void RenderBeginTag_Null ()
402                 {
403                         TableRow tr = GetNewTableRow ();
404                         tr.RenderBeginTag (null);
405                 }
406
407                 [Test]
408                 public void RenderBeginTag_Empty ()
409                 {
410                         HtmlTextWriter writer = GetWriter ();
411                         TableRow tr = GetNewTableRow ();
412                         tr.RenderBeginTag (writer);
413                         string s = writer.InnerWriter.ToString ();
414                         Assert.AreEqual ("<tr>\n", s, "empty");
415                 }
416
417                 [Test]
418                 public void RenderBeginTag_HorizontalAlign ()
419                 {
420                         HtmlTextWriter writer = GetWriter ();
421                         TableRow tr = GetNewTableRow ();
422                         tr.HorizontalAlign = HorizontalAlign.Center;
423                         tr.RenderBeginTag (writer);
424                         string s = writer.InnerWriter.ToString ();
425                         Assert.IsTrue (s.ToLower ().StartsWith ("<tr align=\"center\">"), "HorizontalAlign");
426                 }
427
428                 [Test]
429                 public void RenderBeginTag_Cells ()
430                 {
431                         HtmlTextWriter writer = GetWriter ();
432                         TableRow tr = GetNewTableRow ();
433                         tr.Cells.Add (new TableCell ());
434                         tr.RenderBeginTag (writer);
435                         string s = writer.InnerWriter.ToString ();
436                         Assert.AreEqual ("<tr>\n", s, "td");
437                 }
438         }
439 }