aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/ui/components/grid/StaticSectionTest.java
blob: c030cc16ed70d6de0a458c7ef51147781dfb728b (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
package com.vaadin.ui.components.grid;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.HashSet;

import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.data.ValueProvider;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;

public class StaticSectionTest {
    private final Grid<String> grid = new Grid<>();
    private final Column<String, String> col1 = grid
            .addColumn(ValueProvider.identity()).setId("col1");
    private final Column<String, String> col2 = grid
            .addColumn(ValueProvider.identity()).setId("col2");
    private final Column<String, String> col3 = grid
            .addColumn(ValueProvider.identity()).setId("col3");

    private HeaderRow headerRow;
    private FooterRow footerRow;

    @Before
    public void setUp() {
        footerRow = grid.addFooterRowAt(0);
        headerRow = grid.addHeaderRowAt(0);
    }

    @Test
    public void joinFootersBySet() {
        footerRow.join(new HashSet<>(Arrays.asList(footerRow.getCell(col1),
                footerRow.getCell(col2))));

        assertFootersJoined();
    }

    @Test
    public void joinFootersByCells() {
        footerRow.join(footerRow.getCell(col1), footerRow.getCell(col2));

        assertFootersJoined();
    }

    @Test
    public void joinFootersByColumns() {
        footerRow.join(col1, col2);

        assertFootersJoined();
    }

    @Test
    public void joinFootersByIds() {
        footerRow.join("col1", "col2");

        assertFootersJoined();
    }

    @Test
    public void joinHeadersBySet() {
        headerRow.join(new HashSet<>(Arrays.asList(headerRow.getCell(col1),
                headerRow.getCell(col2))));

        assertHeadersJoined();
    }

    @Test
    public void joinHeadersByCells() {
        headerRow.join(headerRow.getCell(col1), headerRow.getCell(col2));

        assertHeadersJoined();
    }

    @Test
    public void joinHeadersByColumns() {
        headerRow.join(col1, col2);

        assertHeadersJoined();
    }

    @Test
    public void joinHeadersByIds() {
        headerRow.join("col1", "col2");

        assertHeadersJoined();
    }

    @Test(expected = IllegalStateException.class)
    public void joinHeadersByMissingIds() {
        headerRow.join("col1", "col4");
    }

    @Test(expected = IllegalStateException.class)
    public void joinFootersByMissingIds() {
        headerRow.join("col1", "col4");
    }

    private void assertFootersJoined() {
        assertJoined((StaticSection.StaticRow<?>) footerRow);
    }

    private void assertHeadersJoined() {
        assertJoined((StaticSection.StaticRow<?>) headerRow);
    }

    private static void assertJoined(StaticSection.StaticRow<?> staticRow) {
        // There doesn't seem to be any direct API for checking what's joined,
        // so verifying the merging by checking the declarative output
        Element container = new Element(Tag.valueOf("container"), "");

        staticRow.writeDesign(container, null);

        assertEquals(2, container.children().size());
        assertEquals("col1,col2", container.child(0).attr("column-ids"));
        assertEquals("col3", container.child(1).attr("column-ids"));
    }

}