You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BasicJavaScriptComponent.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.javascriptcomponent;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import com.vaadin.annotations.JavaScript;
  21. import com.vaadin.server.ConnectorResource;
  22. import com.vaadin.server.DownloadStream;
  23. import com.vaadin.server.Resource;
  24. import com.vaadin.server.ResourceReference;
  25. import com.vaadin.server.VaadinRequest;
  26. import com.vaadin.shared.communication.ClientRpc;
  27. import com.vaadin.shared.communication.ServerRpc;
  28. import com.vaadin.shared.communication.URLReference;
  29. import com.vaadin.shared.ui.JavaScriptComponentState;
  30. import com.vaadin.tests.components.AbstractTestUI;
  31. import com.vaadin.tests.util.Log;
  32. import com.vaadin.ui.AbstractJavaScriptComponent;
  33. import com.vaadin.ui.Button;
  34. import com.vaadin.ui.Button.ClickEvent;
  35. import com.vaadin.ui.HasComponents;
  36. import com.vaadin.ui.JavaScriptFunction;
  37. import elemental.json.JsonArray;
  38. public class BasicJavaScriptComponent extends AbstractTestUI {
  39. public interface TestRpc extends ServerRpc, ClientRpc {
  40. public void sendRpc(String message);
  41. }
  42. public static class TestState extends JavaScriptComponentState {
  43. private List<String> messages = new ArrayList<String>();
  44. private URLReference url;
  45. public List<String> getMessages() {
  46. return messages;
  47. }
  48. public void setMessages(List<String> messages) {
  49. this.messages = messages;
  50. }
  51. public URLReference getUrl() {
  52. return url;
  53. }
  54. public void setUrl(URLReference url) {
  55. this.url = url;
  56. }
  57. }
  58. @JavaScript("BasicJavaScriptComponentConnector.js")
  59. public class ExampleWidget extends AbstractJavaScriptComponent {
  60. public ExampleWidget() {
  61. registerRpc(new TestRpc() {
  62. @Override
  63. public void sendRpc(String message) {
  64. log.log("Got RPC message: " + message);
  65. }
  66. });
  67. addFunction("messageToServer", new JavaScriptFunction() {
  68. @Override
  69. public void call(JsonArray arguments) {
  70. log.log("Got callback message: " + arguments.getString(0));
  71. }
  72. });
  73. addFunction("reportParentIds", new JavaScriptFunction() {
  74. @Override
  75. public void call(JsonArray arguments) {
  76. JsonArray parentIds = arguments.getArray(0);
  77. if (!parentIds.getString(0).equals(getConnectorId())) {
  78. log.log("Connector ids doesn't match");
  79. }
  80. HasComponents parent = getParent();
  81. int i = 1;
  82. while (parent != null) {
  83. if (!parentIds.getString(i).equals(
  84. parent.getConnectorId())) {
  85. log.log("parentIds[" + i + "] doesn't match");
  86. }
  87. i++;
  88. parent = parent.getParent();
  89. }
  90. log.log("Parent ids checked");
  91. }
  92. });
  93. getRpcProxy(TestRpc.class).sendRpc("RPC message");
  94. callFunction("messageToClient", "Callback message");
  95. getState()
  96. .setMessages(
  97. Arrays.asList("First state message",
  98. "Second state message"));
  99. // Dummy resource used to test URL translation
  100. Resource resource = new ConnectorResource() {
  101. @Override
  102. public String getMIMEType() {
  103. return null;
  104. }
  105. @Override
  106. public DownloadStream getStream() {
  107. return null;
  108. }
  109. @Override
  110. public String getFilename() {
  111. return null;
  112. }
  113. };
  114. getState().setUrl(new ResourceReference(resource, this, "test"));
  115. }
  116. @Override
  117. public TestState getState() {
  118. return (TestState) super.getState();
  119. }
  120. }
  121. private final Log log = new Log(5);
  122. @Override
  123. protected void setup(VaadinRequest request) {
  124. addComponent(log);
  125. final ExampleWidget c = new ExampleWidget();
  126. c.setCaption("Component caption");
  127. addComponent(c);
  128. Button removeButton = new Button("Remove component",
  129. new Button.ClickListener() {
  130. @Override
  131. public void buttonClick(ClickEvent event) {
  132. removeComponent(c);
  133. }
  134. });
  135. removeButton.setId("RemoveButton");
  136. addComponent(removeButton);
  137. }
  138. @Override
  139. protected String getTestDescription() {
  140. return "Test for basic JavaScript component functionality.";
  141. }
  142. @Override
  143. protected Integer getTicketNumber() {
  144. return Integer.valueOf(8888);
  145. }
  146. }