DataBoundControlTest.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Tests for System.Web.UI.WebControls.DataBoundControl.cs
  3. //
  4. // Author:
  5. // Chris Toshok ([email protected])
  6. //
  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. #if NET_2_0
  30. using NUnit.Framework;
  31. using System;
  32. using System.IO;
  33. using System.Globalization;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. using System.Text;
  38. using System.Collections;
  39. using System.Data;
  40. using MonoTests.SystemWeb.Framework;
  41. namespace MonoTests.System.Web.UI.WebControls
  42. {
  43. [TestFixture]
  44. public class DataBoundControlTest {
  45. class Poker : DataBoundControl {
  46. protected override void PerformSelect ()
  47. {
  48. Console.WriteLine ("PerformSelect\n{0}", Environment.StackTrace);
  49. }
  50. public void DoValidateDataSource (object dataSource)
  51. {
  52. ValidateDataSource (dataSource);
  53. }
  54. public bool GetInitialized ()
  55. {
  56. return Initialized;
  57. }
  58. }
  59. class MyDataBoundControl : DataBoundControl
  60. {
  61. public int CreateDataSourceSelectArgumentsCalled;
  62. public DataSourceSelectArguments CreatedDataSourceSelectArguments;
  63. private StringBuilder dataBindTrace = new StringBuilder ();
  64. public string DataBindTrace {
  65. get { return dataBindTrace.ToString (); }
  66. }
  67. public override void DataBind () {
  68. dataBindTrace = new StringBuilder ();
  69. dataBindTrace.Append ("[Start DataBind]");
  70. base.DataBind ();
  71. dataBindTrace.Append ("[End DataBind]");
  72. }
  73. protected override void PerformSelect () {
  74. dataBindTrace.Append ("[Start PerformSelect]");
  75. base.PerformSelect ();
  76. dataBindTrace.Append ("[End PerformSelect]");
  77. }
  78. protected override void PerformDataBinding (IEnumerable data) {
  79. dataBindTrace.Append ("[Start PerformDataBinding]");
  80. base.PerformDataBinding (data);
  81. dataBindTrace.Append ("[End PerformDataBinding]");
  82. }
  83. protected override void OnDataBinding (EventArgs e) {
  84. dataBindTrace.Append ("[Start OnDataBinding]");
  85. base.OnDataBinding (e);
  86. dataBindTrace.Append ("[End OnDataBinding]");
  87. }
  88. protected override void OnDataBound (EventArgs e) {
  89. dataBindTrace.Append ("[Start OnDataBound]");
  90. base.OnDataBound (e);
  91. dataBindTrace.Append ("[End OnDataBound]");
  92. }
  93. protected override DataSourceView GetData () {
  94. dataBindTrace.Append ("[Start GetData]");
  95. DataSourceView d = base.GetData ();
  96. dataBindTrace.Append ("[End GetData]");
  97. return d;
  98. }
  99. public DataSourceView DoGetData () {
  100. return GetData ();
  101. }
  102. public IDataSource DoGetDataSource () {
  103. return GetDataSource ();
  104. }
  105. public void DoEnsureDataBound () {
  106. EnsureDataBound();
  107. }
  108. protected override DataSourceSelectArguments CreateDataSourceSelectArguments () {
  109. CreateDataSourceSelectArgumentsCalled++;
  110. CreatedDataSourceSelectArguments = base.CreateDataSourceSelectArguments ();
  111. return CreatedDataSourceSelectArguments;
  112. }
  113. public DataSourceSelectArguments GetSelectArguments () {
  114. return SelectArguments;
  115. }
  116. }
  117. [TestFixtureTearDown]
  118. public void Unload ()
  119. {
  120. WebTest.Unload ();
  121. }
  122. [Test]
  123. public void DataBoundControl_GetData () {
  124. Page p = new Page ();
  125. MyDataBoundControl dc = new MyDataBoundControl ();
  126. p.Controls.Add (dc);
  127. DataSourceView data = dc.DoGetData ();
  128. Assert.IsNotNull (data, "GetData");
  129. IDataSource dataSource = dc.DoGetDataSource ();
  130. Assert.IsNotNull (dataSource, "GetDataSource");
  131. }
  132. [Test]
  133. public void DataBoundControl_DataBindFlow () {
  134. Page p = new Page ();
  135. MyDataBoundControl dc = new MyDataBoundControl ();
  136. p.Controls.Add (dc);
  137. dc.DataBind ();
  138. string expected = "[Start DataBind][Start PerformSelect][Start OnDataBinding][End OnDataBinding][Start GetData][End GetData][Start PerformDataBinding][End PerformDataBinding][Start OnDataBound][End OnDataBound][End PerformSelect][End DataBind]";
  139. Assert.AreEqual (expected, dc.DataBindTrace, "DataBindFlow");
  140. }
  141. [Test]
  142. [Category ("NunitWeb")]
  143. public void DataBoundControl_DataBindFlow2 () {
  144. new WebTest (PageInvoker.CreateOnLoad (DataBoundControl_DataBindFlow2_Load)).Run ();
  145. }
  146. public static void DataBoundControl_DataBindFlow2_Load(Page p){
  147. MyDataBoundControl dc = new MyDataBoundControl ();
  148. p.Controls.Add (dc);
  149. dc.DataSourceID = "ObjectDataSource1";
  150. ObjectDataSource ods = new ObjectDataSource (typeof(Control).FullName, "ToString");
  151. ods.ID = "ObjectDataSource1";
  152. p.Controls.Add (ods);
  153. dc.DataBind ();
  154. string expected = "[Start DataBind][Start PerformSelect][Start GetData][End GetData][Start OnDataBinding][End OnDataBinding][Start PerformDataBinding][End PerformDataBinding][Start OnDataBound][End OnDataBound][End PerformSelect][End DataBind]";
  155. Assert.AreEqual (expected, dc.DataBindTrace, "DataBindFlow");
  156. }
  157. [Test]
  158. public void DataBoundControl_DataBindFlow3 () {
  159. Page p = new Page ();
  160. MyDataBoundControl dc = new MyDataBoundControl ();
  161. p.Controls.Add (dc);
  162. DataSourceSelectArguments arg1 = dc.GetSelectArguments ();
  163. Assert.AreEqual (1, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#1");
  164. dc.DataBind ();
  165. DataSourceSelectArguments argCreated2 = dc.CreatedDataSourceSelectArguments;
  166. DataSourceSelectArguments arg2 = dc.GetSelectArguments ();
  167. Assert.AreEqual (2, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#2");
  168. dc.DataBind ();
  169. DataSourceSelectArguments argCreated3 = dc.CreatedDataSourceSelectArguments;
  170. Assert.AreEqual (3, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#3");
  171. Assert.IsTrue (object.ReferenceEquals (argCreated2, arg2), "CreateDataSourceSelectArgumentsCalled#4");
  172. }
  173. [Test]
  174. public void Defaults ()
  175. {
  176. Poker p = new Poker ();
  177. Assert.AreEqual ("", p.DataMember, "A1");
  178. Assert.AreEqual ("", p.DataSourceID, "A2");
  179. }
  180. [Test]
  181. public void ValidateDataSource () {
  182. Poker p = new Poker ();
  183. // Allows null
  184. p.DoValidateDataSource (null);
  185. }
  186. // MSDN: The ConfirmInitState method sets the initialized state of the data-bound
  187. // control. The method is called by the DataBoundControl class in its OnLoad method.
  188. [Test]
  189. [Category ("NunitWeb")]
  190. public void Initialized ()
  191. {
  192. WebTest t = new WebTest ();
  193. PageDelegates pd = new PageDelegates ();
  194. pd.Load = Initialized_Load;
  195. pd.PreRenderComplete = Initialized_PreRender;
  196. t.Invoker = new PageInvoker (pd);
  197. t.Run ();
  198. }
  199. public static void Initialized_Load (Page p)
  200. {
  201. Poker c = new Poker ();
  202. p.Form.Controls.Clear ();
  203. p.Form.Controls.Add (c);
  204. Assert.IsFalse (c.GetInitialized (), "Initialized_Load");
  205. }
  206. public static void Initialized_PreRender (Page p)
  207. {
  208. Poker c = (Poker) p.Form.Controls [0];
  209. Assert.IsTrue (c.GetInitialized (), "Initialized_PreRender");
  210. }
  211. }
  212. }
  213. #endif