Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AbstractTestUI.java 8.3KB

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