[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / LoginStatusTest.cs
1 //
2 // LoginStatusTest.cs   - Unit tests for System.Web.UI.WebControls.LoginStatus
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 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
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.Security.Principal;
34 using System.Web;
35 using System.Web.UI;
36 using System.Web.UI.HtmlControls;
37 using System.Web.UI.WebControls;
38
39 using NUnit.Framework;
40
41 namespace MonoTests.System.Web.UI.WebControls {
42
43         public class TestLoginStatus : LoginStatus {
44
45                 private bool cancel;
46                 private bool onLoggedOut;
47                 private bool onLoggingOut;
48                 private bool onPreRender;
49
50                 public string Tag {
51                         get { return base.TagName; }
52                 }
53
54                 public StateBag StateBag {
55                         get { return base.ViewState; }
56                 }
57
58                 private HtmlTextWriter GetWriter ()
59                 {
60                         StringWriter sw = new StringWriter ();
61                         sw.NewLine = "\n";
62                         return new HtmlTextWriter (sw);
63                 }
64
65                 public string Render ()
66                 {
67                         HtmlTextWriter writer = GetWriter ();
68                         Render (writer);
69                         return writer.InnerWriter.ToString ();
70                 }
71
72                 public string RenderContents ()
73                 {
74                         HtmlTextWriter writer = GetWriter ();
75                         base.RenderContents (writer);
76                         return writer.InnerWriter.ToString ();
77                 }
78
79                 public void SetDesignMode (IDictionary dic)
80                 {
81                         base.SetDesignModeState (dic);
82                 }
83
84                 public bool Cancel {
85                         get { return cancel; }
86                         set { cancel = value; }
87                 }
88
89                 public bool OnLoggedOutCalled {
90                         get { return onLoggedOut; }
91                         set { onLoggedOut = value; }
92                 }
93
94                 protected override void OnLoggedOut (EventArgs e)
95                 {
96                         onLoggedOut = true;
97                         base.OnLoggedOut (e);
98                 }
99
100                 public void DoLoggedOut (EventArgs e)
101                 {
102                         base.OnLoggedOut (e);
103                 }
104
105                 public bool OnLoggingOutCalled {
106                         get { return onLoggingOut; }
107                         set { onLoggingOut = value; }
108                 }
109
110                 protected override void OnLoggingOut (LoginCancelEventArgs e)
111                 {
112                         onLoggingOut = true;
113                         e.Cancel = cancel;
114                         base.OnLoggingOut (e);
115                 }
116
117                 public void DoLoggingIn (LoginCancelEventArgs e)
118                 {
119                         base.OnLoggingOut (e);
120                 }
121
122                 public bool OnPreRenderCalled {
123                         get { return onPreRender; }
124                         set { onPreRender = value; }
125                 }
126
127                 protected internal override void OnPreRender (EventArgs e)
128                 {
129                         onPreRender = true;
130                         base.OnPreRender (e);
131                 }
132         }
133
134         [TestFixture]
135         public class LoginStatusTest {
136
137                 private IPrincipal GetPrincipal (string name)
138                 {
139                         return new GenericPrincipal (new GenericIdentity (name), null);
140                 }
141
142                 private IPrincipal GetUnauthenticatedPrincipal (string name)
143                 {
144                         return new GenericPrincipal (new UnauthenticatedIdentity (name), null);
145                 }
146
147                 [Test]
148                 public void DefaultProperties ()
149                 {
150                         TestLoginStatus ls = new TestLoginStatus ();
151                         Assert.AreEqual (0, ls.Attributes.Count, "Attributes.Count");
152                         Assert.AreEqual (0, ls.StateBag.Count, "ViewState.Count");
153
154                         Assert.AreEqual (String.Empty, ls.LoginImageUrl, "LoginImageUrl");
155                         Assert.AreEqual ("Login", ls.LoginText, "LoginText");
156                         Assert.AreEqual (LogoutAction.Refresh, ls.LogoutAction, "LogoutAction");
157                         Assert.AreEqual (String.Empty, ls.LogoutImageUrl, "LogoutImageUrl");
158                         Assert.AreEqual (String.Empty, ls.LogoutPageUrl, "LogoutPageUrl");
159                         Assert.AreEqual ("Logout", ls.LogoutText, "LogoutText");
160
161                         Assert.AreEqual ("a", ls.Tag, "TagName");
162                         Assert.AreEqual (0, ls.Attributes.Count, "Attributes.Count-2");
163                         Assert.AreEqual (0, ls.StateBag.Count, "ViewState.Count-2");
164                 }
165
166                 [Test]
167                 public void SetOriginalProperties ()
168                 {
169                         TestLoginStatus ls = new TestLoginStatus ();
170
171                         ls.LoginImageUrl = String.Empty;
172                         Assert.AreEqual (String.Empty, ls.LoginImageUrl, "LoginImageUrl");
173                         ls.LoginText = "Login";
174                         Assert.AreEqual ("Login", ls.LoginText, "LoginText");
175                         ls.LogoutAction = LogoutAction.Refresh;
176                         Assert.AreEqual (LogoutAction.Refresh, ls.LogoutAction, "LogoutAction");
177                         ls.LogoutImageUrl = String.Empty;
178                         Assert.AreEqual (String.Empty, ls.LogoutImageUrl, "LogoutImageUrl");
179                         ls.LogoutPageUrl = String.Empty;
180                         Assert.AreEqual (String.Empty, ls.LogoutPageUrl, "LogoutPageUrl");
181                         ls.LogoutText = "Logout";
182                         Assert.AreEqual ("Logout", ls.LogoutText, "LogoutText");
183
184                         Assert.AreEqual ("a", ls.Tag, "TagName");
185                         Assert.AreEqual (6, ls.StateBag.Count, "ViewState.Count-1");
186                 }
187
188                 [Test]
189                 public void CleanProperties ()
190                 {
191                         TestLoginStatus ls = new TestLoginStatus ();
192                         ls.LoginImageUrl = String.Empty;
193                         ls.LoginText = "Login";
194                         ls.LogoutAction = LogoutAction.Refresh;
195                         ls.LogoutImageUrl = String.Empty;
196                         ls.LogoutPageUrl = String.Empty;
197                         ls.LogoutText = "Logout";
198                         Assert.AreEqual (6, ls.StateBag.Count, "ViewState.Count-1");
199
200                         ls.LoginImageUrl = null;
201                         Assert.AreEqual (String.Empty, ls.LoginImageUrl, "LoginImageUrl");
202                         ls.LoginText = null;
203                         Assert.AreEqual ("Login", ls.LoginText, "LoginText");
204                         ls.LogoutImageUrl = null;
205                         Assert.AreEqual (String.Empty, ls.LogoutImageUrl, "LogoutImageUrl");
206                         ls.LogoutPageUrl = null;
207                         Assert.AreEqual (String.Empty, ls.LogoutPageUrl, "LogoutPageUrl");
208                         ls.LogoutText = null;
209                         Assert.AreEqual ("Logout", ls.LogoutText, "LogoutText");
210
211                         Assert.AreEqual ("a", ls.Tag, "TagName");
212                         Assert.AreEqual (1, ls.StateBag.Count, "ViewState.Count-2");
213                 }
214
215                 [Test]
216                 public void Tag ()
217                 {
218                         TestLoginStatus ls = new TestLoginStatus ();
219                         Assert.AreEqual ("a", ls.Tag, "TagName");
220
221                         ls.LoginImageUrl = "http://www.go-mono.com";
222                         ls.LogoutImageUrl = "http://www.mono-project.com";
223                         Assert.AreEqual ("a", ls.Tag, "TagName");
224                 }
225
226                 [Test]
227                 public void Controls ()
228                 {
229                         TestLoginStatus ls = new TestLoginStatus ();
230                         Assert.AreEqual (4, ls.Controls.Count, "Count");
231
232                         Assert.IsTrue (ls.Controls[0] is LinkButton, "Type-0");
233                         Assert.IsTrue (ls.Controls[1] is ImageButton, "Type-1");
234                         Assert.IsTrue (ls.Controls[2] is LinkButton, "Type-2");
235                         Assert.IsTrue (ls.Controls[3] is ImageButton, "Type-3");
236
237                         Assert.IsTrue (ls.Controls[0].Visible, "Visible-0");
238                         Assert.IsTrue (ls.Controls[1].Visible, "Visible-1");
239                         Assert.IsTrue (ls.Controls[2].Visible, "Visible-2");
240                         Assert.IsTrue (ls.Controls[3].Visible, "Visible-3");
241
242                         Assert.IsNotNull (ls.Controls[0].UniqueID, "UniqueID-0");
243                         Assert.IsNotNull (ls.Controls[1].UniqueID, "UniqueID-1");
244                         Assert.IsNotNull (ls.Controls[2].UniqueID, "UniqueID-2");
245                         Assert.IsNotNull (ls.Controls[3].UniqueID, "UniqueID-3");
246
247                         Assert.IsNull (ls.Controls[0].ID, "ID-0");
248                         Assert.IsNull (ls.Controls[1].ID, "ID-1");
249                         Assert.IsNull (ls.Controls[2].ID, "ID-2");
250                         Assert.IsNull (ls.Controls[3].ID, "ID-3");
251                         
252                         ls.CssClass = "loginClass";
253                         ls.LoginText = "LoginText";
254                         ls.LoginImageUrl = "LoginImageUrl";
255                         ls.LogoutText = "LogoutText";
256                         ls.LogoutImageUrl = "LogoutImageUrl";
257
258                         Assert.AreEqual (String.Empty, (ls.Controls[0] as LinkButton).Text, "Text-0");
259                         Assert.AreEqual (String.Empty, (ls.Controls[1] as ImageButton).ImageUrl, "ImageUrl-1");
260                         Assert.AreEqual (String.Empty, (ls.Controls[2] as LinkButton).Text, "Text-2");
261                         Assert.AreEqual (String.Empty, (ls.Controls[3] as ImageButton).ImageUrl, "ImageUrl-3");
262                         
263                         ls.Render ();
264                         Assert.IsFalse (ls.OnPreRenderCalled, "!OnPreRender");
265
266                         Assert.IsFalse (ls.Controls[0].Visible, "Render-Visible-0");
267                         Assert.IsFalse (ls.Controls[1].Visible, "Render-Visible-1");
268                         Assert.IsFalse (ls.Controls[2].Visible, "Render-Visible-2");
269                         Assert.IsTrue (ls.Controls[3].Visible, "Render-Visible-3");
270
271                         Assert.AreEqual (String.Empty, (ls.Controls[0] as LinkButton).Text, "Render-Text-0");
272                         Assert.AreEqual (String.Empty, (ls.Controls[1] as ImageButton).ImageUrl, "Render-ImageUrl-1");
273                         Assert.AreEqual (String.Empty, (ls.Controls[2] as LinkButton).Text, "Render-Text-2");
274                         Assert.AreEqual ("LoginImageUrl", (ls.Controls[3] as ImageButton).ImageUrl, "Render-ImageUrl-3");
275                         Assert.AreEqual ("loginClass", (ls.Controls[3] as ImageButton).CssClass, "Render-ImageUrl-4");
276                 }
277
278                 [Test]
279                 public void Render_NoPage ()
280                 {
281                         TestLoginStatus ls = new TestLoginStatus ();
282                         Assert.AreEqual ("<a>Login</a>", ls.Render (), "Render");
283                         Assert.AreEqual ("<a>Login</a>", ls.RenderContents (), "RenderContents");
284                         ls.LoginText = "Log In";
285                         Assert.AreEqual ("<a>Log In</a>", ls.Render (), "Render-2");
286                         Assert.AreEqual ("<a>Log In</a>", ls.RenderContents (), "RenderContents-2");
287                         ls.LoginImageUrl = "http://www.go-mono-com";
288
289                         string s = ls.Render ();
290                         Assert.IsTrue (s.StartsWith ("<input "), "Render-3a");
291                         Assert.IsTrue (s.IndexOf (" type=\"image\" ") > 0, "Render-3b");
292                         Assert.IsTrue (s.IndexOf (" name=\"" + ls.Controls [3].UniqueID + "\" ") > 0, "Render-3c");
293                         Assert.IsTrue (s.IndexOf (" src=\"http://www.go-mono-com\" ") > 0, "Render-3d");
294                         Assert.IsTrue (s.IndexOf (" alt=\"Log In\" ") > 0, "Render-3e");
295                         Assert.IsTrue (s.EndsWith (" />"), "Render-3z");
296                         Assert.AreEqual (s, ls.RenderContents (), "RenderContents-3");
297                         // rendering <input> but we're still report an A as tag
298                         Assert.AreEqual ("a", ls.Tag, "TagName");
299                 }
300
301                 [Test]
302                 [ExpectedException (typeof (HttpException))]
303                 public void Render_User_WithoutForm ()
304                 {
305                         ContextPage page = new ContextPage (GetPrincipal ("me"));
306                         TestLoginStatus ls = new TestLoginStatus ();
307                         ls.Page = page;
308                         ls.Render ();
309                         // must be in a server form
310                 }
311
312                 // all other rendering fails because I can't setup a proper environment
313                 // mostly because overriding Context doesn't make Page.Request to work :-(
314         }
315 }
316
317