2 // Tests for System.Web.UI.Control
5 // Peter Dennis Bartok (pbartok@novell.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using NUnit.Framework;
34 using System.Collections;
37 using System.Globalization;
40 using System.Web.UI.WebControls;
41 using MonoTests.SystemWeb.Framework;
42 using MonoTests.stand_alone.WebHarness;
45 using System.Web.UI.Adapters;
48 namespace MonoTests.System.Web.UI
51 public class ControlTest
54 public void DataBindingInterfaceTest ()
57 DataBindingCollection db;
60 Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB1");
61 db = ((IDataBindingsAccessor) c).DataBindings;
62 Assert.IsNotNull (db, "DB2");
63 Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB3");
64 db.Add (new DataBinding ("property", typeof (bool), "expression"));
65 Assert.AreEqual (true, ((IDataBindingsAccessor) c).HasDataBindings);
69 public void FindControl_NamingContainer ()
71 Control topControl = new NamingContainer ();
72 topControl.ID = "top";
74 Control controlLevel1A = new Control ();
75 controlLevel1A.ID = "Level1#A";
76 topControl.Controls.Add (controlLevel1A);
78 Control controlLevel1B = new Control ();
79 controlLevel1B.ID = "Level1#B";
80 topControl.Controls.Add (controlLevel1B);
82 Control controlLevel2AA = new Control ();
83 controlLevel2AA.ID = "Level2#AA";
84 controlLevel1A.Controls.Add (controlLevel2AA);
86 Control controlLevel2AB = new Control ();
87 controlLevel2AB.ID = "Level2#AB";
88 controlLevel1A.Controls.Add (controlLevel2AB);
90 Control foundControl = topControl.FindControl ("Level1#A");
91 Assert.IsNotNull (foundControl, "#A1");
92 Assert.AreSame (controlLevel1A, foundControl, "#A2");
94 foundControl = topControl.FindControl ("LEVEL1#B");
95 Assert.IsNotNull (foundControl, "#B1");
96 Assert.AreSame (controlLevel1B, foundControl, "#B2");
98 foundControl = topControl.FindControl ("LeVeL2#AB");
99 Assert.IsNotNull (foundControl, "#C1");
100 Assert.AreSame (controlLevel2AB, foundControl, "#C2");
102 foundControl = topControl.FindControl ("doesnotexist");
103 Assert.IsNull (foundControl, "#D1");
104 foundControl = topControl.FindControl ("top");
105 Assert.IsNull (foundControl, "#D2");
107 foundControl = controlLevel1A.FindControl ("Level2#AA");
108 Assert.IsNotNull (foundControl, "#E1");
109 Assert.AreSame (controlLevel2AA, foundControl, "#E2");
111 foundControl = controlLevel1A.FindControl ("LEveL2#ab");
112 Assert.IsNotNull (foundControl, "#F1");
113 Assert.AreSame (controlLevel2AB, foundControl, "#F2");
117 public void UniqueID1 ()
120 Control nc = new MyNC ();
121 Assert.IsNull (nc.UniqueID, "nulltest");
125 public void UniqueID1_1 () {
127 Control nc = new MyNC ();
128 Page p = new Page ();
130 Assert.IsNotNull (nc.UniqueID, "notnull");
134 public void UniqueID2 ()
137 Control nc = new MyNC ();
138 Control nc2 = new MyNC ();
139 nc2.Controls.Add (nc);
140 Assert.IsNotNull (nc.UniqueID, "notnull");
141 Assert.IsTrue (nc.UniqueID.IndexOfAny (new char[] { ':', '$' }) == -1, "separator");
145 public void UniqueID2_1 () {
147 Control nc = new MyNC ();
148 Control nc2 = new MyNC ();
149 nc2.Controls.Add (nc);
150 Page p = new Page ();
151 p.Controls.Add (nc2);
152 Assert.IsNotNull (nc.UniqueID, "notnull");
153 Assert.IsTrue (nc.UniqueID.IndexOfAny (new char [] { ':', '$' }) != -1, "separator");
157 public void UniqueID3 ()
160 Control control = new Control ();
161 Control nc = new MyNC ();
163 control.Controls.Add (nc);
164 Assert.IsNull (nc.UniqueID, "null");
168 public void UniqueID4 ()
171 Control control = new Control ();
172 Control nc = new MyNC ();
174 nc.Controls.Add (control);
175 Assert.IsNotNull (control.UniqueID, "notnull");
179 public void UniqueID5 ()
182 Control control = new Control ();
183 Control nc = new MyNC ();
184 Control nc2 = new MyNC ();
186 nc2.Controls.Add (nc);
187 nc.Controls.Add (control);
188 Assert.IsNotNull (control.UniqueID, "notnull");
189 Assert.IsNull (nc2.ID, "null-1");
190 Assert.IsNull (nc.ID, "null-2");
191 Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char[] { ':', '$' }), "separator");
195 public void UniqueID6 () {
197 Control nc = new MyNC ();
198 Page p = new Page ();
200 Assert.IsNotNull (nc.UniqueID, "notnull");
202 Control c1 = new Control ();
203 Control c2 = new Control ();
204 nc.Controls.Add (c1);
205 nc.Controls.Add (c2);
206 string uid1_1 = c1.UniqueID;
207 string uid2_1 = c2.UniqueID;
209 nc.Controls.Clear ();
211 Assert.IsNull (c1.UniqueID);
212 Assert.IsNull (c2.UniqueID);
214 // ad in another order
215 nc.Controls.Add (c2);
216 nc.Controls.Add (c1);
217 string uid1_2 = c1.UniqueID;
218 string uid2_2 = c2.UniqueID;
220 Assert.IsFalse (uid1_1 == uid1_2);
221 Assert.IsFalse (uid2_1 == uid2_2);
222 Assert.AreEqual (uid1_1, uid2_2);
223 Assert.AreEqual (uid2_1, uid1_2);
225 nc.Controls.Remove (c1);
226 nc.Controls.Add (c1);
227 string uid1_3 = c1.UniqueID;
228 Assert.IsFalse (uid1_3 == uid1_2, "id was not reset");
231 EnsureIDControl c3 = new EnsureIDControl ();
232 nc.Controls.Add (c3);
233 string uid3_1 = c3.UniqueID;
235 Assert.IsNotNull (c3.ID);
236 nc.Controls.Remove (c3);
237 nc.Controls.Add (c3);
238 string uid3_2 = c3.UniqueID;
239 Assert.IsNull (c3.ID);
240 Assert.IsFalse (uid3_1 == uid3_2, "id was not reset");
245 public void ClientID ()
248 Control control = new Control ();
249 Control nc = new MyNC ();
250 Control nc2 = new MyNC ();
251 Control nc3 = new MyNC ();
253 nc3.Controls.Add (nc2);
254 nc2.Controls.Add (nc);
255 nc.Controls.Add (control);
257 string expected = "ctl00_ctl00_ctl00";
259 string expected = "_ctl0__ctl0__ctl0";
261 Assert.AreEqual (expected, control.ClientID, "ClientID");
264 // From bug #76919: Control uses _controls instead of
265 // Controls when RenderChildren is called.
267 public void Controls1 ()
269 DerivedControl derived = new DerivedControl ();
270 derived.Controls.Add (new LiteralControl ("hola"));
271 StringWriter sw = new StringWriter ();
272 HtmlTextWriter htw = new HtmlTextWriter (sw);
273 derived.RenderControl (htw);
274 string result = sw.ToString ();
275 Assert.AreEqual ("", result, "#01");
278 [Test] // Bug #471305
279 [Category ("NunitWeb")]
280 public void NoDoubleOnInitOnRemoveAdd ()
282 WebTest.CopyResource (GetType (), "NoDoubleOnInitOnRemoveAdd.aspx", "NoDoubleOnInitOnRemoveAdd.aspx");
283 WebTest.CopyResource (GetType (), "NoDoubleOnInitOnRemoveAdd.aspx.cs", "NoDoubleOnInitOnRemoveAdd.aspx.cs");
284 WebTest t = new WebTest ("NoDoubleOnInitOnRemoveAdd.aspx");
285 string html = t.Run ();
287 Assert.AreEqual (-1, html.IndexOf ("<span>label</span><span>label</span>"), "#A1");
292 [Category("NunitWeb")]
293 public void AppRelativeTemplateSourceDirectory ()
295 new WebTest(PageInvoker.CreateOnLoad(AppRelativeTemplateSourceDirectory_Load)).Run();
298 public static void AppRelativeTemplateSourceDirectory_Load(Page p)
300 Control ctrl = new Control();
301 Assert.AreEqual("~/", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#1");
302 ctrl.AppRelativeTemplateSourceDirectory = "~/Fake";
303 Assert.AreEqual("~/Fake", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#2");
307 public void ApplyStyleSheetSkin ()
309 Page p = new Page ();
310 p.StyleSheetTheme = "";
311 Control c = new Control ();
312 c.ApplyStyleSheetSkin (p);
316 [Category ("NunitWeb")]
317 public void ApplyStyleSheetSkin_1 ()
319 WebTest.CopyResource (GetType (), "Theme2.skin", "App_Themes/Theme2/Theme2.skin");
320 WebTest t = new WebTest ();
321 PageDelegates pd = new PageDelegates ();
322 pd.PreInit = ApplyStyleSheetSkin_PreInit;
323 pd.Load = ApplyStyleSheetSkin_Load;
324 t.Invoker = new PageInvoker (pd);
325 string str = t.Run ();
327 public static void ApplyStyleSheetSkin_PreInit (Page p)
331 public static void ApplyStyleSheetSkin_Load (Page p)
333 Label lbl = new Label ();
336 lbl.Text = "StyleLabel";
337 p.Controls.Add (lbl);
338 lbl.ApplyStyleSheetSkin (p);
339 Assert.AreEqual (Color.Red, lbl.ForeColor, "ApplyStyleSheetSkin_BackColor");
340 Assert.AreEqual ("TextFromSkinFile", lbl.Text, "ApplyStyleSheetSkin");
344 [Category ("NunitWeb")]
345 public void ClearChildControlState ()
347 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildControlState_Load));
349 FormRequest fr = new FormRequest (t.Response, "form1");
350 fr.Controls.Add ("__EVENTTARGET");
351 fr.Controls.Add ("__EVENTARGUMENT");
352 fr.Controls["__EVENTTARGET"].Value = "";
353 fr.Controls["__EVENTARGUMENT"].Value = "";
357 public static void ClearChildControlState_Load (Page p)
359 ControlWithState c1 = new ControlWithState ();
360 p.Form.Controls.Add (c1);
362 c1.ClearChildControlState ();
364 ControlWithState c2 = new ControlWithState ();
365 c1.Controls.Add (c2);
366 ControlWithState c3 = new ControlWithState ();
367 c2.Controls.Add (c3);
371 c3.State = "SubCool";
374 Assert.AreEqual ("State", c1.State, "ControlState#1");
375 Assert.AreEqual (null, c2.State, "ControlState#2");
376 Assert.AreEqual (null, c3.State, "ControlState#2");
381 [Category ("NunitWeb")]
382 public void ClearChildState ()
384 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildState_Load));
386 FormRequest fr = new FormRequest (t.Response, "form1");
387 fr.Controls.Add ("__EVENTTARGET");
388 fr.Controls.Add ("__EVENTARGUMENT");
389 fr.Controls["__EVENTTARGET"].Value = "";
390 fr.Controls["__EVENTARGUMENT"].Value = "";
394 public static void ClearChildState_Load (Page p)
396 ControlWithState c1 = new ControlWithState ();
397 p.Form.Controls.Add (c1);
399 c1.ClearChildState ();
401 ControlWithState c2 = new ControlWithState ();
402 c1.Controls.Add (c2);
403 ControlWithState c3 = new ControlWithState ();
404 c2.Controls.Add (c3);
408 c2.Viewstate = "Very Cool";
409 c3.State = "SubCool";
410 c3.Viewstate = "Super Cool";
413 Assert.AreEqual ("State", c1.State, "ClearChildState#1");
414 Assert.AreEqual (null, c2.State, "ClearChildState#2");
415 Assert.AreEqual (null, c3.State, "ClearChildState#3");
416 Assert.AreEqual (null, c2.Viewstate, "ClearChildState#4");
417 Assert.AreEqual (null, c3.Viewstate, "ClearChildState#5");
422 public void DataBind ()
424 MyNC ctrl = new MyNC ();
425 ctrl.DataBinding += new EventHandler (ctrl_DataBinding);
426 Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
427 ctrl.DataBind (false);
428 Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
429 ctrl.DataBind (true);
430 Assert.AreEqual (true, _eventDataBinding, "After DataBinding");
432 bool _eventDataBinding;
433 void ctrl_DataBinding (object sender, EventArgs e)
435 _eventDataBinding = true;
439 public void DataBindChildren ()
441 MyNC ctrl1 = new MyNC ();
442 Control ctrl2 = new Control ();
443 Control ctrl3 = new Control ();
444 ctrl2.DataBinding += new EventHandler (ctrl2_DataBinding);
445 ctrl3.DataBinding += new EventHandler (ctrl3_DataBinding);
447 ctrl2.Controls.Add (ctrl3);
448 ctrl1.Controls.Add (ctrl2);
449 Assert.AreEqual (false, _eventChild1, "Before DataBinding#1");
450 Assert.AreEqual (false, _eventChild2, "Before DataBinding#2");
451 ctrl1.DataBindChildren ();
452 Assert.AreEqual (true, _eventChild1, "After DataBinding#1");
453 Assert.AreEqual (true, _eventChild2, "After DataBinding#2");
457 void ctrl3_DataBinding (object sender, EventArgs e)
461 void ctrl2_DataBinding (object sender, EventArgs e)
467 public void EnsureID ()
469 MyNC ctrl = new MyNC ();
470 MyNC ctrl1 = new MyNC ();
471 ctrl.Controls.Add (ctrl1);
472 Page p = new Page ();
473 p.Controls.Add (ctrl);
475 if (String.IsNullOrEmpty (ctrl.ID))
476 Assert.Fail ("EnsureID#1");
478 if (String.IsNullOrEmpty (ctrl1.ID))
479 Assert.Fail ("EnsureID#2");
483 [Category("NotWorking")]
486 WebTest t = new WebTest (PageInvoker.CreateOnLoad (Focus_Load));
487 string html = t.Run ();
488 Assert.AreEqual (3, contain (html, "TestBox"), "Focus script not created");
491 public static void Focus_Load (Page p)
493 TextBox tbx = new TextBox ();
495 p.Controls.Add (tbx);
498 int contain (string orig, string compare)
500 if (orig.IndexOf (compare) == -1)
502 return 1 + contain (orig.Substring (orig.IndexOf (compare) + compare.Length), compare);
506 public void HasEvent ()
508 MyNC ctrl1 = new MyNC ();
509 Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#1");
510 EventHandler ctrl_hdlr = new EventHandler (ctrl1_Init);
511 ctrl1.Init += new EventHandler (ctrl1_Init);
512 ctrl1.Init += ctrl_hdlr;
513 Assert.AreEqual (true, ctrl1.HasEvents (), "HasEvent#2");
514 // Dosn't work than removed handler
515 //ctrl1.Init -= ctrl_hdlr;
516 //Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#3");
518 void ctrl1_Init (object sender, EventArgs e)
520 throw new Exception ("The method or operation is not implemented.");
524 public void IsViewStateEnabled ()
526 DerivedControl c = new DerivedControl ();
527 Assert.IsTrue (c.DoIsViewStateEnabled);
528 Page p = new Page ();
531 Assert.IsTrue (c.DoIsViewStateEnabled);
532 p.EnableViewState = false;
533 Assert.IsFalse (c.DoIsViewStateEnabled);
537 [Category ("NunitWeb")]
538 public void ControlState ()
540 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
542 FormRequest fr = new FormRequest (t.Response, "form1");
543 fr.Controls.Add ("__EVENTTARGET");
544 fr.Controls.Add ("__EVENTARGUMENT");
545 fr.Controls["__EVENTTARGET"].Value = "";
546 fr.Controls["__EVENTARGUMENT"].Value = "";
550 public static void ControlState_Load (Page p)
552 ControlWithState c1 = new ControlWithState ();
553 ControlWithState c2 = new ControlWithState ();
554 c1.Controls.Add (c2);
555 p.Form.Controls.Add (c1);
561 ControlWithState c3 = new ControlWithState ();
562 p.Form.Controls.Add (c3);
563 Assert.AreEqual ("State", c1.State, "ControlState");
564 Assert.AreEqual ("Cool", c2.State, "ControlState");
569 [Category ("NunitWeb")]
570 public void ControlState2 () {
571 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState2_Load));
573 FormRequest fr = new FormRequest (t.Response, "form1");
574 fr.Controls.Add ("__EVENTTARGET");
575 fr.Controls.Add ("__EVENTARGUMENT");
576 fr.Controls ["__EVENTTARGET"].Value = "";
577 fr.Controls ["__EVENTARGUMENT"].Value = "";
581 fr = new FormRequest (t.Response, "form1");
582 fr.Controls.Add ("__EVENTTARGET");
583 fr.Controls.Add ("__EVENTARGUMENT");
584 fr.Controls ["__EVENTTARGET"].Value = "";
585 fr.Controls ["__EVENTARGUMENT"].Value = "";
589 public static void ControlState2_Load (Page p) {
590 ControlWithState parent = new ControlWithState ();
591 p.Form.Controls.Add (parent);
594 parent.Controls.Clear ();
595 parent.ClearChildControlState ();
596 ControlWithState c1 = new ControlWithState ();
597 ControlWithState c2 = new ControlWithState ();
598 parent.Controls.Add (c1);
599 parent.Controls.Add (c2);
600 c1.State = "State1_1";
601 c2.State = "State1_2";
602 parent.State = "First";
604 else if (parent.State == "First") {
606 parent.Controls.Clear ();
607 parent.ClearChildControlState ();
608 ControlWithState c1 = new ControlWithState ();
609 ControlWithState c2 = new ControlWithState ();
610 parent.Controls.Add (c1);
611 parent.Controls.Add (c2);
612 c1.State = "State2_1";
613 c2.State = "State2_2";
614 parent.State = "Second";
617 // emulate CrerateChildControl only
618 parent.Controls.Clear ();
619 ControlWithState c1 = new ControlWithState ();
620 ControlWithState c2 = new ControlWithState ();
621 parent.Controls.Add (c1);
622 parent.Controls.Add (c2);
624 Assert.AreEqual ("State2_1", c1.State, "ControlState#1");
625 Assert.AreEqual ("State2_2", c2.State, "ControlState#2");
630 public void ClientIDSeparator ()
632 DerivedControl ctrl = new DerivedControl ();
633 Assert.AreEqual (95, (int) ctrl.ClientIDSeparator, "ClientIDSeparator");
637 public void IDSeparator ()
639 DerivedControl ctrl = new DerivedControl ();
640 Assert.AreEqual (36, (int) ctrl.IdSeparator, "IDSeparator");
644 [Category ("NunitWeb")]
645 public void IsChildControlStateCleared ()
647 WebTest t = new WebTest (PageInvoker.CreateOnLoad (IsChildControlStateCleared_Load));
649 FormRequest fr = new FormRequest (t.Response, "form1");
650 fr.Controls.Add ("__EVENTTARGET");
651 fr.Controls.Add ("__EVENTARGUMENT");
652 fr.Controls["__EVENTTARGET"].Value = "";
653 fr.Controls["__EVENTARGUMENT"].Value = "";
657 public static void IsChildControlStateCleared_Load (Page p)
659 ControlWithState c1 = new ControlWithState ();
660 p.Form.Controls.Add (c1);
662 Assert.IsFalse (c1.IsChildControlStateCleared, "ControlState#1");
663 c1.ClearChildControlState ();
664 Assert.IsTrue (c1.IsChildControlStateCleared, "ControlState#1");
666 ControlWithState c2 = new ControlWithState ();
667 c1.Controls.Add (c2);
668 ControlWithState c3 = new ControlWithState ();
669 c2.Controls.Add (c3);
671 Assert.IsFalse (c2.IsChildControlStateCleared, "ControlState#1");
672 Assert.IsFalse (c3.IsChildControlStateCleared, "ControlState#1");
677 c3.State = "SubCool";
682 [Category ("NunitWeb")]
683 public void LoadViewStateByID ()
685 ControlWithState c1 = new ControlWithState ();
686 ControlWithState c2 = new ControlWithState ();
687 c1.Controls.Add (c2);
688 Assert.AreEqual (false, c1.LoadViewStateByID, "LoadViewStateByID#1");
692 [Category ("NunitWeb")]
693 public void OpenFile ()
695 WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFile_Load));
698 public static void OpenFile_Load (Page p)
700 DerivedControl ctrl = new DerivedControl ();
701 Stream strem = ctrl.OpenFile ("~/MyPage.aspx");
702 Assert.IsNotNull (strem, "OpenFile failed");
706 [Category ("NunitWeb")]
707 [ExpectedException (typeof (FileNotFoundException))]
708 public void OpenFile_Exception ()
710 WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFileException_Load));
713 public static void OpenFileException_Load (Page p)
715 DerivedControl ctrl = new DerivedControl ();
716 Stream strem = ctrl.OpenFile ("~/Fake.tmp");
720 [Category ("NunitWeb")]
721 public void ResolveClientUrl ()
723 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveClientUrl_Load));
724 string html = t.Run ();
726 public static void ResolveClientUrl_Load (Page p)
728 Control ctrl = new Control ();
729 p.Controls.Add (ctrl);
730 string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
731 Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
733 Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
735 Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
736 Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
738 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
739 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
740 Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
741 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
742 Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/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 ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveClientUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
746 Assert.AreEqual ("NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
747 Assert.AreEqual ("NunitWeb/../MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
748 Assert.AreEqual ("NunitWeb/./MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
749 Assert.AreEqual ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
751 Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
752 Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
754 Assert.AreEqual ("./MyPage.aspx", ctrl.ResolveClientUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
755 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
760 [Category ("NunitWeb")]
761 public void ResolveClientUrl2 ()
763 WebTest t = new WebTest ("ResolveUrl.aspx");
764 PageDelegates delegates = new PageDelegates ();
765 delegates.Load = ResolveClientUrl2_Load;
766 t.Invoker = new PageInvoker (delegates);
767 string html = t.Run ();
770 public static void ResolveClientUrl2_Load (Page p)
772 Control uc = p.FindControl ("WebUserControl1");
773 Control ctrl = uc.FindControl ("Label");
775 string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
776 Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
778 Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
780 Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
781 Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
783 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
784 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
785 Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
786 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
787 Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
788 Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
789 Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
790 Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveClientUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
791 Assert.AreEqual ("Folder/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
792 Assert.AreEqual ("Folder/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
793 Assert.AreEqual ("Folder/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
794 Assert.AreEqual ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
796 Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
797 Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
799 Assert.AreEqual ("Folder/MyPage.aspx", ctrl.ResolveClientUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
800 Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
805 [Category ("NunitWeb")]
806 public void ResolveUrl ()
808 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveUrl_Load));
809 string html = t.Run ();
812 public static void ResolveUrl_Load (Page p)
815 string appPath = "/MainsoftWebApp20";
817 string appPath = "/NunitWeb";
819 Control ctrl = new Control ();
820 p.Controls.Add (ctrl);
821 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~/MyPage.aspx"), "ResolveClientUrl Failed");
823 Assert.AreEqual ("", ctrl.ResolveUrl (""), "empty string");
825 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~"), "~");
826 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~/"), "~/");
828 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
829 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
830 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
831 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
832 Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
833 Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("/NunitWeb/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 (appPath + "/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
837 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
838 Assert.AreEqual (appPath + "/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
839 Assert.AreEqual ("http://google.com/", ctrl.ResolveUrl ("http://google.com/"), "ResolveClientUrl Failed");
841 Assert.AreEqual (appPath + "/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
842 Assert.AreEqual ("/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
844 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
845 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
847 Assert.AreEqual ("/", ctrl.ResolveUrl (".."), "..");
848 Assert.AreEqual ("/", ctrl.ResolveUrl ("../"), "../");
852 [Category ("NunitWeb")]
853 public void ResolveUrl2 ()
855 WebTest t = new WebTest ("ResolveUrl.aspx");
856 PageDelegates delegates = new PageDelegates ();
857 delegates.Load = ResolveUrl2_Load;
858 t.Invoker = new PageInvoker (delegates);
859 string html = t.Run ();
862 public static void ResolveUrl2_Load (Page p)
865 string appPath = "/MainsoftWebApp20";
867 string appPath = "/NunitWeb";
869 Control uc = p.FindControl ("WebUserControl1");
870 Control ctrl = uc.FindControl ("Label");
872 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~/MyPage.aspx"), "ResolveClientUrl Failed");
874 Assert.AreEqual ("", ctrl.ResolveUrl (""), "empty string");
876 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~"), "~");
877 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~/"), "~/");
879 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
880 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
881 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
882 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
883 Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
884 Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
885 Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
886 Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
887 Assert.AreEqual (appPath + "/Folder/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
888 Assert.AreEqual (appPath + "/Folder/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
889 Assert.AreEqual (appPath + "/Folder/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
890 Assert.AreEqual ("http://google.com/", ctrl.ResolveUrl ("http://google.com/"), "ResolveClientUrl Failed");
892 Assert.AreEqual (appPath + "/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
893 Assert.AreEqual ("/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
895 Assert.AreEqual (appPath + "/Folder/MyPage.aspx", ctrl.ResolveUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
896 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
898 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl (".."), "..");
899 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("../"), "../");
900 Assert.AreEqual ("/", ctrl.ResolveUrl ("../.."), "../..");
901 Assert.AreEqual ("/", ctrl.ResolveUrl ("../../"), "../../");
902 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("../../MyPage.aspx"), "../../MyPage.aspx");
906 [Category ("NotWorking")] // Not implemented exception
907 public void ResolveAdapter_2 ()
909 DerivedControl ctrl = new DerivedControl ();
910 Assert.AreEqual (null, ctrl.ResolveAdapter (), "ResolveAdapter");
914 public void EnableTheming ()
916 DerivedControl ctrl = new DerivedControl ();
917 Assert.AreEqual (true, ctrl.EnableTheming, "EnableTheming#1");
918 ctrl.EnableTheming = false;
919 Assert.AreEqual (false, ctrl.EnableTheming, "EnableTheming#2");
924 public void BindingContainer ()
926 ControlWithTemplate c = new ControlWithTemplate ();
927 c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
929 // cause CreateChildControls called
930 c.FindControl ("stam");
933 static void BindingContainer_BuildTemplate (Control control)
935 Control child1 = new Control ();
936 control.Controls.Add (child1);
938 Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
939 Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
941 NamingContainer nc = new NamingContainer ();
942 Control child2 = new Control ();
943 nc.Controls.Add (child2);
944 control.Controls.Add (nc);
946 Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
947 Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
950 // DetailsViewPagerRow marked to be not BindingContainer
951 DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
952 TableCell cell = new TableCell ();
953 Control child3 = new Control ();
954 cell.Controls.Add (child3);
955 row.Cells.Add (cell);
956 control.Controls.Add (row);
958 Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
959 Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
964 public void Control_Adapter ()
966 MyNC ctr = new MyNC ();
967 Assert.AreEqual (null, ctr.Adapter (), "Adapter");
971 public void ChildControlsCreated () {
972 ChildControlsCreatedControl ctr = new ChildControlsCreatedControl ();
973 ctr.Controls.Add (new Control ());
974 //ctr.DoEnsureChildControls ();
976 Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#1");
977 ctr.SetChildControlsCreated (false);
978 Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#2");
982 [Test (Description="Bug #594238")]
983 public void OverridenControlsPropertyAndPostBack_Bug594238 ()
985 WebTest t = new WebTest ("OverridenControlsPropertyAndPostBack_Bug594238.aspx");
988 FormRequest fr = new FormRequest (t.Response, "form1");
989 fr.Controls.Add ("__EVENTTARGET");
990 fr.Controls.Add ("__EVENTARGUMENT");
991 fr.Controls ["__EVENTTARGET"].Value = "container$children$lb";
992 fr.Controls ["__EVENTARGUMENT"].Value = String.Empty;
995 string originalHtml = @"<span id=""container""><a href=""javascript:__doPostBack('container$children$lb','')"" id=""container_children_lb"">Woot! I got clicked!</a></span><hr/>";
996 string pageHtml = t.Run ();
997 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
999 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
1002 [TestFixtureTearDown]
1003 public void Tear_down ()
1009 public void TestFixtureSetUp ()
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");
1020 class ControlWithState : Control
1026 get { return _state; }
1027 set { _state = value; }
1030 public string Viewstate {
1031 get { return (string) ViewState ["Viewstate"]; }
1032 set { ViewState ["Viewstate"] = value; }
1035 protected override void OnInit (EventArgs e)
1038 Page.RegisterRequiresControlState (this);
1041 protected override object SaveControlState ()
1046 protected override void LoadControlState (object savedState)
1048 State = (string) savedState;
1051 public new void ClearChildState ()
1053 base.ClearChildState ();
1056 public new void ClearChildControlState ()
1058 base.ClearChildControlState ();
1061 public new bool IsChildControlStateCleared
1063 get { return base.IsChildControlStateCleared; }
1066 public new bool LoadViewStateByID
1068 get { return base.LoadViewStateByID; }
1073 class MyNC : Control, INamingContainer
1076 public ControlAdapter Adapter ()
1078 return base.Adapter;
1081 public new void DataBind (bool opt)
1083 base.DataBind (opt);
1086 public new void DataBindChildren ()
1088 base.DataBindChildren ();
1091 public new void EnsureID ()
1096 public new bool HasEvents ()
1098 return base.HasEvents ();
1103 class DerivedControl : Control
1105 ControlCollection coll;
1107 public DerivedControl ()
1109 coll = new ControlCollection (this);
1112 public override ControlCollection Controls
1114 get { return coll; }
1118 public bool DoIsViewStateEnabled
1120 get { return IsViewStateEnabled; }
1123 public new char ClientIDSeparator
1125 get { return base.ClientIDSeparator; }
1128 public new char IdSeparator
1130 get { return base.IdSeparator; }
1133 public new Stream OpenFile (string path)
1135 return base.OpenFile (path);
1138 public new ControlAdapter ResolveAdapter ()
1140 return base.ResolveAdapter ();
1145 class NamingContainer : Control, INamingContainer
1149 class ControlWithTemplate : Control
1151 ITemplate _template;
1153 [TemplateContainer (typeof (TemplateContainer))]
1154 public ITemplate Template
1156 get { return _template; }
1157 set { _template = value; }
1160 protected override void CreateChildControls ()
1164 TemplateContainer container = new TemplateContainer ();
1165 Controls.Add (container);
1167 if (Template != null)
1168 Template.InstantiateIn (container);
1172 class TemplateContainer : Control, INamingContainer
1179 public class Customadaptercontrol : Control
1181 public new ControlAdapter Adapter {
1182 get { return base.Adapter; }
1185 public new ControlAdapter ResolveAdapter ()
1187 return base.ResolveAdapter ();
1191 public class Customadapter : ControlAdapter
1195 class EnsureIDControl : Control {
1196 public void DoEnsureID () {
1202 public class ChildControlsCreatedControl : Control
1204 protected override void CreateChildControls () {
1205 Controls.Add (new Control ());
1208 public void DoEnsureChildControls () {
1209 EnsureChildControls ();
1212 public void SetChildControlsCreated (bool value) {
1213 ChildControlsCreated = value;