summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/layouts/MovingComponentsWhileOldParentInvisible.java
blob: cda1407079c098db53f62c49a2f53d74a0455616 (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
package com.vaadin.tests.layouts;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.tests.VaadinClasses;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.PopupView;

public class MovingComponentsWhileOldParentInvisible extends TestBase {

    private ComponentContainer cc = new AbsoluteLayout(); // initial dummy
                                                          // contents
    private Label lab;

    @Override
    protected void setup() {
        lab = new Label("Label inside the component container");
        lab.setWidth(null);

        ComboBox componentContainerSelect = new ComboBox("Container") {
            {
                pageLength = 0;
            }
        };
        componentContainerSelect.setId("componentContainerSelect");
        componentContainerSelect.setWidth("300px");
        componentContainerSelect.setImmediate(true);
        componentContainerSelect.setNullSelectionAllowed(false);
        // componentContainer.addContainerProperty(CAPTION, String.class, "");
        // componentContainer.addContainerProperty(CLASS, Class.class, "");

        for (Class<? extends ComponentContainer> cls : VaadinClasses
                .getComponentContainers()) {
            if (cls == LoginForm.class || cls == CustomLayout.class
                    || CustomComponent.class.isAssignableFrom(cls)
                    || cls == PopupView.class) {
                // Does not support addComponent
                continue;
            }
            componentContainerSelect.addItem(cls);
        }
        componentContainerSelect.addListener(new ValueChangeListener() {

            @Override
            @SuppressWarnings("unchecked")
            public void valueChange(ValueChangeEvent event) {
                ComponentContainer oldCC = cc;
                cc = createComponentContainer((Class<? extends ComponentContainer>) event
                        .getProperty().getValue());
                cc.addComponent(lab);

                replaceComponent(oldCC, cc);
            }
        });

        componentContainerSelect.setValue(componentContainerSelect.getItemIds()
                .iterator().next());
        Button but1 = new Button("Move in and out of component container",
                new Button.ClickListener() {

                    @Override
                    public void buttonClick(ClickEvent event) {
                        cc.setVisible(!cc.isVisible());
                        if (!cc.isVisible()) {
                            getLayout().addComponent(lab);
                            lab.setValue(lab.getValue().replace("inside",
                                    "outside"));
                        } else {
                            cc.addComponent(lab);
                            lab.setValue(lab.getValue().replace("outside",
                                    "inside"));
                        }
                    }
                });

        addComponent(componentContainerSelect);
        addComponent(cc);
        addComponent(but1);
    }

    protected ComponentContainer createComponentContainer(
            Class<? extends ComponentContainer> value) {
        try {
            ComponentContainer cc = value.newInstance();
            cc.setWidth("300px");
            cc.setHeight("300px");
            return cc;
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected String getDescription() {
        return "Client side layouts can easily have a bug where its internal data structures gets messed up when child components from it are moved forth and back when it is invisible (registered, but renders are ignored until becomes visible again). Things are especially easy to mess up when the layout uses wrapper widget over each component (like VOrderedLayout and VGridLayout does). This tests Vertical (Ordered), Grid and CssLayout.";
    }

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

}