// // TableTest.cs // - Unit tests for System.Web.UI.WebControls.Table // // Author: // Sebastien Pouliot // // Copyright (C) 2005 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.IO; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MonoTests.SystemWeb.Framework; using NUnit.Framework; using MonoTests.stand_alone.WebHarness; namespace MonoTests.System.Web.UI.WebControls { public class TestTable : Table { public string Tag { get { return base.TagName; } } public StateBag StateBag { get { return base.ViewState; } } public string Render () { StringWriter sw = new StringWriter (); sw.NewLine = "\n"; HtmlTextWriter writer = new HtmlTextWriter (sw); base.Render (writer); return writer.InnerWriter.ToString (); } public Style GetStyle () { return base.CreateControlStyle (); } #if NET_2_0 protected override void RaisePostBackEvent (string argument) { WebTest.CurrentTest.UserData = "RaisePostBackEvent"; base.RaisePostBackEvent (argument); } #endif } [TestFixture] public class TableTest { private const string imageUrl = "http://www.mono-project.com/stylesheets/images.wiki.png"; private const string localImageUrl = "foo.jpg"; private HtmlTextWriter GetWriter () { StringWriter sw = new StringWriter (); sw.NewLine = "\n"; return new HtmlTextWriter (sw); } [Test] public void DefaultProperties () { TestTable t = new TestTable (); Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count"); Assert.AreEqual (0, t.StateBag.Count, "ViewState.Count"); Assert.AreEqual (String.Empty, t.BackImageUrl, "BackImageUrl"); Assert.AreEqual (String.Empty, t.Caption, "Caption"); Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign"); Assert.AreEqual (-1, t.CellPadding, "CellPadding"); Assert.AreEqual (-1, t.CellSpacing, "CellSpacing"); Assert.AreEqual (GridLines.None, t.GridLines, "GridLines"); Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "HorizontalAlign"); Assert.AreEqual (0, t.Rows.Count, "Rows.Count"); Assert.AreEqual ("table", t.Tag, "TagName"); Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count-2"); Assert.AreEqual (0, t.StateBag.Count, "ViewState.Count-2"); #if NET_2_0 Assert.AreEqual (String.Empty, t.Caption, "Caption"); Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign"); #endif } #if NET_2_0 [Test] public void Caption () { TestTable t = new TestTable (); t.Caption = "CaptionText"; string html = t.Render (); #if NET_4_0 string orig = "\n\t\n
\n\t\tCaptionText\n\t
"; #else string orig = "\n\t\n
\n\t\tCaptionText\n\t
"; #endif HtmlDiff.AssertAreEqual (orig, html, "Caption"); } [Test] public void CaptionAlign () { TestTable t = new TestTable (); t.Caption = "CaptionText"; t.CaptionAlign = TableCaptionAlign.Left; string html = t.Render (); #if NET_4_0 string orig = "\n\t\n
\n\t\tCaptionText\n\t
"; #else string orig = "\n\t\n
\n\t\tCaptionText\n\t
"; #endif HtmlDiff.AssertAreEqual (orig, html, "CaptionAlign"); } #endif [Test] public void NullProperties () { TestTable t = new TestTable (); t.BackImageUrl = String.Empty; // doesn't accept null, see specific test Assert.AreEqual (String.Empty, t.BackImageUrl, "BackImageUrl"); t.Caption = null; // doesn't get added to ViewState Assert.AreEqual (String.Empty, t.Caption, "Caption"); t.CaptionAlign = TableCaptionAlign.NotSet; Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "CaptionAlign"); t.CellPadding = -1; Assert.AreEqual (-1, t.CellPadding, "CellPadding"); t.CellSpacing = -1; Assert.AreEqual (-1, t.CellSpacing, "CellSpacing"); t.GridLines = GridLines.None; Assert.AreEqual (GridLines.None, t.GridLines, "GridLines"); t.HorizontalAlign = HorizontalAlign.NotSet; Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "HorizontalAlign"); Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count"); Assert.AreEqual (6, t.StateBag.Count, "ViewState.Count-1"); } [Test] public void CleanProperties () { TestTable t = new TestTable (); t.BackImageUrl = imageUrl; Assert.AreEqual (imageUrl, t.BackImageUrl, "BackImageUrl"); t.Caption = "Mono"; Assert.AreEqual ("Mono", t.Caption, "Caption"); t.CaptionAlign = TableCaptionAlign.Top; Assert.AreEqual (TableCaptionAlign.Top, t.CaptionAlign, "CaptionAlign"); t.CellPadding = 1; Assert.AreEqual (1, t.CellPadding, "CellPadding"); t.CellSpacing = 2; Assert.AreEqual (2, t.CellSpacing, "CellSpacing"); t.GridLines = GridLines.Both; Assert.AreEqual (GridLines.Both, t.GridLines, "GridLines"); t.HorizontalAlign = HorizontalAlign.Justify; Assert.AreEqual (HorizontalAlign.Justify, t.HorizontalAlign, "HorizontalAlign"); Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count"); Assert.AreEqual (7, t.StateBag.Count, "ViewState.Count"); t.BackImageUrl = String.Empty; // doesn't accept null, see specific test Assert.AreEqual (String.Empty, t.BackImageUrl, "-BackImageUrl"); t.Caption = null; // removed Assert.AreEqual (String.Empty, t.Caption, "-Caption"); t.CaptionAlign = TableCaptionAlign.NotSet; Assert.AreEqual (TableCaptionAlign.NotSet, t.CaptionAlign, "-CaptionAlign"); t.CellPadding = -1; Assert.AreEqual (-1, t.CellPadding, "-CellPadding"); t.CellSpacing = -1; Assert.AreEqual (-1, t.CellSpacing, "-CellSpacing"); t.GridLines = GridLines.None; Assert.AreEqual (GridLines.None, t.GridLines, "-GridLines"); t.HorizontalAlign = HorizontalAlign.NotSet; Assert.AreEqual (HorizontalAlign.NotSet, t.HorizontalAlign, "-HorizontalAlign"); Assert.AreEqual (0, t.Attributes.Count, "Attributes.Count-1"); Assert.AreEqual (6, t.StateBag.Count, "ViewState.Count-1"); } [Test] // LAMESPEC: undocumented (all others property I've seen takes null as the default value) [ExpectedException (typeof (ArgumentNullException))] public void BackImageUrl_Null () { Table t = new Table (); t.BackImageUrl = null; } [Test] [ExpectedException (typeof (ArgumentOutOfRangeException))] public void CaptionAlign_Invalid () { Table t = new Table (); t.CaptionAlign = (TableCaptionAlign)Int32.MinValue; } [Test] // LAMESPEC: undocumented exception but similar to Image [ExpectedException (typeof (ArgumentOutOfRangeException))] public void GridLines_Invalid () { Table t = new Table (); t.GridLines = (GridLines)Int32.MinValue; } [Test] // LAMESPEC: undocumented exception but similar to Image [ExpectedException (typeof (ArgumentOutOfRangeException))] public void HorizontalAlign_Invalid () { Table t = new Table (); t.HorizontalAlign = (HorizontalAlign)Int32.MinValue; } [Test] public void BorderWidth () { Table t = new Table (); Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.None"); t.GridLines = GridLines.Horizontal; Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Horizontal"); t.GridLines = GridLines.Vertical; Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Vertical"); t.GridLines = GridLines.Both; Assert.AreEqual (0, t.BorderWidth.Value, "GridLines.Both"); // note: but border="1" when rendered } [Test] public void Render () { TestTable t = new TestTable (); string s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s, "empty/default"); #else Assert.AreEqual ("\n\n
", s, "empty/default"); #endif t.CellPadding = 1; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s, "CellPadding"); #else Assert.AreEqual ("\n\n
", s, "CellPadding"); #endif t.CellPadding = -1; t.CellSpacing = 2; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s, "CellSpacing"); #else Assert.AreEqual ("\n\n
", s, "CellSpacing"); #endif t.CellSpacing = -1; t.GridLines = GridLines.Horizontal; s = t.Render (); Assert.AreEqual ("\n\n
", s, "GridLines.Horizontal"); t.GridLines = GridLines.Vertical; s = t.Render (); Assert.AreEqual ("\n\n
", s, "GridLines.Vertical"); t.GridLines = GridLines.Both; s = t.Render (); Assert.AreEqual ("\n\n
", s, "GridLines.Both"); t.GridLines = GridLines.None; t.BorderWidth = new Unit (2); s = t.Render (); #if NET_4_0 Assert.IsTrue ((s.IndexOf ("\"border-width:2px;border-style:solid;\"") > 0), "border=0/2"); #else Assert.IsTrue ((s.IndexOf ("border=\"0\"") > 0), "border=0/2"); #endif t.GridLines = GridLines.Horizontal; s = t.Render (); #if NET_4_0 Console.WriteLine (s); Assert.IsTrue ((s.IndexOf ("rules=\"rows\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Horizontal"); #else Assert.IsTrue ((s.IndexOf ("rules=\"rows\" border=\"2\"") > 0), "2/GridLines.Horizontal"); #endif t.GridLines = GridLines.Vertical; s = t.Render (); #if NET_4_0 Assert.IsTrue ((s.IndexOf ("rules=\"cols\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Vertical"); #else Assert.IsTrue ((s.IndexOf ("rules=\"cols\" border=\"2\"") > 0), "2/GridLines.Vertical"); #endif t.GridLines = GridLines.Both; s = t.Render (); #if NET_4_0 Assert.IsTrue ((s.IndexOf ("rules=\"all\" style=\"border-width:2px;border-style:solid;\"") > 0), "2/GridLines.Both"); #else Assert.IsTrue ((s.IndexOf ("rules=\"all\" border=\"2\"") > 0), "2/GridLines.Both"); #endif t.GridLines = GridLines.None; t.BorderWidth = new Unit (); t.HorizontalAlign = HorizontalAlign.Left; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s.ToLower (), "HorizontalAlign.Left"); #else Assert.AreEqual ("\n\n
", s.ToLower (), "HorizontalAlign.Left"); #endif t.HorizontalAlign = HorizontalAlign.Center; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s.ToLower (), "HorizontalAlign.Center"); #else Assert.AreEqual ("\n\n
", s.ToLower (), "HorizontalAlign.Center"); #endif t.HorizontalAlign = HorizontalAlign.Right; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s.ToLower (), "HorizontalAlign.Right"); #else Assert.AreEqual ("\n\n
", s.ToLower (), "HorizontalAlign.Right"); #endif t.HorizontalAlign = HorizontalAlign.Justify; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s.ToLower (), "HorizontalAlign.Justify"); #else Assert.AreEqual ("\n\n
", s.ToLower (), "HorizontalAlign.Justify"); #endif t.HorizontalAlign = HorizontalAlign.NotSet; t.Caption = "mono"; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption"); #else Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption"); #endif t.CaptionAlign = TableCaptionAlign.Top; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption/Top"); #else Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption/Top"); #endif t.CaptionAlign = TableCaptionAlign.Bottom; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption/Bottom"); #else Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption/Bottom"); #endif t.CaptionAlign = TableCaptionAlign.Right; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption/Right"); #else Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption/Right"); #endif t.CaptionAlign = TableCaptionAlign.Left; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption/Left"); #else Assert.AreEqual ("\n\t\n
\n\t\tmono\n\t
", s.ToLower (), "Caption/Left"); #endif t.Caption = null; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s, "CaptionAlign without Caption"); #else Assert.AreEqual ("\n\n
", s, "CaptionAlign without Caption"); #endif t.CaptionAlign = TableCaptionAlign.NotSet; t.BackImageUrl = imageUrl; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s, "BackImageUrl"); #else Assert.AreEqual ("\n\n
", s, "BackImageUrl"); #endif t.BackImageUrl = localImageUrl; s = t.Render (); #if NET_4_0 Assert.AreEqual ("\n\n
", s, "BackImageUrl"); #else Assert.AreEqual ("\n\n
", s, "BackImageUrl"); #endif t.BackImageUrl = String.Empty; } #if NET_2_0 [Test] [Category ("NunitWeb")] public void RenderInAspxPage () { WebTest t = new WebTest (PageInvoker.CreateOnLoad (RenderInAspxPage_OnLoad)); string res = t.Run (); #if NET_4_0 Assert.IsTrue (res.IndexOf ("\n\t\n\n\t\n
"), Adjust (s), "tr-1"); #else Assert.AreEqual (Adjust ("\n\t\n\n\t\n
"), Adjust (s), "tr-1"); #endif // change instance properties tr.HorizontalAlign = HorizontalAlign.Justify; s = t.Render (); #if NET_4_0 Assert.AreEqual (Adjust ("\n\t\n\n\t\n
"), Adjust (s), "tr-1j"); #else Assert.AreEqual (Adjust ("\n\t\n\n\t\n
"), Adjust (s), "tr-1j"); #endif // add it again (same instance) t.Rows.Add (tr); Assert.AreEqual (1, t.Rows.Count, "t1bis"); Assert.AreEqual (1, t.Controls.Count, "c1bis"); s = t.Render (); #if NET_4_0 Assert.AreEqual (Adjust ("\n\t\n\n\t\n
"), Adjust (s), "tr-1bis"); #else Assert.AreEqual (Adjust ("\n\t\n\n\t\n
"), Adjust (s), "tr-1bis"); #endif tr.HorizontalAlign = HorizontalAlign.NotSet; tr = new TableRow (); tr.HorizontalAlign = HorizontalAlign.Justify; t.Rows.Add (tr); Assert.AreEqual (2, t.Rows.Count, "r2"); Assert.AreEqual (2, t.Controls.Count, "c2"); s = t.Render (); #if NET_4_0 Assert.AreEqual (Adjust ("\n\t\n\n\t\n\n\t\n
"), Adjust (s), "tr-2"); #else Assert.AreEqual (Adjust ("\n\t\n\n\t\n\n\t\n
"), Adjust (s), "tr-2"); #endif tr = new TableRow (); tr.VerticalAlign = VerticalAlign.Bottom; t.Controls.Add (tr); Assert.AreEqual (3, t.Rows.Count, "r3"); Assert.AreEqual (3, t.Controls.Count, "c3"); s = t.Render (); #if NET_4_0 Assert.AreEqual (Adjust ("\n\t\n\n\t\n\n\t\n\n\t\n
"), Adjust (s), "tr-3"); #else Assert.AreEqual (Adjust ("\n\t\n\n\t\n\n\t\n\n\t\n
"), Adjust (s), "tr-3"); #endif t.Caption = "caption"; s = t.Render (); #if NET_4_0 Assert.AreEqual (Adjust ("\n\t\n\n\t\n\n\t\n\n\t\n
\n\t\tcaption\n\t
"), Adjust (s), "tr-2c"); #else Assert.AreEqual (Adjust ("\n\t\n\n\t\n\n\t\n\n\t\n
\n\t\tcaption\n\t
"), Adjust (s), "tr-2c"); #endif } [Test] [ExpectedException (typeof (NullReferenceException))] public void ControlsAdd_Null () { new Table ().Controls.Add (null); } [Test] [ExpectedException (typeof (ArgumentException))] public void ControlsAdd_LiteralControl () { new Table ().Controls.Add (new LiteralControl ("mono")); } [Test] public void ControlsAdd_TableRow () { Table t = new Table (); t.Controls.Add (new TableRow ()); Assert.AreEqual (1, t.Controls.Count, "Controls"); Assert.AreEqual (1, t.Rows.Count, "Rows"); } [Test] public void ControlsAdd_TestTableRow () { Table t = new Table (); t.Controls.Add (new TestTableRow ()); Assert.AreEqual (1, t.Controls.Count, "Controls"); Assert.AreEqual (1, t.Rows.Count, "Rows"); } [Test] [ExpectedException (typeof (NullReferenceException))] public void ControlsAddAt_Null () { new Table ().Controls.AddAt (0, null); } [Test] [ExpectedException (typeof (ArgumentOutOfRangeException))] public void ControlsAddAt_Negative () { new Table ().Controls.AddAt (Int32.MinValue, new TableRow ()); } [Test] [ExpectedException (typeof (ArgumentException))] public void ControlsAddAt_LiteralControl () { new Table ().Controls.AddAt (0, new LiteralControl ("mono")); } [Test] public void ControlsAddAt_TableRow () { Table t = new Table (); t.Controls.AddAt (0, new TableRow ()); Assert.AreEqual (1, t.Controls.Count, "Controls"); Assert.AreEqual (1, t.Rows.Count, "Rows"); } [Test] public void ControlsAddAt_TestTableRow () { Table t = new Table (); t.Controls.AddAt (0, new TestTableRow ()); Assert.AreEqual (1, t.Controls.Count, "Controls"); Assert.AreEqual (1, t.Rows.Count, "Rows"); } [Test] [ExpectedException (typeof (NullReferenceException))] public void RenderBeginTag_Null () { Table t = new Table (); t.RenderBeginTag (null); } [Test] public void RenderBeginTag_Empty () { HtmlTextWriter writer = GetWriter (); Table t = new Table (); t.RenderBeginTag (writer); string s = writer.InnerWriter.ToString (); #if NET_4_0 Assert.AreEqual ("\n", s, "empty"); #else Assert.AreEqual ("
\n", s, "empty"); #endif } [Test] public void RenderBeginTag_Attributes () { HtmlTextWriter writer = GetWriter (); Table t = new Table (); t.CellPadding = 1; t.RenderBeginTag (writer); string s = writer.InnerWriter.ToString (); #if NET_4_0 Assert.AreEqual ("
\n", s, "CellPadding"); #else Assert.AreEqual ("
\n", s, "CellPadding"); #endif } [Test] public void RenderBeginTag_Caption () { HtmlTextWriter writer = GetWriter (); Table t = new Table (); t.Caption = "caption"; t.RenderBeginTag (writer); string s = writer.InnerWriter.ToString (); #if NET_4_0 Assert.AreEqual ("
\n\t", s, "caption"); #else Assert.AreEqual ("
\n\t\tcaption\n\t
\n\t", s, "caption"); #endif } [Test] public void RenderBeginTag_Caption_Align () { HtmlTextWriter writer = GetWriter (); Table t = new Table (); t.Caption = "caption"; t.CaptionAlign = TableCaptionAlign.Top; t.RenderBeginTag (writer); string s = writer.InnerWriter.ToString (); #if NET_4_0 Assert.AreEqual ("
\n\t\tcaption\n\t
\n\t", s.ToLower (), "caption"); #else Assert.AreEqual ("
\n\t\tcaption\n\t
\n\t", s.ToLower (), "caption"); #endif } [Test] public void RenderBeginTag_Row () { HtmlTextWriter writer = GetWriter (); Table t = new Table (); t.Rows.Add (new TableRow ()); t.RenderBeginTag (writer); string s = writer.InnerWriter.ToString (); #if NET_4_0 Assert.AreEqual ("
\n\t\tcaption\n\t
\n", s, "tr"); #else Assert.AreEqual ("
\n", s, "tr"); #endif } #if NET_2_0 [Test] [Category("NunitWeb")] // Note: No event fired , only flow been checked. public void RaisePostBackEvent () { WebTest t = new WebTest (PageInvoker.CreateOnLoad (RaisePostBackEvent__Init)); string str = t.Run (); FormRequest fr = new FormRequest (t.Response, "form1"); fr.Controls.Add ("__EVENTTARGET"); fr.Controls.Add ("__EVENTARGUMENT"); fr.Controls["__EVENTTARGET"].Value = "Table"; fr.Controls["__EVENTARGUMENT"].Value = ""; t.Request = fr; t.Run (); Assert.AreEqual ("RaisePostBackEvent", (String) t.UserData, "RaisePostBackEvent"); } public static void RaisePostBackEvent__Init (Page page) { TestTable t = new TestTable (); t.ID = "Table"; page.Form.Controls.Add (t); } [TestFixtureTearDown] public void TearDown () { WebTest.Unload (); } #endif } }