| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- //
- // Tests for System.Web.UI.Adapters.PageAdapter
- //
- // Author:
- // Dean Brettle ([email protected])
- //
- // Copyright (C) 2007 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.
- //
- #if NET_2_0
- using NUnit.Framework;
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Drawing;
- using System.IO;
- using System.Globalization;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.Adapters;
- using System.Web.Configuration;
- using MonoTests.SystemWeb.Framework;
- namespace MonoTests.System.Web.UI.Adapters
- {
- [TestFixture]
- public class PageAdapterTest
- {
- private MyPageAdapter mpa;
- private MyPage page;
- [TestFixtureSetUp]
- public void SetUpTest ()
- {
- #if VISUAL_STUDIO
- WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.PageWithAdapter.aspx", "PageWithAdapter.aspx");
- #else
- WebTest.CopyResource (GetType (), "PageWithAdapter.aspx", "PageWithAdapter.aspx");
- #endif
- }
-
- [SetUp]
- public void SetUp()
- {
- page = new MyPage();
- mpa = new MyPageAdapter (page);
- }
-
- [Test]
- public void CacheVaryByHeaders ()
- {
- Assert.IsNull (mpa.CacheVaryByHeaders, "CacheVaryByHeaders #1");
- }
-
- [Test]
- public void CacheVaryByParams ()
- {
- Assert.IsNull (mpa.CacheVaryByParams, "CacheVaryByParams #1");
- }
-
- [Test]
- public void GetStatePersister ()
- {
- PageStatePersister persister = mpa.GetStatePersister ();
- Assert.AreEqual (typeof(HiddenFieldPageStatePersister),
- persister.GetType (), "GetStatePersister #1");
- }
-
- [Test]
- public void GetPostBackFormReference ()
- {
- Assert.AreEqual("document.forms['test']", mpa.GetPostBackFormReference ("test"),
- "GetPostBackFormReference #1");
- }
-
- [Test]
- public void DeterminePostBackMode ()
- {
- Assert.AreEqual(page.DeterminePostBackMode (), mpa.DeterminePostBackMode (),
- "DeterminePostBackMode #1");
- }
- [Test]
- public void RenderBeginHyperlink_NoEncode ()
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", false, "softKeyLabel");
- Assert.AreEqual("<a href=\"url with &, <, and \"\">", sw.ToString(),
- "RenderBeginHyperlink_NoEncode #1");
- }
- [Test]
- public void RenderBeginHyperlink_Encode ()
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel");
- Assert.AreEqual("<a href=\"url with &, <, and "\">", sw.ToString(),
- "RenderBeginHyperlink_Encode #1");
- }
- [Test]
- public void RenderBeginHyperlink_NoEncode_AccessKey ()
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", false, "softKeyLabel", "X");
- Assert.AreEqual("<a href=\"url with &, <, and \"\" accesskey=\"X\">",
- sw.ToString(), "RenderBeginHyperlink_NoEncode_AccessKey #1");
- }
- [Test]
- public void RenderBeginHyperlink_Encode_AccessKey ()
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel", "X");
- Assert.AreEqual("<a href=\"url with &, <, and "\" accesskey=\"X\">",
- sw.ToString(), "RenderBeginHyperlink_Encode_AccessKey #1");
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void RenderBeginHyperlink_LongAccessKey ()
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel", "accessKey");
- }
- [Test]
- public void EndHyperlink ()
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- mpa.RenderBeginHyperlink (htw, "url", false, null);
- mpa.RenderEndHyperlink (htw);
- Assert.AreEqual("<a href=\"url\"></a>", sw.ToString(), "RenderEndHyperlink #1");
- }
- [Test]
- [Category ("NunitWeb")]
- public void RenderPostBackEvent ()
- {
- WebTest t = new WebTest ("PageWithAdapter.aspx");
- PageDelegates pd = new PageDelegates ();
- pd.SaveStateComplete = RenderPostBackEvent_OnSaveStateComplete;
- t.Invoker = new PageInvoker (pd);
- string html = t.Run ();
- File.WriteAllText("response.html", html);
- }
-
- public static void RenderPostBackEvent_OnSaveStateComplete (Page p)
- {
- TestPageWithAdapter pageWithAdapter = (TestPageWithAdapter) p;
- TestAdapter testAdapter = (TestAdapter)pageWithAdapter.PageAdapter;
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X", true);
- Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&__EVENTTARGET=target&__EVENTARGUMENT=argument&__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
- sw.ToString(), "RenderPostBackEvent #1");
- }
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X", false);
- Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&__EVENTTARGET=target&__EVENTARGUMENT=argument&__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
- sw.ToString(), "RenderPostBackEvent #2");
- }
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X");
- Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&__EVENTTARGET=target&__EVENTARGUMENT=argument&__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
- sw.ToString(), "RenderPostBackEvent #3");
- }
- {
- StringWriter sw = new StringWriter ();
- HtmlTextWriter htw = new HtmlTextWriter (sw);
- testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text");
- Assert.AreEqual("<a href=\"/NunitWeb/PageWithAdapter.aspx?__VIEWSTATE=DAAAAA%3d%3d&__EVENTTARGET=target&__EVENTARGUMENT=argument&__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\">text</a>",
- sw.ToString(), "RenderPostBackEvent #4");
- }
- }
-
- [Test]
- public void RadioButtons ()
- {
- ArrayList group = new ArrayList (mpa.GetRadioButtonsByGroup ("Group1"));
- Assert.AreEqual (0, group.Count, "RadioButtons #0");
- RadioButton g1b1 = new RadioButton ();
- g1b1.GroupName = "Group1";
- mpa.RegisterRadioButton(g1b1);
- RadioButton g1b2 = new RadioButton ();
- g1b2.GroupName = "Group1";
- mpa.RegisterRadioButton(g1b2);
- RadioButton g2b1 = new RadioButton ();
- g2b1.GroupName = "Group2";
- mpa.RegisterRadioButton (g2b1);
- RadioButton noGroupB1 = new RadioButton ();
- mpa.RegisterRadioButton (noGroupB1);
-
- Assert.AreEqual (0, mpa.GetRadioButtonsByGroup ("Non-existent group").Count, "RadioButtons #1");
- ArrayList group1 = new ArrayList (mpa.GetRadioButtonsByGroup ("Group1"));
- Assert.AreEqual (2, group1.Count, "RadioButtons #2");
- Assert.IsTrue (group1.Contains (g1b1), "RadioButtons #3");
- Assert.IsTrue (group1.Contains (g1b2), "RadioButtons #4");
-
- ArrayList group2 = new ArrayList (mpa.GetRadioButtonsByGroup ("Group2"));
- Assert.AreEqual (1, group2.Count, "RadioButtons #5");
- Assert.IsTrue (group2.Contains (g2b1), "RadioButtons #6");
-
- ArrayList noGroup = new ArrayList (mpa.GetRadioButtonsByGroup (""));
- Assert.AreEqual (1, noGroup.Count, "RadioButtons #7");
- Assert.IsTrue (noGroup.Contains (noGroupB1), "RadioButtons #8");
- }
- [Test]
- public void ClientState ()
- {
- page.RawViewState = "test";
- Assert.AreEqual ("test", mpa.ClientState, "ClientState #1");
- }
-
- [Test]
- public void TransformText ()
- {
- Assert.AreEqual ("test", mpa.TransformText("test"), "TransformText #1");
- Assert.IsNull (mpa.TransformText(null), "TransformText #2");
- }
-
- [TestFixtureTearDown]
- public void TearDown ()
- {
- WebTest.Unload ();
- }
- class MyPageAdapter : PageAdapter
- {
- internal MyPageAdapter (Page p) : base (p)
- {
- }
-
- new internal string ClientState {
- get { return base.ClientState; }
- }
- }
- class MyPage : Page
- {
- internal MyPage () : base ()
- {
- }
-
- NameValueCollection post_back_mode = new NameValueCollection ();
-
- override protected internal NameValueCollection DeterminePostBackMode ()
- {
- return post_back_mode;
- }
- }
- }
-
- }
- #endif
|