]> source.dussan.org Git - vaadin-framework.git/commitdiff
New tests for Removing and re-adding all rows in Table (#14581)
authorGuillermo Alvarez <guillermo@vaadin.com>
Thu, 16 Oct 2014 12:40:51 +0000 (15:40 +0300)
committerSauli Tähkäpää <sauli@vaadin.com>
Mon, 10 Nov 2014 09:50:16 +0000 (11:50 +0200)
Refactor of test into separate tests.
Addition of new cases to test when a new container is added and
when all items are removed and only one item is added

Change-Id: I6103404d1f21c02d7469f61ce942f7379b00a17b

uitest/src/com/vaadin/tests/components/table/TableRepairsScrollPositionOnReAddingAllRows.java
uitest/src/com/vaadin/tests/components/table/TableRepairsScrollPositionOnReAddingAllRowsTest.java

index df06580dae35c82dfd51706b6edb11a051d43a7a..bee0839663eab63249b8011778f1e957d02f0802 100644 (file)
@@ -115,11 +115,11 @@ public class TableRepairsScrollPositionOnReAddingAllRows extends AbstractTestUI
                     }
                 });
 
-        Button buttonReplaceByWholeSubsetPlusOnNew = new Button(
+        Button buttonReplaceByWholeSubsetPlusOneNew = new Button(
                 "Replace rows by whole subset plus one new item");
-        buttonReplaceByWholeSubsetPlusOnNew
-                .setId("buttonReplaceByWholeSubsetPlusOnNew");
-        buttonReplaceByWholeSubsetPlusOnNew
+        buttonReplaceByWholeSubsetPlusOneNew
+                .setId("buttonReplaceByWholeSubsetPlusOneNew");
+        buttonReplaceByWholeSubsetPlusOneNew
                 .addClickListener(new ClickListener() {
 
                     @Override
@@ -136,6 +136,40 @@ public class TableRepairsScrollPositionOnReAddingAllRows extends AbstractTestUI
                     }
                 });
 
+        Button buttonRemoveAllAddOne = new Button(
+                "Remove all items and add only one new item");
+        buttonRemoveAllAddOne.setId("buttonRemoveAllAddOne");
+        buttonRemoveAllAddOne.addClickListener(new ClickListener() {
+
+            @Override
+            public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
+                cont.removeAllItems();
+                TableItem ti = new TableItem();
+                ti.setName("Item_" + 20);
+                cont.addBean(ti);
+            }
+        });
+
+        // This should be the last test as it changes the table datasource
+        Button buttonReplaceByNewDatasource = new Button(
+                "Remove all items and add new datasource");
+        buttonReplaceByNewDatasource.setId("buttonReplaceByNewDatasource");
+        buttonReplaceByNewDatasource.addClickListener(new ClickListener() {
+
+            @Override
+            public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
+                cont.removeAllItems();
+                BeanItemContainer<TableItem> newContainer = new BeanItemContainer<TableItem>(
+                        TableItem.class);
+                for (int i = 0; i < 50; i++) {
+                    TableItem ti = new TableItem();
+                    ti.setName("Item_" + i);
+                    newContainer.addBean(ti);
+                }
+                table.setContainerDataSource(newContainer);
+            }
+        });
+
         for (int i = 0; i < 80; i++) {
             TableItem ti = new TableItem();
             ti.setName("Item_" + i);
@@ -147,7 +181,9 @@ public class TableRepairsScrollPositionOnReAddingAllRows extends AbstractTestUI
         getLayout().addComponent(buttonReplaceByAnotherCollectionViaAddAll);
         getLayout().addComponent(buttonReplaceByAnotherCollectionViaAdd);
         getLayout().addComponent(buttonReplaceBySubsetOfSmallerSize);
-        getLayout().addComponent(buttonReplaceByWholeSubsetPlusOnNew);
+        getLayout().addComponent(buttonReplaceByWholeSubsetPlusOneNew);
+        getLayout().addComponent(buttonRemoveAllAddOne);
+        getLayout().addComponent(buttonReplaceByNewDatasource);
         getLayout().addComponent(buttonRestore);
         getLayout().addComponent(table);
     }
index a3e7f29dfed1c4798640adbce03ea17422f280fa..4ca3ed406b22f25cfcd2874faf15a0a60796482f 100644 (file)
@@ -18,17 +18,10 @@ 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 org.openqa.selenium.By;
-import org.openqa.selenium.JavascriptExecutor;
-import org.openqa.selenium.WebDriver;
-import org.openqa.selenium.WebElement;
-import org.openqa.selenium.support.ui.ExpectedCondition;
 
-import com.vaadin.testbench.commands.TestBenchCommandExecutor;
 import com.vaadin.testbench.elements.TableElement;
