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