aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/window/AttachShouldBeCalledForSubWindows.java
blob: 455fb2e531d36977832851a8139f2c20a5eeae69 (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
123
124
125
package com.vaadin.tests.components.window;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.tests.components.AbstractTestCase;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.LegacyWindow;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class AttachShouldBeCalledForSubWindows extends AbstractTestCase {
    private static final long serialVersionUID = 1L;

    private Log log = new Log(20);

    boolean addSubWindowBeforeMainWindow = true;

    @Override
    public void init() {

        VaadinRequest request = VaadinService.getCurrentRequest();
        if (request.getParameter("attachMainFirst") != null) {
            addSubWindowBeforeMainWindow = false;
        } else {
            addSubWindowBeforeMainWindow = true;
        }

        LegacyWindow mainWindow = new LegacyWindow() {
            @Override
            public void attach() {
                log(this);
                super.attach();
            }

            @Override
            public void addWindow(Window w) {
                log.log("Adding sub window");
                super.addWindow(w);
                log.log("Sub window added");

            }
        };
        mainWindow.setCaption("Main window");
        mainWindow.addComponent(log);
        mainWindow.getContent().setSizeFull();
        Label label = new Label("This is the main app") {
            @Override
            public void attach() {
                log(this);
                super.attach();
            }
        };

        mainWindow.addComponent(label);
        Window loginWindow = createSubWindow();
        if (addSubWindowBeforeMainWindow) {
            mainWindow.addWindow(loginWindow);
        }

        log.log("Setting main window");
        setMainWindow(mainWindow); // At this point
        log.log("Main window set");

        if (!addSubWindowBeforeMainWindow) {
            mainWindow.addWindow(loginWindow);
        }
    }

    private Window createSubWindow() {
        VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        Window w = new Window("Sub window", layout) {
            @Override
            public void attach() {
                log(this);
                super.attach();
            }
        };
        Button okButton = new Button("OK") {
            @Override
            public void attach() {
                super.attach();
                log(this);
            }
        };
        okButton.addListener(new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                log.log("Button clicked");

            }
        });
        okButton.setClickShortcut(KeyCode.ENTER);
        layout.addComponent(okButton);
        w.center();
        return w;
    }

    public void log(Component c) {
        Class<?> cls = c.getClass();
        if (cls.isAnonymousClass()) {
            cls = cls.getSuperclass();
        }
        log.log(cls.getName() + " '" + c.getCaption()
                + "' attached to application");
    }

    @Override
    protected String getDescription() {
        return "By default attaches a sub window with a button to the main window and then set the main window to the application. Use ?attachMainFirst to reverse the order. In both cases attach events should be sent for the components in the sub window";
    }

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

}