PageTest.cs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. //
  2. // Tests for System.Web.UI.Page
  3. //
  4. // Authors:
  5. // Peter Dennis Bartok ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. // Yoni Klain <[email protected]>
  8. //
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using NUnit.Framework;
  31. using System;
  32. using System.IO;
  33. using System.Threading;
  34. using System.Security.Principal;
  35. using System.Web;
  36. using System.Web.UI;
  37. using MonoTests.SystemWeb.Framework;
  38. using MonoTests.stand_alone.WebHarness;
  39. using System.Web.UI.WebControls;
  40. using System.Web.UI.HtmlControls;
  41. using System.Collections;
  42. using System.Net;
  43. namespace MonoTests.System.Web.UI {
  44. class TestPage : Page {
  45. private HttpContext ctx;
  46. // don't call base class (so _context is never set to a non-null value)
  47. protected override HttpContext Context {
  48. get {
  49. if (ctx == null) {
  50. ctx = new HttpContext (null);
  51. ctx.User = new GenericPrincipal (new GenericIdentity ("me"), null);
  52. }
  53. return ctx;
  54. }
  55. }
  56. #if NET_2_0
  57. public new bool AsyncMode {
  58. get { return base.AsyncMode; }
  59. set { base.AsyncMode = value; }
  60. }
  61. public new object GetWrappedFileDependencies(string[] virtualFileDependencies)
  62. {
  63. return base.GetWrappedFileDependencies(virtualFileDependencies);
  64. }
  65. public new void InitOutputCache (OutputCacheParameters cacheSettings)
  66. {
  67. base.InitOutputCache (cacheSettings);
  68. }
  69. public new string UniqueFilePathSuffix {
  70. get { return base.UniqueFilePathSuffix; }
  71. }
  72. public new char IdSeparator {
  73. get {
  74. return base.IdSeparator;
  75. }
  76. }
  77. #endif
  78. }
  79. class TestPage2 : Page {
  80. private HttpContext ctx;
  81. // don't call base class (so _context is never set to a non-null value)
  82. protected override HttpContext Context {
  83. get {
  84. if (ctx == null) {
  85. ctx = new HttpContext (
  86. new HttpRequest (String.Empty, "http://www.mono-project.com", String.Empty),
  87. new HttpResponse (new StringWriter ())
  88. );
  89. }
  90. return ctx;
  91. }
  92. }
  93. public HttpContext HttpContext {
  94. get { return Context; }
  95. }
  96. }
  97. [TestFixture]
  98. public class PageTest {
  99. [TestFixtureSetUp]
  100. public void SetUpTest ()
  101. {
  102. #if DOT_NET
  103. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.PageCultureTest.aspx", "PageCultureTest.aspx");
  104. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.PageLifecycleTest.aspx", "PageLifecycleTest.aspx");
  105. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.PageValidationTest.aspx", "PageValidationTest.aspx");
  106. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.AsyncPage.aspx", "AsyncPage.aspx");
  107. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.AsyncPage.aspx", "AsyncPage.aspx");
  108. #else
  109. WebTest.CopyResource (GetType (), "PageCultureTest.aspx", "PageCultureTest.aspx");
  110. WebTest.CopyResource (GetType (), "PageLifecycleTest.aspx", "PageLifecycleTest.aspx");
  111. WebTest.CopyResource (GetType (), "PageValidationTest.aspx", "PageValidationTest.aspx");
  112. WebTest.CopyResource (GetType (), "AsyncPage.aspx", "AsyncPage.aspx");
  113. WebTest.CopyResource (GetType (), "AsyncPage.aspx", "AsyncPage.aspx");
  114. #endif
  115. }
  116. [Test]
  117. [ExpectedException (typeof(HttpException))]
  118. public void RequestExceptionTest ()
  119. {
  120. Page p;
  121. HttpRequest r;
  122. p = new Page ();
  123. r = p.Request;
  124. }
  125. [Test]
  126. #if NET_2_0
  127. [Category ("NotDotNet")] // page.User throw NRE in 2.0 RC
  128. #endif
  129. public void User_OverridenContext ()
  130. {
  131. TestPage page = new TestPage ();
  132. Assert.AreEqual ("me", page.User.Identity.Name, "User");
  133. }
  134. [Test]
  135. [ExpectedException (typeof (HttpException))]
  136. public void Request_OverridenContext ()
  137. {
  138. TestPage2 page = new TestPage2 ();
  139. Assert.IsNotNull (page.Request, "Request");
  140. // it doesn't seems to access the context via the virtual property
  141. }
  142. [Test]
  143. public void Request_OverridenContext_Indirect ()
  144. {
  145. TestPage2 page = new TestPage2 ();
  146. Assert.IsNotNull (page.HttpContext.Request, "Request");
  147. }
  148. #if NET_2_0
  149. [Test]
  150. [Category ("NunitWeb")]
  151. public void PageHeaderOnPreInit ()
  152. {
  153. PageDelegate pd = new PageDelegate (Page_OnPreInit);
  154. WebTest t = new WebTest (PageInvoker.CreateOnPreInit (pd));
  155. string html = t.Run ();
  156. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  157. string origHtml = @" <head id=""Head1""><title>
  158. PreInit
  159. </title></head>";
  160. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderInit");
  161. }
  162. public static void Page_OnPreInit (Page p)
  163. {
  164. Assert.AreEqual (null, p.Header, "HeaderOnPreInit");
  165. p.Title = "PreInit";
  166. }
  167. [Test]
  168. [Category ("NunitWeb")]
  169. public void PageHeaderInit ()
  170. {
  171. PageDelegate pd = new PageDelegate (CheckHeader);
  172. WebTest t = new WebTest (PageInvoker.CreateOnInit (pd));
  173. string html = t.Run ();
  174. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  175. string origHtml = @" <head id=""Head1""><title>
  176. Test
  177. </title></head>";
  178. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderInit");
  179. }
  180. [Test]
  181. [Category ("NunitWeb")]
  182. public void PageHeaderInitComplete ()
  183. {
  184. WebTest t = new WebTest ();
  185. PageDelegates pd = new PageDelegates ();
  186. pd.InitComplete = CheckHeader;
  187. t.Invoker = new PageInvoker (pd);
  188. string html = t.Run ();
  189. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  190. string origHtml = @" <head id=""Head1""><title>
  191. Test
  192. </title></head>";
  193. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderInitComplete");
  194. }
  195. [Test]
  196. [Category ("NunitWeb")]
  197. public void PageHeaderPreLoad ()
  198. {
  199. WebTest t = new WebTest ();
  200. PageDelegates pd = new PageDelegates ();
  201. pd.PreLoad = CheckHeader;
  202. t.Invoker = new PageInvoker (pd);
  203. string html = t.Run ();
  204. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  205. string origHtml = @" <head id=""Head1""><title>
  206. Test
  207. </title></head>";
  208. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderPreLoad");
  209. }
  210. [Test]
  211. [Category ("NunitWeb")]
  212. public void PageHeaderLoad ()
  213. {
  214. PageDelegate pd = new PageDelegate (CheckHeader);
  215. WebTest t = new WebTest (PageInvoker.CreateOnLoad (pd));
  216. string html = t.Run ();
  217. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  218. string origHtml = @" <head id=""Head1""><title>
  219. Test
  220. </title></head>";
  221. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderLoad");
  222. }
  223. [Test]
  224. [Category ("NunitWeb")]
  225. public void PageHeaderLoadComplete ()
  226. {
  227. WebTest t = new WebTest ();
  228. PageDelegates pd = new PageDelegates ();
  229. pd.LoadComplete = CheckHeader;
  230. t.Invoker = new PageInvoker (pd);
  231. string html = t.Run ();
  232. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  233. string origHtml = @" <head id=""Head1""><title>
  234. Test
  235. </title></head>";
  236. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderLoadComplete");
  237. }
  238. [Test]
  239. [Category ("NunitWeb")]
  240. public void PageHeaderPreRender ()
  241. {
  242. WebTest t = new WebTest ();
  243. PageDelegates pd = new PageDelegates ();
  244. pd.PreRender = CheckHeader;
  245. t.Invoker = new PageInvoker (pd);
  246. string html = t.Run ();
  247. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  248. string origHtml = @" <head id=""Head1""><title>
  249. Test
  250. </title></head>";
  251. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderPreRender");
  252. }
  253. [Test]
  254. [Category ("NunitWeb")]
  255. public void PageHeaderPreRenderComplete ()
  256. {
  257. WebTest t = new WebTest ();
  258. PageDelegates pd = new PageDelegates ();
  259. pd.PreRenderComplete = CheckHeader;
  260. t.Invoker = new PageInvoker (pd);
  261. string html = t.Run ();
  262. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  263. string origHtml = @" <head id=""Head1""><title>
  264. Test
  265. </title></head>";
  266. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderPreRenderComplete");
  267. }
  268. public static void CheckHeader (Page p)
  269. {
  270. Assert.AreEqual ("Untitled Page", p.Title, "CheckHeader#1");
  271. Assert.AreEqual ("Untitled Page", p.Header.Title, "CheckHeader#2");
  272. p.Title = "Test0";
  273. Assert.AreEqual ("Test0", p.Header.Title, "CheckHeader#3");
  274. p.Header.Title = "Test";
  275. Assert.AreEqual ("Test", p.Title, "CheckHeader#4");
  276. }
  277. #endif
  278. #if NET_2_0
  279. [Test]
  280. [Category ("NunitWeb")]
  281. public void Page_ValidationGroup () {
  282. new WebTest (PageInvoker.CreateOnLoad (Page_ValidationGroup_Load)).Run ();
  283. }
  284. public static void Page_ValidationGroup_Load (Page page) {
  285. TextBox textbox;
  286. BaseValidator val;
  287. textbox = new TextBox ();
  288. textbox.ID = "T1";
  289. textbox.ValidationGroup = "VG1";
  290. page.Form.Controls.Add (textbox);
  291. val = new RequiredFieldValidator ();
  292. val.ControlToValidate = "T1";
  293. val.ValidationGroup = "VG1";
  294. page.Form.Controls.Add (val);
  295. textbox = new TextBox ();
  296. textbox.ID = "T2";
  297. textbox.ValidationGroup = "VG2";
  298. page.Form.Controls.Add (textbox);
  299. val = new RequiredFieldValidator ();
  300. val.ControlToValidate = "T2";
  301. val.ValidationGroup = "VG2";
  302. page.Form.Controls.Add (val);
  303. textbox = new TextBox ();
  304. textbox.ID = "T3";
  305. page.Form.Controls.Add (textbox);
  306. val = new RequiredFieldValidator ();
  307. val.ControlToValidate = "T3";
  308. page.Form.Controls.Add (val);
  309. Assert.AreEqual (3, page.Validators.Count, "Page_ValidationGroup#1");
  310. Assert.AreEqual (1, page.GetValidators ("").Count, "Page_ValidationGroup#2");
  311. Assert.AreEqual (1, page.GetValidators (null).Count, "Page_ValidationGroup#3");
  312. Assert.AreEqual (0, page.GetValidators ("Fake").Count, "Page_ValidationGroup#4");
  313. Assert.AreEqual (1, page.GetValidators ("VG1").Count, "Page_ValidationGroup#5");
  314. Assert.AreEqual (1, page.GetValidators ("VG2").Count, "Page_ValidationGroup#6");
  315. }
  316. #endif
  317. #if NET_2_0
  318. // This test are testing validation fixture using RequiredFieldValidator for example
  319. [Test]
  320. [Category ("NunitWeb")]
  321. public void Page_ValidationCollection ()
  322. {
  323. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ValidationCollectionload));
  324. t.Run ();
  325. }
  326. public static void ValidationCollectionload (Page p)
  327. {
  328. RequiredFieldValidator validator = new RequiredFieldValidator ();
  329. validator.ID = "v";
  330. RequiredFieldValidator validator1 = new RequiredFieldValidator ();
  331. validator.ID = "v1";
  332. p.Controls.Add (validator);
  333. p.Controls.Add (validator1);
  334. Assert.AreEqual (2, p.Validators.Count, "Validators collection count fail");
  335. Assert.AreEqual (true, p.Validators[0].IsValid, "Validators collection value#1 fail");
  336. Assert.AreEqual (true, p.Validators[1].IsValid, "Validators collection value#2 fail");
  337. }
  338. [Test]
  339. [Category ("NunitWeb")]
  340. public void Page_ValidatorTest1 ()
  341. {
  342. WebTest t = new WebTest ("PageValidationTest.aspx");
  343. string PageRenderHtml = t.Run ();
  344. FormRequest fr = new FormRequest (t.Response, "form1");
  345. fr.Controls.Add ("TextBox1");
  346. PageDelegates pd = new PageDelegates ();
  347. pd.PreRender = ValidatorTest1PreRender;
  348. t.Invoker = new PageInvoker (pd);
  349. fr.Controls["TextBox1"].Value = "";
  350. t.Request = fr;
  351. PageRenderHtml = t.Run ();
  352. Assert.IsNotNull(t.UserData, "Validate server side method not raised fail");
  353. ArrayList list = t.UserData as ArrayList;
  354. if (list == null)
  355. Assert.Fail ("User data not created fail#1");
  356. Assert.AreEqual (1, list.Count, "Just validate with no validation group must be called fail#1");
  357. Assert.AreEqual ("Validate", list[0].ToString (), "Validate with no validation group must be called fail#1");
  358. }
  359. public static void ValidatorTest1PreRender (Page p)
  360. {
  361. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#1");
  362. Assert.AreEqual (false, p.Validators[0].IsValid, "Specific validator value filed#1");
  363. Assert.AreEqual (false, p.IsValid, "Page validation Failed#1");
  364. }
  365. [Test]
  366. [Category ("NunitWeb")]
  367. public void Page_ValidatorTest2 ()
  368. {
  369. WebTest t = new WebTest ("PageValidationTest.aspx");
  370. string PageRenderHtml = t.Run ();
  371. FormRequest fr = new FormRequest (t.Response, "form1");
  372. fr.Controls.Add ("TextBox1");
  373. PageDelegates pd = new PageDelegates ();
  374. pd.PreRender = ValidatorTest2PreRender;
  375. t.Invoker = new PageInvoker (pd);
  376. fr.Controls["TextBox1"].Value = "test";
  377. t.Request = fr;
  378. PageRenderHtml = t.Run ();
  379. Assert.IsNotNull ( t.UserData, "Validate server side method not raised fail#2");
  380. ArrayList list = t.UserData as ArrayList;
  381. if (list == null)
  382. Assert.Fail ("User data not created fail#2");
  383. Assert.AreEqual (1, list.Count, "Just validate with no validation group must be called fail#2");
  384. Assert.AreEqual ("Validate", list[0].ToString (), "Validate with no validation group must be called fail#2");
  385. }
  386. public static void ValidatorTest2PreRender (Page p)
  387. {
  388. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#2");
  389. Assert.AreEqual (true, p.Validators[0].IsValid, "Specific validator value fail#2");
  390. Assert.AreEqual (true, p.IsValid, "Page validation Fail#2");
  391. }
  392. [Test]
  393. [Category ("NunitWeb")]
  394. public void Page_ValidatorTest3 ()
  395. {
  396. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ValidatorTest3Load));
  397. t.Run ();
  398. }
  399. public static void ValidatorTest3Load (Page p)
  400. {
  401. TextBox tbx = new TextBox ();
  402. tbx.ID = "tbx";
  403. RequiredFieldValidator vld = new RequiredFieldValidator ();
  404. vld.ID = "vld";
  405. vld.ControlToValidate = "tbx";
  406. p.Controls.Add (tbx);
  407. p.Controls.Add (vld);
  408. vld.Validate ();
  409. Assert.AreEqual (false, p.Validators[0].IsValid, "RequiredField result fail #1");
  410. tbx.Text = "test";
  411. vld.Validate ();
  412. Assert.AreEqual (true, p.Validators[0].IsValid, "RequiredField result fail #2");
  413. }
  414. [Test]
  415. [Category ("NunitWeb")]
  416. public void Page_ValidatorTest4 ()
  417. {
  418. WebTest t = new WebTest ("PageValidationTest.aspx");
  419. string PageRenderHtml = t.Run ();
  420. FormRequest fr = new FormRequest (t.Response, "form1");
  421. fr.Controls.Add ("__EVENTTARGET");
  422. fr.Controls.Add ("__EVENTARGUMENT");
  423. fr.Controls.Add ("TextBox1");
  424. fr.Controls.Add ("Button1");
  425. PageDelegates pd = new PageDelegates ();
  426. pd.PreRender = ValidatorTest4PreRender;
  427. t.Invoker = new PageInvoker (pd);
  428. fr.Controls["__EVENTTARGET"].Value = "";
  429. fr.Controls["__EVENTARGUMENT"].Value = "";
  430. fr.Controls["TextBox1"].Value = "";
  431. fr.Controls["Button1"].Value = "Button";
  432. t.Request = fr;
  433. PageRenderHtml = t.Run ();
  434. Assert.IsNotNull (t.UserData, "Validate server side method not raised fail#3");
  435. ArrayList list = t.UserData as ArrayList;
  436. if (list == null)
  437. Assert.Fail ("User data not created fail#3");
  438. Assert.AreEqual (1, list.Count, "Just validate with validation group must be called fail#3");
  439. Assert.AreEqual ("Validate_WithGroup", list[0].ToString (), "Validate with validation group must be called fail#3");
  440. }
  441. public static void ValidatorTest4PreRender (Page p)
  442. {
  443. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#3");
  444. Assert.AreEqual (false, p.Validators[0].IsValid, "Specific validator value filed#3");
  445. Assert.AreEqual (false, p.IsValid, "Page validation Failed#3");
  446. }
  447. [Test]
  448. [Category ("NunitWeb")]
  449. public void Page_ValidatorTest5 ()
  450. {
  451. WebTest t = new WebTest ("PageValidationTest.aspx");
  452. string PageRenderHtml = t.Run ();
  453. FormRequest fr = new FormRequest (t.Response, "form1");
  454. fr.Controls.Add ("__EVENTTARGET");
  455. fr.Controls.Add ("__EVENTARGUMENT");
  456. fr.Controls.Add ("TextBox1");
  457. fr.Controls.Add ("Button1");
  458. PageDelegates pd = new PageDelegates ();
  459. pd.PreRender = ValidatorTest5PreRender;
  460. t.Invoker = new PageInvoker (pd);
  461. fr.Controls["__EVENTTARGET"].Value = "";
  462. fr.Controls["__EVENTARGUMENT"].Value = "";
  463. fr.Controls["TextBox1"].Value = "Test";
  464. fr.Controls["Button1"].Value = "Button";
  465. t.Request = fr;
  466. PageRenderHtml = t.Run ();
  467. Assert.IsNotNull ( t.UserData, "Validate server side method not raised fail#3");
  468. ArrayList list = t.UserData as ArrayList;
  469. if (list == null)
  470. Assert.Fail ("User data not created fail#3");
  471. Assert.AreEqual (1, list.Count, "Just validate with validation group must be called fail#3");
  472. Assert.AreEqual ("Validate_WithGroup", list[0].ToString (), "Validate with validation group must be called fail#3");
  473. }
  474. public static void ValidatorTest5PreRender (Page p)
  475. {
  476. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#3");
  477. Assert.AreEqual (true, p.Validators[0].IsValid, "Specific validator value filed#3");
  478. Assert.AreEqual (true, p.IsValid, "Page validation Failed#3");
  479. }
  480. [Test]
  481. [Category ("NunitWeb")]
  482. public void Page_ValidatorTest6 ()
  483. {
  484. WebTest t = new WebTest ("PageValidationTest.aspx");
  485. string PageRenderHtml = t.Run ();
  486. FormRequest fr = new FormRequest (t.Response, "form1");
  487. fr.Controls.Add ("__EVENTTARGET");
  488. fr.Controls.Add ("__EVENTARGUMENT");
  489. fr.Controls.Add ("TextBox1");
  490. fr.Controls.Add ("Button1");
  491. PageDelegates pd = new PageDelegates ();
  492. pd.PreRender = ValidatorTest6PreRender;
  493. pd.Load = ValidatorTest6Load;
  494. t.Invoker = new PageInvoker (pd);
  495. fr.Controls["__EVENTTARGET"].Value = "";
  496. fr.Controls["__EVENTARGUMENT"].Value = "";
  497. fr.Controls["TextBox1"].Value = "Test";
  498. fr.Controls["Button1"].Value = "Button";
  499. t.Request = fr;
  500. PageRenderHtml = t.Run ();
  501. Assert.IsNotNull (t.UserData, "Validate server side method not raised fail#3");
  502. ArrayList list = t.UserData as ArrayList;
  503. if (list == null)
  504. Assert.Fail ("User data not created fail#3");
  505. Assert.AreEqual (1, list.Count, "Just validate with validation group must be called fail#3");
  506. Assert.AreEqual ("Validate_WithGroup", list[0].ToString (), "Validate with validation group must be called fail#3");
  507. }
  508. public static void ValidatorTest6PreRender (Page p)
  509. {
  510. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#3");
  511. Assert.AreEqual (false, p.Validators[0].IsValid, "Specific validator value filed#3");
  512. Assert.AreEqual (false, p.IsValid, "Page validation Failed#3");
  513. }
  514. public static void ValidatorTest6Load (Page p)
  515. {
  516. if (p.IsPostBack) {
  517. RequiredFieldValidator rfv = p.FindControl ("RequiredFieldValidator1") as RequiredFieldValidator;
  518. if (rfv == null)
  519. Assert.Fail ("RequiredFieldValidator does not created fail");
  520. rfv.InitialValue = "Test";
  521. }
  522. }
  523. [Test]
  524. [Category ("NunitWeb")]
  525. public void Page_ValidatorTest7 ()
  526. {
  527. WebTest t = new WebTest ("PageValidationTest.aspx");
  528. string PageRenderHtml = t.Run ();
  529. FormRequest fr = new FormRequest (t.Response, "form1");
  530. fr.Controls.Add ("__EVENTTARGET");
  531. fr.Controls.Add ("__EVENTARGUMENT");
  532. fr.Controls.Add ("TextBox1");
  533. fr.Controls.Add ("Button1");
  534. PageDelegates pd = new PageDelegates ();
  535. pd.PreRender = ValidatorTest7PreRender;
  536. pd.Load = ValidatorTest7Load;
  537. t.Invoker = new PageInvoker (pd);
  538. fr.Controls["__EVENTTARGET"].Value = "";
  539. fr.Controls["__EVENTARGUMENT"].Value = "";
  540. fr.Controls["TextBox1"].Value = "Test";
  541. fr.Controls["Button1"].Value = "Button";
  542. t.Request = fr;
  543. PageRenderHtml = t.Run ();
  544. Assert.IsNotNull (t.UserData, "Validate server side method not raised fail#4");
  545. ArrayList list = t.UserData as ArrayList;
  546. if (list == null)
  547. Assert.Fail ("User data not created fail#4");
  548. Assert.AreEqual (1, list.Count, "Just validate with validation group must be called fail#4");
  549. Assert.AreEqual ("Validate_WithGroup", list[0].ToString (), "Validate with validation group must be called fail#4");
  550. }
  551. public static void ValidatorTest7PreRender (Page p)
  552. {
  553. Assert.AreEqual (2, p.Validators.Count, "Validators count fail#4");
  554. Assert.AreEqual (true, p.Validators[0].IsValid, "Specific validator value filed_1#4");
  555. Assert.AreEqual (true, p.Validators[1].IsValid, "Specific validator value filed#4_2#4");
  556. Assert.AreEqual (true, p.IsValid, "Page validation Failed#4");
  557. }
  558. public static void ValidatorTest7Load (Page p)
  559. {
  560. RequiredFieldValidator validator = new RequiredFieldValidator ();
  561. validator.ID = "validator";
  562. validator.ControlToValidate = "TextBox1";
  563. validator.ValidationGroup = "fake";
  564. validator.InitialValue = "Test";
  565. p.Form.Controls.Add (validator);
  566. }
  567. [Test]
  568. [Category ("NunitWeb")]
  569. public void Page_Lifecycle ()
  570. {
  571. WebTest t = new WebTest ("PageLifecycleTest.aspx");
  572. string PageRenderHtml = t.Run ();
  573. ArrayList eventlist = t.UserData as ArrayList;
  574. if (eventlist == null)
  575. Assert.Fail ("User data does not been created fail");
  576. Assert.AreEqual ("OnPreInit", eventlist[0], "Live Cycle Flow #1");
  577. Assert.AreEqual ("OnInit", eventlist[1], "Live Cycle Flow #2");
  578. Assert.AreEqual ("OnInitComplete", eventlist[2], "Live Cycle Flow #3");
  579. Assert.AreEqual ("OnPreLoad", eventlist[3], "Live Cycle Flow #4");
  580. Assert.AreEqual ("OnLoad", eventlist[4], "Live Cycle Flow #5");
  581. Assert.AreEqual ("OnLoadComplete", eventlist[5], "Live Cycle Flow #6");
  582. Assert.AreEqual ("OnPreRender", eventlist[6], "Live Cycle Flow #7");
  583. Assert.AreEqual ("OnPreRenderComplete", eventlist[7], "Live Cycle Flow #8");
  584. Assert.AreEqual ("OnSaveStateComplete", eventlist[8], "Live Cycle Flow #9");
  585. Assert.AreEqual ("OnUnload", eventlist[9], "Live Cycle Flow #10");
  586. }
  587. [Test]
  588. [Category ("NunitWeb")]
  589. [Category ("NotWorking")]
  590. public void AddOnPreRenderCompleteAsync ()
  591. {
  592. WebTest t = new WebTest ("AsyncPage.aspx");
  593. t.Invoker = PageInvoker.CreateOnLoad (AddOnPreRenderCompleteAsync_Load);
  594. string str = t.Run ();
  595. ArrayList eventlist = t.UserData as ArrayList;
  596. if (eventlist == null)
  597. Assert.Fail ("User data does not been created fail");
  598. Assert.AreEqual ("BeginGetAsyncData", eventlist[0], "BeginGetAsyncData Failed");
  599. Assert.AreEqual ("EndGetAsyncData", eventlist[1], "EndGetAsyncData Failed");
  600. }
  601. [Test]
  602. [Category ("NotWorking")]
  603. [Category ("NunitWeb")]
  604. public void ExecuteRegisteredAsyncTasks ()
  605. {
  606. WebTest t = new WebTest ("AsyncPage.aspx");
  607. t.Invoker = PageInvoker.CreateOnLoad (ExecuteRegisteredAsyncTasks_Load);
  608. string str = t.Run ();
  609. ArrayList eventlist = t.UserData as ArrayList;
  610. if (eventlist == null)
  611. Assert.Fail ("User data does not been created fail");
  612. Assert.AreEqual ("BeginGetAsyncData", eventlist[0], "BeginGetAsyncData Failed");
  613. Assert.AreEqual ("EndGetAsyncData", eventlist[1], "EndGetAsyncData Failed");
  614. }
  615. public static void ExecuteRegisteredAsyncTasks_Load (Page p)
  616. {
  617. BeginEventHandler bh = new BeginEventHandler (BeginGetAsyncData);
  618. EndEventHandler eh = new EndEventHandler (EndGetAsyncData);
  619. p.AddOnPreRenderCompleteAsync (bh, eh);
  620. p.ExecuteRegisteredAsyncTasks ();
  621. }
  622. static WebRequest myRequest;
  623. public static void AddOnPreRenderCompleteAsync_Load (Page p)
  624. {
  625. BeginEventHandler bh = new BeginEventHandler(BeginGetAsyncData);
  626. EndEventHandler eh = new EndEventHandler(EndGetAsyncData);
  627. p.AddOnPreRenderCompleteAsync(bh, eh);
  628. // Initialize the WebRequest.
  629. string address = "http://MyPage.aspx";
  630. myRequest = WebRequest.Create(address);
  631. }
  632. static IAsyncResult BeginGetAsyncData(Object src, EventArgs args, AsyncCallback cb, Object state)
  633. {
  634. if (WebTest.CurrentTest.UserData == null) {
  635. ArrayList list = new ArrayList ();
  636. list.Add ("BeginGetAsyncData");
  637. WebTest.CurrentTest.UserData = list;
  638. }
  639. else {
  640. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  641. if (list == null)
  642. throw new NullReferenceException ();
  643. list.Add ("BeginGetAsyncData");
  644. WebTest.CurrentTest.UserData = list;
  645. }
  646. return new Customresult(); // myRequest.BeginGetResponse (cb, state);
  647. }
  648. static void EndGetAsyncData(IAsyncResult ar)
  649. {
  650. if (WebTest.CurrentTest.UserData == null) {
  651. ArrayList list = new ArrayList ();
  652. list.Add ("EndGetAsyncData");
  653. WebTest.CurrentTest.UserData = list;
  654. }
  655. else {
  656. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  657. if (list == null)
  658. throw new NullReferenceException ();
  659. list.Add ("EndGetAsyncData");
  660. WebTest.CurrentTest.UserData = list;
  661. }
  662. }
  663. [Test]
  664. [Category ("NotWorking")]
  665. public void AsyncMode ()
  666. {
  667. TestPage p = new TestPage ();
  668. Assert.AreEqual (false, p.AsyncMode, "AsyncMode#1");
  669. p.AsyncMode = true;
  670. Assert.AreEqual (true, p.AsyncMode, "AsyncMode#2");
  671. }
  672. [Test]
  673. [Category ("NotWorking")]
  674. public void AsyncTimeout ()
  675. {
  676. Page p = new Page ();
  677. Assert.AreEqual (45, ((TimeSpan) p.AsyncTimeout).Seconds, "AsyncTimeout#1");
  678. p.AsyncTimeout = new TimeSpan (0, 0, 50);
  679. Assert.AreEqual (50, ((TimeSpan) p.AsyncTimeout).Seconds, "AsyncTimeout#2");
  680. }
  681. [Test]
  682. public void ClientQueryString ()
  683. {
  684. // httpContext URL cannot be set.
  685. }
  686. [Test]
  687. public void ClientScript ()
  688. {
  689. Page p = new Page ();
  690. Assert.AreEqual (typeof(ClientScriptManager), p.ClientScript.GetType(), "ClientScriptManager");
  691. }
  692. [Test]
  693. [Category ("NotWorking")]
  694. public void CreateHtmlTextWriterFromType ()
  695. {
  696. HtmlTextWriter writer = Page.CreateHtmlTextWriterFromType (null, typeof (HtmlTextWriter));
  697. Assert.IsNotNull (writer, "CreateHtmlTextWriterFromType Failed");
  698. }
  699. [Test]
  700. public void EnableEventValidation ()
  701. {
  702. Page p = new Page ();
  703. Assert.AreEqual (true, p.EnableEventValidation, "EnableEventValidation#1");
  704. p.EnableEventValidation = false;
  705. Assert.AreEqual (false, p.EnableEventValidation, "EnableEventValidation#2");
  706. }
  707. [Test]
  708. [Category ("NunitWeb")]
  709. public void Form ()
  710. {
  711. Page p = new Page ();
  712. Assert.AreEqual (null, p.Form, "Form#1");
  713. WebTest t = new WebTest (PageInvoker.CreateOnLoad (Form_Load));
  714. t.Run ();
  715. }
  716. public static void Form_Load (Page p)
  717. {
  718. Assert.IsNotNull (p.Form, "Form#2");
  719. Assert.AreEqual ("form1", p.Form.ID, "Form#3");
  720. Assert.AreEqual (typeof (HtmlForm), p.Form.GetType (), "Form#4");
  721. }
  722. [Test]
  723. [Category ("NotWorking")]
  724. public void GetWrappedFileDependencies ()
  725. {
  726. TestPage p = new TestPage ();
  727. string []s = { "test.aspx","fake.aspx" };
  728. object list = p.GetWrappedFileDependencies (s);
  729. Assert.AreEqual (typeof(String[]), list.GetType (), "GetWrappedFileDependencie#1");
  730. Assert.AreEqual (2, ((String[]) list).Length, "GetWrappedFileDependencie#2");
  731. Assert.AreEqual ("test.aspx", ((String[]) list)[0], "GetWrappedFileDependencie#3");
  732. Assert.AreEqual ("fake.aspx", ((String[]) list)[1], "GetWrappedFileDependencie#4");
  733. }
  734. [Test]
  735. public void IdSeparator ()
  736. {
  737. TestPage p = new TestPage ();
  738. Assert.AreEqual ('$', p.IdSeparator, "IdSeparator");
  739. }
  740. [Test]
  741. [Category ("NunitWeb")]
  742. public void InitializeCulture ()
  743. {
  744. WebTest t = new WebTest ("PageCultureTest.aspx");
  745. string PageRenderHtml = t.Run ();
  746. ArrayList eventlist = t.UserData as ArrayList;
  747. if (eventlist == null)
  748. Assert.Fail ("User data does not been created fail");
  749. Assert.AreEqual ("InitializeCulture", eventlist[0], "Live Cycle Flow #0");
  750. Assert.AreEqual ("OnPreInit", eventlist[1], "Live Cycle Flow #1");
  751. Assert.AreEqual ("OnInit", eventlist[2], "Live Cycle Flow #2");
  752. Assert.AreEqual ("OnInitComplete", eventlist[3], "Live Cycle Flow #3");
  753. Assert.AreEqual ("OnPreLoad", eventlist[4], "Live Cycle Flow #4");
  754. Assert.AreEqual ("OnLoad", eventlist[5], "Live Cycle Flow #5");
  755. Assert.AreEqual ("OnLoadComplete", eventlist[6], "Live Cycle Flow #6");
  756. Assert.AreEqual ("OnPreRender", eventlist[7], "Live Cycle Flow #7");
  757. Assert.AreEqual ("OnPreRenderComplete", eventlist[8], "Live Cycle Flow #8");
  758. Assert.AreEqual ("OnSaveStateComplete", eventlist[9], "Live Cycle Flow #9");
  759. Assert.AreEqual ("OnUnload", eventlist[10], "Live Cycle Flow #10");
  760. }
  761. [Test]
  762. [Category ("NotWorking")]
  763. public void IsAsync ()
  764. {
  765. Page p = new Page ();
  766. Assert.AreEqual (false, p.IsAsync, "IsAsync");
  767. }
  768. [Test]
  769. public void IsCallback ()
  770. {
  771. Page p = new Page ();
  772. Assert.AreEqual (false, p.IsCallback, "IsCallback");
  773. }
  774. [Test]
  775. public void IsCrossPagePostBack ()
  776. {
  777. Page p = new Page ();
  778. Assert.AreEqual (false, p.IsCrossPagePostBack, "IsCrossPagePostBack");
  779. }
  780. [Test]
  781. public void Items ()
  782. {
  783. Page p = new Page ();
  784. IDictionary d = p.Items;
  785. d.Add ("key", "test");
  786. Assert.AreEqual (1, p.Items.Count, "Items#1");
  787. Assert.AreEqual ("test", p.Items["key"].ToString(), "Items#2");
  788. }
  789. [Test]
  790. public void MaintainScrollPositionOnPostBack ()
  791. {
  792. Page p = new Page ();
  793. Assert.AreEqual (false, p.MaintainScrollPositionOnPostBack, "MaintainScrollPositionOnPostBack#1");
  794. p.MaintainScrollPositionOnPostBack = true;
  795. Assert.AreEqual (true, p.MaintainScrollPositionOnPostBack, "MaintainScrollPositionOnPostBack#2");
  796. }
  797. [Test]
  798. [Category("NunitWeb")]
  799. public void Master ()
  800. {
  801. Page p = new Page ();
  802. Assert.AreEqual (null, p.Master, "Master#1");
  803. WebTest t = new WebTest ("MyPageWithMaster.aspx");
  804. t.Invoker = PageInvoker.CreateOnLoad (Master_Load);
  805. t.Run ();
  806. Assert.AreEqual ("asp.my_master", t.UserData.ToString ().ToLower(), "Master#2");
  807. }
  808. public static void Master_Load (Page p)
  809. {
  810. WebTest.CurrentTest.UserData = p.Master.GetType().ToString();
  811. }
  812. [Test]
  813. public void MasterPageFile ()
  814. {
  815. Page p = new Page ();
  816. Assert.AreEqual (null, p.MasterPageFile, "MasterPageFile#1");
  817. p.MasterPageFile = "test";
  818. Assert.AreEqual ("test", p.MasterPageFile, "MasterPageFile#2");
  819. }
  820. [Test]
  821. [Category ("NotWorking")]
  822. public void MaxPageStateFieldLength ()
  823. {
  824. Page p = new Page ();
  825. Assert.AreEqual (-1, p.MaxPageStateFieldLength, "MaxPageStateFieldLength#1");
  826. p.MaxPageStateFieldLength = 10;
  827. Assert.AreEqual (10, p.MaxPageStateFieldLength, "MaxPageStateFieldLength#2");
  828. }
  829. [Test]
  830. public void PageAdapter ()
  831. {
  832. Page p = new Page ();
  833. Assert.AreEqual (null, p.PageAdapter, "PageAdapter");
  834. }
  835. [Test]
  836. public void PreviousPage ()
  837. {
  838. // NUnit.Framework limitation for server.transfer
  839. }
  840. [Test]
  841. [Category ("NotWorking")]
  842. public void RegisterRequiresViewStateEncryption ()
  843. {
  844. Page p = new Page ();
  845. p.ViewStateEncryptionMode = ViewStateEncryptionMode.Always;
  846. p.RegisterRequiresViewStateEncryption ();
  847. // No changes after the Encryption
  848. }
  849. [Test]
  850. public void Theme ()
  851. {
  852. Page p = new Page ();
  853. Assert.AreEqual (null, p.Theme, "Theme#1");
  854. p.Theme = "Theme.skin";
  855. Assert.AreEqual ("Theme.skin",p.Theme, "Theme#2");
  856. }
  857. [Test]
  858. [Category ("NotWorking")]
  859. public void UniqueFilePathSuffix ()
  860. {
  861. TestPage p = new TestPage ();
  862. if (!p.UniqueFilePathSuffix.StartsWith ("__ufps=")) {
  863. Assert.Fail ("UniqueFilePathSuffix");
  864. }
  865. }
  866. [Test]
  867. [Category ("NotWorking")]
  868. public void ViewStateEncryptionModeTest ()
  869. {
  870. Page p = new Page ();
  871. Assert.AreEqual (ViewStateEncryptionMode.Auto, p.ViewStateEncryptionMode, "ViewStateEncryptionMode#1");
  872. p.ViewStateEncryptionMode = ViewStateEncryptionMode.Never;
  873. Assert.AreEqual (ViewStateEncryptionMode.Never, p.ViewStateEncryptionMode, "ViewStateEncryptionMode#2");
  874. }
  875. [Test]
  876. [ExpectedException (typeof (InvalidOperationException))]
  877. public void GetDataItem_Exception ()
  878. {
  879. Page p = new Page ();
  880. p.GetDataItem ();
  881. }
  882. #region help_classes
  883. class Customresult : IAsyncResult
  884. {
  885. #region IAsyncResult Members
  886. public object AsyncState
  887. {
  888. get { throw new Exception ("The method or operation is not implemented."); }
  889. }
  890. public WaitHandle AsyncWaitHandle
  891. {
  892. get { throw new Exception ("The method or operation is not implemented."); }
  893. }
  894. public bool CompletedSynchronously
  895. {
  896. get { return true; }
  897. }
  898. public bool IsCompleted
  899. {
  900. get { throw new Exception ("The method or operation is not implemented."); }
  901. }
  902. #endregion
  903. }
  904. #endregion
  905. [Test]
  906. [Category ("NunitWeb")]
  907. public void ProcessPostData_Second_Try () //Just flow and not implementation detail
  908. {
  909. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ProcessPostData_Second_Try_Load));
  910. string html = t.Run ();
  911. FormRequest fr = new FormRequest (t.Response, "form1");
  912. fr.Controls.Add ("__EVENTTARGET");
  913. fr.Controls.Add ("__EVENTARGUMENT");
  914. fr.Controls ["__EVENTTARGET"].Value = "__Page";
  915. fr.Controls ["__EVENTARGUMENT"].Value = "";
  916. t.Request = fr;
  917. t.Run ();
  918. Assert.AreEqual ("CustomPostBackDataHandler_LoadPostData", t.UserData, "User data does not been created fail");
  919. }
  920. public static void ProcessPostData_Second_Try_Load (Page p)
  921. {
  922. CustomPostBackDataHandler c = new CustomPostBackDataHandler ();
  923. c.ID = "CustomPostBackDataHandler1";
  924. p.Form.Controls.Add (c);
  925. }
  926. class CustomPostBackDataHandler : WebControl, IPostBackDataHandler
  927. {
  928. protected override void OnInit (EventArgs e)
  929. {
  930. base.OnInit (e);
  931. Page.RegisterRequiresPostBack (this);
  932. }
  933. #region IPostBackDataHandler Members
  934. public bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
  935. {
  936. WebTest.CurrentTest.UserData = "CustomPostBackDataHandler_LoadPostData";
  937. return false;
  938. }
  939. public void RaisePostDataChangedEvent ()
  940. {
  941. }
  942. #endregion
  943. }
  944. #endif
  945. [TestFixtureTearDown]
  946. public void TearDown ()
  947. {
  948. WebTest.Unload ();
  949. }
  950. }
  951. }