3 // - Unit tests for System.Web.UI.WebControls.TableRow
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
35 using System.Web.UI.WebControls;
37 using NUnit.Framework;
39 namespace MonoTests.System.Web.UI.WebControls {
41 public interface ITableRowTest {
45 StateBag StateBag { get; }
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; }
58 TableRowSection TableSection { get; set; }
62 public class TestTableRow : TableRow, ITableRowTest {
65 get { return base.TagName; }
68 public StateBag StateBag {
69 get { return base.ViewState; }
72 public string Render ()
74 StringWriter sw = new StringWriter ();
76 HtmlTextWriter writer = new HtmlTextWriter (sw);
78 return writer.InnerWriter.ToString ();
81 public Style GetStyle ()
83 return base.CreateControlStyle ();
88 public class TableRowTest {
90 private HtmlTextWriter GetWriter ()
92 StringWriter sw = new StringWriter ();
94 return new HtmlTextWriter (sw);
97 public virtual TableRow GetNewTableRow ()
99 return new TableRow ();
102 public virtual ITableRowTest GetNewTestTableRow ()
104 return new TestTableRow ();
108 public void DefaultProperties ()
110 ITableRowTest tr = GetNewTestTableRow ();
111 Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
112 Assert.AreEqual (0, tr.StateBag.Count, "ViewState.Count");
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");
118 Assert.AreEqual (TableRowSection.TableBody, tr.TableSection, "TableSection");
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");
126 public void NullProperties ()
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");
134 tr.TableSection = TableRowSection.TableBody;
135 Assert.AreEqual (TableRowSection.TableBody, tr.TableSection, "TableSection");
136 Assert.AreEqual (3, tr.StateBag.Count, "ViewState.Count-1");
138 Assert.AreEqual (2, tr.StateBag.Count, "ViewState.Count-1");
140 Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
144 public void CleanProperties ()
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");
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");
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");
167 Assert.AreEqual (2, tr.StateBag.Count, "ViewState.Count-1");
169 Assert.AreEqual (0, tr.Attributes.Count, "Attributes.Count");
173 // LAMESPEC: undocumented exception but similar to Image
174 [ExpectedException (typeof (ArgumentOutOfRangeException))]
175 public void HorizontalAlign_Invalid ()
177 TableRow tr = GetNewTableRow ();
178 tr.HorizontalAlign = (HorizontalAlign)Int32.MinValue;
182 // LAMESPEC: undocumented exception but similar to Image
183 [ExpectedException (typeof (ArgumentOutOfRangeException))]
184 public void VerticalAlign_Invalid ()
186 TableRow tr = GetNewTableRow ();
187 tr.VerticalAlign = (VerticalAlign)Int32.MinValue;
191 [ExpectedException (typeof (ArgumentOutOfRangeException))]
192 public void TableSection_Invalid ()
194 TableRow tr = GetNewTableRow ();
195 tr.TableSection = (TableRowSection)Int32.MinValue;
199 public void Render ()
201 ITableRowTest tr = GetNewTestTableRow ();
202 string s = tr.Render ();
203 Assert.AreEqual ("<tr>\n\n</tr>", s, "empty/default");
205 // case varies with fx versions
206 tr.HorizontalAlign = HorizontalAlign.Left;
208 Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"left\"") > 0), "HorizontalAlign.Left");
209 tr.HorizontalAlign = HorizontalAlign.Center;
211 Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"center\"") > 0), "HorizontalAlign.Center");
212 tr.HorizontalAlign = HorizontalAlign.Right;
214 Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"right\"") > 0), "HorizontalAlign.Justify");
215 tr.HorizontalAlign = HorizontalAlign.Justify;
217 Assert.IsTrue ((s.ToLower ().IndexOf (" align=\"justify\"") > 0), "HorizontalAlign.Justify");
218 tr.HorizontalAlign = HorizontalAlign.NotSet;
220 tr.VerticalAlign = VerticalAlign.Top;
222 Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"top\"") > 0), "VerticalAlign.Top");
223 tr.VerticalAlign = VerticalAlign.Middle;
225 Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"middle\"") > 0), "VerticalAlign.Middle");
226 tr.VerticalAlign = VerticalAlign.Bottom;
228 Assert.IsTrue ((s.ToLower ().IndexOf (" valign=\"bottom\"") > 0), "VerticalAlign.Bottom");
229 tr.VerticalAlign = VerticalAlign.NotSet;
231 // TableSection has no influence over the "row" rendering
232 tr.TableSection = TableRowSection.TableFooter;
234 Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableFooter");
235 tr.TableSection = TableRowSection.TableHeader;
237 Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableHeader");
238 tr.TableSection = TableRowSection.TableBody;
240 Assert.AreEqual ("<tr>\n\n</tr>", s, "TableRowSection.TableBody");
245 public void Render_Style ()
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");
252 TableItemStyle tis = new TableItemStyle ();
253 tis.BackColor = Color.Red;
254 tr.ControlStyle.CopyFrom (tis);
256 Assert.AreEqual ("<tr style=\"background-color:Red;\">\n\n</tr>", s, "CopyFrom");
260 public void CreateControlStyle ()
262 ITableRowTest tr = GetNewTestTableRow ();
263 tr.HorizontalAlign = HorizontalAlign.Left;
264 tr.VerticalAlign = VerticalAlign.Bottom;
266 TableItemStyle tis = (TableItemStyle)tr.GetStyle ();
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");
274 private string Adjust (string s)
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 ();
284 ITableRowTest tr = GetNewTestTableRow ();
285 Assert.AreEqual (0, tr.Cells.Count, "0");
286 TableCell td = new TableCell ();
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");
294 // change instance properties
297 Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td>\n</tr>"), Adjust (s), "td-1r");
299 // add it again (same instance)
301 Assert.AreEqual (1, tr.Cells.Count, "c1bis");
302 Assert.AreEqual (1, tr.Controls.Count, "k1bis");
304 Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td>\n</tr>"), Adjust (s), "tr-1bis");
306 td = new TableCell ();
307 td.VerticalAlign = VerticalAlign.Top;
309 Assert.AreEqual (2, tr.Cells.Count, "c2");
310 Assert.AreEqual (2, tr.Controls.Count, "k2");
312 Assert.AreEqual (Adjust ("<tr>\n\t<td rowspan=\"1\"></td><td valign=\"top\"></td>\n</tr>"), Adjust (s), "tr-2");
314 td = new TableCell ();
315 td.HorizontalAlign = HorizontalAlign.Center;
317 Assert.AreEqual (3, tr.Cells.Count, "c3");
318 Assert.AreEqual (3, tr.Controls.Count, "k3");
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");
322 tr.HorizontalAlign = HorizontalAlign.Right;
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");
328 [ExpectedException (typeof (NullReferenceException))]
329 public void ControlsAdd_Null ()
331 GetNewTableRow ().Controls.Add (null);
335 [ExpectedException (typeof (ArgumentException))]
336 public void ControlsAdd_LiteralControl ()
338 GetNewTableRow ().Controls.Add (new LiteralControl ("mono"));
342 public void ControlsAdd_TableCell ()
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");
351 public void ControlsAdd_TestTableRow ()
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");
360 [ExpectedException (typeof (NullReferenceException))]
361 public void ControlsAddAt_Null ()
363 GetNewTableRow ().Controls.AddAt (0, null);
367 [ExpectedException (typeof (ArgumentException))]
368 // note: for Table it's ArgumentOutOfRangeException
369 public void ControlsAddAt_Negative ()
371 GetNewTableRow ().Controls.AddAt (Int32.MinValue, new TableRow ());
375 [ExpectedException (typeof (ArgumentException))]
376 public void ControlsAddAt_LiteralControl ()
378 GetNewTableRow ().Controls.AddAt (0, new LiteralControl ("mono"));
382 public void ControlsAddAt_TableRow ()
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");
391 public void ControlsAddAt_TestTableRow ()
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");
400 [ExpectedException (typeof (NullReferenceException))]
401 public void RenderBeginTag_Null ()
403 TableRow tr = GetNewTableRow ();
404 tr.RenderBeginTag (null);
408 public void RenderBeginTag_Empty ()
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");
418 public void RenderBeginTag_HorizontalAlign ()
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");
429 public void RenderBeginTag_Cells ()
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");