1fd068cb4ac812c042b82f5bc0cb6ca9009bc15b
[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 (), "EventValidationTest1.aspx", "EventValidationTest1.aspx");
70                         WebTest.CopyResource (GetType (), "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("/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                 public void ClientScriptManager_RegisterOnSubmitStatement ()
222                 {
223                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterOnSubmitStatement));
224                         string html = t.Run ();
225                         if (html.IndexOf ("onsubmit") < 0)
226                                 Assert.Fail ("RegisterOnSubmitStatement");
227                 }
228
229                 public static void RegisterOnSubmitStatement (Page p)
230                 {
231                         String csname = "OnSubmitScript";
232                         Type cstype = p.GetType ();
233                         ClientScriptManager cs = p.ClientScript;
234                         String cstext = "document.write('Text from OnSubmit statement');";
235                         cs.RegisterOnSubmitStatement (cstype, csname, cstext);
236                 }
237
238                 [Test]
239                 [Category ("NunitWeb")]
240                 public void ClientScriptManager_RegisterClientScriptInclude ()
241                 {
242                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterClientScriptInclude));
243                         string html = t.Run ();
244                         if (html.IndexOf ("script_include.js") < 0)
245                                 Assert.Fail ("RegisterClientScriptIncludeFail");
246                 }
247
248                 public static void RegisterClientScriptInclude (Page p)
249                 {
250                         String csname = "ButtonClickScript";
251                         String csurl = "script_include.js";
252                         Type cstype = p.GetType ();
253                         ClientScriptManager cs = p.ClientScript;
254                         cs.RegisterClientScriptInclude (cstype, csname, csurl);
255                 }
256
257                 [Test]
258                 [Category("NunitWeb")]
259                 public void ClientScriptManager_ClientScriptBlockRegister()
260                 {
261                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClientScriptBlockRegister));
262                         string html = t.Run ();
263                         if( html.IndexOf("DoClick()") < 0)
264                                 Assert.Fail ("ClientScriptBlockRegisterFail#2");
265                 }
266
267                 public static void ClientScriptBlockRegister (Page p)
268                 {
269                         ClientScriptManager cs = p.ClientScript;
270                         String csname2 = "ButtonClickScript";
271                         Type cstype = p.GetType ();
272                         StringBuilder cstext2 = new StringBuilder ();
273                         cstext2.Append ("<script type=text/javascript> function DoClick() {");
274                         cstext2.Append ("alert('Text from client script.')} </");
275                         cstext2.Append ("script>");
276                         cs.RegisterClientScriptBlock (cstype, csname2, cstext2.ToString ());
277                 }
278
279                 [Test]
280                 public void ClientScriptManager_IsRegisterStartupScript ()
281                 {
282                         Page p = new Page ();
283                         String csname1 = "PopupScript";
284                         Type cstype = p.GetType ();
285                         ClientScriptManager cs = p.ClientScript;
286                         String cstext1 = "alert('Hello World');";
287                         cs.RegisterStartupScript (cstype, csname1, cstext1);
288                         Assert.AreEqual (true, cs.IsStartupScriptRegistered (cstype, csname1), "StartupScriptRegisteredFail");  
289                 }
290
291                 [Test]
292                 [Category ("NunitWeb")]
293                 public void ClientScriptManager_RegisterStartupScript ()
294                 {
295                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterStartupScript));
296                         string html = t.Run ();
297                         if (html.IndexOf ("alert('Hello World');") < 0)
298                                 Assert.Fail ("RegisterStartupScriptFail#1");
299                 }
300
301                 public static void RegisterStartupScript (Page p)
302                 {
303                         String csname1 = "PopupScript";
304                         Type cstype = p.GetType ();
305                         ClientScriptManager cs = p.ClientScript;
306                         String cstext1 = "alert('Hello World');";
307                         cs.RegisterStartupScript (cstype, csname1, cstext1,true);
308                 }
309
310                 [Test]
311                 [Category ("NunitWeb")]
312                 public void ClientScriptManager_RegisterArrayDeclaration ()
313                 {
314                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterArrayDeclaration));
315                         string html = t.Run ();
316                         if (html.IndexOf ("var MyArray =  new Array(\"1\", \"2\", \"text\");") < 0)
317                                 Assert.Fail ("RegisterArrayDeclarationFail#1");
318                 }
319
320                 public static void RegisterArrayDeclaration (Page p)
321                 {
322                         Type cstype = p.GetType ();
323                         ClientScriptManager cs = p.ClientScript;
324
325                         String arrName = "MyArray";
326                         String arrValue = "\"1\", \"2\", \"text\"";
327
328                         // Register the array with the Page class.
329                         cs.RegisterArrayDeclaration (arrName, arrValue);
330                 }
331
332                 [Test]
333                 [Category ("NunitWeb")]
334                 public void ClientScriptManager_RegisterExpandAttribute ()
335                 {
336                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterExpandAttribute));
337                         string html = t.Run ();
338                         if (html.IndexOf ("Message.title = \"New title from client script.\"") < 0)
339                                 Assert.Fail ("RegisterExpandAttributeFail");
340                 }
341
342                 public static void RegisterExpandAttribute (Page p)
343                 {
344                         ClientScriptManager cs = p.ClientScript;
345                         cs.RegisterExpandoAttribute ("Message", "title", "New title from client script.", true);
346                 }
347
348                 [Test]
349                 [Category ("NunitWeb")]
350                 public void ClientScriptManager_RegisterHiddenField ()
351                 {
352                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterHiddenField));
353                         string html = t.Run ();
354                         if (html.IndexOf ("<input type=\"hidden\" name=\"MyHiddenField\" id=\"MyHiddenField\" value=\"3\" />") < 0)
355                                 Assert.Fail ("RegisterHiddenFieldFail");
356                 }
357
358                 public static void RegisterHiddenField (Page p)
359                 {
360                         ClientScriptManager cs = p.ClientScript;
361                         // Define the hidden field name and initial value.
362                         String hiddenName = "MyHiddenField";
363                         String hiddenValue = "3";
364                         // Register the hidden field with the Page class.
365                         cs.RegisterHiddenField (hiddenName, hiddenValue);
366                 }
367
368                 [Test]
369                 [Category ("NunitWeb")]
370                 [Category ("NotDotNet")] // for dot-net use __CALLBACKID insted __CALLBACKTARGET and __CALLBACKARGUMENT insted __CALLBACKPARAM
371                 public void ClientScriptManager_RegisterForEventValidation_1 ()
372                 {
373                         WebTest t = new WebTest ("EventValidationTest1.aspx");
374                         string html = t.Run ();
375                         
376                         FormRequest fr = new FormRequest (t.Response, "form1");
377                         fr.Controls.Add ("__EVENTTARGET");
378                         fr.Controls.Add ("__EVENTARGUMENT");
379                         fr.Controls.Add ("__CALLBACKTARGET");
380                         fr.Controls.Add ("__CALLBACKARGUMENT");
381                         fr.Controls["__EVENTTARGET"].Value = "";
382                         fr.Controls["__EVENTARGUMENT"].Value = "";
383                         fr.Controls ["__CALLBACKTARGET"].Value = "__Page";
384                         t.Request = fr;
385                         html = t.Run ();
386
387                         if(html.IndexOf("Correct event raised callback.")<0)
388                                 Assert.Fail ("RegisterForEventValidationFail#1");
389                 }
390
391                 [Test]
392                 [Category ("NunitWeb")]
393                 [Category ("NotDotNet")] // for dot-net use __CALLBACKID insted __CALLBACKTARGET and __CALLBACKPARAM insted __CALLBACKARGUMENT
394                 public void ClientScriptManager_RegisterForEventValidation_2 ()
395                 {
396                         WebTest t = new WebTest ("EventValidationTest2.aspx");
397                         string html = t.Run ();
398                         
399                         FormRequest fr = new FormRequest (t.Response, "form1");
400                         fr.Controls.Add ("__EVENTTARGET");
401                         fr.Controls.Add ("__EVENTARGUMENT");
402                         fr.Controls.Add ("__CALLBACKTARGET");
403                         fr.Controls.Add ("__CALLBACKARGUMENT");
404                         fr.Controls["__EVENTTARGET"].Value = "";
405                         fr.Controls["__EVENTARGUMENT"].Value = "";
406                         fr.Controls ["__CALLBACKTARGET"].Value = "__Page";
407                         t.Request = fr;
408                         html = t.Run ();
409
410                         if (html.IndexOf ("Incorrect event raised callback.") < 0)
411                                 Assert.Fail ("RegisterForEventValidationFail#2");
412                 }
413
414                 // Expected Exceptions
415
416                 [Test]
417                 [ExpectedException (typeof (InvalidOperationException))]
418                 public void ClientScriptManager_RegisterForEventValidationException ()
419                 {
420                         Page p = new Page ();
421                         ClientScriptManager cs = p.ClientScript;
422                         cs.RegisterForEventValidation ("ID", "args");
423                 }
424
425                 
426                 [Test]
427                 [ExpectedException (typeof (ArgumentException))]
428                 public void ClientScriptManager_ValidateEventException_1 ()
429                 {
430                         Page p = new Page ();
431                         ClientScriptManager cs = p.ClientScript;
432                         cs.ValidateEvent ("Exception");
433                 }
434
435                 
436                 [Test]
437                 [ExpectedException (typeof (ArgumentException))]
438                 public void ClientScriptManager_ValidateEventException_2 ()
439                 {
440                         Page p = new Page ();
441                         ClientScriptManager cs = p.ClientScript;
442                         cs.ValidateEvent ("Exception", "args");
443                 }
444
445
446                 [Test]
447                 [ExpectedException (typeof (ArgumentNullException))]
448                 public void ClientScriptManager_IsRegisterStartupScriptException ()
449                 {
450                         Page p = new Page ();
451                         String csname1 = "PopupScript";
452                         ClientScriptManager cs = p.ClientScript;
453                         cs.RegisterStartupScript (null, csname1, "");
454                 }
455                 
456                 [Test]
457                 [ExpectedException (typeof (ArgumentNullException))]
458                 public void ClientScriptManager_RegisterOnSubmitStatementException ()
459                 {
460                         Page p = new Page ();
461                         String csname = "OnSubmitScript";
462                         ClientScriptManager cs = p.ClientScript;
463                         String cstext = "document.write('Text from OnSubmit statement');";
464                         cs.RegisterOnSubmitStatement (null, csname, cstext);
465                 }
466
467                 [Test]
468                 [ExpectedException (typeof (ArgumentNullException))]
469                 public void ClientScriptManager_RegisterClientScriptIncludeException_1 ()
470                 {
471                         Page p = new Page ();
472                         String csname = "ButtonClickScript";
473                         Type cstype = p.GetType ();
474                         String csurl = "";
475                         ClientScriptManager cs = p.ClientScript;
476                         cs.RegisterClientScriptInclude (null, csname, csurl);
477                         bool registry = cs.IsClientScriptIncludeRegistered (csname);
478                 }
479
480                 [Test]
481                 [ExpectedException (typeof (ArgumentNullException))]
482                 public void ClientScriptManager_RegisterClientScriptIncludeException_2 ()
483                 {
484                         Page p = new Page ();
485                         String csname = "ButtonClickScript";
486                         String csurl = "ClientScript.js";
487                         ClientScriptManager cs = p.ClientScript;
488                         cs.RegisterClientScriptInclude (null, csname, csurl);
489                         bool registry = cs.IsClientScriptIncludeRegistered (csname);
490                 }
491
492                 [Test]
493                 [ExpectedException (typeof (ArgumentNullException))]
494                 public void ClientScriptManager_ClientScriptBlockRegisterException_2 ()
495                 {
496                         Page p = new Page ();
497                         ClientScriptManager cs = p.ClientScript;
498                         String csname2 = "ButtonClickScript";
499                         cs.RegisterClientScriptBlock (null, csname2, "");
500                 }
501
502
503                 [Test]
504                 [ExpectedException (typeof (ArgumentNullException))]
505                 public void ClientScriptManager_GetWebResourceUrlException_1 ()
506                 {
507                         Page p = new Page ();
508                         ClientScriptManager cs = p.ClientScript;
509                         String cbReference = cs.GetWebResourceUrl (null, "test");
510                 }
511
512                 [Test]
513                 [ExpectedException (typeof (ArgumentNullException))]
514                 public void ClientScriptManager_GetWebResourceUrlException_2 ()
515                 {
516                         Page p = new Page ();
517                         ClientScriptManager cs = p.ClientScript;
518                         String cbReference = cs.GetWebResourceUrl (typeof (ClientScriptManagerTest), "");
519                 }
520
521                 [Test]
522                 [ExpectedException (typeof (InvalidOperationException))]
523                 public void ClientScriptManager_GetCallbackEventReferenceException_1 ()
524                 {
525                         Page p = new Page ();
526                         ClientScriptManager cs = p.ClientScript;
527
528                         // Define callback references.
529                         String cbReference = cs.GetCallbackEventReference (p, "arg",
530                             "ReceiveServerData1", "");
531                 }
532
533                 [Test]
534                 [ExpectedException (typeof (ArgumentNullException))]
535                 public void ClientScriptManager_GetCallbackEventReferenceException_2 ()
536                 {
537                         Page p = new Page ();
538                         ClientScriptManager cs = p.ClientScript;
539
540                         // Define callback references.
541                         String cbReference = cs.GetCallbackEventReference (null, "arg",
542                             "ReceiveServerData1", "");
543                 }
544
545                 [TestFixtureTearDown]
546                 public void Unload()
547                 {
548                         WebTest.Unload();
549                 }
550         }
551 }
552 #endif
553
554
555