FormParameterTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.IO;
  32. using System.Collections.Generic;
  33. using System.Text;
  34. using NUnit.Framework;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. using System.Web.UI.HtmlControls;
  39. using MonoTests.SystemWeb.Framework;
  40. using MonoTests.stand_alone.WebHarness;
  41. namespace MonoTests.System.Web.UI.WebControls
  42. {
  43. public class FormParameterPoker : FormParameter
  44. {
  45. public FormParameterPoker (FormParameter param)
  46. : base (param)
  47. {
  48. }
  49. public FormParameterPoker (string name, TypeCode type, string FormField)
  50. : base (name, type, FormField)
  51. {
  52. }
  53. public FormParameterPoker (string name, string FormField)
  54. : base (name, FormField)
  55. {
  56. }
  57. public FormParameterPoker () // constructor
  58. {
  59. TrackViewState ();
  60. }
  61. public object DoEvaluate (HttpContext context, Control control)
  62. {
  63. return base.Evaluate (context, control);
  64. }
  65. public Parameter DoClone ()
  66. {
  67. return base.Clone ();
  68. }
  69. public object SaveState ()
  70. {
  71. return SaveViewState ();
  72. }
  73. public void LoadState (object o)
  74. {
  75. LoadViewState (o);
  76. }
  77. public StateBag StateBag
  78. {
  79. get { return base.ViewState; }
  80. }
  81. }
  82. [TestFixture]
  83. public class FormParameterTest
  84. {
  85. [TestFixtureTearDown]
  86. public void Unload ()
  87. {
  88. WebTest.Unload ();
  89. }
  90. [Test]
  91. public void FormParameter_DefaultProperties ()
  92. {
  93. FormParameterPoker FormParam1 = new FormParameterPoker ();
  94. Assert.AreEqual ("", FormParam1.FormField , "DefaultFormField");
  95. FormParameterPoker FormParam2 = new FormParameterPoker ("FirstName", "TextBox1");
  96. Assert.AreEqual ("FirstName", FormParam2.Name, "OverloadContructorName1");
  97. Assert.AreEqual ("TextBox1", FormParam2.FormField, "OverloadContructorFormField1");
  98. FormParameterPoker FormParam3 = new FormParameterPoker ("Salary", TypeCode.UInt64, "SalaryTextBox");
  99. Assert.AreEqual ("Salary", FormParam3.Name, "OverloadContructorName2");
  100. Assert.AreEqual ("SalaryTextBox", FormParam3.FormField, "OverloadContructorFormField2");
  101. Assert.AreEqual (TypeCode.UInt64, FormParam3.Type, "OverloadContructorType");
  102. FormParameterPoker FormParam4 = new FormParameterPoker (FormParam3);
  103. Assert.AreEqual ("Salary", FormParam4.Name, "OverloadContructorName2");
  104. Assert.AreEqual ("SalaryTextBox", FormParam4.FormField, "OverloadContructorFormName2");
  105. Assert.AreEqual (TypeCode.UInt64, FormParam4.Type, "OverloadContructorType");
  106. }
  107. [Test]
  108. public void FormParameter_AssignToDefaultProperties ()
  109. {
  110. FormParameterPoker FormParam = new FormParameterPoker ();
  111. FormParam.FormField = "FormFieldTest";
  112. Assert.AreEqual ("FormFieldTest", FormParam.FormField, "AssignToFormName");
  113. }
  114. //Protected Methods
  115. [Test]
  116. public void FormParameter_Clone ()
  117. {
  118. FormParameterPoker FormParam = new FormParameterPoker ("EmployeeName", TypeCode.String, "EmployeeLabel");
  119. FormParameter clonedParam = (FormParameter) FormParam.DoClone ();
  120. Assert.AreEqual ("EmployeeName", clonedParam.Name, "FormParameterCloneName");
  121. Assert.AreEqual (TypeCode.String, clonedParam.Type, "FormParameterCloneType");
  122. Assert.AreEqual ("EmployeeLabel", clonedParam.FormField, "FormParameterCloneFormField");
  123. }
  124. [Test]
  125. [Category("NunitWeb")]
  126. public void FormParameter_Evaluate()
  127. {
  128. WebTest t = new WebTest(PageInvoker.CreateOnInit(InitForm));
  129. t.Run();
  130. FormRequest fr = new FormRequest(t.Response, "form1");
  131. fr.Controls.Add("key");
  132. fr.Controls["key"].Value = "Key1";
  133. t.Request = fr;
  134. PageDelegates pd = new PageDelegates();
  135. pd.Load = EvaluateForm;
  136. t.Invoker = new PageInvoker(pd);
  137. t.Run();
  138. }
  139. public static void InitForm(Page p)
  140. {
  141. LiteralControl ltl = new LiteralControl(@"<input id=""key"" type=""text"" value=""Key1""/>");
  142. p.Form.Controls.Add(ltl);
  143. }
  144. public static void EvaluateForm(Page p)
  145. {
  146. FormParameterPoker formParam = new FormParameterPoker();
  147. formParam.FormField = "key";
  148. formParam.Type = TypeCode.String;
  149. formParam.DefaultValue = "Default";
  150. TextBox tb = new TextBox();
  151. p.Form.Controls.Add(tb);
  152. string value = (string)formParam.DoEvaluate(HttpContext.Current, tb);
  153. Assert.AreEqual("Key1", value, "EvaluateSessionParameter");
  154. }
  155. }
  156. }
  157. #endif