87bb861949511b7cd0cbac9a8161339f5168b0cb
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.HtmlControls / HtmlTableTest.cs
1 //
2 // HtmlTableTest.cs
3 //      - Unit tests for System.Web.UI.HtmlControls.HtmlTable
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.UI;
33 using System.Web.UI.HtmlControls;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Web.UI.HtmlControls {
38
39         public class TestHtmlTable : HtmlTable {
40
41                 public string Render ()
42                 {
43                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
44                         base.Render (writer);
45                         return writer.InnerWriter.ToString ();
46                 }
47
48                 public ControlCollection GetCollection ()
49                 {
50                         return base.CreateControlCollection ();
51                 }
52         }
53
54         public class InheritedHtmlTableRow : HtmlTableRow {
55         }
56
57         [TestFixture]
58         public class HtmlTableTest {
59
60                 [Test]
61                 public void DefaultProperties ()
62                 {
63                         HtmlTable t = new HtmlTable ();
64                         Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
65
66                         Assert.AreEqual (String.Empty, t.Align, "Align");
67                         Assert.AreEqual (String.Empty, t.BgColor, "BgColor");
68                         Assert.AreEqual (-1, t.Border, "Border");
69                         Assert.AreEqual (String.Empty, t.BorderColor, "BorderColor");
70                         Assert.AreEqual (-1, t.CellPadding, "CellPadding");
71                         Assert.AreEqual (-1, t.CellSpacing, "CellSpacing");
72                         Assert.AreEqual (String.Empty, t.Height, "Height");
73                         Assert.AreEqual (0, t.Rows.Count, "Rows");
74                         Assert.AreEqual (String.Empty, t.Width, "Width");
75
76                         Assert.AreEqual ("table", t.TagName, "TagName");
77                 }
78
79                 [Test]
80                 public void NullProperties ()
81                 {
82                         HtmlTable t = new HtmlTable ();
83                         t.Align = null;
84                         Assert.AreEqual (String.Empty, t.Align, "Align");
85                         t.BgColor = null;
86                         Assert.AreEqual (String.Empty, t.BgColor, "BgColor");
87                         t.BorderColor = null;
88                         Assert.AreEqual (String.Empty, t.BorderColor, "BorderColor");
89                         t.Height = null;
90                         Assert.AreEqual (String.Empty, t.Height, "Height");
91                         t.Width = null;
92                         Assert.AreEqual (String.Empty, t.Width, "Width");
93
94                         Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
95                 }
96
97                 [Test]
98                 public void EmptyProperties ()
99                 {
100                         HtmlTable t = new HtmlTable ();
101                         t.Border = -1;
102                         Assert.AreEqual (-1, t.Border, "Border");
103                         t.CellPadding = -1;
104                         Assert.AreEqual (-1, t.CellPadding, "CellPadding");
105                         t.CellSpacing = -1;
106                         Assert.AreEqual (-1, t.CellSpacing, "CellSpacing");
107
108                         Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count");
109                 }
110
111                 [Test]
112                 public void CleanProperties ()
113                 {
114                         HtmlTable t = new HtmlTable ();
115                         t.Align = "center";
116                         Assert.AreEqual ("center", t.Align, "Align");
117                         t.Border = 1;
118                         Assert.AreEqual (1, t.Border, "Border");
119                         Assert.AreEqual (2, t.Attributes.Count, "2");
120
121                         t.Border = -1;
122                         Assert.AreEqual (-1, t.Border, "-Border");
123                         t.Align = null;
124                         Assert.AreEqual (String.Empty, t.Align, "-Align");
125                         Assert.AreEqual (0, t.Attributes.Count, "0");
126                 }
127
128                 [Test]
129                 public void MaxInt32 ()
130                 {
131                         HtmlTable t = new HtmlTable ();
132                         t.Border = Int32.MaxValue;
133                         Assert.AreEqual (Int32.MaxValue, t.Border, "Border");
134                         t.CellPadding = Int32.MaxValue;
135                         Assert.AreEqual (Int32.MaxValue, t.CellPadding, "CellPadding");
136                         t.CellSpacing = Int32.MaxValue;
137                         Assert.AreEqual (Int32.MaxValue, t.CellSpacing, "CellSpacing");
138
139                         Assert.AreEqual (3, t.Attributes.Count, "Attributes.Count");
140                 }
141
142                 [Test]
143                 public void MinInt32 ()
144                 {
145                         HtmlTable t = new HtmlTable ();
146                         t.Border = Int32.MinValue;
147                         Assert.AreEqual (Int32.MinValue, t.Border, "Border");
148                         t.CellPadding = Int32.MinValue;
149                         Assert.AreEqual (Int32.MinValue, t.CellPadding, "CellPadding");
150                         t.CellSpacing = Int32.MinValue;
151                         Assert.AreEqual (Int32.MinValue, t.CellSpacing, "CellSpacing");
152
153                         Assert.AreEqual (3, t.Attributes.Count, "Attributes.Count");
154                 }
155
156                 [Test]
157                 [ExpectedException (typeof (FormatException))]
158                 public void WrongTypeString ()
159                 {
160                         HtmlTable t = new HtmlTable ();
161                         t.Attributes.Add ("Border", "yes");
162                         Assert.AreEqual (-1, t.Border, "Border");
163                 }
164
165                 [Test]
166                 public void WrongTypeInt ()
167                 {
168                         HtmlTable t = new HtmlTable ();
169                         t.Border = 42;
170                         Assert.AreEqual ("42", t.Attributes ["border"], "Border");
171                 }
172
173                 [Test]
174                 [ExpectedException (typeof (NotSupportedException))]
175                 public void InnerHtml_Get ()
176                 {
177                         HtmlTable t = new HtmlTable ();
178                         Assert.IsNotNull (t.InnerHtml);
179                 }
180
181                 [Test]
182                 [ExpectedException (typeof (NotSupportedException))]
183                 public void InnerHtml_Set ()
184                 {
185                         HtmlTable t = new HtmlTable ();
186                         t.InnerHtml = String.Empty;
187                 }
188
189                 [Test]
190                 [ExpectedException (typeof (NotSupportedException))]
191                 public void InnerText_Get ()
192                 {
193                         HtmlTable t = new HtmlTable ();
194                         Assert.IsNotNull (t.InnerText);
195                 }
196
197                 [Test]
198                 [ExpectedException (typeof (NotSupportedException))]
199                 public void InnerText_Set ()
200                 {
201                         HtmlTable t = new HtmlTable ();
202                         t.InnerText = String.Empty;
203                 }
204
205                 private string AdjustLineEndings (string s)
206                 {
207                         return s.Replace ("\r\n", Environment.NewLine);
208                 }
209
210                 [Test]
211                 public void Render_Table_Simple ()
212                 {
213                         TestHtmlTable t = new TestHtmlTable ();
214                         Assert.AreEqual (AdjustLineEndings ("<table>\r\n</table>\r\n"), t.Render ());
215                 }
216
217                 [Test]
218                 public void Render_Table ()
219                 {
220                         TestHtmlTable t = new TestHtmlTable ();
221                         t.Align = "*1*";
222                         t.BgColor = "*2*";
223                         t.Border = 3;
224                         t.BorderColor = "*4*";
225                         t.CellPadding = 5;
226                         t.CellSpacing = 6;
227                         t.Height = "*7*";
228                         t.Width = "*8*";
229                         Assert.AreEqual (AdjustLineEndings ("<table align=\"*1*\" bgcolor=\"*2*\" border=\"3\" bordercolor=\"*4*\" cellpadding=\"5\" cellspacing=\"6\" height=\"*7*\" width=\"*8*\">\r\n</table>\r\n"), t.Render ());
230                 }
231
232                 [Test]
233                 public void Render_TableRow_Simple ()
234                 {
235                         TestHtmlTable t = new TestHtmlTable ();
236                         t.Rows.Add (new HtmlTableRow ());
237                         Assert.AreEqual (AdjustLineEndings ("<table>\r\n\t<tr>\r\n\t</tr>\r\n</table>\r\n"), t.Render ());
238                 }
239
240                 [Test]
241                 public void Render_TableRow ()
242                 {
243                         TestHtmlTable t = new TestHtmlTable ();
244                         t.Border = 0;
245                         HtmlTableRow r1 = new HtmlTableRow ();
246                         r1.Align = "right";
247                         t.Rows.Add (r1);
248                         HtmlTableRow r2 = new HtmlTableRow ();
249                         r2.Align = "left";
250                         t.Rows.Add (r2);
251                         Assert.AreEqual (AdjustLineEndings ("<table border=\"0\">\r\n\t<tr align=\"right\">\r\n\t</tr>\r\n\t<tr align=\"left\">\r\n\t</tr>\r\n</table>\r\n"), t.Render ());
252                 }
253
254                 [Test]
255                 public void Render_TableRowCell_Simple ()
256                 {
257                         TestHtmlTable t = new TestHtmlTable ();
258                         HtmlTableRow r = new HtmlTableRow ();
259                         r.Cells.Add (new HtmlTableCell ());
260                         t.Rows.Add (r);
261                         Assert.AreEqual (AdjustLineEndings ("<table>\r\n\t<tr>\r\n\t\t<td></td>\r\n\t</tr>\r\n</table>\r\n"), t.Render ());
262                 }
263
264                 [Test]
265                 public void Render_TableRowCell ()
266                 {
267                         TestHtmlTable t = new TestHtmlTable ();
268                         t.Align = "center";
269                         HtmlTableRow r = new HtmlTableRow ();
270                         r.VAlign = "top";
271                         t.Rows.Add (r);
272                         HtmlTableCell c1 = new HtmlTableCell ();
273                         c1.Align = "right";
274                         c1.InnerText = "Go";
275                         r.Cells.Add (c1);
276                         HtmlTableCell c2 = new HtmlTableCell ();
277                         c2.Align = "left";
278                         c2.InnerHtml = "<a href=\"http://www.go-mono.com\">Mono</a>";
279                         r.Cells.Add (c2);
280                         Assert.AreEqual (AdjustLineEndings ("<table align=\"center\">\r\n\t<tr valign=\"top\">\r\n\t\t<td align=\"right\">Go</td>\r\n\t\t<td align=\"left\"><a href=\"http://www.go-mono.com\">Mono</a></td>\r\n\t</tr>\r\n</table>\r\n"), t.Render ());
281                 }
282
283                 [Test]
284                 [ExpectedException (typeof (NullReferenceException))]
285                 public void HtmlTableRowControlCollectionAdd_Null ()
286                 {
287                         TestHtmlTable t = new TestHtmlTable ();
288                         ControlCollection c = t.GetCollection ();
289                         c.Add (null);
290                 }
291
292                 [Test]
293                 [ExpectedException (typeof (ArgumentException))]
294                 public void HtmlTableRowControlCollectionAdd_WrongType ()
295                 {
296                         TestHtmlTable t = new TestHtmlTable ();
297                         ControlCollection c = t.GetCollection ();
298                         c.Add (new HtmlTable ());
299                 }
300
301                 [Test]
302                 public void HtmlTableRowControlCollectionAdd ()
303                 {
304                         TestHtmlTable t = new TestHtmlTable ();
305                         ControlCollection c = t.GetCollection ();
306                         c.Add (new HtmlTableRow ());
307                         c.Add (new InheritedHtmlTableRow ());
308                         Assert.AreEqual (2, c.Count, "Rows");
309                 }
310
311                 [Test]
312                 [ExpectedException (typeof (NullReferenceException))]
313                 public void HtmlTableRowControlCollectionAddAt_Null ()
314                 {
315                         TestHtmlTable t = new TestHtmlTable ();
316                         ControlCollection c = t.GetCollection ();
317                         c.AddAt (0, null);
318                 }
319
320                 [Test]
321                 [ExpectedException (typeof (ArgumentException))]
322                 public void HtmlTableRowControlCollectionAddAt_WrongType ()
323                 {
324                         TestHtmlTable t = new TestHtmlTable ();
325                         ControlCollection c = t.GetCollection ();
326                         c.AddAt (0, new HtmlTable ());
327                 }
328
329                 [Test]
330                 public void HtmlTableRowControlCollectionAddAt ()
331                 {
332                         TestHtmlTable t = new TestHtmlTable ();
333                         ControlCollection c = t.GetCollection ();
334                         c.AddAt (0, new HtmlTableRow ());
335                         c.AddAt (0, new InheritedHtmlTableRow ());
336                         Assert.AreEqual (2, c.Count, "Rows");
337                 }
338         }
339 }