PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Web/Test/System.Web.UI.WebControls/TextBoxTest.cs

https://bitbucket.org/danipen/mono
C# | 403 lines | 322 code | 50 blank | 31 comment | 34 complexity | 3ebf1ac610da92b095d6e4c29e148195 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // Tests for System.Web.UI.WebControls.TextBox.cs
  3. //
  4. // Author:
  5. // Ben Maurer (bmaurer@novell.com)
  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. using MonoTests.SystemWeb.Framework;
  37. using MonoTests.stand_alone.WebHarness;
  38. using System.Collections;
  39. namespace MonoTests.System.Web.UI.WebControls {
  40. [TestFixture]
  41. public class TextBoxTest {
  42. class Poker : TextBox {
  43. public new void AddParsedSubObject (object o)
  44. {
  45. base.AddParsedSubObject (o);
  46. }
  47. public void TrackState ()
  48. {
  49. TrackViewState ();
  50. }
  51. public object SaveState ()
  52. {
  53. foreach (string s in ViewState.Keys)
  54. Console.WriteLine ("{0}: {1}", s, ViewState[s]);
  55. return SaveViewState ();
  56. }
  57. public void LoadState (object o)
  58. {
  59. LoadViewState (o);
  60. }
  61. public string Render ()
  62. {
  63. StringWriter sw = new StringWriter ();
  64. sw.NewLine = "\n";
  65. HtmlTextWriter writer = new HtmlTextWriter (sw);
  66. base.Render (writer);
  67. return writer.InnerWriter.ToString ();
  68. }
  69. }
  70. [TestFixtureSetUp]
  71. public void SetUp ()
  72. {
  73. WebTest.CopyResource (GetType (), "TextBoxTestlPage.aspx", "TextBoxTestlPage.aspx");
  74. WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
  75. }
  76. [Test]
  77. public void Defaults ()
  78. {
  79. Poker p = new Poker ();
  80. #if NET_2_0
  81. Assert.AreEqual (string.Empty, p.ValidationGroup, "ValidationGroup");
  82. Assert.AreEqual (false, p.CausesValidation, "CausesValidation");
  83. #endif
  84. }
  85. [Test]
  86. public void Defaults_NotWorking ()
  87. {
  88. Poker p = new Poker ();
  89. #if NET_2_0
  90. Assert.AreEqual (AutoCompleteType.None, p.AutoCompleteType, "AutoCompleteType");
  91. #endif
  92. }
  93. [Test]
  94. public void MultilineRenderEscape ()
  95. {
  96. Poker t = new Poker ();
  97. t.TextMode = TextBoxMode.MultiLine;
  98. t.Text = "</textarea>";
  99. #if NET_4_0
  100. string exp = "<textarea rows=\"2\" cols=\"20\">\r\n&lt;/textarea&gt;</textarea>";
  101. #else
  102. string exp = "<textarea rows=\"2\" cols=\"20\">&lt;/textarea&gt;</textarea>";
  103. #endif
  104. HtmlDiff.AssertAreEqual(exp, t.Render (),"MultilineRenderEscape");
  105. }
  106. #if NET_2_0
  107. [Test]
  108. public void ValidationProperties ()
  109. {
  110. Poker t = new Poker ();
  111. // initial values
  112. Assert.AreEqual (false, t.CausesValidation, "A1");
  113. Assert.AreEqual ("", t.ValidationGroup, "A2");
  114. t.ValidationGroup = "VG";
  115. Assert.AreEqual ("VG", t.ValidationGroup, "A3");
  116. t.CausesValidation = true;
  117. Assert.IsTrue (t.CausesValidation, "A4");
  118. }
  119. [Test]
  120. public void ViewState ()
  121. {
  122. Poker t = new Poker ();
  123. t.TrackState();
  124. t.ValidationGroup = "VG";
  125. t.CausesValidation = true;
  126. object s = t.SaveState ();
  127. Console.WriteLine ("state = {0}", s == null ? "null" : "not-null");
  128. Poker copy = new Poker ();
  129. copy.LoadState (s);
  130. Assert.AreEqual ("VG", copy.ValidationGroup, "A1");
  131. Assert.IsTrue (copy.CausesValidation, "A2");
  132. }
  133. [Test]
  134. public void ValidationRender ()
  135. {
  136. /* test to show that the validation settings
  137. * have no effect on downlevel rendering */
  138. Poker t = new Poker ();
  139. t.TrackState();
  140. t.ValidationGroup = "VG";
  141. t.CausesValidation = true;
  142. t.TextMode = TextBoxMode.MultiLine;
  143. #if NET_4_0
  144. string exp = "<textarea rows=\"2\" cols=\"20\">\r\n</textarea>";
  145. #else
  146. string exp = "<textarea rows=\"2\" cols=\"20\"></textarea>";
  147. #endif
  148. HtmlDiff.AssertAreEqual (exp, t.Render (),"ValidationRender");
  149. }
  150. [Test]
  151. [Category ("NunitWeb")]
  152. public void CausesValidation_ValidationGroup ()
  153. {
  154. WebTest t = new WebTest ("TextBoxTestlPage.aspx");
  155. string str = t.Run ();
  156. FormRequest fr = new FormRequest (t.Response, "form1");
  157. fr.Controls.Add ("__EVENTTARGET");
  158. fr.Controls.Add ("__EVENTARGUMENT");
  159. fr.Controls.Add ("TextBox1");
  160. fr.Controls["__EVENTTARGET"].Value = "TextBox1";
  161. fr.Controls["__EVENTARGUMENT"].Value = "";
  162. fr.Controls["TextBox1"].Value = "TestValue";
  163. t.Request = fr;
  164. string html = t.Run ();
  165. if (html.IndexOf ("Validate_validation_group") == -1)
  166. Assert.Fail ("Validate not created");
  167. if (html.IndexOf ("MyValidationGroup") == -1)
  168. Assert.Fail ("Wrong validation group");
  169. }
  170. #region Help_class
  171. public class PokerL : TextBox
  172. {
  173. public string Render ()
  174. {
  175. StringWriter sw = new StringWriter ();
  176. sw.NewLine = "\n";
  177. HtmlTextWriter writer = new HtmlTextWriter (sw);
  178. base.Render (writer);
  179. return writer.InnerWriter.ToString ();
  180. }
  181. public new void RaisePostDataChangedEvent ()
  182. {
  183. base.RaisePostDataChangedEvent ();
  184. }
  185. protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
  186. {
  187. if (WebTest.CurrentTest.UserData == null) {
  188. ArrayList list = new ArrayList ();
  189. list.Add ("LoadPostData");
  190. WebTest.CurrentTest.UserData = list;
  191. }
  192. else {
  193. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  194. if (list == null)
  195. throw new NullReferenceException ();
  196. list.Add ("LoadPostData");
  197. WebTest.CurrentTest.UserData = list;
  198. }
  199. return base.LoadPostData (postDataKey, postCollection);
  200. }
  201. protected internal override void OnLoad (EventArgs e)
  202. {
  203. if (this.Page.IsPostBack) {
  204. if (WebTest.CurrentTest.UserData == null) {
  205. ArrayList list = new ArrayList ();
  206. list.Add ("ControlLoad");
  207. WebTest.CurrentTest.UserData = list;
  208. }
  209. else {
  210. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  211. if (list == null)
  212. throw new NullReferenceException ();
  213. list.Add ("ControlLoad");
  214. WebTest.CurrentTest.UserData = list;
  215. }
  216. }
  217. base.OnLoad (e);
  218. }
  219. }
  220. #endregion
  221. [Test]
  222. [Category ("NunitWeb")]
  223. public void LoadPostData_Flow () //Just flow and not implementation detail
  224. {
  225. WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
  226. string html = t.Run ();
  227. FormRequest fr = new FormRequest (t.Response, "form1");
  228. fr.Controls.Add ("__EVENTTARGET");
  229. fr.Controls.Add ("__EVENTARGUMENT");
  230. fr.Controls.Add ("pb");
  231. fr.Controls["__EVENTTARGET"].Value = "pb";
  232. fr.Controls["__EVENTARGUMENT"].Value = "";
  233. fr.Controls["pb"].Value = "TestValue";
  234. t.Request = fr;
  235. t.Run ();
  236. ArrayList eventlist = t.UserData as ArrayList;
  237. if (eventlist == null)
  238. Assert.Fail ("User data does not been created fail");
  239. Assert.AreEqual ("PageLoad", eventlist[0], "Live Cycle Flow #1");
  240. Assert.AreEqual ("ControlLoad", eventlist[1], "Live Cycle Flow #2");
  241. Assert.AreEqual ("LoadPostData", eventlist[2], "Live Cycle Flow #3");
  242. }
  243. public static void LoadPostData_Load (Page p)
  244. {
  245. PokerL b = new PokerL ();
  246. b.AutoPostBack = true;
  247. b.ID = "pb";
  248. p.Form.Controls.Add (b);
  249. if (p.IsPostBack) {
  250. if (WebTest.CurrentTest.UserData == null) {
  251. ArrayList list = new ArrayList ();
  252. list.Add ("PageLoad");
  253. WebTest.CurrentTest.UserData = list;
  254. }
  255. else {
  256. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  257. if (list == null)
  258. throw new NullReferenceException ();
  259. list.Add ("PageLoad");
  260. WebTest.CurrentTest.UserData = list;
  261. }
  262. }
  263. }
  264. [Test]
  265. [Category ("NunitWeb")]
  266. public void LoadPostData ()
  267. {
  268. WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData__Load));
  269. string html = t.Run ();
  270. FormRequest fr = new FormRequest (t.Response, "form1");
  271. fr.Controls.Add ("__EVENTTARGET");
  272. fr.Controls.Add ("__EVENTARGUMENT");
  273. fr.Controls.Add ("pb");
  274. fr.Controls["__EVENTTARGET"].Value = "pb";
  275. fr.Controls["__EVENTARGUMENT"].Value = "";
  276. fr.Controls["pb"].Value = "TestValue";
  277. t.Request = fr;
  278. html = t.Run ();
  279. ArrayList eventlist = t.UserData as ArrayList;
  280. if (eventlist == null)
  281. Assert.Fail ("User data does not been created fail");
  282. Assert.AreEqual ("ControlLoad", eventlist[0], "Live Cycle Flow #1");
  283. Assert.AreEqual ("LoadPostData", eventlist[1], "Live Cycle Flow #2");
  284. Assert.AreEqual ("TextChanged", eventlist[2], "Live Cycle Flow #3");
  285. if (html.IndexOf ("TestValue") == -1)
  286. Assert.Fail ("Wrong value failed");
  287. }
  288. public static void LoadPostData__Load (Page p)
  289. {
  290. PokerL b = new PokerL ();
  291. b.ID = "pb";
  292. p.Form.Controls.Add (b);
  293. b.TextChanged += new EventHandler (b_TextChanged);
  294. if (p.IsPostBack)
  295. p.Response.Write (b.Text);
  296. }
  297. public static void b_TextChanged (object sender, EventArgs e)
  298. {
  299. if (WebTest.CurrentTest.UserData == null) {
  300. ArrayList list = new ArrayList ();
  301. list.Add ("TextChanged");
  302. WebTest.CurrentTest.UserData = list;
  303. }
  304. else {
  305. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  306. if (list == null)
  307. throw new NullReferenceException ();
  308. list.Add ("TextChanged");
  309. WebTest.CurrentTest.UserData = list;
  310. }
  311. }
  312. [Test]
  313. public void RaisePostDataChangedEvent ()
  314. {
  315. PokerL p = new PokerL ();
  316. p.TextChanged += new EventHandler (p_TextChanged);
  317. Assert.AreEqual (false, eventTextChanged, "RaisePostDataChangedEvent#1");
  318. p.RaisePostDataChangedEvent ();
  319. Assert.AreEqual (true, eventTextChanged, "RaisePostDataChangedEvent#2");
  320. }
  321. bool eventTextChanged;
  322. void p_TextChanged(object sender, EventArgs e)
  323. {
  324. eventTextChanged = true;
  325. }
  326. [Test]
  327. public void AutoCompleteType_Test ()
  328. {
  329. WebTest t = new WebTest ("NoEventValidation.aspx");
  330. t = new WebTest (PageInvoker.CreateOnLoad (AutoCompleteType__Load));
  331. string html = t.Run ();
  332. string orig ="<input name=\"Poker\" type=\"text\" vcard_name=\"vCard.FirstName\" id=\"Poker\" />";
  333. HtmlDiff.AssertAreEqual (orig, HtmlDiff.GetControlFromPageHtml (html), "AutoCompleteType");
  334. }
  335. public static void AutoCompleteType__Load (Page page)
  336. {
  337. LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
  338. LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
  339. PokerL p = new PokerL ();
  340. p.ID = "Poker";
  341. p.AutoCompleteType = AutoCompleteType.FirstName;
  342. page.Form.Controls.Add (lcb);
  343. page.Form.Controls.Add (p);
  344. page.Form.Controls.Add (lce);
  345. }
  346. [TestFixtureTearDown]
  347. public void TearDown ()
  348. {
  349. WebTest.Unload ();
  350. }
  351. #endif
  352. }
  353. }