aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/tests/design/DesignContextLocalIdTest.java
blob: 4819304cbad7de25283f661ec7916c1b7fc861e8 (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
package com.vaadin.tests.design;

import static org.junit.Assert.assertEquals;

import java.io.FileNotFoundException;

import org.jsoup.nodes.Element;
import org.junit.Test;

import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.SingleComponentContainer;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.Window;
import com.vaadin.ui.declarative.Design;
import com.vaadin.ui.declarative.DesignContext;

/**
 * Tests that setting local id via DesignContext works as intended.
 *
 * @author Vaadin Ltd
 */
public class DesignContextLocalIdTest {

    @Test
    public void testSetLocalId() throws FileNotFoundException {
        DesignContext ctx = Design.read(
                getClass().getResourceAsStream("local-ids.html"),
                new VerticalLayout());
        TextField tf = (TextField) ctx.getComponentByLocalId("foo");
        Button b = (Button) ctx.getComponentByLocalId("bar");
        // A duplicate id should be handled by removing the id from the old
        // component.
        ctx.setComponentLocalId(b, "foo");
        assertEquals("Found the wrong component by local id.",
                ctx.getComponentByLocalId("foo").getClass(), Button.class);
        assertEquals("Found the wrong component by local id.",
                ctx.getComponentByLocalId("bar"), null);
        // Set an id also for the text field.
        ctx.setComponentLocalId(tf, "bar");
        assertEquals("Found the wrong component by local id.",
                ctx.getComponentByLocalId("foo").getClass(), Button.class);
        assertEquals("Found the wrong component by local id.",
                ctx.getComponentByLocalId("bar").getClass(), TextField.class);
    }

    @Test
    public void testWriteLocalId() {
        DesignContext ctx = new DesignContext();

        Button b = new Button();
        ctx.setComponentLocalId(b, "button-id");

        assertEquals("button-id", ctx.createElement(b).attr("_id"));
    }

    @Test
    public void testWriteChildLocalIds() throws Exception {
        DesignContext ctx = new DesignContext();

        ComponentContainer[] ctrs = { new AbsoluteLayout(), new CssLayout(),
                new GridLayout(1, 1), new CustomLayout(),
                new HorizontalLayout(), new VerticalLayout(), new Accordion(),
                new HorizontalSplitPanel(), new TabSheet(),
                new VerticalSplitPanel() };

        Button b = new Button();
        ctx.setComponentLocalId(b, "button-id");

        for (ComponentContainer ctr : ctrs) {
            ctr.addComponent(b);
            Element e = ctx.createElement(ctr);
            assertEquals(
                    "Unexpected child local id for "
                            + ctr.getClass().getSimpleName(),
                    "button-id",
                    e.getElementsByTag("vaadin-button").first().attr("_id"));
        }

        SingleComponentContainer[] sctrs = { new Window(), new Panel() };

        for (SingleComponentContainer ctr : sctrs) {
            ctr.setContent(b);
            Element e = ctx.createElement(ctr);
            assertEquals(
                    "Unexpected child local id for "
                            + ctr.getClass().getSimpleName(),
                    "button-id",
                    e.getElementsByTag("vaadin-button").first().attr("_id"));
        }
    }

    @Test
    public void testGetLocalId() {
        DesignContext ctx = new DesignContext();
        Label label = new Label();
        ctx.setComponentLocalId(label, "my-local-id");
        ctx.setRootComponent(label);
        assertEquals("my-local-id", ctx.getComponentLocalId(label));
    }
}