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;
44 using System.Web.UI.Adapters;
47 namespace MonoTests.System.Web.UI
50 public class ControlTest
53 public void DataBindingInterfaceTest ()
56 DataBindingCollection db;
59 Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB1");
60 db = ((IDataBindingsAccessor) c).DataBindings;
61 Assert.IsNotNull (db, "DB2");
62 Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB3");
63 db.Add (new DataBinding ("property", typeof (bool), "expression"));
64 Assert.AreEqual (true, ((IDataBindingsAccessor) c).HasDataBindings);
68 public void FindControl_NamingContainer ()
70 Control topControl = new NamingContainer ();
71 topControl.ID = "top";
73 Control controlLevel1A = new Control ();
74 controlLevel1A.ID = "Level1#A";
75 topControl.Controls.Add (controlLevel1A);
77 Control controlLevel1B = new Control ();
78 controlLevel1B.ID = "Level1#B";
79 topControl.Controls.Add (controlLevel1B);
81 Control controlLevel2AA = new Control ();
82 controlLevel2AA.ID = "Level2#AA";
83 controlLevel1A.Controls.Add (controlLevel2AA);
85 Control controlLevel2AB = new Control ();
86 controlLevel2AB.ID = "Level2#AB";
87 controlLevel1A.Controls.Add (controlLevel2AB);
89 Control foundControl = topControl.FindControl ("Level1#A");
90 Assert.IsNotNull (foundControl, "#A1");
91 Assert.AreSame (controlLevel1A, foundControl, "#A2");
93 foundControl = topControl.FindControl ("LEVEL1#B");
94 Assert.IsNotNull (foundControl, "#B1");
95 Assert.AreSame (controlLevel1B, foundControl, "#B2");
97 foundControl = topControl.FindControl ("LeVeL2#AB");
98 Assert.IsNotNull (foundControl, "#C1");
99 Assert.AreSame (controlLevel2AB, foundControl, "#C2");
101 foundControl = topControl.FindControl ("doesnotexist");
102 Assert.IsNull (foundControl, "#D1");
103 foundControl = topControl.FindControl ("top");
104 Assert.IsNull (foundControl, "#D2");
106 foundControl = controlLevel1A.FindControl ("Level2#AA");
107 Assert.IsNotNull (foundControl, "#E1");
108 Assert.AreSame (controlLevel2AA, foundControl, "#E2");
110 foundControl = controlLevel1A.FindControl ("LEveL2#ab");
111 Assert.IsNotNull (foundControl, "#F1");
112 Assert.AreSame (controlLevel2AB, foundControl, "#F2");
116 public void UniqueID1 ()
119 Control nc = new MyNC ();
120 Assert.IsNull (nc.UniqueID, "nulltest");
124 public void UniqueID1_1 () {
126 Control nc = new MyNC ();
127 Page p = new Page ();
129 Assert.IsNotNull (nc.UniqueID, "notnull");
133 public void UniqueID2 ()
136 Control nc = new MyNC ();
137 Control nc2 = new MyNC ();
138 nc2.Controls.Add (nc);
139 Assert.IsNotNull (nc.UniqueID, "notnull");
140 Assert.IsTrue (nc.UniqueID.IndexOfAny (new char[] { ':', '$' }) == -1, "separator");
144 public void UniqueID2_1 () {
146 Control nc = new MyNC ();
147 Control nc2 = new MyNC ();
148 nc2.Controls.Add (nc);
149 Page p = new Page ();
150 p.Controls.Add (nc2);
151 Assert.IsNotNull (nc.UniqueID, "notnull");
152 Assert.IsTrue (nc.UniqueID.IndexOfAny (new char [] { ':', '$' }) != -1, "separator");
156 public void UniqueID3 ()
159 Control control = new Control ();
160 Control nc = new MyNC ();
162 control.Controls.Add (nc);
163 Assert.IsNull (nc.UniqueID, "null");
167 public void UniqueID4 ()
170 Control control = new Control ();
171 Control nc = new MyNC ();
173 nc.Controls.Add (control);
174 Assert.IsNotNull (control.UniqueID, "notnull");
178 public void UniqueID5 ()
181 Control control = new Control ();
182 Control nc = new MyNC ();
183 Control nc2 = new MyNC ();
185 nc2.Controls.Add (nc);
186 nc.Controls.Add (control);
187 Assert.IsNotNull (control.UniqueID, "notnull");
188 Assert.IsNull (nc2.ID, "null-1");
189 Assert.IsNull (nc.ID, "null-2");
190 Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char[] { ':', '$' }), "separator");
194 public void UniqueID6 () {
196 Control nc = new MyNC ();
197 Page p = new Page ();
199 Assert.IsNotNull (nc.UniqueID, "notnull");
201 Control c1 = new Control ();
202 Control c2 = new Control ();
203 nc.Controls.Add (c1);
204 nc.Controls.Add (c2);
205 string uid1_1 = c1.UniqueID;
206 string uid2_1 = c2.UniqueID;
208 nc.Controls.Clear ();
210 Assert.IsNull (c1.UniqueID);
211 Assert.IsNull (c2.UniqueID);
213 // ad in another order
214 nc.Controls.Add (c2);
215 nc.Controls.Add (c1);
216 string uid1_2 = c1.UniqueID;
217 string uid2_2 = c2.UniqueID;
219 Assert.IsFalse (uid1_1 == uid1_2);
220 Assert.IsFalse (uid2_1 == uid2_2);
221 Assert.AreEqual (uid1_1, uid2_2);
222 Assert.AreEqual (uid2_1, uid1_2);
224 nc.Controls.Remove (c1);
225 nc.Controls.Add (c1);
226 string uid1_3 = c1.UniqueID;
227 Assert.IsFalse (uid1_3 == uid1_2, "id was not reset");
230 EnsureIDControl c3 = new EnsureIDControl ();
231 nc.Controls.Add (c3);
232 string uid3_1 = c3.UniqueID;
234 Assert.IsNotNull (c3.ID);
235 nc.Controls.Remove (c3);
236 nc.Controls.Add (c3);
237 string uid3_2 = c3.UniqueID;
238 Assert.IsNull (c3.ID);
239 Assert.IsFalse (uid3_1 == uid3_2, "id was not reset");
244 public void ClientID ()
247 Control control = new Control ();
248 Control nc = new MyNC ();
249 Control nc2 = new MyNC ();
250 Control nc3 = new MyNC ();
252 nc3.Controls.Add (nc2);
253 nc2.Controls.Add (nc);
254 nc.Controls.Add (control);
256 string expected = "ctl00_ctl00_ctl00";
258 string expected = "_ctl0__ctl0__ctl0";
260 Assert.AreEqual (expected, control.ClientID, "ClientID");
263 // From bug #76919: Control uses _controls instead of
264 // Controls when RenderChildren is called.
266 public void Controls1 ()
268 DerivedControl derived = new DerivedControl ();
269 derived.Controls.Add (new LiteralControl ("hola"));
270 StringWriter sw = new StringWriter ();
271 HtmlTextWriter htw = new HtmlTextWriter (sw);
272 derived.RenderControl (htw);
273 string result = sw.ToString ();
274 Assert.AreEqual ("", result, "#01");
277 [Test] // Bug #471305
278 [Category ("NunitWeb")]
279 public void NoDoubleOnInitOnRemoveAdd ()
281 WebTest.CopyResource (GetType (), "NoDoubleOnInitOnRemoveAdd.aspx", "NoDoubleOnInitOnRemoveAdd.aspx");
282 WebTest.CopyResource (GetType (), "NoDoubleOnInitOnRemoveAdd.aspx.cs", "NoDoubleOnInitOnRemoveAdd.aspx.cs");
283 WebTest t = new WebTest ("NoDoubleOnInitOnRemoveAdd.aspx");
284 string html = t.Run ();
286 Assert.AreEqual (-1, html.IndexOf ("<span>label</span><span>label</span>"), "#A1");
291 [Category("NunitWeb")]
292 public void AppRelativeTemplateSourceDirectory ()
294 new WebTest(PageInvoker.CreateOnLoad(AppRelativeTemplateSourceDirectory_Load)).Run();
297 public static void AppRelativeTemplateSourceDirectory_Load(Page p)
299 Control ctrl = new Control();
300 Assert.AreEqual("~/", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#1");
301 ctrl.AppRelativeTemplateSourceDirectory = "~/Fake";
302 Assert.AreEqual("~/Fake", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#2");
306 public void ApplyStyleSheetSkin ()
308 Page p = new Page ();
309 p.StyleSheetTheme = "";
310 Control c = new Control ();
311 c.ApplyStyleSheetSkin (p);
315 [Category ("NunitWeb")]
316 public void ApplyStyleSheetSkin_1 ()
318 WebTest.CopyResource (GetType (), "Theme2.skin", "App_Themes/Theme2/Theme2.skin");
319 WebTest t = new WebTest ();
320 PageDelegates pd = new PageDelegates ();
321 pd.PreInit = ApplyStyleSheetSkin_PreInit;
322 pd.Load = ApplyStyleSheetSkin_Load;
323 t.Invoker = new PageInvoker (pd);
324 string str = t.Run ();
326 public static void ApplyStyleSheetSkin_PreInit (Page p)
330 public static void ApplyStyleSheetSkin_Load (Page p)
332 Label lbl = new Label ();
335 lbl.Text = "StyleLabel";
336 p.Controls.Add (lbl);
337 lbl.ApplyStyleSheetSkin (p);
338 Assert.AreEqual (Color.Red, lbl.ForeColor, "ApplyStyleSheetSkin_BackColor");
339 Assert.AreEqual ("TextFromSkinFile", lbl.Text, "ApplyStyleSheetSkin");
343 [Category ("NunitWeb")]
344 public void ClearChildControlState ()
346 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildControlState_Load));
348 FormRequest fr = new FormRequest (t.Response, "form1");
349 fr.Controls.Add ("__EVENTTARGET");
350 fr.Controls.Add ("__EVENTARGUMENT");
351 fr.Controls["__EVENTTARGET"].Value = "";
352 fr.Controls["__EVENTARGUMENT"].Value = "";
356 public static void ClearChildControlState_Load (Page p)
358 ControlWithState c1 = new ControlWithState ();
359 p.Form.Controls.Add (c1);
361 c1.ClearChildControlState ();
363 ControlWithState c2 = new ControlWithState ();
364 c1.Controls.Add (c2);
365 ControlWithState c3 = new ControlWithState ();
366 c2.Controls.Add (c3);
370 c3.State = "SubCool";
373 Assert.AreEqual ("State", c1.State, "ControlState#1");
374 Assert.AreEqual (null, c2.State, "ControlState#2");
375 Assert.AreEqual (null, c3.State, "ControlState#2");
380 [Category ("NunitWeb")]
381 public void ClearChildState ()
383 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildState_Load));
385 FormRequest fr = new FormRequest (t.Response, "form1");
386 fr.Controls.Add ("__EVENTTARGET");
387 fr.Controls.Add ("__EVENTARGUMENT");
388 fr.Controls["__EVENTTARGET"].Value = "";
389 fr.Controls["__EVENTARGUMENT"].Value = "";
393 public static void ClearChildState_Load (Page p)
395 ControlWithState c1 = new ControlWithState ();
396 p.Form.Controls.Add (c1);
398 c1.ClearChildState ();
400 ControlWithState c2 = new ControlWithState ();
401 c1.Controls.Add (c2);
402 ControlWithState c3 = new ControlWithState ();
403 c2.Controls.Add (c3);
407 c2.Viewstate = "Very Cool";
408 c3.State = "SubCool";
409 c3.Viewstate = "Super Cool";
412 Assert.AreEqual ("State", c1.State, "ClearChildState#1");
413 Assert.AreEqual (null, c2.State, "ClearChildState#2");
414 Assert.AreEqual (null, c3.State, "ClearChildState#3");
415 Assert.AreEqual (null, c2.Viewstate, "ClearChildState#4");
416 Assert.AreEqual (null, c3.Viewstate, "ClearChildState#5");
421 public void DataBind ()
423 MyNC ctrl = new MyNC ();
424 ctrl.DataBinding += new EventHandler (ctrl_DataBinding);
425 Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
426 ctrl.DataBind (false);
427 Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
428 ctrl.DataBind (true);
429 Assert.AreEqual (true, _eventDataBinding, "After DataBinding");
431 bool _eventDataBinding;
432 void ctrl_DataBinding (object sender, EventArgs e)
434 _eventDataBinding = true;
438 public void DataBindChildren ()
440 MyNC ctrl1 = new MyNC ();
441 Control ctrl2 = new Control ();
442 Control ctrl3 = new Control ();
443 ctrl2.DataBinding += new EventHandler (ctrl2_DataBinding);
444 ctrl3.DataBinding += new EventHandler (ctrl3_DataBinding);
446 ctrl2.Controls.Add (ctrl3);
447 ctrl1.Controls.Add (ctrl2);
448 Assert.AreEqual (false, _eventChild1, "Before DataBinding#1");
449 Assert.AreEqual (false, _eventChild2, "Before DataBinding#2");
450 ctrl1.DataBindChildren ();
451 Assert.AreEqual (true, _eventChild1, "After DataBinding#1");
452 Assert.AreEqual (true, _eventChild2, "After DataBinding#2");
456 void ctrl3_DataBinding (object sender, EventArgs e)
460 void ctrl2_DataBinding (object sender, EventArgs e)
466 public void EnsureID ()
468 MyNC ctrl = new MyNC ();
469 MyNC ctrl1 = new MyNC ();
470 ctrl.Controls.Add (ctrl1);
471 Page p = new Page ();
472 p.Controls.Add (ctrl);
474 if (String.IsNullOrEmpty (ctrl.ID))
475 Assert.Fail ("EnsureID#1");
477 if (String.IsNullOrEmpty (ctrl1.ID))
478 Assert.Fail ("EnsureID#2");
482 [Category("NotWorking")]
485 WebTest t = new WebTest (PageInvoker.CreateOnLoad (Focus_Load));
486 string html = t.Run ();
487 Assert.AreEqual (3, contain (html, "TestBox"), "Focus script not created");
490 public static void Focus_Load (Page p)
492 TextBox tbx = new TextBox ();
494 p.Controls.Add (tbx);
497 int contain (string orig, string compare)
499 if (orig.IndexOf (compare) == -1)
501 return 1 + contain (orig.Substring (orig.IndexOf (compare) + compare.Length), compare);
505 public void HasEvent ()
507 MyNC ctrl1 = new MyNC ();
508 Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#1");
509 EventHandler ctrl_hdlr = new EventHandler (ctrl1_Init);
510 ctrl1.Init += new EventHandler (ctrl1_Init);
511 ctrl1.Init += ctrl_hdlr;
512 Assert.AreEqual (true, ctrl1.HasEvents (), "HasEvent#2");
513 // Dosn't work than removed handler
514 //ctrl1.Init -= ctrl_hdlr;
515 //Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#3");
517 void ctrl1_Init (object sender, EventArgs e)
519 throw new Exception ("The method or operation is not implemented.");
523 public void IsViewStateEnabled ()
525 DerivedControl c = new DerivedControl ();
526 Assert.IsTrue (c.DoIsViewStateEnabled);
527 Page p = new Page ();
530 Assert.IsTrue (c.DoIsViewStateEnabled);
531 p.EnableViewState = false;
532 Assert.IsFalse (c.DoIsViewStateEnabled);
536 [Category ("NunitWeb")]
537 public void ControlState ()
539 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
541 FormRequest fr = new FormRequest (t.Response, "form1");
542 fr.Controls.Add ("__EVENTTARGET");
543 fr.Controls.Add ("__EVENTARGUMENT");
544 fr.Controls["__EVENTTARGET"].Value = "";
545 fr.Controls["__EVENTARGUMENT"].Value = "";
549 public static void ControlState_Load (Page p)
551 ControlWithState c1 = new ControlWithState ();
552 ControlWithState c2 = new ControlWithState ();
553 c1.Controls.Add (c2);
554 p.Form.Controls.Add (c1);
560 ControlWithState c3 = new ControlWithState ();
561 p.Form.Controls.Add (c3);
562 Assert.AreEqual ("State", c1.State, "ControlState");
563 Assert.AreEqual ("Cool", c2.State, "ControlState");
568 [Category ("NunitWeb")]
569 public void ControlState2 () {
570 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState2_Load));
572 FormRequest fr = new FormRequest (t.Response, "form1");
573 fr.Controls.Add ("__EVENTTARGET");
574 fr.Controls.Add ("__EVENTARGUMENT");
575 fr.Controls ["__EVENTTARGET"].Value = "";
576 fr.Controls ["__EVENTARGUMENT"].Value = "";
580 fr = new FormRequest (t.Response, "form1");
581 fr.Controls.Add ("__EVENTTARGET");
582 fr.Controls.Add ("__EVENTARGUMENT");
583 fr.Controls ["__EVENTTARGET"].Value = "";
584 fr.Controls ["__EVENTARGUMENT"].Value = "";
588 public static void ControlState2_Load (Page p) {
589 ControlWithState parent = new ControlWithState ();
590 p.Form.Controls.Add (parent);
593 parent.Controls.Clear ();
594 parent.ClearChildControlState ();
595 ControlWithState c1 = new ControlWithState ();
596 ControlWithState c2 = new ControlWithState ();
597 parent.Controls.Add (c1);
598 parent.Controls.Add (c2);
599 c1.State = "State1_1";
600 c2.State = "State1_2";
601 parent.State = "First";
603 else if (parent.State == "First") {
605 parent.Controls.Clear ();
606 parent.ClearChildControlState ();
607 ControlWithState c1 = new ControlWithState ();
608 ControlWithState c2 = new ControlWithState ();
609 parent.Controls.Add (c1);
610 parent.Controls.Add (c2);
611 c1.State = "State2_1";
612 c2.State = "State2_2";
613 parent.State = "Second";
616 // emulate CrerateChildControl only
617 parent.Controls.Clear ();
618 ControlWithState c1 = new ControlWithState ();
619 ControlWithState c2 = new ControlWithState ();
620 parent.Controls.Add (c1);
621 parent.Controls.Add (c2);
623 Assert.AreEqual ("State2_1", c1.State, "ControlState#1");
624 Assert.AreEqual ("State2_2", c2.State, "ControlState#2");
629 public void ClientIDSeparator ()
631 DerivedControl ctrl = new DerivedControl ();
632 Assert.AreEqual (95, (int) ctrl.ClientIDSeparator, "ClientIDSeparator");
636 public void IDSeparator ()
638 DerivedControl ctrl = new DerivedControl ();
639 Assert.AreEqual (36, (int) ctrl.IdSeparator, "IDSeparator");
643 [Category ("NunitWeb")]
644 public void IsChildControlStateCleared ()
646 WebTest t = new WebTest (PageInvoker.CreateOnLoad (IsChildControlStateCleared_Load));
648 FormRequest fr = new FormRequest (t.Response, "form1");
649 fr.Controls.Add ("__EVENTTARGET");
650 fr.Controls.Add ("__EVENTARGUMENT");
651 fr.Controls["__EVENTTARGET"].Value = "";
652 fr.Controls["__EVENTARGUMENT"].Value = "";
656 public static void IsChildControlStateCleared_Load (Page p)
658 ControlWithState c1 = new ControlWithState ();
659 p.Form.Controls.Add (c1);
661 Assert.IsFalse (c1.IsChildControlStateCleared, "ControlState#1");
662 c1.ClearChildControlState ();
663 Assert.IsTrue (c1.IsChildControlStateCleared, "ControlState#1");
665 ControlWithState c2 = new ControlWithState ();
666 c1.Controls.Add (c2);
667 ControlWithState c3 = new ControlWithState ();
668 c2.Controls.Add (c3);
670 Assert.IsFalse (c2.IsChildControlStateCleared, "ControlState#1");
671 Assert.IsFalse (c3.IsChildControlStateCleared, "ControlState#1");
676 c3.State = "SubCool";
681 [Category ("NunitWeb")]
682 public void LoadViewStateByID ()
684 ControlWithState c1 = new ControlWithState ();
685 ControlWithState c2 = new ControlWithState ();
686 c1.Controls.Add (c2);
687 Assert.AreEqual (false, c1.LoadViewStateByID, "LoadViewStateByID#1");
691 [Category ("NunitWeb")]
692 public void OpenFile ()
694 WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFile_Load));
697 public static void OpenFile_Load (Page p)
699 DerivedControl ctrl = new DerivedControl ();
700 Stream strem = ctrl.OpenFile ("~/MyPage.aspx");
701 Assert.IsNotNull (strem, "OpenFile failed");
705 [Category ("NunitWeb")]
706 [ExpectedException (typeof (FileNotFoundException))]
707 public void OpenFile_Exception ()
709 WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFileException_Load));
712 public static void OpenFileException_Load (Page p)
714 DerivedControl ctrl = new DerivedControl ();
715 Stream strem = ctrl.OpenFile ("~/Fake.tmp");
719 [Category ("NunitWeb")]
720 public void ResolveClientUrl ()
722 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveClientUrl_Load));
723 string html = t.Run ();
725 public static void ResolveClientUrl_Load (Page p)
727 Control ctrl = new Control ();
728 p.Controls.Add (ctrl);
729 string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
730 Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
732 Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
734 Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
735 Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
737 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
738 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
739 Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
740 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
741 Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
742 Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
743 Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
744 Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveClientUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
745 Assert.AreEqual ("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 ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
750 Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
751 Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
753 Assert.AreEqual ("./MyPage.aspx", ctrl.ResolveClientUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
754 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
759 [Category ("NunitWeb")]
760 public void ResolveClientUrl2 ()
762 WebTest t = new WebTest ("ResolveUrl.aspx");
763 PageDelegates delegates = new PageDelegates ();
764 delegates.Load = ResolveClientUrl2_Load;
765 t.Invoker = new PageInvoker (delegates);
766 string html = t.Run ();
769 public static void ResolveClientUrl2_Load (Page p)
771 Control uc = p.FindControl ("WebUserControl1");
772 Control ctrl = uc.FindControl ("Label");
774 string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
775 Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
777 Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
779 Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
780 Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
782 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
783 Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
784 Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
785 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
786 Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
787 Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("/NunitWeb/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 ("Folder/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
791 Assert.AreEqual ("Folder/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
792 Assert.AreEqual ("Folder/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
793 Assert.AreEqual ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
795 Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
796 Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
798 Assert.AreEqual ("Folder/MyPage.aspx", ctrl.ResolveClientUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
799 Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
804 [Category ("NunitWeb")]
805 public void ResolveUrl ()
807 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveUrl_Load));
808 string html = t.Run ();
811 public static void ResolveUrl_Load (Page p)
814 string appPath = "/MainsoftWebApp20";
816 string appPath = "/NunitWeb";
818 Control ctrl = new Control ();
819 p.Controls.Add (ctrl);
820 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~/MyPage.aspx"), "ResolveClientUrl Failed");
822 Assert.AreEqual ("", ctrl.ResolveUrl (""), "empty string");
824 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~"), "~");
825 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~/"), "~/");
827 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
828 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
829 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
830 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
831 Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
832 Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("/NunitWeb/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 (appPath + "/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
836 Assert.AreEqual (appPath + "/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 ("http://google.com/", ctrl.ResolveUrl ("http://google.com/"), "ResolveClientUrl Failed");
840 Assert.AreEqual (appPath + "/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
841 Assert.AreEqual ("/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
843 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
844 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
846 Assert.AreEqual ("/", ctrl.ResolveUrl (".."), "..");
847 Assert.AreEqual ("/", ctrl.ResolveUrl ("../"), "../");
851 [Category ("NunitWeb")]
852 public void ResolveUrl2 ()
854 WebTest t = new WebTest ("ResolveUrl.aspx");
855 PageDelegates delegates = new PageDelegates ();
856 delegates.Load = ResolveUrl2_Load;
857 t.Invoker = new PageInvoker (delegates);
858 string html = t.Run ();
861 public static void ResolveUrl2_Load (Page p)
864 string appPath = "/MainsoftWebApp20";
866 string appPath = "/NunitWeb";
868 Control uc = p.FindControl ("WebUserControl1");
869 Control ctrl = uc.FindControl ("Label");
871 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~/MyPage.aspx"), "ResolveClientUrl Failed");
873 Assert.AreEqual ("", ctrl.ResolveUrl (""), "empty string");
875 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~"), "~");
876 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~/"), "~/");
878 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
879 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
880 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
881 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
882 Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
883 Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("/NunitWeb/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 (appPath + "/Folder/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
887 Assert.AreEqual (appPath + "/Folder/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
888 Assert.AreEqual (appPath + "/Folder/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
889 Assert.AreEqual ("http://google.com/", ctrl.ResolveUrl ("http://google.com/"), "ResolveClientUrl Failed");
891 Assert.AreEqual (appPath + "/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
892 Assert.AreEqual ("/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
894 Assert.AreEqual (appPath + "/Folder/MyPage.aspx", ctrl.ResolveUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
895 Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
897 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl (".."), "..");
898 Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("../"), "../");
899 Assert.AreEqual ("/", ctrl.ResolveUrl ("../.."), "../..");
900 Assert.AreEqual ("/", ctrl.ResolveUrl ("../../"), "../../");
901 Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("../../MyPage.aspx"), "../../MyPage.aspx");
905 [Category ("NotWorking")] // Not implemented exception
906 public void ResolveAdapter_2 ()
908 DerivedControl ctrl = new DerivedControl ();
909 Assert.AreEqual (null, ctrl.ResolveAdapter (), "ResolveAdapter");
913 public void EnableTheming ()
915 DerivedControl ctrl = new DerivedControl ();
916 Assert.AreEqual (true, ctrl.EnableTheming, "EnableTheming#1");
917 ctrl.EnableTheming = false;
918 Assert.AreEqual (false, ctrl.EnableTheming, "EnableTheming#2");
923 public void BindingContainer ()
925 ControlWithTemplate c = new ControlWithTemplate ();
926 c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
928 // cause CreateChildControls called
929 c.FindControl ("stam");
932 static void BindingContainer_BuildTemplate (Control control)
934 Control child1 = new Control ();
935 control.Controls.Add (child1);
937 Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
938 Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
940 NamingContainer nc = new NamingContainer ();
941 Control child2 = new Control ();
942 nc.Controls.Add (child2);
943 control.Controls.Add (nc);
945 Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
946 Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
949 // DetailsViewPagerRow marked to be not BindingContainer
950 DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
951 TableCell cell = new TableCell ();
952 Control child3 = new Control ();
953 cell.Controls.Add (child3);
954 row.Cells.Add (cell);
955 control.Controls.Add (row);
957 Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
958 Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
963 public void Control_Adapter ()
965 MyNC ctr = new MyNC ();
966 Assert.AreEqual (null, ctr.Adapter (), "Adapter");
970 public void ChildControlsCreated () {
971 ChildControlsCreatedControl ctr = new ChildControlsCreatedControl ();
972 ctr.Controls.Add (new Control ());
973 //ctr.DoEnsureChildControls ();
975 Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#1");
976 ctr.SetChildControlsCreated (false);
977 Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#2");
981 [TestFixtureTearDown]
982 public void Tear_down ()
988 public void TestFixtureSetUp ()
990 WebTest.CopyResource (GetType (), "ResolveUrl.aspx", "ResolveUrl.aspx");
991 WebTest.CopyResource (GetType (), "ResolveUrl.ascx", "Folder/ResolveUrl.ascx");
998 class ControlWithState : Control
1004 get { return _state; }
1005 set { _state = value; }
1008 public string Viewstate {
1009 get { return (string) ViewState ["Viewstate"]; }
1010 set { ViewState ["Viewstate"] = value; }
1013 protected override void OnInit (EventArgs e)
1016 Page.RegisterRequiresControlState (this);
1019 protected override object SaveControlState ()
1024 protected override void LoadControlState (object savedState)
1026 State = (string) savedState;
1029 public new void ClearChildState ()
1031 base.ClearChildState ();
1034 public new void ClearChildControlState ()
1036 base.ClearChildControlState ();
1039 public new bool IsChildControlStateCleared
1041 get { return base.IsChildControlStateCleared; }
1044 public new bool LoadViewStateByID
1046 get { return base.LoadViewStateByID; }
1051 class MyNC : Control, INamingContainer
1054 public ControlAdapter Adapter ()
1056 return base.Adapter;
1059 public new void DataBind (bool opt)
1061 base.DataBind (opt);
1064 public new void DataBindChildren ()
1066 base.DataBindChildren ();
1069 public new void EnsureID ()
1074 public new bool HasEvents ()
1076 return base.HasEvents ();
1081 class DerivedControl : Control
1083 ControlCollection coll;
1085 public DerivedControl ()
1087 coll = new ControlCollection (this);
1090 public override ControlCollection Controls
1092 get { return coll; }
1096 public bool DoIsViewStateEnabled
1098 get { return IsViewStateEnabled; }
1101 public new char ClientIDSeparator
1103 get { return base.ClientIDSeparator; }
1106 public new char IdSeparator
1108 get { return base.IdSeparator; }
1111 public new Stream OpenFile (string path)
1113 return base.OpenFile (path);
1116 public new ControlAdapter ResolveAdapter ()
1118 return base.ResolveAdapter ();
1123 class NamingContainer : Control, INamingContainer
1127 class ControlWithTemplate : Control
1129 ITemplate _template;
1131 [TemplateContainer (typeof (TemplateContainer))]
1132 public ITemplate Template
1134 get { return _template; }
1135 set { _template = value; }
1138 protected override void CreateChildControls ()
1142 TemplateContainer container = new TemplateContainer ();
1143 Controls.Add (container);
1145 if (Template != null)
1146 Template.InstantiateIn (container);
1150 class TemplateContainer : Control, INamingContainer
1157 public class Customadaptercontrol : Control
1159 public new ControlAdapter Adapter {
1160 get { return base.Adapter; }
1163 public new ControlAdapter ResolveAdapter ()
1165 return base.ResolveAdapter ();
1169 public class Customadapter : ControlAdapter
1173 class EnsureIDControl : Control {
1174 public void DoEnsureID () {
1180 public class ChildControlsCreatedControl : Control
1182 protected override void CreateChildControls () {
1183 Controls.Add (new Control ());
1186 public void DoEnsureChildControls () {
1187 EnsureChildControls ();
1190 public void SetChildControlsCreated (bool value) {
1191 ChildControlsCreated = value;