2006-11-09 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / ControlTest.cs
1 //
2 // Tests for System.Web.UI.Control
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@novell.com)
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.Collections;
35 using System.Drawing;
36 using System.IO;
37 using System.Globalization;
38 using System.Web;
39 using System.Web.UI;
40 using System.Web.UI.WebControls;
41 using MonoTests.SystemWeb.Framework;
42
43 namespace MonoTests.System.Web.UI
44 {
45         [TestFixture]   
46         public class ControlTest {
47                 [Test]
48                 public void DataBindingInterfaceTest ()
49                 {
50                         Control c;
51                         DataBindingCollection db;
52
53                         c = new Control ();
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);
60
61                 }
62
63                 class MyNC : Control, INamingContainer {
64                 }
65
66                 [Test]
67                 public void UniqueID1 ()
68                 {
69                         // Standalone NC
70                         Control nc = new MyNC ();
71                         Assert.IsNull (nc.UniqueID, "nulltest");
72                 }
73
74                 [Test]
75                 public void UniqueID2 ()
76                 {
77                         // NC in NC
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");
83                 }
84
85                 [Test]
86                 public void UniqueID3 ()
87                 {
88                         // NC in control
89                         Control control = new Control ();
90                         Control nc = new MyNC ();
91
92                         control.Controls.Add (nc);
93                         Assert.IsNull (nc.UniqueID, "null");
94                 }
95
96                 [Test]
97                 public void UniqueID4 ()
98                 {
99                         // NC in control
100                         Control control = new Control ();
101                         Control nc = new MyNC ();
102
103                         nc.Controls.Add (control);
104                         Assert.IsNotNull (control.UniqueID, "notnull");
105                 }
106
107                 [Test]
108                 public void UniqueID5 ()
109                 {
110                         // NC in control
111                         Control control = new Control ();
112                         Control nc = new MyNC ();
113                         Control nc2 = new MyNC ();
114
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");
121                 }
122
123                 class DerivedControl : Control {
124                         ControlCollection coll;
125
126                         public DerivedControl ()
127                         {
128                                 coll = new ControlCollection (this);
129                         }
130
131                         public override ControlCollection Controls {
132                                 get { return coll; }
133                         }
134                         
135 #if NET_2_0
136                         public bool DoIsViewStateEnabled {
137                                 get { return IsViewStateEnabled; }
138                         }
139 #endif
140                 }
141
142                 // From bug #76919: Control uses _controls instead of
143                 // Controls when RenderChildren is called.
144                 [Test]
145                 public void Controls1 ()
146                 {
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 ();
153
154                         Assert.AreEqual ("", result, "#01");
155                 }
156
157 #if NET_2_0
158                 [Test]
159                 public void ApplyStyleSheetSkin ()
160                 {
161                         Page p = new Page ();
162                         p.StyleSheetTheme = "";
163                         Control c = new Control ();
164                         c.ApplyStyleSheetSkin (p);
165                 }
166                 
167                 [Test]
168         [Category ("NotWorking")]
169                 public void IsViewStateEnabled ()
170                 {
171                         DerivedControl c = new DerivedControl ();
172                         Assert.IsTrue (c.DoIsViewStateEnabled);
173                         Page p = new Page ();
174                         c.Page = p;
175                         p.Controls.Add (c);
176                         Assert.IsTrue (c.DoIsViewStateEnabled);
177                         p.EnableViewState = false;
178                         Assert.IsFalse (c.DoIsViewStateEnabled);
179                 }
180
181                 [Test]
182                 [Category ("NunitWeb")]
183                 public void ControlState () {
184                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
185                         t.Run ();
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 = "";
191                         t.Request = fr;
192                         t.Run ();
193                 }
194
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);
200                         if (!p.IsPostBack) {
201                                 c1.State = "State";
202                                 c2.State = "Cool";
203                         }
204                         else {
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");
209                         }
210                 }
211
212                 class ControlWithState : Control
213                 {
214                         string _state;
215
216                         public string State {
217                                 get { return _state; }
218                                 set { _state = value; }
219                         }
220
221                         protected override void OnInit (EventArgs e) {
222                                 base.OnInit (e);
223                                 Page.RegisterRequiresControlState (this);
224                                 Page.RegisterRequiresControlState (this);
225                         }
226
227                         protected override object SaveControlState () {
228                                 return State;
229                         }
230
231                         protected override void LoadControlState (object savedState) {
232                                 State = (string) savedState;
233                         }
234                 }
235 #endif
236
237                 [Test]
238                 public void BindingContainer ()
239                 {
240                         ControlWithTemplate c = new ControlWithTemplate ();
241                         c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
242
243                         // cause CreateChildControls called
244                         c.FindControl ("stam");
245                 }
246
247                 static void BindingContainer_BuildTemplate (Control control)
248                 {
249                         Control child1 = new Control ();
250                         control.Controls.Add (child1);
251
252                         Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
253                         Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
254
255                         NamingContainer nc = new NamingContainer ();
256                         Control child2 = new Control ();
257                         nc.Controls.Add (child2);
258                         control.Controls.Add (nc);
259
260                         Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
261                         Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
262
263 #if NET_2_0
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);
271
272                         Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
273                         Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
274 #endif
275                 }
276
277                 class NamingContainer : Control, INamingContainer
278                 {
279                 }
280
281                 class ControlWithTemplate : Control
282                 {
283                         ITemplate _template;
284
285                         [TemplateContainer (typeof(TemplateContainer))]
286                         public ITemplate Template {
287                                 get { return _template; }
288                                 set { _template = value; }
289                         }
290
291                         protected override void CreateChildControls () {
292                                 Controls.Clear ();
293
294                                 TemplateContainer container = new TemplateContainer ();
295                                 Controls.Add (container);
296
297                                 if (Template != null)
298                                         Template.InstantiateIn (container);
299                         }
300                 }
301
302                 class TemplateContainer : Control, INamingContainer
303                 {
304                 }
305         }
306 }
307