2 * Copyright 2000-2016 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.tests.components.table;
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.number.IsCloseTo.closeTo;
21 import org.junit.Before;
22 import org.junit.Test;
24 import com.vaadin.tests.tb3.MultiBrowserTest;
25 import com.vaadin.v7.testbench.customelements.TableElement;
28 * Scroll position should be restored when removing and re-adding all rows in
33 public class TableRepairsScrollPositionOnReAddingAllRowsTest
34 extends MultiBrowserTest {
36 private int rowLocation0;
40 public void setup() throws Exception {
44 rowLocation0 = getCellY(0);
49 public void testReAddAllViaAddAll() {
50 int rowLocation = getCellY(70);
52 // This button is for re-adding all rows (original itemIds) at once
53 // (removeAll() + addAll())
54 hitButton("buttonReAddAllViaAddAll");
56 int newRowLocation = getCellY(70);
59 "Scroll position should be the same as before Re-Adding rows via addAll()",
60 newRowLocation, rowLocation);
65 public void testReplaceByAnotherCollectionViaAddAll() {
66 int rowLocation = getCellY(70);
68 // This button is for replacing all rows at once (removeAll() +
70 hitButton("buttonReplaceByAnotherCollectionViaAddAll");
72 // new collection has one less element
73 int newRowLocation = getCellY(69);
76 "Scroll position should be the same as before Replacing rows via addAll()",
77 newRowLocation, rowLocation);
81 public void testReplaceByAnotherCollectionViaAdd() {
83 // This button is for replacing all rows one by one (removeAll() + add()
85 hitButton("buttonReplaceByAnotherCollectionViaAdd");
87 int newRowLocation = getCellY(0);
89 assertCloseTo("Scroll position should be 0", newRowLocation,
94 public void testReplaceBySubsetOfSmallerSize() {
95 // This button is for replacing all rows at once but the count of rows
96 // is less then first index to scroll
97 hitButton("buttonReplaceBySubsetOfSmallerSize");
99 int newRowLocation = getCellY(5);
101 assertCloseTo("Scroll position should be 0", newRowLocation,
106 public void testReplaceByWholeSubsetPlusOneNew() {
107 int rowLocation = getCellY(70);
109 // This button is for replacing by whole original sub-set of items plus
111 hitButton("buttonReplaceByWholeSubsetPlusOneNew");
113 int newRowLocation = getCellY(70);
115 assertCloseTo("Scroll position should be the same as before Replacing",
116 newRowLocation, rowLocation);
121 public void testRemoveAllAddOne() {
122 // This button is for removing all and then adding only one new item
123 hitButton("buttonRemoveAllAddOne");
125 int newRowLocation = getCellY(0);
127 assertCloseTo("Scroll position should be 0", newRowLocation,
132 public void testReplaceByNewDatasource() {
133 // This button is for remove all items and add new datasource
134 hitButton("buttonReplaceByNewDatasource");
136 int newRowLocation = getCellY(0);
138 assertCloseTo("Scroll position should be 0", newRowLocation,
142 private TableElement getTable() {
143 return $(TableElement.class).first();
146 private void scrollToBottom() {
147 scrollTable(getTable(), 80, 70);
150 private int getCellY(int row) {
151 return getTable().getCell(row, 0).getLocation().getY();
154 private void assertCloseTo(String reason, int actual, int expected) {
155 // ranged check because IE9 consistently misses the mark by 1 pixel
156 assertThat(reason, (double) actual, closeTo(expected, 1));