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

import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.number.IsCloseTo.closeTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.CheckBoxElement;
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.tests.tb3.SingleBrowserTest;

public class GridRecalculateColumnWidthNewItemTest extends SingleBrowserTest {

    GridElement grid;
    ButtonElement addButton;
    ButtonElement removeButton;

    @Before
    public void init() {
        openTestURL();
        grid = $(GridElement.class).first();
        addButton = $(ButtonElement.class).id("add");
        removeButton = $(ButtonElement.class).id("remove");
    }

    @Test
    public void recalculateAfterAddingAndRemovingWorks() throws IOException {
        assertEquals("CheckBox should be checked.", "checked",
                $(CheckBoxElement.class).first().getValue());

        int initialWidth = grid.getHeaderCell(0, 0).getSize().width;

        addButton.click();
        int newWidth = grid.getHeaderCell(0, 0).getSize().width;
        // ensure the column width has increased significantly
        assertThat(
                "Unexpected column width after adding a row and calling recalculate.",
                (double) newWidth, not(closeTo(initialWidth, 20)));

        removeButton.click();
        newWidth = grid.getHeaderCell(0, 0).getSize().width;
        // ensure the column width has decreased significantly (even if it might
        // not be exactly the original width)
        assertThat(
                "Unexpected column width after removing a row and calling recalculate.",
                (double) newWidth, closeTo(initialWidth, 2));
    }

    @Test
    public void addingWithoutRecalculateWorks() throws IOException {
        CheckBoxElement checkBox = $(CheckBoxElement.class).first();
        checkBox.click();
        assertEquals("CheckBox should not be checked.", "unchecked",
                checkBox.getValue());

        int initialWidth = grid.getHeaderCell(0, 0).getSize().width;

        addButton.click();
        int newWidth = grid.getHeaderCell(0, 0).getSize().width;
        // ensure the column width did not change significantly
        assertThat(
                "Unexpected column width after adding a row without calling recalculate.",
                (double) newWidth, closeTo(initialWidth, 2));
    }

    @Test
    public void removingWithoutRecalculateWorks() throws IOException {
        // add a row before unchecking
        addButton.click();

        CheckBoxElement checkBox = $(CheckBoxElement.class).first();
        checkBox.click();
        assertEquals("CheckBox should not be checked.", "unchecked",
                checkBox.getValue());

        int initialWidth = grid.getHeaderCell(0, 0).getSize().width;

        removeButton.click();
        int newWidth = grid.getHeaderCell(0, 0).getSize().width;
        // ensure the column width did not change significantly
        assertThat(
                "Unexpected column width after removing a row without calling recalculate.",
                (double) newWidth, closeTo(initialWidth, 2));
    }
}