[richfaces-svn-commits] JBoss Rich Faces SVN: r18575 - in modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest: richDataGrid and 1 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Wed Aug 11 17:19:19 EDT 2010


Author: lfryc at redhat.com
Date: 2010-08-11 17:19:18 -0400 (Wed, 11 Aug 2010)
New Revision: 18575

Added:
   modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/PaginationTester.java
Modified:
   modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/model/DataScroller.java
   modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/AbstractDataGridTest.java
   modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/TestPagination.java
   modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/TestSimple.java
   modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java
Log:
derived general PaginationTester for use in new dataGrid's TestPagination; derived AbstractDataGridTest to use across dataGrid tests

Modified: modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/model/DataScroller.java
===================================================================
--- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/model/DataScroller.java	2010-08-11 21:16:21 UTC (rev 18574)
+++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/model/DataScroller.java	2010-08-11 21:19:18 UTC (rev 18575)
@@ -24,6 +24,7 @@
 
 import static org.jboss.test.selenium.guard.request.RequestTypeGuardFactory.guardXhr;
 import static org.jboss.test.selenium.locator.reference.ReferencedLocator.ref;
+import static org.jboss.test.selenium.utils.text.SimplifiedFormat.format;
 
 import org.jboss.test.selenium.framework.AjaxSelenium;
 import org.jboss.test.selenium.framework.AjaxSeleniumProxy;
@@ -69,10 +70,18 @@
         this.fastStep = fastStep;
     }
 
+    public Integer getFastStep() {
+        return fastStep;
+    }
+
     public void setLastPage(int pageNumber) {
         this.lastPage = pageNumber;
     }
 
