2 // Tests for System.Web.UI.ClientScriptManagerTest.cs
5 // Yoni Klein (yonik@mainsoft.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
35 using System.Web.UI.WebControls;
36 using NUnit.Framework;
37 using MonoTests.stand_alone.WebHarness;
38 using MonoTests.SystemWeb.Framework;
40 using System.Threading;
42 namespace MonoTests.System.Web.UI
45 public class MyPage : Page, ICallbackEventHandler
47 #region ICallbackEventHandler Members
49 public string GetCallbackResult ()
51 throw new Exception ("The method or operation is not implemented.");
54 public void RaiseCallbackEvent (string eventArgument)
56 throw new Exception ("The method or operation is not implemented.");
63 public class ClientScriptManagerTest
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");
72 WebTest.CopyResource (GetType (), "EventValidationTest1.aspx", "EventValidationTest1.aspx");
73 WebTest.CopyResource (GetType (), "EventValidationTest2.aspx", "EventValidationTest2.aspx");
78 public void SetupTestCase ()
84 public void ClientScriptManager_GetCallbackEventReference_1 ()
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 ("}");
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");
102 public void ClientScriptManager_GetCallbackEventReference_2 ()
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 ("}");
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");
120 public void ClientScriptManager_GetCallbackEventReference_3 ()
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 ("}");
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");
138 public void ClientScriptManager_GetPostBackEventReference_1 ()
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");
147 public void ClientScriptManager_GetPostBackEventReference_2 ()
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");
156 public void ClientScriptManager_GetPostBackClientHyperlink ()
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");
165 [Category("NunitWeb")]
166 public void ClientScriptManager_GetWebResourceUrl ()
168 string html = new WebTest (PageInvoker.CreateOnLoad (GetWebResourceUrlLoad)).Run();
171 public static void GetWebResourceUrlLoad (Page p)
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");
180 public void ClientScriptManager_IsClientScriptBlockRegistered ()
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");
195 public void ClientScriptManager_IsRegisterClientScriptInclude ()
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");
208 public void ClientScriptManager_IsRegisterOnSubmitStatement ()
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");
220 [Category ("NunitWeb")]
221 [Category ("NotWorking")] // implementation specific
222 public void ClientScriptManager_RegisterOnSubmitStatement ()
224 WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterOnSubmitStatement));
225 string html = t.Run ();
226 if (html.IndexOf ("WebForm_OnSubmit()") < 0)
227 Assert.Fail ("RegisterOnSubmitStatement");
230 public static void RegisterOnSubmitStatement (Page p)
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);
240 [Category ("NunitWeb")]
241 public void ClientScriptManager_RegisterClientScriptInclude ()
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");
249 public static void RegisterClientScriptInclude (Page p)
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);
259 [Category("NunitWeb")]
260 public void ClientScriptManager_ClientScriptBlockRegister()
262 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClientScriptBlockRegister));
263 string html = t.Run ();
264 if( html.IndexOf("DoClick()") < 0)
265 Assert.Fail ("ClientScriptBlockRegisterFail#2");
268 public static void ClientScriptBlockRegister (Page p)
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 ());
281 public void ClientScriptManager_IsRegisterStartupScript ()
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");
293 [Category ("NunitWeb")]
294 public void ClientScriptManager_RegisterStartupScript ()
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");
302 public static void RegisterStartupScript (Page p)
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);
312 [Category ("NunitWeb")]
313 public void ClientScriptManager_RegisterArrayDeclaration ()
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");
321 public static void RegisterArrayDeclaration (Page p)
323 Type cstype = p.GetType ();
324 ClientScriptManager cs = p.ClientScript;
326 String arrName = "MyArray";
327 String arrValue = "\"1\", \"2\", \"text\"";
329 // Register the array with the Page class.
330 cs.RegisterArrayDeclaration (arrName, arrValue);
334 [Category ("NunitWeb")]
335 public void ClientScriptManager_RegisterExpandAttribute ()
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");
343 public static void RegisterExpandAttribute (Page p)
345 ClientScriptManager cs = p.ClientScript;
346 cs.RegisterExpandoAttribute ("Message", "title", "New title from client script.", true);
350 [Category ("NunitWeb")]
351 public void ClientScriptManager_RegisterHiddenField ()
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");
359 public static void RegisterHiddenField (Page p)
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);
370 [Category ("NunitWeb")]
371 [Category ("NotDotNet")] // for dot-net use __CALLBACKID insted __CALLBACKTARGET and __CALLBACKARGUMENT insted __CALLBACKPARAM
372 public void ClientScriptManager_RegisterForEventValidation_1 ()
374 WebTest t = new WebTest ("EventValidationTest1.aspx");
375 string html = t.Run ();
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";
388 if(html.IndexOf("Correct event raised callback.")<0)
389 Assert.Fail ("RegisterForEventValidationFail#1");
393 [Category ("NunitWeb")]
394 [Category ("NotDotNet")] // for dot-net use __CALLBACKID insted __CALLBACKTARGET and __CALLBACKPARAM insted __CALLBACKARGUMENT
395 public void ClientScriptManager_RegisterForEventValidation_2 ()
397 WebTest t = new WebTest ("EventValidationTest2.aspx");
398 string html = t.Run ();
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";
411 if (html.IndexOf ("Incorrect event raised callback.") < 0)
412 Assert.Fail ("RegisterForEventValidationFail#2");
415 // Expected Exceptions
418 [ExpectedException (typeof (InvalidOperationException))]
419 public void ClientScriptManager_RegisterForEventValidationException ()
421 Page p = new Page ();
422 ClientScriptManager cs = p.ClientScript;
423 cs.RegisterForEventValidation ("ID", "args");
428 [ExpectedException (typeof (ArgumentException))]
429 public void ClientScriptManager_ValidateEventException_1 ()
431 Page p = new Page ();
432 ClientScriptManager cs = p.ClientScript;
433 cs.ValidateEvent ("Exception");
438 [ExpectedException (typeof (ArgumentException))]
439 public void ClientScriptManager_ValidateEventException_2 ()
441 Page p = new Page ();
442 ClientScriptManager cs = p.ClientScript;
443 cs.ValidateEvent ("Exception", "args");
448 [ExpectedException (typeof (ArgumentNullException))]
449 public void ClientScriptManager_IsRegisterStartupScriptException ()
451 Page p = new Page ();
452 String csname1 = "PopupScript";
453 ClientScriptManager cs = p.ClientScript;
454 cs.RegisterStartupScript (null, csname1, "");
458 [ExpectedException (typeof (ArgumentNullException))]
459 public void ClientScriptManager_RegisterOnSubmitStatementException ()
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);
469 [ExpectedException (typeof (ArgumentNullException))]
470 public void ClientScriptManager_RegisterClientScriptIncludeException_1 ()
472 Page p = new Page ();
473 String csname = "ButtonClickScript";
474 Type cstype = p.GetType ();
476 ClientScriptManager cs = p.ClientScript;
477 cs.RegisterClientScriptInclude (null, csname, csurl);
478 bool registry = cs.IsClientScriptIncludeRegistered (csname);
482 [ExpectedException (typeof (ArgumentNullException))]
483 public void ClientScriptManager_RegisterClientScriptIncludeException_2 ()
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);
494 [ExpectedException (typeof (ArgumentNullException))]
495 public void ClientScriptManager_ClientScriptBlockRegisterException_2 ()
497 Page p = new Page ();
498 ClientScriptManager cs = p.ClientScript;
499 String csname2 = "ButtonClickScript";
500 cs.RegisterClientScriptBlock (null, csname2, "");
505 [ExpectedException (typeof (ArgumentNullException))]
506 public void ClientScriptManager_GetWebResourceUrlException_1 ()
508 Page p = new Page ();
509 ClientScriptManager cs = p.ClientScript;
510 String cbReference = cs.GetWebResourceUrl (null, "test");
514 [ExpectedException (typeof (ArgumentNullException))]
515 public void ClientScriptManager_GetWebResourceUrlException_2 ()
517 Page p = new Page ();
518 ClientScriptManager cs = p.ClientScript;
519 String cbReference = cs.GetWebResourceUrl (typeof (ClientScriptManagerTest), "");
523 [ExpectedException (typeof (InvalidOperationException))]
524 public void ClientScriptManager_GetCallbackEventReferenceException_1 ()
526 Page p = new Page ();
527 ClientScriptManager cs = p.ClientScript;
529 // Define callback references.
530 String cbReference = cs.GetCallbackEventReference (p, "arg",
531 "ReceiveServerData1", "");
535 [ExpectedException (typeof (ArgumentNullException))]
536 public void ClientScriptManager_GetCallbackEventReferenceException_2 ()
538 Page p = new Page ();
539 ClientScriptManager cs = p.ClientScript;
541 // Define callback references.
542 String cbReference = cs.GetCallbackEventReference (null, "arg",
543 "ReceiveServerData1", "");
546 [TestFixtureTearDown]