LoginTest.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. //
  2. // LoginTest.cs - Unit tests for System.Web.UI.WebControls.Login
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  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. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Web;
  33. using System.Web.UI;
  34. using System.Web.UI.WebControls;
  35. using NUnit.Framework;
  36. using MonoTests.SystemWeb.Framework;
  37. using MonoTests.stand_alone.WebHarness;
  38. namespace MonoTests.System.Web.UI.WebControls {
  39. public class LoginTemplate :WebControl, ITemplate {
  40. public LoginTemplate() {
  41. ID = "kuku";
  42. }
  43. void ITemplate.InstantiateIn(Control container) {
  44. container.Controls.Add(this);
  45. }
  46. }
  47. public class LayoutTemplate :WebControl, ITemplate
  48. {
  49. TextBox user;
  50. TextBox pass;
  51. CheckBox remme;
  52. Button login;
  53. Literal failure;
  54. public LayoutTemplate(){
  55. Buildcontrols();
  56. Addtocontainer();
  57. }
  58. #region build
  59. public void Buildcontrols()
  60. {
  61. user = new TextBox();
  62. pass = new TextBox();
  63. remme = new CheckBox();
  64. login = new Button();
  65. failure = new Literal();
  66. ID = "Template";
  67. user.ID = "UserName";
  68. pass.ID = "Password";
  69. remme.ID = "RememberMe";
  70. login.ID = "Login";
  71. failure.ID = "FailureText";
  72. }
  73. public void Addtocontainer()
  74. {
  75. this.Controls.Add(user);
  76. this.Controls.Add(pass);
  77. this.Controls.Add(remme);
  78. this.Controls.Add(login);
  79. this.Controls.Add(failure);
  80. }
  81. #endregion
  82. void ITemplate.InstantiateIn(Control container)
  83. {
  84. container.Controls.Add(this);
  85. }
  86. }
  87. public class TestLogin : Login {
  88. public string Tag {
  89. get { return base.TagName; }
  90. }
  91. public StateBag StateBag {
  92. get { return base.ViewState; }
  93. }
  94. public string Render ()
  95. {
  96. StringWriter sw = new StringWriter ();
  97. sw.NewLine = "\n";
  98. HtmlTextWriter writer = new HtmlTextWriter (sw);
  99. base.Render (writer);
  100. return writer.InnerWriter.ToString ();
  101. }
  102. public Style GetStyle ()
  103. {
  104. return base.CreateControlStyle ();
  105. }
  106. public void TrackState ()
  107. {
  108. TrackViewState ();
  109. }
  110. public void LoadState (object state)
  111. {
  112. LoadViewState (state);
  113. }
  114. public object SaveState ()
  115. {
  116. return SaveViewState ();
  117. }
  118. public void SetDesignMode (IDictionary dic)
  119. {
  120. base.SetDesignModeState (dic);
  121. }
  122. private bool authenticated;
  123. private bool cancel;
  124. private bool onAuthenticate;
  125. private bool onBubble;
  126. private bool onLoggedIn;
  127. private bool onLoggingIn;
  128. private bool onLoginError;
  129. public bool Authenticated {
  130. get { return authenticated; }
  131. set { authenticated = value; }
  132. }
  133. public bool Cancel {
  134. get { return cancel; }
  135. set { cancel = value; }
  136. }
  137. public bool OnAuthenticateCalled {
  138. get { return onAuthenticate; }
  139. set { onAuthenticate = value; }
  140. }
  141. protected override void OnAuthenticate (AuthenticateEventArgs e)
  142. {
  143. onAuthenticate = true;
  144. e.Authenticated = authenticated;
  145. base.OnAuthenticate (e);
  146. e.Authenticated = authenticated;
  147. }
  148. public void DoAuthenticate (AuthenticateEventArgs e)
  149. {
  150. base.OnAuthenticate (e);
  151. }
  152. public bool OnBubbleEventCalled {
  153. get { return onBubble; }
  154. set { onBubble = value; }
  155. }
  156. protected override bool OnBubbleEvent (object source, EventArgs e)
  157. {
  158. onBubble = true;
  159. return base.OnBubbleEvent (source, e);
  160. }
  161. public bool DoBubbleEvent (object source, EventArgs e)
  162. {
  163. return base.OnBubbleEvent (source, e);
  164. }
  165. public bool OnLoggedInCalled {
  166. get { return onLoggedIn; }
  167. set { onLoggedIn = value; }
  168. }
  169. protected override void OnLoggedIn (EventArgs e)
  170. {
  171. onLoggedIn = true;
  172. base.OnLoggedIn (e);
  173. }
  174. public void DoLoggedIn (EventArgs e)
  175. {
  176. base.OnLoggedIn (e);
  177. }
  178. public bool OnLoggingInCalled {
  179. get { return onLoggingIn; }
  180. set { onLoggingIn = value; }
  181. }
  182. protected override void OnLoggingIn (LoginCancelEventArgs e)
  183. {
  184. onLoggingIn = true;
  185. e.Cancel = cancel;
  186. base.OnLoggingIn (e);
  187. }
  188. public void DoLoggingIn (LoginCancelEventArgs e)
  189. {
  190. base.OnLoggingIn (e);
  191. }
  192. public bool OnLoginErrorCalled {
  193. get { return onLoginError; }
  194. set { onLoginError = value; }
  195. }
  196. protected override void OnLoginError (EventArgs e)
  197. {
  198. onLoginError = true;
  199. base.OnLoginError (e);
  200. }
  201. public void DoLoginError (EventArgs e)
  202. {
  203. base.OnLoginError (e);
  204. }
  205. public void DoEnsureChildControls() {
  206. base.EnsureChildControls ();
  207. }
  208. }
  209. [TestFixture]
  210. public class LoginTest {
  211. private HtmlTextWriter GetWriter ()
  212. {
  213. StringWriter sw = new StringWriter ();
  214. sw.NewLine = "\n";
  215. return new HtmlTextWriter (sw);
  216. }
  217. [Test]
  218. public void ReadOnlyFields ()
  219. {
  220. Assert.AreEqual ("Login", Login.LoginButtonCommandName, "LoginButtonCommandName");
  221. }
  222. [Test]
  223. public void DefaultProperties ()
  224. {
  225. TestLogin l = new TestLogin ();
  226. Assert.AreEqual (0, l.Attributes.Count, "Attributes.Count");
  227. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count");
  228. Assert.AreEqual (1, l.BorderPadding, "BorderPadding");
  229. Assert.AreEqual (String.Empty, l.CreateUserIconUrl, "CreateUserIconUrl");
  230. Assert.AreEqual (String.Empty, l.CreateUserText, "CreateUserText");
  231. Assert.AreEqual (String.Empty, l.DestinationPageUrl, "DestinationPageUrl");
  232. Assert.IsTrue (l.DisplayRememberMe, "DisplayRememberMe");
  233. Assert.AreEqual (LoginFailureAction.Refresh, l.FailureAction, "FailureAction");
  234. Assert.AreEqual ("Your login attempt was not successful. Please try again.", l.FailureText, "FailureText");
  235. Assert.AreEqual (String.Empty, l.HelpPageIconUrl, "HelpPageIconUrl");
  236. Assert.AreEqual (String.Empty, l.HelpPageText, "HelpPageText");
  237. Assert.AreEqual (String.Empty, l.HelpPageUrl, "HelpPageUrl");
  238. Assert.AreEqual (String.Empty, l.InstructionText, "InstructionText");
  239. Assert.AreEqual (String.Empty, l.LoginButtonImageUrl, "LoginButtonImageUrl");
  240. Assert.AreEqual ("Log In", l.LoginButtonText, "LoginButtonText");
  241. Assert.AreEqual (ButtonType.Button, l.LoginButtonType, "LoginButtonType");
  242. Assert.AreEqual (String.Empty, l.MembershipProvider, "MembershipProvider");
  243. Assert.AreEqual (Orientation.Vertical, l.Orientation, "Orientation");
  244. Assert.AreEqual (String.Empty, l.Password, "Password");
  245. Assert.AreEqual ("Password:", l.PasswordLabelText, "PasswordLabelText");
  246. Assert.AreEqual (String.Empty, l.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl");
  247. Assert.AreEqual (String.Empty, l.PasswordRecoveryText, "PasswordRecoveryText");
  248. Assert.AreEqual (String.Empty, l.PasswordRecoveryUrl, "PasswordRecoveryUrl");
  249. Assert.AreEqual ("Password is required.", l.PasswordRequiredErrorMessage, "PasswordRequiredErrorMessage");
  250. Assert.IsFalse (l.RememberMeSet, "RememberMeSet");
  251. Assert.AreEqual ("Remember me next time.", l.RememberMeText, "RememberMeText");
  252. Assert.AreEqual (LoginTextLayout.TextOnLeft, l.TextLayout, "TextLayout");
  253. Assert.AreEqual ("Log In", l.TitleText, "TitleText");
  254. Assert.AreEqual (String.Empty, l.UserName, "UserName");
  255. Assert.AreEqual ("User Name:", l.UserNameLabelText, "UserNameLabelText");
  256. Assert.AreEqual ("User Name is required.", l.UserNameRequiredErrorMessage, "UserNameRequiredErrorMessage");
  257. Assert.IsTrue (l.VisibleWhenLoggedIn, "VisibleWhenLoggedIn");
  258. // Styles
  259. Assert.IsNotNull (l.CheckBoxStyle, "CheckBoxStyle");
  260. Assert.IsNotNull (l.FailureTextStyle, "FailureTextStyle");
  261. Assert.IsNotNull (l.HyperLinkStyle, "HyperLinkStyle");
  262. Assert.IsNotNull (l.InstructionTextStyle, "InstructionTextStyle");
  263. Assert.IsNotNull (l.LabelStyle, "LabelStyle");
  264. Assert.IsNotNull (l.LoginButtonStyle, "LoginButtonStyle");
  265. Assert.IsNotNull (l.TextBoxStyle, "TextBoxStyle");
  266. Assert.IsNotNull (l.TitleTextStyle, "TitleTextStyle");
  267. Assert.IsNotNull (l.ValidatorTextStyle, "ValidatorTextStyle");
  268. // Templates
  269. Assert.IsNull (l.LayoutTemplate, "LayoutTemplate");
  270. Assert.AreEqual ("table", l.Tag, "TagName");
  271. Assert.AreEqual (0, l.Attributes.Count, "Attributes.Count-2");
  272. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-2");
  273. }
  274. [Test]
  275. public void AssignToDefaultProperties ()
  276. {
  277. TestLogin l = new TestLogin ();
  278. Assert.AreEqual (0, l.Attributes.Count, "Attributes.Count");
  279. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count");
  280. l.BorderPadding = 1;
  281. Assert.AreEqual (1, l.BorderPadding, "BorderPadding");
  282. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-1");
  283. l.CreateUserIconUrl = String.Empty;
  284. Assert.AreEqual (String.Empty, l.CreateUserIconUrl, "CreateUserIconUrl");
  285. Assert.AreEqual (2, l.StateBag.Count, "ViewState.Count-2");
  286. l.CreateUserText = String.Empty;
  287. Assert.AreEqual (String.Empty, l.CreateUserText, "CreateUserText");
  288. Assert.AreEqual (3, l.StateBag.Count, "ViewState.Count-3");
  289. l.DestinationPageUrl = String.Empty;
  290. Assert.AreEqual (String.Empty, l.DestinationPageUrl, "DestinationPageUrl");
  291. Assert.AreEqual (4, l.StateBag.Count, "ViewState.Count-4");
  292. l.DisplayRememberMe = true;
  293. Assert.IsTrue (l.DisplayRememberMe, "DisplayRememberMe");
  294. Assert.AreEqual (5, l.StateBag.Count, "ViewState.Count-5");
  295. l.FailureAction = LoginFailureAction.Refresh;
  296. Assert.AreEqual (LoginFailureAction.Refresh, l.FailureAction, "FailureAction");
  297. Assert.AreEqual (6, l.StateBag.Count, "ViewState.Count-6");
  298. l.FailureText = "Your login attempt was not successful. Please try again.";
  299. Assert.AreEqual ("Your login attempt was not successful. Please try again.", l.FailureText, "FailureText");
  300. Assert.AreEqual (7, l.StateBag.Count, "ViewState.Count-7");
  301. l.HelpPageIconUrl = String.Empty;
  302. Assert.AreEqual (String.Empty, l.HelpPageIconUrl, "HelpPageIconUrl");
  303. Assert.AreEqual (8, l.StateBag.Count, "ViewState.Count-8");
  304. l.HelpPageText = String.Empty;
  305. Assert.AreEqual (String.Empty, l.HelpPageText, "HelpPageText");
  306. Assert.AreEqual (9, l.StateBag.Count, "ViewState.Count-9");
  307. l.HelpPageUrl = String.Empty;
  308. Assert.AreEqual (String.Empty, l.HelpPageUrl, "HelpPageUrl");
  309. Assert.AreEqual (10, l.StateBag.Count, "ViewState.Count-10");
  310. l.InstructionText = String.Empty;
  311. Assert.AreEqual (String.Empty, l.InstructionText, "InstructionText");
  312. Assert.AreEqual (11, l.StateBag.Count, "ViewState.Count-11");
  313. l.LayoutTemplate = null;
  314. Assert.IsNull (l.LayoutTemplate, "LayoutTemplate");
  315. l.LoginButtonImageUrl = String.Empty;
  316. Assert.AreEqual (String.Empty, l.LoginButtonImageUrl, "LoginButtonImageUrl");
  317. Assert.AreEqual (12, l.StateBag.Count, "ViewState.Count-12");
  318. l.LoginButtonText = "Log In";
  319. Assert.AreEqual ("Log In", l.LoginButtonText, "LoginButtonText");
  320. Assert.AreEqual (13, l.StateBag.Count, "ViewState.Count-13");
  321. l.LoginButtonType = ButtonType.Button;
  322. Assert.AreEqual (ButtonType.Button, l.LoginButtonType, "LoginButtonType");
  323. Assert.AreEqual (14, l.StateBag.Count, "ViewState.Count-14");
  324. l.MembershipProvider = String.Empty;
  325. Assert.AreEqual (String.Empty, l.MembershipProvider, "MembershipProvider");
  326. Assert.AreEqual (15, l.StateBag.Count, "ViewState.Count-15");
  327. l.Orientation = Orientation.Vertical;
  328. Assert.AreEqual (Orientation.Vertical, l.Orientation, "Orientation");
  329. Assert.AreEqual (16, l.StateBag.Count, "ViewState.Count-16");
  330. // note: Password is read-only
  331. Assert.AreEqual (String.Empty, l.Password, "Password");
  332. l.PasswordLabelText = "Password:";
  333. Assert.AreEqual ("Password:", l.PasswordLabelText, "PasswordLabelText");
  334. Assert.AreEqual (17, l.StateBag.Count, "ViewState.Count-17");
  335. l.PasswordRecoveryIconUrl = String.Empty;
  336. Assert.AreEqual (String.Empty, l.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl");
  337. Assert.AreEqual (18, l.StateBag.Count, "ViewState.Count-18");
  338. l.PasswordRecoveryText = String.Empty;
  339. Assert.AreEqual (String.Empty, l.PasswordRecoveryText, "PasswordRecoveryText");
  340. Assert.AreEqual (19, l.StateBag.Count, "ViewState.Count-19");
  341. l.PasswordRecoveryUrl = String.Empty;
  342. Assert.AreEqual (String.Empty, l.PasswordRecoveryUrl, "PasswordRecoveryUrl");
  343. Assert.AreEqual (20, l.StateBag.Count, "ViewState.Count-20");
  344. l.PasswordRequiredErrorMessage = "Password is required.";
  345. Assert.AreEqual ("Password is required.", l.PasswordRequiredErrorMessage, "PasswordRequiredErrorMessage");
  346. Assert.AreEqual (21, l.StateBag.Count, "ViewState.Count-21");
  347. l.RememberMeSet = false;
  348. Assert.IsFalse (l.RememberMeSet, "RememberMeSet");
  349. Assert.AreEqual (22, l.StateBag.Count, "ViewState.Count-22");
  350. l.RememberMeText = "Remember me next time.";
  351. Assert.AreEqual ("Remember me next time.", l.RememberMeText, "RememberMeText");
  352. Assert.AreEqual (23, l.StateBag.Count, "ViewState.Count-23");
  353. l.TextLayout = LoginTextLayout.TextOnLeft;
  354. Assert.AreEqual (LoginTextLayout.TextOnLeft, l.TextLayout, "TextLayout");
  355. Assert.AreEqual (24, l.StateBag.Count, "ViewState.Count-24");
  356. l.TitleText = "Log In";
  357. Assert.AreEqual ("Log In", l.TitleText, "TitleText");
  358. Assert.AreEqual (25, l.StateBag.Count, "ViewState.Count-25");
  359. l.UserName = String.Empty;
  360. Assert.AreEqual (String.Empty, l.UserName, "UserName");
  361. Assert.AreEqual (26, l.StateBag.Count, "ViewState.Count-26");
  362. l.UserNameLabelText = "User Name:";
  363. Assert.AreEqual ("User Name:", l.UserNameLabelText, "UserNameLabelText");
  364. Assert.AreEqual (27, l.StateBag.Count, "ViewState.Count-27");
  365. l.UserNameRequiredErrorMessage = "User Name is required.";
  366. Assert.AreEqual ("User Name is required.", l.UserNameRequiredErrorMessage, "UserNameRequiredErrorMessage");
  367. Assert.AreEqual (28, l.StateBag.Count, "ViewState.Count-28");
  368. l.VisibleWhenLoggedIn = true;
  369. Assert.IsTrue (l.VisibleWhenLoggedIn, "VisibleWhenLoggedIn");
  370. Assert.AreEqual (29, l.StateBag.Count, "ViewState.Count-29");
  371. Assert.AreEqual (0, l.Attributes.Count, "Attributes.Count-2");
  372. }
  373. [Test]
  374. public void NullProperties ()
  375. {
  376. TestLogin l = new TestLogin ();
  377. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count");
  378. // unlike 1.1 controls it seems that null (and not the default value)
  379. // must be used to remove values from the ViewState
  380. l.CreateUserIconUrl = "*";
  381. Assert.AreEqual ("*", l.CreateUserIconUrl, "CreateUserIconUrl*");
  382. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-1*");
  383. l.CreateUserIconUrl = null;
  384. Assert.AreEqual (String.Empty, l.CreateUserIconUrl, "CreateUserIconUrl");
  385. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-1");
  386. l.CreateUserText = "*";
  387. Assert.AreEqual ("*", l.CreateUserText, "CreateUserText*");
  388. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-2*");
  389. l.CreateUserText = null;
  390. Assert.AreEqual (String.Empty, l.CreateUserText, "CreateUserText");
  391. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-2");
  392. l.DestinationPageUrl = "*";
  393. Assert.AreEqual ("*", l.DestinationPageUrl, "DestinationPageUrl*");
  394. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-3*");
  395. l.DestinationPageUrl = null;
  396. Assert.AreEqual (String.Empty, l.DestinationPageUrl, "DestinationPageUrl");
  397. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-3");
  398. l.FailureText = "*";
  399. Assert.AreEqual ("*", l.FailureText, "FailureTex*");
  400. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-4*");
  401. l.FailureText = null;
  402. Assert.AreEqual ("Your login attempt was not successful. Please try again.", l.FailureText, "FailureText");
  403. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-4");
  404. l.HelpPageIconUrl = "*";
  405. Assert.AreEqual ("*", l.HelpPageIconUrl, "HelpPageIconUrl*");
  406. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-5*");
  407. l.HelpPageIconUrl = null;
  408. Assert.AreEqual (String.Empty, l.HelpPageIconUrl, "HelpPageIconUrl");
  409. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-5");
  410. l.HelpPageText = "*";
  411. Assert.AreEqual ("*", l.HelpPageText, "HelpPageText*");
  412. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-6*");
  413. l.HelpPageText = null;
  414. Assert.AreEqual (String.Empty, l.HelpPageText, "HelpPageText");
  415. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-6");
  416. l.HelpPageUrl = "*";
  417. Assert.AreEqual ("*", l.HelpPageUrl, "HelpPageUrl*");
  418. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-7*");
  419. l.HelpPageUrl = null;
  420. Assert.AreEqual (String.Empty, l.HelpPageUrl, "HelpPageUrl");
  421. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-7");
  422. l.InstructionText = "*";
  423. Assert.AreEqual ("*", l.InstructionText, "InstructionText*");
  424. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-8*");
  425. l.InstructionText = null;
  426. Assert.AreEqual (String.Empty, l.InstructionText, "InstructionText");
  427. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-8");
  428. l.LoginButtonImageUrl = "*";
  429. Assert.AreEqual ("*", l.LoginButtonImageUrl, "LoginButtonImageUrl*");
  430. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-9*");
  431. l.LoginButtonImageUrl = null;
  432. Assert.AreEqual (String.Empty, l.LoginButtonImageUrl, "LoginButtonImageUrl");
  433. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-9");
  434. l.LoginButtonText = "*";
  435. Assert.AreEqual ("*", l.LoginButtonText, "LoginButtonText*");
  436. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-10*");
  437. l.LoginButtonText = null;
  438. Assert.AreEqual ("Log In", l.LoginButtonText, "LoginButtonText");
  439. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-10");
  440. l.MembershipProvider = "*";
  441. Assert.AreEqual ("*", l.MembershipProvider, "MembershipProvider*");
  442. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-11*");
  443. l.MembershipProvider = null;
  444. Assert.AreEqual (String.Empty, l.MembershipProvider, "MembershipProvider");
  445. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-11");
  446. l.PasswordLabelText = "*";
  447. Assert.AreEqual ("*", l.PasswordLabelText, "PasswordLabelText*");
  448. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-12*");
  449. l.PasswordLabelText = null;
  450. Assert.AreEqual ("Password:", l.PasswordLabelText, "PasswordLabelText");
  451. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-12");
  452. l.PasswordRecoveryIconUrl = "*";
  453. Assert.AreEqual ("*", l.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl*");
  454. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-13*");
  455. l.PasswordRecoveryIconUrl = null;
  456. Assert.AreEqual (String.Empty, l.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl");
  457. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-13");
  458. l.PasswordRecoveryText = "*";
  459. Assert.AreEqual ("*", l.PasswordRecoveryText, "PasswordRecoveryText*");
  460. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-14*");
  461. l.PasswordRecoveryText = null;
  462. Assert.AreEqual (String.Empty, l.PasswordRecoveryText, "PasswordRecoveryText");
  463. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-14");
  464. l.PasswordRecoveryUrl = "*";
  465. Assert.AreEqual ("*", l.PasswordRecoveryUrl, "PasswordRecoveryUrl*");
  466. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-15*");
  467. l.PasswordRecoveryUrl = null;
  468. Assert.AreEqual (String.Empty, l.PasswordRecoveryUrl, "PasswordRecoveryUrl");
  469. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-15");
  470. l.PasswordRequiredErrorMessage = "*";
  471. Assert.AreEqual ("*", l.PasswordRequiredErrorMessage, "PasswordRequiredErrorMessage*");
  472. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-16*");
  473. l.PasswordRequiredErrorMessage = null;
  474. Assert.AreEqual ("Password is required.", l.PasswordRequiredErrorMessage, "PasswordRequiredErrorMessage");
  475. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-16");
  476. l.RememberMeText = "*";
  477. Assert.AreEqual ("*", l.RememberMeText, "RememberMeText*");
  478. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-17*");
  479. l.RememberMeText = null;
  480. Assert.AreEqual ("Remember me next time.", l.RememberMeText, "RememberMeText");
  481. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-17");
  482. l.TitleText = "*";
  483. Assert.AreEqual ("*", l.TitleText, "TitleText*");
  484. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-18*");
  485. l.TitleText = null;
  486. Assert.AreEqual ("Log In", l.TitleText, "TitleText");
  487. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-18");
  488. l.UserName = "*";
  489. Assert.AreEqual ("*", l.UserName, "UserName*");
  490. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-19*");
  491. l.UserName = null;
  492. Assert.AreEqual (String.Empty, l.UserName, "UserName");
  493. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-19");
  494. l.UserNameLabelText = "*";
  495. Assert.AreEqual ("*", l.UserNameLabelText, "UserNameLabelText*");
  496. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-20*");
  497. l.UserNameLabelText = null;
  498. Assert.AreEqual ("User Name:", l.UserNameLabelText, "UserNameLabelText");
  499. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-20");
  500. l.UserNameRequiredErrorMessage = "*";
  501. Assert.AreEqual ("*", l.UserNameRequiredErrorMessage, "UserNameRequiredErrorMessage*");
  502. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-21*");
  503. l.UserNameRequiredErrorMessage = null;
  504. Assert.AreEqual ("User Name is required.", l.UserNameRequiredErrorMessage, "UserNameRequiredErrorMessage");
  505. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-21");
  506. }
  507. [Test]
  508. public void BorderPadding ()
  509. {
  510. TestLogin l = new TestLogin ();
  511. l.BorderPadding = Int32.MaxValue;
  512. Assert.AreEqual (Int32.MaxValue, l.BorderPadding, "BorderPadding");
  513. l.BorderPadding = 1;
  514. Assert.AreEqual (1, l.BorderPadding, "BorderPadding");
  515. l.BorderPadding = 0;
  516. Assert.AreEqual (0, l.BorderPadding, "BorderPadding");
  517. l.BorderPadding = -1;
  518. Assert.AreEqual (-1, l.BorderPadding, "BorderPadding");
  519. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  520. }
  521. [Test]
  522. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  523. public void BorderPadding_Negative ()
  524. {
  525. TestLogin l = new TestLogin ();
  526. l.BorderPadding = Int32.MinValue;
  527. Assert.AreEqual (1, l.BorderPadding, "BorderPadding");
  528. }
  529. [Test]
  530. public void FailureAction_All ()
  531. {
  532. TestLogin l = new TestLogin ();
  533. foreach (LoginFailureAction lfa in Enum.GetValues (typeof (LoginFailureAction))) {
  534. l.FailureAction = lfa;
  535. }
  536. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  537. }
  538. [Test]
  539. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  540. public void FailureAction_Invalid ()
  541. {
  542. TestLogin l = new TestLogin ();
  543. l.FailureAction = (LoginFailureAction) Int32.MinValue;
  544. }
  545. [Test]
  546. public void LayoutTemplate()
  547. {
  548. TestLogin l = new TestLogin();
  549. l.LayoutTemplate = new LayoutTemplate();
  550. l.DoEnsureChildControls();
  551. Assert.IsNotNull(l.FindControl("Template"), "LoginTemplate");
  552. Assert.IsNotNull(l.FindControl("UserName"), "UserName");
  553. }
  554. [Test]
  555. [ExpectedException(typeof(HttpException))]
  556. public void LayoutTemplateException ()
  557. {
  558. TestLogin l = new TestLogin ();
  559. l.LayoutTemplate = new LoginTemplate();
  560. l.DoEnsureChildControls();
  561. }
  562. [Test]
  563. public void LoginButtonType_All ()
  564. {
  565. TestLogin l = new TestLogin ();
  566. foreach (ButtonType bt in Enum.GetValues (typeof (ButtonType))) {
  567. l.LoginButtonType = bt;
  568. }
  569. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  570. }
  571. [Test]
  572. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  573. public void LoginButtonType_Invalid ()
  574. {
  575. TestLogin l = new TestLogin ();
  576. l.LoginButtonType = (ButtonType)Int32.MinValue;
  577. }
  578. [Test]
  579. public void Orientation_All ()
  580. {
  581. TestLogin l = new TestLogin ();
  582. foreach (Orientation o in Enum.GetValues (typeof (Orientation))) {
  583. l.Orientation = o;
  584. }
  585. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  586. }
  587. [Test]
  588. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  589. public void Orientation_Invalid ()
  590. {
  591. TestLogin l = new TestLogin ();
  592. l.Orientation = (Orientation)Int32.MinValue;
  593. }
  594. [Test]
  595. public void TextLayout_All ()
  596. {
  597. TestLogin l = new TestLogin ();
  598. foreach (LoginTextLayout ltl in Enum.GetValues (typeof (LoginTextLayout))) {
  599. l.TextLayout = ltl;
  600. }
  601. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  602. }
  603. [Test]
  604. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  605. public void TextLayout_Invalid ()
  606. {
  607. TestLogin l = new TestLogin ();
  608. l.TextLayout = (LoginTextLayout)Int32.MinValue;
  609. }
  610. [Test]
  611. public void SaveViewState ()
  612. {
  613. TestLogin l = new TestLogin ();
  614. l.TrackState ();
  615. Assert.IsNull (l.SaveState (), "Empty");
  616. l.BorderPadding = 2;
  617. object[] vs = (object[])l.SaveState ();
  618. Assert.AreEqual (10, vs.Length, "Size");
  619. l.CreateUserIconUrl = String.Empty;
  620. l.CreateUserText = String.Empty;
  621. l.DestinationPageUrl = String.Empty;
  622. l.DisplayRememberMe = true;
  623. l.FailureAction = LoginFailureAction.Refresh;
  624. l.FailureText = "Your login attempt was not successful. Please try again.";
  625. l.HelpPageIconUrl = String.Empty;
  626. l.HelpPageText = String.Empty;
  627. l.HelpPageUrl = String.Empty;
  628. l.InstructionText = String.Empty;
  629. l.LayoutTemplate = null;
  630. l.LoginButtonImageUrl = String.Empty;
  631. l.LoginButtonText = "Log In";
  632. l.LoginButtonType = ButtonType.Button;
  633. l.MembershipProvider = String.Empty;
  634. l.Orientation = Orientation.Vertical;
  635. // note: Password is read-only
  636. l.PasswordLabelText = "Password:";
  637. l.PasswordRecoveryIconUrl = String.Empty;
  638. l.PasswordRecoveryText = String.Empty;
  639. l.PasswordRecoveryUrl = String.Empty;
  640. l.PasswordRequiredErrorMessage = "Password is required.";
  641. l.RememberMeSet = false;
  642. l.RememberMeText = "Remember me next time.";
  643. l.TextLayout = LoginTextLayout.TextOnLeft;
  644. l.TitleText = "Log In";
  645. l.UserName = String.Empty;
  646. l.UserNameLabelText = "User Name:";
  647. l.UserNameRequiredErrorMessage = "User Name is required.";
  648. l.VisibleWhenLoggedIn = true;
  649. vs = (object[])l.SaveState ();
  650. // the viewstate is all null except the first element
  651. Assert.IsNotNull (vs[0], "NotEmpty-0");
  652. for (int i = 1; i < vs.Length; i++)
  653. Assert.IsNull (vs[i], "Empty-" + i);
  654. l.CheckBoxStyle.HorizontalAlign = HorizontalAlign.Justify;
  655. vs = (object[])l.SaveState ();
  656. Assert.IsNotNull (vs[7], "NotEmpty-7");
  657. l.FailureTextStyle.HorizontalAlign = HorizontalAlign.Justify;
  658. vs = (object[])l.SaveState ();
  659. Assert.IsNotNull (vs[8], "NotEmpty-8");
  660. l.HyperLinkStyle.HorizontalAlign = HorizontalAlign.Justify;
  661. vs = (object[])l.SaveState ();
  662. Assert.IsNotNull (vs[4], "NotEmpty-4");
  663. l.InstructionTextStyle.HorizontalAlign = HorizontalAlign.Justify;
  664. vs = (object[])l.SaveState ();
  665. Assert.IsNotNull (vs[5], "NotEmpty-5");
  666. l.LabelStyle.HorizontalAlign = HorizontalAlign.Justify;
  667. vs = (object[])l.SaveState ();
  668. Assert.IsNotNull (vs[2], "NotEmpty-2");
  669. l.LoginButtonStyle.BorderStyle = BorderStyle.Double;
  670. vs = (object[])l.SaveState ();
  671. Assert.IsNotNull (vs[1], "NotEmpty-1");
  672. l.TextBoxStyle.BorderStyle = BorderStyle.Double;
  673. vs = (object[])l.SaveState ();
  674. Assert.IsNotNull (vs[3], "NotEmpty-3");
  675. l.TitleTextStyle.HorizontalAlign = HorizontalAlign.Justify;
  676. vs = (object[])l.SaveState ();
  677. Assert.IsNotNull (vs[6], "NotEmpty-6");
  678. l.ValidatorTextStyle.BorderStyle = BorderStyle.Double;
  679. vs = (object[])l.SaveState ();
  680. Assert.IsNotNull (vs[9], "NotEmpty-9");
  681. }
  682. [Test]
  683. [Category ("NunitWeb")]
  684. [Category("NotWorking")]
  685. public void OnBubbleEvent ()
  686. {
  687. WebTest t = new WebTest (PageInvoker.CreateOnPreInit (_OnBubbleEvent));
  688. string html = t.Run ();
  689. }
  690. public static void _OnBubbleEvent(Page p)
  691. {
  692. TestLogin l = new TestLogin ();
  693. l.Page = p;
  694. l.Page.Validate ();
  695. l.MembershipProvider = "FakeProvider";
  696. Button b = (Button)l.FindControl ("LoginButton");
  697. Assert.IsNotNull (b, "LoginButton");
  698. CommandEventArgs cea = new CommandEventArgs ("Login", null);
  699. l.DoBubbleEvent (b, cea);
  700. Assert.IsTrue (l.OnLoggingInCalled, "OnLoggingIn");
  701. Assert.IsFalse (l.Cancel, "Cancel");
  702. Assert.IsTrue (l.OnAuthenticateCalled, "OnAuthenticate");
  703. Assert.IsFalse (l.Authenticated, "Authenticated");
  704. Assert.IsTrue (l.OnLoginErrorCalled, "OnLoginError");
  705. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  706. }
  707. [Test]
  708. public void OnBubbleEvent_Cancel_OnLoggingIn ()
  709. {
  710. TestLogin l = new TestLogin ();
  711. l.Page = new Page ();
  712. l.Page.Validate ();
  713. Button b = (Button)l.FindControl ("LoginButton");
  714. Assert.IsNotNull (b, "LoginButton");
  715. CommandEventArgs cea = new CommandEventArgs ("Login", null);
  716. l.Cancel = true;
  717. l.DoBubbleEvent (b, cea);
  718. Assert.IsTrue (l.OnLoggingInCalled, "OnLoggingIn");
  719. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  720. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  721. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  722. }
  723. [Test]
  724. [Category("NotWorking")]
  725. public void OnBubbleEvent_Authenticated_OnAuthenticate ()
  726. {
  727. WebTest t = new WebTest (PageInvoker.CreateOnPreInit (_OnBubbleEvent_Authenticated_OnAuthenticate));
  728. string html = t.Run ();
  729. }
  730. public static void _OnBubbleEvent_Authenticated_OnAuthenticate (Page p)
  731. {
  732. TestLogin l = new TestLogin ();
  733. l.Page = p;
  734. l.Page.Validate ();
  735. Button b = (Button) l.FindControl ("LoginButton");
  736. Assert.IsNotNull (b, "LoginButton");
  737. CommandEventArgs cea = new CommandEventArgs ("Login", null);
  738. l.UserName = "me";
  739. l.Authenticated = true;
  740. l.MembershipProvider = "FakeProvider";
  741. l.Authenticate += new AuthenticateEventHandler(l_Authenticate);
  742. try {
  743. l.DoBubbleEvent (b, cea);
  744. }
  745. catch (NullReferenceException) {
  746. // ms
  747. }
  748. catch (HttpException) {
  749. // no context is available
  750. }
  751. }
  752. public static void l_Authenticate (object sender, AuthenticateEventArgs e)
  753. {
  754. if (e.Authenticated == true) {
  755. TestLogin l = (TestLogin) sender;
  756. l.Authenticated = false;
  757. }
  758. else
  759. Assert.Fail ("Login failed: l_Authenticate");
  760. }
  761. private void OnLoggingIn (bool cancel)
  762. {
  763. TestLogin l = new TestLogin ();
  764. LoginCancelEventArgs lcea = new LoginCancelEventArgs (cancel);
  765. l.DoLoggingIn (lcea);
  766. Assert.IsFalse (l.OnBubbleEventCalled, "OnBubbleEvent");
  767. Assert.IsFalse (l.OnLoggingInCalled, "OnLoggingIn");
  768. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  769. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  770. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  771. }
  772. [Test]
  773. public void OnLoggingIn_False ()
  774. {
  775. OnLoggingIn (false);
  776. }
  777. [Test]
  778. public void OnLoggingIn_True ()
  779. {
  780. OnLoggingIn (true);
  781. }
  782. private void OnAuthenticate (bool authenticate)
  783. {
  784. TestLogin l = new TestLogin ();
  785. AuthenticateEventArgs aea = new AuthenticateEventArgs (authenticate);
  786. l.DoAuthenticate (aea);
  787. Assert.IsFalse (l.OnBubbleEventCalled, "OnBubbleEvent");
  788. Assert.IsFalse (l.OnLoggingInCalled, "OnLoggingIn");
  789. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  790. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  791. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  792. }
  793. [Test]
  794. public void OnAuthenticate_False ()
  795. {
  796. OnAuthenticate (false);
  797. }
  798. [Test]
  799. public void OnAuthenticate_True ()
  800. {
  801. OnAuthenticate (true);
  802. }
  803. [Test]
  804. public void OnLoginError ()
  805. {
  806. TestLogin l = new TestLogin ();
  807. l.DoLoginError (EventArgs.Empty);
  808. Assert.IsFalse (l.OnBubbleEventCalled, "OnBubbleEvent");
  809. Assert.IsFalse (l.OnLoggingInCalled, "OnLoggingIn");
  810. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  811. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  812. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  813. }
  814. [Test]
  815. public void OnLoggedIn ()
  816. {
  817. TestLogin l = new TestLogin ();
  818. l.DoLoggedIn (EventArgs.Empty);
  819. Assert.IsFalse (l.OnBubbleEventCalled, "OnBubbleEvent");
  820. Assert.IsFalse (l.OnLoggingInCalled, "OnLoggingIn");
  821. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  822. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  823. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  824. }
  825. }
  826. }
  827. #endif