aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/navigator/NavigatorViewBlocksBackButtonAction.java
blob: 7178b572b8460dc95545781b5e2f8c141ec0d14b (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
package com.vaadin.tests.navigator;

import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.PushStateNavigation;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@PushStateNavigation
public class NavigatorViewBlocksBackButtonAction
        extends AbstractReindeerTestUI {

    private Navigator navigator;

    protected static final String LABEL_MAINVIEW_ID = "LABEL_MAINVIEW_ID";
    protected static final String LABEL_PROMPTEDVIEW_ID = "LABEL_PROMPTEDVIEW_ID";

    @Override
    protected void setup(VaadinRequest request) {
        navigator = new Navigator(this, this);
        navigator.addView(MainView.NAME, new MainView());
        navigator.addView(ViewWithPromptedLeave.NAME,
                new ViewWithPromptedLeave());
        navigator.navigateTo(MainView.NAME);
    }

    class MainView extends VerticalLayout implements View {

        public static final String NAME = "mainview";

        public MainView() {
            Label label = new Label("MainView content");
            label.setId(LABEL_MAINVIEW_ID);
            addComponent(label);

            Button buttonNavToAnotherView = new Button(
                    "Navigate to another view",
                    event -> navigator.navigateTo(ViewWithPromptedLeave.NAME));
            addComponent(buttonNavToAnotherView);
        }

        @Override
        public void enter(ViewChangeEvent event) {
        }

    }

    class ViewWithPromptedLeave extends VerticalLayout
            implements View, ViewChangeListener {

        public static final String NAME = "prompted";

        protected boolean okToLeave;

        public ViewWithPromptedLeave() {
            Label label = new Label("ViewWithPromptedLeave content");
            label.setId(LABEL_PROMPTEDVIEW_ID);
            addComponent(label);
            addComponent(new Label(
                    "Try to navigate back to first view with browser back button."));
        }

        @Override
        public void enter(ViewChangeEvent event) {
            event.getNavigator().addViewChangeListener(this);
        }

        @Override
        public boolean beforeViewChange(final ViewChangeEvent event) {
            if (okToLeave) {
                okToLeave = false;
                return true;
            } else {
                final Window confirmationWindow = new Window("Confirm");
                confirmationWindow.setModal(true);
                confirmationWindow.setClosable(true);

                VerticalLayout confirmationWindowLayout = new VerticalLayout();
                confirmationWindow.setContent(confirmationWindowLayout);
                confirmationWindowLayout.setMargin(true);
                confirmationWindowLayout.setSpacing(true);
                confirmationWindowLayout
                        .addComponent(new Label("Really exit this view?"));
                confirmationWindowLayout
                        .addComponent(new Button("Yeah, sure!", clickEvent -> {
                            okToLeave = true;
                            getUI().removeWindow(confirmationWindow);
                            event.getNavigator().navigateTo(event.getViewName()
                                    + "/" + event.getParameters());
                        }));
                getUI().addWindow(confirmationWindow);
                return false;
            }
        }

        @Override
        public void afterViewChange(ViewChangeEvent event) {
            if (event.getNewView() != this) {
                event.getNavigator().removeViewChangeListener(this);
            }
        }
    }

    @Override
    protected String getTestDescription() {
        return "URL should not be changed when view blocks navigating away from view using the browser's Back-button";
    }

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