merge -r 60814:60815
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.HtmlControls / HtmlTextAreaTest.cs
1 //
2 // HtmlTextAreaTest.cs
3 //      - Unit tests for System.Web.UI.HtmlControls.HtmlTextArea
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
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
30 using System;
31 using System.Collections.Specialized;
32 using System.IO;
33 using System.Web;
34 using System.Web.UI;
35 using System.Web.UI.HtmlControls;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Web.UI.HtmlControls {
40
41         public class TestHtmlTextArea : HtmlTextArea {
42
43                 public StateBag StateBag {
44                         get { return base.ViewState; }
45                 }
46
47                 public string RenderAttributes ()
48                 {
49                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
50                         base.RenderAttributes (writer);
51                         return writer.InnerWriter.ToString ();
52                 }
53
54                 public string Render ()
55                 {
56                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
57                         base.Render (writer);
58                         return writer.InnerWriter.ToString ();
59                 }
60
61                 public void PublicAddParsedSubObject (object o)
62                 {
63                         base.AddParsedSubObject (o);
64                 }
65 #if NET_2_0
66                 public bool LoadPost (string key, NameValueCollection nvc)
67                 {
68                         return base.LoadPostData (key, nvc);
69                 }
70
71                 public void Raise ()
72                 {
73                         base.RaisePostDataChangedEvent ();
74                 }
75 #endif
76         }
77
78         [TestFixture]
79         public class HtmlTextAreaTest {
80
81                 [Test]
82                 public void DefaultProperties ()
83                 {
84                         TestHtmlTextArea ta = new TestHtmlTextArea ();
85                         Assert.AreEqual (0, ta.Attributes.Count, "Attributes.Count");
86                         Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count");
87
88                         Assert.AreEqual (-1, ta.Cols, "Cols");
89                         Assert.IsNull (ta.Name, "Name");
90                         Assert.AreEqual (-1, ta.Rows, "Rows");
91                         Assert.AreEqual (String.Empty, ta.Value, "Value");
92
93                         Assert.AreEqual ("textarea", ta.TagName, "TagName");
94                         Assert.AreEqual (0, ta.Attributes.Count, "Attributes.Count-2");
95                         Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count-2");
96                 }
97
98                 [Test]
99                 public void NullProperties ()
100                 {
101                         TestHtmlTextArea ta = new TestHtmlTextArea ();
102                         ta.Cols = -1;
103                         Assert.AreEqual (-1, ta.Cols, "Cols");
104                         ta.Name = null;
105                         Assert.IsNull (ta.Name, "Name");
106                         ta.Rows = -1;
107                         Assert.AreEqual (-1, ta.Rows, "Rows");
108                         ta.Value = null;
109                         Assert.AreEqual (String.Empty, ta.Value, "Value");
110
111                         Assert.AreEqual (0, ta.Attributes.Count, "Attributes.Count");
112                         Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count");
113                 }
114
115                 [Test]
116                 public void CleanProperties ()
117                 {
118                         TestHtmlTextArea ta = new TestHtmlTextArea ();
119                         ta.Cols = 1;
120                         Assert.AreEqual (1, ta.Cols, "Cols");
121                         ta.Name = "name";
122                         Assert.IsNull (ta.Name, "Name");
123                         ta.Rows = 2;
124                         Assert.AreEqual (2, ta.Rows, "Rows");
125                         ta.Value = "value";
126                         Assert.AreEqual ("value", ta.Value, "Value");
127                         Assert.AreEqual (3, ta.Attributes.Count, "3");
128                         Assert.AreEqual (3, ta.StateBag.Count, "StateBag.Count=3");
129
130                         ta.Cols = -1;
131                         Assert.AreEqual (-1, ta.Cols, "-Cols");
132                         ta.Name = null;
133                         Assert.IsNull (ta.Name, "-Name");
134                         ta.Rows = -1;
135                         Assert.AreEqual (-1, ta.Rows, "Rows");
136                         ta.Value = null;
137                         Assert.AreEqual (String.Empty, ta.Value, "-Value");
138                         Assert.AreEqual (0, ta.Attributes.Count, "0");
139                         Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count=0");
140                 }
141
142                 [Test]
143                 public void Name ()
144                 {
145                         HtmlTextArea ta = new HtmlTextArea ();
146                         Assert.IsNull (ta.ID, "ID");
147                         ta.Name = "name";
148                         Assert.IsNull (ta.Name, "Name");
149
150                         ta.ID = "id";
151                         Assert.AreEqual ("id", ta.ID, "ID-2");
152                         Assert.AreEqual ("id", ta.Name, "Name-ID");
153
154                         ta.Name = "name";
155                         Assert.AreEqual ("id", ta.Name, "Name-ID-2");
156
157                         ta.ID = null;
158                         Assert.IsNull (ta.ID, "ID-3");
159                         Assert.IsNull (ta.Name, "Name-2");
160                 }
161
162                 [Test]
163                 public void Value ()
164                 {
165                         TestHtmlTextArea ta = new TestHtmlTextArea ();
166                         Assert.AreEqual (0, ta.Attributes.Count, "0");
167                         Assert.AreEqual (0, ta.StateBag.Count, "StateBag.Count=0");
168
169                         ta.Value = "value";
170                         Assert.AreEqual ("value", ta.Value, "Value");
171                         Assert.AreEqual (1, ta.Attributes.Count, "1");
172                         Assert.AreEqual (1, ta.StateBag.Count, "StateBag.Count=1");
173
174                         // however it's not in attributes
175                         Assert.IsNull (ta.Attributes["value"], "Attributes");
176                         // but in InnerText and InnerHtml
177                         Assert.AreEqual ("value", ta.InnerText, "InnerText");
178                         Assert.AreEqual ("value", ta.InnerHtml, "InnerHtml");
179                         // the later is kept in the attributes
180                         Assert.IsNull (ta.Attributes["innertext"], "Attributes-InnerText");
181                         Assert.AreEqual ("value", ta.Attributes["innerhtml"], "Attributes-InnerHtml");
182                 }
183
184                 [Test]
185                 public void RenderAttributes ()
186                 {
187                         TestHtmlTextArea ta = new TestHtmlTextArea ();
188                         ta.Cols = 4;
189                         ta.Rows = 2;
190                         ta.Name = "mono";
191                         ta.Value = "value";
192                         // value is out
193                         Assert.AreEqual (" name cols=\"4\" rows=\"2\"", ta.RenderAttributes ());
194
195                         ta.ID = "go";
196                         Assert.AreEqual (" name=\"go\" id=\"go\" cols=\"4\" rows=\"2\"", ta.RenderAttributes ());
197                 }
198
199                 [Test]
200                 public void RenderName1 ()
201                 {
202                         UserControl ctrl = new UserControl ();
203                         ctrl.ID = "UC";
204                         Page page = new Page ();
205                         TestHtmlTextArea ta = new TestHtmlTextArea ();
206                         page.Controls.Add (ctrl);
207                         ctrl.Controls.Add (ta);
208                         ta.Name = "mono";
209                         ta.ID = "go";
210                         Assert.AreEqual (" name=\"UC:go\" id=\"UC_go\"", ta.RenderAttributes ());
211                 }
212
213                 [Test]
214                 public void Render ()
215                 {
216                         TestHtmlTextArea ta = new TestHtmlTextArea ();
217                         ta.Cols = 4;
218                         ta.Rows = 2;
219                         ta.Name = "mono";
220                         ta.Value = "value";
221                         // value is out
222                         Assert.AreEqual ("<textarea name cols=\"4\" rows=\"2\">value</textarea>", ta.Render ());
223
224                         ta.ID = "go";
225                         Assert.AreEqual ("<textarea name=\"go\" id=\"go\" cols=\"4\" rows=\"2\">value</textarea>", ta.Render ());
226                 }
227
228                 [Test]
229                 [ExpectedException (typeof (NullReferenceException))]
230                 [NUnit.Framework.Category ("NotWorking")] // Mono throw HttpException
231                 public void AddParsedSubObject_Null ()
232                 {
233                         TestHtmlTextArea ta = new TestHtmlTextArea ();
234                         ta.PublicAddParsedSubObject (null);
235                 }
236
237                 [Test]
238                 [ExpectedException (typeof (HttpException))]
239                 public void AddParsedSubObject_WrongType ()
240                 {
241                         TestHtmlTextArea ta = new TestHtmlTextArea ();
242                         ta.PublicAddParsedSubObject (this);
243                 }
244
245                 [Test]
246                 public void AddParsedSubObject_LiteralControl ()
247                 {
248                         TestHtmlTextArea ta = new TestHtmlTextArea ();
249                         ta.PublicAddParsedSubObject (new LiteralControl ());
250                 }
251
252                 [Test]
253                 public void AddParsedSubObject_DataBoundLiteralControl ()
254                 {
255                         TestHtmlTextArea ta = new TestHtmlTextArea ();
256                         ta.PublicAddParsedSubObject (new DataBoundLiteralControl (1,1));
257                 }
258
259                 private bool serverChange;
260                 private void ServerChange (object sender, EventArgs e)
261                 {
262                         serverChange = true;
263                 }
264
265                 [Test]
266                 public void IPostBackDataHandler_RaisePostBackEvent ()
267                 {
268                         TestHtmlTextArea ta = new TestHtmlTextArea ();
269                         ta.ServerChange += new EventHandler (ServerChange);
270                         IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
271                         serverChange = false;
272                         pbdh.RaisePostDataChangedEvent ();
273                         Assert.IsTrue (serverChange, "ServerChange");
274                 }
275
276                 [Test]
277                 [ExpectedException (typeof (NullReferenceException))]
278                 public void IPostBackDataHandler_LoadPostData_NullCollection ()
279                 {
280                         TestHtmlTextArea ta = new TestHtmlTextArea ();
281                         IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
282                         pbdh.LoadPostData ("id1", null);
283                 }
284
285                 [Test]
286                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
287                 public void IPostBackDataHandler_LoadPostData_IdNull ()
288                 {
289                         TestHtmlTextArea ta = new TestHtmlTextArea ();
290                         ta.ID = "id1";
291                         IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
292                         NameValueCollection nvc = new NameValueCollection ();
293                         nvc.Add ("id1", "mono");
294                         Assert.IsFalse (pbdh.LoadPostData (null, new NameValueCollection ()));
295                         Assert.AreEqual (String.Empty, ta.Value, "Value");
296                 }
297
298                 [Test]
299                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
300                 public void IPostBackDataHandler_LoadPostData_WrongId ()
301                 {
302                         TestHtmlTextArea ta = new TestHtmlTextArea ();
303                         ta.ID = "id1";
304                         IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
305                         NameValueCollection nvc = new NameValueCollection ();
306                         nvc.Add ("id1", "mono");
307                         Assert.IsFalse (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
308                         Assert.AreEqual (String.Empty, ta.Value, "Value");
309                 }
310
311                 [Test]
312                 public void IPostBackDataHandler_LoadPostData ()
313                 {
314                         TestHtmlTextArea ta = new TestHtmlTextArea ();
315                         ta.ID = "id1";
316                         IPostBackDataHandler pbdh = (ta as IPostBackDataHandler);
317                         NameValueCollection nvc = new NameValueCollection ();
318                         nvc.Add ("id1", "mono");
319                         Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
320                         Assert.AreEqual ("mono", ta.Value, "Value");
321                 }
322 #if NET_2_0
323                 [Test]
324                 public void RaisePostBackEvent ()
325                 {
326                         TestHtmlTextArea ta = new TestHtmlTextArea ();
327                         ta.ServerChange += new EventHandler (ServerChange);
328                         serverChange = false;
329                         ta.Raise ();
330                         Assert.IsTrue (serverChange, "ServerClick");
331                 }
332
333                 [Test]
334                 [ExpectedException (typeof (NullReferenceException))]
335                 public void LoadPostData_NullCollection ()
336                 {
337                         TestHtmlTextArea ta = new TestHtmlTextArea ();
338                         ta.LoadPost ("id1", null);
339                 }
340
341                 [Test]
342                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
343                 public void LoadPostData_IdNull ()
344                 {
345                         TestHtmlTextArea ta = new TestHtmlTextArea ();
346                         ta.ID = "id1";
347                         NameValueCollection nvc = new NameValueCollection ();
348                         nvc.Add ("id1", "mono");
349                         Assert.IsFalse (ta.LoadPost (null, nvc), "LoadPostData");
350                         Assert.AreEqual (String.Empty, ta.Value, "Value");
351                 }
352
353                 [Test]
354                 [Category ("NotDotNet")] // MS throws a NullReferenceException here
355                 public void LoadPostData_WrongId ()
356                 {
357                         TestHtmlTextArea ta = new TestHtmlTextArea ();
358                         ta.ID = "id1";
359                         NameValueCollection nvc = new NameValueCollection ();
360                         nvc.Add ("id1", "mono");
361                         Assert.IsFalse (ta.LoadPost ("id2", nvc), "LoadPostData");
362                         Assert.AreEqual (String.Empty, ta.Value, "Value");
363                 }
364
365                 [Test]
366                 public void LoadPostData ()
367                 {
368                         TestHtmlTextArea ta = new TestHtmlTextArea ();
369                         ta.ID = "id1";
370                         NameValueCollection nvc = new NameValueCollection ();
371                         nvc.Add ("id1", "mono");
372                         Assert.IsTrue (ta.LoadPost ("id1", nvc), "LoadPostData");
373                         Assert.AreEqual ("mono", ta.Value, "Value");
374                 }
375 #endif
376         }
377 }