2 // Tests for System.Web.UI.WebControls.TextBox.cs
5 // Ben Maurer (bmaurer@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
34 using System.Globalization;
37 using System.Web.UI.WebControls;
38 using MonoTests.SystemWeb.Framework;
39 using MonoTests.stand_alone.WebHarness;
40 using System.Collections;
42 namespace MonoTests.System.Web.UI.WebControls {
45 public class TextBoxTest {
46 class Poker : TextBox {
47 public new void AddParsedSubObject (object o)
49 base.AddParsedSubObject (o);
52 public void TrackState ()
57 public object SaveState ()
59 foreach (string s in ViewState.Keys)
60 Console.WriteLine ("{0}: {1}", s, ViewState[s]);
62 return SaveViewState ();
65 public void LoadState (object o)
70 public string Render ()
72 StringWriter sw = new StringWriter ();
74 HtmlTextWriter writer = new HtmlTextWriter (sw);
76 return writer.InnerWriter.ToString ();
83 WebTest.CopyResource (GetType (), "TextBoxTestlPage.aspx", "TextBoxTestlPage.aspx");
84 WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
88 public void Defaults ()
90 Poker p = new Poker ();
91 Assert.AreEqual (string.Empty, p.ValidationGroup, "ValidationGroup");
92 Assert.AreEqual (false, p.CausesValidation, "CausesValidation");
96 public void Defaults_NotWorking ()
98 Poker p = new Poker ();
99 Assert.AreEqual (AutoCompleteType.None, p.AutoCompleteType, "AutoCompleteType");
103 public void MultilineRenderEscape ()
105 Poker t = new Poker ();
106 t.TextMode = TextBoxMode.MultiLine;
107 t.Text = "</textarea>";
109 string exp = "<textarea rows=\"2\" cols=\"20\">\r\n</textarea></textarea>";
111 string exp = "<textarea rows=\"2\" cols=\"20\"></textarea></textarea>";
114 HtmlDiff.AssertAreEqual(exp, t.Render (),"MultilineRenderEscape");
119 public void ValidationProperties ()
121 Poker t = new Poker ();
124 Assert.AreEqual (false, t.CausesValidation, "A1");
125 Assert.AreEqual ("", t.ValidationGroup, "A2");
127 t.ValidationGroup = "VG";
128 Assert.AreEqual ("VG", t.ValidationGroup, "A3");
130 t.CausesValidation = true;
131 Assert.IsTrue (t.CausesValidation, "A4");
135 public void ViewState ()
137 Poker t = new Poker ();
141 t.ValidationGroup = "VG";
142 t.CausesValidation = true;
144 object s = t.SaveState ();
145 Console.WriteLine ("state = {0}", s == null ? "null" : "not-null");
147 Poker copy = new Poker ();
151 Assert.AreEqual ("VG", copy.ValidationGroup, "A1");
152 Assert.IsTrue (copy.CausesValidation, "A2");
156 public void ValidationRender ()
158 /* test to show that the validation settings
159 * have no effect on downlevel rendering */
160 Poker t = new Poker ();
164 t.ValidationGroup = "VG";
165 t.CausesValidation = true;
166 t.TextMode = TextBoxMode.MultiLine;
168 string exp = "<textarea rows=\"2\" cols=\"20\">\r\n</textarea>";
170 string exp = "<textarea rows=\"2\" cols=\"20\"></textarea>";
172 HtmlDiff.AssertAreEqual (exp, t.Render (),"ValidationRender");
176 [Category ("NunitWeb")]
177 public void CausesValidation_ValidationGroup ()
179 WebTest t = new WebTest ("TextBoxTestlPage.aspx");
180 string str = t.Run ();
181 FormRequest fr = new FormRequest (t.Response, "form1");
182 fr.Controls.Add ("__EVENTTARGET");
183 fr.Controls.Add ("__EVENTARGUMENT");
184 fr.Controls.Add ("TextBox1");
185 fr.Controls["__EVENTTARGET"].Value = "TextBox1";
186 fr.Controls["__EVENTARGUMENT"].Value = "";
187 fr.Controls["TextBox1"].Value = "TestValue";
189 string html = t.Run ();
191 if (html.IndexOf ("Validate_validation_group") == -1)
192 Assert.Fail ("Validate not created");
193 if (html.IndexOf ("MyValidationGroup") == -1)
194 Assert.Fail ("Wrong validation group");
198 public class PokerL : TextBox
200 public string Render ()
202 StringWriter sw = new StringWriter ();
204 HtmlTextWriter writer = new HtmlTextWriter (sw);
205 base.Render (writer);
206 return writer.InnerWriter.ToString ();
209 public new void RaisePostDataChangedEvent ()
211 base.RaisePostDataChangedEvent ();
214 protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
216 if (WebTest.CurrentTest.UserData == null) {
217 ArrayList list = new ArrayList ();
218 list.Add ("LoadPostData");
219 WebTest.CurrentTest.UserData = list;
222 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
224 throw new NullReferenceException ();
225 list.Add ("LoadPostData");
226 WebTest.CurrentTest.UserData = list;
228 return base.LoadPostData (postDataKey, postCollection);
231 protected internal override void OnLoad (EventArgs e)
233 if (this.Page.IsPostBack) {
234 if (WebTest.CurrentTest.UserData == null) {
235 ArrayList list = new ArrayList ();
236 list.Add ("ControlLoad");
237 WebTest.CurrentTest.UserData = list;
240 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
242 throw new NullReferenceException ();
243 list.Add ("ControlLoad");
244 WebTest.CurrentTest.UserData = list;
253 [Category ("NunitWeb")]
254 public void LoadPostData_Flow () //Just flow and not implementation detail
256 WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
257 string html = t.Run ();
258 FormRequest fr = new FormRequest (t.Response, "form1");
259 fr.Controls.Add ("__EVENTTARGET");
260 fr.Controls.Add ("__EVENTARGUMENT");
261 fr.Controls.Add ("pb");
262 fr.Controls["__EVENTTARGET"].Value = "pb";
263 fr.Controls["__EVENTARGUMENT"].Value = "";
264 fr.Controls["pb"].Value = "TestValue";
268 ArrayList eventlist = t.UserData as ArrayList;
269 if (eventlist == null)
270 Assert.Fail ("User data does not been created fail");
271 Assert.AreEqual ("PageLoad", eventlist[0], "Live Cycle Flow #1");
272 Assert.AreEqual ("ControlLoad", eventlist[1], "Live Cycle Flow #2");
273 Assert.AreEqual ("LoadPostData", eventlist[2], "Live Cycle Flow #3");
276 public static void LoadPostData_Load (Page p)
278 PokerL b = new PokerL ();
279 b.AutoPostBack = true;
281 p.Form.Controls.Add (b);
283 if (WebTest.CurrentTest.UserData == null) {
284 ArrayList list = new ArrayList ();
285 list.Add ("PageLoad");
286 WebTest.CurrentTest.UserData = list;
289 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
291 throw new NullReferenceException ();
292 list.Add ("PageLoad");
293 WebTest.CurrentTest.UserData = list;
299 [Category ("NunitWeb")]
300 public void LoadPostData ()
302 WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData__Load));
303 string html = t.Run ();
304 FormRequest fr = new FormRequest (t.Response, "form1");
305 fr.Controls.Add ("__EVENTTARGET");
306 fr.Controls.Add ("__EVENTARGUMENT");
307 fr.Controls.Add ("pb");
308 fr.Controls["__EVENTTARGET"].Value = "pb";
309 fr.Controls["__EVENTARGUMENT"].Value = "";
310 fr.Controls["pb"].Value = "TestValue";
314 ArrayList eventlist = t.UserData as ArrayList;
315 if (eventlist == null)
316 Assert.Fail ("User data does not been created fail");
317 Assert.AreEqual ("ControlLoad", eventlist[0], "Live Cycle Flow #1");
318 Assert.AreEqual ("LoadPostData", eventlist[1], "Live Cycle Flow #2");
319 Assert.AreEqual ("TextChanged", eventlist[2], "Live Cycle Flow #3");
321 if (html.IndexOf ("TestValue") == -1)
322 Assert.Fail ("Wrong value failed");
325 public static void LoadPostData__Load (Page p)
327 PokerL b = new PokerL ();
329 p.Form.Controls.Add (b);
330 b.TextChanged += new EventHandler (b_TextChanged);
332 p.Response.Write (b.Text);
335 public static void b_TextChanged (object sender, EventArgs e)
337 if (WebTest.CurrentTest.UserData == null) {
338 ArrayList list = new ArrayList ();
339 list.Add ("TextChanged");
340 WebTest.CurrentTest.UserData = list;
343 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
345 throw new NullReferenceException ();
346 list.Add ("TextChanged");
347 WebTest.CurrentTest.UserData = list;
352 public void RaisePostDataChangedEvent ()
354 PokerL p = new PokerL ();
355 p.TextChanged += new EventHandler (p_TextChanged);
356 Assert.AreEqual (false, eventTextChanged, "RaisePostDataChangedEvent#1");
357 p.RaisePostDataChangedEvent ();
358 Assert.AreEqual (true, eventTextChanged, "RaisePostDataChangedEvent#2");
361 bool eventTextChanged;
362 void p_TextChanged(object sender, EventArgs e)
364 eventTextChanged = true;
368 public void AutoCompleteType_Test ()
370 WebTest t = new WebTest ("NoEventValidation.aspx");
371 t = new WebTest (PageInvoker.CreateOnLoad (AutoCompleteType__Load));
372 string html = t.Run ();
373 string orig ="<input name=\"Poker\" type=\"text\" vcard_name=\"vCard.FirstName\" id=\"Poker\" />";
374 HtmlDiff.AssertAreEqual (orig, HtmlDiff.GetControlFromPageHtml (html), "AutoCompleteType");
377 public static void AutoCompleteType__Load (Page page)
379 LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
380 LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
382 PokerL p = new PokerL ();
384 p.AutoCompleteType = AutoCompleteType.FirstName;
385 page.Form.Controls.Add (lcb);
386 page.Form.Controls.Add (p);
387 page.Form.Controls.Add (lce);
390 [TestFixtureTearDown]
391 public void TearDown ()