BaseDataBoundControlTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Tests for System.Web.UI.WebControls.BaseDataBoundControl.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. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Globalization;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36. namespace MonoTests.System.Web.UI.WebControls
  37. {
  38. [TestFixture]
  39. public class BaseDataBoundControlTest {
  40. class Poker : BaseDataBoundControl {
  41. public bool OnDataPropertyChangedCalled;
  42. public bool ValidateDataSourceCalled;
  43. public Poker () {
  44. TrackViewState ();
  45. }
  46. public object SaveState () {
  47. return SaveViewState ();
  48. }
  49. public void LoadState (object state) {
  50. LoadViewState (state);
  51. }
  52. protected override void PerformSelect ()
  53. {
  54. Assert.IsTrue (RequiresDataBinding);
  55. //Console.WriteLine ("PerformSelect\n{0}", Environment.StackTrace);
  56. }
  57. protected override void ValidateDataSource (object dataSource)
  58. {
  59. ValidateDataSourceCalled = true;
  60. //Console.WriteLine ("PerformSelect\n{0}", Environment.StackTrace);
  61. }
  62. public bool GetIsBoundUsingDataSourceID ()
  63. {
  64. return IsBoundUsingDataSourceID;
  65. }
  66. public bool GetInitialized ()
  67. {
  68. return Initialized;
  69. }
  70. protected override void OnDataPropertyChanged () {
  71. base.OnDataPropertyChanged ();
  72. OnDataPropertyChangedCalled = true;
  73. }
  74. public void SetRequiresDataBinding (bool val)
  75. {
  76. RequiresDataBinding = val;
  77. }
  78. public bool GetRequiresDataBinding ()
  79. {
  80. return RequiresDataBinding;
  81. }
  82. public override void DataBind ()
  83. {
  84. Assert.IsTrue (RequiresDataBinding);
  85. base.DataBind ();
  86. Assert.IsTrue (RequiresDataBinding);
  87. }
  88. public void DoEnsureDataBound ()
  89. {
  90. Assert.IsTrue (RequiresDataBinding);
  91. EnsureDataBound ();
  92. Assert.IsTrue (RequiresDataBinding);
  93. }
  94. }
  95. [Test]
  96. public void Defaults ()
  97. {
  98. Poker p = new Poker ();
  99. Assert.IsNull (p.DataSource, "A1");
  100. Assert.AreEqual ("", p.DataSourceID, "A2");
  101. Assert.IsFalse (p.GetIsBoundUsingDataSourceID(), "A3");
  102. Assert.IsFalse (p.GetInitialized(), "A4");
  103. }
  104. [Test]
  105. public void ViewState ()
  106. {
  107. Poker p = new Poker ();
  108. Poker copy = new Poker ();
  109. p.DataSourceID = "hi";
  110. object state = p.SaveState ();
  111. copy.LoadState (state);
  112. Assert.AreEqual ("hi", copy.DataSourceID, "A1");
  113. }
  114. [Test]
  115. public void OnDataPropertyChanged ()
  116. {
  117. Poker p = new Poker ();
  118. Assert.IsFalse (p.OnDataPropertyChangedCalled);
  119. p.DataSourceID = "hi";
  120. Assert.IsTrue (p.OnDataPropertyChangedCalled, "OnDataPropertyChanged: DataSourceID");
  121. }
  122. [Test]
  123. public void OnDataPropertyChanged2 ()
  124. {
  125. Poker p = new Poker ();
  126. Assert.IsFalse (p.OnDataPropertyChangedCalled);
  127. p.DataSource = null;
  128. Assert.IsTrue (p.OnDataPropertyChangedCalled, "OnDataPropertyChanged: DataSource");
  129. }
  130. [Test]
  131. public void DataBind ()
  132. {
  133. Poker p = new Poker ();
  134. p.DataSourceID = "DataSourceID";
  135. p.SetRequiresDataBinding (true);
  136. p.DataBind ();
  137. }
  138. [Test]
  139. public void EnsureDataBound ()
  140. {
  141. Poker p = new Poker ();
  142. p.DataSourceID = "DataSourceID";
  143. p.SetRequiresDataBinding (true);
  144. p.DoEnsureDataBound ();
  145. }
  146. [Test]
  147. public void DataSource_ValidateDataSource ()
  148. {
  149. Poker p = new Poker ();
  150. p.DataSource = null;
  151. Assert.AreEqual (false, p.ValidateDataSourceCalled);
  152. p.DataSource = new Object();
  153. Assert.AreEqual (true, p.ValidateDataSourceCalled);
  154. }
  155. [Test]
  156. public void SupportsDisabledAttribute ()
  157. {
  158. var ver40 = new Version (4, 0);
  159. var ver35 = new Version (3, 5);
  160. var p = new Poker ();
  161. Assert.AreEqual (ver40, p.RenderingCompatibility, "#A1-1");
  162. Assert.IsFalse (p.SupportsDisabledAttribute, "#A1-2");
  163. p.RenderingCompatibility = new Version (3, 5);
  164. Assert.AreEqual (ver35, p.RenderingCompatibility, "#A2-1");
  165. Assert.IsTrue (p.SupportsDisabledAttribute, "#A2-2");
  166. }
  167. }
  168. }