aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/splitpanel/AbstractSplitPanelTest.java
blob: 7c0648876bca491f996555f709c845739051d1bf (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
package com.vaadin.tests.components.splitpanel;

import com.vaadin.server.Sizeable;
import com.vaadin.server.Sizeable.Unit;
import com.vaadin.tests.components.AbstractComponentContainerTest;
import com.vaadin.ui.AbstractSplitPanel;
import com.vaadin.ui.AbstractSplitPanel.SplitterClickEvent;
import com.vaadin.ui.AbstractSplitPanel.SplitterClickListener;

public abstract class AbstractSplitPanelTest<T extends AbstractSplitPanel>
        extends AbstractComponentContainerTest<T> implements
        SplitterClickListener {

    private Command<T, Boolean> splitterClickListenerCommand = new Command<T, Boolean>() {

        @Override
        public void execute(T c, Boolean value, Object data) {
            if (value) {
                c.addListener((SplitterClickListener) AbstractSplitPanelTest.this);
            } else {
                c.removeListener((SplitterClickListener) AbstractSplitPanelTest.this);
            }

        }
    };
    private Command<T, SplitPosition> setSplitPositionCommand = new Command<T, AbstractSplitPanelTest.SplitPosition>() {
        @Override
        public void execute(T c, SplitPosition value, Object data) {
            value.apply(c);
        }
    };
    private Command<T, Boolean> splitterLockCommand = new Command<T, Boolean>() {

        @Override
        public void execute(T c, Boolean value, Object data) {
            c.setLocked(value);
        }
    };

    @Override
    protected void createActions() {
        super.createActions();
        createSetSplitPositionAction(CATEGORY_FEATURES);
        createSplitterClickListenerAction(CATEGORY_LISTENERS);
        createSplitterLockAction(CATEGORY_FEATURES);

        // Default to 100% x 100% as SplitPanel does not work as undefined
        for (T c : getTestComponents()) {
            c.setSizeFull();
        }
    }

    private void createSplitterLockAction(String categoryFeatures) {
        createBooleanAction("Splitter locked", categoryFeatures, false,
                splitterLockCommand);

    }

    public static class SplitPosition {

        private boolean reverse = false;
        private int position;
        private Unit unit;
        private String posString;

        public SplitPosition(String pos) {
            this.posString = pos;
            if (pos.startsWith("-")) {
                reverse = true;
                pos = pos.substring(1);
            }

            if (pos.endsWith("px")) {
                position = Integer.parseInt(pos.substring(0, pos.length() - 2));
                unit = Sizeable.Unit.PIXELS;
            } else if (pos.endsWith("%")) {
                position = Integer.parseInt(pos.substring(0, pos.length() - 1));
                unit = Sizeable.Unit.PERCENTAGE;
            } else {
                throw new RuntimeException("Could not parse " + pos);
            }
        }

        public void apply(AbstractSplitPanel sp) {
            sp.setSplitPosition(position, unit, reverse);
        }

        @Override
        public String toString() {
            return posString;
        }
    }

    private void createSetSplitPositionAction(String categoryFeatures) {
        String subCategory = "Set splitter position";
        createCategory(subCategory, categoryFeatures);

        createClickAction("0px from left/top", subCategory,
                setSplitPositionCommand, new SplitPosition("0px"));
        createClickAction("200px from left/top", subCategory,
                setSplitPositionCommand, new SplitPosition("200px"));
        createClickAction("0px from right/bottom", subCategory,
                setSplitPositionCommand, new SplitPosition("-0px"));
        createClickAction("200px from right/bottom", subCategory,
                setSplitPositionCommand, new SplitPosition("-200px"));

        createClickAction("0% from left/top", subCategory,
                setSplitPositionCommand, new SplitPosition("0%"));
        createClickAction("0% from right/bottom", subCategory,
                setSplitPositionCommand, new SplitPosition("-0%"));
        createClickAction("50% from left/top", subCategory,
                setSplitPositionCommand, new SplitPosition("50%"));
        createClickAction("50% from right/bottom", subCategory,
                setSplitPositionCommand, new SplitPosition("-50%"));
        createClickAction("100% from left/top", subCategory,
                setSplitPositionCommand, new SplitPosition("100%"));
        createClickAction("100% from right/bottom", subCategory,
                setSplitPositionCommand, new SplitPosition("-100%"));

    }

    private void createSplitterClickListenerAction(String category) {
        createBooleanAction("SplitterClickListener", category, false,
                splitterClickListenerCommand);

    }

    @Override
    public void splitterClick(SplitterClickEvent event) {
        log(event.getClass().getSimpleName() + ": " + event.getButtonName()
                + " at " + event.getRelativeX() + "," + event.getRelativeY());
    }
}