This should fix #76928. This fix incorporates ideas from a patch
[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
42 namespace MonoTests.System.Web.UI
43 {
44         [TestFixture]   
45         public class ControlTest {
46                 [Test]
47                 public void DataBindingInterfaceTest ()
48                 {
49                         Control c;
50                         DataBindingCollection db;
51
52                         c = new Control ();
53                         Assert.AreEqual (false, ((IDataBindingsAccessor)c).HasDataBindings, "DB1");
54                         db = ((IDataBindingsAccessor)c).DataBindings;
55                         Assert.IsNotNull (db, "DB2");
56                         Assert.AreEqual (false, ((IDataBindingsAccessor)c).HasDataBindings, "DB3");
57                         db.Add(new DataBinding ("property", typeof(bool), "expression"));
58                         Assert.AreEqual (true, ((IDataBindingsAccessor)c).HasDataBindings);
59
60                 }
61
62                 class MyNC : Control, INamingContainer {
63                 }
64
65                 [Test]
66                 public void UniqueID1 ()
67                 {
68                         // Standalone NC
69                         Control nc = new MyNC ();
70                         Assert.IsNull (nc.UniqueID, "nulltest");
71                 }
72
73                 [Test]
74                 public void UniqueID2 ()
75                 {
76                         // NC in NC
77                         Control nc = new MyNC ();
78                         Control nc2 = new MyNC ();
79                         nc2.Controls.Add (nc);
80                         Assert.IsNotNull (nc.UniqueID, "notnull");
81                         Assert.IsTrue (nc.UniqueID.IndexOfAny (new char [] {':', '$' }) == -1, "separator");
82                 }
83
84                 [Test]
85                 public void UniqueID3 ()
86                 {
87                         // NC in control
88                         Control control = new Control ();
89                         Control nc = new MyNC ();
90
91                         control.Controls.Add (nc);
92                         Assert.IsNull (nc.UniqueID, "null");
93                 }
94
95                 [Test]
96                 public void UniqueID4 ()
97                 {
98                         // NC in control
99                         Control control = new Control ();
100                         Control nc = new MyNC ();
101
102                         nc.Controls.Add (control);
103                         Assert.IsNotNull (control.UniqueID, "notnull");
104                 }
105
106                 [Test]
107                 public void UniqueID5 ()
108                 {
109                         // NC in control
110                         Control control = new Control ();
111                         Control nc = new MyNC ();
112                         Control nc2 = new MyNC ();
113
114                         nc2.Controls.Add (nc);
115                         nc.Controls.Add (control);
116                         Assert.IsNotNull (control.UniqueID, "notnull");
117                         Assert.IsNull (nc2.ID, "null-1");
118                         Assert.IsNull (nc.ID, "null-2");
119                         Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char [] {':', '$' }), "separator");
120                 }
121
122                 class DerivedControl : Control {
123                         ControlCollection coll;
124
125                         public DerivedControl ()
126                         {
127                                 coll = new ControlCollection (this);
128                         }
129
130                         public override ControlCollection Controls {
131                                 get { return coll; }
132                         }
133                 }
134
135                 // From bug #76919: Control uses _controls instead of
136                 // Controls when RenderChildren is called.
137                 [Test]
138                 public void Controls1 ()
139                 {
140                         DerivedControl derived = new DerivedControl ();
141                         derived.Controls.Add (new LiteralControl ("hola"));
142                         StringWriter sw = new StringWriter ();
143                         HtmlTextWriter htw = new HtmlTextWriter (sw);
144                         derived.RenderControl (htw);
145                         string result = sw.ToString ();
146
147                         Assert.AreEqual ("", result, "#01");
148                 }
149         }
150 }
151