]> source.dussan.org Git - vaadin-framework.git/commitdiff
Tests behavior rather than log output (#13334)
authorHenrik Paul <henrik@vaadin.com>
Mon, 29 Sep 2014 11:25:35 +0000 (14:25 +0300)
committerVaadin Code Review <review@vaadin.com>
Thu, 2 Oct 2014 12:48:58 +0000 (12:48 +0000)
Change-Id: Id00aa2d79251a6235486d39e66552a9ed4b3c534

uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridSortingTest.java

index acc5bfe51aa0e3f833dc190be0e15d302c41f2b9..e66b8b36d6d47381b51ebc0ff5903d6e21036a6e 100644 (file)
@@ -18,25 +18,53 @@ package com.vaadin.tests.components.grid.basicfeatures.server;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
+import static org.junit.Assert.fail;
 
 import org.junit.Test;
 import org.openqa.selenium.Keys;
 import org.openqa.selenium.interactions.Actions;
 
+import com.vaadin.shared.ui.grid.SortDirection;
 import com.vaadin.tests.components.grid.GridElement;
+import com.vaadin.tests.components.grid.GridElement.GridCellElement;
 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
 
 public class GridSortingTest extends GridBasicFeaturesTest {
 
+    private static class SortInfo {
+        public final int sortOrder;
+        public final SortDirection sortDirection;
+
+        private SortInfo(int sortOrder, SortDirection sortDirection) {
+            this.sortOrder = sortOrder;
+            this.sortDirection = sortDirection;
+        }
+    }
+
+    private static class SortInfoWithColumn extends SortInfo {
+        public final int columnIndex;
+
+        private SortInfoWithColumn(int columnIndex, int sortOrder,
+                SortDirection sortDirection) {
+            super(sortOrder, sortDirection);
+            this.columnIndex = columnIndex;
+        }
+    }
+
+    private static SortInfo _(int sortOrder, SortDirection sortDirection) {
+        return new SortInfo(sortOrder, sortDirection);
+    }
+
+    private static SortInfoWithColumn _(int columnIndex, int sortOrder,
+            SortDirection sortDirection) {
+        return new SortInfoWithColumn(columnIndex, sortOrder, sortDirection);
+    }
+
     @Test
-    public void testProgrammaticSorting() throws IOException {
+    public void testProgrammaticSorting() throws Exception {
         openTestURL();
 
-        GridElement grid = getGridElement();
-
         // Sorting by column 9 is sorting by row index that is represented as a
         // String.
         // First cells for first 3 rows are (9, 0), (99, 0) and (999, 0)
@@ -44,42 +72,33 @@ public class GridSortingTest extends GridBasicFeaturesTest {
 
         // Verify that programmatic sorting calls are identified as originating
         // from API
-        assertEquals("3. Sort order: [Column 9 DESCENDING] by API",
-                getLogRow(0));
-
-        assertTrue("Column 9 should have the sort-desc stylename", grid
-                .getHeaderCell(0, 9).getAttribute("class")
-                .contains("sort-desc"));
+        assertColumnsAreSortedAs(_(9, 1, SortDirection.DESCENDING));
 
         String row = "";
         for (int i = 0; i < 3; ++i) {
             row += "9";
-            assertEquals(
-                    "Grid is not sorted by Column 9 using descending direction.",
-                    "(" + row + ", 0)", grid.getCell(i, 0).getText());
+            String expected = "(" + row + ", 0)";
+            String cellValue = getGridElement().getCell(i, 0).getText();
+            assertEquals("Grid is not sorted by Column 9 "
+                    + "using descending direction.", expected, cellValue);
         }
 
         // Column 10 is random numbers from Random with seed 13334
         sortBy("Column 10, ASC");
 
-        assertFalse(
-                "Column 9 should no longer have the sort-desc stylename",
-                grid.getHeaderCell(0, 9).getAttribute("class")
+        assertFalse("Column 9 should no longer have the sort-desc stylename",
+                getGridElement().getHeaderCell(0, 9).getAttribute("class")
                         .contains("sort-desc"));
-        assertTrue("Column 10 should have the sort-asc stylename", grid
-                .getHeaderCell(0, 10).getAttribute("class")
-                .contains("sort-asc"));
 
-        // Not cleaning up correctly causes exceptions when scrolling.
-        grid.scrollToRow(50);
-        assertFalse("Scrolling caused and exception when shuffled.",
-                getLogRow(0).contains("Exception"));
+        assertColumnsAreSortedAs(_(10, 1, SortDirection.ASCENDING));
 
         for (int i = 0; i < 5; ++i) {
-            assertGreater(
-                    "Grid is not sorted by Column 10 using ascending direction",
-                    Integer.parseInt(grid.getCell(i + 1, 10).getText()),
-                    Integer.parseInt(grid.getCell(i, 10).getText()));
+            Integer firstRow = Integer.valueOf(getGridElement().getCell(i + 1,
+                    10).getText());
+            Integer secondRow = Integer.valueOf(getGridElement().getCell(i, 10)
+                    .getText());
+            assertGreater("Grid is not sorted by Column 10 using"
+                    + " ascending direction", firstRow, secondRow);
 
         }
 
@@ -87,24 +106,22 @@ public class GridSortingTest extends GridBasicFeaturesTest {
         // 2, 1 and 0.
         sortBy("Column 7, DESC");
         for (int i = 0; i < 3; ++i) {
-            assertEquals(
-                    "Grid is not sorted by Column 7 using descending direction",
-                    "(" + i + ", 0)",
-                    grid.getCell(GridBasicFeatures.ROWS - (i + 1), 0).getText());
+            String expected = "(" + i + ", 0)";
+            String cellContent = getGridElement().getCell(
+                    GridBasicFeatures.ROWS - (i + 1), 0).getText();
+            assertEquals("Grid is not sorted by Column 7 using "
+                    + "descending direction", expected, cellContent);
         }
 
-        assertFalse(
-                "Column 10 should no longer have the sort-asc stylename",
-                grid.getHeaderCell(0, 10).getAttribute("class")
+        assertFalse("Column 10 should no longer have the sort-asc stylename",
+                getGridElement().getHeaderCell(0, 10).getAttribute("class")
                         .contains("sort-asc"));
-        assertTrue("Column 7 should have the sort-desc stylename", grid
-                .getHeaderCell(0, 7).getAttribute("class")
-                .contains("sort-desc"));
 
+        assertColumnsAreSortedAs(_(7, 1, SortDirection.DESCENDING));
     }
 
     @Test
-    public void testUserSorting() throws InterruptedException {
+    public void testMouseSorting() throws Exception {
         openTestURL();
 
         GridElement grid = getGridElement();
@@ -115,7 +132,10 @@ public class GridSortingTest extends GridBasicFeaturesTest {
 
         // Click header twice to sort descending
         grid.getHeaderCell(0, 9).click();
+        assertColumnsAreSortedAs(_(9, 1, SortDirection.ASCENDING));
         grid.getHeaderCell(0, 9).click();
+        assertColumnsAreSortedAs(_(9, 1, SortDirection.DESCENDING));
+
         String row = "";
         for (int i = 0; i < 3; ++i) {
             row += "9";
@@ -124,28 +144,18 @@ public class GridSortingTest extends GridBasicFeaturesTest {
                     "(" + row + ", 0)", grid.getCell(i, 0).getText());
         }
 
-        assertEquals("2. Sort order: [Column 9 ASCENDING] by USER",
-                getLogRow(2));
-        assertEquals("4. Sort order: [Column 9 DESCENDING] by USER",
-                getLogRow(0));
-
         // Column 10 is random numbers from Random with seed 13334
         // Click header to sort ascending
         grid.getHeaderCell(0, 10).click();
-
-        assertEquals("6. Sort order: [Column 10 ASCENDING] by USER",
-                getLogRow(0));
-
-        // Not cleaning up correctly causes exceptions when scrolling.
-        grid.scrollToRow(50);
-        assertFalse("Scrolling caused and exception when shuffled.",
-                getLogRow(0).contains("Exception"));
+        assertColumnsAreSortedAs(_(10, 1, SortDirection.ASCENDING));
 
         for (int i = 0; i < 5; ++i) {
+            Integer firstRow = Integer.valueOf(grid.getCell(i + 1, 10)
+                    .getText());
+            Integer secondRow = Integer.valueOf(grid.getCell(i, 10).getText());
             assertGreater(
                     "Grid is not sorted by Column 10 using ascending direction",
-                    Integer.parseInt(grid.getCell(i + 1, 10).getText()),
-                    Integer.parseInt(grid.getCell(i, 10).getText()));
+                    firstRow, secondRow);
 
         }
 
@@ -153,7 +163,10 @@ public class GridSortingTest extends GridBasicFeaturesTest {
         // 2, 1 and 0.
         // Click header twice to sort descending
         grid.getHeaderCell(0, 7).click();
+        assertColumnsAreSortedAs(_(7, 1, SortDirection.ASCENDING));
         grid.getHeaderCell(0, 7).click();
+        assertColumnsAreSortedAs(_(7, 1, SortDirection.DESCENDING));
+
         for (int i = 0; i < 3; ++i) {
             assertEquals(
                     "Grid is not sorted by Column 7 using descending direction",
@@ -161,10 +174,6 @@ public class GridSortingTest extends GridBasicFeaturesTest {
                     grid.getCell(GridBasicFeatures.ROWS - (i + 1), 0).getText());
         }
 
-        assertEquals("9. Sort order: [Column 7 ASCENDING] by USER",
-                getLogRow(3));
-        assertEquals("11. Sort order: [Column 7 DESCENDING] by USER",
-                getLogRow(1));
     }
 
     @Test
@@ -191,7 +200,7 @@ public class GridSortingTest extends GridBasicFeaturesTest {
 
     }
 
-    private void sendKeys(CharSequence... seq) {
+    private void sendKey(Keys seq) {
         new Actions(getDriver()).sendKeys(seq).perform();
     }
 
@@ -203,68 +212,135 @@ public class GridSortingTest extends GridBasicFeaturesTest {
         new Actions(getDriver()).keyUp(key).perform();
     }
 
-    private void assertLog(String expected) {
-        assertEquals(expected, getLogRow(0));
-    }
-
     @Test
     public void testKeyboardSorting() {
         openTestURL();
 
-        //
-        // NOTE: This is a work-around to get the focus to the first header
-        // cell.
-        // We can't use element.focus() because TestBench (or, rather, Selenium
-        // beneath it) has rather interesting bugs regarding focus handling.
-        //
+        /*
+         * We can't click on the header directly, since it will sort the header
+         * immediately. We need to focus some other column first, and only then
+         * navigate there.
+         */
         getGridElement().getCell(0, 0).click();
-        sendKeys(Keys.ARROW_UP);
+        sendKey(Keys.ARROW_UP);
 
         // Sort ASCENDING on first column
-        sendKeys(Keys.ENTER);
-        assertLog("2. Sort order: [Column 0 ASCENDING] by USER");
+        sendKey(Keys.ENTER);
+        assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING));
 
         // Move to next column
-        sendKeys(Keys.RIGHT);
+        sendKey(Keys.RIGHT);
 
         // Add this column to the existing sorting group
         holdKey(Keys.SHIFT);
-        sendKeys(Keys.ENTER);
+        sendKey(Keys.ENTER);
         releaseKey(Keys.SHIFT);
-        assertLog("4. Sort order: [Column 0 ASCENDING, Column 1 ASCENDING] by USER");
+        assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
+                _(2, SortDirection.ASCENDING));
 
         // Move to next column
-        sendKeys(Keys.RIGHT);
+        sendKey(Keys.RIGHT);
 
         // Add a third column to the sorting group
         holdKey(Keys.SHIFT);
-        sendKeys(Keys.ENTER);
+        sendKey(Keys.ENTER);
         releaseKey(Keys.SHIFT);
-        assertLog("6. Sort order: [Column 0 ASCENDING, Column 1 ASCENDING, Column 2 ASCENDING] by USER");
+        assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
+                _(2, SortDirection.ASCENDING), _(3, SortDirection.ASCENDING));
 
         // Move back to the second column
-        sendKeys(Keys.LEFT);
+        sendKey(Keys.LEFT);
 
         // Change sort direction of the second column to DESCENDING
         holdKey(Keys.SHIFT);
-        sendKeys(Keys.ENTER);
+        sendKey(Keys.ENTER);
         releaseKey(Keys.SHIFT);
-        assertLog("8. Sort order: [Column 0 ASCENDING, Column 1 DESCENDING, Column 2 ASCENDING] by USER");
+        assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
+                _(2, SortDirection.DESCENDING), _(3, SortDirection.ASCENDING));
 
         // Move back to the third column
-        sendKeys(Keys.RIGHT);
+        sendKey(Keys.RIGHT);
 
         // Set sorting to third column, ASCENDING
-        sendKeys(Keys.ENTER);
-        assertLog("10. Sort order: [Column 2 ASCENDING] by USER");
+        sendKey(Keys.ENTER);
+        assertColumnsAreSortedAs(_(2, 1, SortDirection.ASCENDING));
 
         // Move to the fourth column
-        sendKeys(Keys.RIGHT);
+        sendKey(Keys.RIGHT);
 
         // Make sure that single-column sorting also works as expected
-        sendKeys(Keys.ENTER);
-        assertLog("12. Sort order: [Column 3 ASCENDING] by USER");
+        sendKey(Keys.ENTER);
+        assertColumnsAreSortedAs(_(3, 1, SortDirection.ASCENDING));
+
+    }
+
+    private void assertColumnsAreSortedAs(SortInfoWithColumn... sortInfos) {
+        for (SortInfoWithColumn sortInfo : sortInfos) {
+            assertSort(sortInfo, sortInfo.columnIndex,
+                    onlyOneColumnIsSorted(sortInfos));
+        }
+    }
+
+    /**
+     * @param sortDirections
+     *            <code>null</code> if not interested in that index, otherwise a
+     *            direction that the column needs to be sorted as
+     */
+    private void assertColumnsAreSortedAs(SortInfo... sortInfos) {
+        for (int column = 0; column < sortInfos.length; column++) {
+            SortInfo sortInfo = sortInfos[column];
+            assertSort(sortInfo, column, onlyOneColumnIsSorted(sortInfos));
+        }
+    }
+
+    private void assertSort(SortInfo sortInfo, int column,
+            boolean onlyOneColumnIsSorted) {
+        if (sortInfo == null) {
+            return;
+        }
 
+        GridCellElement headerCell = getGridElement().getHeaderCell(0, column);
+        String classValue = headerCell.getAttribute("class");
+
+        boolean isSortedAscending = sortInfo.sortDirection == SortDirection.ASCENDING
+                && classValue.contains("sort-asc");
+        boolean isSortedDescending = sortInfo.sortDirection == SortDirection.DESCENDING
+                && classValue.contains("sort-desc");
+
+        if (isSortedAscending || isSortedDescending) {
+            String sortOrderAttribute = headerCell.getAttribute("sort-order");
+
+            if (sortOrderAttribute == null) {
+                if (!(sortInfo.sortOrder == 1 && onlyOneColumnIsSorted)) {
+                    fail("missing sort-order element attribute from column "
+                            + column);
+                }
+            } else {
+                assertEquals("sort order was not as expected",
+                        String.valueOf(sortInfo.sortOrder), sortOrderAttribute);
+            }
+        } else {
+            fail("column index " + column + " was not sorted as "
+                    + sortInfo.sortDirection + " (class: " + classValue + ")");
+        }
+    }
+
+    private static boolean onlyOneColumnIsSorted(SortInfo[] sortInfos) {
+
+        boolean foundSortedColumn = false;
+        for (SortInfo sortInfo : sortInfos) {
+            if (sortInfo == null) {
+                continue;
+            }
+
+            if (!foundSortedColumn) {
+                foundSortedColumn = true;
+            } else {
+                // two columns were sorted
+                return false;
+            }
+        }
+        return foundSortedColumn;
     }
 
     private void sortBy(String column) {