Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / ControlTest.cs
1 //
2 // Tests for System.Web.UI.Control
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@novell.com)
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.Collections;
35 using System.Drawing;
36 using System.IO;
37 using System.Globalization;
38 using System.Web;
39 using System.Web.UI;
40 using System.Web.UI.WebControls;
41 using MonoTests.SystemWeb.Framework;
42 using MonoTests.stand_alone.WebHarness;
43 using MonoTests.Common;
44
45 using System.Web.UI.Adapters;
46
47 using System.Web.Routing;
48
49 namespace MonoTests.System.Web.UI
50 {
51         [TestFixture]
52         public class ControlTest
53         {
54                 [Test]
55                 public void DataBindingInterfaceTest ()
56                 {
57                         Control c;
58                         DataBindingCollection db;
59
60                         c = new Control ();
61                         Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB1");
62                         db = ((IDataBindingsAccessor) c).DataBindings;
63                         Assert.IsNotNull (db, "DB2");
64                         Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB3");
65                         db.Add (new DataBinding ("property", typeof (bool), "expression"));
66                         Assert.AreEqual (true, ((IDataBindingsAccessor) c).HasDataBindings);
67                 }
68
69                 [Test] // bug #82546
70                 public void FindControl_NamingContainer ()
71                 {
72                         Control topControl = new NamingContainer ();
73                         topControl.ID = "top";
74
75                         Control controlLevel1A = new Control ();
76                         controlLevel1A.ID = "Level1#A";
77                         topControl.Controls.Add (controlLevel1A);
78
79                         Control controlLevel1B = new Control ();
80                         controlLevel1B.ID = "Level1#B";
81                         topControl.Controls.Add (controlLevel1B);
82
83                         Control controlLevel2AA = new Control ();
84                         controlLevel2AA.ID = "Level2#AA";
85                         controlLevel1A.Controls.Add (controlLevel2AA);
86
87                         Control controlLevel2AB = new Control ();
88                         controlLevel2AB.ID = "Level2#AB";
89                         controlLevel1A.Controls.Add (controlLevel2AB);
90
91                         Control foundControl = topControl.FindControl ("Level1#A");
92                         Assert.IsNotNull (foundControl, "#A1");
93                         Assert.AreSame (controlLevel1A, foundControl, "#A2");
94
95                         foundControl = topControl.FindControl ("LEVEL1#B");
96                         Assert.IsNotNull (foundControl, "#B1");
97                         Assert.AreSame (controlLevel1B, foundControl, "#B2");
98
99                         foundControl = topControl.FindControl ("LeVeL2#AB");
100                         Assert.IsNotNull (foundControl, "#C1");
101                         Assert.AreSame (controlLevel2AB, foundControl, "#C2");
102
103                         foundControl = topControl.FindControl ("doesnotexist");
104                         Assert.IsNull (foundControl, "#D1");
105                         foundControl = topControl.FindControl ("top");
106                         Assert.IsNull (foundControl, "#D2");
107
108                         foundControl = controlLevel1A.FindControl ("Level2#AA");
109                         Assert.IsNotNull (foundControl, "#E1");
110                         Assert.AreSame (controlLevel2AA, foundControl, "#E2");
111
112                         foundControl = controlLevel1A.FindControl ("LEveL2#ab");
113                         Assert.IsNotNull (foundControl, "#F1");
114                         Assert.AreSame (controlLevel2AB, foundControl, "#F2");
115                 }
116
117                 [Test]
118                 public void UniqueID1 ()
119                 {
120                         // Standalone NC
121                         Control nc = new MyNC ();
122                         Assert.IsNull (nc.UniqueID, "nulltest");
123                 }
124                 
125                 [Test]
126                 public void UniqueID1_1 () {
127                         // Standalone NC
128                         Control nc = new MyNC ();
129                         Page p = new Page ();
130                         p.Controls.Add (nc);
131                         Assert.IsNotNull (nc.UniqueID, "notnull");
132                 }
133                 
134                 [Test]
135                 public void UniqueID2 ()
136                 {
137                         // NC in NC
138                         Control nc = new MyNC ();
139                         Control nc2 = new MyNC ();
140                         nc2.Controls.Add (nc);
141                         Assert.IsNotNull (nc.UniqueID, "notnull");
142                         Assert.IsTrue (nc.UniqueID.IndexOfAny (new char[] { ':', '$' }) == -1, "separator");
143                 }
144
145                 [Test]
146                 public void UniqueID2_1 () {
147                         // NC in NC
148                         Control nc = new MyNC ();
149                         Control nc2 = new MyNC ();
150                         nc2.Controls.Add (nc);
151                         Page p = new Page ();
152                         p.Controls.Add (nc2);
153                         Assert.IsNotNull (nc.UniqueID, "notnull");
154                         Assert.IsTrue (nc.UniqueID.IndexOfAny (new char [] { ':', '$' }) != -1, "separator");
155                 }
156
157                 [Test]
158                 public void UniqueID3 ()
159                 {
160                         // NC in control
161                         Control control = new Control ();
162                         Control nc = new MyNC ();
163
164                         control.Controls.Add (nc);
165                         Assert.IsNull (nc.UniqueID, "null");
166                 }
167
168                 [Test]
169                 public void UniqueID4 ()
170                 {
171                         // NC in control
172                         Control control = new Control ();
173                         Control nc = new MyNC ();
174
175                         nc.Controls.Add (control);
176                         Assert.IsNotNull (control.UniqueID, "notnull");
177                 }
178
179                 [Test]
180                 public void UniqueID5 ()
181                 {
182                         // NC in control
183                         Control control = new Control ();
184                         Control nc = new MyNC ();
185                         Control nc2 = new MyNC ();
186
187                         nc2.Controls.Add (nc);
188                         nc.Controls.Add (control);
189                         Assert.IsNotNull (control.UniqueID, "notnull");
190                         Assert.IsNull (nc2.ID, "null-1");
191                         Assert.IsNull (nc.ID, "null-2");
192                         Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char[] { ':', '$' }), "separator");
193                 }
194
195                 [Test]
196                 public void UniqueID6 () {
197                         // NC in NC
198                         Control nc = new MyNC ();
199                         Page p = new Page ();
200                         p.Controls.Add (nc);
201                         Assert.IsNotNull (nc.UniqueID, "notnull");
202
203                         Control c1 = new Control ();
204                         Control c2 = new Control ();
205                         nc.Controls.Add (c1);
206                         nc.Controls.Add (c2);
207                         string uid1_1 = c1.UniqueID;
208                         string uid2_1 = c2.UniqueID;
209
210                         nc.Controls.Clear ();
211
212                         Assert.IsNull (c1.UniqueID);
213                         Assert.IsNull (c2.UniqueID);
214
215                         // ad in another order
216                         nc.Controls.Add (c2);
217                         nc.Controls.Add (c1);
218                         string uid1_2 = c1.UniqueID;
219                         string uid2_2 = c2.UniqueID;
220
221                         Assert.IsFalse (uid1_1 == uid1_2);
222                         Assert.IsFalse (uid2_1 == uid2_2);
223                         Assert.AreEqual (uid1_1, uid2_2);
224                         Assert.AreEqual (uid2_1, uid1_2);
225
226                         nc.Controls.Remove (c1);
227                         nc.Controls.Add (c1);
228                         string uid1_3 = c1.UniqueID;
229                         Assert.IsFalse (uid1_3 == uid1_2, "id was not reset");
230
231                         EnsureIDControl c3 = new EnsureIDControl ();
232                         nc.Controls.Add (c3);
233                         string uid3_1 = c3.UniqueID;
234                         c3.DoEnsureID ();
235                         Assert.IsNotNull (c3.ID);
236                         nc.Controls.Remove (c3);
237                         nc.Controls.Add (c3);
238                         string uid3_2 = c3.UniqueID;
239                         Assert.IsNull (c3.ID);
240                         Assert.IsFalse (uid3_1 == uid3_2, "id was not reset");
241                 }
242
243                 [Test]
244                 public void ClientID () 
245                 {
246                         // NC in control
247                         Control control = new Control ();
248                         Control nc = new MyNC ();
249                         Control nc2 = new MyNC ();
250                         Control nc3 = new MyNC ();
251
252                         nc3.Controls.Add (nc2);
253                         nc2.Controls.Add (nc);
254                         nc.Controls.Add (control);
255                         string expected = "ctl00_ctl00_ctl00";
256                         Assert.AreEqual (expected, control.ClientID, "ClientID");
257                 }
258
259                 // From bug #76919: Control uses _controls instead of
260                 // Controls when RenderChildren is called.
261                 [Test]
262                 public void Controls1 ()
263                 {
264                         DerivedControl derived = new DerivedControl ();
265                         derived.Controls.Add (new LiteralControl ("hola"));
266                         StringWriter sw = new StringWriter ();
267                         HtmlTextWriter htw = new HtmlTextWriter (sw);
268                         derived.RenderControl (htw);
269                         string result = sw.ToString ();
270                         Assert.AreEqual ("", result, "#01");
271                 }
272
273                 [Test] // Bug #471305
274                 [Category ("NunitWeb")]
275                 public void NoDoubleOnInitOnRemoveAdd ()
276                 {
277                         WebTest.CopyResource (GetType (), "NoDoubleOnInitOnRemoveAdd.aspx", "NoDoubleOnInitOnRemoveAdd.aspx");
278                         WebTest.CopyResource (GetType (), "NoDoubleOnInitOnRemoveAdd.aspx.cs", "NoDoubleOnInitOnRemoveAdd.aspx.cs");
279                         WebTest t = new WebTest ("NoDoubleOnInitOnRemoveAdd.aspx");
280                         string html = t.Run ();
281
282                         Assert.AreEqual (-1, html.IndexOf ("<span>label</span><span>label</span>"), "#A1");
283                 }
284                 
285                 [Test]
286                 [Category("NunitWeb")]
287                 public void AppRelativeTemplateSourceDirectory ()
288                 {
289                         new WebTest(PageInvoker.CreateOnLoad(AppRelativeTemplateSourceDirectory_Load)).Run();
290                 }
291                 
292                 public static void AppRelativeTemplateSourceDirectory_Load(Page p)
293                 {
294                         Control ctrl = new Control();
295                         Assert.AreEqual("~/", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#1");
296                         ctrl.AppRelativeTemplateSourceDirectory = "~/Fake";
297                         Assert.AreEqual("~/Fake", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#2");
298                 }
299
300                 [Test]
301                 public void ApplyStyleSheetSkin ()
302                 {
303                         Page p = new Page ();
304                         p.StyleSheetTheme = "";
305                         Control c = new Control ();
306                         c.ApplyStyleSheetSkin (p);
307                 }
308
309                 [Test]
310                 [Category ("NunitWeb")]
311                 public void ApplyStyleSheetSkin_1 ()
312                 {
313                         WebTest.CopyResource (GetType (), "Theme2.skin", "App_Themes/Theme2/Theme2.skin");
314                         WebTest t = new WebTest ();
315                         PageDelegates pd = new PageDelegates ();
316                         pd.PreInit = ApplyStyleSheetSkin_PreInit;
317                         pd.Load = ApplyStyleSheetSkin_Load;
318                         t.Invoker = new PageInvoker (pd);
319                         string str = t.Run ();
320                 }
321                 public static void ApplyStyleSheetSkin_PreInit (Page p)
322                 {
323                         p.Theme = "Theme2";
324                 }
325                 public static void ApplyStyleSheetSkin_Load (Page p)
326                 {
327                         Label lbl = new Label ();
328                         lbl.ID = "StyleLbl";
329                         lbl.SkinID = "red";
330                         lbl.Text = "StyleLabel";
331                         p.Controls.Add (lbl);
332                         lbl.ApplyStyleSheetSkin (p);
333                         Assert.AreEqual (Color.Red, lbl.ForeColor, "ApplyStyleSheetSkin_BackColor");
334                         Assert.AreEqual ("TextFromSkinFile", lbl.Text, "ApplyStyleSheetSkin");
335                 }
336
337                 [Test]
338                 [Category ("NunitWeb")]
339                 public void ClearChildControlState ()
340                 {
341                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildControlState_Load));
342                         t.Run ();
343                         FormRequest fr = new FormRequest (t.Response, "form1");
344                         fr.Controls.Add ("__EVENTTARGET");
345                         fr.Controls.Add ("__EVENTARGUMENT");
346                         fr.Controls["__EVENTTARGET"].Value = "";
347                         fr.Controls["__EVENTARGUMENT"].Value = "";
348                         t.Request = fr;
349                         t.Run ();
350                 }
351                 public static void ClearChildControlState_Load (Page p)
352                 {
353                         ControlWithState c1 = new ControlWithState ();
354                         p.Form.Controls.Add (c1);
355                         if (p.IsPostBack) {
356                                 c1.ClearChildControlState ();
357                         }
358                         ControlWithState c2 = new ControlWithState ();
359                         c1.Controls.Add (c2);
360                         ControlWithState c3 = new ControlWithState ();
361                         c2.Controls.Add (c3);
362                         if (!p.IsPostBack) {
363                                 c1.State = "State";
364                                 c2.State = "Cool";
365                                 c3.State = "SubCool";
366                         }
367                         else {
368                                 Assert.AreEqual ("State", c1.State, "ControlState#1");
369                                 Assert.AreEqual (null, c2.State, "ControlState#2");
370                                 Assert.AreEqual (null, c3.State, "ControlState#2");
371                         }
372                 }
373
374                 [Test]
375                 [Category ("NunitWeb")]
376                 public void ClearChildState ()
377                 {
378                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildState_Load));
379                         t.Run ();
380                         FormRequest fr = new FormRequest (t.Response, "form1");
381                         fr.Controls.Add ("__EVENTTARGET");
382                         fr.Controls.Add ("__EVENTARGUMENT");
383                         fr.Controls["__EVENTTARGET"].Value = "";
384                         fr.Controls["__EVENTARGUMENT"].Value = "";
385                         t.Request = fr;
386                         t.Run ();
387                 }
388                 public static void ClearChildState_Load (Page p)
389                 {
390                         ControlWithState c1 = new ControlWithState ();
391                         p.Form.Controls.Add (c1);
392                         if (p.IsPostBack) {
393                                 c1.ClearChildState ();
394                         }
395                         ControlWithState c2 = new ControlWithState ();
396                         c1.Controls.Add (c2);
397                         ControlWithState c3 = new ControlWithState ();
398                         c2.Controls.Add (c3);
399                         if (!p.IsPostBack) {
400                                 c1.State = "State";
401                                 c2.State = "Cool";
402                                 c2.Viewstate = "Very Cool";
403                                 c3.State = "SubCool";
404                                 c3.Viewstate = "Super Cool";
405                         }
406                         else {
407                                 Assert.AreEqual ("State", c1.State, "ClearChildState#1");
408                                 Assert.AreEqual (null, c2.State, "ClearChildState#2");
409                                 Assert.AreEqual (null, c3.State, "ClearChildState#3");
410                                 Assert.AreEqual (null, c2.Viewstate, "ClearChildState#4");
411                                 Assert.AreEqual (null, c3.Viewstate, "ClearChildState#5");
412                         }
413                 }
414
415                 [Test]
416                 public void DataBind ()
417                 {
418                         MyNC ctrl = new MyNC ();
419                         ctrl.DataBinding += new EventHandler (ctrl_DataBinding);
420                         Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
421                         ctrl.DataBind (false);
422                         Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
423                         ctrl.DataBind (true);
424                         Assert.AreEqual (true, _eventDataBinding, "After DataBinding");
425                 }
426                 bool _eventDataBinding;
427                 void ctrl_DataBinding (object sender, EventArgs e)
428                 {
429                         _eventDataBinding = true;
430                 }
431
432                 [Test]
433                 public void DataBindChildren ()
434                 {
435                         MyNC ctrl1 = new MyNC ();
436                         Control ctrl2 = new Control ();
437                         Control ctrl3 = new Control ();
438                         ctrl2.DataBinding += new EventHandler (ctrl2_DataBinding);
439                         ctrl3.DataBinding += new EventHandler (ctrl3_DataBinding);
440
441                         ctrl2.Controls.Add (ctrl3);
442                         ctrl1.Controls.Add (ctrl2);
443                         Assert.AreEqual (false, _eventChild1, "Before DataBinding#1");
444                         Assert.AreEqual (false, _eventChild2, "Before DataBinding#2");
445                         ctrl1.DataBindChildren ();
446                         Assert.AreEqual (true, _eventChild1, "After DataBinding#1");
447                         Assert.AreEqual (true, _eventChild2, "After DataBinding#2");
448                 }
449                 bool _eventChild1;
450                 bool _eventChild2;
451                 void ctrl3_DataBinding (object sender, EventArgs e)
452                 {
453                         _eventChild1 = true;
454                 }
455                 void ctrl2_DataBinding (object sender, EventArgs e)
456                 {
457                         _eventChild2 = true;
458                 }
459
460                 [Test]
461                 public void EnsureID ()
462                 {
463                         MyNC ctrl = new MyNC ();
464                         MyNC ctrl1 = new MyNC ();
465                         ctrl.Controls.Add (ctrl1);
466                         Page p = new Page ();
467                         p.Controls.Add (ctrl);
468                         ctrl.EnsureID ();
469                         if (String.IsNullOrEmpty (ctrl.ID))
470                                 Assert.Fail ("EnsureID#1");
471                         ctrl1.EnsureID ();
472                         if (String.IsNullOrEmpty (ctrl1.ID))
473                                 Assert.Fail ("EnsureID#2");
474                 }
475
476                 [Test]
477                 [Category("NotWorking")]
478                 public void Focus ()
479                 {
480                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (Focus_Load));
481                         string html = t.Run ();
482                         Assert.AreEqual (3, contain (html, "TestBox"), "Focus script not created");
483
484                 }
485                 public static void Focus_Load (Page p)
486                 {
487                         TextBox tbx = new TextBox ();
488                         tbx.ID = "TestBox";
489                         p.Controls.Add (tbx);
490                         tbx.Focus ();
491                 }
492                 int contain (string orig, string compare)
493                 {
494                         if (orig.IndexOf (compare) == -1)
495                                 return 0;
496                         return 1 + contain (orig.Substring (orig.IndexOf (compare) + compare.Length), compare);
497                 }
498
499                 [Test]
500                 public void HasEvent ()
501                 {
502                         MyNC ctrl1 = new MyNC ();
503                         Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#1");
504                         EventHandler ctrl_hdlr = new EventHandler (ctrl1_Init);
505                         ctrl1.Init += new EventHandler (ctrl1_Init);
506                         ctrl1.Init += ctrl_hdlr;
507                         Assert.AreEqual (true, ctrl1.HasEvents (), "HasEvent#2");
508                         // Dosn't work than removed handler
509                         //ctrl1.Init -= ctrl_hdlr;
510                         //Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#3");
511                 }
512                 void ctrl1_Init (object sender, EventArgs e)
513                 {
514                         throw new Exception ("The method or operation is not implemented.");
515                 }
516
517                 [Test]
518                 public void IsViewStateEnabled ()
519                 {
520                         DerivedControl c = new DerivedControl ();
521                         Assert.IsTrue (c.DoIsViewStateEnabled);
522                         Page p = new Page ();
523                         c.Page = p;
524                         p.Controls.Add (c);
525                         Assert.IsTrue (c.DoIsViewStateEnabled);
526                         p.EnableViewState = false;
527                         Assert.IsFalse (c.DoIsViewStateEnabled);
528                 }
529
530                 [Test]
531                 [Category ("NunitWeb")]
532                 public void ControlState ()
533                 {
534                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
535                         t.Run ();
536                         FormRequest fr = new FormRequest (t.Response, "form1");
537                         fr.Controls.Add ("__EVENTTARGET");
538                         fr.Controls.Add ("__EVENTARGUMENT");
539                         fr.Controls["__EVENTTARGET"].Value = "";
540                         fr.Controls["__EVENTARGUMENT"].Value = "";
541                         t.Request = fr;
542                         t.Run ();
543                 }
544                 public static void ControlState_Load (Page p)
545                 {
546                         ControlWithState c1 = new ControlWithState ();
547                         ControlWithState c2 = new ControlWithState ();
548                         c1.Controls.Add (c2);
549                         p.Form.Controls.Add (c1);
550                         if (!p.IsPostBack) {
551                                 c1.State = "State";
552                                 c2.State = "Cool";
553                         }
554                         else {
555                                 ControlWithState c3 = new ControlWithState ();
556                                 p.Form.Controls.Add (c3);
557                                 Assert.AreEqual ("State", c1.State, "ControlState");
558                                 Assert.AreEqual ("Cool", c2.State, "ControlState");
559                         }
560                 }
561
562                 [Test]
563                 [Category ("NunitWeb")]
564                 public void ControlState2 () {
565                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState2_Load));
566                         t.Run ();
567                         FormRequest fr = new FormRequest (t.Response, "form1");
568                         fr.Controls.Add ("__EVENTTARGET");
569                         fr.Controls.Add ("__EVENTARGUMENT");
570                         fr.Controls ["__EVENTTARGET"].Value = "";
571                         fr.Controls ["__EVENTARGUMENT"].Value = "";
572                         t.Request = fr;
573                         t.Run ();
574                         
575                         fr = new FormRequest (t.Response, "form1");
576                         fr.Controls.Add ("__EVENTTARGET");
577                         fr.Controls.Add ("__EVENTARGUMENT");
578                         fr.Controls ["__EVENTTARGET"].Value = "";
579                         fr.Controls ["__EVENTARGUMENT"].Value = "";
580                         t.Request = fr;
581                         t.Run ();
582                 }
583                 public static void ControlState2_Load (Page p) {
584                         ControlWithState parent = new ControlWithState ();
585                         p.Form.Controls.Add (parent);
586                         if (!p.IsPostBack) {
587                                 // emulate DataBind
588                                 parent.Controls.Clear ();
589                                 parent.ClearChildControlState ();
590                                 ControlWithState c1 = new ControlWithState ();
591                                 ControlWithState c2 = new ControlWithState ();
592                                 parent.Controls.Add (c1);
593                                 parent.Controls.Add (c2);
594                                 c1.State = "State1_1";
595                                 c2.State = "State1_2";
596                                 parent.State = "First";
597                         }
598                         else if (parent.State == "First") {
599                                 // emulate DataBind
600                                 parent.Controls.Clear ();
601                                 parent.ClearChildControlState ();
602                                 ControlWithState c1 = new ControlWithState ();
603                                 ControlWithState c2 = new ControlWithState ();
604                                 parent.Controls.Add (c1);
605                                 parent.Controls.Add (c2);
606                                 c1.State = "State2_1";
607                                 c2.State = "State2_2";
608                                 parent.State = "Second";
609                         }
610                         else {
611                                 // emulate CrerateChildControl only
612                                 parent.Controls.Clear ();
613                                 ControlWithState c1 = new ControlWithState ();
614                                 ControlWithState c2 = new ControlWithState ();
615                                 parent.Controls.Add (c1);
616                                 parent.Controls.Add (c2);
617
618                                 Assert.AreEqual ("State2_1", c1.State, "ControlState#1");
619                                 Assert.AreEqual ("State2_2", c2.State, "ControlState#2");
620                         }
621                 }
622
623                 [Test]
624                 public void ClientIDSeparator ()
625                 {
626                         DerivedControl ctrl = new DerivedControl ();
627                         Assert.AreEqual (95, (int) ctrl.ClientIDSeparator, "ClientIDSeparator");
628                 }
629
630                 [Test]
631                 public void IDSeparator ()
632                 {
633                         DerivedControl ctrl = new DerivedControl ();
634                         Assert.AreEqual (36, (int) ctrl.IdSeparator, "IDSeparator");
635                 }
636
637                 [Test]
638                 [Category ("NunitWeb")]
639                 public void IsChildControlStateCleared ()
640                 {
641                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (IsChildControlStateCleared_Load));
642                         t.Run ();
643                         FormRequest fr = new FormRequest (t.Response, "form1");
644                         fr.Controls.Add ("__EVENTTARGET");
645                         fr.Controls.Add ("__EVENTARGUMENT");
646                         fr.Controls["__EVENTTARGET"].Value = "";
647                         fr.Controls["__EVENTARGUMENT"].Value = "";
648                         t.Request = fr;
649                         t.Run ();
650                 }
651                 public static void IsChildControlStateCleared_Load (Page p)
652                 {
653                         ControlWithState c1 = new ControlWithState ();
654                         p.Form.Controls.Add (c1);
655                         if (p.IsPostBack) {
656                                 Assert.IsFalse (c1.IsChildControlStateCleared, "ControlState#1");
657                                 c1.ClearChildControlState ();
658                                 Assert.IsTrue (c1.IsChildControlStateCleared, "ControlState#1");
659                         }
660                         ControlWithState c2 = new ControlWithState ();
661                         c1.Controls.Add (c2);
662                         ControlWithState c3 = new ControlWithState ();
663                         c2.Controls.Add (c3);
664                         if (p.IsPostBack) {
665                                 Assert.IsFalse (c2.IsChildControlStateCleared, "ControlState#1");
666                                 Assert.IsFalse (c3.IsChildControlStateCleared, "ControlState#1");
667                         }
668                         if (!p.IsPostBack) {
669                                 c1.State = "State";
670                                 c2.State = "Cool";
671                                 c3.State = "SubCool";
672                         }
673                 }
674
675                 [Test]
676                 [Category ("NunitWeb")]
677                 public void LoadViewStateByID ()
678                 {
679                         ControlWithState c1 = new ControlWithState ();
680                         ControlWithState c2 = new ControlWithState ();
681                         c1.Controls.Add (c2);
682                         Assert.AreEqual (false, c1.LoadViewStateByID, "LoadViewStateByID#1");
683                 }
684
685                 [Test]
686                 [Category ("NunitWeb")]
687                 public void OpenFile ()
688                 {
689                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFile_Load));
690                         t.Run ();
691                 }
692                 public static void OpenFile_Load (Page p)
693                 {
694                         DerivedControl ctrl = new DerivedControl ();
695                         Stream strem = ctrl.OpenFile ("~/MyPage.aspx");
696                         Assert.IsNotNull (strem, "OpenFile failed");
697                 }
698
699                 [Test]
700                 [Category ("NunitWeb")]
701                 [ExpectedException (typeof (FileNotFoundException))]
702                 public void OpenFile_Exception ()
703                 {
704                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFileException_Load));
705                         t.Run ();
706                 }
707                 public static void OpenFileException_Load (Page p)
708                 {
709                         DerivedControl ctrl = new DerivedControl ();
710                         Stream strem = ctrl.OpenFile ("~/Fake.tmp");
711                 }
712
713                 [Test]
714                 [Category ("NunitWeb")]
715                 public void ResolveClientUrl ()
716                 {
717                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveClientUrl_Load));
718                         string html = t.Run ();
719                 }
720                 public static void ResolveClientUrl_Load (Page p)
721                 {
722                         Control ctrl = new Control ();
723                         p.Controls.Add (ctrl);
724                         string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
725                         Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
726
727                         Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
728                         
729                         Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
730                         Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
731
732                         Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
733                         Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
734                         Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
735                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
736                         Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
737                         Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
738                         Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
739                         Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveClientUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
740                         Assert.AreEqual ("NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
741                         Assert.AreEqual ("NunitWeb/../MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
742                         Assert.AreEqual ("NunitWeb/./MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
743                         Assert.AreEqual ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
744
745                         Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
746                         Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
747
748                         Assert.AreEqual ("./MyPage.aspx", ctrl.ResolveClientUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
749                         Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
750                 
751                 }
752                 
753                 [Test]
754                 [Category ("NunitWeb")]
755                 public void ResolveClientUrl2 ()
756                 {
757                         WebTest t = new WebTest ("ResolveUrl.aspx");
758                         PageDelegates delegates = new PageDelegates ();
759                         delegates.Load = ResolveClientUrl2_Load;
760                         t.Invoker = new PageInvoker (delegates);
761                         string html = t.Run ();
762                 }
763                 
764                 public static void ResolveClientUrl2_Load (Page p)
765                 {
766                         Control uc = p.FindControl ("WebUserControl1");
767                         Control ctrl = uc.FindControl ("Label");
768                         
769                         string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
770                         Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
771
772                         Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
773
774                         Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
775                         Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
776
777                         Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
778                         Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
779                         Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
780                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
781                         Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
782                         Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
783                         Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
784                         Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveClientUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
785                         Assert.AreEqual ("Folder/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
786                         Assert.AreEqual ("Folder/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
787                         Assert.AreEqual ("Folder/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
788                         Assert.AreEqual ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
789
790                         Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
791                         Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
792
793                         Assert.AreEqual ("Folder/MyPage.aspx", ctrl.ResolveClientUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
794                         Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
795
796                 }
797
798                 [Test]
799                 [Category ("NunitWeb")]
800                 public void ResolveUrl ()
801                 {
802                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveUrl_Load));
803                         string html = t.Run ();
804                 }
805
806                 public static void ResolveUrl_Load (Page p)
807                 {
808                         string appPath = "/NunitWeb";
809                         Control ctrl = new Control ();
810                         p.Controls.Add (ctrl);
811                         Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~/MyPage.aspx"), "ResolveClientUrl Failed");
812
813                         Assert.AreEqual ("", ctrl.ResolveUrl (""), "empty string");
814
815                         Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~"), "~");
816                         Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~/"), "~/");
817
818                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
819                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
820                         Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
821                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
822                         Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
823                         Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
824                         Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
825                         Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
826                         Assert.AreEqual (appPath + "/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
827                         Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
828                         Assert.AreEqual (appPath + "/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
829                         Assert.AreEqual ("http://google.com/", ctrl.ResolveUrl ("http://google.com/"), "ResolveClientUrl Failed");
830
831                         Assert.AreEqual (appPath + "/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
832                         Assert.AreEqual ("/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
833
834                         Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
835                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
836
837                         Assert.AreEqual ("/", ctrl.ResolveUrl (".."), "..");
838                         Assert.AreEqual ("/", ctrl.ResolveUrl ("../"), "../");
839                 }
840
841                 [Test]
842                 [Category ("NunitWeb")]
843                 public void ResolveUrl2 ()
844                 {
845                         WebTest t = new WebTest ("ResolveUrl.aspx");
846                         PageDelegates delegates = new PageDelegates ();
847                         delegates.Load = ResolveUrl2_Load;
848                         t.Invoker = new PageInvoker (delegates);
849                         string html = t.Run ();
850                 }
851
852                 public static void ResolveUrl2_Load (Page p)
853                 {
854                         string appPath = "/NunitWeb";
855                         Control uc = p.FindControl ("WebUserControl1");
856                         Control ctrl = uc.FindControl ("Label");
857
858                         Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~/MyPage.aspx"), "ResolveClientUrl Failed");
859
860                         Assert.AreEqual ("", ctrl.ResolveUrl (""), "empty string");
861
862                         Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~"), "~");
863                         Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~/"), "~/");
864
865                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
866                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
867                         Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
868                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
869                         Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
870                         Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
871                         Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
872                         Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
873                         Assert.AreEqual (appPath + "/Folder/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
874                         Assert.AreEqual (appPath + "/Folder/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
875                         Assert.AreEqual (appPath + "/Folder/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
876                         Assert.AreEqual ("http://google.com/", ctrl.ResolveUrl ("http://google.com/"), "ResolveClientUrl Failed");
877
878                         Assert.AreEqual (appPath + "/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
879                         Assert.AreEqual ("/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
880
881                         Assert.AreEqual (appPath + "/Folder/MyPage.aspx", ctrl.ResolveUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
882                         Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
883
884                         Assert.AreEqual (appPath + "/", ctrl.ResolveUrl (".."), "..");
885                         Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("../"), "../");
886                         Assert.AreEqual ("/", ctrl.ResolveUrl ("../.."), "../..");
887                         Assert.AreEqual ("/", ctrl.ResolveUrl ("../../"), "../../");
888                         Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("../../MyPage.aspx"), "../../MyPage.aspx");
889                 }
890
891                 [Test]
892                 [Category ("NotWorking")] // Not implemented exception
893                 public void ResolveAdapter_2 ()
894                 {
895                         DerivedControl ctrl = new DerivedControl ();
896                         Assert.AreEqual (null, ctrl.ResolveAdapter (), "ResolveAdapter");
897                 }
898
899                 [Test]
900                 public void EnableTheming ()
901                 {
902                         DerivedControl ctrl = new DerivedControl ();
903                         Assert.AreEqual (true, ctrl.EnableTheming, "EnableTheming#1");
904                         ctrl.EnableTheming = false;
905                         Assert.AreEqual (false, ctrl.EnableTheming, "EnableTheming#2");
906                 }
907
908                 [Test]
909                 public void BindingContainer ()
910                 {
911                         ControlWithTemplate c = new ControlWithTemplate ();
912                         c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
913
914                         // cause CreateChildControls called
915                         c.FindControl ("stam");
916                 }
917
918                 static void BindingContainer_BuildTemplate (Control control)
919                 {
920                         Control child1 = new Control ();
921                         control.Controls.Add (child1);
922
923                         Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
924                         Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
925
926                         NamingContainer nc = new NamingContainer ();
927                         Control child2 = new Control ();
928                         nc.Controls.Add (child2);
929                         control.Controls.Add (nc);
930
931                         Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
932                         Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
933
934                         // DetailsViewPagerRow marked to be not BindingContainer 
935                         DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
936                         TableCell cell = new TableCell ();
937                         Control child3 = new Control ();
938                         cell.Controls.Add (child3);
939                         row.Cells.Add (cell);
940                         control.Controls.Add (row);
941
942                         Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
943                         Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
944                 }
945                 [Test]
946                 public void Control_Adapter ()
947                 {
948                         MyNC ctr = new MyNC ();
949                         Assert.AreEqual (null, ctr.Adapter (), "Adapter");
950                 }
951                 [Test]
952                 public void ChildControlsCreated () {
953                         ChildControlsCreatedControl ctr = new ChildControlsCreatedControl ();
954                         ctr.Controls.Add (new Control ());
955                         //ctr.DoEnsureChildControls ();
956
957                         Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#1");
958                         ctr.SetChildControlsCreated (false);
959                         Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#2");
960                 }
961
962                 [Test (Description="Bug #594238")]
963                 public void OverridenControlsPropertyAndPostBack_Bug594238 ()
964                 {
965                         WebTest t = new WebTest ("OverridenControlsPropertyAndPostBack_Bug594238.aspx");
966                         t.Run ();
967
968                         FormRequest fr = new FormRequest (t.Response, "form1");
969                         fr.Controls.Add ("__EVENTTARGET");
970                         fr.Controls.Add ("__EVENTARGUMENT");
971                         fr.Controls ["__EVENTTARGET"].Value = "container$children$lb";
972                         fr.Controls ["__EVENTARGUMENT"].Value = String.Empty;
973                         t.Request = fr;
974                         string originalHtml = "<span id=\"container\"><a id=\"container_children_lb\" href=\"javascript:__doPostBack(&#39;container$children$lb&#39;,&#39;&#39;)\">Woot! I got clicked!</a></span><hr/>";
975                         string pageHtml = t.Run ();
976                         string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
977
978                         HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
979                 }
980                 
981                 [TestFixtureTearDown]
982                 public void Tear_down ()
983                 {
984                         WebTest.Unload ();
985                 }
986                 
987                 [TestFixtureSetUp]
988                 public void TestFixtureSetUp ()
989                 {
990                         WebTest.CopyResource (GetType (), "ResolveUrl.aspx", "ResolveUrl.aspx");
991                         WebTest.CopyResource (GetType (), "ResolveUrl.ascx", "Folder/ResolveUrl.ascx");
992                         WebTest.CopyResource (GetType (), "OverridenControlsPropertyAndPostBack_Bug594238.aspx", "OverridenControlsPropertyAndPostBack_Bug594238.aspx");
993                 }
994
995                 [Test]
996                 public void GetRouteUrl_Object ()
997                 {
998                         var t = new WebTest (PageInvoker.CreateOnLoad (GetRouteUrl_Object_Load));
999                         t.Run ();
1000                 }
1001
1002                 public static void GetRouteUrl_Object_Load (Page p)
1003                 {
1004                         RouteTable.Routes.Clear ();
1005
1006                         var ctl = new Control ();
1007                         object obj = new { foo = "one", bar = "two" };
1008                         string path = ctl.GetRouteUrl (obj);
1009                         Assert.IsNull (path, "#A1");
1010
1011                         RouteTable.Routes.Add (new Route ("{foo}-{bar}", new PageRouteHandler ("~/default.aspx")));
1012                         path = ctl.GetRouteUrl (obj);
1013                         Assert.IsNotNull (path, "#A2-1");
1014                         Assert.AreEqual ("/NunitWeb/one-two", path, "#A2-2");
1015
1016                         path = ctl.GetRouteUrl ((object)null);
1017                         Assert.IsNull (path, "#A3");
1018                 }
1019
1020                 [Test]
1021                 public void GetRouteUrl_RouteValueDictionary ()
1022                 {
1023                         var t = new WebTest (PageInvoker.CreateOnLoad (GetRouteUrl_RouteValueDictionary_Load));
1024                         t.Run ();
1025                 }
1026
1027                 public static void GetRouteUrl_RouteValueDictionary_Load (Page p)
1028                 {
1029                         RouteTable.Routes.Clear ();
1030
1031                         var ctl = new Control ();
1032                         var rvd = new RouteValueDictionary {
1033                                 {"foo", "one"},
1034                                 {"bar", "two"}
1035                         };
1036                         string path = ctl.GetRouteUrl (rvd);
1037                         Assert.IsNull (path, "#A1");
1038
1039                         RouteTable.Routes.Add (new Route ("{foo}-{bar}", new PageRouteHandler ("~/default.aspx")));
1040                         path = ctl.GetRouteUrl (rvd);
1041                         Assert.IsNotNull (path, "#A2-1");
1042                         Assert.AreEqual ("/NunitWeb/one-two", path, "#A2-2");
1043
1044                         path = ctl.GetRouteUrl ((RouteValueDictionary) null);
1045                         Assert.IsNull (path, "#A3");
1046                 }
1047
1048                 [Test]
1049                 public void GetRouteUrl_String_Object ()
1050                 {
1051                         var t = new WebTest (PageInvoker.CreateOnLoad (GetRouteUrl_String_Object_Load));
1052                         t.Run ();
1053                 }
1054
1055                 public static void GetRouteUrl_String_Object_Load (Page p)
1056                 {
1057                         RouteTable.Routes.Clear ();
1058
1059                         var ctl = new Control ();
1060                         object obj = new { foo = "one", bar = "two" };
1061                         string path;
1062                         AssertExtensions.Throws<ArgumentException> (() => {
1063                                 path = ctl.GetRouteUrl ("myroute1", obj);
1064                         }, "#A1");
1065
1066                         RouteTable.Routes.Add (new Route ("{foo}-{bar}", new PageRouteHandler ("~/default.aspx")));
1067                         RouteTable.Routes.Add ("myroute1", new Route ("{bar}-{foo}", new PageRouteHandler ("~/default.aspx")));
1068                         path = ctl.GetRouteUrl ("myroute1", obj);
1069                         Assert.IsNotNull (path, "#A2-1");
1070                         Assert.AreEqual ("/NunitWeb/two-one", path, "#A2-2");
1071
1072                         path = ctl.GetRouteUrl ("myroute1", (object) null);
1073                         Assert.IsNull (path, "#A3");
1074                 }
1075
1076                 [Test]
1077                 public void GetRouteUrl_String_RouteValueDictionary ()
1078                 {
1079                         var t = new WebTest (PageInvoker.CreateOnLoad (GetRouteUrl_String_RouteValueDictionary_Load));
1080                         t.Run ();
1081                 }
1082
1083                 public static void GetRouteUrl_String_RouteValueDictionary_Load (Page p)
1084                 {
1085                         RouteTable.Routes.Clear ();
1086
1087                         var ctl = new Control ();
1088                         var rvd = new RouteValueDictionary {
1089                                 {"foo", "one"},
1090                                 {"bar", "two"}
1091                         };
1092                         string path;
1093                         AssertExtensions.Throws<ArgumentException> (() => {
1094                                 path = ctl.GetRouteUrl ("myroute", rvd);
1095                         }, "#A1");
1096
1097                         RouteTable.Routes.Add (new Route ("{foo}-{bar}", new PageRouteHandler ("~/default.aspx")));
1098                         RouteTable.Routes.Add ("myroute", new Route ("{bar}-{foo}", new PageRouteHandler ("~/default.aspx")));
1099                         path = ctl.GetRouteUrl ("myroute", rvd);
1100                         Assert.IsNotNull (path, "#A2-1");
1101                         Assert.AreEqual ("/NunitWeb/two-one", path, "#A2-2");
1102
1103                         path = ctl.GetRouteUrl ("myroute", (RouteValueDictionary) null);
1104                         Assert.IsNull (path, "#A3-1");
1105
1106                         path = ctl.GetRouteUrl (null, (RouteValueDictionary) null);
1107                         Assert.IsNull (path, "#A3-2");
1108
1109                         path = ctl.GetRouteUrl (String.Empty, (RouteValueDictionary) null);
1110                         Assert.IsNull (path, "#A3-3");
1111                 }
1112                 #region helpcalsses
1113                 class ControlWithState : Control
1114                 {
1115                         string _state;
1116
1117                         public string State
1118                         {
1119                                 get { return _state; }
1120                                 set { _state = value; }
1121                         }
1122
1123                         public string Viewstate {
1124                                 get { return (string) ViewState ["Viewstate"]; }
1125                                 set { ViewState ["Viewstate"] = value; }
1126                         }
1127
1128                         protected internal override void OnInit (EventArgs e)
1129                         {
1130                                 base.OnInit (e);
1131                                 Page.RegisterRequiresControlState (this);
1132                         }
1133
1134                         protected internal override object SaveControlState ()
1135                         {
1136                                 return State;
1137                         }
1138
1139                         protected internal override void LoadControlState (object savedState)
1140                         {
1141                                 State = (string) savedState;
1142                         }
1143
1144                         public new void ClearChildState ()
1145                         {
1146                                 base.ClearChildState ();
1147                         }
1148
1149                         public new void ClearChildControlState ()
1150                         {
1151                                 base.ClearChildControlState ();
1152                         }
1153
1154                         public new bool IsChildControlStateCleared
1155                         {
1156                                 get { return base.IsChildControlStateCleared; }
1157                         }
1158
1159                         public new bool LoadViewStateByID
1160                         {
1161                                 get { return base.LoadViewStateByID; }
1162                         }
1163                 }
1164
1165                 class MyNC : Control, INamingContainer
1166                 {
1167                         public ControlAdapter Adapter ()
1168                         {
1169                                 return base.Adapter;
1170                         }
1171
1172                         public new void DataBind (bool opt)
1173                         {
1174                                 base.DataBind (opt);
1175                         }
1176
1177                         public new void DataBindChildren ()
1178                         {
1179                                 base.DataBindChildren ();
1180                         }
1181
1182                         public new void EnsureID ()
1183                         {
1184                                 base.EnsureID ();
1185                         }
1186
1187                         public new bool HasEvents ()
1188                         {
1189                                 return base.HasEvents ();
1190                         }
1191                 }
1192
1193                 class DerivedControl : Control
1194                 {
1195                         ControlCollection coll;
1196
1197                         public DerivedControl ()
1198                         {
1199                                 coll = new ControlCollection (this);
1200                         }
1201
1202                         public override ControlCollection Controls
1203                         {
1204                                 get { return coll; }
1205                         }
1206
1207                         public bool DoIsViewStateEnabled
1208                         {
1209                                 get { return IsViewStateEnabled; }
1210                         }
1211
1212                         public new char ClientIDSeparator
1213                         {
1214                                 get { return base.ClientIDSeparator; }
1215                         }
1216
1217                         public new char IdSeparator
1218                         {
1219                                 get { return base.IdSeparator; }
1220                         }
1221
1222                         public new Stream OpenFile (string path)
1223                         {
1224                                 return base.OpenFile (path);
1225                         }
1226
1227                         public new ControlAdapter ResolveAdapter ()
1228                         {
1229                                 return base.ResolveAdapter ();
1230                         }
1231                 }
1232
1233                 class NamingContainer : Control, INamingContainer
1234                 {
1235                 }
1236
1237                 class ControlWithTemplate : Control
1238                 {
1239                         ITemplate _template;
1240
1241                         [TemplateContainer (typeof (TemplateContainer))]
1242                         public ITemplate Template
1243                         {
1244                                 get { return _template; }
1245                                 set { _template = value; }
1246                         }
1247
1248                         protected internal override void CreateChildControls ()
1249                         {
1250                                 Controls.Clear ();
1251
1252                                 TemplateContainer container = new TemplateContainer ();
1253                                 Controls.Add (container);
1254
1255                                 if (Template != null)
1256                                         Template.InstantiateIn (container);
1257                         }
1258                 }
1259
1260                 class TemplateContainer : Control, INamingContainer
1261                 {
1262                 }
1263                 #endregion
1264         }
1265
1266         public class Customadaptercontrol : Control
1267         {
1268                 public new ControlAdapter Adapter {
1269                         get { return base.Adapter; }
1270                 }
1271
1272                 public new ControlAdapter ResolveAdapter ()
1273                 {
1274                         return base.ResolveAdapter ();
1275                 }
1276         }
1277
1278         public class Customadapter : ControlAdapter
1279         {
1280         }
1281
1282         class EnsureIDControl : Control {
1283                 public void DoEnsureID () {
1284                         EnsureID ();
1285                 }
1286         }
1287
1288         public class ChildControlsCreatedControl : Control
1289         {
1290                 protected internal override void CreateChildControls () {
1291                         Controls.Add (new Control ());
1292                 }
1293
1294                 public void DoEnsureChildControls () {
1295                         EnsureChildControls ();
1296                 }
1297
1298                 public void SetChildControlsCreated (bool value) {
1299                         ChildControlsCreated = value;
1300                 }
1301         }
1302 }