Author: lfryc(a)redhat.com
Date: 2011-01-18 04:04:20 -0500 (Tue, 18 Jan 2011)
New Revision: 21051
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richCollapsibleSubTable/TestCollapsibleSubTableFiltering.java
Log:
rich:collapsibleSubTable - automated Filtering sample (RFPL-731)
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richCollapsibleSubTable/TestCollapsibleSubTableFiltering.java
===================================================================
---
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richCollapsibleSubTable/TestCollapsibleSubTableFiltering.java
(rev 0)
+++
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richCollapsibleSubTable/TestCollapsibleSubTableFiltering.java 2011-01-18
09:04:20 UTC (rev 21051)
@@ -0,0 +1,160 @@
+/*******************************************************************************
+ * 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.richCollapsibleSubTable;
+
+import static org.apache.commons.lang.StringUtils.isBlank;
+import static org.jboss.test.selenium.guard.request.RequestTypeGuardFactory.guardXhr;
+import static org.jboss.test.selenium.locator.LocatorFactory.jq;
+import static org.jboss.test.selenium.utils.URLUtils.buildUrl;
+import static org.testng.Assert.assertEquals;
+
+import java.net.URL;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jboss.test.selenium.framework.AjaxSelenium;
+import org.jboss.test.selenium.framework.AjaxSeleniumProxy;
+import org.jboss.test.selenium.locator.JQueryLocator;
+import org.richfaces.tests.metamer.ftest.model.DataTable;
+import org.richfaces.tests.metamer.model.Employee;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class TestCollapsibleSubTableFiltering extends AbstractCollapsibleSubTableTest {
+
+ @Override
+ public URL getTestUrl() {
+ return buildUrl(contextPath,
"faces/components/richCollapsibleSubTable/filtering.xhtml");
+ }
+
+ int rows = 0;
+
+ @BeforeMethod
+ public void prepare() {
+ attributes.setRows(rows);
+ Column.resetAll();
+ }
+
+ @Test
+ public void testFilteringExpressionContainsIgnoreCase() {
+ filterBy(Column.NAME, "Alexander");
+ verifyFiltering();
+
+ filterBy(Column.NAME, "aLEX");
+ verifyFiltering();
+ }
+
+ @Test
+ public void testFilteringExpressionEquals() {
+ filterBy(Column.TITLE, "Director");
+ verifyFiltering();
+
+ filterBy(Column.TITLE, "director");
+ verifyFiltering();
+
+ filterBy(Column.TITLE, "direct");
+ verifyFiltering();
+ }
+
+ public void verifyFiltering() {
+ filterEmployees();
+
+ int length = (rows <= 0) ? employees.size() : Math.min(rows,
employees.size());
+ List<Employee> visibleEmployees = employees.subList(0, length);
+
+ int rowCount = subtable.getRowCount();
+ assertEquals(rowCount, visibleEmployees.size());
+
+ for (int i = 0; i < visibleEmployees.size(); i++) {
+ String name = selenium.getText(subtable.getCell(1, i + 1));
+ String title = selenium.getText(subtable.getCell(2, i + 1));
+
+ assertEquals(name, visibleEmployees.get(i).getName());
+ assertEquals(title, visibleEmployees.get(i).getTitle());
+ }
+ }
+
+ public void filterBy(Column column, String filter) {
+ resetEmployees();
+ column.filter(dataTable, filter);
+ }
+
+ public enum Column {
+ NAME, TITLE, BIRTHDAY;
+
+ AjaxSelenium selenium = AjaxSeleniumProxy.getInstance();
+ String filter = "";
+
+ public static void resetAll() {
+ for (Column value : values()) {
+ value.reset();
+ }
+ }
+
+ public void reset() {
+ filter = "";
+ }
+
+ public void filter(DataTable dataTable, String filter) {
+ this.filter = filter;
+ int headerIndex = this.ordinal() + 2; // 1 for index; 1 for additional header
on top of links
+ JQueryLocator input =
dataTable.getHeader().getNthOccurence(headerIndex).getChild(jq("input:text"));
+ guardXhr(selenium).type(input, filter);
+ }
+
+ public boolean apply(Employee employee) {
+ switch (this) {
+ case NAME:
+ boolean result = isBlank(filter) ||
employee.getName().toLowerCase().contains(filter.toLowerCase());
+ return result;
+ case TITLE:
+ boolean result1 = isBlank(filter) ||
employee.getTitle().equals(filter);
+ return result1;
+ case BIRTHDAY:
+ return true;
+ default:
+ throw new IllegalStateException();
+ }
+ }
+ }
+
+ public void filterEmployees() {
+ employees = new LinkedList<Employee>(Collections2.filter(employees, new
Predicate<Employee>() {
+ @Override
+ public boolean apply(Employee input) {
+ for (Column column : Column.values()) {
+ if (!column.apply(input)) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }));
+ }
+}