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