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