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 
Render()41 		public string Render ()
42 		{
43 			HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
44 			base.Render (writer);
45 			return writer.InnerWriter.ToString ();
46 		}
47 
GetCollection()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]
DefaultProperties()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]
NullProperties()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]
EmptyProperties()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]
CleanProperties()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]
MaxInt32()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]
MinInt32()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))]
WrongTypeString()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]
WrongTypeInt()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))]
InnerHtml_Get()175 		public void InnerHtml_Get ()
176 		{
177 			HtmlTable t = new HtmlTable ();
178 			Assert.IsNotNull (t.InnerHtml);
179 		}
180 
181 		[Test]
182 		[ExpectedException (typeof (NotSupportedException))]
InnerHtml_Set()183 		public void InnerHtml_Set ()
184 		{
185 			HtmlTable t = new HtmlTable ();
186 			t.InnerHtml = String.Empty;
187 		}
188 
189 		[Test]
190 		[ExpectedException (typeof (NotSupportedException))]
InnerText_Get()191 		public void InnerText_Get ()
192 		{
193 			HtmlTable t = new HtmlTable ();
194 			Assert.IsNotNull (t.InnerText);
195 		}
196 
197 		[Test]
198 		[ExpectedException (typeof (NotSupportedException))]
InnerText_Set()199 		public void InnerText_Set ()
200 		{
201 			HtmlTable t = new HtmlTable ();
202 			t.InnerText = String.Empty;
203 		}
204 
RemoveWS(string s)205 		private string RemoveWS (string s)
206 		{
207 			s = s.Replace ("\t", "");
208 			s = s.Replace ("\r", "");
209 			return s.Replace ("\n", "");
210 		}
211 
212 		[Test]
Render_Table_Simple()213 		public void Render_Table_Simple ()
214 		{
215 			TestHtmlTable t = new TestHtmlTable ();
216 			Assert.AreEqual (RemoveWS ("<table>\r\n</table>\r\n"), RemoveWS (t.Render ()));
217 		}
218 
219 		[Test]
Render_Table()220 		public void Render_Table ()
221 		{
222 			TestHtmlTable t = new TestHtmlTable ();
223 			t.Align = "*1*";
224 			t.BgColor = "*2*";
225 			t.Border = 3;
226 			t.BorderColor = "*4*";
227 			t.CellPadding = 5;
228 			t.CellSpacing = 6;
229 			t.Height = "*7*";
230 			t.Width = "*8*";
231 			Assert.AreEqual (RemoveWS ("<table align=\"*1*\" bgcolor=\"*2*\" border=\"3\" bordercolor=\"*4*\" cellpadding=\"5\" cellspacing=\"6\" height=\"*7*\" width=\"*8*\">\r\n</table>\r\n"), RemoveWS (t.Render ()));
232 		}
233 
234 		[Test]
Render_TableRow_Simple()235 		public void Render_TableRow_Simple ()
236 		{
237 			TestHtmlTable t = new TestHtmlTable ();
238 			t.Rows.Add (new HtmlTableRow ());
239 			Assert.AreEqual (RemoveWS ("<table>\r\n\t<tr>\r\n\t</tr>\r\n</table>\r\n"), RemoveWS (t.Render ()));
240 		}
241 
242 		[Test]
Render_TableRow()243 		public void Render_TableRow ()
244 		{
245 			TestHtmlTable t = new TestHtmlTable ();
246 			t.Border = 0;
247 			HtmlTableRow r1 = new HtmlTableRow ();
248 			r1.Align = "right";
249 			t.Rows.Add (r1);
250 			HtmlTableRow r2 = new HtmlTableRow ();
251 			r2.Align = "left";
252 			t.Rows.Add (r2);
253 			Assert.AreEqual (RemoveWS ("<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"), RemoveWS (t.Render ()));
254 		}
255 
256 		[Test]
Render_TableRowCell_Simple()257 		public void Render_TableRowCell_Simple ()
258 		{
259 			TestHtmlTable t = new TestHtmlTable ();
260 			HtmlTableRow r = new HtmlTableRow ();
261 			r.Cells.Add (new HtmlTableCell ());
262 			t.Rows.Add (r);
263 			Assert.AreEqual (RemoveWS ("<table>\r\n\t<tr>\r\n\t\t<td></td>\r\n\t</tr>\r\n</table>\r\n"),
264 					RemoveWS (t.Render ()));
265 		}
266 
267 		[Test]
Render_TableRowCell()268 		public void Render_TableRowCell ()
269 		{
270 			TestHtmlTable t = new TestHtmlTable ();
271 			t.Align = "center";
272 			HtmlTableRow r = new HtmlTableRow ();
273 			r.VAlign = "top";
274 			t.Rows.Add (r);
275 			HtmlTableCell c1 = new HtmlTableCell ();
276 			c1.Align = "right";
277 			c1.InnerText = "Go";
278 			r.Cells.Add (c1);
279 			HtmlTableCell c2 = new HtmlTableCell ();
280 			c2.Align = "left";
281 			c2.InnerHtml = "<a href=\"http://www.go-mono.com\">Mono</a>";
282 			r.Cells.Add (c2);
283 			Assert.AreEqual (RemoveWS ("<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"), RemoveWS (t.Render ()));
284 		}
285 
286 		[Test]
287 		[ExpectedException (typeof (NullReferenceException))]
HtmlTableRowControlCollectionAdd_Null()288 		public void HtmlTableRowControlCollectionAdd_Null ()
289 		{
290 			TestHtmlTable t = new TestHtmlTable ();
291 			ControlCollection c = t.GetCollection ();
292 			c.Add (null);
293 		}
294 
295 		[Test]
296 		[ExpectedException (typeof (ArgumentException))]
HtmlTableRowControlCollectionAdd_WrongType()297 		public void HtmlTableRowControlCollectionAdd_WrongType ()
298 		{
299 			TestHtmlTable t = new TestHtmlTable ();
300 			ControlCollection c = t.GetCollection ();
301 			c.Add (new HtmlTable ());
302 		}
303 
304 		[Test]
HtmlTableRowControlCollectionAdd()305 		public void HtmlTableRowControlCollectionAdd ()
306 		{
307 			TestHtmlTable t = new TestHtmlTable ();
308 			ControlCollection c = t.GetCollection ();
309 			c.Add (new HtmlTableRow ());
310 			c.Add (new InheritedHtmlTableRow ());
311 			Assert.AreEqual (2, c.Count, "Rows");
312 		}
313 
314 		[Test]
315 		[ExpectedException (typeof (NullReferenceException))]
HtmlTableRowControlCollectionAddAt_Null()316 		public void HtmlTableRowControlCollectionAddAt_Null ()
317 		{
318 			TestHtmlTable t = new TestHtmlTable ();
319 			ControlCollection c = t.GetCollection ();
320 			c.AddAt (0, null);
321 		}
322 
323 		[Test]
324 		[ExpectedException (typeof (ArgumentException))]
HtmlTableRowControlCollectionAddAt_WrongType()325 		public void HtmlTableRowControlCollectionAddAt_WrongType ()
326 		{
327 			TestHtmlTable t = new TestHtmlTable ();
328 			ControlCollection c = t.GetCollection ();
329 			c.AddAt (0, new HtmlTable ());
330 		}
331 
332 		[Test]
HtmlTableRowControlCollectionAddAt()333 		public void HtmlTableRowControlCollectionAddAt ()
334 		{
335 			TestHtmlTable t = new TestHtmlTable ();
336 			ControlCollection c = t.GetCollection ();
337 			c.AddAt (0, new HtmlTableRow ());
338 			c.AddAt (0, new InheritedHtmlTableRow ());
339 			Assert.AreEqual (2, c.Count, "Rows");
340 		}
341 	}
342 }
343