aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/window/CloseWindowAsync.java
blob: 5b600bc306113c81161fe750df5e20bdf09c52f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.vaadin.tests.components.window;

import java.util.concurrent.CompletableFuture;

import com.vaadin.annotations.PreserveOnRefresh;
import com.vaadin.annotations.Push;
import com.vaadin.annotations.Theme;
import com.vaadin.navigator.PushStateNavigation;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.communication.PushMode;
import com.vaadin.shared.ui.ui.Transport;
import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.UIDetachedException;
import com.vaadin.ui.Window;

@Theme("tests-valo-disabled-animations")
@PreserveOnRefresh
@Push(value = PushMode.MANUAL, transport = Transport.LONG_POLLING)
@PushStateNavigation
public class CloseWindowAsync extends AbstractTestUIWithLog {
    private final boolean manualPush;

    public CloseWindowAsync() {
        this(true);
    }

    public CloseWindowAsync(boolean manualPush) {
        this.manualPush = manualPush;
    }

    @Override
    protected void setup(VaadinRequest request) {
        final Button button = new Button("Open and directly close busy window");
        button.addClickListener((Button.ClickListener) event -> {
            final Window window = createWindow(1);
            final UI ui = getUI();
            ui.addWindow(window);

            CompletableFuture.runAsync(() -> {
                // task duration variable, could be a few ms or longer
                ui.accessSynchronously(() -> {
                    window.close();
                    if (manualPush) {
                        ui.push();
                    }
                });
            });
        });
        addComponents(button);

        final Button button2 = new Button(
                "Open and directly close busy window with error notification");
        button2.addClickListener((Button.ClickListener) event -> {
            final Window window = createWindow(2);
            final UI ui = getUI();
            ui.addWindow(window);

            CompletableFuture.runAsync(() -> {
                // task duration variable, could be a few ms or longer
                ui.accessSynchronously(() -> {
                    window.close();
                    Notification.show("error", Notification.Type.ERROR_MESSAGE);
                    if (manualPush) {
                        ui.push();
                    }
                });
            });
        });
        addComponents(button2);

        // Reconstructed the issue using the vaadin push training
        // https://vaadin.com/learn/training/vaadin-push

        final Button button3 = new Button(
                "Open and directly close busy window (vaadin push training)");
        button3.addClickListener((Button.ClickListener) event -> {
            final Window window = createWindow(3);
            final UI ui = getUI();
            ui.addWindow(window);

            CompletableFuture.runAsync(() -> {
                // task duration variable, could be a few ms or longer
                try {
                    ui.access(() -> {
                        ui.removeWindow(window);
                        if (manualPush) {
                            ui.push();
                        }
                    });
                } catch (UIDetachedException e) {
                    // browser closed
                }
            });
        });
        addComponents(button3);
    }

    private Window createWindow(int index) {
        final Window window = new Window();
        window.setCaption("Window");
        window.setWidth(30, Unit.PERCENTAGE);
        window.setHeight(30, Unit.PERCENTAGE);
        window.setModal(true);
        window.setContent(new Label("Window content"));
        window.addCloseListener(e -> log("closed " + index));
        return window;
    }

    @Override
    protected Integer getTicketNumber() {
        return 11942;
    }

    @Override
    protected String getTestDescription() {
        return "All buttons should successfully open and close window on all browsers.";
    }
}