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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 ("streaming".equals(transport)) {
  109. enablePush(Transport.STREAMING);
  110. } else if ("long-polling".equals(transport)) {
  111. enablePush(Transport.LONG_POLLING);
  112. } else if (transport != null) {
  113. throw new IllegalArgumentException("Unknown transport value '"
  114. + transport
  115. + "'. Supported are xhr,websocket,streaming,long-polling");
  116. }
  117. }
  118. protected void enablePush(Transport transport) {
  119. PushConfiguration config = getPushConfiguration();
  120. if (!config.getPushMode().isEnabled()) {
  121. config.setPushMode(PushMode.AUTOMATIC);
  122. }
  123. config.setTransport(transport);
  124. // Ensure no fallback is used
  125. getPushConfiguration().setParameter(
  126. PushConfigurationState.FALLBACK_TRANSPORT_PARAM, "none");
  127. }
  128. /**
  129. * This method is inherited from the super class, but it should generally
  130. * not be used. If you want to just add components to your test, use e.g.
  131. * {@link #addComponent(Component)} instead to add the component to the
  132. * layout used by this UI. If you don't want to use the top-level layout
  133. * used by this class, you instead inherit directly from UI.
  134. *
  135. * @deprecated Use {@link #addComponent(Component)} or inherit from UI
  136. * instead.
  137. */
  138. @Override
  139. @Deprecated
  140. public void setContent(Component content) {
  141. // Overridden just to deprecate
  142. super.setContent(content);
  143. }
  144. private VerticalLayout layout;
  145. protected VerticalLayout getLayout() {
  146. return layout;
  147. }
  148. protected abstract void setup(VaadinRequest request);
  149. public void addComponent(Component c) {
  150. getLayout().addComponent(c);
  151. }
  152. public void addComponents(Component... c) {
  153. getLayout().addComponents(c);
  154. }
  155. public void removeComponent(Component c) {
  156. getLayout().removeComponent(c);
  157. }
  158. public void replaceComponent(Component oldComponent, Component newComponent) {
  159. getLayout().replaceComponent(oldComponent, newComponent);
  160. }
  161. protected void addButton(String caption, Button.ClickListener listener) {
  162. Button button = new Button(caption);
  163. button.addClickListener(listener);
  164. addComponent(button);
  165. }
  166. protected String getTestDescription() {
  167. return null;
  168. };
  169. protected Integer getTicketNumber() {
  170. return null;
  171. };
  172. protected WebBrowser getBrowser() {
  173. return getSession().getBrowser();
  174. }
  175. /**
  176. * Execute the provided runnable on the UI thread as soon as the current
  177. * request has been sent.
  178. */
  179. protected void runAfterResponse(final Runnable runnable) {
  180. // Immediately start a thread that will start waiting for the session to
  181. // get unlocked.
  182. new Thread() {
  183. @Override
  184. public void run() {
  185. accessSynchronously(runnable);
  186. }
  187. }.start();
  188. }
  189. }