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.

CloseWindowAsync.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.vaadin.tests.components.window;
  2. import java.util.concurrent.CompletableFuture;
  3. import com.vaadin.annotations.PreserveOnRefresh;
  4. import com.vaadin.annotations.Push;
  5. import com.vaadin.annotations.Theme;
  6. import com.vaadin.navigator.PushStateNavigation;
  7. import com.vaadin.server.VaadinRequest;
  8. import com.vaadin.shared.communication.PushMode;
  9. import com.vaadin.shared.ui.ui.Transport;
  10. import com.vaadin.tests.components.AbstractTestUIWithLog;
  11. import com.vaadin.ui.Button;
  12. import com.vaadin.ui.Label;
  13. import com.vaadin.ui.Notification;
  14. import com.vaadin.ui.UI;
  15. import com.vaadin.ui.UIDetachedException;
  16. import com.vaadin.ui.Window;
  17. @Theme("tests-valo-disabled-animations")
  18. @PreserveOnRefresh
  19. @Push(value = PushMode.MANUAL, transport = Transport.LONG_POLLING)
  20. @PushStateNavigation
  21. public class CloseWindowAsync extends AbstractTestUIWithLog {
  22. private final boolean manualPush;
  23. public CloseWindowAsync() {
  24. this(true);
  25. }
  26. public CloseWindowAsync(boolean manualPush) {
  27. this.manualPush = manualPush;
  28. }
  29. @Override
  30. protected void setup(VaadinRequest request) {
  31. final Button button = new Button("Open and directly close busy window");
  32. button.addClickListener((Button.ClickListener) event -> {
  33. final Window window = createWindow(1);
  34. final UI ui = getUI();
  35. ui.addWindow(window);
  36. CompletableFuture.runAsync(() -> {
  37. // task duration variable, could be a few ms or longer
  38. ui.accessSynchronously(() -> {
  39. window.close();
  40. if (manualPush) {
  41. ui.push();
  42. }
  43. });
  44. });
  45. });
  46. addComponents(button);
  47. final Button button2 = new Button(
  48. "Open and directly close busy window with error notification");
  49. button2.addClickListener((Button.ClickListener) event -> {
  50. final Window window = createWindow(2);
  51. final UI ui = getUI();
  52. ui.addWindow(window);
  53. CompletableFuture.runAsync(() -> {
  54. // task duration variable, could be a few ms or longer
  55. ui.accessSynchronously(() -> {
  56. window.close();
  57. Notification.show("error", Notification.Type.ERROR_MESSAGE);
  58. if (manualPush) {
  59. ui.push();
  60. }
  61. });
  62. });
  63. });
  64. addComponents(button2);
  65. // Reconstructed the issue using the vaadin push training
  66. // https://vaadin.com/learn/training/vaadin-push
  67. final Button button3 = new Button(
  68. "Open and directly close busy window (vaadin push training)");
  69. button3.addClickListener((Button.ClickListener) event -> {
  70. final Window window = createWindow(3);
  71. final UI ui = getUI();
  72. ui.addWindow(window);
  73. CompletableFuture.runAsync(() -> {
  74. // task duration variable, could be a few ms or longer
  75. try {
  76. ui.access(() -> {
  77. ui.removeWindow(window);
  78. if (manualPush) {
  79. ui.push();
  80. }
  81. });
  82. } catch (UIDetachedException e) {
  83. // browser closed
  84. }
  85. });
  86. });
  87. addComponents(button3);
  88. }
  89. private Window createWindow(int index) {
  90. final Window window = new Window();
  91. window.setCaption("Window");
  92. window.setWidth(30, Unit.PERCENTAGE);
  93. window.setHeight(30, Unit.PERCENTAGE);
  94. window.setModal(true);
  95. window.setContent(new Label("Window content"));
  96. window.addCloseListener(e -> log("closed " + index));
  97. return window;
  98. }
  99. @Override
  100. protected Integer getTicketNumber() {
  101. return 11942;
  102. }
  103. @Override
  104. protected String getTestDescription() {
  105. return "All buttons should successfully open and close window on all browsers.";
  106. }
  107. }