2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.Adapters / PageAdapterTest.cs
1 //
2 // Tests for System.Web.UI.Adapters.PageAdapter
3 //
4 // Author:
5 //      Dean Brettle (dean@brettle.com)
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
29 #if NET_2_0
30 using NUnit.Framework;
31 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Drawing;
35 using System.IO;
36 using System.Globalization;
37 using System.Web;
38 using System.Web.UI;
39 using System.Web.UI.WebControls;
40 using System.Web.UI.Adapters;
41 using System.Web.Configuration;
42 using MonoTests.SystemWeb.Framework;
43
44
45 namespace MonoTests.System.Web.UI.Adapters
46 {
47         [TestFixture]
48         public class PageAdapterTest
49         {
50                 private MyPageAdapter mpa;
51                 private MyPage page;
52
53                 [TestFixtureSetUp]
54                 public void SetUpTest ()
55                 {
56 #if VISUAL_STUDIO
57                         WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.PageWithAdapter.aspx", "PageWithAdapter.aspx");
58 #else
59                         WebTest.CopyResource (GetType (), "PageWithAdapter.aspx", "PageWithAdapter.aspx");
60 #endif
61                 }
62                 
63                 [SetUp]
64                 public void SetUp()
65                 {
66                         page = new MyPage();
67                         mpa = new MyPageAdapter (page);
68                 }
69                 
70                 [Test]
71                 public void CacheVaryByHeaders ()
72                 {
73                         Assert.IsNull (mpa.CacheVaryByHeaders, "CacheVaryByHeaders #1");
74                 }
75                 
76                 [Test]
77                 public void CacheVaryByParams ()
78                 {
79                         Assert.IsNull (mpa.CacheVaryByParams, "CacheVaryByParams #1");
80                 }
81                 
82                 [Test]
83                 public void GetStatePersister ()
84                 {
85                         PageStatePersister persister = mpa.GetStatePersister ();
86                         Assert.AreEqual (typeof(HiddenFieldPageStatePersister), 
87                                 persister.GetType (), "GetStatePersister #1");
88                 }
89                 
90                 [Test]
91                 public void GetPostBackFormReference ()
92                 {
93                         Assert.AreEqual("document.forms['test']", mpa.GetPostBackFormReference ("test"),
94                                 "GetPostBackFormReference #1");
95                 }
96                 
97                 [Test]
98                 public void DeterminePostBackMode ()
99                 {
100                         Assert.AreEqual(page.DeterminePostBackMode (), mpa.DeterminePostBackMode (),
101                                 "DeterminePostBackMode #1");
102                 }
103
104                 [Test]
105                 public void RenderBeginHyperlink_NoEncode ()
106                 {
107                         StringWriter sw = new StringWriter ();
108                         HtmlTextWriter htw = new HtmlTextWriter (sw);
109                         mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", false, "softKeyLabel");
110                         Assert.AreEqual("<a href=\"url with &, <, and \"\">", sw.ToString(),
111                                 "RenderBeginHyperlink_NoEncode #1");
112                 }
113
114                 [Test]
115                 public void RenderBeginHyperlink_Encode ()
116                 {
117                         StringWriter sw = new StringWriter ();
118                         HtmlTextWriter htw = new HtmlTextWriter (sw);
119                         mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel");
120                         Assert.AreEqual("<a href=\"url with &amp;, &lt;, and &quot;\">", sw.ToString(),
121                                 "RenderBeginHyperlink_Encode #1");
122                 }
123
124                 [Test]
125                 public void RenderBeginHyperlink_NoEncode_AccessKey ()
126                 {
127                         StringWriter sw = new StringWriter ();
128                         HtmlTextWriter htw = new HtmlTextWriter (sw);
129                         mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", false, "softKeyLabel", "X");
130                         Assert.AreEqual("<a href=\"url with &, <, and \"\" accesskey=\"X\">",
131                                 sw.ToString(), "RenderBeginHyperlink_NoEncode_AccessKey #1");
132                 }
133
134                 [Test]
135                 public void RenderBeginHyperlink_Encode_AccessKey ()
136                 {
137                         StringWriter sw = new StringWriter ();
138                         HtmlTextWriter htw = new HtmlTextWriter (sw);
139                         mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel", "X");
140                         Assert.AreEqual("<a href=\"url with &amp;, &lt;, and &quot;\" accesskey=\"X\">",
141                                 sw.ToString(), "RenderBeginHyperlink_Encode_AccessKey #1");
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
146                 public void RenderBeginHyperlink_LongAccessKey ()
147                 {
148                         StringWriter sw = new StringWriter ();
149                         HtmlTextWriter htw = new HtmlTextWriter (sw);
150                         mpa.RenderBeginHyperlink (htw, "url with &, <, and \"", true, "softKeyLabel", "accessKey");
151                 }
152
153                 [Test]
154                 public void EndHyperlink ()
155                 {
156                         StringWriter sw = new StringWriter ();
157                         HtmlTextWriter htw = new HtmlTextWriter (sw);
158                         mpa.RenderBeginHyperlink (htw, "url", false, null);
159                         mpa.RenderEndHyperlink (htw);
160                         Assert.AreEqual("<a href=\"url\"></a>", sw.ToString(), "RenderEndHyperlink #1");
161                 }
162
163                 [Test]
164                 [Category ("NunitWeb")]
165                 public void RenderPostBackEvent ()
166                 {
167                         WebTest t = new WebTest ("PageWithAdapter.aspx");
168                         PageDelegates pd = new PageDelegates ();
169                         pd.SaveStateComplete = RenderPostBackEvent_OnSaveStateComplete;
170                         t.Invoker = new PageInvoker (pd);
171                         string html = t.Run ();
172                         File.WriteAllText("response.html", html);
173                 }
174                 
175                 public static void RenderPostBackEvent_OnSaveStateComplete (Page p)
176                 {
177                         TestPageWithAdapter pageWithAdapter = (TestPageWithAdapter) p;                  
178                         TestAdapter testAdapter = (TestAdapter)pageWithAdapter.PageAdapter;
179                         {
180                                 StringWriter sw = new StringWriter ();
181                                 HtmlTextWriter htw = new HtmlTextWriter (sw);
182                                 testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X", true);
183                                 Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&amp;__EVENTTARGET=target&amp;__EVENTARGUMENT=argument&amp;__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
184                                         sw.ToString(), "RenderPostBackEvent #1");
185                         }
186                         {
187                                 StringWriter sw = new StringWriter ();
188                                 HtmlTextWriter htw = new HtmlTextWriter (sw);
189                                 testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X", false);
190                                 Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&__EVENTTARGET=target&__EVENTARGUMENT=argument&__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
191                                         sw.ToString(), "RenderPostBackEvent #2");
192                         }
193                         {
194                                 StringWriter sw = new StringWriter ();
195                                 HtmlTextWriter htw = new HtmlTextWriter (sw);
196                                 testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text", "postUrl", "X");
197                                 Assert.AreEqual("<a href=\"postUrl?__VIEWSTATE=DAAAAA%3d%3d&amp;__EVENTTARGET=target&amp;__EVENTARGUMENT=argument&amp;__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\" accesskey=\"X\">text</a>",
198                                         sw.ToString(), "RenderPostBackEvent #3");
199                         }
200                         {
201                                 StringWriter sw = new StringWriter ();
202                                 HtmlTextWriter htw = new HtmlTextWriter (sw);
203                                 testAdapter.RenderPostBackEvent (htw, "target", "argument", "softKeyLabel", "text");
204                                 Assert.AreEqual("<a href=\"/NunitWeb/PageWithAdapter.aspx?__VIEWSTATE=DAAAAA%3d%3d&amp;__EVENTTARGET=target&amp;__EVENTARGUMENT=argument&amp;__PREVIOUSPAGE=/NunitWeb/PageWithAdapter.aspx\">text</a>",
205                                         sw.ToString(), "RenderPostBackEvent #4");
206                         }
207
208                 }
209                 
210                 [Test]
211                 public void RadioButtons ()
212                 {
213                         ArrayList group = new ArrayList (mpa.GetRadioButtonsByGroup ("Group1"));
214                         Assert.AreEqual (0, group.Count, "RadioButtons #0");
215
216                         RadioButton g1b1 = new RadioButton ();
217                         g1b1.GroupName = "Group1";
218                         mpa.RegisterRadioButton(g1b1);
219                         RadioButton g1b2 = new RadioButton ();
220                         g1b2.GroupName = "Group1";
221                         mpa.RegisterRadioButton(g1b2);  
222                         RadioButton g2b1 = new RadioButton ();
223                         g2b1.GroupName = "Group2";
224                         mpa.RegisterRadioButton (g2b1);
225                         RadioButton noGroupB1 = new RadioButton ();
226                         mpa.RegisterRadioButton (noGroupB1);
227                         
228                         Assert.AreEqual (0, mpa.GetRadioButtonsByGroup ("Non-existent group").Count, "RadioButtons #1");
229
230                         ArrayList group1 = new ArrayList (mpa.GetRadioButtonsByGroup ("Group1"));                       
231                         Assert.AreEqual (2, group1.Count, "RadioButtons #2");
232                         Assert.IsTrue (group1.Contains (g1b1), "RadioButtons #3");
233                         Assert.IsTrue (group1.Contains (g1b2), "RadioButtons #4");
234                         
235                         ArrayList group2 = new ArrayList (mpa.GetRadioButtonsByGroup ("Group2"));                       
236                         Assert.AreEqual (1, group2.Count, "RadioButtons #5");
237                         Assert.IsTrue (group2.Contains (g2b1), "RadioButtons #6");
238                         
239                         ArrayList noGroup = new ArrayList (mpa.GetRadioButtonsByGroup (""));                    
240                         Assert.AreEqual (1, noGroup.Count, "RadioButtons #7");
241                         Assert.IsTrue (noGroup.Contains (noGroupB1), "RadioButtons #8");
242                 }
243
244                 [Test]
245                 public void ClientState ()
246                 {
247                         page.RawViewState = "test";
248                         Assert.AreEqual ("test", mpa.ClientState, "ClientState #1");
249                 }
250                 
251                 [Test]
252                 public void TransformText ()
253                 {
254                         Assert.AreEqual ("test", mpa.TransformText("test"), "TransformText #1");
255                         Assert.IsNull (mpa.TransformText(null), "TransformText #2");
256                 }
257                 
258                 [TestFixtureTearDown]
259                 public void TearDown ()
260                 {
261                         WebTest.Unload ();
262                 }
263
264                 class MyPageAdapter : PageAdapter
265                 {
266                         internal MyPageAdapter (Page p) : base (p)
267                         {
268                         }
269                         
270                         new internal string ClientState {
271                                 get { return base.ClientState; }
272                         }
273                 }
274
275                 class MyPage : Page
276                 {
277                         internal MyPage () : base ()
278                         {
279                         }
280                         
281                         NameValueCollection post_back_mode = new NameValueCollection ();
282                         
283                         override protected internal NameValueCollection DeterminePostBackMode ()
284                         {
285                                 return post_back_mode;
286                         }
287                 }
288         }
289         
290 }
291 #endif