summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/tests/tickets/Ticket2279.java
blob: f7889c599fb2b714eecca1c565425b18915b391f (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
package com.itmill.toolkit.tests.tickets;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.terminal.gwt.client.ui.AlignmentInfo;
import com.itmill.toolkit.ui.AbstractOrderedLayout;
import com.itmill.toolkit.ui.GridLayout;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Layout;
import com.itmill.toolkit.ui.VerticalLayout;
import com.itmill.toolkit.ui.Window;
import com.itmill.toolkit.ui.Layout.AlignmentHandler;

public class Ticket2279 extends Application {

    private Label label;

    private static Map<String, Integer> expected = new HashMap<String, Integer>();

    private static Set<String> longHorizontalAlignments = new HashSet<String>();
    private static Set<String> shortHorizontalAlignments = new HashSet<String>();
    private static Set<String> longVerticalAlignments = new HashSet<String>();
    private static Set<String> shortVerticalAlignments = new HashSet<String>();

    static {
        expected.put("r", AlignmentInfo.Bits.ALIGNMENT_RIGHT);
        expected.put("l", AlignmentInfo.Bits.ALIGNMENT_LEFT);
        expected.put("c", AlignmentInfo.Bits.ALIGNMENT_HORIZONTAL_CENTER);
        expected.put("t", AlignmentInfo.Bits.ALIGNMENT_TOP);
        expected.put("b", AlignmentInfo.Bits.ALIGNMENT_BOTTOM);
        expected.put("m", AlignmentInfo.Bits.ALIGNMENT_VERTICAL_CENTER);

        expected.put("right", AlignmentInfo.Bits.ALIGNMENT_RIGHT);
        expected.put("left", AlignmentInfo.Bits.ALIGNMENT_LEFT);
        expected.put("center", AlignmentInfo.Bits.ALIGNMENT_HORIZONTAL_CENTER);
        expected.put("top", AlignmentInfo.Bits.ALIGNMENT_TOP);
        expected.put("bottom", AlignmentInfo.Bits.ALIGNMENT_BOTTOM);
        expected.put("middle", AlignmentInfo.Bits.ALIGNMENT_VERTICAL_CENTER);

        shortHorizontalAlignments.add("r");
        shortHorizontalAlignments.add("l");
        shortHorizontalAlignments.add("c");
        shortVerticalAlignments.add("t");
        shortVerticalAlignments.add("b");
        shortVerticalAlignments.add("m");

        longHorizontalAlignments.add("right");
        longHorizontalAlignments.add("left");
        longHorizontalAlignments.add("center");
        longVerticalAlignments.add("top");
        longVerticalAlignments.add("bottom");
        longVerticalAlignments.add("middle");

    }

    @Override
    public void init() {
        Window w = new Window(getClass().getSimpleName());
        setMainWindow(w);
        setTheme("tests-tickets");
        AbstractOrderedLayout layout = (AbstractOrderedLayout) w.getLayout();

        createUI(layout);
    }

    private void createUI(Layout layout) {
        VerticalLayout vl = new VerticalLayout();
        vl.setWidth("500px");
        vl.setHeight("500px");
        vl.setStyleName("borders");
        label = new Label("<b>Error messages follows:</b><br/>",
                Label.CONTENT_XHTML);
        vl.addComponent(label);
        layout.addComponent(vl);

        testAlignments(vl);

        GridLayout gl = new GridLayout(1, 1);
        gl.setWidth("500px");
        gl.setHeight("500px");
        gl.setStyleName("borders");
        label = new Label("<b>Error messages follows:</b><br/>",
                Label.CONTENT_XHTML);
        gl.addComponent(label);
        layout.addComponent(gl);

        testAlignments(gl);

    }

    private void testAlignments(AlignmentHandler layout) {
        HashSet<String> horizontals = new HashSet<String>();
        horizontals.addAll(shortHorizontalAlignments);
        horizontals.addAll(longHorizontalAlignments);

        for (String horiz : horizontals) {
            // Test "l","r","left","right" etc
            int expectedHoriz = expected.get(horiz);
            checkAlignment(layout, horiz, AlignmentHandler.ALIGNMENT_TOP
                    | expectedHoriz);

            for (String vert : shortVerticalAlignments) {
                int expectedVert = expected.get(vert);

                // Test "lt","rt" etc
                if (horiz.length() == 1) {
                    checkAlignment(layout, horiz + vert, expectedHoriz
                            | expectedVert);
                    checkAlignment(layout, vert + horiz, expectedHoriz
                            | expectedVert);
                } else {
                    boolean ok = false;
                    try {
                        checkAlignment(layout, horiz + vert, expectedHoriz
                                | expectedVert);
                    } catch (IllegalArgumentException e) {
                        // OK, "centert","rightb" etc are not valid
                        ok = true;
                    }
                    if (!ok) {
                        error("IllegalArgumentException was not thrown for "
                                + horiz + vert);
                    }
                    ok = false;
                    try {
                        checkAlignment(layout, vert + horiz, expectedHoriz
                                | expectedVert);
                    } catch (IllegalArgumentException e) {
                        // OK, "centert","rightb" etc are not valid
                        ok = true;
                    }
                    if (!ok) {
                        error("IllegalArgumentException was not thrown for "
                                + horiz + vert);
                    }

                }

                // Test "l t","r t" etc
                checkAlignment(layout, horiz + " " + vert, expectedHoriz
                        | expectedVert);
                checkAlignment(layout, vert + " " + horiz, expectedHoriz
                        | expectedVert);
            }

            for (String vert : longVerticalAlignments) {
                int expectedVert = expected.get(vert);

                // Test "right t","right b" etc
                checkAlignment(layout, horiz + " " + vert, expectedHoriz
                        | expectedVert);
                checkAlignment(layout, vert + " " + horiz, expectedHoriz
                        | expectedVert);

                // Three alignments should throw an exception
                boolean ok = false;
                try {
                    checkAlignment(layout, horiz + " " + vert + " " + horiz,
                            expectedHoriz | expectedVert);
                } catch (IllegalArgumentException e) {
                    // OK, "centert","rightb" etc are not valid
                    ok = true;
                }
                if (!ok) {
                    error("IllegalArgumentException was not thrown for "
                            + horiz + " " + vert + " " + horiz);
                }
            }
        }

        checkAlignment(layout, "left right", AlignmentHandler.ALIGNMENT_TOP
                | AlignmentHandler.ALIGNMENT_RIGHT);
    }

    private void checkAlignment(AlignmentHandler layout,
            String alignmentString, int expected) {
        layout.setComponentAlignment(label, AlignmentInfo.Bits.ALIGNMENT_TOP,
                AlignmentInfo.Bits.ALIGNMENT_LEFT);
        if (layout instanceof AbstractOrderedLayout) {
            ((AbstractOrderedLayout) layout).setComponentAlignment(label,
                    alignmentString);
        } else {
            ((GridLayout) layout).setComponentAlignment(label, alignmentString);
        }

        int actual = layout.getComponentAlignment(label).getBitMask();
        if (actual != expected) {
            String error = "Error " + alignmentString
                    + " did not produce expected results";
            error(error);
        } else {
            String str = layout.getClass().getSimpleName() + "/"
                    + alignmentString + ": OK";
            System.out.println(str);
        }

    }

    private void error(String error) {
        label.setValue(label.getValue() + error + "<br/>");
        System.out.println(error);
    }
}