PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Web/Test/System.Web.UI/PageTest.cs

https://bitbucket.org/danipen/mono
C# | 1497 lines | 1263 code | 190 blank | 44 comment | 62 complexity | c904441d477343de8ab470d0bfc71585 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // Tests for System.Web.UI.Page
  3. //
  4. // Authors:
  5. // Peter Dennis Bartok (pbartok@novell.com)
  6. // Sebastien Pouliot <sebastien@ximian.com>
  7. // Yoni Klain <yonik@mainsoft.com>
  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. #if NET_2_0
  38. using System.Web.UI.Adapters;
  39. #endif
  40. using MonoTests.SystemWeb.Framework;
  41. using MonoTests.stand_alone.WebHarness;
  42. using System.Web.UI.WebControls;
  43. using System.Web.UI.HtmlControls;
  44. using System.Collections;
  45. using System.Collections.Specialized;
  46. using System.Net;
  47. namespace MonoTests.System.Web.UI {
  48. class TestPage : Page {
  49. private HttpContext ctx;
  50. // don't call base class (so _context is never set to a non-null value)
  51. protected internal override HttpContext Context {
  52. get {
  53. if (ctx == null) {
  54. ctx = new HttpContext (null);
  55. ctx.User = new GenericPrincipal (new GenericIdentity ("me"), null);
  56. }
  57. return ctx;
  58. }
  59. }
  60. #if NET_2_0
  61. public new bool AsyncMode {
  62. get { return base.AsyncMode; }
  63. set { base.AsyncMode = value; }
  64. }
  65. public new object GetWrappedFileDependencies(string[] virtualFileDependencies)
  66. {
  67. return base.GetWrappedFileDependencies(virtualFileDependencies);
  68. }
  69. public new void InitOutputCache (OutputCacheParameters cacheSettings)
  70. {
  71. base.InitOutputCache (cacheSettings);
  72. }
  73. public new string UniqueFilePathSuffix {
  74. get { return base.UniqueFilePathSuffix; }
  75. }
  76. public new char IdSeparator {
  77. get {
  78. return base.IdSeparator;
  79. }
  80. }
  81. #endif
  82. }
  83. class TestPage2 : Page {
  84. private HttpContext ctx;
  85. // don't call base class (so _context is never set to a non-null value)
  86. protected internal override HttpContext Context {
  87. get {
  88. if (ctx == null) {
  89. ctx = new HttpContext (
  90. new HttpRequest (String.Empty, "http://www.mono-project.com", String.Empty),
  91. new HttpResponse (new StringWriter ())
  92. );
  93. }
  94. return ctx;
  95. }
  96. }
  97. public HttpContext HttpContext {
  98. get { return Context; }
  99. }
  100. }
  101. [TestFixture]
  102. public class PageTest {
  103. [TestFixtureSetUp]
  104. public void SetUpTest ()
  105. {
  106. WebTest.CopyResource (GetType (), "PageCultureTest.aspx", "PageCultureTest.aspx");
  107. WebTest.CopyResource (GetType (), "PageLifecycleTest.aspx", "PageLifecycleTest.aspx");
  108. WebTest.CopyResource (GetType (), "PageValidationTest.aspx", "PageValidationTest.aspx");
  109. WebTest.CopyResource (GetType (), "AsyncPage.aspx", "AsyncPage.aspx");
  110. WebTest.CopyResource (GetType (), "PageWithAdapter.aspx", "PageWithAdapter.aspx");
  111. WebTest.CopyResource (GetType (), "RedirectOnError.aspx", "RedirectOnError.aspx");
  112. WebTest.CopyResource (GetType (), "ClearErrorOnError.aspx", "ClearErrorOnError.aspx");
  113. }
  114. [Test]
  115. [ExpectedException (typeof(HttpException))]
  116. public void RequestExceptionTest ()
  117. {
  118. Page p;
  119. HttpRequest r;
  120. p = new Page ();
  121. r = p.Request;
  122. }
  123. [Test]
  124. #if NET_2_0
  125. [Category ("NotDotNet")] // page.User throw NRE in 2.0 RC
  126. #endif
  127. public void User_OverridenContext ()
  128. {
  129. TestPage page = new TestPage ();
  130. Assert.AreEqual ("me", page.User.Identity.Name, "User");
  131. }
  132. [Test]
  133. [ExpectedException (typeof (HttpException))]
  134. public void Request_OverridenContext ()
  135. {
  136. TestPage2 page = new TestPage2 ();
  137. Assert.IsNotNull (page.Request, "Request");
  138. // it doesn't seems to access the context via the virtual property
  139. }
  140. [Test]
  141. public void Request_OverridenContext_Indirect ()
  142. {
  143. TestPage2 page = new TestPage2 ();
  144. Assert.IsNotNull (page.HttpContext.Request, "Request");
  145. }
  146. [Test]
  147. [ExpectedException (typeof (HttpException))]
  148. public void Response_OverridenContext ()
  149. {
  150. TestPage2 page = new TestPage2 ();
  151. Assert.IsNotNull (page.Response, "Response");
  152. }
  153. [Test]
  154. [ExpectedException (typeof (HttpException))]
  155. public void Cache_OverridenContext ()
  156. {
  157. TestPage2 page = new TestPage2 ();
  158. Assert.IsNotNull (page.Cache, "Cache");
  159. }
  160. [Test]
  161. [ExpectedException (typeof (HttpException))]
  162. public void Session_OverridenContext ()
  163. {
  164. TestPage2 page = new TestPage2 ();
  165. Assert.IsNotNull (page.Session, "Session");
  166. }
  167. [Test]
  168. public void Application_OverridenContext ()
  169. {
  170. TestPage2 page = new TestPage2 ();
  171. Assert.IsNull (page.Application, "Application");
  172. }
  173. #if NET_2_0
  174. [Test]
  175. [Category ("NunitWeb")]
  176. public void PageHeaderOnPreInit ()
  177. {
  178. PageDelegate pd = new PageDelegate (Page_OnPreInit);
  179. WebTest t = new WebTest (PageInvoker.CreateOnPreInit (pd));
  180. string html = t.Run ();
  181. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  182. string origHtml = @" <head id=""Head1""><title>
  183. PreInit
  184. </title></head>";
  185. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderInit");
  186. }
  187. public static void Page_OnPreInit (Page p)
  188. {
  189. Assert.AreEqual (null, p.Header, "HeaderOnPreInit");
  190. p.Title = "PreInit";
  191. }
  192. [Test]
  193. [Category ("NunitWeb")]
  194. public void PageHeaderInit ()
  195. {
  196. PageDelegate pd = new PageDelegate (CheckHeader);
  197. WebTest t = new WebTest (PageInvoker.CreateOnInit (pd));
  198. string html = t.Run ();
  199. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  200. string origHtml = @" <head id=""Head1""><title>
  201. Test
  202. </title></head>";
  203. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderInit");
  204. }
  205. [Test]
  206. [Category ("NunitWeb")]
  207. public void PageHeaderInitComplete ()
  208. {
  209. WebTest t = new WebTest ();
  210. PageDelegates pd = new PageDelegates ();
  211. pd.InitComplete = CheckHeader;
  212. t.Invoker = new PageInvoker (pd);
  213. string html = t.Run ();
  214. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  215. string origHtml = @" <head id=""Head1""><title>
  216. Test
  217. </title></head>";
  218. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderInitComplete");
  219. }
  220. [Test]
  221. [Category ("NunitWeb")]
  222. public void PageHeaderPreLoad ()
  223. {
  224. WebTest t = new WebTest ();
  225. PageDelegates pd = new PageDelegates ();
  226. pd.PreLoad = CheckHeader;
  227. t.Invoker = new PageInvoker (pd);
  228. string html = t.Run ();
  229. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  230. string origHtml = @" <head id=""Head1""><title>
  231. Test
  232. </title></head>";
  233. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderPreLoad");
  234. }
  235. [Test]
  236. [Category ("NunitWeb")]
  237. public void PageHeaderLoad ()
  238. {
  239. PageDelegate pd = new PageDelegate (CheckHeader);
  240. WebTest t = new WebTest (PageInvoker.CreateOnLoad (pd));
  241. string html = t.Run ();
  242. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  243. string origHtml = @" <head id=""Head1""><title>
  244. Test
  245. </title></head>";
  246. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderLoad");
  247. }
  248. [Test]
  249. [Category ("NunitWeb")]
  250. public void PageHeaderLoadComplete ()
  251. {
  252. WebTest t = new WebTest ();
  253. PageDelegates pd = new PageDelegates ();
  254. pd.LoadComplete = CheckHeader;
  255. t.Invoker = new PageInvoker (pd);
  256. string html = t.Run ();
  257. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  258. string origHtml = @" <head id=""Head1""><title>
  259. Test
  260. </title></head>";
  261. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderLoadComplete");
  262. }
  263. [Test]
  264. [Category ("NunitWeb")]
  265. public void PageHeaderPreRender ()
  266. {
  267. WebTest t = new WebTest ();
  268. PageDelegates pd = new PageDelegates ();
  269. pd.PreRender = CheckHeader;
  270. t.Invoker = new PageInvoker (pd);
  271. string html = t.Run ();
  272. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  273. string origHtml = @" <head id=""Head1""><title>
  274. Test
  275. </title></head>";
  276. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderPreRender");
  277. }
  278. [Test]
  279. [Category ("NunitWeb")]
  280. public void PageHeaderPreRenderComplete ()
  281. {
  282. WebTest t = new WebTest ();
  283. PageDelegates pd = new PageDelegates ();
  284. pd.PreRenderComplete = CheckHeader;
  285. t.Invoker = new PageInvoker (pd);
  286. string html = t.Run ();
  287. string newHtml = html.Substring (html.IndexOf ("<head"), (html.IndexOf ("<body") - html.IndexOf ("<head")));
  288. string origHtml = @" <head id=""Head1""><title>
  289. Test
  290. </title></head>";
  291. HtmlDiff.AssertAreEqual (origHtml, newHtml, "HeaderRenderPreRenderComplete");
  292. }
  293. public static void CheckHeader (Page p)
  294. {
  295. Assert.AreEqual ("Untitled Page", p.Title, "CheckHeader#1");
  296. Assert.AreEqual ("Untitled Page", p.Header.Title, "CheckHeader#2");
  297. p.Title = "Test0";
  298. Assert.AreEqual ("Test0", p.Header.Title, "CheckHeader#3");
  299. p.Header.Title = "Test";
  300. Assert.AreEqual ("Test", p.Title, "CheckHeader#4");
  301. }
  302. #endif
  303. #if NET_2_0
  304. [Test]
  305. [Category ("NunitWeb")]
  306. public void Page_ValidationGroup ()
  307. {
  308. new WebTest (PageInvoker.CreateOnLoad (Page_ValidationGroup_Load)).Run ();
  309. }
  310. public static void Page_ValidationGroup_Load (Page page)
  311. {
  312. TextBox textbox;
  313. BaseValidator val;
  314. textbox = new TextBox ();
  315. textbox.ID = "T1";
  316. textbox.ValidationGroup = "VG1";
  317. page.Form.Controls.Add (textbox);
  318. val = new RequiredFieldValidator ();
  319. val.ControlToValidate = "T1";
  320. val.ValidationGroup = "VG1";
  321. page.Form.Controls.Add (val);
  322. textbox = new TextBox ();
  323. textbox.ID = "T2";
  324. textbox.ValidationGroup = "VG2";
  325. page.Form.Controls.Add (textbox);
  326. val = new RequiredFieldValidator ();
  327. val.ControlToValidate = "T2";
  328. val.ValidationGroup = "VG2";
  329. page.Form.Controls.Add (val);
  330. textbox = new TextBox ();
  331. textbox.ID = "T3";
  332. page.Form.Controls.Add (textbox);
  333. val = new RequiredFieldValidator ();
  334. val.ControlToValidate = "T3";
  335. page.Form.Controls.Add (val);
  336. Assert.AreEqual (3, page.Validators.Count, "Page_ValidationGroup#1");
  337. Assert.AreEqual (1, page.GetValidators ("").Count, "Page_ValidationGroup#2");
  338. Assert.AreEqual (1, page.GetValidators (null).Count, "Page_ValidationGroup#3");
  339. Assert.AreEqual (0, page.GetValidators ("Fake").Count, "Page_ValidationGroup#4");
  340. Assert.AreEqual (1, page.GetValidators ("VG1").Count, "Page_ValidationGroup#5");
  341. Assert.AreEqual (1, page.GetValidators ("VG2").Count, "Page_ValidationGroup#6");
  342. }
  343. [Test]
  344. [Category("NunitWeb")]
  345. public void InitOutputCache_UsesAdapter ()
  346. {
  347. WebTest t = new WebTest ("PageWithAdapter.aspx");
  348. t.Invoker = PageInvoker.CreateOnLoad (InitOutputCache_UsesAdapter_OnLoad);
  349. t.Run ();
  350. }
  351. public static void InitOutputCache_UsesAdapter_OnLoad (Page p)
  352. {
  353. Assert.IsTrue (p.Response.Cache.VaryByHeaders ["header-from-aspx"],
  354. "InitOutputCache_UsesAdapter #1");
  355. Assert.IsTrue (p.Response.Cache.VaryByParams ["param-from-aspx"],
  356. "InitOutputCache_UsesAdapter #2");
  357. Assert.IsTrue (p.Response.Cache.VaryByHeaders ["header-from-adapter"],
  358. "InitOutputCache_UsesAdapter #3");
  359. Assert.IsTrue (p.Response.Cache.VaryByParams ["param-from-adapter"],
  360. "InitOutputCache_UsesAdapter #4");
  361. }
  362. [Test]
  363. [Category("NunitWeb")]
  364. public void PageStatePersister_UsesAdapter ()
  365. {
  366. WebTest t = new WebTest ("PageWithAdapter.aspx");
  367. t.Invoker = PageInvoker.CreateOnLoad (PageStatePersister_UsesAdapter_OnLoad);
  368. t.Run ();
  369. }
  370. public static void PageStatePersister_UsesAdapter_OnLoad (Page p)
  371. {
  372. TestPageWithAdapter pageWithAdapter = (TestPageWithAdapter) p;
  373. Assert.IsTrue (pageWithAdapter.PageStatePersister is TestPersister,
  374. "PageStatePersister_UsesAdapter #1");
  375. }
  376. [Test]
  377. [Category("NunitWeb")]
  378. public void ScriptUsesAdapter ()
  379. {
  380. WebTest t = new WebTest ("PageWithAdapter.aspx");
  381. string html = t.Run ();
  382. Assert.IsTrue(html.IndexOf("var theForm = /* testFormReference */document.forms[") != -1, "ScriptUsesAdapter #1");
  383. }
  384. [Test]
  385. [Category("NunitWeb")]
  386. public void DeterminePostBackMode_UsesAdapter ()
  387. {
  388. WebTest t = new WebTest ("PageWithAdapter.aspx");
  389. t.Run ();
  390. t.Request = new FormRequest (t.Response, "form1");
  391. t.Invoker = PageInvoker.CreateOnInit(DeterminePostBackMode_UsesAdapter_OnInit);
  392. t.Run ();
  393. }
  394. public static void DeterminePostBackMode_UsesAdapter_OnInit (Page p)
  395. {
  396. // We need to use this special version of HtmlInputHidden because the
  397. // original class registers itself for event validation in RenderAttributes
  398. // which is not called by WebTest after this OnInit handler returns (WebTest
  399. // performs a fake postback which bypasses the rendering phase) and it would
  400. // result in an event validation failure.
  401. HtmlInputHidden h = new TestHtmlInputHidden();
  402. h.ID = "DeterminePostBackModeTestField";
  403. p.Controls.Add(h);
  404. p.Load += new EventHandler(DeterminePostBackMode_UsesAdapter_OnLoad);
  405. }
  406. public static void DeterminePostBackMode_UsesAdapter_OnLoad(object source, EventArgs args)
  407. {
  408. Page p = (Page)source;
  409. HtmlInputHidden h = (HtmlInputHidden)p.FindControl("DeterminePostBackModeTestField");
  410. Assert.AreEqual("DeterminePostBackModeTestValue", h.Value,
  411. "DeterminePostBackMode #1");
  412. }
  413. #endif
  414. #if NET_2_0
  415. // This test are testing validation fixture using RequiredFieldValidator for example
  416. [Test]
  417. [Category ("NunitWeb")]
  418. public void Page_ValidationCollection ()
  419. {
  420. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ValidationCollectionload));
  421. string html = t.Run ();
  422. }
  423. public static void ValidationCollectionload (Page p)
  424. {
  425. TextBox txt = new TextBox ();
  426. txt.ID = "txt";
  427. RequiredFieldValidator validator = new RequiredFieldValidator ();
  428. validator.ID = "v";
  429. validator.ControlToValidate = "txt";
  430. RequiredFieldValidator validator1 = new RequiredFieldValidator ();
  431. validator1.ID = "v1";
  432. validator1.ControlToValidate = "txt";
  433. p.Form.Controls.Add (txt);
  434. p.Form.Controls.Add (validator);
  435. p.Form.Controls.Add (validator1);
  436. Assert.AreEqual (2, p.Validators.Count, "Validators collection count fail");
  437. Assert.AreEqual (true, p.Validators[0].IsValid, "Validators collection value#1 fail");
  438. Assert.AreEqual (true, p.Validators[1].IsValid, "Validators collection value#2 fail");
  439. }
  440. [Test]
  441. [Category ("NunitWeb")]
  442. public void Page_ValidatorTest1 ()
  443. {
  444. WebTest t = new WebTest ("PageValidationTest.aspx");
  445. string PageRenderHtml = t.Run ();
  446. FormRequest fr = new FormRequest (t.Response, "form1");
  447. fr.Controls.Add ("TextBox1");
  448. PageDelegates pd = new PageDelegates ();
  449. pd.PreRender = ValidatorTest1PreRender;
  450. t.Invoker = new PageInvoker (pd);
  451. fr.Controls["TextBox1"].Value = "";
  452. t.Request = fr;
  453. PageRenderHtml = t.Run ();
  454. Assert.IsNotNull(t.UserData, "Validate server side method not raised fail");
  455. ArrayList list = t.UserData as ArrayList;
  456. if (list == null)
  457. Assert.Fail ("User data not created fail#1");
  458. Assert.AreEqual (1, list.Count, "Just validate with no validation group must be called fail#1");
  459. Assert.AreEqual ("Validate", list[0].ToString (), "Validate with no validation group must be called fail#1");
  460. }
  461. public static void ValidatorTest1PreRender (Page p)
  462. {
  463. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#1");
  464. Assert.AreEqual (false, p.Validators[0].IsValid, "Specific validator value filed#1");
  465. Assert.AreEqual (false, p.IsValid, "Page validation Failed#1");
  466. }
  467. [Test]
  468. [Category ("NunitWeb")]
  469. public void Page_ValidatorTest2 ()
  470. {
  471. WebTest t = new WebTest ("PageValidationTest.aspx");
  472. string PageRenderHtml = t.Run ();
  473. FormRequest fr = new FormRequest (t.Response, "form1");
  474. fr.Controls.Add ("TextBox1");
  475. PageDelegates pd = new PageDelegates ();
  476. pd.PreRender = ValidatorTest2PreRender;
  477. t.Invoker = new PageInvoker (pd);
  478. fr.Controls["TextBox1"].Value = "test";
  479. t.Request = fr;
  480. PageRenderHtml = t.Run ();
  481. Assert.IsNotNull ( t.UserData, "Validate server side method not raised fail#2");
  482. ArrayList list = t.UserData as ArrayList;
  483. if (list == null)
  484. Assert.Fail ("User data not created fail#2");
  485. Assert.AreEqual (1, list.Count, "Just validate with no validation group must be called fail#2");
  486. Assert.AreEqual ("Validate", list[0].ToString (), "Validate with no validation group must be called fail#2");
  487. }
  488. public static void ValidatorTest2PreRender (Page p)
  489. {
  490. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#2");
  491. Assert.AreEqual (true, p.Validators[0].IsValid, "Specific validator value fail#2");
  492. Assert.AreEqual (true, p.IsValid, "Page validation Fail#2");
  493. }
  494. [Test]
  495. [Category ("NunitWeb")]
  496. public void Page_ValidatorTest3 ()
  497. {
  498. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ValidatorTest3Load));
  499. t.Run ();
  500. }
  501. public static void ValidatorTest3Load (Page p)
  502. {
  503. TextBox tbx = new TextBox ();
  504. tbx.ID = "tbx";
  505. RequiredFieldValidator vld = new RequiredFieldValidator ();
  506. vld.ID = "vld";
  507. vld.ControlToValidate = "tbx";
  508. p.Controls.Add (tbx);
  509. p.Controls.Add (vld);
  510. vld.Validate ();
  511. Assert.AreEqual (false, p.Validators[0].IsValid, "RequiredField result fail #1");
  512. tbx.Text = "test";
  513. vld.Validate ();
  514. Assert.AreEqual (true, p.Validators[0].IsValid, "RequiredField result fail #2");
  515. }
  516. [Test]
  517. [Category ("NunitWeb")]
  518. public void Page_ValidatorTest4 ()
  519. {
  520. WebTest t = new WebTest ("PageValidationTest.aspx");
  521. string PageRenderHtml = t.Run ();
  522. FormRequest fr = new FormRequest (t.Response, "form1");
  523. fr.Controls.Add ("__EVENTTARGET");
  524. fr.Controls.Add ("__EVENTARGUMENT");
  525. fr.Controls.Add ("TextBox1");
  526. fr.Controls.Add ("Button1");
  527. PageDelegates pd = new PageDelegates ();
  528. pd.PreRender = ValidatorTest4PreRender;
  529. t.Invoker = new PageInvoker (pd);
  530. fr.Controls["__EVENTTARGET"].Value = "";
  531. fr.Controls["__EVENTARGUMENT"].Value = "";
  532. fr.Controls["TextBox1"].Value = "";
  533. fr.Controls["Button1"].Value = "Button";
  534. t.Request = fr;
  535. PageRenderHtml = t.Run ();
  536. Assert.IsNotNull (t.UserData, "Validate server side method not raised fail#3");
  537. ArrayList list = t.UserData as ArrayList;
  538. if (list == null)
  539. Assert.Fail ("User data not created fail#3");
  540. Assert.AreEqual (1, list.Count, "Just validate with validation group must be called fail#3");
  541. Assert.AreEqual ("Validate_WithGroup", list[0].ToString (), "Validate with validation group must be called fail#3");
  542. }
  543. public static void ValidatorTest4PreRender (Page p)
  544. {
  545. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#3");
  546. Assert.AreEqual (false, p.Validators[0].IsValid, "Specific validator value filed#3");
  547. Assert.AreEqual (false, p.IsValid, "Page validation Failed#3");
  548. }
  549. [Test]
  550. [Category ("NunitWeb")]
  551. public void Page_ValidatorTest5 ()
  552. {
  553. WebTest t = new WebTest ("PageValidationTest.aspx");
  554. string PageRenderHtml = t.Run ();
  555. FormRequest fr = new FormRequest (t.Response, "form1");
  556. fr.Controls.Add ("__EVENTTARGET");
  557. fr.Controls.Add ("__EVENTARGUMENT");
  558. fr.Controls.Add ("TextBox1");
  559. fr.Controls.Add ("Button1");
  560. PageDelegates pd = new PageDelegates ();
  561. pd.PreRender = ValidatorTest5PreRender;
  562. t.Invoker = new PageInvoker (pd);
  563. fr.Controls["__EVENTTARGET"].Value = "";
  564. fr.Controls["__EVENTARGUMENT"].Value = "";
  565. fr.Controls["TextBox1"].Value = "Test";
  566. fr.Controls["Button1"].Value = "Button";
  567. t.Request = fr;
  568. PageRenderHtml = t.Run ();
  569. Assert.IsNotNull ( t.UserData, "Validate server side method not raised fail#3");
  570. ArrayList list = t.UserData as ArrayList;
  571. if (list == null)
  572. Assert.Fail ("User data not created fail#3");
  573. Assert.AreEqual (1, list.Count, "Just validate with validation group must be called fail#3");
  574. Assert.AreEqual ("Validate_WithGroup", list[0].ToString (), "Validate with validation group must be called fail#3");
  575. }
  576. public static void ValidatorTest5PreRender (Page p)
  577. {
  578. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#3");
  579. Assert.AreEqual (true, p.Validators[0].IsValid, "Specific validator value filed#3");
  580. Assert.AreEqual (true, p.IsValid, "Page validation Failed#3");
  581. }
  582. [Test]
  583. [Category ("NunitWeb")]
  584. public void Page_ValidatorTest6 ()
  585. {
  586. WebTest t = new WebTest ("PageValidationTest.aspx");
  587. string PageRenderHtml = t.Run ();
  588. FormRequest fr = new FormRequest (t.Response, "form1");
  589. fr.Controls.Add ("__EVENTTARGET");
  590. fr.Controls.Add ("__EVENTARGUMENT");
  591. fr.Controls.Add ("TextBox1");
  592. fr.Controls.Add ("Button1");
  593. PageDelegates pd = new PageDelegates ();
  594. pd.PreRender = ValidatorTest6PreRender;
  595. pd.Load = ValidatorTest6Load;
  596. t.Invoker = new PageInvoker (pd);
  597. fr.Controls["__EVENTTARGET"].Value = "";
  598. fr.Controls["__EVENTARGUMENT"].Value = "";
  599. fr.Controls["TextBox1"].Value = "Test";
  600. fr.Controls["Button1"].Value = "Button";
  601. t.Request = fr;
  602. PageRenderHtml = t.Run ();
  603. Assert.IsNotNull (t.UserData, "Validate server side method not raised fail#3");
  604. ArrayList list = t.UserData as ArrayList;
  605. if (list == null)
  606. Assert.Fail ("User data not created fail#3");
  607. Assert.AreEqual (1, list.Count, "Just validate with validation group must be called fail#3");
  608. Assert.AreEqual ("Validate_WithGroup", list[0].ToString (), "Validate with validation group must be called fail#3");
  609. }
  610. public static void ValidatorTest6PreRender (Page p)
  611. {
  612. Assert.AreEqual (1, p.Validators.Count, "Validators count fail#3");
  613. Assert.AreEqual (false, p.Validators[0].IsValid, "Specific validator value filed#3");
  614. Assert.AreEqual (false, p.IsValid, "Page validation Failed#3");
  615. }
  616. public static void ValidatorTest6Load (Page p)
  617. {
  618. if (p.IsPostBack) {
  619. RequiredFieldValidator rfv = p.FindControl ("RequiredFieldValidator1") as RequiredFieldValidator;
  620. if (rfv == null)
  621. Assert.Fail ("RequiredFieldValidator does not created fail");
  622. rfv.InitialValue = "Test";
  623. }
  624. }
  625. [Test]
  626. [Category ("NunitWeb")]
  627. public void Page_ValidatorTest7 ()
  628. {
  629. WebTest t = new WebTest ("PageValidationTest.aspx");
  630. string PageRenderHtml = t.Run ();
  631. FormRequest fr = new FormRequest (t.Response, "form1");
  632. fr.Controls.Add ("__EVENTTARGET");
  633. fr.Controls.Add ("__EVENTARGUMENT");
  634. fr.Controls.Add ("TextBox1");
  635. fr.Controls.Add ("Button1");
  636. PageDelegates pd = new PageDelegates ();
  637. pd.PreRender = ValidatorTest7PreRender;
  638. pd.Load = ValidatorTest7Load;
  639. t.Invoker = new PageInvoker (pd);
  640. fr.Controls["__EVENTTARGET"].Value = "";
  641. fr.Controls["__EVENTARGUMENT"].Value = "";
  642. fr.Controls["TextBox1"].Value = "Test";
  643. fr.Controls["Button1"].Value = "Button";
  644. t.Request = fr;
  645. PageRenderHtml = t.Run ();
  646. Assert.IsNotNull (t.UserData, "Validate server side method not raised fail#4");
  647. ArrayList list = t.UserData as ArrayList;
  648. if (list == null)
  649. Assert.Fail ("User data not created fail#4");
  650. Assert.AreEqual (1, list.Count, "Just validate with validation group must be called fail#4");
  651. Assert.AreEqual ("Validate_WithGroup", list[0].ToString (), "Validate with validation group must be called fail#4");
  652. }
  653. public static void ValidatorTest7PreRender (Page p)
  654. {
  655. Assert.AreEqual (2, p.Validators.Count, "Validators count fail#4");
  656. Assert.AreEqual (true, p.Validators[0].IsValid, "Specific validator value filed_1#4");
  657. Assert.AreEqual (true, p.Validators[1].IsValid, "Specific validator value filed#4_2#4");
  658. Assert.AreEqual (true, p.IsValid, "Page validation Failed#4");
  659. }
  660. public static void ValidatorTest7Load (Page p)
  661. {
  662. RequiredFieldValidator validator = new RequiredFieldValidator ();
  663. validator.ID = "validator";
  664. validator.ControlToValidate = "TextBox1";
  665. validator.ValidationGroup = "fake";
  666. validator.InitialValue = "Test";
  667. p.Form.Controls.Add (validator);
  668. }
  669. [Test]
  670. [Category ("NunitWeb")]
  671. public void Page_Lifecycle ()
  672. {
  673. WebTest t = new WebTest ("PageLifecycleTest.aspx");
  674. string PageRenderHtml = t.Run ();
  675. ArrayList eventlist = t.UserData as ArrayList;
  676. if (eventlist == null)
  677. Assert.Fail ("User data does not been created fail");
  678. Assert.AreEqual ("OnPreInit", eventlist[0], "Live Cycle Flow #1");
  679. Assert.AreEqual ("OnInit", eventlist[1], "Live Cycle Flow #2");
  680. Assert.AreEqual ("OnInitComplete", eventlist[2], "Live Cycle Flow #3");
  681. Assert.AreEqual ("OnPreLoad", eventlist[3], "Live Cycle Flow #4");
  682. Assert.AreEqual ("OnLoad", eventlist[4], "Live Cycle Flow #5");
  683. Assert.AreEqual ("OnLoadComplete", eventlist[5], "Live Cycle Flow #6");
  684. Assert.AreEqual ("OnPreRender", eventlist[6], "Live Cycle Flow #7");
  685. Assert.AreEqual ("OnPreRenderComplete", eventlist[7], "Live Cycle Flow #8");
  686. Assert.AreEqual ("OnSaveStateComplete", eventlist[8], "Live Cycle Flow #9");
  687. Assert.AreEqual ("OnUnload", eventlist[9], "Live Cycle Flow #10");
  688. }
  689. [Test]
  690. [Category ("NunitWeb")]
  691. #if !TARGET_JVM
  692. [Category ("NotWorking")] // Mono PageParser does not handle @Page Async=true
  693. #endif
  694. public void AddOnPreRenderCompleteAsync ()
  695. {
  696. WebTest t = new WebTest ("AsyncPage.aspx");
  697. t.Invoker = PageInvoker.CreateOnLoad (AddOnPreRenderCompleteAsync_Load);
  698. string str = t.Run ();
  699. ArrayList eventlist = t.UserData as ArrayList;
  700. if (eventlist == null)
  701. Assert.Fail ("User data does not been created fail");
  702. Assert.AreEqual ("BeginGetAsyncData", eventlist[0], "BeginGetAsyncData Failed");
  703. Assert.AreEqual ("EndGetAsyncData", eventlist[1], "EndGetAsyncData Failed");
  704. }
  705. [Test]
  706. #if !TARGET_JVM
  707. [Category ("NotWorking")] // Mono PageParser does not handle @Page Async=true
  708. #endif
  709. [Category ("NunitWeb")]
  710. public void ExecuteRegisteredAsyncTasks ()
  711. {
  712. WebTest t = new WebTest ("AsyncPage.aspx");
  713. t.Invoker = PageInvoker.CreateOnLoad (ExecuteRegisteredAsyncTasks_Load);
  714. string str = t.Run ();
  715. ArrayList eventlist = t.UserData as ArrayList;
  716. if (eventlist == null)
  717. Assert.Fail ("User data does not been created fail");
  718. Assert.AreEqual ("BeginGetAsyncData", eventlist[0], "BeginGetAsyncData Failed");
  719. Assert.AreEqual ("EndGetAsyncData", eventlist[1], "EndGetAsyncData Failed");
  720. }
  721. [Test]
  722. [Category ("NunitWeb")]
  723. #if !TARGET_JVM
  724. [Category ("NotWorking")] // Mono PageParser does not handle @Page Async=true
  725. #endif
  726. [ExpectedException (typeof (Exception))]
  727. public void AddOnPreRenderCompleteAsyncBeginThrows ()
  728. {
  729. WebTest t = new WebTest ("AsyncPage.aspx");
  730. t.Invoker = PageInvoker.CreateOnLoad (AddOnPreRenderCompleteAsyncBeginThrows_Load);
  731. string str = t.Run ();
  732. }
  733. [Test]
  734. [Category ("NunitWeb")]
  735. #if !TARGET_JVM
  736. [Category ("NotWorking")] // Mono PageParser does not handle @Page Async=true
  737. #endif
  738. [ExpectedException (typeof (Exception))]
  739. public void AddOnPreRenderCompleteAsyncEndThrows ()
  740. {
  741. WebTest t = new WebTest ("AsyncPage.aspx");
  742. t.Invoker = PageInvoker.CreateOnLoad (AddOnPreRenderCompleteAsyncEndThrows_Load);
  743. string str = t.Run ();
  744. }
  745. public static void ExecuteRegisteredAsyncTasks_Load (Page p)
  746. {
  747. BeginEventHandler bh = new BeginEventHandler (BeginGetAsyncData);
  748. EndEventHandler eh = new EndEventHandler (EndGetAsyncData);
  749. p.AddOnPreRenderCompleteAsync (bh, eh);
  750. p.ExecuteRegisteredAsyncTasks ();
  751. }
  752. static WebRequest myRequest;
  753. public static void AddOnPreRenderCompleteAsync_Load (Page p)
  754. {
  755. BeginEventHandler bh = new BeginEventHandler(BeginGetAsyncData);
  756. EndEventHandler eh = new EndEventHandler(EndGetAsyncData);
  757. p.AddOnPreRenderCompleteAsync(bh, eh);
  758. // Initialize the WebRequest.
  759. string address = "http://MyPage.aspx";
  760. myRequest = WebRequest.Create(address);
  761. }
  762. public static void AddOnPreRenderCompleteAsyncBeginThrows_Load (Page p)
  763. {
  764. BeginEventHandler bh = new BeginEventHandler (BeginGetAsyncDataThrows);
  765. EndEventHandler eh = new EndEventHandler (EndGetAsyncData);
  766. p.AddOnPreRenderCompleteAsync (bh, eh);
  767. // Initialize the WebRequest.
  768. string address = "http://MyPage.aspx";
  769. myRequest = WebRequest.Create (address);
  770. }
  771. public static void AddOnPreRenderCompleteAsyncEndThrows_Load (Page p)
  772. {
  773. BeginEventHandler bh = new BeginEventHandler (BeginGetAsyncData);
  774. EndEventHandler eh = new EndEventHandler (EndGetAsyncDataThrows);
  775. p.AddOnPreRenderCompleteAsync (bh, eh);
  776. // Initialize the WebRequest.
  777. string address = "http://MyPage.aspx";
  778. myRequest = WebRequest.Create (address);
  779. }
  780. static IAsyncResult BeginGetAsyncData (Object src, EventArgs args, AsyncCallback cb, Object state)
  781. {
  782. if (WebTest.CurrentTest.UserData == null) {
  783. ArrayList list = new ArrayList ();
  784. list.Add ("BeginGetAsyncData");
  785. WebTest.CurrentTest.UserData = list;
  786. }
  787. else {
  788. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  789. if (list == null)
  790. throw new NullReferenceException ();
  791. list.Add ("BeginGetAsyncData");
  792. WebTest.CurrentTest.UserData = list;
  793. }
  794. return new Customresult(); // myRequest.BeginGetResponse (cb, state);
  795. }
  796. static IAsyncResult BeginGetAsyncDataThrows (Object src, EventArgs args, AsyncCallback cb, Object state)
  797. {
  798. ArrayList list = null;
  799. if (WebTest.CurrentTest.UserData == null) {
  800. list = new ArrayList ();
  801. }
  802. else {
  803. list = WebTest.CurrentTest.UserData as ArrayList;
  804. if (list == null)
  805. throw new NullReferenceException ();
  806. }
  807. list.Add ("BeginGetAsyncData");
  808. WebTest.CurrentTest.UserData = list;
  809. throw new Exception ("BeginGetAsyncDataThrows");
  810. }
  811. static void EndGetAsyncData (IAsyncResult ar)
  812. {
  813. if (WebTest.CurrentTest.UserData == null) {
  814. ArrayList list = new ArrayList ();
  815. list.Add ("EndGetAsyncData");
  816. WebTest.CurrentTest.UserData = list;
  817. }
  818. else {
  819. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  820. if (list == null)
  821. throw new NullReferenceException ();
  822. list.Add ("EndGetAsyncData");
  823. WebTest.CurrentTest.UserData = list;
  824. }
  825. }
  826. static void EndGetAsyncDataThrows (IAsyncResult ar)
  827. {
  828. ArrayList list = null;
  829. if (WebTest.CurrentTest.UserData == null) {
  830. list = new ArrayList ();
  831. }
  832. else {
  833. list = WebTest.CurrentTest.UserData as ArrayList;
  834. if (list == null)
  835. throw new NullReferenceException ();
  836. }
  837. list.Add ("EndGetAsyncData");
  838. WebTest.CurrentTest.UserData = list;
  839. throw new Exception ("EndGetAsyncDataThrows");
  840. }
  841. [Test]
  842. public void AsyncMode ()
  843. {
  844. TestPage p = new TestPage ();
  845. Assert.AreEqual (false, p.AsyncMode, "AsyncMode#1");
  846. p.AsyncMode = true;
  847. Assert.AreEqual (true, p.AsyncMode, "AsyncMode#2");
  848. }
  849. [Test]
  850. public void AsyncTimeout ()
  851. {
  852. Page p = new Page ();
  853. Assert.AreEqual (45, ((TimeSpan) p.AsyncTimeout).Seconds, "AsyncTimeout#1");
  854. p.AsyncTimeout = new TimeSpan (0, 0, 50);
  855. Assert.AreEqual (50, ((TimeSpan) p.AsyncTimeout).Seconds, "AsyncTimeout#2");
  856. }
  857. [Test]
  858. public void ClientQueryString ()
  859. {
  860. // httpContext URL cannot be set.
  861. }
  862. [Test]
  863. public void ClientScript ()
  864. {
  865. Page p = new Page ();
  866. Assert.AreEqual (typeof(ClientScriptManager), p.ClientScript.GetType(), "ClientScriptManager");
  867. }
  868. [Test]
  869. public void CreateHtmlTextWriterFromType ()
  870. {
  871. HtmlTextWriter writer = Page.CreateHtmlTextWriterFromType (null, typeof (HtmlTextWriter));
  872. Assert.IsNotNull (writer, "CreateHtmlTextWriterFromType Failed");
  873. }
  874. [Test]
  875. public void EnableEventValidation ()
  876. {
  877. Page p = new Page ();
  878. Assert.AreEqual (true, p.EnableEventValidation, "EnableEventValidation#1");
  879. p.EnableEventValidation = false;
  880. Assert.AreEqual (false, p.EnableEventValidation, "EnableEventValidation#2");
  881. }
  882. [Test]
  883. [Category ("NunitWeb")]
  884. public void Form ()
  885. {
  886. Page p = new Page ();
  887. Assert.AreEqual (null, p.Form, "Form#1");
  888. WebTest t = new WebTest (PageInvoker.CreateOnLoad (Form_Load));
  889. t.Run ();
  890. }
  891. public static void Form_Load (Page p)
  892. {
  893. Assert.IsNotNull (p.Form, "Form#2");
  894. Assert.AreEqual ("form1", p.Form.ID, "Form#3");
  895. Assert.AreEqual (typeof (HtmlForm), p.Form.GetType (), "Form#4");
  896. }
  897. [Test]
  898. public void GetWrappedFileDependencies ()
  899. {
  900. TestPage p = new TestPage ();
  901. string []s = { "test.aspx","fake.aspx" };
  902. object list = p.GetWrappedFileDependencies (s);
  903. Assert.AreEqual (typeof(String[]), list.GetType (), "GetWrappedFileDependencie#1");
  904. Assert.AreEqual (2, ((String[]) list).Length, "GetWrappedFileDependencie#2");
  905. Assert.AreEqual ("test.aspx", ((String[]) list)[0], "GetWrappedFileDependencie#3");
  906. Assert.AreEqual ("fake.aspx", ((String[]) list)[1], "GetWrappedFileDependencie#4");
  907. }
  908. [Test]
  909. public void IdSeparator ()
  910. {
  911. TestPage p = new TestPage ();
  912. Assert.AreEqual ('$', p.IdSeparator, "IdSeparator");
  913. }
  914. [Test]
  915. [Category ("NunitWeb")]
  916. public void InitializeCulture ()
  917. {
  918. WebTest t = new WebTest ("PageCultureTest.aspx");
  919. string PageRenderHtml = t.Run ();
  920. ArrayList eventlist = t.UserData as ArrayList;
  921. if (eventlist == null)
  922. Assert.Fail ("User data does not been created fail");
  923. Assert.AreEqual ("InitializeCulture:0", eventlist[0], "Live Cycle Flow #0");
  924. Assert.AreEqual ("OnPreInit", eventlist[1], "Live Cycle Flow #1");
  925. Assert.AreEqual ("OnInit", eventlist[2], "Live Cycle Flow #2");
  926. Assert.AreEqual ("OnInitComplete", eventlist[3], "Live Cycle Flow #3");
  927. Assert.AreEqual ("OnPreLoad", eventlist[4], "Live Cycle Flow #4");
  928. Assert.AreEqual ("OnLoad", eventlist[5], "Live Cycle Flow #5");
  929. Assert.AreEqual ("OnLoadComplete", eventlist[6], "Live Cycle Flow #6");
  930. Assert.AreEqual ("OnPreRender", eventlist[7], "Live Cycle Flow #7");
  931. Assert.AreEqual ("OnPreRenderComplete", eventlist[8], "Live Cycle Flow #8");
  932. Assert.AreEqual ("OnSaveStateComplete", eventlist[9], "Live Cycle Flow #9");
  933. Assert.AreEqual ("OnUnload", eventlist[10], "Live Cycle Flow #10");
  934. }
  935. [Test]
  936. public void IsAsync ()
  937. {
  938. Page p = new Page ();
  939. Assert.AreEqual (false, p.IsAsync, "IsAsync");
  940. }
  941. [Test]
  942. public void IsCallback ()
  943. {
  944. Page p = new Page ();
  945. Assert.AreEqual (false, p.IsCallback, "IsCallback");
  946. }
  947. [Test]
  948. public void IsCrossPagePostBack ()
  949. {
  950. Page p = new Page ();
  951. Assert.AreEqual (false, p.IsCrossPagePostBack, "IsCrossPagePostBack");
  952. }
  953. [Test]
  954. public void Items ()
  955. {
  956. Page p = new Page ();
  957. IDictionary d = p.Items;
  958. d.Add ("key", "test");
  959. Assert.AreEqual (1, p.Items.Count, "Items#1");
  960. Assert.AreEqual ("test", p.Items["key"].ToString(), "Items#2");
  961. }
  962. [Test]
  963. public void MaintainScrollPositionOnPostBack ()
  964. {
  965. Page p = new Page ();
  966. Assert.AreEqual (false, p.MaintainScrollPositionOnPostBack, "MaintainScrollPositionOnPostBack#1");
  967. p.MaintainScrollPositionOnPostBack = true;
  968. Assert.AreEqual (true, p.MaintainScrollPositionOnPostBack, "MaintainScrollPositionOnPostBack#2");
  969. }
  970. [Test]
  971. [Category("NunitWeb")]
  972. public void Master ()
  973. {
  974. Page p = new Page ();
  975. Assert.AreEqual (null, p.Master, "Master#1");
  976. WebTest t = new WebTest ("MyPageWithMaster.aspx");
  977. t.Invoker = PageInvoker.CreateOnLoad (Master_Load);
  978. t.Run ();
  979. Assert.AreEqual ("asp.my_master", t.UserData.ToString ().ToLower(), "Master#2");
  980. }
  981. public static void Master_Load (Page p)
  982. {
  983. WebTest.CurrentTest.UserData = p.Master.GetType().ToString();
  984. }
  985. [Test]
  986. public void MasterPageFile ()
  987. {
  988. Page p = new Page ();
  989. Assert.AreEqual (null, p.MasterPageFile, "MasterPageFile#1");
  990. p.MasterPageFile = "test";
  991. Assert.AreEqual ("test", p.MasterPageFile, "MasterPageFile#2");
  992. }
  993. [Test]
  994. [Category ("NotWorking")]
  995. public void MaxPageStateFieldLength ()
  996. {
  997. Page p = new Page ();
  998. Assert.AreEqual (-1, p.MaxPageStateFieldLength, "MaxPageStateFieldLength#1");
  999. p.MaxPageStateFieldLength = 10;
  1000. Assert.AreEqual (10, p.MaxPageStateFieldLength, "MaxPageStateFieldLength#2");
  1001. }
  1002. [Test]
  1003. public void PageAdapterWithNoAdapter ()
  1004. {
  1005. Page p = new Page ();
  1006. Assert.AreEqual (null, p.PageAdapter, "PageAdapter");
  1007. }
  1008. [Test]
  1009. public void PageAdapterWithPageAdapter ()
  1010. {
  1011. TestPageWithAdapter p = new TestPageWithAdapter ();
  1012. Assert.AreEqual (p.page_adapter, p.PageAdapter, "PageAdapter");
  1013. }
  1014. [Test]
  1015. public void PageAdapterWithControlAdapter ()
  1016. {
  1017. TestPageWithControlAdapter p = new TestPageWithControlAdapter ();
  1018. Assert.AreEqual (null, p.PageAdapter, "PageAdapter");
  1019. }
  1020. [Test]
  1021. public void PreviousPage ()
  1022. {
  1023. // NUnit.Framework limitation for server.transfer
  1024. }
  1025. [Test]
  1026. public void RegisterRequiresViewStateEncryption ()
  1027. {
  1028. Page p = new Page ();
  1029. p.ViewStateEncryptionMode = ViewStateEncryptionMode.Always;
  1030. p.RegisterRequiresViewStateEncryption ();
  1031. // No changes after the Encryption
  1032. }
  1033. [Test]
  1034. public void Theme ()
  1035. {
  1036. Page p = new Page ();
  1037. Assert.AreEqual (null, p.Theme, "Theme#1");
  1038. p.Theme = "Theme.skin";
  1039. Assert.AreEqual ("Theme.skin",p.Theme, "Theme#2");
  1040. }
  1041. [Test]
  1042. [Category ("NotWorking")]
  1043. public void UniqueFilePathSuffix ()
  1044. {
  1045. TestPage p = new TestPage ();
  1046. if (!p.UniqueFilePathSuffix.StartsWith ("__ufps=")) {
  1047. Assert.Fail ("UniqueFilePathSuffix");
  1048. }
  1049. }
  1050. [Test]
  1051. public void ViewStateEncryptionModeTest ()
  1052. {
  1053. Page p = new Page ();
  1054. Assert.AreEqual (ViewStateEncryptionMode.Auto, p.ViewStateEncryptionMode, "ViewStateEncryptionMode#1");
  1055. p.ViewStateEncryptionMode = ViewStateEncryptionMode.Never;
  1056. Assert.AreEqual (ViewStateEncryptionMode.Never, p.ViewStateEncryptionMode, "ViewStateEncryptionMode#2");
  1057. }
  1058. [Test]
  1059. [ExpectedException (typeof (InvalidOperationException))]
  1060. public void GetDataItem_Exception ()
  1061. {
  1062. Page p = new Page ();
  1063. p.GetDataItem ();
  1064. }
  1065. #region help_classes
  1066. class Customresult : IAsyncResult
  1067. {
  1068. #region IAsyncResult Members
  1069. public object AsyncState
  1070. {
  1071. get { throw new Exception ("The method or operation is not implemented."); }
  1072. }
  1073. public WaitHandle AsyncWaitHandle
  1074. {
  1075. get { throw new Exception ("The method or operation is not implemented."); }
  1076. }
  1077. public bool CompletedSynchronously
  1078. {
  1079. get { return true; }
  1080. }
  1081. public bool IsCompleted
  1082. {
  1083. get { throw new Exception ("The method or operation is not implemented."); }
  1084. }
  1085. #endregion
  1086. }
  1087. #endregion
  1088. [Test]
  1089. [Category ("NunitWeb")]
  1090. public void ProcessPostData_Second_Try () //Just flow and not implementation detail
  1091. {
  1092. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ProcessPostData_Second_Try_Load));
  1093. string html = t.Run ();
  1094. FormRequest fr = new FormRequest (t.Response, "form1");
  1095. fr.Controls.Add ("__EVENTTARGET");
  1096. fr.Controls.Add ("__EVENTARGUMENT");
  1097. fr.Controls ["__EVENTTARGET"].Value = "__Page";
  1098. fr.Controls ["__EVENTARGUMENT"].Value = "";
  1099. t.Request = fr;
  1100. t.Run ();
  1101. Assert.AreEqual ("CustomPostBackDataHandler_LoadPostData", t.UserData, "User data does not been created fail");
  1102. }
  1103. public static void ProcessPostData_Second_Try_Load (Page p)
  1104. {
  1105. CustomPostBackDataHandler c = new CustomPostBackDataHandler ();
  1106. c.ID = "CustomPostBackDataHandler1";
  1107. p.Form.Controls.Add (c);
  1108. }
  1109. class CustomPostBackDataHandler : WebControl, IPostBackDataHandler
  1110. {
  1111. protected internal override void OnInit (EventArgs e)
  1112. {
  1113. base.OnInit (e);
  1114. Page.RegisterRequiresPostBack (this);
  1115. }
  1116. #region IPostBackDataHandler Members
  1117. public bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
  1118. {
  1119. WebTest.CurrentTest.UserData = "CustomPostBackDataHandler_LoadPostData";
  1120. return false;
  1121. }
  1122. public void RaisePostDataChangedEvent ()
  1123. {
  1124. }
  1125. #endregion
  1126. }
  1127. [Test]
  1128. [Category ("NunitWeb")]
  1129. public void RegisterRequiresPostBack () //Just flow and not implementation detail
  1130. {
  1131. PageDelegates delegates = new PageDelegates ();
  1132. delegates.Init = RegisterRequiresPostBack_Init;
  1133. delegates.Load = RegisterRequiresPostBack_Load;
  1134. WebTest t = new WebTest (new PageInvoker (delegates));
  1135. string html = t.Run ();
  1136. FormRequest fr = new FormRequest (t.Response, "form1");
  1137. fr.Controls.Add ("__EVENTTARGET");
  1138. fr.Controls.Add ("__EVENTARGUMENT");
  1139. fr.Controls ["__EVENTTARGET"].Value = "__Page";
  1140. fr.Controls ["__EVENTARGUMENT"].Value = "";
  1141. t.Request = fr;
  1142. html = t.Run ();
  1143. Assert.AreEqual ("CustomPostBackDataHandler2_LoadPostData", t.UserData, "RegisterRequiresPostBack#1");
  1144. t.UserData = null;
  1145. fr = new FormRequest (t.Response, "form1");
  1146. fr.Controls.Add ("__EVENTTARGET");
  1147. fr.Controls.Add ("__EVENTARGUMENT");
  1148. fr.Controls ["__EVENTTARGET"].Value = "__Page";
  1149. fr.Controls ["__EVENTARGUMENT"].Value = "";
  1150. t.Request = fr;
  1151. html = t.Run ();
  1152. Assert.AreEqual (null, t.UserData, "RegisterRequiresPostBack#2");
  1153. }
  1154. public static void RegisterRequiresPostBack_Init (Page p)
  1155. {
  1156. CustomPostBackDataHandler2 c = new CustomPostBackDataHandler2 ();
  1157. c.ID = "CustomPostBackDataHandler2";
  1158. p.Form.Controls.Add (c);
  1159. }
  1160. public static void RegisterRequiresPostBack_Load (Page p)
  1161. {
  1162. if (!p.IsPostBack)
  1163. p.RegisterRequiresPostBack (p.Form.FindControl ("CustomPostBackDataHandler2"));
  1164. }
  1165. class CustomPostBackDataHandler2 : WebControl, IPostBackDataHandler
  1166. {
  1167. #region IPostBackDataHandler Members
  1168. public bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection) {
  1169. WebTest.CurrentTest.UserData = "CustomPostBackDataHandler2_LoadPostData";
  1170. return false;
  1171. }
  1172. public void RaisePostDataChangedEvent () {
  1173. }
  1174. #endregion
  1175. }
  1176. [Test]
  1177. [Category ("NunitWeb")]
  1178. public void ClearErrorOnErrorTest ()
  1179. {
  1180. WebTest t = new WebTest ("ClearErrorOnError.aspx");
  1181. string html = t.Run ();
  1182. Assert.AreEqual (HttpStatusCode.OK, t.Response.StatusCode);
  1183. }
  1184. [Test]
  1185. [Category ("NunitWeb")]
  1186. public void RedirectOnErrorTest ()
  1187. {
  1188. WebTest t = new WebTest ("RedirectOnError.aspx");
  1189. string html = t.Run ();
  1190. Assert.AreEqual (HttpStatusCode.Found, t.Response.StatusCode);
  1191. }
  1192. #endif
  1193. [TestFixtureTearDown]
  1194. public void TearDown ()
  1195. {
  1196. WebTest.Unload ();
  1197. }
  1198. }
  1199. #if NET_2_0
  1200. class TestHtmlInputHidden : global::System.Web.UI.HtmlControls.HtmlInputHidden
  1201. {
  1202. protected override bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  1203. {
  1204. string data = postCollection [postDataKey];
  1205. if (data != null && data != Value) {
  1206. Value = data;
  1207. return true;
  1208. }
  1209. return false;
  1210. }
  1211. }
  1212. class TestAdapter : global::System.Web.UI.Adapters.PageAdapter
  1213. {
  1214. public override StringCollection CacheVaryByParams {
  1215. get {
  1216. StringCollection paramNames = new StringCollection();
  1217. paramNames.AddRange (new string[] {"param-from-adapter"});
  1218. return paramNames;
  1219. }
  1220. }
  1221. public override StringCollection CacheVaryByHeaders {
  1222. get {
  1223. StringCollection headerNames = new StringCollection();
  1224. headerNames.AddRange (new string[] {"header-from-adapter"});
  1225. return headerNames;
  1226. }
  1227. }
  1228. PageStatePersister persister;
  1229. public override PageStatePersister GetStatePersister ()
  1230. {
  1231. if (persister == null)
  1232. persister = new TestPersister(Page);
  1233. return persister;
  1234. }
  1235. protected internal override string GetPostBackFormReference (string formId)
  1236. {
  1237. return String.Format("/* testFormReference */{0}",
  1238. base.GetPostBackFormReference (formId));
  1239. }
  1240. public override NameValueCollection DeterminePostBackMode ()
  1241. {
  1242. NameValueCollection origRequestValues = base.DeterminePostBackMode ();
  1243. if (origRequestValues == null)
  1244. return null;
  1245. NameValueCollection requestValues = new NameValueCollection ();
  1246. requestValues.Add (origRequestValues);
  1247. requestValues ["DeterminePostBackModeTestField"]
  1248. = "DeterminePostBackModeTestValue";
  1249. return requestValues;
  1250. }
  1251. internal new void RenderPostBackEvent (HtmlTextWriter w,
  1252. string target,
  1253. string argument,
  1254. string softKeyLabel,
  1255. string text,
  1256. string postUrl,
  1257. string accessKey,
  1258. bool encode)
  1259. {
  1260. base.RenderPostBackEvent (w, target, argument, softKeyLabel, text, postUrl,
  1261. accessKey, encode);
  1262. }
  1263. }
  1264. class TestPersister : HiddenFieldPageStatePersister
  1265. {
  1266. public TestPersister (Page p) : base (p)
  1267. {
  1268. }
  1269. }
  1270. public class TestPageWithAdapter : Page
  1271. {
  1272. public global::System.Web.UI.Adapters.PageAdapter page_adapter;
  1273. public TestPageWithAdapter () : base ()
  1274. {
  1275. page_adapter = new TestAdapter ();
  1276. WebTest t = WebTest.CurrentTest;
  1277. if (t != null)
  1278. t.Invoke (this);
  1279. }
  1280. protected override global::System.Web.UI.Adapters.ControlAdapter ResolveAdapter ()
  1281. {
  1282. return page_adapter;
  1283. }
  1284. public new PageStatePersister PageStatePersister {
  1285. get { return base.PageStatePersister; }
  1286. }
  1287. }
  1288. public class TestControlAdapter : ControlAdapter
  1289. {
  1290. }
  1291. public class TestPageWithControlAdapter : Page
  1292. {
  1293. private global::System.Web.UI.Adapters.ControlAdapter control_adapter;
  1294. public TestPageWithControlAdapter () : base ()
  1295. {
  1296. control_adapter = new TestControlAdapter ();
  1297. WebTest t = WebTest.CurrentTest;
  1298. if (t != null)
  1299. t.Invoke (this);
  1300. }
  1301. protected override global::System.Web.UI.Adapters.ControlAdapter ResolveAdapter ()
  1302. {
  1303. return control_adapter;
  1304. }
  1305. }
  1306. #endif
  1307. }