aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/layouts/LayoutPerformanceTests.java
blob: 4e50f0d83b0d547927126cd8d6872808119792ac (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package com.vaadin.tests.layouts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Iterator;

import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class LayoutPerformanceTests extends TestBase {
    private static final String[] widths = { null, "100%", "200px" };

    private enum ContainerType {
        SIMPLE_WRAPS {
            @Override
            public ComponentContainer buildLayout(int depth, int leafs,
                    SampleType leafType, boolean fullHeight) {
                if (depth == 0) {
                    return buildInnerLayout(leafs, leafType, fullHeight);
                }

                AbstractOrderedLayout layout = createOrderedLayout(depth,
                        fullHeight);
                layout.addComponent(buildLayout(depth - 1, leafs, leafType,
                        fullHeight));
                return layout;
            }
        },
        BORDER_LAYOUT {
            @Override
            public ComponentContainer buildLayout(int depth, int leafs,
                    SampleType leafType, boolean fullHeight) {
                if (depth == 0) {
                    return buildInnerLayout(leafs, leafType, fullHeight);
                }

                AbstractOrderedLayout layout = createOrderedLayout(depth,
                        fullHeight);
                Component content = leafType.createContent();
                content.setSizeUndefined();
                layout.addComponent(content);
                layout.addComponent(buildLayout(depth - 1, leafs, leafType,
                        fullHeight));
                layout.setExpandRatio(layout.getComponent(1), 1);
                return layout;
            }
        },
        FRACTAL {
            @Override
            public ComponentContainer buildLayout(int depth, int leafs,
                    SampleType leafType, boolean fullHeight) {
                if (depth == 0) {
                    return buildInnerLayout(leafs, leafType, fullHeight);
                }

                AbstractOrderedLayout layout = createOrderedLayout(depth,
                        fullHeight);
                layout.addComponent(buildLayout(depth - 1, leafs, leafType,
                        fullHeight));
                layout.addComponent(buildLayout(depth - 1, leafs, leafType,
                        fullHeight));
                layout.setExpandRatio(layout.getComponent(0), 1);
                layout.setExpandRatio(layout.getComponent(1), 2);
                return layout;
            }
        };
        public abstract ComponentContainer buildLayout(int depth, int leafs,
                SampleType leafType, boolean fullHeight);

        protected AbstractOrderedLayout createOrderedLayout(int depth,
                boolean fullHeight) {
            AbstractOrderedLayout layout = (depth % 2) == 0 ? new VerticalLayout()
                    : new HorizontalLayout();
            layout.setWidth("100%");
            if (fullHeight) {
                layout.setHeight("100%");
            } else {
                layout.setHeight(null);
            }

            return layout;
        }

        public ComponentContainer buildInnerLayout(int leafs,
                SampleType leafType, boolean fullHeight) {
            VerticalLayout layout = new VerticalLayout();
            if (fullHeight) {
                layout.setHeight("100%");
                layout.setWidth("100%");
            }
            for (int i = 0; i < leafs; i++) {
                Component leaf = leafType.createContent();
                if (leaf.getWidth() <= 0) {
                    leaf.setWidth(widths[i % 3]);
                }
                layout.addComponent(leaf);
            }
            return layout;
        }
    }

    private enum SampleType {
        SHORT_LABEL {
            @Override
            public Component createContent() {
                return new Label("Short label");
            }
        },
        LONG_LABEL {
            @Override
            public Component createContent() {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < 100; i++) {
                    builder.append("A rather long text. ");
                }
                return new Label(builder.toString());
            }
        },
        BUTTON {
            @Override
            public Component createContent() {
                return new Button("Text");
            }
        },
        TEXT_FIELD {
            @Override
            public Component createContent() {
                return new TextField("Field label");
            }
        },
        HORIZONTAL_LAYOUT {
            @Override
            public Component createContent() {
                HorizontalLayout layout = new HorizontalLayout();
                layout.addComponent(new Label("Left"));
                layout.addComponent(new Label("Right"));
                layout.setComponentAlignment(layout.getComponent(1),
                        Alignment.BOTTOM_RIGHT);

                return layout;
            }
        },
        WRAPPED_PANEL {
            @Override
            public Component createContent() {
                HorizontalLayout horizontal = new HorizontalLayout();
                horizontal.setWidth("100%");
                horizontal.setHeight(null);
                horizontal.setMargin(true);

                VerticalLayout left = new VerticalLayout();
                left.setWidth("100%");
                left.addComponent(new Label("Text 1"));
                left.addComponent(new Label("Text 2"));
                left.addComponent(new Label("Text 3"));
                horizontal.addComponent(left);

                VerticalLayout right = new VerticalLayout();
                right.setWidth("100%");
                right.addComponent(new Label("Text 1"));
                right.addComponent(new Label("Text 2"));
                right.addComponent(new Label("Text 3"));
                horizontal.addComponent(right);

                Panel panel = new Panel(horizontal);
                panel.setCaption("Panel caption");
                panel.setWidth("100%");
                panel.setHeight(null);

                return panel;
            }
        };
        public abstract Component createContent();
    }

    private Component testLayout = new Label("");

    private final CheckBox wrapInPanel = new CheckBox("Wrap in Panel");
    private final NativeSelect containerSelector = new NativeSelect(
            "Wrapping structure", EnumSet.allOf(ContainerType.class));
    @SuppressWarnings("boxing")
    private final NativeSelect levels = new NativeSelect("Wrapping depth",
            Arrays.asList(0, 1, 2, 3, 4, 5, 10, 15, 20, 25));
    private final NativeSelect leafSelector = new NativeSelect("Leaf type",
            EnumSet.allOf(SampleType.class));
    @SuppressWarnings("boxing")
    private final NativeSelect childAmount = new NativeSelect("Leaf count",
            Arrays.asList(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
                    4096, 8192, 16384));

    @Override
    protected void setup() {
        HorizontalLayout controls = new HorizontalLayout();
        controls.setSpacing(true);

        controls.addComponent(wrapInPanel);
        controls.addComponent(containerSelector);
        controls.addComponent(levels);
        controls.addComponent(leafSelector);
        controls.addComponent(childAmount);

        controls.addComponent(new Button("Clear", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                setTestLayout(new Label(""));
            }
        }));

        controls.addComponent(new Button("Apply", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                SampleType leafType = (SampleType) leafSelector.getValue();
                if (leafType == null) {
                    return;
                }

                ContainerType containerType = (ContainerType) containerSelector
                        .getValue();
                if (containerType == null) {
                    return;
                }

                boolean wrapped = wrapInPanel.booleanValue();
                ComponentContainer container = containerType.buildLayout(
                        ((Number) levels.getValue()).intValue(),
                        ((Number) childAmount.getValue()).intValue(), leafType,
                        !wrapped);
                if (wrapped) {
                    Panel panel = new Panel(container);
                    panel.setSizeFull();
                    setTestLayout(panel);
                } else {
                    setTestLayout(container);
                }
            }
        }));

        for (Iterator<Component> i = controls.getComponentIterator(); i
                .hasNext();) {
            Component component = i.next();
            if (component instanceof NativeSelect) {
                NativeSelect nativeSelect = (NativeSelect) component;
                nativeSelect.setNullSelectionAllowed(false);
                nativeSelect.setValue(new ArrayList<Object>(nativeSelect
                        .getItemIds()).get(0));
            }
            controls.setComponentAlignment(component, Alignment.BOTTOM_LEFT);
        }

        VerticalLayout layout = getLayout();
        layout.addComponent(controls);
        layout.addComponent(testLayout);
        layout.setExpandRatio(testLayout, 1);
        layout.setSizeFull();
    }

    public void setTestLayout(Component testLayout) {
        getLayout().replaceComponent(this.testLayout, testLayout);
        getLayout().setExpandRatio(testLayout, 1);
        this.testLayout = testLayout;
    }

    @Override
    protected String getDescription() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Integer getTicketNumber() {
        // TODO Auto-generated method stub
        return null;
    }

}