PageTest.cs 35 KB

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