aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/grid/GridColumnWidthsWithoutDataTest.java
blob: 7864f285c2e10698647e862a239db36e699e83b0 (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
package com.vaadin.tests.components.grid;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Test;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.testbench.elements.GridElement.GridCellElement;
import com.vaadin.testbench.elements.NativeSelectElement;
import com.vaadin.testbench.elements.NotificationElement;
import com.vaadin.testbench.parallel.TestCategory;
import com.vaadin.tests.tb3.SingleBrowserTest;

@TestCategory("grid")
public class GridColumnWidthsWithoutDataTest extends SingleBrowserTest {

    @Test
    public void testWidthsWhenAddingDataBack() {
        openTestURL();
        GridElement grid = $(GridElement.class).first();

        int[] baseWidths = getColWidths(grid);
        assertEquals("Sanity check", 2, baseWidths.length);

        assertTrue("Columns should not have equal width",
                Math.abs(baseWidths[0] - baseWidths[1]) > 2);

        removeData();

        assertSameWidths(baseWidths, getColWidths(grid));

        addData();

        assertSameWidths(baseWidths, getColWidths(grid));
    }

    @Test
    public void testWidthsWhenInitiallyEmpty() {
        setDebug(true);
        openTestURL();
        $(ButtonElement.class).caption("Recreate without data").first().click();

        GridElement grid = $(GridElement.class).first();

        int[] baseWidths = getColWidths(grid);
        assertEquals("Sanity check", 2, baseWidths.length);

        assertTrue("Columns should have roughly equal width",
                Math.abs(baseWidths[0] - baseWidths[1]) < 10);
        assertTrue("Columns should not have default widths",
                baseWidths[0] > 140);
        assertTrue("Columns should not have default widths",
                baseWidths[1] > 140);

        addData();

        assertSameWidths(baseWidths, getColWidths(grid));

        assertFalse("Notification was present",
                isElementPresent(NotificationElement.class));
    }

    @Test
    public void testMultiSelectWidths() {
        setDebug(true);
        openTestURL();
        $(NativeSelectElement.class).caption("Selection mode").first()
                .selectByText("MULTI");

        GridElement grid = $(GridElement.class).first();

        int sum = sumUsedWidths(grid);

        // 295 instead of 300 to avoid rounding issues
        assertTrue("Only " + sum + " out of 300px was used", sum > 295);

        $(ButtonElement.class).caption("Recreate without data").first().click();

        grid = $(GridElement.class).first();
        sum = sumUsedWidths(grid);

        // 295 instead of 300 to avoid rounding issues
        assertTrue("Only " + sum + " out of 300px was used", sum > 295);
    }

    private int sumUsedWidths(GridElement grid) {
        int sum = 0;
        for (int i : getColWidths(grid)) {
            sum += i;
        }
        return sum;
    }

    private static void assertSameWidths(int[] expected, int[] actual) {
        assertEquals("Arrays have differing lengths", expected.length,
                actual.length);

        for (int i = 0; i < expected.length; i++) {
            if (Math.abs(expected[i] - actual[i]) > 1) {
                fail("Differing sizes at index " + i + ". Expected "
                        + expected[i] + " but got " + actual[i]);
            }
        }
    }

    private void removeData() {
        $(ButtonElement.class).caption("Remove data").first().click();
    }

    private void addData() {
        $(ButtonElement.class).caption("Add data").first().click();
    }

    private int[] getColWidths(GridElement grid) {
        List<GridCellElement> headerCells = grid.getHeaderCells(0);
        int[] widths = new int[headerCells.size()];
        for (int i = 0; i < widths.length; i++) {
            widths[i] = headerCells.get(i).getSize().getWidth();
        }
        return widths;
    }
}