PageTest.cs 46 KB

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