New test.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / ClientScriptManagerTest.cs
1 //
2 // Tests for System.Web.UI.ClientScriptManagerTest.cs
3 //
4 // Author:
5 //      Yoni Klein (yonik@mainsoft.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
30 #if NET_2_0
31
32 using System;
33 using System.Web;
34 using System.Web.UI;
35 using System.Web.UI.WebControls;
36 using NUnit.Framework;
37 using MonoTests.stand_alone.WebHarness;
38 using MonoTests.SystemWeb.Framework;
39 using System.Text;
40 using System.Threading;
41
42 namespace MonoTests.System.Web.UI
43 {
44
45         public class MyPage : Page, ICallbackEventHandler
46         {
47                 #region ICallbackEventHandler Members
48
49                 public string GetCallbackResult ()
50                 {
51                         throw new Exception ("The method or operation is not implemented.");
52                 }
53
54                 public void RaiseCallbackEvent (string eventArgument)
55                 {
56                         throw new Exception ("The method or operation is not implemented.");
57                 }
58
59                 #endregion
60         }
61
62         [TestFixture]
63         public class ClientScriptManagerTest
64         {
65                 [TestFixtureSetUp]
66                 public void Set_Up ()
67                 {
68 #if DOT_NET
69                         WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.EventValidationTest1.aspx", "EventValidationTest1.aspx");
70                         WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.EventValidationTest2.aspx", "EventValidationTest2.aspx");
71 #else
72                         WebTest.CopyResource (GetType (), "EventValidationTest1.aspx", "EventValidationTest1.aspx");
73                         WebTest.CopyResource (GetType (), "EventValidationTest2.aspx", "EventValidationTest2.aspx");
74 #endif
75                 }
76
77                 [SetUp]
78                 public void SetupTestCase ()
79                 {
80                         Thread.Sleep (100);
81                 }
82
83                 [Test]
84                 public void ClientScriptManager_GetCallbackEventReference_1 ()
85                 {
86                         MyPage p = new MyPage ();
87                         ClientScriptManager cs = p.ClientScript;
88                         StringBuilder context1 = new StringBuilder ();
89                         context1.Append ("function ReceiveServerData1(arg, context)");
90                         context1.Append ("{");
91                         context1.Append ("Message1.innerText =  arg;");
92                         context1.Append ("value1 = arg;");
93                         context1.Append ("}");
94
95                         // Define callback references.
96                         String cbReference = cs.GetCallbackEventReference (p, "arg",
97                             "ReceiveServerData1", context1.ToString ());
98                         Assert.AreEqual ("WebForm_DoCallback('__Page',arg,ReceiveServerData1,function ReceiveServerData1(arg, context){Message1.innerText =  arg;value1 = arg;},null,false)", cbReference, "GetCallbackEventReferenceFail1");
99                 }
100
101                 [Test]
102                 public void ClientScriptManager_GetCallbackEventReference_2 ()
103                 {
104                         MyPage p = new MyPage ();
105                         ClientScriptManager cs = p.ClientScript;
106                         StringBuilder context1 = new StringBuilder ();
107                         context1.Append ("function ReceiveServerData1(arg, context)");
108                         context1.Append ("{");
109                         context1.Append ("Message1.innerText =  arg;");
110                         context1.Append ("value1 = arg;");
111                         context1.Append ("}");
112
113                         // Define callback references.
114                         String cbReference = cs.GetCallbackEventReference (p, "arg",
115                             "ReceiveServerData1", context1.ToString (), true);
116                         Assert.AreEqual ("WebForm_DoCallback('__Page',arg,ReceiveServerData1,function ReceiveServerData1(arg, context){Message1.innerText =  arg;value1 = arg;},null,true)", cbReference, "GetCallbackEventReferenceFail2");
117                 }
118
119                 [Test]
120                 public void ClientScriptManager_GetCallbackEventReference_3 ()
121                 {
122                         MyPage p = new MyPage ();
123                         ClientScriptManager cs = p.ClientScript;
124                         StringBuilder context1 = new StringBuilder ();
125                         context1.Append ("function ReceiveServerData1(arg, context)");
126                         context1.Append ("{");
127                         context1.Append ("Message1.innerText =  arg;");
128                         context1.Append ("value1 = arg;");
129                         context1.Append ("}");
130
131                         // Define callback references.
132                         String cbReference = cs.GetCallbackEventReference (p, "arg",
133                             "ReceiveServerData1", context1.ToString (), "ErrorCallback", false);
134                         Assert.AreEqual ("WebForm_DoCallback('__Page',arg,ReceiveServerData1,function ReceiveServerData1(arg, context){Message1.innerText =  arg;value1 = arg;},ErrorCallback,false)", cbReference, "GetCallbackEventReferenceFail3");
135                 }
136
137                 [Test]
138                 public void ClientScriptManager_GetPostBackEventReference_1 ()
139                 {
140                         MyPage p = new MyPage ();
141                         ClientScriptManager cs = p.ClientScript;
142                         String result = cs.GetPostBackEventReference (new PostBackOptions (p, "args"));
143                         Assert.AreEqual ("__doPostBack('__Page','args')", result, "GetPostBackEventReference#1");
144                 }
145
146                 [Test]
147                 public void ClientScriptManager_GetPostBackEventReference_2 ()
148                 {
149                         MyPage p = new MyPage ();
150                         ClientScriptManager cs = p.ClientScript;
151                         String result = cs.GetPostBackEventReference (p, "args");
152                         Assert.AreEqual ("__doPostBack('__Page','args')", result, "GetPostBackEventReference#2");
153                 }
154
155                 [Test]
156                 public void ClientScriptManager_GetPostBackClientHyperlink ()
157                 {
158                         MyPage p = new MyPage ();
159                         ClientScriptManager cs = p.ClientScript;
160                         String hyperlink = cs.GetPostBackClientHyperlink (p, "args");
161                         Assert.AreEqual ("javascript:__doPostBack('__Page','args')", hyperlink, "GetPostBackClientHyperlink");
162                 }
163
164                 [Test]
165                 [Category("NunitWeb")]
166                 public void ClientScriptManager_GetWebResourceUrl ()
167                 {
168                         string html = new WebTest (PageInvoker.CreateOnLoad (GetWebResourceUrlLoad)).Run();
169                 }
170
171                 public static void GetWebResourceUrlLoad (Page p)
172                 {
173                         ClientScriptManager cs = p.ClientScript;
174                         String cbReference = cs.GetWebResourceUrl (typeof (MonoTests.System.Web.UI.ClientScriptManagerTest), "ClientScript.js");
175                         if (cbReference.IndexOf("/NunitWeb/WebResource.axd?")<0)
176                                 Assert.Fail ("GetWebResourceUrlFail");
177                 }
178
179                 [Test]
180                 public void ClientScriptManager_IsClientScriptBlockRegistered ()
181                 {
182                         Page p = new Page ();
183                         ClientScriptManager cs = p.ClientScript;
184                         String csname2 = "ButtonClickScript";
185                         Type cstype = p.GetType ();
186                         StringBuilder cstext2 = new StringBuilder ();
187                         cstext2.Append ("<script type=text/javascript> function DoClick() {");
188                         cstext2.Append ("alert('Text from client script.')} </");
189                         cstext2.Append ("script>");
190                         cs.RegisterClientScriptBlock (cstype, csname2, cstext2.ToString ());
191                         Assert.AreEqual (true, cs.IsClientScriptBlockRegistered (cstype, csname2), "ClientScriptBlockRegisterFail#1");
192                 }
193
194                 [Test]
195                 public void ClientScriptManager_IsRegisterClientScriptInclude ()
196                 {
197                         Page p = new Page ();
198                         String csname = "ButtonClickScript";
199                         String csurl = "ClientScript.js";
200                         Type cstype = p.GetType ();
201                         ClientScriptManager cs = p.ClientScript;
202                         cs.RegisterClientScriptInclude (cstype, csname, csurl);
203                         bool registry = cs.IsClientScriptIncludeRegistered (cstype, csname);
204                         Assert.AreEqual (true, registry, "RegisterClientScriptIncludeFail");
205                 }
206
207                 [Test]
208                 public void ClientScriptManager_IsRegisterOnSubmitStatement ()
209                 {
210                         Page p = new Page ();
211                         String csname = "ButtonClickScript";
212                         Type cstype = p.GetType ();
213                         ClientScriptManager cs = p.ClientScript;
214                         cs.RegisterClientScriptInclude (cstype, csname, "document.write('Text from OnSubmit statement');");
215                         bool registry = cs.IsClientScriptIncludeRegistered (cstype, csname);
216                         Assert.AreEqual (true, registry, "RegisterClientScriptIncludeFail");
217                 }
218
219                 [Test]
220                 [Category ("NunitWeb")]
221                 [Category ("NotWorking")] // implementation specific
222                 public void ClientScriptManager_RegisterOnSubmitStatement ()
223                 {
224                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterOnSubmitStatement));
225                         string html = t.Run ();
226                         if (html.IndexOf ("WebForm_OnSubmit()") < 0)
227                                 Assert.Fail ("RegisterOnSubmitStatement");
228                 }
229
230                 public static void RegisterOnSubmitStatement (Page p)
231                 {
232                         String csname = "OnSubmitScript";
233                         Type cstype = p.GetType ();
234                         ClientScriptManager cs = p.ClientScript;
235                         String cstext = "document.write('Text from OnSubmit statement');";
236                         cs.RegisterOnSubmitStatement (cstype, csname, cstext);
237                 }
238
239                 [Test]
240                 [Category ("NunitWeb")]
241                 public void ClientScriptManager_RegisterClientScriptInclude ()
242                 {
243                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterClientScriptInclude));
244                         string html = t.Run ();
245                         if (html.IndexOf ("script_include.js") < 0)
246                                 Assert.Fail ("RegisterClientScriptIncludeFail");
247                 }
248
249                 public static void RegisterClientScriptInclude (Page p)
250                 {
251                         String csname = "ButtonClickScript";
252                         String csurl = "script_include.js";
253                         Type cstype = p.GetType ();
254                         ClientScriptManager cs = p.ClientScript;
255                         cs.RegisterClientScriptInclude (cstype, csname, csurl);
256                 }
257
258                 [Test]
259                 [Category("NunitWeb")]
260                 public void ClientScriptManager_ClientScriptBlockRegister()
261                 {
262                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClientScriptBlockRegister));
263                         string html = t.Run ();
264                         if( html.IndexOf("DoClick()") < 0)
265                                 Assert.Fail ("ClientScriptBlockRegisterFail#2");
266                 }
267
268                 public static void ClientScriptBlockRegister (Page p)
269                 {
270                         ClientScriptManager cs = p.ClientScript;
271                         String csname2 = "ButtonClickScript";
272                         Type cstype = p.GetType ();
273                         StringBuilder cstext2 = new StringBuilder ();
274                         cstext2.Append ("<script type=text/javascript> function DoClick() {");
275                         cstext2.Append ("alert('Text from client script.')} </");
276                         cstext2.Append ("script>");
277                         cs.RegisterClientScriptBlock (cstype, csname2, cstext2.ToString ());
278                 }
279
280                 [Test]
281                 public void ClientScriptManager_IsRegisterStartupScript ()
282                 {
283                         Page p = new Page ();
284                         String csname1 = "PopupScript";
285                         Type cstype = p.GetType ();
286                         ClientScriptManager cs = p.ClientScript;
287                         String cstext1 = "alert('Hello World');";
288                         cs.RegisterStartupScript (cstype, csname1, cstext1);
289                         Assert.AreEqual (true, cs.IsStartupScriptRegistered (cstype, csname1), "StartupScriptRegisteredFail");  
290                 }
291
292                 [Test]
293                 [Category ("NunitWeb")]
294                 public void ClientScriptManager_RegisterStartupScript ()
295                 {
296                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterStartupScript));
297                         string html = t.Run ();
298                         if (html.IndexOf ("alert('Hello World');") < 0)
299                                 Assert.Fail ("RegisterStartupScriptFail#1");
300                 }
301
302                 public static void RegisterStartupScript (Page p)
303                 {
304                         String csname1 = "PopupScript";
305                         Type cstype = p.GetType ();
306                         ClientScriptManager cs = p.ClientScript;
307                         String cstext1 = "alert('Hello World');";
308                         cs.RegisterStartupScript (cstype, csname1, cstext1,true);
309                 }
310
311                 [Test]
312                 [Category ("NunitWeb")]
313                 public void ClientScriptManager_RegisterArrayDeclaration ()
314                 {
315                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterArrayDeclaration));
316                         string html = t.Run ();
317                         if (html.IndexOf ("var MyArray =  new Array(\"1\", \"2\", \"text\");") < 0)
318                                 Assert.Fail ("RegisterArrayDeclarationFail#1");
319                 }
320
321                 public static void RegisterArrayDeclaration (Page p)
322                 {
323                         Type cstype = p.GetType ();
324                         ClientScriptManager cs = p.ClientScript;
325
326                         String arrName = "MyArray";
327                         String arrValue = "\"1\", \"2\", \"text\"";
328
329                         // Register the array with the Page class.
330                         cs.RegisterArrayDeclaration (arrName, arrValue);
331                 }
332
333                 [Test]
334                 [Category ("NunitWeb")]
335                 public void ClientScriptManager_RegisterExpandAttribute ()
336                 {
337                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterExpandAttribute));
338                         string html = t.Run ();
339                         if (html.IndexOf ("Message.title = \"New title from client script.\"") < 0)
340                                 Assert.Fail ("RegisterExpandAttributeFail");
341                 }
342
343                 public static void RegisterExpandAttribute (Page p)
344                 {
345                         ClientScriptManager cs = p.ClientScript;
346                         cs.RegisterExpandoAttribute ("Message", "title", "New title from client script.", true);
347                 }
348
349                 [Test]
350                 [Category ("NunitWeb")]
351                 public void ClientScriptManager_RegisterHiddenField ()
352                 {
353                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterHiddenField));
354                         string html = t.Run ();
355                         if (html.IndexOf ("<input type=\"hidden\" name=\"MyHiddenField\" id=\"MyHiddenField\" value=\"3\" />") < 0)
356                                 Assert.Fail ("RegisterHiddenFieldFail");
357                 }
358
359                 public static void RegisterHiddenField (Page p)
360                 {
361                         ClientScriptManager cs = p.ClientScript;
362                         // Define the hidden field name and initial value.
363                         String hiddenName = "MyHiddenField";
364                         String hiddenValue = "3";
365                         // Register the hidden field with the Page class.
366                         cs.RegisterHiddenField (hiddenName, hiddenValue);
367                 }
368
369                 [Test]
370                 [Category ("NunitWeb")]
371                 [Category ("NotDotNet")] // for dot-net use __CALLBACKID insted __CALLBACKTARGET and __CALLBACKARGUMENT insted __CALLBACKPARAM
372                 public void ClientScriptManager_RegisterForEventValidation_1 ()
373                 {
374                         WebTest t = new WebTest ("EventValidationTest1.aspx");
375                         string html = t.Run ();
376                         
377                         FormRequest fr = new FormRequest (t.Response, "form1");
378                         fr.Controls.Add ("__EVENTTARGET");
379                         fr.Controls.Add ("__EVENTARGUMENT");
380                         fr.Controls.Add ("__CALLBACKTARGET");
381                         fr.Controls.Add ("__CALLBACKARGUMENT");
382                         fr.Controls["__EVENTTARGET"].Value = "";
383                         fr.Controls["__EVENTARGUMENT"].Value = "";
384                         fr.Controls ["__CALLBACKTARGET"].Value = "__Page";
385                         t.Request = fr;
386                         html = t.Run ();
387
388                         if(html.IndexOf("Correct event raised callback.")<0)
389                                 Assert.Fail ("RegisterForEventValidationFail#1");
390                 }
391
392                 [Test]
393                 [Category ("NunitWeb")]
394                 [Category ("NotDotNet")] // for dot-net use __CALLBACKID insted __CALLBACKTARGET and __CALLBACKPARAM insted __CALLBACKARGUMENT
395                 public void ClientScriptManager_RegisterForEventValidation_2 ()
396                 {
397                         WebTest t = new WebTest ("EventValidationTest2.aspx");
398                         string html = t.Run ();
399                         
400                         FormRequest fr = new FormRequest (t.Response, "form1");
401                         fr.Controls.Add ("__EVENTTARGET");
402                         fr.Controls.Add ("__EVENTARGUMENT");
403                         fr.Controls.Add ("__CALLBACKTARGET");
404                         fr.Controls.Add ("__CALLBACKARGUMENT");
405                         fr.Controls["__EVENTTARGET"].Value = "";
406                         fr.Controls["__EVENTARGUMENT"].Value = "";
407                         fr.Controls ["__CALLBACKTARGET"].Value = "__Page";
408                         t.Request = fr;
409                         html = t.Run ();
410
411                         if (html.IndexOf ("Incorrect event raised callback.") < 0)
412                                 Assert.Fail ("RegisterForEventValidationFail#2");
413                 }
414
415                 // Expected Exceptions
416
417                 [Test]
418                 [ExpectedException (typeof (InvalidOperationException))]
419                 public void ClientScriptManager_RegisterForEventValidationException ()
420                 {
421                         Page p = new Page ();
422                         ClientScriptManager cs = p.ClientScript;
423                         cs.RegisterForEventValidation ("ID", "args");
424                 }
425
426                 
427                 [Test]
428                 [ExpectedException (typeof (ArgumentException))]
429                 public void ClientScriptManager_ValidateEventException_1 ()
430                 {
431                         Page p = new Page ();
432                         ClientScriptManager cs = p.ClientScript;
433                         cs.ValidateEvent ("Exception");
434                 }
435
436                 
437                 [Test]
438                 [ExpectedException (typeof (ArgumentException))]
439                 public void ClientScriptManager_ValidateEventException_2 ()
440                 {
441                         Page p = new Page ();
442                         ClientScriptManager cs = p.ClientScript;
443                         cs.ValidateEvent ("Exception", "args");
444                 }
445
446
447                 [Test]
448                 [ExpectedException (typeof (ArgumentNullException))]
449                 public void ClientScriptManager_IsRegisterStartupScriptException ()
450                 {
451                         Page p = new Page ();
452                         String csname1 = "PopupScript";
453                         ClientScriptManager cs = p.ClientScript;
454                         cs.RegisterStartupScript (null, csname1, "");
455                 }
456                 
457                 [Test]
458                 [ExpectedException (typeof (ArgumentNullException))]
459                 public void ClientScriptManager_RegisterOnSubmitStatementException ()
460                 {
461                         Page p = new Page ();
462                         String csname = "OnSubmitScript";
463                         ClientScriptManager cs = p.ClientScript;
464                         String cstext = "document.write('Text from OnSubmit statement');";
465                         cs.RegisterOnSubmitStatement (null, csname, cstext);
466                 }
467
468                 [Test]
469                 [ExpectedException (typeof (ArgumentNullException))]
470                 public void ClientScriptManager_RegisterClientScriptIncludeException_1 ()
471                 {
472                         Page p = new Page ();
473                         String csname = "ButtonClickScript";
474                         Type cstype = p.GetType ();
475                         String csurl = "";
476                         ClientScriptManager cs = p.ClientScript;
477                         cs.RegisterClientScriptInclude (null, csname, csurl);
478                         bool registry = cs.IsClientScriptIncludeRegistered (csname);
479                 }
480
481                 [Test]
482                 [ExpectedException (typeof (ArgumentNullException))]
483                 public void ClientScriptManager_RegisterClientScriptIncludeException_2 ()
484                 {
485                         Page p = new Page ();
486                         String csname = "ButtonClickScript";
487                         String csurl = "ClientScript.js";
488                         ClientScriptManager cs = p.ClientScript;
489                         cs.RegisterClientScriptInclude (null, csname, csurl);
490                         bool registry = cs.IsClientScriptIncludeRegistered (csname);
491                 }
492
493                 [Test]
494                 [ExpectedException (typeof (ArgumentNullException))]
495                 public void ClientScriptManager_ClientScriptBlockRegisterException_2 ()
496                 {
497                         Page p = new Page ();
498                         ClientScriptManager cs = p.ClientScript;
499                         String csname2 = "ButtonClickScript";
500                         cs.RegisterClientScriptBlock (null, csname2, "");
501                 }
502
503
504                 [Test]
505                 [ExpectedException (typeof (ArgumentNullException))]
506                 public void ClientScriptManager_GetWebResourceUrlException_1 ()
507                 {
508                         Page p = new Page ();
509                         ClientScriptManager cs = p.ClientScript;
510                         String cbReference = cs.GetWebResourceUrl (null, "test");
511                 }
512
513                 [Test]
514                 [ExpectedException (typeof (ArgumentNullException))]
515                 public void ClientScriptManager_GetWebResourceUrlException_2 ()
516                 {
517                         Page p = new Page ();
518                         ClientScriptManager cs = p.ClientScript;
519                         String cbReference = cs.GetWebResourceUrl (typeof (ClientScriptManagerTest), "");
520                 }
521
522                 [Test]
523                 [ExpectedException (typeof (InvalidOperationException))]
524                 public void ClientScriptManager_GetCallbackEventReferenceException_1 ()
525                 {
526                         Page p = new Page ();
527                         ClientScriptManager cs = p.ClientScript;
528
529                         // Define callback references.
530                         String cbReference = cs.GetCallbackEventReference (p, "arg",
531                             "ReceiveServerData1", "");
532                 }
533
534                 [Test]
535                 [ExpectedException (typeof (ArgumentNullException))]
536                 public void ClientScriptManager_GetCallbackEventReferenceException_2 ()
537                 {
538                         Page p = new Page ();
539                         ClientScriptManager cs = p.ClientScript;
540
541                         // Define callback references.
542                         String cbReference = cs.GetCallbackEventReference (null, "arg",
543                             "ReceiveServerData1", "");
544                 }
545
546                 [TestFixtureTearDown]
547                 public void Unload()
548                 {
549                         WebTest.Unload();
550                 }
551         }
552 }
553 #endif
554
555
556