aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/tests/design/ParseLayoutTest.java
blob: e8756e160a03dc7d862a87e43d6eb807bc5a074e (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
/*
 * Copyright 2000-2016 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.tests.design;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.declarative.Design;
import com.vaadin.ui.declarative.DesignContext;
import com.vaadin.ui.declarative.DesignException;

/**
 * A test for checking that parsing a layout preserves the IDs and the mapping
 * from prefixes to package names (for example
 * <meta name=”package-mapping” content=”my:com.addon.mypackage” />)
 *
 * @since
 * @author Vaadin Ltd
 */
public class ParseLayoutTest {
    // The context is used for accessing the created component hierarchy.
    private DesignContext ctx;

    @Before
    public void setUp() throws Exception {
        ctx = Design.read(getClass().getResourceAsStream("testFile.html"),
                null);
    }

    @Test
    public void buttonWithIdIsParsed() {
        Component button = ctx.getComponentByLocalId("firstButton");

        assertThat(ctx.getComponentByCaption("Native click me"), is(button));
        assertThat(button.getCaption(), is("Native click me"));
    }

    @Test
    public void buttonWithIdAndLocalIdIsParsed() {
        Component button = ctx.getComponentById("secondButton");

        assertThat(ctx.getComponentByCaption("Another button"), is(button));
        assertThat(ctx.getComponentByLocalId("localID"), is(button));
        assertThat(button.getCaption(), is("Another button"));
    }

    @Test
    public void buttonWithoutIdsIsParsed() {
        assertThat(ctx.getComponentByCaption("Yet another button"),
                is(not(nullValue())));
    }

    @Test
    public void serializationPreservesProperties() throws IOException {
        ByteArrayOutputStream out = serializeDesign(ctx);
        ctx = deSerializeDesign(out);

        assertButtonProperties();
    }

    @Test
    public void serializationPreservesHierarchy() throws IOException {
        ByteArrayOutputStream out = serializeDesign(ctx);
        ctx = deSerializeDesign(out);

        assertComponentHierarchy();
    }

    @Test
    public void designIsSerializedWithCorrectPrefixesAndPackageNames()
            throws IOException {
        ByteArrayOutputStream out = serializeDesign(ctx);

        // Check the mapping from prefixes to package names using the html tree
        String[] expectedPrefixes = { "my" };
        String[] expectedPackageNames = { "com.addon.mypackage" };
        int index = 0;

        Document doc = Jsoup.parse(out.toString("UTF-8"));
        Element head = doc.head();
        for (Node child : head.childNodes()) {
            if ("meta".equals(child.nodeName())) {
                String name = child.attributes().get("name");
                if ("package-mapping".equals(name)) {
                    String content = child.attributes().get("content");
                    String[] parts = content.split(":");
                    assertEquals("Unexpected prefix.", expectedPrefixes[index],
                            parts[0]);
                    assertEquals("Unexpected package name.",
                            expectedPackageNames[index], parts[1]);
                    index++;
                }
            }
        }
        assertEquals("Unexpected number of prefix - package name pairs.", 1,
                index);
    }

    private DesignContext deSerializeDesign(ByteArrayOutputStream out) {
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        return Design.read(in, null);
    }

    private ByteArrayOutputStream serializeDesign(DesignContext context)
            throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Design.write(context, out);

        return out;
    }

    private void assertButtonProperties() {
        buttonWithIdAndLocalIdIsParsed();
        buttonWithIdIsParsed();
        buttonWithoutIdsIsParsed();
    }

    @Test
    public void fieldsAreBoundToATemplate() throws IOException {
        LayoutTemplate template = new LayoutTemplate();
        InputStream htmlFile = getClass().getResourceAsStream("testFile.html");
        Design.read(htmlFile, template);
        assertNotNull(template.getFirstButton());
        assertNotNull(template.getSecondButton());
        assertNotNull(template.getYetanotherbutton());
        assertNotNull(template.getClickme());
        assertEquals("Native click me", template.getFirstButton().getCaption());
    }

    @Test(expected = DesignException.class)
    public void fieldsCannotBeBoundToAnInvalidTemplate() throws IOException {
        InvalidLayoutTemplate template = new InvalidLayoutTemplate();
        InputStream htmlFile = getClass().getResourceAsStream("testFile.html");

        Design.read(htmlFile, template);
    }

    @Test
    public void rootHasCorrectComponents() {
        Component root = ctx.getRootComponent();

        VerticalLayout vlayout = (VerticalLayout) root;

        assertThat(vlayout.getComponentCount(), is(3));
    }

    @Test
    public void rootChildHasCorrectComponents() {
        Component root = ctx.getRootComponent();
        VerticalLayout vlayout = (VerticalLayout) root;
        HorizontalLayout hlayout = (HorizontalLayout) vlayout.getComponent(0);

        assertThat(hlayout.getComponentCount(), is(5));
        assertThat(hlayout.getComponent(0).getCaption(), is("FooBar"));
        assertThat(hlayout.getComponent(1).getCaption(), is("Native click me"));
        assertThat(hlayout.getComponent(2).getCaption(), is("Another button"));
        assertThat(hlayout.getComponent(3).getCaption(),
                is("Yet another button"));
        assertThat(hlayout.getComponent(4).getCaption(), is("Click me"));
        assertThat(hlayout.getComponent(4).getWidth(), is(150f));

        // Check the remaining two components of the vertical layout
        assertTextField(vlayout);
        assertTextArea(vlayout);
    }

    private void assertComponentHierarchy() {
        rootHasCorrectComponents();
        rootChildHasCorrectComponents();
    }

    private void assertTextField(VerticalLayout vlayout) {
        TextField tf = (TextField) vlayout.getComponent(1);

        assertThat(tf.getCaption(), is("Text input"));
    }

    private void assertTextArea(VerticalLayout layout) {
        TextArea ta = (TextArea) layout.getComponent(2);

        assertThat(ta.getCaption(), is("Text area"));
        assertThat(ta.getWidth(), is(300f));
        assertThat(ta.getHeight(), is(200f));
    }
}