From: Leif Åstrand Date: Thu, 5 Jul 2012 12:10:02 +0000 (+0300) Subject: Add test for JavaScript component fundamentals (#8888) X-Git-Tag: 7.0.0.beta1~236^2~52 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b48d4648ee228802cf32de0101d6b815e191d4b2;p=vaadin-framework.git Add test for JavaScript component fundamentals (#8888) --- diff --git a/WebContent/statictestfiles/jsconnector.js b/WebContent/statictestfiles/jsconnector.js deleted file mode 100644 index a87df92bc8..0000000000 --- a/WebContent/statictestfiles/jsconnector.js +++ /dev/null @@ -1,13 +0,0 @@ -window.com_vaadin_tests_components_javascriptcomponent_BasicJavaScriptComponent_ExampleWidget = function() { - var connector = this; - - var rootElement = connector.getElement(); - rootElement.innerHTML = 'Hello world!'; - rootElement.onclick = function() { - connector.getRpcProxy().onClick("message"); - connector.onclick("another message"); - } - connector.onStateChange = function() { - console.log('state change:', this.getState()); - } -} \ No newline at end of file diff --git a/tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponent.java b/tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponent.java index 15c5f6b642..a7430705f5 100644 --- a/tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponent.java +++ b/tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponent.java @@ -3,6 +3,7 @@ */ package com.vaadin.tests.components.javascriptcomponent; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -10,72 +11,102 @@ import com.vaadin.annotations.JavaScript; import com.vaadin.external.json.JSONArray; import com.vaadin.external.json.JSONException; import com.vaadin.terminal.WrappedRequest; +import com.vaadin.terminal.gwt.client.communication.ClientRpc; import com.vaadin.terminal.gwt.client.communication.ServerRpc; import com.vaadin.terminal.gwt.client.ui.JavaScriptComponentState; import com.vaadin.tests.components.AbstractTestRoot; +import com.vaadin.tests.util.Log; import com.vaadin.ui.AbstractJavaScriptComponent; +import com.vaadin.ui.HasComponents; import com.vaadin.ui.JavaScriptCallback; -import com.vaadin.ui.Notification; public class BasicJavaScriptComponent extends AbstractTestRoot { - public interface ExampleClickRpc extends ServerRpc { - public void onClick(String message); + public interface TestRpc extends ServerRpc, ClientRpc { + public void sendRpc(String message); } - public static class SpecialState extends JavaScriptComponentState { - private List data; + public static class TestState extends JavaScriptComponentState { + private List messages = new ArrayList(); - public List getData() { - return data; + public List getMessages() { + return messages; } - public void setData(List data) { - this.data = data; + public void setMessages(List messages) { + this.messages = messages; } } - @JavaScript("/statictestfiles/jsconnector.js") - public static class ExampleWidget extends AbstractJavaScriptComponent { + @JavaScript("BasicJavaScriptComponentConnector.js") + public class ExampleWidget extends AbstractJavaScriptComponent { public ExampleWidget() { - registerRpc(new ExampleClickRpc() { - public void onClick(String message) { - Notification.show("Got a click: " + message); + registerRpc(new TestRpc() { + public void sendRpc(String message) { + log.log("Got RPC message: " + message); } }); - registerCallback("onclick", new JavaScriptCallback() { + registerCallback("messageToServer", new JavaScriptCallback() { public void call(JSONArray arguments) throws JSONException { - Notification.show("Got a callback: " - + arguments.getString(0)); + log.log("Got callback message: " + arguments.getString(0)); } }); - getState().setData(Arrays.asList("a", "b", "c")); + + registerCallback("reportParentIds", new JavaScriptCallback() { + public void call(JSONArray arguments) throws JSONException { + JSONArray parentIds = arguments.getJSONArray(0); + if (!parentIds.getString(0).equals(getConnectorId())) { + log.log("Connector ids doesn't match"); + } + + HasComponents parent = getParent(); + int i = 1; + while (parent != null) { + if (!parentIds.getString(i).equals( + parent.getConnectorId())) { + log.log("parentIds[" + i + "] doesn't match"); + } + i++; + parent = parent.getParent(); + } + log.log("Parent ids checked"); + } + }); + + getRpcProxy(TestRpc.class).sendRpc("RPC message"); + invokeCallback("messageToClient", "Callback message"); + + getState() + .setMessages( + Arrays.asList("First state message", + "Second state message")); } @Override - public SpecialState getState() { - return (SpecialState) super.getState(); + public TestState getState() { + return (TestState) super.getState(); } } + private final Log log = new Log(5); + @Override protected void setup(WrappedRequest request) { + addComponent(log); + ExampleWidget c = new ExampleWidget(); - c.setCaption("test caption"); - c.setDescription("Some description"); + c.setCaption("Component caption"); addComponent(c); } @Override protected String getTestDescription() { - // TODO Auto-generated method stub - return null; + return "Test for basic JavaScript component functionality."; } @Override protected Integer getTicketNumber() { - // TODO Auto-generated method stub - return null; + return Integer.valueOf(8888); } } diff --git a/tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponentConnector.js b/tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponentConnector.js new file mode 100644 index 0000000000..4e9c5280b3 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponentConnector.js @@ -0,0 +1,37 @@ +window.com_vaadin_tests_components_javascriptcomponent_BasicJavaScriptComponent_ExampleWidget = function() { + var parentIds = []; + var connectorId = this.getConnectorId(); + while(connectorId) { + parentIds.push(connectorId); + connectorId = this.getParentId(connectorId); + } + this.reportParentIds(parentIds); + + this.onStateChange = function() { + var e = this.getElement(); + + e.innerHTML = ''; + + var row = 1; + var log = function(text) { + e.innerHTML = "
" + row++ + ". " + text + "
" + e.innerHTML; + } + + log("Parent element className: " + this.getElement(this.getParentId()).className); + + var messages = this.getState().messages; + for(var i = 0; i < messages.length; i++) { + log("State message: " + messages[i]); + } + } + + this.registerRpc({ + sendRpc: function(message) { + this.getRpcProxy().sendRpc(message + " processed"); + } + }); + + this.messageToClient = function(message) { + this.messageToServer(message + " processed"); + } +} \ No newline at end of file