ControlParameterTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Tests for System.Web.UI.WebControls.FormView.cs
  3. //
  4. // Author:
  5. // Merav Sudri ([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 System;
  31. using System.Collections.Generic;
  32. using System.Collections.Specialized;
  33. using System.Text;
  34. using NUnit.Framework;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. namespace MonoTests.System.Web.UI.WebControls
  39. {
  40. public class ControlParameterPoker : ControlParameter
  41. {
  42. public ControlParameterPoker (ControlParameter control)
  43. : base (control)
  44. {
  45. }
  46. public ControlParameterPoker (string name,TypeCode type, string controlID,string propertyName)
  47. :base(name,type,controlID,propertyName)
  48. {
  49. }
  50. public ControlParameterPoker (string name, string controlId, string propertyName)
  51. : base (name, controlId, propertyName)
  52. {
  53. }
  54. public ControlParameterPoker (string name, string controlId)
  55. : base (name, controlId)
  56. {
  57. }
  58. public ControlParameterPoker() // constructor
  59. {
  60. TrackViewState ();
  61. }
  62. public object DoEvaluate (HttpContext context,Control control)
  63. {
  64. return base.Evaluate (context,control);
  65. }
  66. public Parameter DoClone ()
  67. {
  68. return base.Clone ();
  69. }
  70. public object SaveState ()
  71. {
  72. return SaveViewState ();
  73. }
  74. public void LoadState (object o)
  75. {
  76. LoadViewState (o);
  77. }
  78. public StateBag StateBag
  79. {
  80. get { return base.ViewState; }
  81. }
  82. }
  83. class TestControl : Control
  84. {
  85. DataKey _values;
  86. public DataKey Values {
  87. get { return _values; }
  88. }
  89. public TestControl (DataKey values)
  90. {
  91. this._values = values;
  92. }
  93. }
  94. [TestFixture]
  95. public class ControlParameterTest
  96. {
  97. [Test]
  98. public void ControlParameter_DefaultProperties ()
  99. {
  100. ControlParameterPoker ctrlParam1 = new ControlParameterPoker ();
  101. Assert.AreEqual ("",ctrlParam1.ControlID, "ControlIdDefault");
  102. Assert.AreEqual ("", ctrlParam1.PropertyName, "PropertyNameDefault");
  103. ControlParameterPoker ctrlParam2 = new ControlParameterPoker ("ControlTest","ControlID");
  104. Assert.AreEqual ("ControlID", ctrlParam2.ControlID, "OverloadConstructorControlId1");
  105. Assert.AreEqual ("ControlTest", ctrlParam2.Name, "OverloadConstructorPropertyName1");
  106. ControlParameterPoker ctrlParam3 = new ControlParameterPoker ("Age",TypeCode.Int32,"Label1","Text");
  107. Assert.AreEqual ("Age", ctrlParam3.Name, "OverloadConstructorName2");
  108. Assert.AreEqual (TypeCode.Int32, ctrlParam3.Type, "OverloadConstructorType2");
  109. Assert.AreEqual ("Label1", ctrlParam3.ControlID, "OverloadConstructorControlID2");
  110. Assert.AreEqual ("Text", ctrlParam3.PropertyName, "OverloadConstructorPropertyName2");
  111. ControlParameterPoker ctrlParam4 = new ControlParameterPoker ("Age","Label1","Text");
  112. Assert.AreEqual ("Age", ctrlParam4.Name, "OverloadConstructorName3");
  113. Assert.AreEqual ("Label1", ctrlParam4.ControlID, "OverloadConstructorControlID3");
  114. Assert.AreEqual ("Text", ctrlParam4.PropertyName, "OverloadConstructorPropertyName3");
  115. ControlParameterPoker ctrlParam5 = new ControlParameterPoker (ctrlParam3);
  116. Assert.AreEqual ("Age", ctrlParam3.Name, "OverloadConstructorName4");
  117. Assert.AreEqual (TypeCode.Int32, ctrlParam3.Type, "OverloadConstructorType4");
  118. Assert.AreEqual ("Label1", ctrlParam3.ControlID, "OverloadConstructorControlID4");
  119. Assert.AreEqual ("Text", ctrlParam3.PropertyName, "OverloadConstructorPropertyName4");
  120. }
  121. [Test]
  122. public void ControlParameter_AssignToDefaultProperties ()
  123. {
  124. ControlParameterPoker ctrlParam = new ControlParameterPoker ();
  125. ctrlParam.PropertyName = "Text";
  126. Assert.AreEqual ("Text",ctrlParam.PropertyName ,"AssignToPropertyName");
  127. ctrlParam.ControlID ="Label";
  128. Assert.AreEqual ("Label",ctrlParam.ControlID ,"AssignToPropertyName");
  129. }
  130. //Protected Methods
  131. [Test]
  132. public void ControlParameter_Clone ()
  133. {
  134. ControlParameterPoker ctrlParam = new ControlParameterPoker ("Salary", TypeCode.Int64, "TextBox1", "Text");
  135. ControlParameter clonedParam = (ControlParameter)ctrlParam.DoClone ();
  136. Assert.AreEqual ("Salary", clonedParam.Name, "ClonedParamName");
  137. Assert.AreEqual (TypeCode.Int64, clonedParam.Type, "ClonedParamType");
  138. Assert.AreEqual ("TextBox1", clonedParam.ControlID, "ClonedParamControlID");
  139. Assert.AreEqual ("Text", clonedParam.PropertyName, "ClonedParamPropertyName");
  140. }
  141. [Test]
  142. public void ControlParameter_Evaluate ()
  143. {
  144. ControlParameterPoker ctrlParam = new ControlParameterPoker ("Salary",TypeCode.Int64,"Label1","Text");
  145. Page page = new Page ();
  146. Label label1 = new Label ();
  147. label1.ID = "Label1";
  148. label1.Text = "2000";
  149. page.Controls.Add (label1);
  150. string value=(string)ctrlParam.DoEvaluate (HttpContext.Current,label1);
  151. Assert.AreEqual ("2000", value, "EvaluateValue1");
  152. label1.Text = "TestNewValue";
  153. ctrlParam.Type = TypeCode.String;
  154. value = (string) ctrlParam.DoEvaluate (HttpContext.Current, label1);
  155. Assert.AreEqual ("TestNewValue", value, "EvaluateValue2");
  156. }
  157. [Test]
  158. public void ControlParameter_EvaluateComplex ()
  159. {
  160. ControlParameterPoker ctrlParam = new ControlParameterPoker ("Test", "TestControl1", "Values['one']");
  161. Page page = new Page ();
  162. OrderedDictionary dict = new OrderedDictionary ();
  163. dict.Add ("one", "1");
  164. DataKey values = new DataKey (dict);
  165. TestControl test = new TestControl (values);
  166. test.ID = "TestControl1";
  167. page.Controls.Add (test);
  168. string value = ctrlParam.DoEvaluate (HttpContext.Current, test) as string;
  169. Assert.AreEqual ("1", value, "#1");
  170. }
  171. [Test]
  172. [ExpectedException (typeof (ArgumentException))]
  173. public void EvaluateArgumemtException ()
  174. {
  175. ControlParameterPoker ctrlParam = new ControlParameterPoker ();
  176. TextBox textBox1 = new TextBox ();
  177. textBox1.ID = "textbox1";
  178. Page page = new Page ();
  179. page.Controls.Add (textBox1);
  180. ctrlParam.DoEvaluate (HttpContext.Current, textBox1);
  181. }
  182. [Test]
  183. [ExpectedException (typeof (InvalidOperationException))]
  184. public void EvaluateInvalidOperationException ()
  185. {
  186. ControlParameterPoker ctrlParam = new ControlParameterPoker ("age", "Button", "parameter");
  187. Button b = new Button ();
  188. Page page = new Page ();
  189. page.Controls.Add (b);
  190. ctrlParam.DoEvaluate (HttpContext.Current, b);
  191. }
  192. }
  193. }
  194. #endif