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.

AbstractTestUI.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package com.vaadin.tests.components;
  2. import java.io.File;
  3. import com.vaadin.annotations.Widgetset;
  4. import com.vaadin.server.VaadinRequest;
  5. import com.vaadin.server.VaadinServlet;
  6. import com.vaadin.server.WebBrowser;
  7. import com.vaadin.shared.communication.PushMode;
  8. import com.vaadin.shared.ui.label.ContentMode;
  9. import com.vaadin.shared.ui.ui.Transport;
  10. import com.vaadin.shared.ui.ui.UIState.PushConfigurationState;
  11. import com.vaadin.ui.Button;
  12. import com.vaadin.ui.Component;
  13. import com.vaadin.ui.Label;
  14. import com.vaadin.ui.Notification;
  15. import com.vaadin.ui.Notification.Type;
  16. import com.vaadin.ui.PushConfiguration;
  17. import com.vaadin.ui.UI;
  18. import com.vaadin.ui.VerticalLayout;
  19. public abstract class AbstractTestUI extends UI {
  20. @Override
  21. public void init(VaadinRequest request) {
  22. getPage().setTitle(getClass().getName());
  23. Label label = new Label(getTestDescription(), ContentMode.HTML);
  24. label.setWidth("100%");
  25. VerticalLayout rootLayout = new VerticalLayout();
  26. rootLayout.setMargin(true);
  27. setContent(rootLayout);
  28. layout = new VerticalLayout();
  29. rootLayout.addComponent(label);
  30. rootLayout.addComponent(layout);
  31. ((VerticalLayout) getContent()).setExpandRatio(layout, 1);
  32. warnIfWidgetsetMaybeNotCompiled();
  33. setTransport(request);
  34. setup(request);
  35. }
  36. protected void warnIfWidgetsetMaybeNotCompiled() {
  37. // Can't check location if sendUrlAsParameters is disabled
  38. if (!getSession().getConfiguration().isSendUrlsAsParameters()) {
  39. return;
  40. }
  41. // Ignore if using debug mode
  42. String query = getPage().getLocation().getQuery();
  43. if (query != null && query.matches(".*[&?]gwt\\.codesvr.*")) {
  44. return;
  45. }
  46. // Find out the widgetset of this UI based on @Widgetset annotation
  47. Class<?> currentType = getClass();
  48. String usedWidgetset = VaadinServlet.DEFAULT_WIDGETSET;
  49. while (currentType != Object.class) {
  50. Widgetset annotation = currentType.getAnnotation(Widgetset.class);
  51. if (annotation != null) {
  52. usedWidgetset = annotation.value();
  53. break;
  54. } else {
  55. currentType = currentType.getSuperclass();
  56. }
  57. }
  58. // Assuming the same folder structure as in git repo
  59. // Assuming project root is the working dir of this process
  60. File widgetsetsFolder = new File("WebContent/VAADIN/widgetsets");
  61. if (!widgetsetsFolder.isDirectory()) {
  62. return;
  63. }
  64. // Find the most newly compiled widgetset
  65. long newestWidgetsetTimestamp = -1;
  66. String newestWidgetsetName = null;
  67. File[] children = widgetsetsFolder.listFiles();
  68. for (File child : children) {
  69. if (!child.isDirectory() || child.getName().equals("WEB-INF")) {
  70. continue;
  71. }
  72. long lastModified = child.lastModified();
  73. if (lastModified > newestWidgetsetTimestamp) {
  74. newestWidgetsetTimestamp = lastModified;
  75. newestWidgetsetName = child.getName();
  76. }
  77. }
  78. // Compare to currently used widgetset, with a 30 minute grace period
  79. File currentWidgetsetFolder = new File(widgetsetsFolder, usedWidgetset);
  80. long currentWidgetsetTimestamp = currentWidgetsetFolder.lastModified();
  81. int halfHour = 30 * 60 * 1000;
  82. if (currentWidgetsetTimestamp + halfHour < newestWidgetsetTimestamp) {
  83. Notification
  84. .show("The currently used widgetset ("
  85. + usedWidgetset
  86. + ") was compiled long before the most recently compiled one ("
  87. + newestWidgetsetName
  88. + "). Are you sure you have compiled the right widgetset?",
  89. Type.WARNING_MESSAGE);
  90. }
  91. }
  92. /**
  93. * Sets the push transport according to the transport= URL parameter if such
  94. * is given. Supports transport=xhr (disables push), transport=websocket
  95. * (forces websocket into use), transport=streaming (forces streaming into
  96. * use). Using ?transport=xyz disables the fallback transport.
  97. *
  98. * @param request
  99. * The UI init request
  100. */
  101. protected void setTransport(VaadinRequest request) {
  102. String transport = request.getParameter("transport");
  103. PushConfiguration config = getPushConfiguration();
  104. if ("xhr".equals(transport)) {
  105. config.setPushMode(PushMode.DISABLED);
  106. } else if ("websocket".equals(transport)) {
  107. enablePush(Transport.WEBSOCKET);
  108. } else if ("websocket-xhr".equals(transport)) {
  109. enablePush(Transport.WEBSOCKET_XHR);
  110. } else if ("streaming".equals(transport)) {
  111. enablePush(Transport.STREAMING);
  112. } else if ("long-polling".equals(transport)) {
  113. enablePush(Transport.LONG_POLLING);
  114. } else if (transport != null) {
  115. throw new IllegalArgumentException("Unknown transport value '"
  116. + transport
  117. + "'. Supported are xhr,websocket,streaming,long-polling");
  118. }
  119. }
  120. protected void enablePush(Transport transport) {
  121. PushConfiguration config = getPushConfiguration();
  122. if (!config.getPushMode().isEnabled()) {
  123. config.setPushMode(PushMode.AUTOMATIC);
  124. }
  125. config.setTransport(transport);
  126. // Ensure no fallback is used
  127. getPushConfiguration().setParameter(
  128. PushConfigurationState.FALLBACK_TRANSPORT_PARAM, "none");
  129. }
  130. /**
  131. * This method is inherited from the super class, but it should generally
  132. * not be used. If you want to just add components to your test, use e.g.
  133. * {@link #addComponent(Component)} instead to add the component to the
  134. * layout used by this UI. If you don't want to use the top-level layout
  135. * used by this class, you instead inherit directly from UI.
  136. *
  137. * @deprecated Use {@link #addComponent(Component)} or inherit from UI
  138. * instead.
  139. */
  140. @Override
  141. @Deprecated
  142. public void setContent(Component content) {
  143. // Overridden just to deprecate
  144. super.setContent(content);
  145. }
  146. private VerticalLayout layout;
  147. protected VerticalLayout getLayout() {
  148. return layout;
  149. }
  150. protected abstract void setup(VaadinRequest request);
  151. public void addComponent(Component c) {
  152. getLayout().addComponent(c);
  153. }
  154. public void addComponents(Component... c) {
  155. getLayout().addComponents(c);
  156. }
  157. public void removeComponent(Component c) {
  158. getLayout().removeComponent(c);
  159. }
  160. public void replaceComponent(Component oldComponent, Component newComponent) {
  161. getLayout().replaceComponent(oldComponent, newComponent);
  162. }
  163. protected void addButton(String caption, Button.ClickListener listener) {
  164. Button button = new Button(caption);
  165. button.addClickListener(listener);
  166. addComponent(button);
  167. }
  168. protected String getTestDescription() {
  169. return null;
  170. };
  171. protected Integer getTicketNumber() {
  172. return null;
  173. };
  174. protected WebBrowser getBrowser() {
  175. return getSession().getBrowser();
  176. }
  177. /**
  178. * Execute the provided runnable on the UI thread as soon as the current
  179. * request has been sent.
  180. */
  181. protected void runAfterResponse(final Runnable runnable) {
  182. // Immediately start a thread that will start waiting for the session to
  183. // get unlocked.
  184. new Thread() {
  185. @Override
  186. public void run() {
  187. accessSynchronously(runnable);
  188. }
  189. }.start();
  190. }
  191. }