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;
43 namespace MonoTests.System.Web.UI
46 public class ControlTest {
48 public void DataBindingInterfaceTest ()
51 DataBindingCollection db;
54 Assert.AreEqual (false, ((IDataBindingsAccessor)c).HasDataBindings, "DB1");
55 db = ((IDataBindingsAccessor)c).DataBindings;
56 Assert.IsNotNull (db, "DB2");
57 Assert.AreEqual (false, ((IDataBindingsAccessor)c).HasDataBindings, "DB3");
58 db.Add(new DataBinding ("property", typeof(bool), "expression"));
59 Assert.AreEqual (true, ((IDataBindingsAccessor)c).HasDataBindings);
63 class MyNC : Control, INamingContainer {
67 public void UniqueID1 ()
70 Control nc = new MyNC ();
71 Assert.IsNull (nc.UniqueID, "nulltest");
75 public void UniqueID2 ()
78 Control nc = new MyNC ();
79 Control nc2 = new MyNC ();
80 nc2.Controls.Add (nc);
81 Assert.IsNotNull (nc.UniqueID, "notnull");
82 Assert.IsTrue (nc.UniqueID.IndexOfAny (new char [] {':', '$' }) == -1, "separator");
86 public void UniqueID3 ()
89 Control control = new Control ();
90 Control nc = new MyNC ();
92 control.Controls.Add (nc);
93 Assert.IsNull (nc.UniqueID, "null");
97 public void UniqueID4 ()
100 Control control = new Control ();
101 Control nc = new MyNC ();
103 nc.Controls.Add (control);
104 Assert.IsNotNull (control.UniqueID, "notnull");
108 public void UniqueID5 ()
111 Control control = new Control ();
112 Control nc = new MyNC ();
113 Control nc2 = new MyNC ();
115 nc2.Controls.Add (nc);
116 nc.Controls.Add (control);
117 Assert.IsNotNull (control.UniqueID, "notnull");
118 Assert.IsNull (nc2.ID, "null-1");
119 Assert.IsNull (nc.ID, "null-2");
120 Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char [] {':', '$' }), "separator");
123 class DerivedControl : Control {
124 ControlCollection coll;
126 public DerivedControl ()
128 coll = new ControlCollection (this);
131 public override ControlCollection Controls {
136 public bool DoIsViewStateEnabled {
137 get { return IsViewStateEnabled; }
142 // From bug #76919: Control uses _controls instead of
143 // Controls when RenderChildren is called.
145 public void Controls1 ()
147 DerivedControl derived = new DerivedControl ();
148 derived.Controls.Add (new LiteralControl ("hola"));
149 StringWriter sw = new StringWriter ();
150 HtmlTextWriter htw = new HtmlTextWriter (sw);
151 derived.RenderControl (htw);
152 string result = sw.ToString ();
154 Assert.AreEqual ("", result, "#01");
159 public void ApplyStyleSheetSkin ()
161 Page p = new Page ();
162 p.StyleSheetTheme = "";
163 Control c = new Control ();
164 c.ApplyStyleSheetSkin (p);
168 [Category ("NotWorking")]
169 public void IsViewStateEnabled ()
171 DerivedControl c = new DerivedControl ();
172 Assert.IsTrue (c.DoIsViewStateEnabled);
173 Page p = new Page ();
176 Assert.IsTrue (c.DoIsViewStateEnabled);
177 p.EnableViewState = false;
178 Assert.IsFalse (c.DoIsViewStateEnabled);
182 [Category ("NunitWeb")]
183 public void ControlState () {
184 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
186 FormRequest fr = new FormRequest (t.Response, "form1");
187 fr.Controls.Add ("__EVENTTARGET");
188 fr.Controls.Add ("__EVENTARGUMENT");
189 fr.Controls ["__EVENTTARGET"].Value = "";
190 fr.Controls ["__EVENTARGUMENT"].Value = "";
195 public static void ControlState_Load (Page p) {
196 ControlWithState c1 = new ControlWithState ();
197 ControlWithState c2 = new ControlWithState ();
198 c1.Controls.Add (c2);
199 p.Form.Controls.Add (c1);
205 ControlWithState c3 = new ControlWithState ();
206 p.Form.Controls.Add (c3);
207 Assert.AreEqual ("State", c1.State, "ControlState");
208 Assert.AreEqual ("Cool", c2.State, "ControlState");
212 class ControlWithState : Control
216 public string State {
217 get { return _state; }
218 set { _state = value; }
221 protected override void OnInit (EventArgs e) {
223 Page.RegisterRequiresControlState (this);
224 Page.RegisterRequiresControlState (this);
227 protected override object SaveControlState () {
231 protected override void LoadControlState (object savedState) {
232 State = (string) savedState;
238 public void BindingContainer ()
240 ControlWithTemplate c = new ControlWithTemplate ();
241 c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
243 // cause CreateChildControls called
244 c.FindControl ("stam");
247 static void BindingContainer_BuildTemplate (Control control)
249 Control child1 = new Control ();
250 control.Controls.Add (child1);
252 Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
253 Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
255 NamingContainer nc = new NamingContainer ();
256 Control child2 = new Control ();
257 nc.Controls.Add (child2);
258 control.Controls.Add (nc);
260 Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
261 Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
264 // DetailsViewPagerRow marked to be not BindingContainer
265 DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
266 TableCell cell = new TableCell ();
267 Control child3 = new Control ();
268 cell.Controls.Add (child3);
269 row.Cells.Add (cell);
270 control.Controls.Add (row);
272 Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
273 Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
277 class NamingContainer : Control, INamingContainer
281 class ControlWithTemplate : Control
285 [TemplateContainer (typeof(TemplateContainer))]
286 public ITemplate Template {
287 get { return _template; }
288 set { _template = value; }
291 protected override void CreateChildControls () {
294 TemplateContainer container = new TemplateContainer ();
295 Controls.Add (container);
297 if (Template != null)
298 Template.InstantiateIn (container);
302 class TemplateContainer : Control, INamingContainer