-import com.vaadin.testbench.screenshot.ImageComparison;
-import com.vaadin.testbench.screenshot.ReferenceNameGenerator;
 import com.vaadin.tests.tb3.MultiBrowserTest;
 
 /**
@@ -40,138 +33,127 @@ import com.vaadin.tests.tb3.MultiBrowserTest;
 public class TableRepairsScrollPositionOnReAddingAllRowsTest extends
         MultiBrowserTest {
 
-    @Test
-    public void testScrollRepairsAfterReAddingAllRows()
-            throws InterruptedException {
-        openTestURL();
-
-        WebElement row0 = $(TableElement.class).first().getCell(0, 0);
-        int rowLocation0 = row0.getLocation().getY();
+    private int rowLocation0;
 
-        // case 1
-        scrollUp();
+    @Override
+    @Before
+    public void setup() throws Exception {
+        super.setup();
+        openTestURL();
 
-        waitUntilNot(new ExpectedCondition<Boolean>() {
-            @Override
-            public Boolean apply(WebDriver input) {
-                return $(TableElement.class).first().getCell(48, 0) == null;
-            }
-        }, 10);
+        rowLocation0 = getCellY(0);
+        scrollToBottom();
+    }
 
-        WebElement row = $(TableElement.class).first().getCell(48, 0);
-        int rowLocation = row.getLocation().getY();
+    @Test
+    public void testReAddAllViaAddAll() {
+        int rowLocation = getCellY(70);
 
         // This button is for re-adding all rows (original itemIds) at once
         // (removeAll() + addAll())
         hitButton("buttonReAddAllViaAddAll");
 
-        row = $(TableElement.class).first().getCell(48, 0);
-        int newRowLocation = row.getLocation().getY();
+        int newRowLocation = getCellY(70);
 
-        // ranged check because IE9 consistently misses the mark by 1 pixel
-        assertThat(
+        assertCloseTo(
                 "Scroll position should be the same as before Re-Adding rows via addAll()",
-                (double) newRowLocation,
-                closeTo(rowLocation, row.getSize().height + 1));
-
-        // case 2
-        scrollUp();
+                newRowLocation, rowLocation);
 
-        waitUntilNot(new ExpectedCondition<Boolean>() {
-            @Override
-            public Boolean apply(WebDriver input) {
-                return $(TableElement.class).first().getCell(48, 0) == null;
-            }
-        }, 10);
+    }
 
-        row = $(TableElement.class).first().getCell(48, 0);
-        rowLocation = row.getLocation().getY();
+    @Test
+    public void testReplaceByAnotherCollectionViaAddAll() {
+        int rowLocation = getCellY(70);
 
         // This button is for replacing all rows at once (removeAll() +
         // addAll())
         hitButton("buttonReplaceByAnotherCollectionViaAddAll");
 
-        row = $(TableElement.class).first().getCell(48, 0);
-        newRowLocation = row.getLocation().getY();
+        // new collection has one less element
+        int newRowLocation = getCellY(69);
 
-        // ranged check because IE9 consistently misses the mark by 1 pixel
-        assertThat(
+        assertCloseTo(
                 "Scroll position should be the same as before Replacing rows via addAll()",
-                (double) newRowLocation,
-                closeTo(rowLocation, row.getSize().height + 1));
+                newRowLocation, rowLocation);
+    }
+
+    @Test
+    public void testReplaceByAnotherCollectionViaAdd() {
 
-        // case 3
         // This button is for replacing all rows one by one (removeAll() + add()
         // + add()..)
         hitButton("buttonReplaceByAnotherCollectionViaAdd");
 
-        row = $(TableElement.class).first().getCell(0, 0);
-        newRowLocation = row.getLocation().getY();
+        int newRowLocation = getCellY(0);
 
-        // ranged check because IE9 consistently misses the mark by 1 pixel
-        assertThat("Scroll position should be 0", (double) newRowLocation,
-                closeTo(rowLocation0, 1));
-
-        // case 4
-        // This button is for restoring initial list and for scrolling to 0
-        // position
-        hitButton("buttonRestore");
-        scrollUp();
-
-        waitUntilNot(new ExpectedCondition<Boolean>() {
-            @Override
-            public Boolean apply(WebDriver input) {
-                return $(TableElement.class).first().getCell(48, 0) == null;
-            }
-        }, 10);
+        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");
 
-        row = $(TableElement.class).first().getCell(5, 0);
+        int newRowLocation = getCellY(5);
 
-        newRowLocation = row.getLocation().getY();
+        assertCloseTo("Scroll position should be 0", newRowLocation,
+                rowLocation0);
+    }
 
-        // ranged check because IE9 consistently misses the mark by 1 pixel
-        assertThat("Scroll position should be 0", (double) newRowLocation,
-                closeTo(rowLocation0, 1));
+    @Test
+    public void testReplaceByWholeSubsetPlusOneNew() {
+        int rowLocation = getCellY(70);
 
-        // case 5
-        // This button is for restoring initial list and for scrolling to 0
-        // position
-        hitButton("buttonRestore");
-        scrollUp();
+        // This button is for replacing by whole original sub-set of items plus
+        // one new
+        hitButton("buttonReplaceByWholeSubsetPlusOneNew");
 
-        waitUntilNot(new ExpectedCondition<Boolean>() {
-            @Override
-            public Boolean apply(WebDriver input) {
-                return $(TableElement.class).first().getCell(48, 0) == null;
-            }
-        }, 10);
+        int newRowLocation = getCellY(70);
 
-        row = $(TableElement.class).first().getCell(48, 0);
-        rowLocation = row.getLocation().getY();
+        assertCloseTo("Scroll position should be the same as before Replacing",
+                newRowLocation, rowLocation);
 
-        // This button is for replacing by whole original sub-set of items plus
-        // one new
-        hitButton("buttonReplaceByWholeSubsetPlusOnNew");
+    }
 
-        row = $(TableElement.class).first().getCell(48, 0);
-        newRowLocation = row.getLocation().getY();
+    @Test
+    public void testRemoveAllAddOne() {
+        // This button is for removing all and then adding only one new item
+        hitButton("buttonRemoveAllAddOne");
 
-        // ranged check because IE9 consistently misses the mark by 1 pixel
-        assertThat("Scroll position should be the same as before Replacing",
-                (double) newRowLocation,
-                closeTo(rowLocation, row.getSize().height + 1));
+        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 void scrollUp() {
-        WebElement actualElement = getDriver().findElement(
-                By.className("v-table-body-wrapper"));
-        JavascriptExecutor js = new TestBenchCommandExecutor(getDriver(),
-                new ImageComparison(), new ReferenceNameGenerator());
-        js.executeScript("arguments[0].scrollTop = " + 1205, actualElement);
+    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));
+    }
+
 }