]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add test for JavaScript component fundamentals (#8888)
authorLeif Åstrand <leif@vaadin.com>
Thu, 5 Jul 2012 12:10:02 +0000 (15:10 +0300)
committerLeif Åstrand <leif@vaadin.com>
Thu, 5 Jul 2012 12:10:02 +0000 (15:10 +0300)
WebContent/statictestfiles/jsconnector.js [deleted file]
tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponent.java
tests/testbench/com/vaadin/tests/components/javascriptcomponent/BasicJavaScriptComponentConnector.js [new file with mode: 0644]

diff --git a/WebContent/statictestfiles/jsconnector.js b/WebContent/statictestfiles/jsconnector.js
deleted file mode 100644 (file)
index a87df92..0000000
+++ /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
index 15c5f6b6427a0d7ecf7bb12e9e5eaa1845e66dc2..a7430705f57e226a93be95b1167c78b078e00148 100644 (file)
@@ -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<String> data;
+    public static class TestState extends JavaScriptComponentState {
+        private List<String> messages = new ArrayList<String>();
 
-        public List<String> getData() {
-            return data;
+        public List<String> getMessages() {
+            return messages;
         }
 
-        public void setData(List<String> data) {
-            this.data = data;
+        public void setMessages(List<String> 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 (file)
index 0000000..4e9c528
--- /dev/null
@@ -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 = "<div>" + row++ + ". " + text + "</div>" + 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