PageAdapterTest.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // Tests for System.Web.UI.Adapters.PageAdapter
  3. //
  4. // Author:
  5. // Dean Brettle ([email protected])
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Drawing;
  34. using System.IO;
  35. using System.Globalization;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. using System.Web.UI.Adapters;
  40. using System.Web.Configuration;
  41. using MonoTests.SystemWeb.Framework;
  42. namespace MonoTests.System.Web.UI.Adapters
  43. {
  44. [TestFixture]
  45. public class PageAdapterTest
  46. {
  47. private MyPageAdapter mpa;
  48. private MyPage page;
  49. [TestFixtureSetUp]
  50. public void SetUpTest ()
  51. {
  52. #if VISUAL_STUDIO
  53. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.PageWithAdapter.aspx", "PageWithAdapter.aspx");
  54. #else
  55. WebTest.CopyResource (GetType (), "PageWithAdapter.aspx", "PageWithAdapter.aspx");
  56. #endif
  57. }
  58. [SetUp]
  59. public void SetUp()
  60. {
  61. page = new MyPage();
  62. mpa = new MyPageAdapter (page);
  63. }
  64. [Test]
  65. public void CacheVaryByHeaders ()
  66. {
  67. Assert.IsNull (mpa.CacheVaryByHeaders, "CacheVaryByHeaders #1");
  68. }
  69. [Test]
  70. public void CacheVaryByParams ()
  71. {
  72. Assert.IsNull (mpa.CacheVaryByParams, "CacheVaryByParams #1");
  73. }
  74. [Test]
  75. public void GetStatePersister ()
  76. {
  77. PageStatePersister persister = mpa.GetStatePersister ();
  78. Assert.AreEqual (typeof(HiddenFieldPageStatePersister),
  79. persister.GetType (), "GetStatePersister #1");
  80. }
  81. [Test]
  82. public void GetPostBackFormReference ()
  83. {
  84. Assert.AreEqual("document.forms['test']", mpa.GetPostBackFormReference ("test"),
  85. "GetPostBackFormReference #1");
  86. }
  87. [Test]
  88. public void DeterminePostBackMode ()
  89. {
  90. Assert.AreEqual(page.DeterminePostBackMode (), mpa.DeterminePostBackMode (),
  91. "DeterminePostBackMode #1");
  92. }
  93. [Test]
  94. public void RenderBeginHyperlink_NoEncode ()
  95. {
  96. StringWriter sw = new StringWriter ();
  97. HtmlTextWriter htw = new HtmlTextWriter (sw);
  98. mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", false, "softKeyLabel");
  99. Assert.AreEqual("<a href=\"url with &, <, and \"\">", sw.ToString(),
  100. "RenderBeginHyperlink_NoEncode #1");
  101. }
  102. [Test]
  103. public void RenderBeginHyperlink_Encode ()
  104. {
  105. StringWriter sw = new StringWriter ();
  106. HtmlTextWriter htw = new HtmlTextWriter (sw);
  107. mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel");
  108. Assert.AreEqual("<a href=\"url with &amp;, &lt;, and &quot;\">", sw.ToString(),
  109. "RenderBeginHyperlink_Encode #1");
  110. }
  111. [Test]
  112. public void RenderBeginHyperlink_NoEncode_AccessKey ()
  113. {
  114. StringWriter sw = new StringWriter ();
  115. HtmlTextWriter htw = new HtmlTextWriter (sw);
  116. mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", false, "softKeyLabel", "X");
  117. Assert.AreEqual("<a href=\"url with &, <, and \"\" accesskey=\"X\">",
  118. sw.ToString(), "RenderBeginHyperlink_NoEncode_AccessKey #1");
  119. }
  120. [Test]
  121. public void RenderBeginHyperlink_Encode_AccessKey ()
  122. {
  123. StringWriter sw = new StringWriter ();
  124. HtmlTextWriter htw = new HtmlTextWriter (sw);
  125. mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel", "X");
  126. Assert.AreEqual("<a href=\"url with &amp;, &lt;, and &quot;\" accesskey=\"X\">",
  127. sw.ToString(), "RenderBeginHyperlink_Encode_AccessKey #1");
  128. }
  129. [Test]
  130. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  131. public void RenderBeginHyperlink_LongAccessKey ()
  132. {
  133. StringWriter sw = new StringWriter ();
  134. HtmlTextWriter htw = new HtmlTextWriter (sw);
  135. mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel", "accessKey");
  136. }
  137. [Test]
  138. public void EndHyperlink ()
  139. {
  140. StringWriter sw = new StringWriter ();
  141. HtmlTextWriter htw = new HtmlTextWriter (sw);
  142. mpa.RenderBeginHyperlink (htw, "url", false, null);
  143. mpa.RenderEndHyperlink (htw);
  144. Assert.AreEqual("<a href=\"url\"></a>", sw.ToString(), "RenderEndHyperlink #1");
  145. }
  146. [Test]
  147. [Category ("NunitWeb")]
  148. public void RenderPostBackEvent ()
  149. {
  150. WebTest t = new WebTest ("PageWithAdapter.aspx");
  151. PageDelegates pd = new PageDelegates ();
  152. pd.SaveStateComplete = RenderPostBackEvent_OnSaveStateComplete;
  153. t.Invoker = new PageInvoker (pd);
  154. string html = t.Run ();
  155. File.WriteAllText("response.html", html);
  156. }
  157. public static void RenderPostBackEvent_OnSaveStateComplete (Page p)
  158. {
  159. TestPageWithAdapter pageWithAdapter = (TestPageWithAdapter) p;
  160. TestAdapter testAdapter = (TestAdapter)pageWithAdapter.PageAdapter;
  161. {
  162. StringWriter sw = new StringWriter ();
  163. HtmlTextWriter htw = new HtmlTextWriter (sw);
  164. testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X", true);
  165. Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&amp;__EVENTTARGET=target&amp;__EVENTARGUMENT=argument&amp;__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
  166. sw.ToString(), "RenderPostBackEvent #1");
  167. }
  168. {
  169. StringWriter sw = new StringWriter ();
  170. HtmlTextWriter htw = new HtmlTextWriter (sw);
  171. testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X", false);
  172. Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&__EVENTTARGET=target&__EVENTARGUMENT=argument&__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
  173. sw.ToString(), "RenderPostBackEvent #2");
  174. }
  175. {
  176. StringWriter sw = new StringWriter ();
  177. HtmlTextWriter htw = new HtmlTextWriter (sw);
  178. testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X");
  179. Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&amp;__EVENTTARGET=target&amp;__EVENTARGUMENT=argument&amp;__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
  180. sw.ToString(), "RenderPostBackEvent #3");
  181. }
  182. {
  183. StringWriter sw = new StringWriter ();
  184. HtmlTextWriter htw = new HtmlTextWriter (sw);
  185. testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text");
  186. Assert.AreEqual("<a href=\"/NunitWeb/PageWithAdapter.aspx?__VIEWSTATE=DAAAAA%3d%3d&amp;__EVENTTARGET=target&amp;__EVENTARGUMENT=argument&amp;__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\">text</a>",
  187. sw.ToString(), "RenderPostBackEvent #4");
  188. }
  189. }
  190. [Test]
  191. public void RadioButtons ()
  192. {
  193. ArrayList group = new ArrayList (mpa.GetRadioButtonsByGroup ("Group1"));
  194. Assert.AreEqual (0, group.Count, "RadioButtons #0");
  195. RadioButton g1b1 = new RadioButton ();
  196. g1b1.GroupName = "Group1";
  197. mpa.RegisterRadioButton(g1b1);
  198. RadioButton g1b2 = new RadioButton ();
  199. g1b2.GroupName = "Group1";
  200. mpa.RegisterRadioButton(g1b2);
  201. RadioButton g2b1 = new RadioButton ();
  202. g2b1.GroupName = "Group2";
  203. mpa.RegisterRadioButton (g2b1);
  204. RadioButton noGroupB1 = new RadioButton ();
  205. mpa.RegisterRadioButton (noGroupB1);
  206. Assert.AreEqual (0, mpa.GetRadioButtonsByGroup ("Non-existent group").Count, "RadioButtons #1");
  207. ArrayList group1 = new ArrayList (mpa.GetRadioButtonsByGroup ("Group1"));
  208. Assert.AreEqual (2, group1.Count, "RadioButtons #2");
  209. Assert.IsTrue (group1.Contains (g1b1), "RadioButtons #3");
  210. Assert.IsTrue (group1.Contains (g1b2), "RadioButtons #4");
  211. ArrayList group2 = new ArrayList (mpa.GetRadioButtonsByGroup ("Group2"));
  212. Assert.AreEqual (1, group2.Count, "RadioButtons #5");
  213. Assert.IsTrue (group2.Contains (g2b1), "RadioButtons #6");
  214. ArrayList noGroup = new ArrayList (mpa.GetRadioButtonsByGroup (""));
  215. Assert.AreEqual (1, noGroup.Count, "RadioButtons #7");
  216. Assert.IsTrue (noGroup.Contains (noGroupB1), "RadioButtons #8");
  217. }
  218. [Test]
  219. public void ClientState ()
  220. {
  221. page.RawViewState = "test";
  222. Assert.AreEqual ("test", mpa.ClientState, "ClientState #1");
  223. }
  224. [Test]
  225. public void TransformText ()
  226. {
  227. Assert.AreEqual ("test", mpa.TransformText("test"), "TransformText #1");
  228. Assert.IsNull (mpa.TransformText(null), "TransformText #2");
  229. }
  230. [TestFixtureTearDown]
  231. public void TearDown ()
  232. {
  233. WebTest.Unload ();
  234. }
  235. class MyPageAdapter : PageAdapter
  236. {
  237. internal MyPageAdapter (Page p) : base (p)
  238. {
  239. }
  240. new internal string ClientState {
  241. get { return base.ClientState; }
  242. }
  243. }
  244. class MyPage : Page
  245. {
  246. internal MyPage () : base ()
  247. {
  248. }
  249. NameValueCollection post_back_mode = new NameValueCollection ();
  250. override protected internal NameValueCollection DeterminePostBackMode ()
  251. {
  252. return post_back_mode;
  253. }
  254. }
  255. }
  256. }
  257. #endif