aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/table/TableRepairsScrollPositionOnReAddingAllRowsTest.java
blob: 63a7e18ebee433fb3deda16765e5ef45fe2c94d7 (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
package com.vaadin.tests.components.table;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.number.IsCloseTo.closeTo;

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

import com.vaadin.testbench.elements.TableElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

/**
 * Scroll position should be restored when removing and re-adding all rows in
 * Table.
 *
 * @author Vaadin Ltd
 */
public class TableRepairsScrollPositionOnReAddingAllRowsTest
        extends MultiBrowserTest {

    private int rowLocation0;

    @Override
    @Before
    public void setup() throws Exception {
        super.setup();
        openTestURL();

        rowLocation0 = getCellY(0);
        scrollToBottom();
    }

    @Test
    public void testReAddAllViaAddAll() {
        int rowLocation = getCellY(70);

        // This button is for re-adding all rows (original itemIds) at once
        // (removeAll() + addAll())
        hitButton("buttonReAddAllViaAddAll");

        int newRowLocation = getCellY(70);

        assertCloseTo(
                "Scroll position should be the same as before Re-Adding rows via addAll()",
                newRowLocation, rowLocation);

    }

    @Test
    public void testReplaceByAnotherCollectionViaAddAll() {
        int rowLocation = getCellY(70);

        // This button is for replacing all rows at once (removeAll() +
        // addAll())
        hitButton("buttonReplaceByAnotherCollectionViaAddAll");

        // new collection has one less element
        int newRowLocation = getCellY(69);

        assertCloseTo(
                "Scroll position should be the same as before Replacing rows via addAll()",
                newRowLocation, rowLocation);
    }

    @Test
    public void testReplaceByAnotherCollectionViaAdd() {

        // This button is for replacing all rows one by one (removeAll() + add()
        // + add()..)
        hitButton("buttonReplaceByAnotherCollectionViaAdd");

        int newRowLocation = getCellY(0);

        assertCloseTo("Scroll position should be 0", newRowLocation,
                rowLocation0);
    }

    @Test
    public void testReplaceBySubsetOfSmallerSize() {
        // This button is for replacing all rows at once but the count of rows
        // is less then first index to scroll
        hitButton("buttonReplaceBySubsetOfSmallerSize");

        int newRowLocation = getCellY(5);

        assertCloseTo("Scroll position should be 0", newRowLocation,
                rowLocation0);
    }

    @Test
    public void testReplaceByWholeSubsetPlusOneNew() {
        int rowLocation = getCellY(70);

        // This button is for replacing by whole original sub-set of items plus
        // one new
        hitButton("buttonReplaceByWholeSubsetPlusOneNew");

        int newRowLocation = getCellY(70);

        assertCloseTo("Scroll position should be the same as before Replacing",
                newRowLocation, rowLocation);

    }

    @Test
    public void testRemoveAllAddOne() {
        // This button is for removing all and then adding only one new item
        hitButton("buttonRemoveAllAddOne");

        int newRowLocation = getCellY(0);

        assertCloseTo("Scroll position should be 0", newRowLocation,
                rowLocation0);
    }

    @Test
    public void testReplaceByNewDatasource() {
        // This button is for remove all items and add new datasource
        hitButton("buttonReplaceByNewDatasource");

        int newRowLocation = getCellY(0);

        assertCloseTo("Scroll position should be 0", newRowLocation,
                rowLocation0);
    }

    private TableElement getTable() {
        return $(TableElement.class).first();
    }

    private void scrollToBottom() {
        scrollTable(getTable(), 80, 70);
    }

    private int getCellY(int row) {
        return getTable().getCell(row, 0).getLocation().getY();
    }

    private void assertCloseTo(String reason, int actual, int expected) {
        // ranged check because IE9 consistently misses the mark by 1 pixel
        assertThat(reason, (double) actual, closeTo(expected, 1));
    }

}