public void TestEventHandler() { var loadType = this.asm.GetType("DotWeb.Hosting.Test.Script.EventHandlerTest"); var alertArg = (string)loadType.GetField("AlertArg").GetValue(null); CachingObjectFactory factory = new CachingObjectFactory(); TestHelper(factory, (SessionHelper session) => { session.OnLoadMessage(loadType); // new Nativeobject(); var ctor = session.DefineFunctionMessage(this.nativeObject.GetConstructor(Type.EmptyTypes)); session.InvokeFunctionMessage(ctor.Name, 0); // return the scopeId for the newly created native object session.OnReturnMessage(false, JsValueType.JsObject, 1); // nativeObject.onmouseover = native_OnMouseOver; var method = session.DefineFunctionMessage(this.nativeObject.GetMethod("set_onmouseover")); session.InvokeFunctionMessage(method.Name, 1, new JsValue(JsValueType.Delegate, 1)); session.OnReturnMessage(false, JsValueType.Void, null); // return from initial ctor session.ReturnMessage(); // simulate an event firing session.OnInvokeDelegateMessage(1, new JsValue(JsValueType.JsObject, 2)); // nativeObject.Alert(); var alert = session.DefineFunctionMessage(this.nativeObject.GetMethod("Alert")); session.InvokeFunctionMessage(alert.Name, 1, new JsValue(alertArg)); session.OnReturnMessage(false, JsValueType.Void, null); // return from event handler session.ReturnMessage(); session.OnQuitMessage(); }); //EventHandlerTest test = (EventHandlerTest)factory.Get(typeof(EventHandlerTest)); //Assert.IsTrue(test.HasFired); }
public void TestNativeCallback() { // Let's say the native js code looked like: //function NativeCaller(config) { // for(var key in config){ // this[key] = config[key]; // } //}; //NativeCaller.prototype.Start = function() { // this.nativeObject.NativeCall(); //}; //function NativeObject() { //}; //NativeObject.prototype.NativeCall = function() { // alert('OK'); //}; CachingObjectFactory factory = new CachingObjectFactory(); int localId = 0; int remoteId = 0; var loadType = this.asm.GetType("DotWeb.Hosting.Test.Script.NativeCallbackTest"); var nativeCaller = this.asm.GetType("DotWeb.Hosting.Test.Script.NativeCaller"); var configType = this.asm.GetType("DotWeb.Hosting.Test.Script.Config"); var cfg = Activator.CreateInstance(configType); TestHelper(factory, (SessionHelper session) => { session.OnLoadMessage(loadType); // new Nativeobject(); var ctor = session.DefineFunctionMessage(this.nativeObject.GetConstructor(Type.EmptyTypes)); session.InvokeFunctionMessage(ctor.Name, 0); // return the scopeId for the newly created native object var nativeObjectId = ++remoteId; session.OnReturnMessage(false, JsValueType.JsObject, nativeObjectId); //var caller = new NativeCaller(cfg); var ctor2 = session.DefineFunctionMessage(nativeCaller.GetConstructor(new Type[] { typeof(object) })); var cfgId = ++localId; session.InvokeFunctionMessage(ctor2.Name, 0, new JsValue(JsValueType.Object, cfgId)); // the ctor for NativeCaller() will grab the nativeObject from the cfg passed in session.OnGetTypeRequestMessage(cfgId); session.GetTypeResponseMessage(session.Bridge, cfg); session.OnInvokeMemberMessage(cfgId, new DispatchIdentifier("nativeObject"), DispatchType.PropertyGet); // give back the nativeObject handle session.ReturnMessage(new JsValue(JsValueType.JsObject, nativeObjectId)); // return the scopeId for the newly created native object var nativeCallerId = ++remoteId; session.OnReturnMessage(false, JsValueType.JsObject, nativeCallerId); //caller.Start(); var method = session.DefineFunctionMessage(nativeCaller.GetMethod("Start")); session.InvokeFunctionMessage(method.Name, nativeCallerId); session.OnReturnMessage(false, JsValueType.Void, null); // return from initial ctor session.ReturnMessage(); session.OnQuitMessage(); }); }
public void TestDelegateWrapper() { //var ctor = NativeObject.Constructor; //NativeObject.TakeObject(ctor); var loadType = this.asm.GetType("DotWeb.Hosting.Test.Script.DelegateWrapperTest"); int remoteId = 0; CachingObjectFactory factory = new CachingObjectFactory(); TestHelper(factory, (SessionHelper session) => { session.OnLoadMessage(loadType); var ctor = session.DefineFunctionMessage(this.nativeObject.GetMethod("get_Constructor")); session.InvokeFunctionMessage(ctor.Name, 0); var ctorId = ++remoteId; session.OnReturnMessage(false, JsValueType.JsObject, ctorId); var method = session.DefineFunctionMessage(this.nativeObject.GetMethod("TakeObject")); session.InvokeFunctionMessage(method.Name, 0, new JsValue(JsValueType.JsObject, ctorId)); session.OnReturnMessage(false, JsValueType.Void, null); SimulateAction(session, this.nativeObject, "GetAction0", JsValueType.Void, ref remoteId); SimulateAction(session, this.nativeObject, "GetAction1", JsValueType.Void, ref remoteId, 1); SimulateAction(session, this.nativeObject, "GetAction2", JsValueType.Void, ref remoteId, 1, 2); SimulateAction(session, this.nativeObject, "GetFunc0", JsValueType.Null, ref remoteId); SimulateAction(session, this.nativeObject, "GetFunc1", JsValueType.Null, ref remoteId, 1); SimulateAction(session, this.nativeObject, "GetFunc2", JsValueType.Null, ref remoteId, 1, 2); // return from initial ctor session.ReturnMessage(); session.OnQuitMessage(); }); }