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

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