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 (), "EventValidationTest1.aspx", "EventValidationTest1.aspx");
70 WebTest.CopyResource (GetType (), "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("/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 public void ClientScriptManager_RegisterOnSubmitStatement ()
223 WebTest t = new WebTest (PageInvoker.CreateOnLoad (RegisterOnSubmitStatement));
224 string html = t.Run ();
225 if (html.IndexOf ("onsubmit") < 0)
226 Assert.Fail ("RegisterOnSubmitStatement");
229 public static void RegisterOnSubmitStatement (Page p)
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);
239 [Category ("NunitWeb")]
240 public void ClientScriptManager_RegisterClientScriptInclude ()
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");
248 public static void RegisterClientScriptInclude (Page p)
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);
258 [Category("NunitWeb")]
259 public void ClientScriptManager_ClientScriptBlockRegister()
261 WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClientScriptBlockRegister));
262 string html = t.Run ();
263 if( html.IndexOf("DoClick()") < 0)
264 Assert.Fail ("ClientScriptBlockRegisterFail#2");
267 public static void ClientScriptBlockRegister (Page p)
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 ());
280 public void ClientScriptManager_IsRegisterStartupScript ()
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");
292 [Category ("NunitWeb")]
293 public void ClientScriptManager_RegisterStartupScript ()
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");
301 public static void RegisterStartupScript (Page p)
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);
311 [Category ("NunitWeb")]
312 public void ClientScriptManager_RegisterArrayDeclaration ()
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");
320 public static void RegisterArrayDeclaration (Page p)
322 Type cstype = p.GetType ();
323 ClientScriptManager cs = p.ClientScript;
325 String arrName = "MyArray";
326 String arrValue = "\"1\", \"2\", \"text\"";
328 // Register the array with the Page class.
329 cs.RegisterArrayDeclaration (arrName, arrValue);
333 [Category ("NunitWeb")]
334 public void ClientScriptManager_RegisterExpandAttribute ()
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");
342 public static void RegisterExpandAttribute (Page p)
344 ClientScriptManager cs = p.ClientScript;
345 cs.RegisterExpandoAttribute ("Message", "title", "New title from client script.", true);
349 [Category ("NunitWeb")]
350 public void ClientScriptManager_RegisterHiddenField ()
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");
358 public static void RegisterHiddenField (Page p)
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);
369 [Category ("NunitWeb")]
370 [Category ("NotDotNet")] // for dot-net use __CALLBACKID insted __CALLBACKTARGET and __CALLBACKARGUMENT insted __CALLBACKPARAM
371 public void ClientScriptManager_RegisterForEventValidation_1 ()
373 WebTest t = new WebTest ("EventValidationTest1.aspx");
374 string html = t.Run ();
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";
387 if(html.IndexOf("Correct event raised callback.")<0)
388 Assert.Fail ("RegisterForEventValidationFail#1");
392 [Category ("NunitWeb")]
393 [Category ("NotDotNet")] // for dot-net use __CALLBACKID insted __CALLBACKTARGET and __CALLBACKPARAM insted __CALLBACKARGUMENT
394 public void ClientScriptManager_RegisterForEventValidation_2 ()
396 WebTest t = new WebTest ("EventValidationTest2.aspx");
397 string html = t.Run ();
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";
410 if (html.IndexOf ("Incorrect event raised callback.") < 0)
411 Assert.Fail ("RegisterForEventValidationFail#2");
414 // Expected Exceptions
417 [ExpectedException (typeof (InvalidOperationException))]
418 public void ClientScriptManager_RegisterForEventValidationException ()
420 Page p = new Page ();
421 ClientScriptManager cs = p.ClientScript;
422 cs.RegisterForEventValidation ("ID", "args");
427 [ExpectedException (typeof (ArgumentException))]
428 public void ClientScriptManager_ValidateEventException_1 ()
430 Page p = new Page ();
431 ClientScriptManager cs = p.ClientScript;
432 cs.ValidateEvent ("Exception");
437 [ExpectedException (typeof (ArgumentException))]
438 public void ClientScriptManager_ValidateEventException_2 ()
440 Page p = new Page ();
441 ClientScriptManager cs = p.ClientScript;
442 cs.ValidateEvent ("Exception", "args");
447 [ExpectedException (typeof (ArgumentNullException))]
448 public void ClientScriptManager_IsRegisterStartupScriptException ()
450 Page p = new Page ();
451 String csname1 = "PopupScript";
452 ClientScriptManager cs = p.ClientScript;
453 cs.RegisterStartupScript (null, csname1, "");
457 [ExpectedException (typeof (ArgumentNullException))]
458 public void ClientScriptManager_RegisterOnSubmitStatementException ()
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);
468 [ExpectedException (typeof (ArgumentNullException))]
469 public void ClientScriptManager_RegisterClientScriptIncludeException_1 ()
471 Page p = new Page ();
472 String csname = "ButtonClickScript";
473 Type cstype = p.GetType ();
475 ClientScriptManager cs = p.ClientScript;
476 cs.RegisterClientScriptInclude (null, csname, csurl);
477 bool registry = cs.IsClientScriptIncludeRegistered (csname);
481 [ExpectedException (typeof (ArgumentNullException))]
482 public void ClientScriptManager_RegisterClientScriptIncludeException_2 ()
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);
493 [ExpectedException (typeof (ArgumentNullException))]
494 public void ClientScriptManager_ClientScriptBlockRegisterException_2 ()
496 Page p = new Page ();
497 ClientScriptManager cs = p.ClientScript;
498 String csname2 = "ButtonClickScript";
499 cs.RegisterClientScriptBlock (null, csname2, "");
504 [ExpectedException (typeof (ArgumentNullException))]
505 public void ClientScriptManager_GetWebResourceUrlException_1 ()
507 Page p = new Page ();
508 ClientScriptManager cs = p.ClientScript;
509 String cbReference = cs.GetWebResourceUrl (null, "test");
513 [ExpectedException (typeof (ArgumentNullException))]
514 public void ClientScriptManager_GetWebResourceUrlException_2 ()
516 Page p = new Page ();
517 ClientScriptManager cs = p.ClientScript;
518 String cbReference = cs.GetWebResourceUrl (typeof (ClientScriptManagerTest), "");
522 [ExpectedException (typeof (InvalidOperationException))]
523 public void ClientScriptManager_GetCallbackEventReferenceException_1 ()
525 Page p = new Page ();
526 ClientScriptManager cs = p.ClientScript;
528 // Define callback references.
529 String cbReference = cs.GetCallbackEventReference (p, "arg",
530 "ReceiveServerData1", "");
534 [ExpectedException (typeof (ArgumentNullException))]
535 public void ClientScriptManager_GetCallbackEventReferenceException_2 ()
537 Page p = new Page ();
538 ClientScriptManager cs = p.ClientScript;
540 // Define callback references.
541 String cbReference = cs.GetCallbackEventReference (null, "arg",
542 "ReceiveServerData1", "");
545 [TestFixtureTearDown]