CompositeDataBoundControlTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Tests for System.Web.UI.WebControls.CompositeDataBoundControl.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. //
  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.Collections;
  38. namespace MonoTests.System.Web.UI.WebControls
  39. {
  40. [TestFixture]
  41. public class CompositeDataBoundControlTest
  42. {
  43. class MyCompositeDataBoundControl : CompositeDataBoundControl
  44. {
  45. public bool ensureDataBound = false;
  46. public bool ensureCreateChildControls = false;
  47. public bool createChildControls1 = false;
  48. public bool createChildControls2 = false;
  49. public bool dataBind = false;
  50. public bool CreateChildControls_ChildControlsCreated;
  51. public int CreateChildControls_Controls_Count;
  52. protected override int CreateChildControls (IEnumerable dataSource, bool dataBinding) {
  53. createChildControls2 = true;
  54. CreateChildControls_ChildControlsCreated = ChildControlsCreated;
  55. CreateChildControls_Controls_Count = Controls.Count;
  56. return 10;
  57. }
  58. public void DoEnsureChildControls () {
  59. EnsureChildControls ();
  60. }
  61. public void DoCreateChildControls () {
  62. CreateChildControls ();
  63. }
  64. public override void DataBind () {
  65. base.DataBind ();
  66. dataBind = true;
  67. }
  68. protected override void EnsureDataBound () {
  69. base.EnsureDataBound ();
  70. ensureDataBound = true;
  71. }
  72. protected override void EnsureChildControls () {
  73. base.EnsureChildControls ();
  74. ensureCreateChildControls = true;
  75. }
  76. protected override void CreateChildControls () {
  77. base.CreateChildControls ();
  78. createChildControls1 = true;
  79. }
  80. public bool GetRequiresDataBinding () {
  81. return RequiresDataBinding;
  82. }
  83. public void SetRequiresDataBinding (bool value) {
  84. RequiresDataBinding = value;
  85. }
  86. public bool GetChildControlsCreated () {
  87. return ChildControlsCreated;
  88. }
  89. }
  90. [Test]
  91. public void DetailsView_EnsureChildControlsFlow () {
  92. MyCompositeDataBoundControl c = new MyCompositeDataBoundControl ();
  93. Assert.IsFalse (c.GetRequiresDataBinding());
  94. c.DoEnsureChildControls ();
  95. Assert.IsTrue (c.ensureCreateChildControls);
  96. Assert.IsFalse (c.ensureDataBound);
  97. Assert.IsFalse (c.dataBind);
  98. Assert.IsTrue (c.createChildControls1);
  99. Assert.IsFalse (c.createChildControls2);
  100. }
  101. [Test]
  102. public void DetailsView_EnsureChildControlsFlow2 () {
  103. MyCompositeDataBoundControl c = new MyCompositeDataBoundControl ();
  104. c.SetRequiresDataBinding (true);
  105. Assert.IsTrue (c.GetRequiresDataBinding ());
  106. c.DoEnsureChildControls ();
  107. Assert.IsTrue (c.ensureCreateChildControls);
  108. Assert.IsTrue (c.ensureDataBound);
  109. Assert.IsFalse (c.dataBind);
  110. Assert.IsTrue (c.createChildControls1);
  111. Assert.IsFalse (c.createChildControls2);
  112. }
  113. [Test]
  114. public void DetailsView_CreateChildControls_Clear () {
  115. MyCompositeDataBoundControl c = new MyCompositeDataBoundControl ();
  116. c.Controls.Add (new WebControl (HtmlTextWriterTag.A));
  117. Assert.AreEqual (1, c.Controls.Count);
  118. c.DoCreateChildControls ();
  119. Assert.AreEqual (0, c.Controls.Count);
  120. }
  121. [Test]
  122. public void DetailsView_CreateChildControls_Clear2 () {
  123. MyCompositeDataBoundControl c = new MyCompositeDataBoundControl ();
  124. c.Controls.Add (new WebControl (HtmlTextWriterTag.A));
  125. Assert.AreEqual (1, c.Controls.Count, "Controls.Count before DataBind");
  126. c.DataBind ();
  127. Assert.AreEqual (0, c.CreateChildControls_Controls_Count, "Controls.Count in CreateChildControls");
  128. Assert.AreEqual (0, c.Controls.Count, "Controls.Count after DataBind");
  129. }
  130. [Test]
  131. public void DataBind_ChildControlsCreated () {
  132. Page p = new Page ();
  133. MyCompositeDataBoundControl c = new MyCompositeDataBoundControl ();
  134. p.Controls.Add (c);
  135. Assert.IsFalse (c.GetChildControlsCreated (), "ChildControlsCreated before DataBind");
  136. c.DataBind ();
  137. Assert.IsTrue (c.ensureCreateChildControls);
  138. Assert.IsTrue (c.createChildControls1);
  139. Assert.IsTrue (c.createChildControls2);
  140. Assert.IsTrue (c.CreateChildControls_ChildControlsCreated, "ChildControlsCreated in CreateChildControls");
  141. Assert.IsTrue (c.GetChildControlsCreated (), "ChildControlsCreated after DataBind");
  142. }
  143. }
  144. }
  145. #endif