Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1c0686c1fb2f9d905a6df1c29956a5be7dfc5779 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2009 Holger Voormann and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Holger Voormann - initial API and implementation
 *     Igor Jacy Lino Campista - Java 5 warnings fixed (bug 311325)
 *******************************************************************************/
package org.eclipse.wst.xml.vex.core.internal.layout;

import java.util.Arrays;
import java.util.Stack;

import junit.framework.TestCase;

import org.eclipse.wst.xml.vex.core.internal.core.DisplayDevice;
import org.eclipse.wst.xml.vex.core.internal.css.MockDisplayDevice;
import org.eclipse.wst.xml.vex.core.internal.css.StyleSheet;
import org.eclipse.wst.xml.vex.core.internal.css.StyleSheetReader;
import org.eclipse.wst.xml.vex.core.internal.dom.Document;
import org.eclipse.wst.xml.vex.core.internal.dom.Element;
import org.eclipse.wst.xml.vex.core.internal.dom.RootElement;

public class TableLayoutTest extends TestCase {

    private static interface StackVisitor {
        void visit(StackElement element);
    }

    private static class StackElement {
        public final int indent;
        public final Box box;
        public StackElement(int indent, Box box) {
            this.indent = indent;
            this.box = box;
        }
    }

    private LayoutContext context;
    private Document document;
    private RootBox rootBox;
    private int caretPosition;

    @Override
    protected void setUp() throws Exception {

        // display dummy
        DisplayDevice.setCurrent(new MockDisplayDevice(90, 90));

        // context dummy
        context = new LayoutContext();
        context.setBoxFactory(new MockBoxFactory());
        context.setGraphics(new FakeGraphics());

        // set CSS
        String css =   "root   {display:block}"
                     + "inline {display:inline}"
                     + "table  {display:table}"
                     + "tcap   {display:table-caption}"
                     + "td     {display:table-cell}"
                     + "tc     {display:table-column}"
                     + "tcg    {display:table-column-group}"
                     + "tfg    {display:table-footer-group}"
                     + "thg    {display:table-header-group}"
                     + "tr     {display:table-row}"
                     + "trg    {display:table-row-group}";
        StyleSheet styleSheet = new StyleSheetReader().read(css);
        context.setStyleSheet(styleSheet);

        resetDocument();
    }

    @Override
    protected void tearDown() throws Exception {
        rootBox = null;
        document = null;
        context = null;
    }

    private void resetDocument() {
        document = new Document(new RootElement("root"));
        context.setDocument(document);
        caretPosition = 1;
        rootBox = new RootBox(this.context, document.getRootElement(), 500);
    }

    private void insertElement(String elementName) {
        document.insertElement(caretPosition, new Element(elementName));
        caretPosition++;
    }

    private void insertText(String text) {
        document.insertText(caretPosition, text);;
        caretPosition += text.length();
    }

    public void testValidTable() throws Exception {

        // single cell Table
        insertElement("table");
        insertElement("tr");
        insertElement("td");
        insertText("a");
        assertCount(1, TableBox.class);
        assertCount(1, TableRowBox.class);
        assertCount(1, TableCellBox.class);
        assertCount(1, DocumentTextBox.class);
        assertEquals("a", contentAsText());

        // 2x2 table plus text
        resetDocument();
        insertText("_");
        insertElement("table");
        insertElement("tr");
        insertElement("td");
        insertText("a");
        caretPosition++;
        insertElement("td");
        insertText("b");
        caretPosition+=2;
        insertElement("tr");
        insertElement("td");
        insertText("c");
        caretPosition++;
        insertElement("td");
        insertText("d");
        assertCount(1, TableBox.class);
        assertCount(2, TableRowBox.class);
        assertCount(4, TableCellBox.class);
        assertCount(5, DocumentTextBox.class);
        assertEquals("_abcd", contentAsText());
    }

    // table elements outside table (separately tested to improve tracing if
    // StackOverflowError will be thrown)
    public void testCaptionOutsideTable()     { test("tcap"); }
    public void testCellOutsideTable()        { test("td"); }
    public void testColumnOutsideTable()      { test("tc"); }
    public void testColumnGroupOutsideTable() { test("tcg"); }
    public void testFooterGroupOutsideTable() { test("tfg"); }
    public void testHeaderGroupOutsideTable() { test("thg"); }
    public void testRowOutsideTable()         { test("tr"); }
    public void testRowGroupOutsideTable()    { test("trg"); }
    
    // invalid nested table elements (separately tested to improve tracing if
    // StackOverflowError will be thrown)
    public void testInvalidNesting1() { test("inline", "tcap"); }
    public void testInvalidNesting2() { test("table", "td"); }
    public void testInvalidNesting3() { test("td", "tr"); }
    public void testInvalidNesting4() { test("trg", "trg"); }
    public void testInvalidNesting5() { test("tr", "tfg"); }
    public void testInvalidNesting6() { test("td", "thg"); }
    public void testInvalidNesting7() { test("table", "tc"); }
    public void testInvalidNesting8() { test("thg", "tcg"); }
    
    public void test(String ... elements) {
        resetDocument();
        insertElement("inline");
        for (String element : elements) {
        	insertElement(element);
		}
        insertText("x");
        assertCount(1, DocumentTextBox.class);
        assertEquals("x", contentAsText());
    }
    
    private String contentAsText() {
        return document.getText(0, document.getLength());
    }

    private void assertCount(int expected, Class<? extends Box> blockClass) {
        int count = count(blockClass);
        String message =   "expected count of <"
                         + blockClass.getSimpleName()
                         + ">: <"
                         + expected
                         + "> but was: <"
                         + count
                         + ">\n"
                         + "Actual layout stack trace:\n"
                         + layoutStackToString();
        assertEquals(message, expected, count);
    }

    private int count(final Class<? extends Box> blockClass) {
        final int[] mutableInteger = new int[1];
        mutableInteger[0] = 0;
        travelLayoutStack(new StackVisitor() {

            public void visit(StackElement element) {
                if (element.box.getClass().equals(blockClass)) {
                    mutableInteger[0]++;
                }
            }

        });
        return mutableInteger[0];
    }

    private String layoutStackToString() {
        final StringBuilder result = new StringBuilder();
        travelLayoutStack(new StackVisitor() {

            public void visit(StackElement element) {
                if (element.indent > 0) {
                    char[] indentChars = new char[element.indent * 2];
                    Arrays.fill(indentChars, ' ');
                    result.append(indentChars);
                }
                result.append(element.box.getClass().getSimpleName());
                result.append('\n');

            }

        });
        return result.toString();
    }

    private void travelLayoutStack(StackVisitor visitor) {

        // already layouted?
        Box[] rootElementChildren = rootBox.getChildren()[0].getChildren();
        if (rootElementChildren == null || rootElementChildren.length == 0) {
            rootBox.layout(this.context, 0, Integer.MAX_VALUE);
        }

        Stack<StackElement> stack = new Stack<StackElement>();
        stack.push(new StackElement(0, rootBox));
        while (!stack.isEmpty()) {
            StackElement current = stack.pop();
            visitor.visit(current);

            // iterate deep-first
            for (Box child : current.box.getChildren()) {
            	stack.push(new StackElement(current.indent + 1, child));
			}
        }
    }

}

Back to the top