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