ControlTest.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // Tests for System.Web.UI.Control
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Drawing;
  33. using System.IO;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. using MonoTests.SystemWeb.Framework;
  39. namespace MonoTests.System.Web.UI
  40. {
  41. [TestFixture]
  42. public class ControlTest {
  43. [Test]
  44. public void DataBindingInterfaceTest ()
  45. {
  46. Control c;
  47. DataBindingCollection db;
  48. c = new Control ();
  49. Assert.AreEqual (false, ((IDataBindingsAccessor)c).HasDataBindings, "DB1");
  50. db = ((IDataBindingsAccessor)c).DataBindings;
  51. Assert.IsNotNull (db, "DB2");
  52. Assert.AreEqual (false, ((IDataBindingsAccessor)c).HasDataBindings, "DB3");
  53. db.Add(new DataBinding ("property", typeof(bool), "expression"));
  54. Assert.AreEqual (true, ((IDataBindingsAccessor)c).HasDataBindings);
  55. }
  56. class MyNC : Control, INamingContainer {
  57. }
  58. [Test]
  59. public void UniqueID1 ()
  60. {
  61. // Standalone NC
  62. Control nc = new MyNC ();
  63. Assert.IsNull (nc.UniqueID, "nulltest");
  64. }
  65. [Test]
  66. public void UniqueID2 ()
  67. {
  68. // NC in NC
  69. Control nc = new MyNC ();
  70. Control nc2 = new MyNC ();
  71. nc2.Controls.Add (nc);
  72. Assert.IsNotNull (nc.UniqueID, "notnull");
  73. Assert.IsTrue (nc.UniqueID.IndexOfAny (new char [] {':', '$' }) == -1, "separator");
  74. }
  75. [Test]
  76. public void UniqueID3 ()
  77. {
  78. // NC in control
  79. Control control = new Control ();
  80. Control nc = new MyNC ();
  81. control.Controls.Add (nc);
  82. Assert.IsNull (nc.UniqueID, "null");
  83. }
  84. [Test]
  85. public void UniqueID4 ()
  86. {
  87. // NC in control
  88. Control control = new Control ();
  89. Control nc = new MyNC ();
  90. nc.Controls.Add (control);
  91. Assert.IsNotNull (control.UniqueID, "notnull");
  92. }
  93. [Test]
  94. public void UniqueID5 ()
  95. {
  96. // NC in control
  97. Control control = new Control ();
  98. Control nc = new MyNC ();
  99. Control nc2 = new MyNC ();
  100. nc2.Controls.Add (nc);
  101. nc.Controls.Add (control);
  102. Assert.IsNotNull (control.UniqueID, "notnull");
  103. Assert.IsNull (nc2.ID, "null-1");
  104. Assert.IsNull (nc.ID, "null-2");
  105. Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char [] {':', '$' }), "separator");
  106. }
  107. class DerivedControl : Control {
  108. ControlCollection coll;
  109. public DerivedControl ()
  110. {
  111. coll = new ControlCollection (this);
  112. }
  113. public override ControlCollection Controls {
  114. get { return coll; }
  115. }
  116. #if NET_2_0
  117. public bool DoIsViewStateEnabled {
  118. get { return IsViewStateEnabled; }
  119. }
  120. #endif
  121. }
  122. // From bug #76919: Control uses _controls instead of
  123. // Controls when RenderChildren is called.
  124. [Test]
  125. public void Controls1 ()
  126. {
  127. DerivedControl derived = new DerivedControl ();
  128. derived.Controls.Add (new LiteralControl ("hola"));
  129. StringWriter sw = new StringWriter ();
  130. HtmlTextWriter htw = new HtmlTextWriter (sw);
  131. derived.RenderControl (htw);
  132. string result = sw.ToString ();
  133. Assert.AreEqual ("", result, "#01");
  134. }
  135. #if NET_2_0
  136. [Test]
  137. public void ApplyStyleSheetSkin ()
  138. {
  139. Page p = new Page ();
  140. p.StyleSheetTheme = "";
  141. Control c = new Control ();
  142. c.ApplyStyleSheetSkin (p);
  143. }
  144. [Test]
  145. [Category ("NotWorking")]
  146. public void IsViewStateEnabled ()
  147. {
  148. DerivedControl c = new DerivedControl ();
  149. Assert.IsTrue (c.DoIsViewStateEnabled);
  150. Page p = new Page ();
  151. c.Page = p;
  152. p.Controls.Add (c);
  153. Assert.IsTrue (c.DoIsViewStateEnabled);
  154. p.EnableViewState = false;
  155. Assert.IsFalse (c.DoIsViewStateEnabled);
  156. }
  157. [Test]
  158. [Category ("NunitWeb")]
  159. public void ControlState () {
  160. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
  161. t.Run ();
  162. FormRequest fr = new FormRequest (t.Response, "form1");
  163. fr.Controls.Add ("__EVENTTARGET");
  164. fr.Controls.Add ("__EVENTARGUMENT");
  165. fr.Controls ["__EVENTTARGET"].Value = "";
  166. fr.Controls ["__EVENTARGUMENT"].Value = "";
  167. t.Request = fr;
  168. t.Run ();
  169. }
  170. public static void ControlState_Load (Page p) {
  171. ControlWithState c1 = new ControlWithState ();
  172. ControlWithState c2 = new ControlWithState ();
  173. c1.Controls.Add (c2);
  174. p.Form.Controls.Add (c1);
  175. if (!p.IsPostBack) {
  176. c1.State = "State";
  177. c2.State = "Cool";
  178. }
  179. else {
  180. ControlWithState c3 = new ControlWithState ();
  181. p.Form.Controls.Add (c3);
  182. Assert.AreEqual ("State", c1.State, "ControlState");
  183. Assert.AreEqual ("Cool", c2.State, "ControlState");
  184. }
  185. }
  186. class ControlWithState : Control
  187. {
  188. string _state;
  189. public string State {
  190. get { return _state; }
  191. set { _state = value; }
  192. }
  193. protected override void OnInit (EventArgs e) {
  194. base.OnInit (e);
  195. Page.RegisterRequiresControlState (this);
  196. Page.RegisterRequiresControlState (this);
  197. }
  198. protected override object SaveControlState () {
  199. return State;
  200. }
  201. protected override void LoadControlState (object savedState) {
  202. State = (string) savedState;
  203. }
  204. }
  205. #endif
  206. [Test]
  207. public void BindingContainer ()
  208. {
  209. ControlWithTemplate c = new ControlWithTemplate ();
  210. c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
  211. // cause CreateChildControls called
  212. c.FindControl ("stam");
  213. }
  214. static void BindingContainer_BuildTemplate (Control control)
  215. {
  216. Control child1 = new Control ();
  217. control.Controls.Add (child1);
  218. Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
  219. Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
  220. NamingContainer nc = new NamingContainer ();
  221. Control child2 = new Control ();
  222. nc.Controls.Add (child2);
  223. control.Controls.Add (nc);
  224. Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
  225. Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
  226. #if NET_2_0
  227. // DetailsViewPagerRow marked to be not BindingContainer
  228. DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
  229. TableCell cell = new TableCell ();
  230. Control child3 = new Control ();
  231. cell.Controls.Add (child3);
  232. row.Cells.Add (cell);
  233. control.Controls.Add (row);
  234. Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
  235. Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
  236. #endif
  237. }
  238. class NamingContainer : Control, INamingContainer
  239. {
  240. }
  241. class ControlWithTemplate : Control
  242. {
  243. ITemplate _template;
  244. [TemplateContainer (typeof(TemplateContainer))]
  245. public ITemplate Template {
  246. get { return _template; }
  247. set { _template = value; }
  248. }
  249. protected override void CreateChildControls () {
  250. Controls.Clear ();
  251. TemplateContainer container = new TemplateContainer ();
  252. Controls.Add (container);
  253. if (Template != null)
  254. Template.InstantiateIn (container);
  255. }
  256. }
  257. class TemplateContainer : Control, INamingContainer
  258. {
  259. }
  260. }
  261. }