aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/layouts/TestLayoutPerformance.java
blob: 095f597873b0f38366397a3d14922215977b6fb6 (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
package com.vaadin.tests.layouts;

import com.vaadin.shared.ui.ContentMode;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.v7.ui.NativeSelect;
import com.vaadin.v7.ui.TextField;

public class TestLayoutPerformance extends TestBase {
    private NativeSelect ns;
    private int i;
    private NativeSelect ns2;
    private VerticalLayout testarea = new VerticalLayout();

    @Override
    protected String getDescription() {
        return "Test app to test simple rendering to various layouts.";
    }

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

    @Override
    protected void setup() {
        Label label = new Label("<h1>CssLayout performance test.</h1>",
                ContentMode.HTML);
        getLayout().addComponent(label);

        label = new Label(
                "<em>Hint</em>. Use debug dialog to measure rendering times TODO: extend with size settings (to both layout and content).",
                ContentMode.HTML);
        getLayout().addComponent(label);

        ns = new NativeSelect("Select component to test");
        ns.addItem(CssLayout.class);
        ns.addItem(GridLayout.class);
        ns.addItem(VerticalLayout.class);
        ns.setNullSelectionAllowed(false);
        ns.setValue(CssLayout.class);

        ns2 = new NativeSelect("Select component to render inside layout.");
        ns2.addItem(Label.class);
        ns2.addItem(Button.class);
        ns2.setNullSelectionAllowed(false);
        ns2.setValue(Label.class);

        final TextField n = new TextField("Number of components");

        n.setValue("1000");

        final CheckBox cb = new CheckBox("Generate captions", false);

        Button b = new Button("Render component");

        b.addClickListener(event -> {
            int components = Integer.parseInt(n.getValue());
            Layout layout = getCurrentLayout();
            for (int i = 0; i < components; i++) {
                Component component = newTestComponent();
                if (cb.getValue()) {
                    component.setCaption("caption " + i);
                }
                layout.addComponent(component);
            }

            testarea.removeAllComponents();
            testarea.addComponent(layout);
        });

        getLayout().addComponent(ns);
        getLayout().addComponent(ns2);
        getLayout().addComponent(n);
        getLayout().addComponent(cb);
        getLayout().addComponent(b);
        getLayout().addComponent(testarea);

    }

    private Layout getCurrentLayout() {
        Class<?> value = (Class<?>) ns.getValue();
        if (value == GridLayout.class) {
            return new GridLayout(10, 1);
        }

        try {
            return (Layout) value.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;

    }

    private Component newTestComponent() {
        Class<?> componentClass = (Class<?>) ns2.getValue();
        AbstractComponent newInstance = null;
        try {
            newInstance = (AbstractComponent) componentClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        if (componentClass == Label.class) {
            ((Label) newInstance).setValue("Test l " + (i++));
            ((Label) newInstance).setSizeUndefined();
        } else {
            newInstance.setCaption("Test l " + (i++));
        }
        return newInstance;
    }

}