HierarchicalDataBoundControlTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Tests for System.Web.UI.WebControls.HierarchicalDataBoundControl.cs
  3. //
  4. // Author:
  5. // Igor Zelmanovich ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2006 Mainsoft, Inc (http://www.mainsoft.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. #if NET_2_0
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Text;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.Adapters;
  36. using System.Web.UI.WebControls;
  37. using System.Web.UI.WebControls.Adapters;
  38. using System.IO;
  39. using System.Drawing;
  40. using System.Threading;
  41. using MyWebControl = System.Web.UI.WebControls;
  42. using System.Collections;
  43. using MonoTests.SystemWeb.Framework;
  44. using MonoTests.stand_alone.WebHarness;
  45. using System.Text.RegularExpressions;
  46. using System.Reflection;
  47. namespace MonoTests.System.Web.UI.WebControls
  48. {
  49. [Serializable]
  50. [TestFixture]
  51. public class HierarchicalDataBoundControlTest
  52. {
  53. class MyHierarchicalDataBoundControl : HierarchicalDataBoundControl
  54. {
  55. private StringBuilder dataBindTrace;
  56. public string DataBindTrace {
  57. get { return dataBindTrace.ToString (); }
  58. }
  59. public override void DataBind () {
  60. dataBindTrace = new StringBuilder ();
  61. dataBindTrace.Append("[Start DataBind]");
  62. base.DataBind ();
  63. dataBindTrace.Append ("[End DataBind]");
  64. }
  65. protected override void PerformSelect () {
  66. dataBindTrace.Append ("[Start PerformSelect]");
  67. base.PerformSelect ();
  68. dataBindTrace.Append ("[End PerformSelect]");
  69. }
  70. protected override void PerformDataBinding () {
  71. dataBindTrace.Append ("[Start PerformDataBinding]");
  72. base.PerformDataBinding ();
  73. dataBindTrace.Append ("[End PerformDataBinding]");
  74. }
  75. public HierarchicalDataSourceView GetData (string path) {
  76. dataBindTrace.Append ("[Start GetData]");
  77. return base.GetData (path);
  78. }
  79. protected override void OnDataBinding (EventArgs e) {
  80. dataBindTrace.Append ("[Start OnDataBinding]");
  81. base.OnDataBinding (e);
  82. dataBindTrace.Append ("[End OnDataBinding]");
  83. }
  84. protected override void OnDataBound (EventArgs e) {
  85. dataBindTrace.Append ("[Start OnDataBound]");
  86. base.OnDataBound (e);
  87. dataBindTrace.Append ("[End OnDataBound]");
  88. }
  89. internal void DoValidateDataSource (object dataSource)
  90. {
  91. ValidateDataSource (dataSource);
  92. }
  93. internal ControlAdapter controlAdapter;
  94. protected override global::System.Web.UI.Adapters.ControlAdapter ResolveAdapter ()
  95. {
  96. return controlAdapter;
  97. }
  98. }
  99. [Test]
  100. public void HierarchicalDataBoundControl_DataBindFlow () {
  101. Page p = new Page ();
  102. MyHierarchicalDataBoundControl hc = new MyHierarchicalDataBoundControl ();
  103. p.Controls.Add (hc);
  104. hc.DataBind ();
  105. string expected = "[Start DataBind][Start PerformSelect][Start OnDataBinding][End OnDataBinding][Start PerformDataBinding][End PerformDataBinding][Start OnDataBound][End OnDataBound][End PerformSelect][End DataBind]";
  106. Assert.AreEqual (expected, hc.DataBindTrace, "DataBindFlow");
  107. }
  108. [Test]
  109. public void HierarchicalDataBoundControl_ValidateDataSource_Null ()
  110. {
  111. MyHierarchicalDataBoundControl hc = new MyHierarchicalDataBoundControl ();
  112. hc.DoValidateDataSource (null);
  113. }
  114. class MyHierarchicalDataBoundControlAdapter : HierarchicalDataBoundControlAdapter
  115. {
  116. internal bool perform_data_binding_called;
  117. protected override void PerformDataBinding ()
  118. {
  119. perform_data_binding_called = true;
  120. }
  121. }
  122. class MyControlAdapter : ControlAdapter
  123. {
  124. }
  125. [Test]
  126. public void PerformDataBinding_UsesAdapter ()
  127. {
  128. MyHierarchicalDataBoundControl c = new MyHierarchicalDataBoundControl ();
  129. MyHierarchicalDataBoundControlAdapter a = new MyHierarchicalDataBoundControlAdapter();;
  130. c.controlAdapter = a;
  131. c.DataBind ();
  132. Assert.IsTrue (a.perform_data_binding_called, "PerformDataBinding_UsesAdapter");
  133. }
  134. [Test]
  135. public void PerformDataBinding_WorksWithControlAdapter ()
  136. {
  137. MyHierarchicalDataBoundControl c = new MyHierarchicalDataBoundControl ();
  138. MyControlAdapter a = new MyControlAdapter();
  139. c.controlAdapter = a;
  140. c.DataBind ();
  141. }
  142. public class TestHierarchy : IHierarchicalEnumerable
  143. {
  144. List<TestNode> list;
  145. public TestHierarchy (List<TestNode> source)
  146. {
  147. list = source;
  148. }
  149. public IHierarchyData GetHierarchyData (object enumeratedItem)
  150. {
  151. return enumeratedItem as TestNode;
  152. }
  153. public IEnumerator GetEnumerator ()
  154. {
  155. return list.GetEnumerator ();
  156. }
  157. }
  158. public class TestNode : IHierarchyData
  159. {
  160. string name;
  161. TestNode parent;
  162. List<TestNode> childNodes;
  163. public TestNode (string name, TestNode parent, List<TestNode> children)
  164. {
  165. this.name = name;
  166. this.parent = parent;
  167. this.childNodes = children;
  168. }
  169. public string Name
  170. {
  171. get { return name; }
  172. }
  173. public IHierarchicalEnumerable GetChildren ()
  174. {
  175. return new TestHierarchy (childNodes);
  176. }
  177. public IHierarchyData GetParent ()
  178. {
  179. return parent;
  180. }
  181. public bool HasChildren
  182. {
  183. get
  184. {
  185. if (childNodes == null)
  186. return false;
  187. return childNodes.Count > 0;
  188. }
  189. }
  190. public object Item
  191. {
  192. get { return this; }
  193. }
  194. public string Path
  195. {
  196. get
  197. {
  198. TestNode node = this;
  199. string s = name;
  200. while ((node = (TestNode)node.GetParent ()) != null)
  201. s = node.Name + ": " + s;
  202. return s.Trim ();
  203. }
  204. }
  205. public string Type
  206. {
  207. get { return this.ToString (); }
  208. }
  209. }
  210. [Test]
  211. public void TestIHierarchicalEnumerableDataSource ()
  212. {
  213. List<TestNode> list = new List<TestNode> ();
  214. list.Add (new TestNode ("test", null, null));
  215. TestHierarchy hierarchy = new TestHierarchy (list);
  216. MyHierarchicalDataBoundControl c = new MyHierarchicalDataBoundControl ();
  217. c.DataSource = hierarchy;
  218. c.DataBind ();
  219. HierarchicalDataSourceView view = c.GetData ("");
  220. Assert.IsNotNull (view);
  221. }
  222. }
  223. }
  224. #endif