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