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.

NavigateWithOngoingXHR.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2012 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.application;
  17. import java.io.IOException;
  18. import java.io.PrintWriter;
  19. import com.vaadin.server.ExternalResource;
  20. import com.vaadin.server.RequestHandler;
  21. import com.vaadin.server.VaadinRequest;
  22. import com.vaadin.server.VaadinResponse;
  23. import com.vaadin.server.VaadinSession;
  24. import com.vaadin.tests.components.AbstractReindeerTestUI;
  25. import com.vaadin.ui.Link;
  26. import com.vaadin.v7.shared.ui.progressindicator.ProgressIndicatorServerRpc;
  27. import com.vaadin.v7.ui.ProgressIndicator;
  28. public class NavigateWithOngoingXHR extends AbstractReindeerTestUI {
  29. private final RequestHandler slowRequestHandler = new RequestHandler() {
  30. @Override
  31. public boolean handleRequest(VaadinSession session,
  32. VaadinRequest request, VaadinResponse response)
  33. throws IOException {
  34. if ("/slowRequestHandler".equals(request.getPathInfo())) {
  35. // Make the navigation request last longer to keep the
  36. // communication error visible
  37. // System.out.println("Got slow content request");
  38. try {
  39. Thread.sleep(5000);
  40. } catch (InterruptedException e) {
  41. e.printStackTrace();
  42. }
  43. if (request.getParameter("download") != null) {
  44. response.setHeader("Content-Disposition", "attachment");
  45. }
  46. response.setContentType("text/plain");
  47. PrintWriter writer = response.getWriter();
  48. writer.println("Loaded slowly");
  49. writer.close();
  50. // System.out.println("Finished slow content request");
  51. return true;
  52. }
  53. return false;
  54. }
  55. };
  56. @Override
  57. protected void setup(VaadinRequest request) {
  58. addComponent(new ProgressIndicator() {
  59. {
  60. registerRpc(new ProgressIndicatorServerRpc() {
  61. @Override
  62. public void poll() {
  63. // System.out.println("Pausing poll request");
  64. try {
  65. // Make the XHR request last longer to make it
  66. // easier to click the link at the right moment.
  67. Thread.sleep(1000);
  68. } catch (InterruptedException e) {
  69. e.printStackTrace();
  70. }
  71. // System.out.println("Continuing poll request");
  72. }
  73. });
  74. setPollingInterval(3000);
  75. }
  76. });
  77. // Hacky URLs that are might not work in all deployment scenarios
  78. addComponent(new Link("Navigate away",
  79. new ExternalResource("slowRequestHandler")));
  80. addComponent(new Link("Start download",
  81. new ExternalResource("slowRequestHandler?download")));
  82. }
  83. @Override
  84. public void attach() {
  85. super.attach();
  86. getSession().addRequestHandler(slowRequestHandler);
  87. }
  88. @Override
  89. public void detach() {
  90. getSession().removeRequestHandler(slowRequestHandler);
  91. super.detach();
  92. }
  93. @Override
  94. protected String getTestDescription() {
  95. return "Navigating away from a Vaadin page while there's an ongoing XHR request should not cause a communication error to be displayed";
  96. }
  97. @Override
  98. protected Integer getTicketNumber() {
  99. return Integer.valueOf(8891);
  100. }
  101. }