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