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 UniqueID1 ()
71 Control nc = new MyNC ();
72 Assert.IsNull (nc.UniqueID, "nulltest");
76 public void UniqueID2 ()
79 Control nc = new MyNC ();
80 Control nc2 = new MyNC ();
81 nc2.Controls.Add (nc);
82 Assert.IsNotNull (nc.UniqueID, "notnull");
83 Assert.IsTrue (nc.UniqueID.IndexOfAny (new char[] { ':', '$' }) == -1, "separator");
87 public void UniqueID3 ()
90 Control control = new Control ();
91 Control nc = new MyNC ();
93 control.Controls.Add (nc);
94 Assert.IsNull (nc.UniqueID, "null");
98 public void UniqueID4 ()
101 Control control = new Control ();
102 Control nc = new MyNC ();
104 nc.Controls.Add (control);
105 Assert.IsNotNull (control.UniqueID, "notnull");
109 public void UniqueID5 ()
112 Control control = new Control ();
113 Control nc = new MyNC ();
114 Control nc2 = new MyNC ();
116 nc2.Controls.Add (nc);
117 nc.Controls.Add (control);
118 Assert.IsNotNull (control.UniqueID, "notnull");
119 Assert.IsNull (nc2.ID, "null-1");
120 Assert.IsNull (nc.ID, "null-2");
121 Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char[] { ':', '$' }), "separator");
124 // From bug #76919: Control uses _controls instead of
125 // Controls when RenderChildren is called.
127 public void Controls1 ()
129 DerivedControl derived = new DerivedControl ();
130 derived.Controls.Add (new LiteralControl ("hola"));
131 StringWriter sw = new StringWriter ();
132 HtmlTextWriter htw = new HtmlTextWriter (sw);
133 derived.RenderControl (htw);
134 string result = sw.ToString ();
135 Assert.AreEqual ("", result, "#01");
140 [Category ("NotWorking")]
141 public void AppRelativeTemplateSourceDirectory ()
143 Control ctrl = new Control ();
144 Assert.AreEqual ("", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#1");
145 ctrl.AppRelativeTemplateSourceDirectory = "~/Fake";
146 Assert.AreEqual ("~/Fake", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#2");
150 public void ApplyStyleSheetSkin ()
152 Page p = new Page ();
153 p.StyleSheetTheme = "";
154 Control c = new Control ();
155 c.ApplyStyleSheetSkin (p);
159 [Category ("NunitWeb")]
160 public void ApplyStyleSheetSkin_1 ()
163 WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.Theme2.skin", "App_Themes/Theme2/Theme2.skin");
165 WebTest.CopyResource (GetType (), "Theme2.skin", "App_Themes/Theme2/Theme2.skin");
167 WebTest t = new WebTest ();
168 PageDelegates pd = new PageDelegates ();
169 pd.PreInit = ApplyStyleSheetSkin_PreInit;
170 pd.Load = ApplyStyleSheetSkin_Load;
171 t.Invoker = new PageInvoker (pd);
172 string str = t.Run ();
174 public static void ApplyStyleSheetSkin_PreInit (Page p)
178 public static void ApplyStyleSheetSkin_Load (Page p)
180 Label lbl = new Label ();
183 lbl.Text = "StyleLabel";
184 p.Controls.Add (lbl);
185 lbl.ApplyStyleSheetSkin (p);
186 Assert.AreEqual (Color.Red, lbl.ForeColor, "ApplyStyleSheetSkin_BackColor");
187 Assert.AreEqual ("TextFromSkinFile", lbl.Text, "ApplyStyleSheetSkin");
191 [Category ("NunitWeb")]
192 public void ClearChildControlState ()
194 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildControlState_Load));
196 FormRequest fr = new FormRequest (t.Response, "form1");
197 fr.Controls.Add ("__EVENTTARGET");
198 fr.Controls.Add ("__EVENTARGUMENT");
199 fr.Controls["__EVENTTARGET"].Value = "";
200 fr.Controls["__EVENTARGUMENT"].Value = "";
204 public static void ClearChildControlState_Load (Page p)
206 ControlWithState c1 = new ControlWithState ();
207 p.Form.Controls.Add (c1);
209 c1.ClearChildControlState ();
211 ControlWithState c2 = new ControlWithState ();
212 c1.Controls.Add (c2);
213 ControlWithState c3 = new ControlWithState ();
214 c2.Controls.Add (c3);
218 c3.State = "SubCool";
221 Assert.AreEqual ("State", c1.State, "ControlState#1");
222 Assert.AreEqual (null, c2.State, "ControlState#2");
223 Assert.AreEqual (null, c3.State, "ControlState#2");
228 [Category ("NunitWeb")]
229 public void ClearChildState ()
231 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildState_Load));
233 FormRequest fr = new FormRequest (t.Response, "form1");
234 fr.Controls.Add ("__EVENTTARGET");
235 fr.Controls.Add ("__EVENTARGUMENT");
236 fr.Controls["__EVENTTARGET"].Value = "";
237 fr.Controls["__EVENTARGUMENT"].Value = "";
241 public static void ClearChildState_Load (Page p)
243 ControlWithState c1 = new ControlWithState ();
244 p.Form.Controls.Add (c1);
246 c1.ClearChildState ();
248 ControlWithState c2 = new ControlWithState ();
249 c1.Controls.Add (c2);
250 ControlWithState c3 = new ControlWithState ();
251 c2.Controls.Add (c3);
255 c2.Viewstate = "Very Cool";
256 c3.State = "SubCool";
257 c3.Viewstate = "Super Cool";
260 Assert.AreEqual ("State", c1.State, "ClearChildState#1");
261 Assert.AreEqual (null, c2.State, "ClearChildState#2");
262 Assert.AreEqual (null, c3.State, "ClearChildState#3");
263 Assert.AreEqual (null, c2.Viewstate, "ClearChildState#4");
264 Assert.AreEqual (null, c3.Viewstate, "ClearChildState#5");
269 public void DataBind ()
271 MyNC ctrl = new MyNC ();
272 ctrl.DataBinding += new EventHandler (ctrl_DataBinding);
273 Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
274 ctrl.DataBind (false);
275 Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
276 ctrl.DataBind (true);
277 Assert.AreEqual (true, _eventDataBinding, "After DataBinding");
279 bool _eventDataBinding;
280 void ctrl_DataBinding (object sender, EventArgs e)
282 _eventDataBinding = true;
286 public void DataBindChildren ()
288 MyNC ctrl1 = new MyNC ();
289 Control ctrl2 = new Control ();
290 Control ctrl3 = new Control ();
291 ctrl2.DataBinding += new EventHandler (ctrl2_DataBinding);
292 ctrl3.DataBinding += new EventHandler (ctrl3_DataBinding);
294 ctrl2.Controls.Add (ctrl3);
295 ctrl1.Controls.Add (ctrl2);
296 Assert.AreEqual (false, _eventChild1, "Before DataBinding#1");
297 Assert.AreEqual (false, _eventChild2, "Before DataBinding#2");
298 ctrl1.DataBindChildren ();
299 Assert.AreEqual (true, _eventChild1, "After DataBinding#1");
300 Assert.AreEqual (true, _eventChild2, "After DataBinding#2");
304 void ctrl3_DataBinding (object sender, EventArgs e)
308 void ctrl2_DataBinding (object sender, EventArgs e)
314 [Category ("NotWorking")] // Not implemented exception
315 public void EnsureID ()
317 MyNC ctrl = new MyNC ();
318 MyNC ctrl1 = new MyNC ();
319 ctrl.Controls.Add (ctrl1);
320 Page p = new Page ();
321 p.Controls.Add (ctrl);
323 if (ctrl.ID == string.Empty)
324 Assert.Fail ("EnsureID#1");
325 if (ctrl1.ID == string.Empty)
326 Assert.Fail ("EnsureID#2");
332 WebTest t = new WebTest (PageInvoker.CreateOnLoad (Focus_Load));
333 string html = t.Run ();
334 Assert.AreEqual (3, contain (html, "TestBox"), "Focus script not created");
337 public static void Focus_Load (Page p)
339 TextBox tbx = new TextBox ();
341 p.Controls.Add (tbx);
344 int contain (string orig, string compare)
346 if (orig.IndexOf (compare) == -1)
348 return 1 + contain (orig.Substring (orig.IndexOf (compare) + compare.Length), compare);
352 [Category ("NotWorking")] // Not implemented exception
353 public void HasEvent ()
355 MyNC ctrl1 = new MyNC ();
356 Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#1");
357 EventHandler ctrl_hdlr = new EventHandler (ctrl1_Init);
358 ctrl1.Init += new EventHandler (ctrl1_Init);
359 ctrl1.Init += ctrl_hdlr;
360 Assert.AreEqual (true, ctrl1.HasEvents (), "HasEvent#2");
361 // Dosn't work than removed handler
362 //ctrl1.Init -= ctrl_hdlr;
363 //Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#3");
365 void ctrl1_Init (object sender, EventArgs e)
367 throw new Exception ("The method or operation is not implemented.");
371 [Category ("NotWorking")]
372 public void IsViewStateEnabled ()
374 DerivedControl c = new DerivedControl ();
375 Assert.IsTrue (c.DoIsViewStateEnabled);
376 Page p = new Page ();
379 Assert.IsTrue (c.DoIsViewStateEnabled);
380 p.EnableViewState = false;
381 Assert.IsFalse (c.DoIsViewStateEnabled);
385 [Category ("NunitWeb")]
386 public void ControlState ()
388 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
390 FormRequest fr = new FormRequest (t.Response, "form1");
391 fr.Controls.Add ("__EVENTTARGET");
392 fr.Controls.Add ("__EVENTARGUMENT");
393 fr.Controls["__EVENTTARGET"].Value = "";
394 fr.Controls["__EVENTARGUMENT"].Value = "";
398 public static void ControlState_Load (Page p)
400 ControlWithState c1 = new ControlWithState ();
401 ControlWithState c2 = new ControlWithState ();
402 c1.Controls.Add (c2);
403 p.Form.Controls.Add (c1);
409 ControlWithState c3 = new ControlWithState ();
410 p.Form.Controls.Add (c3);
411 Assert.AreEqual ("State", c1.State, "ControlState");
412 Assert.AreEqual ("Cool", c2.State, "ControlState");
417 [Category ("NunitWeb")]
418 public void ControlState2 () {
419 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState2_Load));
421 FormRequest fr = new FormRequest (t.Response, "form1");
422 fr.Controls.Add ("__EVENTTARGET");
423 fr.Controls.Add ("__EVENTARGUMENT");
424 fr.Controls ["__EVENTTARGET"].Value = "";
425 fr.Controls ["__EVENTARGUMENT"].Value = "";
429 fr = new FormRequest (t.Response, "form1");
430 fr.Controls.Add ("__EVENTTARGET");
431 fr.Controls.Add ("__EVENTARGUMENT");
432 fr.Controls ["__EVENTTARGET"].Value = "";
433 fr.Controls ["__EVENTARGUMENT"].Value = "";
437 public static void ControlState2_Load (Page p) {
438 ControlWithState parent = new ControlWithState ();
439 p.Form.Controls.Add (parent);
442 parent.Controls.Clear ();
443 parent.ClearChildControlState ();
444 ControlWithState c1 = new ControlWithState ();
445 ControlWithState c2 = new ControlWithState ();
446 parent.Controls.Add (c1);
447 parent.Controls.Add (c2);
448 c1.State = "State1_1";
449 c2.State = "State1_2";
450 parent.State = "First";
452 else if (parent.State == "First") {
454 parent.Controls.Clear ();
455 parent.ClearChildControlState ();
456 ControlWithState c1 = new ControlWithState ();
457 ControlWithState c2 = new ControlWithState ();
458 parent.Controls.Add (c1);
459 parent.Controls.Add (c2);
460 c1.State = "State2_1";
461 c2.State = "State2_2";
462 parent.State = "Second";
465 // emulate CrerateChildControl only
466 parent.Controls.Clear ();
467 ControlWithState c1 = new ControlWithState ();
468 ControlWithState c2 = new ControlWithState ();
469 parent.Controls.Add (c1);
470 parent.Controls.Add (c2);
472 Assert.AreEqual ("State2_1", c1.State, "ControlState#1");
473 Assert.AreEqual ("State2_2", c2.State, "ControlState#2");
478 public void ClientIDSeparator ()
480 DerivedControl ctrl = new DerivedControl ();
481 Assert.AreEqual (95, (int) ctrl.ClientIDSeparator, "ClientIDSeparator");
485 public void IDSeparator ()
487 DerivedControl ctrl = new DerivedControl ();
488 Assert.AreEqual (36, (int) ctrl.IdSeparator, "IDSeparator");
492 [Category ("NunitWeb")]
493 public void IsChildControlStateCleared ()
495 WebTest t = new WebTest (PageInvoker.CreateOnLoad (IsChildControlStateCleared_Load));
497 FormRequest fr = new FormRequest (t.Response, "form1");
498 fr.Controls.Add ("__EVENTTARGET");
499 fr.Controls.Add ("__EVENTARGUMENT");
500 fr.Controls["__EVENTTARGET"].Value = "";
501 fr.Controls["__EVENTARGUMENT"].Value = "";
505 public static void IsChildControlStateCleared_Load (Page p)
507 ControlWithState c1 = new ControlWithState ();
508 p.Form.Controls.Add (c1);
510 Assert.IsFalse (c1.IsChildControlStateCleared, "ControlState#1");
511 c1.ClearChildControlState ();
512 Assert.IsTrue (c1.IsChildControlStateCleared, "ControlState#1");
514 ControlWithState c2 = new ControlWithState ();
515 c1.Controls.Add (c2);
516 ControlWithState c3 = new ControlWithState ();
517 c2.Controls.Add (c3);
519 Assert.IsFalse (c2.IsChildControlStateCleared, "ControlState#1");
520 Assert.IsFalse (c3.IsChildControlStateCleared, "ControlState#1");
525 c3.State = "SubCool";
530 [Category ("NunitWeb")]
531 public void LoadViewStateByID ()
533 ControlWithState c1 = new ControlWithState ();
534 ControlWithState c2 = new ControlWithState ();
535 c1.Controls.Add (c2);
536 Assert.AreEqual (false, c1.LoadViewStateByID, "LoadViewStateByID#1");
540 [Category ("NunitWeb")]
541 public void OpenFile ()
543 WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFile_Load));
546 public static void OpenFile_Load (Page p)
548 DerivedControl ctrl = new DerivedControl ();
549 Stream strem = ctrl.OpenFile ("~/MyPage.aspx");
550 Assert.IsNotNull (strem, "OpenFile failed");
554 [Category ("NunitWeb")]
555 [ExpectedException (typeof (FileNotFoundException))]
556 public void OpenFile_Exception ()
558 WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFileException_Load));
561 public static void OpenFileException_Load (Page p)
563 DerivedControl ctrl = new DerivedControl ();
564 Stream strem = ctrl.OpenFile ("~/Fake.tmp");
567 //// MonoTests.SystemWeb.Framework limitation for Add_browsers - directory include in project
569 //[Category ("NunitWeb")]
570 //public void ResolveAdapter_1 ()
572 // WebTest.CopyResource (GetType (), "adapters.browser", "App_Browsers/adapters.browser");
573 // WebTest t = new WebTest (PageInvoker.CreateOnInit (ResolveAdapter_Init));
574 // string html = t.Run ();
576 //public static void ResolveAdapter_Init (Page p)
578 // Customadaptercontrol ctrl = new Customadaptercontrol ();
579 // p.Controls.Add (ctrl);
580 // ctrl.Load += new EventHandler (ctrl_Load);
582 //static void ctrl_Load (object sender, EventArgs e)
584 // Assert.IsNotNull (((Customadaptercontrol) sender).ResolveAdapter (), "ResolveAdapter Failed#1");
585 // Assert.AreEqual ("Customadapter", ((Customadaptercontrol) sender).ResolveAdapter ().ToString (), "ResolveAdapter Failed#2");
589 [Category ("NunitWeb")]
590 public void ResolveClientUrl ()
592 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveClientUrl_Load));
593 string html = t.Run ();
595 public static void ResolveClientUrl_Load (Page p)
597 Control ctrl = new Control ();
598 p.Controls.Add (ctrl);
599 string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
600 Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
604 [Category ("NotWorking")] // Not implemented exception
605 public void ResolveAdapter_2 ()
607 DerivedControl ctrl = new DerivedControl ();
608 Assert.AreEqual (null, ctrl.ResolveAdapter (), "ResolveAdapter");
612 public void EnableTheming ()
614 DerivedControl ctrl = new DerivedControl ();
615 Assert.AreEqual (true, ctrl.EnableTheming, "EnableTheming#1");
616 ctrl.EnableTheming = false;
617 Assert.AreEqual (false, ctrl.EnableTheming, "EnableTheming#2");
622 public void BindingContainer ()
624 ControlWithTemplate c = new ControlWithTemplate ();
625 c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
627 // cause CreateChildControls called
628 c.FindControl ("stam");
630 static void BindingContainer_BuildTemplate (Control control)
632 Control child1 = new Control ();
633 control.Controls.Add (child1);
635 Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
636 Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
638 NamingContainer nc = new NamingContainer ();
639 Control child2 = new Control ();
640 nc.Controls.Add (child2);
641 control.Controls.Add (nc);
643 Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
644 Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
647 // DetailsViewPagerRow marked to be not BindingContainer
648 DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
649 TableCell cell = new TableCell ();
650 Control child3 = new Control ();
651 cell.Controls.Add (child3);
652 row.Cells.Add (cell);
653 control.Controls.Add (row);
655 Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
656 Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
661 public void Contorl_Adapter ()
663 MyNC ctr = new MyNC ();
664 Assert.AreEqual (null, ctr.Adapter (), "Adapter");
668 [TestFixtureTearDown]
669 public void Tear_down ()
677 class ControlWithState : Control
683 get { return _state; }
684 set { _state = value; }
687 public string Viewstate {
688 get { return (string) ViewState ["Viewstate"]; }
689 set { ViewState ["Viewstate"] = value; }
692 protected override void OnInit (EventArgs e)
695 Page.RegisterRequiresControlState (this);
698 protected override object SaveControlState ()
703 protected override void LoadControlState (object savedState)
705 State = (string) savedState;
708 public new void ClearChildState ()
710 base.ClearChildState ();
713 public new void ClearChildControlState ()
715 base.ClearChildControlState ();
718 public new bool IsChildControlStateCleared
720 get { return base.IsChildControlStateCleared; }
723 public new bool LoadViewStateByID
725 get { return base.LoadViewStateByID; }
730 class MyNC : Control, INamingContainer
733 public ControlAdapter Adapter ()
738 public new void DataBind (bool opt)
743 public new void DataBindChildren ()
745 base.DataBindChildren ();
748 public new void EnsureID ()
753 public new bool HasEvents ()
755 return base.HasEvents ();
760 class DerivedControl : Control
762 ControlCollection coll;
764 public DerivedControl ()
766 coll = new ControlCollection (this);
769 public override ControlCollection Controls
775 public bool DoIsViewStateEnabled
777 get { return IsViewStateEnabled; }
780 public new char ClientIDSeparator
782 get { return base.ClientIDSeparator; }
785 public new char IdSeparator
787 get { return base.IdSeparator; }
790 public new Stream OpenFile (string path)
792 return base.OpenFile (path);
795 public new ControlAdapter ResolveAdapter ()
797 return base.ResolveAdapter ();
802 class NamingContainer : Control, INamingContainer
806 class ControlWithTemplate : Control
810 [TemplateContainer (typeof (TemplateContainer))]
811 public ITemplate Template
813 get { return _template; }
814 set { _template = value; }
817 protected override void CreateChildControls ()
821 TemplateContainer container = new TemplateContainer ();
822 Controls.Add (container);
824 if (Template != null)
825 Template.InstantiateIn (container);
829 class TemplateContainer : Control, INamingContainer
836 public class Customadaptercontrol : Control
838 public new ControlAdapter ResolveAdapter ()
840 return base.ResolveAdapter ();
844 public class Customadapter : ControlAdapter