// // Tests for System.Web.UI.ClientScriptManagerTest.cs // // Author: // Yoni Klein (yonik@mainsoft.com) // // // Copyright (C) 2005 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using NUnit.Framework; using MonoTests.stand_alone.WebHarness; using MonoTests.SystemWeb.Framework; using System.Text; using System.Threading; namespace MonoTests.System.Web.UI { public class MyPage : Page, ICallbackEventHandler { #region ICallbackEventHandler Members public string GetCallbackResult () { throw new Exception ("The method or operation is not implemented."); } public void RaiseCallbackEvent (string eventArgument) { throw new Exception ("The method or operation is not implemented."); } #endregion } [TestFixture] public class ClientScriptManagerTest { [TestFixtureSetUp] public void Set_Up () { WebTest.CopyResource (GetType (), "EventValidationTest1.aspx", "EventValidationTest1.aspx"); WebTest.CopyResource (GetType (), "EventValidationTest2.aspx", "EventValidationTest2.aspx"); } [SetUp] public void SetupTestCase () { Thread.Sleep (100); } [Test] public void ClientScriptManager_GetCallbackEventReference_1 () { MyPage p = new MyPage (); ClientScriptManager cs = p.ClientScript; StringBuilder func = new StringBuilder (); func.Append ("function ReceiveServerData1(arg, context)"); func.Append ("{"); func.Append ("Message1.innerText = arg;"); func.Append ("value1 = arg;"); func.Append ("}"); // Define callback references. String cbReference = cs.GetCallbackEventReference (p, "callArg", func.ToString (), "ReceiveServerData1Ctx"); Assert.IsTrue (cbReference.IndexOf ("callArg") != -1, "GetCallbackEventReferenceFail1_arg"); Assert.IsTrue (cbReference.IndexOf (func.ToString ()) != -1, "GetCallbackEventReferenceFail1_callback"); Assert.IsTrue (cbReference.IndexOf ("ReceiveServerData1Ctx") != -1, "GetCallbackEventReferenceFail1_context"); } [Test] public void ClientScriptManager_GetCallbackEventReference_2 () { MyPage p = new MyPage (); ClientScriptManager cs = p.ClientScript; StringBuilder func = new StringBuilder (); func.Append ("function ReceiveServerData1(arg, context)"); func.Append ("{"); func.Append ("Message1.innerText = arg;"); func.Append ("value1 = arg;"); func.Append ("}"); // Define callback references. String cbReference = cs.GetCallbackEventReference (p, "callArg", func.ToString (), "ReceiveServerData1Ctx", true); Assert.IsTrue (cbReference.IndexOf ("callArg") != -1, "GetCallbackEventReferenceFail2_arg"); Assert.IsTrue (cbReference.IndexOf (func.ToString ()) != -1, "GetCallbackEventReferenceFail2_callback"); Assert.IsTrue (cbReference.IndexOf ("ReceiveServerData1Ctx") != -1, "GetCallbackEventReferenceFail2_context"); String cbReference2 = cs.GetCallbackEventReference (p, "arg", func.ToString (), "ReceiveServerData1Ctx", false); // Check that we get different results when useAsync differs. Assert.IsTrue (cbReference != cbReference2, "GetCallbackEventReferenceFail2_useAsync"); } [Test] public void ClientScriptManager_GetCallbackEventReference_3 () { MyPage p = new MyPage (); ClientScriptManager cs = p.ClientScript; StringBuilder func = new StringBuilder (); func.Append ("function ReceiveServerData1(arg, context)"); func.Append ("{"); func.Append ("Message1.innerText = arg;"); func.Append ("value1 = arg;"); func.Append ("}"); // Define callback references. String cbReference = cs.GetCallbackEventReference (p, "callArg", func.ToString (), "ReceiveServerData1Ctx", "ErrorCallback", false); Assert.IsTrue (cbReference.IndexOf ("callArg") != -1, "GetCallbackEventReferenceFail3_arg"); Assert.IsTrue (cbReference.IndexOf (func.ToString ()) != -1, "GetCallbackEventReferenceFail3_callback"); Assert.IsTrue (cbReference.IndexOf ("ReceiveServerData1Ctx") != -1, "GetCallbackEventReferenceFail3_context"); Assert.IsTrue (cbReference.IndexOf ("ErrorCallback") != -1, "GetCallbackEventReferenceFail3_errorCallback"); } [Test] public void ClientScriptManager_GetPostBackEventReference_1 () { MyPage p = new MyPage (); ClientScriptManager cs = p.ClientScript; String result = cs.GetPostBackEventReference (new PostBackOptions (p, "args1")); Assert.IsTrue (result.IndexOf(p.ClientID) != -1, "GetPostBackEventReference#1_targetEvent"); Assert.IsTrue (result.IndexOf("args1") != -1, "GetPostBackEventReference#1_targetArgs"); } [Test] public void ClientScriptManager_GetPostBackEventReference_2 () { MyPage p = new MyPage (); ClientScriptManager cs = p.ClientScript; String result = cs.GetPostBackEventReference (p, "args1"); Assert.IsTrue (result.IndexOf(p.ClientID) != -1, "GetPostBackEventReference#2_targetEvent"); Assert.IsTrue (result.IndexOf("args1") != -1, "GetPostBackEventReference#2_targetArgs"); } [Test] public void ClientScriptManager_GetPostBackClientHyperlink () { MyPage p = new MyPage (); ClientScriptManager cs = p.ClientScript; String hyperlink = cs.GetPostBackClientHyperlink (p, "args1"); Assert.IsTrue (hyperlink.IndexOf("javascript:") != -1, "GetPostBackClientHyperlink_javaScript"); Assert.IsTrue (hyperlink.IndexOf(p.ClientID) != -1, "GetPostBackClientHyperlink_targetEvent"); Assert.IsTrue (hyperlink.IndexOf("args1") != -1, "GetPostBackClientHyperlink_targetArgs"); } [Test] [Category("NunitWeb")] public void ClientScriptManager_GetWebResourceUrl () { string html = new WebTest (PageInvoker.CreateOnLoad (GetWebResourceUrlLoad)).Run(); } public static void GetWebResourceUrlLoad (Page p) { ClientScriptManager cs = p.ClientScript; String cbReference = cs.GetWebResourceUrl (typeof (MonoTests.System.Web.UI.ClientScriptManagerTest), "ClientScript.js"); if (cbReference.IndexOf("/WebResource.axd?")<0) Assert.Fail ("GetWebResourceUrlFail"); } [Test] public void ClientScriptManager_IsClientScriptBlockRegistered () { Page p = new Page (); ClientScriptManager cs = p.ClientScript; String csname2 = "ButtonClickScript"; Type cstype = p.GetType (); StringBuilder cstext2 = new StringBuilder (); cstext2.Append ("