Revise ClientScripting protocol
namespace now is rwt.scripting
renamed "Listener" to "Function"
added obligatory "name" property to Function creation
diff --git a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/EventBinding.js b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/EventBinding.js
index 3290f89..2b77613 100644
--- a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/EventBinding.js
+++ b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/EventBinding.js
@@ -19,9 +19,7 @@
this._eventType = eventType;
this._source = source;
this._public = ClientScriptingUtil.isPublicObject( source );
- this._targetFunction = function() {
- targetFunction.call.apply( targetFunction, arguments );
- };
+ this._targetFunction = targetFunction;
if( this._public ) {
this._eventSource = source;
this._nativeType = eventType;
diff --git a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/Function.js b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/Function.js
deleted file mode 100644
index 44665de..0000000
--- a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/Function.js
+++ /dev/null
@@ -1,44 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2012 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * EclipseSource - initial API and implementation
- ******************************************************************************/
-
-rwt.qx.Class.createNamespace( "rwt.scripting", {} );
-
-// TODO [tb] : consider a name thats not a native Constructor ( "Function" )
-
-/*global handleEvent:false */
-rwt.scripting.Function = function( /* code */ ) {
- // NOTE: the eval'd code will have the same scope as this function, therefore no local
- // variables except the "imports" are used.
- // TODO [tb] : It would be cleaner if the "import" was part of the eval'd code
- var SWT = rwt.scripting.SWT;
- try {
- eval( arguments[ 0 ] );
- } catch( ex ) {
- var msg = "Could not parse ClientFunction: " + ( ex.message ? ex.message : ex );
- throw new Error( msg );
- }
- try {
- this._function = handleEvent; // TODO [tb] : allow specific function name(s)
- } catch( ex ) {
- // handled in next if
- }
- if( typeof this._function !== "function" ) {
- throw new Error( "JavaScript code does not define a \"handleEvent\" function" );
- }
-};
-
-rwt.scripting.Function.prototype = {
-
- call : function() {
- this._function.apply( window, arguments );
- }
-
-};
diff --git a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/FunctionFactory.js b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/FunctionFactory.js
new file mode 100644
index 0000000..ffcad5a
--- /dev/null
+++ b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/FunctionFactory.js
@@ -0,0 +1,58 @@
+/*******************************************************************************
+ * Copyright (c) 2012 EclipseSource and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * EclipseSource - initial API and implementation
+ ******************************************************************************/
+
+rwt.qx.Class.createNamespace( "rwt.scripting", {} );
+
+(function(){
+
+/*global handleEvent:false */
+rwt.scripting.FunctionFactory = {
+
+ createFunction : function( functionScript, name, scope ) {
+ var result;
+ var code = [
+ this._getScopeScript( scope ),
+ functionScript,
+ "\n\n",
+ "typeof ",
+ name,
+ " === \"undefined\" ? null : ",
+ name,
+ ";" ];
+ try {
+ result = this._secureEval.apply( window, [ code.join( "" ) ] );
+ } catch( ex ) {
+ var msg = "Could not parse Script for " + name + ":" + ( ex.message ? ex.message : ex );
+ throw new Error( msg );
+ }
+ if( typeof result !== "function" ) {
+ throw new Error( "Script does not define a function " + name );
+ }
+ return result;
+ },
+
+ _secureEval : function() {
+ return eval( arguments[ 0 ] );
+ },
+
+ _getScopeScript : function( scope ) {
+ var result = [];
+ for( var key in scope ) {
+ // NOTE: currently the values are evaluated "as is", i.e. json string.
+ // Support for protocol objects would be possible, but may require "evalInScope" feature
+ result.push( "var ", key, " = ", scope[ key ], ";\n" );
+ }
+ return result.join( "" );
+ }
+
+};
+
+}());
diff --git a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/EventBindingHandler.js b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/EventBindingHandler.js
index ebb8256..496b42f 100644
--- a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/EventBindingHandler.js
+++ b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/EventBindingHandler.js
@@ -11,7 +11,7 @@
(function(){
-rwt.remote.HandlerRegistry.add( "rwt.clientscripting.EventBinding", {
+rwt.remote.HandlerRegistry.add( "rwt.scripting.EventBinding", {
factory : function( properties ) {
var ObjectRegistry = rwt.remote.ObjectRegistry;
@@ -26,4 +26,4 @@
} );
-}());
\ No newline at end of file
+}());
diff --git a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/ListenerHandler.js b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/FunctionHandler.js
similarity index 76%
rename from bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/ListenerHandler.js
rename to bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/FunctionHandler.js
index d1963f6..07bf8ea 100644
--- a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/ListenerHandler.js
+++ b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/FunctionHandler.js
@@ -9,14 +9,16 @@
* EclipseSource - initial API and implementation
******************************************************************************/
-rwt.remote.HandlerRegistry.add( "rwt.clientscripting.Listener", {
+rwt.remote.HandlerRegistry.add( "rwt.scripting.Function", {
factory : function( properties ) {
var scriptCode = properties.scriptCode;
if( !scriptCode ) {
scriptCode = rap.getObject( properties.scriptId ).getText();
}
- return new rwt.scripting.Function( scriptCode );
+ var name = properties.name;
+ var scope = { "SWT" : "rwt.scripting.SWT" };
+ return rwt.scripting.FunctionFactory.createFunction( scriptCode, name, scope );
}
} );
diff --git a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/ScriptHandler.js b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/ScriptHandler.js
index edb2326..aa77121 100644
--- a/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/ScriptHandler.js
+++ b/bundles/org.eclipse.rap.clientscripting/js/rwt/scripting/handler/ScriptHandler.js
@@ -11,7 +11,7 @@
(function(){
-rap.registerTypeHandler( "rwt.clientscripting.Script", {
+rap.registerTypeHandler( "rwt.scripting.Script", {
factory : function( properties ) {
var text = properties.text;
diff --git a/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/Script.java b/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/Script.java
index 3b895c7..0de7710 100644
--- a/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/Script.java
+++ b/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/Script.java
@@ -17,7 +17,7 @@
public class Script {
- private static final String REMOTE_TYPE = "rwt.clientscripting.Script";
+ private static final String REMOTE_TYPE = "rwt.scripting.Script";
private final RemoteObject remoteObject;
public Script( String scriptCode ) {
diff --git a/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/ClientFunction.java b/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/ClientFunction.java
index 0e7d049..cb48315 100644
--- a/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/ClientFunction.java
+++ b/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/ClientFunction.java
@@ -21,7 +21,7 @@
public class ClientFunction {
- private static final String REMOTE_TYPE = "rwt.clientscripting.Listener";
+ private static final String REMOTE_TYPE = "rwt.scripting.Function";
private final RemoteObject remoteObject;
private final Collection<ClientListenerBinding> bindings;
@@ -46,6 +46,7 @@
bindings = new ArrayList<ClientListenerBinding>();
remoteObject = RWT.getUISession().getConnection().createRemoteObject( REMOTE_TYPE );
ClientScriptingResources.ensure();
+ remoteObject.set( "name", "handleEvent" );
}
protected String getRemoteId() {
diff --git a/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/ClientListenerBinding.java b/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/ClientListenerBinding.java
index 4dbcda0..da13bb6 100644
--- a/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/ClientListenerBinding.java
+++ b/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/ClientListenerBinding.java
@@ -16,7 +16,7 @@
public class ClientListenerBinding {
- private static final String REMOTE_TYPE = "rwt.clientscripting.EventBinding";
+ private static final String REMOTE_TYPE = "rwt.scripting.EventBinding";
private final ClientFunction function;
private final String targetId;
diff --git a/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/resources/ClientScriptingResources.java b/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/resources/ClientScriptingResources.java
index 31e137b..b5af539 100644
--- a/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/resources/ClientScriptingResources.java
+++ b/bundles/org.eclipse.rap.clientscripting/src/org/eclipse/rap/clientscripting/internal/resources/ClientScriptingResources.java
@@ -30,8 +30,8 @@
"handler/EventBindingHandler.js",
"EventBinding.js",
"EventProxy.js",
- "Function.js",
- "handler/ListenerHandler.js",
+ "FunctionFactory.js",
+ "handler/FunctionHandler.js",
"SWT.js",
"WidgetProxyFactory.js",
"handler/ScriptHandler.js",
diff --git a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/EventBinding_Test.js b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/EventBinding_Test.js
index 5931107..7f06b07 100644
--- a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/EventBinding_Test.js
+++ b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/EventBinding_Test.js
@@ -33,16 +33,17 @@
Processor.processOperation( {
"target" : "w4",
"action" : "create",
- "type" : "rwt.clientscripting.Listener",
+ "type" : "rwt.scripting.Function",
"properties" : {
- "scriptCode" : code
+ "scriptCode" : code,
+ "name" : "handleEvent"
}
} );
Processor.processOperation( {
"target" : "w5",
"action" : "create",
- "type" : "rwt.clientscripting.EventBinding",
+ "type" : "rwt.scripting.EventBinding",
"properties" : {
"eventType" : "KeyDown",
"targetObject" : "w3",
@@ -98,10 +99,8 @@
},
testDoItFalseKeyDown : function() {
- var listener = {
- "call" : function( event ) {
- event.doit = false;
- }
+ var listener = function( event ) {
+ event.doit = false;
};
new EventBinding( text, "KeyDown", listener );
@@ -112,10 +111,8 @@
},
testDoItFalseMouseDown : function() {
- var listener = {
- "call" : function( event ) {
- event.doit = false;
- }
+ var listener = function( event ) {
+ event.doit = false;
};
new EventBinding( text, "MouseDown", listener );
@@ -253,10 +250,8 @@
TestUtil.flush();
text.setValue( "foo" );
var textValue;
- var handler = {
- "call" : function( event ) {
- textValue = event.widget.getText();
- }
+ var handler = function( event ) {
+ textValue = event.widget.getText();
};
new EventBinding( text, "Verify", handler );
@@ -269,10 +264,8 @@
testVerifyEventDoItFalse : function() {
TestUtil.flush();
text.setValue( "foo" );
- var handler = {
- "call" : function( event ) {
- event.doit = false;
- }
+ var handler = function( event ) {
+ event.doit = false;
};
new EventBinding( text, "Verify", handler );
@@ -285,10 +278,8 @@
testVerifyEventDoItFalseSelection : function() {
TestUtil.flush();
text.setValue( "fooxxx" );
- var handler = {
- "call" : function( event ) {
- event.doit = false;
- }
+ var handler = function( event ) {
+ event.doit = false;
};
new EventBinding( text, "Verify", handler );
@@ -301,10 +292,8 @@
testVerifyBindingProtectAgainstTypeOverwrite : function() {
TestUtil.flush();
text.setValue( "foo" );
- var handler = {
- "call" : function( event ) {
- event.type = "boom";
- }
+ var handler = function( event ) {
+ event.type = "boom";
} ;
new EventBinding( text, "Verify", handler );
@@ -316,10 +305,8 @@
testVerifyEventTextOverwrite : function() {
TestUtil.flush();
text.setValue( "foo" );
- var handler = {
- "call" : function( event ) {
- event.text = "bar";
- }
+ var handler = function( event ) {
+ event.text = "bar";
};
new EventBinding( text, "Verify", handler );
@@ -331,10 +318,8 @@
testVerifyEventSelectionAfterTextOverwrite : function() {
TestUtil.flush();
text.setValue( "foo" );
- var handler = {
- "call" : function( event ) {
- event.text = "bar";
- }
+ var handler = function( event ) {
+ event.text = "bar";
} ;
new EventBinding( text, "Verify", handler );
@@ -348,10 +333,8 @@
testVerifyEventSelectionAfterReplacementTextOverwrite : function() {
TestUtil.flush();
text.setValue( "foo" );
- var handler = {
- "call" : function( event ) {
- event.text = "bar";
- }
+ var handler = function( event ) {
+ event.text = "bar";
} ;
new EventBinding( text, "Verify", handler );
@@ -366,12 +349,10 @@
TestUtil.flush();
text.setValue( "foo" );
var selection;
- var handler = {
- "call" : function( event ) {
- event.text = "bar";
- selection = event.widget.getSelection();
- }
- } ;
+ var handler = function( event ) {
+ event.text = "bar";
+ selection = event.widget.getSelection();
+ };
new EventBinding( text, "Verify", handler );
this._inputText( text, "foxo", [ 2, 2 ] );
@@ -385,11 +366,9 @@
TestUtil.flush();
text.setValue( "foo" );
var selection;
- var handler = {
- "call" : function( event ) {
- event.text = "bar";
- selection = event.widget.getSelection();
- }
+ var handler = function( event ) {
+ event.text = "bar";
+ selection = event.widget.getSelection();
} ;
new EventBinding( text, "Verify", handler );
@@ -519,12 +498,11 @@
// helper
_createLogger : function() {
- var result = {
- "log" : [],
- "call" : function( arg ) {
- this.log.push( arg ); // it's important that "this" is available, like in Function.js
- }
+ var log = [];
+ var result = function( arg ) {
+ log.push( arg );
};
+ result.log = log;
return result;
},
diff --git a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/EventProxy_Test.js b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/EventProxy_Test.js
index b3dab80..07b0f55 100644
--- a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/EventProxy_Test.js
+++ b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/EventProxy_Test.js
@@ -1,4 +1,4 @@
-/*******************************************************************************
+ /*******************************************************************************
* Copyright (c) 2012, 2013 EclipseSource and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -490,10 +490,8 @@
TestUtil.flush();
var gc;
- new EventBinding( canvas, "Paint", {
- "call" : function( ev ) {
- gc = ev.gc;
- }
+ new EventBinding( canvas, "Paint", function( ev ) {
+ gc = ev.gc;
} );
WidgetProxyFactory.getWidgetProxy( canvas ).redraw();
@@ -516,10 +514,8 @@
TestUtil.flush();
var gc = [];
- new EventBinding( canvas, "Paint", {
- "call" : function( ev ) {
- gc.push( ev.gc );
- }
+ new EventBinding( canvas, "Paint", function( ev ) {
+ gc.push( ev.gc );
} );
WidgetProxyFactory.getWidgetProxy( canvas ).redraw();
WidgetProxyFactory.getWidgetProxy( canvas ).redraw();
@@ -552,10 +548,8 @@
TestUtil.flush();
var gc;
- new EventBinding( canvas, "Paint", {
- "call" : function( ev ) {
- gc = ev.gc;
- }
+ new EventBinding( canvas, "Paint", function( ev ) {
+ gc = ev.gc;
} );
WidgetProxyFactory.getWidgetProxy( canvas ).redraw();
@@ -580,17 +574,15 @@
TestUtil.flush();
var props;
- new EventBinding( canvas, "Paint", {
- "call" : function( ev ) {
- var gc = ev.gc;
- props = [
- gc.strokeStyle,
- gc.fillStyle,
- rwt.html.Font.fromString( gc.font ).toCss()
- ];
- gc.strokeStyle = "#ff00ff";
- gc.fillStyle = "#00ff00";
- }
+ new EventBinding( canvas, "Paint", function( ev ) {
+ var gc = ev.gc;
+ props = [
+ gc.strokeStyle,
+ gc.fillStyle,
+ rwt.html.Font.fromString( gc.font ).toCss()
+ ];
+ gc.strokeStyle = "#ff00ff";
+ gc.fillStyle = "#00ff00";
} );
WidgetProxyFactory.getWidgetProxy( canvas ).redraw();
WidgetProxyFactory.getWidgetProxy( canvas ).redraw();
@@ -616,13 +608,11 @@
TestUtil.flush();
var props;
- new EventBinding( canvas, "Paint", {
- "call" : function( ev ) {
- var gc = ev.gc;
- props = [ gc.strokeStyle, gc.fillStyle ];
- gc.strokeStyle = "#ff00ff";
- gc.fillStyle = "#00ff00";
- }
+ new EventBinding( canvas, "Paint", function( ev ) {
+ var gc = ev.gc;
+ props = [ gc.strokeStyle, gc.fillStyle ];
+ gc.strokeStyle = "#ff00ff";
+ gc.fillStyle = "#00ff00";
} );
WidgetProxyFactory.getWidgetProxy( canvas ).redraw();
WidgetProxyFactory.getWidgetProxy( canvas ).redraw();
@@ -656,17 +646,15 @@
var fontArr = [ [ "Arial" ], 11, false, true ];
var props;
- new EventBinding( canvas, "Paint", {
- "call" : function( ev ) {
- var gc = ev.gc;
- props = [
- gc.strokeStyle,
- gc.fillStyle,
- rwt.html.Font.fromString( gc.font ).toCss()
- ];
- gc.strokeStyle = "#ff00ff";
- gc.fillStyle = "#00ff00";
- }
+ new EventBinding( canvas, "Paint", function( ev ) {
+ var gc = ev.gc;
+ props = [
+ gc.strokeStyle,
+ gc.fillStyle,
+ rwt.html.Font.fromString( gc.font ).toCss()
+ ];
+ gc.strokeStyle = "#ff00ff";
+ gc.fillStyle = "#00ff00";
} );
serverGc.init( 500, 500, fontArr, [ 170, 170, 170 ], [ 187, 187, 187 ] );
diff --git a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/Function_Test.js b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/Function_Test.js
index b3d292b..6cab237 100644
--- a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/Function_Test.js
+++ b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/Function_Test.js
@@ -11,7 +11,7 @@
(function() {
-var Function = rwt.scripting.Function;
+var FunctionFactory = rwt.scripting.FunctionFactory;
var SWT = rwt.scripting.SWT;
rwt.qx.Class.define( "org.eclipse.rap.clientscripting.Function_Test", {
@@ -23,7 +23,7 @@
testCreateFunctionWrongNamed : function() {
var code = "function foo(){}";
try {
- new Function( code );
+ FunctionFactory.createFunction( code, "handleEvent" );
fail();
} catch( ex ) {
// expected
@@ -33,8 +33,8 @@
/*global global:true */
testCreateFunctionWithHelper : function() {
var code = "var foo = function(){ global = 1; };var handleEvent = function(){ foo(); };";
- var listener = new Function( code );
- listener.call();
+ var listener = FunctionFactory.createFunction( code, "handleEvent" );
+ listener();
assertEquals( 1, global );
delete global; // An alternative would be to create a storage for such cases in TestUtil
},
@@ -42,7 +42,7 @@
testCreateFunctionSyntaxError : function() {
var code = "null.no!;";
try {
- new Function( code );
+ FunctionFactory.createFunction( code, "handleEvent" );
fail();
} catch( ex ) {
// expected
@@ -52,7 +52,7 @@
testCreateFunctionNoFunction : function() {
var code = "1";
try {
- new Function( code );
+ FunctionFactory.createFunction( code, "handleEvent" );
fail();
} catch( ex ) {
// expected
@@ -67,9 +67,10 @@
processor.processOperation( {
"target" : "w3",
"action" : "create",
- "type" : "rwt.clientscripting.Listener",
+ "type" : "rwt.scripting.Function",
"properties" : {
- "scriptCode" : code
+ "scriptCode" : code,
+ "name" : "handleEvent"
}
} );
@@ -81,15 +82,16 @@
var ObjectManager = rwt.remote.ObjectRegistry;
var processor = rwt.remote.MessageProcessor;
var code = "var handleEvent = function(){};";
- var createScript = [ "create", "r3", "rwt.clientscripting.Script", { "text" : code } ];
+ var createScript = [ "create", "r3", "rwt.scripting.Script", { "text" : code } ];
processor.processOperationArray( createScript );
processor.processOperation( {
"target" : "w3",
"action" : "create",
- "type" : "rwt.clientscripting.Listener",
+ "type" : "rwt.scripting.Function",
"properties" : {
- "scriptId" : "r3"
+ "scriptId" : "r3",
+ "name" : "handleEvent"
}
} );
@@ -99,34 +101,47 @@
testCallWithArgument : function() {
var code = "function handleEvent( e ){ e.x++; }";
- var listener = new Function( code );
+ var listener = FunctionFactory.createFunction( code, "handleEvent" );
+
var event = {
x : 1
};
- listener.call( event );
+ listener( event );
assertEquals( 2, event.x );
},
testNoContext : function() {
var code = "var handleEvent = function(){ this.x++; }";
- var listener = new Function( code );
+ var listener = FunctionFactory.createFunction( code, "handleEvent" );
listener.x = 1;
- listener.call();
+ listener();
assertEquals( 1, listener.x );
},
- testImportedClasses : function() {
+ testImportSWT : function() {
+ var ObjectManager = rwt.remote.ObjectRegistry;
+ var processor = rwt.remote.MessageProcessor;
+ var code = "var handleEvent = function( obj ){ obj.SWT = SWT;};";
+
+ processor.processOperation( {
+ "target" : "w3",
+ "action" : "create",
+ "type" : "rwt.scripting.Function",
+ "properties" : {
+ "scriptCode" : code,
+ "name" : "handleEvent"
+ }
+ } );
+ var result = ObjectManager.getObject( "w3" );
var obj = {};
- var code = "function handleEvent( obj ){ obj.SWT = SWT; }";
- var fun = new Function( code );
- fun.call( obj );
+ result( obj );
- assertIdentical( SWT, obj.SWT );
+ assertIdentical( rwt.scripting.SWT, obj.SWT );
}
}
diff --git a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/Script_Test.js b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/Script_Test.js
index c94569b..be62da0 100644
--- a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/Script_Test.js
+++ b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/Script_Test.js
@@ -24,7 +24,7 @@
testCreateScriptWithTextByProtocol : function() {
var code = "1+1;";
- var op = [ "create", "r3", "rwt.clientscripting.Script", { "text" : code } ];
+ var op = [ "create", "r3", "rwt.scripting.Script", { "text" : code } ];
MessageProcessor.processOperationArray( op );
var ObjectManager = rwt.remote.ObjectRegistry;
diff --git a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/WidgetProxyFactory_Test.js b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/WidgetProxyFactory_Test.js
index 659d373..d6e457a 100644
--- a/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/WidgetProxyFactory_Test.js
+++ b/tests/org.eclipse.rap.clientscripting.jstest/js/org/eclipse/rap/clientscripting/WidgetProxyFactory_Test.js
@@ -284,12 +284,10 @@
_createLogger : function() {
var log = [];
- var result = {
- "log" : log,
- "call" : function( arg ) {
- log.push( arg );
- }
+ var result = function( arg ) {
+ log.push( arg );
};
+ result.log = log;
return result;
},
diff --git a/tests/org.eclipse.rap.clientscripting.jstest/src/org/eclipse/rap/clientscripting/internal/ClientScriptingResourcesContribution.java b/tests/org.eclipse.rap.clientscripting.jstest/src/org/eclipse/rap/clientscripting/internal/ClientScriptingResourcesContribution.java
index 5a2921c..506d1dd 100644
--- a/tests/org.eclipse.rap.clientscripting.jstest/src/org/eclipse/rap/clientscripting/internal/ClientScriptingResourcesContribution.java
+++ b/tests/org.eclipse.rap.clientscripting.jstest/src/org/eclipse/rap/clientscripting/internal/ClientScriptingResourcesContribution.java
@@ -24,8 +24,8 @@
private static final String[] RESOURCES = new String[] {
"ClientScriptingUtil.js",
"SWT.js",
- "Function.js",
- "handler/ListenerHandler.js",
+ "FunctionFactory.js",
+ "handler/FunctionHandler.js",
"EventBinding.js",
"handler/EventBindingHandler.js",
"EventProxy.js",
diff --git a/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/Script_Test.java b/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/Script_Test.java
index 325300e..8282f04 100644
--- a/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/Script_Test.java
+++ b/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/Script_Test.java
@@ -10,7 +10,7 @@
******************************************************************************/
package org.eclipse.rap.clientscripting;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -24,7 +24,7 @@
public class Script_Test {
- private static final String REMOTE_TYPE = "rwt.clientscripting.Script";
+ private static final String REMOTE_TYPE = "rwt.scripting.Script";
private Connection connection;
private RemoteObject remoteObject;
diff --git a/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/ClientFunction_Test.java b/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/ClientFunction_Test.java
index 0c9cff2..9c37407 100644
--- a/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/ClientFunction_Test.java
+++ b/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/ClientFunction_Test.java
@@ -32,9 +32,8 @@
public class ClientFunction_Test {
- private static final String CLIENT_LISTENER_TYPE = "rwt.clientscripting.Listener";
- private static final String LISTENER_BINDING_TYPE = "rwt.clientscripting.EventBinding";
- private static final String SCRIPT_TYPE = "rwt.clientscripting.Script";
+ private static final String CLIENT_LISTENER_TYPE = "rwt.scripting.Function";
+ private static final String LISTENER_BINDING_TYPE = "rwt.scripting.EventBinding";
private ClientFunction function;
@@ -75,27 +74,39 @@
new ClientFunction( new Script( "script code" ) );
- verify( connection ).createRemoteObject( "rwt.clientscripting.Listener" );
+ verify( connection ).createRemoteObject( "rwt.scripting.Function" );
}
@Test
- public void testCreationWithString_sendsString() {
+ public void testCreation_setsName() {
RemoteObject listenerRemoteObject = mock( RemoteObject.class );
RemoteObject scriptRemoteObject = mock( RemoteObject.class );
Connection connection = mock( Connection.class );
when( connection.createRemoteObject( eq( CLIENT_LISTENER_TYPE ) ) )
.thenReturn( listenerRemoteObject );
- when( connection.createRemoteObject( eq( SCRIPT_TYPE ) ) ).thenReturn( scriptRemoteObject );
when( scriptRemoteObject.getId() ).thenReturn( "fooId" );
Fixture.fakeConnection( connection );
new ClientFunction( "my script code" );
+ verify( listenerRemoteObject ).set( eq( "name" ), eq( "handleEvent" ) );
+ }
+
+ @Test
+ public void testCreationWithString_setsString() {
+ RemoteObject listenerRemoteObject = mock( RemoteObject.class );
+ Connection connection = mock( Connection.class );
+ when( connection.createRemoteObject( eq( CLIENT_LISTENER_TYPE ) ) )
+ .thenReturn( listenerRemoteObject );
+ Fixture.fakeConnection( connection );
+
+ new ClientFunction( "my script code" );
+
verify( listenerRemoteObject ).set( eq( "scriptCode" ), eq( "my script code" ) );
}
@Test
- public void testCreationWithScript_initializesRemoteObject() {
+ public void testCreationWithScript_setsScriptId() {
RemoteObject listenerRemoteObject = mock( RemoteObject.class );
fakeConnection( listenerRemoteObject, CLIENT_LISTENER_TYPE );
Script script = mock( Script.class );
diff --git a/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/ClientListenerBinding_Test.java b/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/ClientListenerBinding_Test.java
index daa644d..bc7a287 100644
--- a/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/ClientListenerBinding_Test.java
+++ b/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/ClientListenerBinding_Test.java
@@ -27,7 +27,7 @@
public class ClientListenerBinding_Test {
- private static final String LISTENER_BINDING_TYPE = "rwt.clientscripting.EventBinding";
+ private static final String LISTENER_BINDING_TYPE = "rwt.scripting.EventBinding";
private ClientListener listener1;
private ClientListener listener2;
@@ -80,7 +80,7 @@
binding = new ClientListenerBinding( listener1, "w101", "KeyDown" );
- verify( connection ).createRemoteObject( eq( "rwt.clientscripting.EventBinding" ) );
+ verify( connection ).createRemoteObject( eq( "rwt.scripting.EventBinding" ) );
}
@Test
diff --git a/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/resources/ClientScriptingResources_Test.java b/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/resources/ClientScriptingResources_Test.java
index 3f117e3..292514f 100644
--- a/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/resources/ClientScriptingResources_Test.java
+++ b/tests/org.eclipse.rap.clientscripting.test/src/org/eclipse/rap/clientscripting/internal/resources/ClientScriptingResources_Test.java
@@ -10,6 +10,8 @@
******************************************************************************/
package org.eclipse.rap.clientscripting.internal.resources;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.endsWith;
@@ -23,10 +25,6 @@
import java.io.*;
import java.util.concurrent.atomic.AtomicReference;
-import static org.junit.Assert.*;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
import org.eclipse.rap.rwt.application.Application;
import org.eclipse.rap.rwt.client.Client;
@@ -34,6 +32,7 @@
import org.eclipse.rap.rwt.service.ResourceLoader;
import org.eclipse.rap.rwt.service.ResourceManager;
import org.eclipse.rap.rwt.testfixture.Fixture;
+import org.junit.*;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
@@ -78,10 +77,10 @@
ClientScriptingResources.ensure();
String registeredCode = registeredStringCaptor.get();
- assertTrue( registeredCode.contains( "clientscripting.ClientScriptingUtil =" ) );
- assertTrue( registeredCode.contains( "clientscripting.EventBinding =" ) );
- assertTrue( registeredCode.contains( "clientscripting.SWT =" ) );
- assertTrue( registeredCode.contains( "clientscripting.EventProxy =" ) );
+ assertTrue( registeredCode.contains( "scripting.ClientScriptingUtil =" ) );
+ assertTrue( registeredCode.contains( "scripting.EventBinding =" ) );
+ assertTrue( registeredCode.contains( "scripting.SWT =" ) );
+ assertTrue( registeredCode.contains( "scripting.EventProxy =" ) );
}
static String read( InputStream inputStream, String charset ) throws IOException {