[SRE] Improved token fixups processing.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / TextBoxTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.TextBox.cs 
3 //
4 // Author:
5 //     Ben Maurer (bmaurer@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Globalization;
35 using System.Web;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38 using MonoTests.SystemWeb.Framework;
39 using MonoTests.stand_alone.WebHarness;
40 using System.Collections;
41
42 namespace MonoTests.System.Web.UI.WebControls {
43         
44         [TestFixture]   
45         public class TextBoxTest {
46                 class Poker : TextBox {
47                         public new void AddParsedSubObject (object o)
48                         {
49                                 base.AddParsedSubObject (o);
50                         }
51
52                         public void TrackState () 
53                         {
54                                 TrackViewState ();
55                         }
56                         
57                         public object SaveState ()
58                         {
59                                 foreach (string s in ViewState.Keys)
60                                         Console.WriteLine ("{0}: {1}", s, ViewState[s]);
61
62                                 return SaveViewState ();
63                         }
64                         
65                         public void LoadState (object o)
66                         {
67                                 LoadViewState (o);
68                         }
69                         
70                         public string Render ()
71                         {
72                                 StringWriter sw = new StringWriter ();
73                                 sw.NewLine = "\n";
74                                 HtmlTextWriter writer = new HtmlTextWriter (sw);
75                                 base.Render (writer);
76                                 return writer.InnerWriter.ToString ();
77                         }                       
78                 }
79
80                 [TestFixtureSetUp]
81                 public void SetUp ()
82                 {
83                         WebTest.CopyResource (GetType (), "TextBoxTestlPage.aspx", "TextBoxTestlPage.aspx");
84                         WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
85                 }
86
87                 [Test]
88                 public void Defaults ()
89                 {
90                         Poker p = new Poker ();
91                         Assert.AreEqual (string.Empty, p.ValidationGroup, "ValidationGroup");
92                         Assert.AreEqual (false, p.CausesValidation, "CausesValidation");
93                 }
94
95                 [Test]
96                 public void Defaults_NotWorking ()
97                 {
98                         Poker p = new Poker ();
99                         Assert.AreEqual (AutoCompleteType.None, p.AutoCompleteType, "AutoCompleteType");
100                 }
101
102                 [Test]
103                 public void MultilineRenderEscape ()
104                 {
105                         Poker t = new Poker ();
106                         t.TextMode = TextBoxMode.MultiLine;
107                         t.Text = "</textarea>";
108 #if NET_4_0
109                         string exp = "<textarea rows=\"2\" cols=\"20\">\r\n&lt;/textarea&gt;</textarea>";
110 #else
111                         string exp = "<textarea rows=\"2\" cols=\"20\">&lt;/textarea&gt;</textarea>";
112 #endif
113
114                         HtmlDiff.AssertAreEqual(exp, t.Render (),"MultilineRenderEscape");
115                 }
116
117
118                 [Test]
119                 public void ValidationProperties ()
120                 {
121                         Poker t = new Poker ();
122
123                         // initial values
124                         Assert.AreEqual (false, t.CausesValidation, "A1");
125                         Assert.AreEqual ("", t.ValidationGroup, "A2");
126
127                         t.ValidationGroup = "VG";
128                         Assert.AreEqual ("VG", t.ValidationGroup, "A3");
129
130                         t.CausesValidation = true;
131                         Assert.IsTrue (t.CausesValidation, "A4");
132                 }
133
134                 [Test]
135                 public void ViewState ()
136                 {
137                         Poker t = new Poker ();
138
139                         t.TrackState();
140
141                         t.ValidationGroup = "VG";
142                         t.CausesValidation = true;
143
144                         object s = t.SaveState ();
145                         Console.WriteLine ("state = {0}", s == null ? "null" : "not-null");
146
147                         Poker copy = new Poker ();
148
149                         copy.LoadState (s);
150
151                         Assert.AreEqual ("VG", copy.ValidationGroup, "A1");
152                         Assert.IsTrue (copy.CausesValidation, "A2");
153                 }
154
155                 [Test]
156                 public void ValidationRender ()
157                 {
158                         /* test to show that the validation settings
159                          * have no effect on downlevel rendering */
160                         Poker t = new Poker ();
161
162                         t.TrackState();
163
164                         t.ValidationGroup = "VG";
165                         t.CausesValidation = true;
166                         t.TextMode = TextBoxMode.MultiLine;
167 #if NET_4_0
168                         string exp = "<textarea rows=\"2\" cols=\"20\">\r\n</textarea>";
169 #else
170                         string exp = "<textarea rows=\"2\" cols=\"20\"></textarea>";
171 #endif
172                         HtmlDiff.AssertAreEqual (exp, t.Render (),"ValidationRender");
173                 }
174
175                 [Test]
176                 [Category ("NunitWeb")]
177                 public void CausesValidation_ValidationGroup ()
178                 {
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";
188                         t.Request = fr;
189                         string html = t.Run ();
190
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");
195                 }
196
197                 #region Help_class
198                 public class PokerL : TextBox
199                 {
200                         public string Render ()
201                         {
202                                 StringWriter sw = new StringWriter ();
203                                 sw.NewLine = "\n";
204                                 HtmlTextWriter writer = new HtmlTextWriter (sw);
205                                 base.Render (writer);
206                                 return writer.InnerWriter.ToString ();
207                         }                       
208
209                         public new void RaisePostDataChangedEvent ()
210                         {
211                                 base.RaisePostDataChangedEvent ();
212                         }
213
214                         protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
215                         {
216                                 if (WebTest.CurrentTest.UserData == null) {
217                                         ArrayList list = new ArrayList ();
218                                         list.Add ("LoadPostData");
219                                         WebTest.CurrentTest.UserData = list;
220                                 }
221                                 else {
222                                         ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
223                                         if (list == null)
224                                                 throw new NullReferenceException ();
225                                         list.Add ("LoadPostData");
226                                         WebTest.CurrentTest.UserData = list;
227                                 }
228                                 return base.LoadPostData (postDataKey, postCollection);
229                         }
230
231                         protected internal override void OnLoad (EventArgs e)
232                         {
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;
238                                         }
239                                         else {
240                                                 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
241                                                 if (list == null)
242                                                         throw new NullReferenceException ();
243                                                 list.Add ("ControlLoad");
244                                                 WebTest.CurrentTest.UserData = list;
245                                         }
246                                 }
247                                 base.OnLoad (e);
248                         }
249                 }
250                 #endregion
251
252                 [Test]
253                 [Category ("NunitWeb")]
254                 public void LoadPostData_Flow ()  //Just flow and not implementation detail
255                 {
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";
265                         t.Request = fr;
266                         t.Run ();
267
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");
274                 }
275
276                 public static void LoadPostData_Load (Page p)
277                 {
278                         PokerL b = new PokerL ();
279                         b.AutoPostBack = true;
280                         b.ID = "pb";
281                         p.Form.Controls.Add (b);
282                         if (p.IsPostBack) {
283                                 if (WebTest.CurrentTest.UserData == null) {
284                                         ArrayList list = new ArrayList ();
285                                         list.Add ("PageLoad");
286                                         WebTest.CurrentTest.UserData = list;
287                                 }
288                                 else {
289                                         ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
290                                         if (list == null)
291                                                 throw new NullReferenceException ();
292                                         list.Add ("PageLoad");
293                                         WebTest.CurrentTest.UserData = list;
294                                 }
295                         }
296                 }
297
298                 [Test]
299                 [Category ("NunitWeb")]
300                 public void LoadPostData ()  
301                 {
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";
311                         t.Request = fr;
312                         html = t.Run ();
313
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");
320
321                         if (html.IndexOf ("TestValue") == -1)
322                                 Assert.Fail ("Wrong value failed");
323                 }
324
325                 public static void LoadPostData__Load (Page p)
326                 {
327                         PokerL b = new PokerL ();
328                         b.ID = "pb";
329                         p.Form.Controls.Add (b);
330                         b.TextChanged += new EventHandler (b_TextChanged);
331                         if (p.IsPostBack)
332                                 p.Response.Write (b.Text);
333                 }
334
335                 public static void b_TextChanged (object sender, EventArgs e)
336                 {
337                         if (WebTest.CurrentTest.UserData == null) {
338                                 ArrayList list = new ArrayList ();
339                                 list.Add ("TextChanged");
340                                 WebTest.CurrentTest.UserData = list;
341                         }
342                         else {
343                                 ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
344                                 if (list == null)
345                                         throw new NullReferenceException ();
346                                 list.Add ("TextChanged");
347                                 WebTest.CurrentTest.UserData = list;
348                         }
349                 }
350
351                 [Test]
352                 public void RaisePostDataChangedEvent ()
353                 {
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");
359                 }
360
361                 bool eventTextChanged;
362                 void  p_TextChanged(object sender, EventArgs e)
363                 {
364                         eventTextChanged = true;        
365                 }
366
367                 [Test]
368                 public void AutoCompleteType_Test ()
369                 {
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");
375                 }
376
377                 public static void AutoCompleteType__Load (Page page)
378                 {
379                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
380                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
381
382                         PokerL p = new PokerL ();
383                         p.ID = "Poker";
384                         p.AutoCompleteType = AutoCompleteType.FirstName;
385                         page.Form.Controls.Add (lcb);
386                         page.Form.Controls.Add (p);
387                         page.Form.Controls.Add (lce);
388                 }
389
390                 [TestFixtureTearDown]
391                 public void TearDown ()
392                 {
393                         WebTest.Unload ();
394                 }
395         }
396 }
397