LoginTest.cs 38 KB

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