LoginTest.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. #if VISUAL_STUDIO
  223. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.NoEventValidation.aspx", "NoEventValidation.aspx");
  224. #else
  225. WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
  226. #endif
  227. }
  228. #endif
  229. [Test]
  230. public void ReadOnlyFields ()
  231. {
  232. Assert.AreEqual ("Login", Login.LoginButtonCommandName, "LoginButtonCommandName");
  233. }
  234. [Test]
  235. public void DefaultProperties ()
  236. {
  237. TestLogin l = new TestLogin ();
  238. Assert.AreEqual (0, l.Attributes.Count, "Attributes.Count");
  239. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count");
  240. Assert.AreEqual (1, l.BorderPadding, "BorderPadding");
  241. Assert.AreEqual (String.Empty, l.CreateUserIconUrl, "CreateUserIconUrl");
  242. Assert.AreEqual (String.Empty, l.CreateUserText, "CreateUserText");
  243. Assert.AreEqual (String.Empty, l.DestinationPageUrl, "DestinationPageUrl");
  244. Assert.IsTrue (l.DisplayRememberMe, "DisplayRememberMe");
  245. Assert.AreEqual (LoginFailureAction.Refresh, l.FailureAction, "FailureAction");
  246. Assert.AreEqual ("Your login attempt was not successful. Please try again.", l.FailureText, "FailureText");
  247. Assert.AreEqual (String.Empty, l.HelpPageIconUrl, "HelpPageIconUrl");
  248. Assert.AreEqual (String.Empty, l.HelpPageText, "HelpPageText");
  249. Assert.AreEqual (String.Empty, l.HelpPageUrl, "HelpPageUrl");
  250. Assert.AreEqual (String.Empty, l.InstructionText, "InstructionText");
  251. Assert.AreEqual (String.Empty, l.LoginButtonImageUrl, "LoginButtonImageUrl");
  252. Assert.AreEqual ("Log In", l.LoginButtonText, "LoginButtonText");
  253. Assert.AreEqual (ButtonType.Button, l.LoginButtonType, "LoginButtonType");
  254. Assert.AreEqual (String.Empty, l.MembershipProvider, "MembershipProvider");
  255. Assert.AreEqual (Orientation.Vertical, l.Orientation, "Orientation");
  256. Assert.AreEqual (String.Empty, l.Password, "Password");
  257. Assert.AreEqual ("Password:", l.PasswordLabelText, "PasswordLabelText");
  258. Assert.AreEqual (String.Empty, l.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl");
  259. Assert.AreEqual (String.Empty, l.PasswordRecoveryText, "PasswordRecoveryText");
  260. Assert.AreEqual (String.Empty, l.PasswordRecoveryUrl, "PasswordRecoveryUrl");
  261. Assert.AreEqual ("Password is required.", l.PasswordRequiredErrorMessage, "PasswordRequiredErrorMessage");
  262. Assert.IsFalse (l.RememberMeSet, "RememberMeSet");
  263. Assert.AreEqual ("Remember me next time.", l.RememberMeText, "RememberMeText");
  264. Assert.AreEqual (LoginTextLayout.TextOnLeft, l.TextLayout, "TextLayout");
  265. Assert.AreEqual ("Log In", l.TitleText, "TitleText");
  266. Assert.AreEqual (String.Empty, l.UserName, "UserName");
  267. Assert.AreEqual ("User Name:", l.UserNameLabelText, "UserNameLabelText");
  268. Assert.AreEqual ("User Name is required.", l.UserNameRequiredErrorMessage, "UserNameRequiredErrorMessage");
  269. Assert.IsTrue (l.VisibleWhenLoggedIn, "VisibleWhenLoggedIn");
  270. // Styles
  271. Assert.IsNotNull (l.CheckBoxStyle, "CheckBoxStyle");
  272. Assert.IsNotNull (l.FailureTextStyle, "FailureTextStyle");
  273. Assert.IsNotNull (l.HyperLinkStyle, "HyperLinkStyle");
  274. Assert.IsNotNull (l.InstructionTextStyle, "InstructionTextStyle");
  275. Assert.IsNotNull (l.LabelStyle, "LabelStyle");
  276. Assert.IsNotNull (l.LoginButtonStyle, "LoginButtonStyle");
  277. Assert.IsNotNull (l.TextBoxStyle, "TextBoxStyle");
  278. Assert.IsNotNull (l.TitleTextStyle, "TitleTextStyle");
  279. Assert.IsNotNull (l.ValidatorTextStyle, "ValidatorTextStyle");
  280. // Templates
  281. Assert.IsNull (l.LayoutTemplate, "LayoutTemplate");
  282. Assert.AreEqual ("table", l.Tag, "TagName");
  283. Assert.AreEqual (0, l.Attributes.Count, "Attributes.Count-2");
  284. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-2");
  285. }
  286. [Test]
  287. public void AssignToDefaultProperties ()
  288. {
  289. TestLogin l = new TestLogin ();
  290. Assert.AreEqual (0, l.Attributes.Count, "Attributes.Count");
  291. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count");
  292. l.BorderPadding = 1;
  293. Assert.AreEqual (1, l.BorderPadding, "BorderPadding");
  294. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-1");
  295. l.CreateUserIconUrl = String.Empty;
  296. Assert.AreEqual (String.Empty, l.CreateUserIconUrl, "CreateUserIconUrl");
  297. Assert.AreEqual (2, l.StateBag.Count, "ViewState.Count-2");
  298. l.CreateUserText = String.Empty;
  299. Assert.AreEqual (String.Empty, l.CreateUserText, "CreateUserText");
  300. Assert.AreEqual (3, l.StateBag.Count, "ViewState.Count-3");
  301. l.DestinationPageUrl = String.Empty;
  302. Assert.AreEqual (String.Empty, l.DestinationPageUrl, "DestinationPageUrl");
  303. Assert.AreEqual (4, l.StateBag.Count, "ViewState.Count-4");
  304. l.DisplayRememberMe = true;
  305. Assert.IsTrue (l.DisplayRememberMe, "DisplayRememberMe");
  306. Assert.AreEqual (5, l.StateBag.Count, "ViewState.Count-5");
  307. l.FailureAction = LoginFailureAction.Refresh;
  308. Assert.AreEqual (LoginFailureAction.Refresh, l.FailureAction, "FailureAction");
  309. Assert.AreEqual (6, l.StateBag.Count, "ViewState.Count-6");
  310. l.FailureText = "Your login attempt was not successful. Please try again.";
  311. Assert.AreEqual ("Your login attempt was not successful. Please try again.", l.FailureText, "FailureText");
  312. Assert.AreEqual (7, l.StateBag.Count, "ViewState.Count-7");
  313. l.HelpPageIconUrl = String.Empty;
  314. Assert.AreEqual (String.Empty, l.HelpPageIconUrl, "HelpPageIconUrl");
  315. Assert.AreEqual (8, l.StateBag.Count, "ViewState.Count-8");
  316. l.HelpPageText = String.Empty;
  317. Assert.AreEqual (String.Empty, l.HelpPageText, "HelpPageText");
  318. Assert.AreEqual (9, l.StateBag.Count, "ViewState.Count-9");
  319. l.HelpPageUrl = String.Empty;
  320. Assert.AreEqual (String.Empty, l.HelpPageUrl, "HelpPageUrl");
  321. Assert.AreEqual (10, l.StateBag.Count, "ViewState.Count-10");
  322. l.InstructionText = String.Empty;
  323. Assert.AreEqual (String.Empty, l.InstructionText, "InstructionText");
  324. Assert.AreEqual (11, l.StateBag.Count, "ViewState.Count-11");
  325. l.LayoutTemplate = null;
  326. Assert.IsNull (l.LayoutTemplate, "LayoutTemplate");
  327. l.LoginButtonImageUrl = String.Empty;
  328. Assert.AreEqual (String.Empty, l.LoginButtonImageUrl, "LoginButtonImageUrl");
  329. Assert.AreEqual (12, l.StateBag.Count, "ViewState.Count-12");
  330. l.LoginButtonText = "Log In";
  331. Assert.AreEqual ("Log In", l.LoginButtonText, "LoginButtonText");
  332. Assert.AreEqual (13, l.StateBag.Count, "ViewState.Count-13");
  333. l.LoginButtonType = ButtonType.Button;
  334. Assert.AreEqual (ButtonType.Button, l.LoginButtonType, "LoginButtonType");
  335. Assert.AreEqual (14, l.StateBag.Count, "ViewState.Count-14");
  336. l.MembershipProvider = String.Empty;
  337. Assert.AreEqual (String.Empty, l.MembershipProvider, "MembershipProvider");
  338. Assert.AreEqual (15, l.StateBag.Count, "ViewState.Count-15");
  339. l.Orientation = Orientation.Vertical;
  340. Assert.AreEqual (Orientation.Vertical, l.Orientation, "Orientation");
  341. Assert.AreEqual (16, l.StateBag.Count, "ViewState.Count-16");
  342. // note: Password is read-only
  343. Assert.AreEqual (String.Empty, l.Password, "Password");
  344. l.PasswordLabelText = "Password:";
  345. Assert.AreEqual ("Password:", l.PasswordLabelText, "PasswordLabelText");
  346. Assert.AreEqual (17, l.StateBag.Count, "ViewState.Count-17");
  347. l.PasswordRecoveryIconUrl = String.Empty;
  348. Assert.AreEqual (String.Empty, l.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl");
  349. Assert.AreEqual (18, l.StateBag.Count, "ViewState.Count-18");
  350. l.PasswordRecoveryText = String.Empty;
  351. Assert.AreEqual (String.Empty, l.PasswordRecoveryText, "PasswordRecoveryText");
  352. Assert.AreEqual (19, l.StateBag.Count, "ViewState.Count-19");
  353. l.PasswordRecoveryUrl = String.Empty;
  354. Assert.AreEqual (String.Empty, l.PasswordRecoveryUrl, "PasswordRecoveryUrl");
  355. Assert.AreEqual (20, l.StateBag.Count, "ViewState.Count-20");
  356. l.PasswordRequiredErrorMessage = "Password is required.";
  357. Assert.AreEqual ("Password is required.", l.PasswordRequiredErrorMessage, "PasswordRequiredErrorMessage");
  358. Assert.AreEqual (21, l.StateBag.Count, "ViewState.Count-21");
  359. l.RememberMeSet = false;
  360. Assert.IsFalse (l.RememberMeSet, "RememberMeSet");
  361. Assert.AreEqual (22, l.StateBag.Count, "ViewState.Count-22");
  362. l.RememberMeText = "Remember me next time.";
  363. Assert.AreEqual ("Remember me next time.", l.RememberMeText, "RememberMeText");
  364. Assert.AreEqual (23, l.StateBag.Count, "ViewState.Count-23");
  365. l.TextLayout = LoginTextLayout.TextOnLeft;
  366. Assert.AreEqual (LoginTextLayout.TextOnLeft, l.TextLayout, "TextLayout");
  367. Assert.AreEqual (24, l.StateBag.Count, "ViewState.Count-24");
  368. l.TitleText = "Log In";
  369. Assert.AreEqual ("Log In", l.TitleText, "TitleText");
  370. Assert.AreEqual (25, l.StateBag.Count, "ViewState.Count-25");
  371. l.UserName = String.Empty;
  372. Assert.AreEqual (String.Empty, l.UserName, "UserName");
  373. Assert.AreEqual (26, l.StateBag.Count, "ViewState.Count-26");
  374. l.UserNameLabelText = "User Name:";
  375. Assert.AreEqual ("User Name:", l.UserNameLabelText, "UserNameLabelText");
  376. Assert.AreEqual (27, l.StateBag.Count, "ViewState.Count-27");
  377. l.UserNameRequiredErrorMessage = "User Name is required.";
  378. Assert.AreEqual ("User Name is required.", l.UserNameRequiredErrorMessage, "UserNameRequiredErrorMessage");
  379. Assert.AreEqual (28, l.StateBag.Count, "ViewState.Count-28");
  380. l.VisibleWhenLoggedIn = true;
  381. Assert.IsTrue (l.VisibleWhenLoggedIn, "VisibleWhenLoggedIn");
  382. Assert.AreEqual (29, l.StateBag.Count, "ViewState.Count-29");
  383. Assert.AreEqual (0, l.Attributes.Count, "Attributes.Count-2");
  384. }
  385. [Test]
  386. public void NullProperties ()
  387. {
  388. TestLogin l = new TestLogin ();
  389. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count");
  390. // unlike 1.1 controls it seems that null (and not the default value)
  391. // must be used to remove values from the ViewState
  392. l.CreateUserIconUrl = "*";
  393. Assert.AreEqual ("*", l.CreateUserIconUrl, "CreateUserIconUrl*");
  394. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-1*");
  395. l.CreateUserIconUrl = null;
  396. Assert.AreEqual (String.Empty, l.CreateUserIconUrl, "CreateUserIconUrl");
  397. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-1");
  398. l.CreateUserText = "*";
  399. Assert.AreEqual ("*", l.CreateUserText, "CreateUserText*");
  400. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-2*");
  401. l.CreateUserText = null;
  402. Assert.AreEqual (String.Empty, l.CreateUserText, "CreateUserText");
  403. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-2");
  404. l.DestinationPageUrl = "*";
  405. Assert.AreEqual ("*", l.DestinationPageUrl, "DestinationPageUrl*");
  406. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-3*");
  407. l.DestinationPageUrl = null;
  408. Assert.AreEqual (String.Empty, l.DestinationPageUrl, "DestinationPageUrl");
  409. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-3");
  410. l.FailureText = "*";
  411. Assert.AreEqual ("*", l.FailureText, "FailureTex*");
  412. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-4*");
  413. l.FailureText = null;
  414. Assert.AreEqual ("Your login attempt was not successful. Please try again.", l.FailureText, "FailureText");
  415. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-4");
  416. l.HelpPageIconUrl = "*";
  417. Assert.AreEqual ("*", l.HelpPageIconUrl, "HelpPageIconUrl*");
  418. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-5*");
  419. l.HelpPageIconUrl = null;
  420. Assert.AreEqual (String.Empty, l.HelpPageIconUrl, "HelpPageIconUrl");
  421. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-5");
  422. l.HelpPageText = "*";
  423. Assert.AreEqual ("*", l.HelpPageText, "HelpPageText*");
  424. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-6*");
  425. l.HelpPageText = null;
  426. Assert.AreEqual (String.Empty, l.HelpPageText, "HelpPageText");
  427. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-6");
  428. l.HelpPageUrl = "*";
  429. Assert.AreEqual ("*", l.HelpPageUrl, "HelpPageUrl*");
  430. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-7*");
  431. l.HelpPageUrl = null;
  432. Assert.AreEqual (String.Empty, l.HelpPageUrl, "HelpPageUrl");
  433. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-7");
  434. l.InstructionText = "*";
  435. Assert.AreEqual ("*", l.InstructionText, "InstructionText*");
  436. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-8*");
  437. l.InstructionText = null;
  438. Assert.AreEqual (String.Empty, l.InstructionText, "InstructionText");
  439. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-8");
  440. l.LoginButtonImageUrl = "*";
  441. Assert.AreEqual ("*", l.LoginButtonImageUrl, "LoginButtonImageUrl*");
  442. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-9*");
  443. l.LoginButtonImageUrl = null;
  444. Assert.AreEqual (String.Empty, l.LoginButtonImageUrl, "LoginButtonImageUrl");
  445. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-9");
  446. l.LoginButtonText = "*";
  447. Assert.AreEqual ("*", l.LoginButtonText, "LoginButtonText*");
  448. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-10*");
  449. l.LoginButtonText = null;
  450. Assert.AreEqual ("Log In", l.LoginButtonText, "LoginButtonText");
  451. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-10");
  452. l.MembershipProvider = "*";
  453. Assert.AreEqual ("*", l.MembershipProvider, "MembershipProvider*");
  454. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-11*");
  455. l.MembershipProvider = null;
  456. Assert.AreEqual (String.Empty, l.MembershipProvider, "MembershipProvider");
  457. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-11");
  458. l.PasswordLabelText = "*";
  459. Assert.AreEqual ("*", l.PasswordLabelText, "PasswordLabelText*");
  460. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-12*");
  461. l.PasswordLabelText = null;
  462. Assert.AreEqual ("Password:", l.PasswordLabelText, "PasswordLabelText");
  463. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-12");
  464. l.PasswordRecoveryIconUrl = "*";
  465. Assert.AreEqual ("*", l.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl*");
  466. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-13*");
  467. l.PasswordRecoveryIconUrl = null;
  468. Assert.AreEqual (String.Empty, l.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl");
  469. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-13");
  470. l.PasswordRecoveryText = "*";
  471. Assert.AreEqual ("*", l.PasswordRecoveryText, "PasswordRecoveryText*");
  472. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-14*");
  473. l.PasswordRecoveryText = null;
  474. Assert.AreEqual (String.Empty, l.PasswordRecoveryText, "PasswordRecoveryText");
  475. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-14");
  476. l.PasswordRecoveryUrl = "*";
  477. Assert.AreEqual ("*", l.PasswordRecoveryUrl, "PasswordRecoveryUrl*");
  478. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-15*");
  479. l.PasswordRecoveryUrl = null;
  480. Assert.AreEqual (String.Empty, l.PasswordRecoveryUrl, "PasswordRecoveryUrl");
  481. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-15");
  482. l.PasswordRequiredErrorMessage = "*";
  483. Assert.AreEqual ("*", l.PasswordRequiredErrorMessage, "PasswordRequiredErrorMessage*");
  484. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-16*");
  485. l.PasswordRequiredErrorMessage = null;
  486. Assert.AreEqual ("Password is required.", l.PasswordRequiredErrorMessage, "PasswordRequiredErrorMessage");
  487. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-16");
  488. l.RememberMeText = "*";
  489. Assert.AreEqual ("*", l.RememberMeText, "RememberMeText*");
  490. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-17*");
  491. l.RememberMeText = null;
  492. Assert.AreEqual ("Remember me next time.", l.RememberMeText, "RememberMeText");
  493. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-17");
  494. l.TitleText = "*";
  495. Assert.AreEqual ("*", l.TitleText, "TitleText*");
  496. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-18*");
  497. l.TitleText = null;
  498. Assert.AreEqual ("Log In", l.TitleText, "TitleText");
  499. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-18");
  500. l.UserName = "*";
  501. Assert.AreEqual ("*", l.UserName, "UserName*");
  502. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-19*");
  503. l.UserName = null;
  504. Assert.AreEqual (String.Empty, l.UserName, "UserName");
  505. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-19");
  506. l.UserNameLabelText = "*";
  507. Assert.AreEqual ("*", l.UserNameLabelText, "UserNameLabelText*");
  508. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-20*");
  509. l.UserNameLabelText = null;
  510. Assert.AreEqual ("User Name:", l.UserNameLabelText, "UserNameLabelText");
  511. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-20");
  512. l.UserNameRequiredErrorMessage = "*";
  513. Assert.AreEqual ("*", l.UserNameRequiredErrorMessage, "UserNameRequiredErrorMessage*");
  514. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count-21*");
  515. l.UserNameRequiredErrorMessage = null;
  516. Assert.AreEqual ("User Name is required.", l.UserNameRequiredErrorMessage, "UserNameRequiredErrorMessage");
  517. Assert.AreEqual (0, l.StateBag.Count, "ViewState.Count-21");
  518. }
  519. [Test]
  520. public void BorderPadding ()
  521. {
  522. TestLogin l = new TestLogin ();
  523. l.BorderPadding = Int32.MaxValue;
  524. Assert.AreEqual (Int32.MaxValue, l.BorderPadding, "BorderPadding");
  525. l.BorderPadding = 1;
  526. Assert.AreEqual (1, l.BorderPadding, "BorderPadding");
  527. l.BorderPadding = 0;
  528. Assert.AreEqual (0, l.BorderPadding, "BorderPadding");
  529. l.BorderPadding = -1;
  530. Assert.AreEqual (-1, l.BorderPadding, "BorderPadding");
  531. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  532. }
  533. [Test]
  534. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  535. public void BorderPadding_Negative ()
  536. {
  537. TestLogin l = new TestLogin ();
  538. l.BorderPadding = Int32.MinValue;
  539. Assert.AreEqual (1, l.BorderPadding, "BorderPadding");
  540. }
  541. [Test]
  542. public void FailureAction_All ()
  543. {
  544. TestLogin l = new TestLogin ();
  545. foreach (LoginFailureAction lfa in Enum.GetValues (typeof (LoginFailureAction))) {
  546. l.FailureAction = lfa;
  547. }
  548. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  549. }
  550. [Test]
  551. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  552. public void FailureAction_Invalid ()
  553. {
  554. TestLogin l = new TestLogin ();
  555. l.FailureAction = (LoginFailureAction) Int32.MinValue;
  556. }
  557. [Test]
  558. public void LayoutTemplate()
  559. {
  560. TestLogin l = new TestLogin();
  561. l.LayoutTemplate = new LayoutTemplate();
  562. l.DoEnsureChildControls();
  563. Assert.IsNotNull(l.FindControl("Template"), "LoginTemplate");
  564. Assert.IsNotNull(l.FindControl("UserName"), "UserName");
  565. }
  566. [Test]
  567. [ExpectedException(typeof(HttpException))]
  568. public void LayoutTemplateException ()
  569. {
  570. TestLogin l = new TestLogin ();
  571. l.LayoutTemplate = new LoginTemplate();
  572. l.DoEnsureChildControls();
  573. }
  574. [Test]
  575. public void LoginButtonType_All ()
  576. {
  577. TestLogin l = new TestLogin ();
  578. foreach (ButtonType bt in Enum.GetValues (typeof (ButtonType))) {
  579. l.LoginButtonType = bt;
  580. }
  581. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  582. }
  583. [Test]
  584. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  585. public void LoginButtonType_Invalid ()
  586. {
  587. TestLogin l = new TestLogin ();
  588. l.LoginButtonType = (ButtonType)Int32.MinValue;
  589. }
  590. [Test]
  591. public void Orientation_All ()
  592. {
  593. TestLogin l = new TestLogin ();
  594. foreach (Orientation o in Enum.GetValues (typeof (Orientation))) {
  595. l.Orientation = o;
  596. }
  597. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  598. }
  599. [Test]
  600. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  601. public void Orientation_Invalid ()
  602. {
  603. TestLogin l = new TestLogin ();
  604. l.Orientation = (Orientation)Int32.MinValue;
  605. }
  606. [Test]
  607. public void TextLayout_All ()
  608. {
  609. TestLogin l = new TestLogin ();
  610. foreach (LoginTextLayout ltl in Enum.GetValues (typeof (LoginTextLayout))) {
  611. l.TextLayout = ltl;
  612. }
  613. Assert.AreEqual (1, l.StateBag.Count, "ViewState.Count");
  614. }
  615. [Test]
  616. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  617. public void TextLayout_Invalid ()
  618. {
  619. TestLogin l = new TestLogin ();
  620. l.TextLayout = (LoginTextLayout)Int32.MinValue;
  621. }
  622. [Test]
  623. public void SaveViewState ()
  624. {
  625. TestLogin l = new TestLogin ();
  626. l.TrackState ();
  627. Assert.IsNull (l.SaveState (), "Empty");
  628. l.BorderPadding = 2;
  629. object[] vs = (object[])l.SaveState ();
  630. Assert.AreEqual (10, vs.Length, "Size");
  631. l.CreateUserIconUrl = String.Empty;
  632. l.CreateUserText = String.Empty;
  633. l.DestinationPageUrl = String.Empty;
  634. l.DisplayRememberMe = true;
  635. l.FailureAction = LoginFailureAction.Refresh;
  636. l.FailureText = "Your login attempt was not successful. Please try again.";
  637. l.HelpPageIconUrl = String.Empty;
  638. l.HelpPageText = String.Empty;
  639. l.HelpPageUrl = String.Empty;
  640. l.InstructionText = String.Empty;
  641. l.LayoutTemplate = null;
  642. l.LoginButtonImageUrl = String.Empty;
  643. l.LoginButtonText = "Log In";
  644. l.LoginButtonType = ButtonType.Button;
  645. l.MembershipProvider = String.Empty;
  646. l.Orientation = Orientation.Vertical;
  647. // note: Password is read-only
  648. l.PasswordLabelText = "Password:";
  649. l.PasswordRecoveryIconUrl = String.Empty;
  650. l.PasswordRecoveryText = String.Empty;
  651. l.PasswordRecoveryUrl = String.Empty;
  652. l.PasswordRequiredErrorMessage = "Password is required.";
  653. l.RememberMeSet = false;
  654. l.RememberMeText = "Remember me next time.";
  655. l.TextLayout = LoginTextLayout.TextOnLeft;
  656. l.TitleText = "Log In";
  657. l.UserName = String.Empty;
  658. l.UserNameLabelText = "User Name:";
  659. l.UserNameRequiredErrorMessage = "User Name is required.";
  660. l.VisibleWhenLoggedIn = true;
  661. vs = (object[])l.SaveState ();
  662. // the viewstate is all null except the first element
  663. Assert.IsNotNull (vs[0], "NotEmpty-0");
  664. for (int i = 1; i < vs.Length; i++)
  665. Assert.IsNull (vs[i], "Empty-" + i);
  666. l.CheckBoxStyle.HorizontalAlign = HorizontalAlign.Justify;
  667. vs = (object[])l.SaveState ();
  668. Assert.IsNotNull (vs[7], "NotEmpty-7");
  669. l.FailureTextStyle.HorizontalAlign = HorizontalAlign.Justify;
  670. vs = (object[])l.SaveState ();
  671. Assert.IsNotNull (vs[8], "NotEmpty-8");
  672. l.HyperLinkStyle.HorizontalAlign = HorizontalAlign.Justify;
  673. vs = (object[])l.SaveState ();
  674. Assert.IsNotNull (vs[4], "NotEmpty-4");
  675. l.InstructionTextStyle.HorizontalAlign = HorizontalAlign.Justify;
  676. vs = (object[])l.SaveState ();
  677. Assert.IsNotNull (vs[5], "NotEmpty-5");
  678. l.LabelStyle.HorizontalAlign = HorizontalAlign.Justify;
  679. vs = (object[])l.SaveState ();
  680. Assert.IsNotNull (vs[2], "NotEmpty-2");
  681. l.LoginButtonStyle.BorderStyle = BorderStyle.Double;
  682. vs = (object[])l.SaveState ();
  683. Assert.IsNotNull (vs[1], "NotEmpty-1");
  684. l.TextBoxStyle.BorderStyle = BorderStyle.Double;
  685. vs = (object[])l.SaveState ();
  686. Assert.IsNotNull (vs[3], "NotEmpty-3");
  687. l.TitleTextStyle.HorizontalAlign = HorizontalAlign.Justify;
  688. vs = (object[])l.SaveState ();
  689. Assert.IsNotNull (vs[6], "NotEmpty-6");
  690. l.ValidatorTextStyle.BorderStyle = BorderStyle.Double;
  691. vs = (object[])l.SaveState ();
  692. Assert.IsNotNull (vs[9], "NotEmpty-9");
  693. }
  694. [Test]
  695. [Category ("NunitWeb")]
  696. public void OnBubbleEvent ()
  697. {
  698. WebTest t = new WebTest (PageInvoker.CreateOnPreInit (_OnBubbleEvent));
  699. string html = t.Run ();
  700. }
  701. public static void DoOnBubbleEvent(Page p, string cmdname)
  702. {
  703. TestLogin l = new TestLogin ();
  704. l.Page = p;
  705. l.Page.Validate ();
  706. l.MembershipProvider = "FakeProvider";
  707. Button b = (Button)l.FindControl ("LoginButton");
  708. Assert.IsNotNull (b, "LoginButton");
  709. CommandEventArgs cea = new CommandEventArgs (cmdname, null);
  710. l.DoBubbleEvent (b, cea);
  711. Assert.IsTrue (l.OnLoggingInCalled, "OnLoggingIn");
  712. Assert.IsFalse (l.Cancel, "Cancel");
  713. Assert.IsTrue (l.OnAuthenticateCalled, "OnAuthenticate");
  714. Assert.IsFalse (l.Authenticated, "Authenticated");
  715. Assert.IsTrue (l.OnLoginErrorCalled, "OnLoginError");
  716. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  717. }
  718. public static void _OnBubbleEvent(Page p)
  719. {
  720. DoOnBubbleEvent(p, "Login");
  721. }
  722. [Test]
  723. [Category ("NunitWeb")]
  724. public void OnBubbleEventCaseSensitivity ()
  725. {
  726. WebTest t = new WebTest (PageInvoker.CreateOnPreInit (_OnBubbleEventCaseSensitivity));
  727. string html = t.Run ();
  728. }
  729. public static void _OnBubbleEventCaseSensitivity(Page p)
  730. {
  731. DoOnBubbleEvent(p, "login");
  732. }
  733. [Test]
  734. public void OnBubbleEvent_Cancel_OnLoggingIn ()
  735. {
  736. TestLogin l = new TestLogin ();
  737. l.Page = new Page ();
  738. l.Page.Validate ();
  739. Button b = (Button)l.FindControl ("LoginButton");
  740. Assert.IsNotNull (b, "LoginButton");
  741. CommandEventArgs cea = new CommandEventArgs ("Login", null);
  742. l.Cancel = true;
  743. l.DoBubbleEvent (b, cea);
  744. Assert.IsTrue (l.OnLoggingInCalled, "OnLoggingIn");
  745. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  746. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  747. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  748. }
  749. [Test]
  750. public void OnBubbleEvent_Authenticated_OnAuthenticate ()
  751. {
  752. WebTest t = new WebTest (PageInvoker.CreateOnPreInit (_OnBubbleEvent_Authenticated_OnAuthenticate));
  753. string html = t.Run ();
  754. }
  755. public static void _OnBubbleEvent_Authenticated_OnAuthenticate (Page p)
  756. {
  757. TestLogin l = new TestLogin ();
  758. l.Page = p;
  759. l.Page.Validate ();
  760. Button b = (Button) l.FindControl ("LoginButton");
  761. Assert.IsNotNull (b, "LoginButton");
  762. CommandEventArgs cea = new CommandEventArgs ("Login", null);
  763. l.UserName = "me";
  764. l.Authenticated = true;
  765. l.MembershipProvider = "FakeProvider";
  766. l.Authenticate += new AuthenticateEventHandler(l_Authenticate);
  767. try {
  768. l.DoBubbleEvent (b, cea);
  769. }
  770. catch (NullReferenceException) {
  771. // ms
  772. }
  773. catch (HttpException) {
  774. // no context is available
  775. }
  776. }
  777. public static void l_Authenticate (object sender, AuthenticateEventArgs e)
  778. {
  779. if (e.Authenticated == true) {
  780. TestLogin l = (TestLogin) sender;
  781. l.Authenticated = false;
  782. }
  783. else
  784. Assert.Fail ("Login failed: l_Authenticate");
  785. }
  786. private void OnLoggingIn (bool cancel)
  787. {
  788. TestLogin l = new TestLogin ();
  789. LoginCancelEventArgs lcea = new LoginCancelEventArgs (cancel);
  790. l.DoLoggingIn (lcea);
  791. Assert.IsFalse (l.OnBubbleEventCalled, "OnBubbleEvent");
  792. Assert.IsFalse (l.OnLoggingInCalled, "OnLoggingIn");
  793. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  794. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  795. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  796. }
  797. [Test]
  798. public void OnLoggingIn_False ()
  799. {
  800. OnLoggingIn (false);
  801. }
  802. [Test]
  803. public void OnLoggingIn_True ()
  804. {
  805. OnLoggingIn (true);
  806. }
  807. private void OnAuthenticate (bool authenticate)
  808. {
  809. TestLogin l = new TestLogin ();
  810. AuthenticateEventArgs aea = new AuthenticateEventArgs (authenticate);
  811. l.DoAuthenticate (aea);
  812. Assert.IsFalse (l.OnBubbleEventCalled, "OnBubbleEvent");
  813. Assert.IsFalse (l.OnLoggingInCalled, "OnLoggingIn");
  814. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  815. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  816. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  817. }
  818. [Test]
  819. public void OnAuthenticate_False ()
  820. {
  821. OnAuthenticate (false);
  822. }
  823. [Test]
  824. public void OnAuthenticate_True ()
  825. {
  826. OnAuthenticate (true);
  827. }
  828. [Test]
  829. public void OnLoginError ()
  830. {
  831. TestLogin l = new TestLogin ();
  832. l.DoLoginError (EventArgs.Empty);
  833. Assert.IsFalse (l.OnBubbleEventCalled, "OnBubbleEvent");
  834. Assert.IsFalse (l.OnLoggingInCalled, "OnLoggingIn");
  835. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  836. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  837. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  838. }
  839. [Test]
  840. public void OnLoggedIn ()
  841. {
  842. TestLogin l = new TestLogin ();
  843. l.DoLoggedIn (EventArgs.Empty);
  844. Assert.IsFalse (l.OnBubbleEventCalled, "OnBubbleEvent");
  845. Assert.IsFalse (l.OnLoggingInCalled, "OnLoggingIn");
  846. Assert.IsFalse (l.OnAuthenticateCalled, "OnAuthenticate");
  847. Assert.IsFalse (l.OnLoginErrorCalled, "OnLoginError");
  848. Assert.IsFalse (l.OnLoggedInCalled, "OnLoggedIn");
  849. }
  850. [Test]
  851. [Category ("NunitWeb")]
  852. public void PostBackFireEvent_1 ()
  853. {
  854. WebTest t = new WebTest ("NoEventValidation.aspx");
  855. t.Invoker = PageInvoker.CreateOnInit (PostBackFireEvent_Init);
  856. string html = t.Run ();
  857. FormRequest fr = new FormRequest (t.Response, "form1");
  858. fr.Controls.Add ("__EVENTTARGET");
  859. fr.Controls.Add ("__EVENTARGUMENT");
  860. fr.Controls.Add (GetDecoratedId (html, "UserName")); //%24
  861. fr.Controls.Add (GetDecoratedId (html, "Password"));
  862. fr.Controls.Add (GetDecoratedId (html, "LoginButton"));
  863. fr.Controls ["__EVENTTARGET"].Value = "";
  864. fr.Controls ["__EVENTARGUMENT"].Value = "";
  865. fr.Controls ["Login1$UserName"].Value = "yonik";
  866. fr.Controls ["Login1$Password"].Value = "123456";
  867. fr.Controls ["Login1$LoginButton"].Value = "Log In";
  868. t.Request = fr;
  869. t.Run ();
  870. ArrayList eventlist = t.UserData as ArrayList;
  871. if (eventlist == null)
  872. Assert.Fail ("User data does not been created fail");
  873. Assert.AreEqual ("LoggingIn", eventlist [0], "#1");
  874. Assert.AreEqual ("Authenticate", eventlist [1], "#2");
  875. Assert.AreEqual ("LoginError", eventlist [2], "#3");
  876. }
  877. [Test]
  878. [Category ("NunitWeb")]
  879. public void PostBackFireEvent_2 ()
  880. {
  881. WebTest t = new WebTest ("NoEventValidation.aspx");
  882. t.Invoker = PageInvoker.CreateOnInit (PostBackFireEvent_Init_2);
  883. string html = t.Run ();
  884. FormRequest fr = new FormRequest (t.Response, "form1");
  885. fr.Controls.Add ("__EVENTTARGET");
  886. fr.Controls.Add ("__EVENTARGUMENT");
  887. fr.Controls.Add (GetDecoratedId (html, "UserName")); //%24
  888. fr.Controls.Add (GetDecoratedId (html, "Password"));
  889. fr.Controls.Add (GetDecoratedId (html, "LoginButton"));
  890. fr.Controls ["__EVENTTARGET"].Value = "";
  891. fr.Controls ["__EVENTARGUMENT"].Value = "";
  892. fr.Controls ["Login1$UserName"].Value = "yonik";
  893. fr.Controls ["Login1$Password"].Value = "123456";
  894. fr.Controls ["Login1$LoginButton"].Value = "Log In";
  895. t.Request = fr;
  896. t.Run ();
  897. ArrayList eventlist = t.UserData as ArrayList;
  898. if (eventlist == null)
  899. Assert.Fail ("User data does not been created fail");
  900. Assert.AreEqual ("LoggingIn", eventlist [0], "#1");
  901. Assert.AreEqual ("LoggedIn", eventlist [1], "#2");
  902. }
  903. public static void PostBackFireEvent_Init_2 (Page p)
  904. {
  905. Login l = new Login ();
  906. l.LoggedIn += new EventHandler (l_LoggedIn);
  907. l.LoggingIn += new LoginCancelEventHandler (l_LoggingIn);
  908. l.ID = "Login1";
  909. l.MembershipProvider = "FakeProvider";
  910. p.Controls.Add (l);
  911. p.Validate ();
  912. }
  913. public static void PostBackFireEvent_Init (Page p)
  914. {
  915. Login l = new Login ();
  916. l.Authenticate += new AuthenticateEventHandler (Authenticate_Event);
  917. l.LoggedIn += new EventHandler (l_LoggedIn);
  918. l.LoggingIn += new LoginCancelEventHandler (l_LoggingIn);
  919. l.LoginError += new EventHandler (l_LoginError);
  920. l.ID = "Login1";
  921. l.MembershipProvider = "FakeProvider";
  922. p.Controls.Add (l);
  923. p.Validate ();
  924. }
  925. static void l_LoginError (object sender, EventArgs e)
  926. {
  927. if (WebTest.CurrentTest.UserData == null) {
  928. ArrayList list = new ArrayList ();
  929. list.Add ("LoginError");
  930. WebTest.CurrentTest.UserData = list;
  931. }
  932. else {
  933. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  934. if (list == null)
  935. throw new NullReferenceException ();
  936. list.Add ("LoginError");
  937. WebTest.CurrentTest.UserData = list;
  938. }
  939. }
  940. static void l_LoggingIn (object sender, LoginCancelEventArgs e)
  941. {
  942. if (WebTest.CurrentTest.UserData == null) {
  943. ArrayList list = new ArrayList ();
  944. list.Add ("LoggingIn");
  945. WebTest.CurrentTest.UserData = list;
  946. }
  947. else {
  948. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  949. if (list == null)
  950. throw new NullReferenceException ();
  951. list.Add ("LoggingIn");
  952. WebTest.CurrentTest.UserData = list;
  953. }
  954. }
  955. static void l_LoggedIn (object sender, EventArgs e)
  956. {
  957. if (WebTest.CurrentTest.UserData == null) {
  958. ArrayList list = new ArrayList ();
  959. list.Add ("LoggedIn");
  960. WebTest.CurrentTest.UserData = list;
  961. }
  962. else {
  963. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  964. if (list == null)
  965. throw new NullReferenceException ();
  966. list.Add ("LoggedIn");
  967. WebTest.CurrentTest.UserData = list;
  968. }
  969. }
  970. public static void Authenticate_Event (object sender, AuthenticateEventArgs e)
  971. {
  972. if (WebTest.CurrentTest.UserData == null) {
  973. ArrayList list = new ArrayList ();
  974. list.Add ("Authenticate");
  975. WebTest.CurrentTest.UserData = list;
  976. }
  977. else {
  978. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  979. if (list == null)
  980. throw new NullReferenceException ();
  981. list.Add ("Authenticate");
  982. WebTest.CurrentTest.UserData = list;
  983. }
  984. }
  985. [TestFixtureTearDown]
  986. public void Teardown ()
  987. {
  988. WebTest.Unload ();
  989. }
  990. private string GetDecoratedId (string html, string id)
  991. {
  992. Regex reg = new Regex ("name=\".*[\\$\\:]" + id + "\"");
  993. Match match = reg.Match (html);
  994. string fixedId = match.Value;
  995. if (fixedId.Length > 0)
  996. fixedId = fixedId.Substring (fixedId.IndexOf ("\""), fixedId.Length - fixedId.IndexOf ("\"")).Trim ('\"');
  997. return fixedId;
  998. }
  999. }
  1000. }
  1001. #endif