+    public Integer getLastPage() {
+        return lastPage;
+    }
+
     public void gotoFirstPage() {
         if (!isFirstPage()) {
             clickFirstPageButton();
@@ -86,18 +95,25 @@
     }
 
     public void gotoPage(int pageNumber) {
+        if (lastPage != null && (pageNumber < 1 || pageNumber > lastPage)) {
+            throw new IllegalStateException(format("The given pageNumber '{0}' is out of range of pages <1,{1}>", pageNumber, lastPage));
+        }
         while (pageNumber > getLastVisiblePage()) {
-            fastForward();
+            fastForward(pageNumber);
         }
 
         while (pageNumber < getFirstVisiblePage()) {
-            fastRewind();
+            fastRewind(pageNumber);
         }
+        
+        if (pageNumber == getCurrentPage()) {
+            return;
+        }
 
         clickPageButton(pageNumber);
     }
 
-    public void fastForward() {
+    public void fastForward(Integer pageNumber) {
         if (selenium.belongsClass(fastForwardButton, CLASS_DISABLED)) {
             if (fastStep != null && lastPage != null) {
                 if (getCurrentPage() + fastStep > lastPage) {
@@ -109,11 +125,21 @@
                 gotoPage(getLastVisiblePage());
             }
         } else {
+            if (pageNumber != null && lastPage != null) {
+                if (Math.abs(getLastVisiblePage() - pageNumber) > Math.abs(lastPage - pageNumber)) {
+                    clickLastPageButton();
+                    return;
+                }
+            }
+            if (fastStep == null) {
+                gotoPage(getLastVisiblePage());
+                return;
+            }
             clickFastForward();
         }
     }
 
-    public void fastRewind() {
+    public void fastRewind(Integer pageNumber) {
         if (selenium.belongsClass(fastRewindButton, CLASS_DISABLED)) {
             if (fastStep != null) {
                 if (getCurrentPage() - fastStep <= 0) {
@@ -125,7 +151,18 @@
                 gotoPage(getFirstVisiblePage());
             }
         } else {
+            if (pageNumber != null) {
+                if (Math.abs(getFirstVisiblePage() - pageNumber) > pageNumber) {
+                    clickFirstPageButton();
+                    return;
+                }
+            }
+            if (fastStep == null) {
+                gotoPage(getFirstVisiblePage());
+                return;
+            }
             clickFastRewind();
+
         }
     }
 
@@ -151,7 +188,10 @@
         return integer(selenium.getText(lastVisiblePage));
     }
 
-    public int getLastPage() {
+    public int obtainLastPage() {
+        if (!hasPages()) {
+            return 1;
+        }
         int startPage = getCurrentPage();
         clickLastPageButton();
         int lastPage = getCurrentPage();

Modified: modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/AbstractDataGridTest.java
===================================================================
--- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/AbstractDataGridTest.java	2010-08-11 21:16:21 UTC (rev 18574)
+++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/AbstractDataGridTest.java	2010-08-11 21:19:18 UTC (rev 18575)
@@ -72,6 +72,13 @@
     @Use(empty = true)
     Integer first;
 
+    int expectedFirst;
+    int expectedElements;
+    int expectedColumns;
+    double expectedRows;
+    int page = 1;
+    int lastPage = 1;
+
     @SuppressWarnings("restriction")
     public AbstractDataGridTest() throws JAXBException {
         capitals = Model.unmarshallCapitals();
@@ -89,31 +96,72 @@
         guardHttp(selenium).type(inputLocator, v);
     }
 
+    protected void verifyGrid() {
+        countExpectedValues();
+        verifyCounts();
+        verifyElements();
+    }
+
     protected void verifyCounts() {
-        int nFirst = first == null ? 0 : min(ELEMENTS_TOTAL, max(0, first));
-        int nElements = (elements == null ? ELEMENTS_TOTAL : min(elements, ELEMENTS_TOTAL)) - nFirst;
-        int nColumns = columns;
-        double nRows = ceil((float) nElements / columns);
-
-        assertEquals(dataGrid.getElementCount(), (int) nElements);
-        assertEquals(dataGrid.getColumnCount(), (int) nColumns);
-        assertEquals(dataGrid.getRowCount(), (int) nRows);
+        try {
+            assertEquals(dataGrid.getElementCount(), (int) expectedElements, "elements");
+            assertEquals(dataGrid.getColumnCount(), (int) expectedColumns, "columns");
+            assertEquals(dataGrid.getRowCount(), (int) expectedRows, "rows");
+        } catch (AssertionError e) {
+            throw e;
+        }
     }
 
     protected void verifyElements() {
-        int nFirst = first == null ? 0 : min(ELEMENTS_TOTAL, max(0, first));
-        int nElements = (elements == null ? ELEMENTS_TOTAL : min(elements, ELEMENTS_TOTAL)) - nFirst;
+        int elementNumber;
+        try {
+            Iterator<Capital> capitalIterator = getExpectedCapitalsIterator();
+            Iterator<JQueryLocator> elementIterator = dataGrid.iterateElements();
 
-        Iterator<Capital> capitalIterator = capitals.subList(nFirst, nFirst + nElements).iterator();
-        Iterator<JQueryLocator> elementIterator = dataGrid.iterateElements();
+            elementNumber = 1;
+            while (capitalIterator.hasNext()) {
+                final Capital capital = capitalIterator.next();
+                if (!elementIterator.hasNext()) {
+                    fail("there should be next element for state name: " + capital.getState());
+                }
+                elementNumber += 1;
+                final JQueryLocator element = elementIterator.next().getChild(jq("span"));
+                assertEquals(selenium.getText(element), capital.getState());
+            }
+        } catch (AssertionError e) {
+            throw e;
+        }
+    }
 
-        while (capitalIterator.hasNext()) {
-            final Capital capital = capitalIterator.next();
-            if (!elementIterator.hasNext()) {
-                fail("there should be next element for state name: " + capital.getState());
+    protected void countExpectedValues() {
+        if (elements == null) {
+            elements = ELEMENTS_TOTAL;
+        }
+
+        if (first == null) {
+            first = 0;
+        }
+
+        int firstOnPage = ((page - 1) * elements);
+        int firstInRange = min(ELEMENTS_TOTAL, max(0, first));
+
+        expectedFirst = firstOnPage + firstInRange;
+
+        int elementsInRange = min(elements, ELEMENTS_TOTAL);
+        expectedElements = elementsInRange;
+        if (page == lastPage) {
+            if (elements == 0) {
+                expectedElements = ELEMENTS_TOTAL - expectedFirst;
+            } else {
+                expectedElements = min(elements, ELEMENTS_TOTAL - expectedFirst);
             }
-            final JQueryLocator element = elementIterator.next().getChild(jq("span"));
-            assertEquals(selenium.getText(element), capital.getState());
         }
+
+        expectedRows = ceil((float) expectedElements / columns);
+        expectedColumns = columns;
     }
+
+    private Iterator<Capital> getExpectedCapitalsIterator() {
+        return capitals.subList(expectedFirst, expectedFirst + expectedElements).iterator();
+    }
 }

Modified: modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/TestPagination.java
===================================================================
--- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/TestPagination.java	2010-08-11 21:16:21 UTC (rev 18574)
+++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/TestPagination.java	2010-08-11 21:19:18 UTC (rev 18575)
@@ -27,27 +27,77 @@
 
 import javax.xml.bind.JAXBException;
 
+import org.jboss.test.selenium.locator.JQueryLocator;
+import org.richfaces.tests.metamer.ftest.annotations.Inject;
 import org.richfaces.tests.metamer.ftest.annotations.Use;
+import org.richfaces.tests.metamer.ftest.model.DataScroller;
+import org.richfaces.tests.metamer.ftest.richDataScroller.PaginationTester;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 /**
  * @author <a href="mailto:lfryc at redhat.com">Lukas Fryc</a>
  * @version $Revision$
  */
- at Use(field = "elements", ints = { 7 })
+ at Use(field = "elements", ints = 7)
 public class TestPagination extends AbstractDataGridTest {
 
+    @Inject
+    @Use("dataScrollerLocator*")
+    JQueryLocator dataScrollerLocator;
+    JQueryLocator dataScrollerLocator1 = PaginationTester.DATA_SCROLLER_OUTSIDE_TABLE;
+    JQueryLocator dataScrollerLocator2 = PaginationTester.DATA_SCROLLER_IN_TABLE_FOOTER;
+
+    PaginationTester paginationTester = new PaginationTester() {
+
+        @Override
+        protected void verifyBeforeScrolling() {
+        }
+
+        @Override
+        protected void verifyAfterScrolling() {
+            page = getDataScroller().getCurrentPage();
+            lastPage = getDataScroller().getLastPage();
+            verifyGrid();
+        }
+    };
+
+    DataScroller dataScroller = paginationTester.getDataScroller();
+
     public TestPagination() throws JAXBException {
         super();
     }
 
+    @BeforeMethod
+    public void setupDataScroller() {
+        dataScroller.setRoot(dataScrollerLocator);
+
+        int lastPage = dataScroller.obtainLastPage();
+        dataScroller.setLastPage(lastPage);
+        paginationTester.initializeTestedPages(lastPage);
+    }
+
     @Override
     public URL getTestUrl() {
         return buildUrl(contextPath, "faces/components/richDataGrid/scroller.xhtml");
     }
 
     @Test
-    public void testPagination() {
-        
+    @Use(field = "columns", ints = { 1, 3, 11, ELEMENTS_TOTAL / 2, ELEMENTS_TOTAL - 1, ELEMENTS_TOTAL,
+        ELEMENTS_TOTAL + 1 })
+    public void testColumnsAttribute() {
+        paginationTester.testNumberedPages();
     }
-}
\ No newline at end of file
+
+    @Test
+    @Use(field = "elements", ints = { 0, 1, ELEMENTS_TOTAL / 2, ELEMENTS_TOTAL - 1, ELEMENTS_TOTAL, ELEMENTS_TOTAL + 1 })
+    public void testElementsAttribute() {
+        paginationTester.testNumberedPages();
+    }
+
+    @Test
+    @Use(field = "first", ints = { 0, 1, ELEMENTS_TOTAL / 2, ELEMENTS_TOTAL - 1, ELEMENTS_TOTAL, ELEMENTS_TOTAL + 1 })
+    public void testFirstAttribute() {
+        paginationTester.testNumberedPages();
+    }
+}

Modified: modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/TestSimple.java
===================================================================
--- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/TestSimple.java	2010-08-11 21:16:21 UTC (rev 18574)
+++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataGrid/TestSimple.java	2010-08-11 21:19:18 UTC (rev 18575)
@@ -49,21 +49,18 @@
     @Use(field = "columns", ints = { 1, 3, 11, ELEMENTS_TOTAL / 2, ELEMENTS_TOTAL - 1, ELEMENTS_TOTAL,
         ELEMENTS_TOTAL + 1 })
     public void testColumnsAttribute() {
-        verifyCounts();
-        verifyElements();
+        verifyGrid();
     }
 
     @Test
     @Use(field = "elements", ints = { 0, 1, ELEMENTS_TOTAL / 2, ELEMENTS_TOTAL - 1, ELEMENTS_TOTAL, ELEMENTS_TOTAL + 1 })
     public void testElementsAttribute() {
-        verifyCounts();
-        verifyElements();
+        verifyGrid();
     }
 
     @Test
     @Use(field = "first", ints = { 0, 1, ELEMENTS_TOTAL / 2, ELEMENTS_TOTAL - 1, ELEMENTS_TOTAL, ELEMENTS_TOTAL + 1 })
     public void testFirstAttribute() {
-        verifyCounts();
-        verifyElements();
+        verifyGrid();
     }
 }

Added: modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/PaginationTester.java
===================================================================
--- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/PaginationTester.java	                        (rev 0)
+++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/PaginationTester.java	2010-08-11 21:19:18 UTC (rev 18575)
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *******************************************************************************/
+package org.richfaces.tests.metamer.ftest.richDataScroller;
+
+import org.jboss.test.selenium.locator.JQueryLocator;
+import org.richfaces.tests.metamer.ftest.model.AssertingDataScroller;
+import org.richfaces.tests.metamer.ftest.model.DataScroller;
+
+import static org.richfaces.tests.metamer.ftest.AbstractMetamerTest.pjq;
+import static java.lang.Math.*;
+
+/**
+ * @author <a href="mailto:lfryc at redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public abstract class PaginationTester {
+
+    public static final JQueryLocator DATA_SCROLLER_OUTSIDE_TABLE = pjq("span.rf-ds[id$=scroller1]");
+    public static final JQueryLocator DATA_SCROLLER_IN_TABLE_FOOTER = pjq("span.rf-ds[id$=scroller2]");
+
+    protected AssertingDataScroller dataScroller = new AssertingDataScroller(null);
+    private int[] testPages;
+
+    public void initializeTestedPages(int lastPage) {
+        int l = lastPage;
+        testPages = new int[] { 3, l, 1, l - 2, l, 2, l - 2, l - 1 };
+        for (int i = 0; i < testPages.length; i++) {
+            testPages[i] = min(l, max(1, testPages[i]));
+        }
+    }
+
+    public DataScroller getDataScroller() {
+        return dataScroller;
+    }
+
+    public void testNumberedPages() {
+        Integer lastNumber = null;
+        for (int pageNumber : testPages) {
+            if (lastNumber == (Integer) pageNumber) {
+                continue;
+            }
+            verifyBeforeScrolling();
+            dataScroller.gotoPage(pageNumber);
+            verifyAfterScrolling();
+            lastNumber = pageNumber;
+        }
+    }
+
+    protected abstract void verifyBeforeScrolling();
+
+    protected abstract void verifyAfterScrolling();
+}

Modified: modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java
===================================================================
--- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java	2010-08-11 21:16:21 UTC (rev 18574)
+++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java	2010-08-11 21:19:18 UTC (rev 18575)
@@ -24,15 +24,17 @@
 import static org.jboss.test.selenium.guard.request.RequestTypeGuardFactory.guardHttp;
 import static org.jboss.test.selenium.locator.LocatorFactory.id;
 import static org.jboss.test.selenium.utils.URLUtils.buildUrl;
-import static org.testng.Assert.*;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
 
 import java.net.URL;
 
 import org.jboss.test.selenium.locator.IdLocator;
+import org.jboss.test.selenium.locator.JQueryLocator;
 import org.richfaces.tests.metamer.ftest.AbstractMetamerTest;
 import org.richfaces.tests.metamer.ftest.annotations.Inject;
 import org.richfaces.tests.metamer.ftest.annotations.Use;
-import org.richfaces.tests.metamer.ftest.model.AssertingDataScroller;
+import org.richfaces.tests.metamer.ftest.model.DataScroller;
 import org.richfaces.tests.metamer.ftest.model.DataTable;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
@@ -45,51 +47,62 @@
  */
 public class TestPagination extends AbstractMetamerTest {
 
-    private static final int[] PAGES = new int[] { 3, 6, 1, 4, 6, 2, 4, 5 };
+    @Inject
+    @Use(ints = { 2, 3 })
+    int fastStep;
 
     @Inject
-    @Use(value = { "dataScroller*" })
-    AssertingDataScroller dataScroller;
-    AssertingDataScroller dataScroller1 = new AssertingDataScroller("outside-table", pjq("span.rf-ds[id$=scroller1]"));
-    AssertingDataScroller dataScroller2 = new AssertingDataScroller("in-table-footer", pjq("span.rf-ds[id$=scroller2]"));
+    @Use(ints = { 3, 4 })
+    int maxPages;
 
+    @Inject
+    @Use("dataScrollerLocator*")
+    JQueryLocator dataScrollerLocator;
+    JQueryLocator dataScrollerLocator1 = PaginationTester.DATA_SCROLLER_OUTSIDE_TABLE;
+    JQueryLocator dataScrollerLocator2 = PaginationTester.DATA_SCROLLER_IN_TABLE_FOOTER;
+
+    PaginationTester paginationTester = new PaginationTester() {
+
+        @Override
+        protected void verifyBeforeScrolling() {
+            tableText = dataTable.getTableText();
+        }
+
+        @Override
+        protected void verifyAfterScrolling() {
+            assertFalse(tableText.equals(dataTable.getTableText()));
+            assertEquals(maxPages, dataScroller.getCountOfVisiblePages());
+        }
+    };
+
     IdLocator attributeFastStep = id("form:attributes:fastStepInput");
     IdLocator attributeMaxPages = id("form:attributes:maxPagesInput");
 
+    DataScroller dataScroller = paginationTester.getDataScroller();
     DataTable dataTable = new DataTable(pjq("table.rf-dt[id$=richDataTable]"));
 
-    @Inject
-    @Use(ints = { 2, 3 })
-    int fastStep;
+    String tableText;
 
-    @Inject
-    @Use(ints = { 3, 4 })
-    int maxPages;
-
-    public TestPagination() {
-    }
-
     @Override
     public URL getTestUrl() {
         return buildUrl(contextPath, "faces/components/richDataScroller/simple.xhtml");
     }
 
     @BeforeMethod
-    public void prepareAttributes() {
+    public void prepareComponent() {
         guardHttp(selenium).type(attributeFastStep, String.valueOf(fastStep));
         guardHttp(selenium).type(attributeMaxPages, String.valueOf(maxPages));
-    }
 
-    @Test
-    public void testNumberedPagesWithMaxPagesAndFastStep() {
+        dataScroller.setRoot(dataScrollerLocator);
         dataScroller.setFastStep(fastStep);
-        dataScroller.setLastPage(dataScroller.getLastPage());
 
-        for (int pageNumber : PAGES) {
-            String tableText = dataTable.getTableText();
-            dataScroller.gotoPage(pageNumber);
-            assertFalse(tableText.equals(dataTable.getTableText()));
-            assertEquals(maxPages, dataScroller.getCountOfVisiblePages());
-        }
+        int lastPage = dataScroller.obtainLastPage();
+        dataScroller.setLastPage(lastPage);
+        paginationTester.initializeTestedPages(lastPage);
     }
+
+    @Test
+    public void testNumberedPages() {
+        paginationTester.testNumberedPages();
+    }
 }



More information about the richfaces-svn-commits mailing list