aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/ui/PushStateAndReplaceState.java
blob: 55fa374a2b705aaf0763bae1ba6137269442a324 (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
package com.vaadin.tests.components.ui;

import java.net.URI;

import com.vaadin.annotations.Title;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;

@Title("Original title")
public class PushStateAndReplaceState extends AbstractReindeerTestUI {

    private final Label locationLabel = new Label();
    private CheckBox replace;

    @Override
    protected void setup(VaadinRequest request) {
        locationLabel.setId("locationLabel");
        addComponent(locationLabel);
        updateLabel();

        getPage().addPopStateListener(event -> {
            Notification.show("Popstate event");
            updateLabel();
        });

        replace = new CheckBox("replace");
        replace.setId("replace");
        addComponent(replace);

        addComponent(createButton("test", "Move to ./test",
                Page.getCurrent().getLocation() + "/test"));
        addComponent(createButton("X", "Move to X", "X"));
        addComponent(createButton("root_X", "Move to /X", "/X"));
    }

    private Button createButton(String id, String caption,
            final String newUri) {
        Button button = new Button(caption, event -> {
            getPage().setTitle(caption);
            if (replace.getValue()) {
                getPage().replaceState(newUri);
            } else {
                getPage().pushState(newUri);
            }
            updateLabel();
        });

        button.setId(id);

        return button;
    }

    private void updateLabel() {
        URI location = getPage().getLocation();
        locationLabel.setValue("Current Location: " + location);
    }

    @Override
    public String getTestDescription() {
        return "Modern web framework shouldn't force you to use hashbang style urls for deep linking";
    }

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

}