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 ("NotWorking")]
160 [Category ("NunitWeb")]
161 public void ApplyStyleSheetSkin_1 ()
163 WebTest.CopyResource (GetType (), "Theme2.skin", "App_Themes/Theme2/Theme2.skin");
164 WebTest t = new WebTest ();
165 PageDelegates pd = new PageDelegates ();
166 pd.PreInit = ApplyStyleSheetSkin_PreInit;
167 pd.Load = ApplyStyleSheetSkin_Load;
168 t.Invoker = new PageInvoker (pd);
169 string str = t.Run ();
171 public static void ApplyStyleSheetSkin_PreInit (Page p)
175 public static void ApplyStyleSheetSkin_Load (Page p)
177 Label lbl = new Label ();
180 lbl.Text = "StyleLabel";
181 p.Controls.Add (lbl);
182 lbl.ApplyStyleSheetSkin (p);
183 Assert.AreEqual (Color.Red, lbl.ForeColor, "ApplyStyleSheetSkin_BackColor");
184 Assert.AreEqual ("TextFromSkinFile", lbl.Text, "ApplyStyleSheetSkin");
188 [Category ("NotWorking")]
189 [Category ("NunitWeb")]
190 public void ClearChildControlState ()
192 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildControlState_Load));
194 FormRequest fr = new FormRequest (t.Response, "form1");
195 fr.Controls.Add ("__EVENTTARGET");
196 fr.Controls.Add ("__EVENTARGUMENT");
197 fr.Controls["__EVENTTARGET"].Value = "";
198 fr.Controls["__EVENTARGUMENT"].Value = "";
202 public static void ClearChildControlState_Load (Page p)
204 ParentControlWithState c1 = new ParentControlWithState ();
205 ControlWithState c2 = new ControlWithState ();
206 c1.Controls.Add (c2);
207 p.Form.Controls.Add (c1);
213 c1.ClearChildControlState ();
214 Assert.AreEqual ("State", c1.State, "ControlState#1");
215 Assert.AreEqual (null, c2.State, "ControlState#2");
220 [Category ("NotWorking")]
221 [Category ("NunitWeb")]
222 public void ClearChildState ()
224 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildState_Load));
226 FormRequest fr = new FormRequest (t.Response, "form1");
227 fr.Controls.Add ("__EVENTTARGET");
228 fr.Controls.Add ("__EVENTARGUMENT");
229 fr.Controls["__EVENTTARGET"].Value = "";
230 fr.Controls["__EVENTARGUMENT"].Value = "";
234 public static void ClearChildState_Load (Page p)
236 ParentControlWithViewState c1 = new ParentControlWithViewState ();
237 ParentControlWithViewState c2 = new ParentControlWithViewState ();
238 c1.Controls.Add (c2);
239 p.Form.Controls.Add (c1);
243 c1.Viewstate = "VState";
244 c2.Viewstate = "VCool";
247 Assert.AreEqual ("State", c1.State, "ControlState#1");
248 Assert.AreEqual ("VState", c1.Viewstate, "ControlViewState#1");
249 Assert.AreEqual (null, c2.State, "ControlState#2");
250 Assert.AreEqual (null, c2.Viewstate, "ControlViewState#2");
255 public void DataBind ()
257 MyNC ctrl = new MyNC ();
258 ctrl.DataBinding += new EventHandler (ctrl_DataBinding);
259 Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
260 ctrl.DataBind (false);
261 Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
262 ctrl.DataBind (true);
263 Assert.AreEqual (true, _eventDataBinding, "After DataBinding");
265 bool _eventDataBinding;
266 void ctrl_DataBinding (object sender, EventArgs e)
268 _eventDataBinding = true;
272 public void DataBindChildren ()
274 MyNC ctrl1 = new MyNC ();
275 Control ctrl2 = new Control ();
276 Control ctrl3 = new Control ();
277 ctrl2.DataBinding += new EventHandler (ctrl2_DataBinding);
278 ctrl3.DataBinding += new EventHandler (ctrl3_DataBinding);
280 ctrl2.Controls.Add (ctrl3);
281 ctrl1.Controls.Add (ctrl2);
282 Assert.AreEqual (false, _eventChild1, "Before DataBinding#1");
283 Assert.AreEqual (false, _eventChild2, "Before DataBinding#2");
284 ctrl1.DataBindChildren ();
285 Assert.AreEqual (true, _eventChild1, "After DataBinding#1");
286 Assert.AreEqual (true, _eventChild2, "After DataBinding#2");
290 void ctrl3_DataBinding (object sender, EventArgs e)
294 void ctrl2_DataBinding (object sender, EventArgs e)
300 [Category ("NotWorking")] // Not implemented exception
301 public void EnsureID ()
303 MyNC ctrl = new MyNC ();
304 MyNC ctrl1 = new MyNC ();
305 ctrl.Controls.Add (ctrl1);
306 Page p = new Page ();
307 p.Controls.Add (ctrl);
309 if (ctrl.ID == string.Empty)
310 Assert.Fail ("EnsureID#1");
311 if (ctrl1.ID == string.Empty)
312 Assert.Fail ("EnsureID#2");
316 [Category ("NotWorking")] // Not implemented exception
319 WebTest t = new WebTest (PageInvoker.CreateOnLoad (Focus_Load));
320 string html = t.Run ();
321 Assert.AreEqual (3, contain (html, "TestBox"), "Focus script not created");
324 public static void Focus_Load (Page p)
326 TextBox tbx = new TextBox ();
328 p.Controls.Add (tbx);
331 int contain (string orig, string compare)
333 if (orig.IndexOf (compare) == -1)
335 return 1 + contain (orig.Substring (orig.IndexOf (compare) + compare.Length), compare);
339 [Category ("NotWorking")] // Not implemented exception
340 public void HasEvent ()
342 MyNC ctrl1 = new MyNC ();
343 Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#1");
344 EventHandler ctrl_hdlr = new EventHandler (ctrl1_Init);
345 ctrl1.Init += new EventHandler (ctrl1_Init);
346 ctrl1.Init += ctrl_hdlr;
347 Assert.AreEqual (true, ctrl1.HasEvents (), "HasEvent#2");
348 // Dosn't work than removed handler
349 //ctrl1.Init -= ctrl_hdlr;
350 //Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#3");
352 void ctrl1_Init (object sender, EventArgs e)
354 throw new Exception ("The method or operation is not implemented.");
358 [Category ("NotWorking")]
359 public void IsViewStateEnabled ()
361 DerivedControl c = new DerivedControl ();
362 Assert.IsTrue (c.DoIsViewStateEnabled);
363 Page p = new Page ();
366 Assert.IsTrue (c.DoIsViewStateEnabled);
367 p.EnableViewState = false;
368 Assert.IsFalse (c.DoIsViewStateEnabled);
372 [Category ("NotWorking")]
373 [Category ("NunitWeb")]
374 public void ControlState ()
376 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
378 FormRequest fr = new FormRequest (t.Response, "form1");
379 fr.Controls.Add ("__EVENTTARGET");
380 fr.Controls.Add ("__EVENTARGUMENT");
381 fr.Controls["__EVENTTARGET"].Value = "";
382 fr.Controls["__EVENTARGUMENT"].Value = "";
386 public static void ControlState_Load (Page p)
388 ControlWithState c1 = new ControlWithState ();
389 ControlWithState c2 = new ControlWithState ();
390 c1.Controls.Add (c2);
391 p.Form.Controls.Add (c1);
397 ControlWithState c3 = new ControlWithState ();
398 p.Form.Controls.Add (c3);
399 Assert.AreEqual ("State", c1.State, "ControlState");
400 Assert.AreEqual ("Cool", c2.State, "ControlState");
405 [Category ("NotWorking")] // Not implemented exception
406 public void ClientIDSeparator ()
408 DerivedControl ctrl = new DerivedControl ();
409 Assert.AreEqual (95, (int) ctrl.ClientIDSeparator, "ClientIDSeparator");
413 [Category ("NotWorking")] // Not implemented exception
414 public void IDSeparator ()
416 DerivedControl ctrl = new DerivedControl ();
417 Assert.AreEqual (36, (int) ctrl.IdSeparator, "IDSeparator");
421 [Category ("NunitWeb")]
422 [Category ("NotWorking")]
423 public void IsChildControlStateCleared ()
425 WebTest t = new WebTest (PageInvoker.CreateOnLoad (IsChildControlStateCleared_Load));
427 FormRequest fr = new FormRequest (t.Response, "form1");
428 fr.Controls.Add ("__EVENTTARGET");
429 fr.Controls.Add ("__EVENTARGUMENT");
430 fr.Controls["__EVENTTARGET"].Value = "";
431 fr.Controls["__EVENTARGUMENT"].Value = "";
435 public static void IsChildControlStateCleared_Load (Page p)
437 ParentControlWithState c1 = new ParentControlWithState ();
438 ControlWithState c2 = new ControlWithState ();
439 c1.Controls.Add (c2);
440 p.Form.Controls.Add (c1);
446 c1.ClearChildControlState ();
447 Assert.AreEqual ("State", c1.State, "ControlState#1");
448 Assert.AreEqual (null, c2.State, "ControlState#2");
449 Assert.AreEqual (true, c1.IsChildControlStateCleared, "IsChildControlStateCleared");
454 [Category ("NotWorking")] // Not implemented exception
455 [Category ("NunitWeb")]
456 public void LoadViewStateByID ()
458 ParentControlWithState c1 = new ParentControlWithState ();
459 ControlWithState c2 = new ControlWithState ();
460 c1.Controls.Add (c2);
461 Assert.AreEqual (false, c1.LoadViewStateByID, "LoadViewStateByID#1");
465 [Category ("NotWorking")] // Not implemented exception
466 [Category ("NunitWeb")]
467 public void OpenFile ()
469 WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFile_Load));
472 public static void OpenFile_Load (Page p)
474 DerivedControl ctrl = new DerivedControl ();
475 Stream strem = ctrl.OpenFile ("~/MyPage.aspx");
476 Assert.IsNotNull (strem, "OpenFile failed");
480 [Category ("NotWorking")]
481 [Category ("NunitWeb")]
482 [ExpectedException (typeof (FileNotFoundException))]
483 public void OpenFile_Exception ()
485 WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFileException_Load));
488 public static void OpenFileException_Load (Page p)
490 DerivedControl ctrl = new DerivedControl ();
491 Stream strem = ctrl.OpenFile ("~/Fake.tmp");
494 //// MonoTests.SystemWeb.Framework limitation for Add_browsers - directory include in project
496 //[Category ("NunitWeb")]
497 //public void ResolveAdapter_1 ()
499 // WebTest.CopyResource (GetType (), "adapters.browser", "App_Browsers/adapters.browser");
500 // WebTest t = new WebTest (PageInvoker.CreateOnInit (ResolveAdapter_Init));
501 // string html = t.Run ();
503 //public static void ResolveAdapter_Init (Page p)
505 // Customadaptercontrol ctrl = new Customadaptercontrol ();
506 // p.Controls.Add (ctrl);
507 // ctrl.Load += new EventHandler (ctrl_Load);
509 //static void ctrl_Load (object sender, EventArgs e)
511 // Assert.IsNotNull (((Customadaptercontrol) sender).ResolveAdapter (), "ResolveAdapter Failed#1");
512 // Assert.AreEqual ("Customadapter", ((Customadaptercontrol) sender).ResolveAdapter ().ToString (), "ResolveAdapter Failed#2");
516 [Category ("NunitWeb")]
517 public void ResolveClientUrl ()
519 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveClientUrl_Load));
520 string html = t.Run ();
522 public static void ResolveClientUrl_Load (Page p)
524 Control ctrl = new Control ();
525 p.Controls.Add (ctrl);
526 string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
527 Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
531 [Category ("NotWorking")] // Not implemented exception
532 public void ResolveAdapter_2 ()
534 DerivedControl ctrl = new DerivedControl ();
535 Assert.AreEqual (null, ctrl.ResolveAdapter (), "ResolveAdapter");
539 public void EnableTheming ()
541 DerivedControl ctrl = new DerivedControl ();
542 Assert.AreEqual (true, ctrl.EnableTheming, "EnableTheming#1");
543 ctrl.EnableTheming = false;
544 Assert.AreEqual (false, ctrl.EnableTheming, "EnableTheming#2");
549 public void BindingContainer ()
551 ControlWithTemplate c = new ControlWithTemplate ();
552 c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
554 // cause CreateChildControls called
555 c.FindControl ("stam");
557 static void BindingContainer_BuildTemplate (Control control)
559 Control child1 = new Control ();
560 control.Controls.Add (child1);
562 Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
563 Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
565 NamingContainer nc = new NamingContainer ();
566 Control child2 = new Control ();
567 nc.Controls.Add (child2);
568 control.Controls.Add (nc);
570 Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
571 Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
574 // DetailsViewPagerRow marked to be not BindingContainer
575 DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
576 TableCell cell = new TableCell ();
577 Control child3 = new Control ();
578 cell.Controls.Add (child3);
579 row.Cells.Add (cell);
580 control.Controls.Add (row);
582 Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
583 Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
588 public void Contorl_Adapter ()
590 MyNC ctr = new MyNC ();
591 Assert.AreEqual (null, ctr.Adapter (), "Adapter");
595 [TestFixtureTearDown]
596 public void Tear_down ()
604 class ControlWithState : Control
610 get { return _state; }
611 set { _state = value; }
614 protected override void OnInit (EventArgs e)
617 Page.RegisterRequiresControlState (this);
618 Page.RegisterRequiresControlState (this);
621 protected override object SaveControlState ()
626 protected override void LoadControlState (object savedState)
628 State = (string) savedState;
631 public new void ClearChildControlState ()
633 base.ClearChildControlState ();
637 class ParentControlWithState : Control
642 get { return _state; }
643 set { _state = value; }
646 protected override void OnInit (EventArgs e)
649 Page.RegisterRequiresControlState (this);
650 Page.RegisterRequiresControlState (this);
653 protected override void CreateChildControls ()
656 base.CreateChildControls ();
657 ClearChildControlState ();
660 protected override object SaveControlState ()
665 protected override void LoadControlState (object savedState)
667 State = (string) savedState;
670 public new void ClearChildControlState ()
672 base.ClearChildControlState ();
675 public new bool IsChildControlStateCleared
677 get { return base.IsChildControlStateCleared; }
680 public new bool LoadViewStateByID
682 get { return base.LoadViewStateByID; }
686 class ParentControlWithViewState : Control
690 public string Viewstate
692 get { return _viewstate; }
693 set { _viewstate = value; }
698 get { return _state; }
699 set { _state = value; }
702 protected override void OnInit (EventArgs e)
705 Page.RegisterRequiresControlState (this);
706 Page.RegisterRequiresControlState (this);
709 protected override void CreateChildControls ()
712 base.CreateChildControls ();
713 ClearChildControlState ();
716 protected override object SaveViewState ()
721 protected override void LoadViewState (object savedState)
723 Viewstate = (string) savedState;
726 protected override object SaveControlState ()
731 protected override void LoadControlState (object savedState)
733 State = (string) savedState;
736 public new void ClearChildControlState ()
738 base.ClearChildControlState ();
742 class MyNC : Control, INamingContainer
744 public ControlAdapter Adapter ()
749 public new void DataBind (bool opt)
754 public new void DataBindChildren ()
756 base.DataBindChildren ();
759 public new void EnsureID ()
764 public new bool HasEvents ()
766 return base.HasEvents ();
770 class DerivedControl : Control
772 ControlCollection coll;
774 public DerivedControl ()
776 coll = new ControlCollection (this);
779 public override ControlCollection Controls
785 public bool DoIsViewStateEnabled
787 get { return IsViewStateEnabled; }
790 public new char ClientIDSeparator
792 get { return base.ClientIDSeparator; }
795 public new char IdSeparator
797 get { return base.IdSeparator; }
800 public new Stream OpenFile (string path)
802 return base.OpenFile (path);
805 public new ControlAdapter ResolveAdapter ()
807 return base.ResolveAdapter ();
812 class NamingContainer : Control, INamingContainer
816 class ControlWithTemplate : Control
820 [TemplateContainer (typeof (TemplateContainer))]
821 public ITemplate Template
823 get { return _template; }
824 set { _template = value; }
827 protected override void CreateChildControls ()
831 TemplateContainer container = new TemplateContainer ();
832 Controls.Add (container);
834 if (Template != null)
835 Template.InstantiateIn (container);
839 class TemplateContainer : Control, INamingContainer
846 public class Customadaptercontrol : Control
848 public new ControlAdapter ResolveAdapter ()
850 return base.ResolveAdapter ();
854 public class Customadapter : ControlAdapter