Author: lfryc(a)redhat.com
Date: 2010-10-28 12:03:36 -0400 (Thu, 28 Oct 2010)
New Revision: 19737
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/model/Autocomplete.java
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/AutocompleteAttributes.java
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/TestAutocomplete.java
Log:
rich:autocompletion initial tests (selectFirst + autofill support)
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/model/Autocomplete.java
===================================================================
---
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/model/Autocomplete.java
(rev 0)
+++
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/model/Autocomplete.java 2010-10-28
16:03:36 UTC (rev 19737)
@@ -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.model;
+
+import static org.richfaces.tests.metamer.ftest.AbstractMetamerTest.pjq;
+import static org.jboss.test.selenium.locator.LocatorFactory.jq;
+import static org.jboss.test.selenium.javascript.JQueryScript.jqScript;
+import static org.jboss.test.selenium.javascript.JQueryScript.jqObject;
+
+import java.awt.event.KeyEvent;
+
+import org.jboss.test.selenium.RequestTypeModelGuard.Model;
+import org.jboss.test.selenium.dom.Event;
+import org.jboss.test.selenium.encapsulated.JavaScript;
+import org.jboss.test.selenium.framework.AjaxSelenium;
+import org.jboss.test.selenium.framework.AjaxSeleniumProxy;
+import org.jboss.test.selenium.locator.JQueryLocator;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class Autocomplete implements Model {
+
+ private static String KEY_ENTER = "13";
+ private static String KEY_UP = "38";
+ private static String KEY_DOWN = "40";
+
+ AjaxSelenium selenium = AjaxSeleniumProxy.getInstance();
+
+ JQueryLocator input = pjq("input.rf-au-inp[id$=autocompleteInput]");
+
+ JQueryLocator items = jq("div.rf-au-lst-cord[id$=autocompleteList]
div[id$=autocompleteItems]");
+ JQueryLocator selection = items.getDescendant(jq("div.rf-au-sel"));
+ JQueryLocator option = jq("div.rf-au-opt");
+ JQueryLocator labeledOption = jq("div.rf-au-opt:contains('{0}')");
+
+ public void typeKeys(String keys) {
+ for (int i = 0; i < keys.length(); i++) {
+ final String key = String.valueOf(keys.charAt(i));
+ selenium.focus(input);
+ selenium.keyPress(input, key);
+ selenium.fireEvent(input, Event.KEYDOWN);
+ }
+ }
+
+ public void confirmByKeys() {
+ pressEnter();
+ }
+
+ public void selectByKeys(String label) {
+ int labeledIndex = getLabeledOptionIndex(label);
+ while (getSelectedOptionIndex() < labeledIndex) {
+ pressDown();
+ }
+ while (getSelectedOptionIndex() > labeledIndex) {
+ pressUp();
+ }
+ }
+
+ public boolean isLabeledOptionAvailable(String label) {
+ return selenium.isElementPresent(getLabeledOption(label));
+ }
+
+ public int getLabeledOptionIndex(String label) {
+ String index = selenium.getEval(jqScript(getLabeledOption(label),
"index()"));
+ return Integer.valueOf(index);
+ }
+
+ public int getSelectedOptionIndex() {
+ JavaScript script = jqScript(option,
"index({0})").parametrize(jqObject(selection));
+ String index = selenium.getEval(script);
+ return Integer.valueOf(index);
+ }
+
+ public String getSelectedOptionText() {
+ return selenium.getText(selection);
+ }
+
+ public String getInputText() {
+ return selenium.getValue(input);
+ }
+
+ private JQueryLocator getLabeledOption(String label) {
+ return labeledOption.format(label);
+ }
+
+ public void pressBackspace() {
+ selenium.keyPressNative(String.valueOf(KeyEvent.VK_BACK_SPACE));
+ }
+
+ public void pressUp() {
+ selenium.keyDown(input, KEY_UP);
+ }
+
+ public void pressDown() {
+ selenium.keyDown(input, KEY_DOWN);
+ }
+
+ public void pressEnter() {
+ selenium.keyDown(input, KEY_ENTER);
+ }
+
+ public void pressLeft() {
+ selenium.keyPressNative(String.valueOf(KeyEvent.VK_LEFT));
+ }
+
+ public void pressRight() {
+ selenium.keyPressNative(String.valueOf(KeyEvent.VK_RIGHT));
+ }
+
+ public void pressDelete() {
+ selenium.keyPressNative(String.valueOf(KeyEvent.VK_DELETE));
+ }
+
+ public void textSelectionLeft(int size) {
+ selenium.keyDownNative(String.valueOf(KeyEvent.VK_SHIFT));
+ for (int i = 0; i < size; i++) {
+ selenium.keyPressNative(String.valueOf(KeyEvent.VK_LEFT));
+ }
+ selenium.keyUpNative(String.valueOf(KeyEvent.VK_SHIFT));
+ }
+
+ public void textSelectionRight(int size) {
+ selenium.keyDownNative(String.valueOf(KeyEvent.VK_SHIFT));
+ for (int i = 0; i < size; i++) {
+ selenium.keyPressNative(String.valueOf(KeyEvent.VK_RIGHT));
+ }
+ selenium.keyUpNative(String.valueOf(KeyEvent.VK_SHIFT));
+ }
+
+ public void textSelectAll() {
+ selenium.keyDownNative(String.valueOf(KeyEvent.VK_CONTROL));
+ selenium.keyPressNative(String.valueOf(KeyEvent.VK_A));
+ selenium.keyDownNative(String.valueOf(KeyEvent.VK_CONTROL));
+ }
+
+ public boolean isCompletionVisible() {
+ return selenium.isElementPresent(option);
+ }
+}
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/AutocompleteAttributes.java
===================================================================
---
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/AutocompleteAttributes.java
(rev 0)
+++
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/AutocompleteAttributes.java 2010-10-28
16:03:36 UTC (rev 19737)
@@ -0,0 +1,79 @@
+/*******************************************************************************
+ * 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.richAutocomplete;
+
+import org.jboss.test.selenium.encapsulated.JavaScript;
+import org.richfaces.tests.metamer.ftest.AbstractComponentAttributes;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class AutocompleteAttributes extends AbstractComponentAttributes {
+ public void setAutofill(Boolean autofill) {
+ setProperty("autofill", autofill);
+ }
+
+ public void setClientFilter(JavaScript clientFilter) {
+ setProperty("clientFilter", clientFilter);
+ }
+
+ public void setDisabled(Boolean disabled) {
+ setProperty("disabled", disabled);
+ }
+
+ public void setImmediate(Boolean immediate) {
+ setProperty("immediate", immediate);
+ }
+
+ public void setMinChars(int minChars) {
+ setProperty("minChars", minChars);
+ }
+
+ public void setMode(Mode mode) {
+ setProperty("mode", mode);
+ }
+
+ public void setRendered(Boolean rendered) {
+ setProperty("rendered", rendered);
+ }
+
+ public void setRequired(Boolean required) {
+ setProperty("required", required);
+ }
+
+ public void setSelectFirst(Boolean selectFirst) {
+ setProperty("selectFirst", selectFirst);
+ }
+
+ public void setShowButton(Boolean showButton) {
+ setProperty("showButton", showButton);
+ }
+
+ public void setTokens(String tokens) {
+ setProperty("tokens", tokens);
+ }
+
+ public enum Mode {
+ AJAX, CACHED_AJAX, CLIENT
+ }
+}
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/TestAutocomplete.java
===================================================================
---
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/TestAutocomplete.java
(rev 0)
+++
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richAutocomplete/TestAutocomplete.java 2010-10-28
16:03:36 UTC (rev 19737)
@@ -0,0 +1,149 @@
+/*******************************************************************************
+ * 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.richAutocomplete;
+
+import static org.jboss.test.selenium.utils.URLUtils.buildUrl;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.jboss.test.selenium.RequestTypeModelGuard.guardXhr;
+
+import java.net.URL;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.xml.bind.JAXBException;
+
+import org.richfaces.tests.metamer.bean.Model;
+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.Autocomplete;
+import org.richfaces.tests.metamer.model.Capital;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class TestAutocomplete extends AbstractMetamerTest {
+
+ final static Boolean[] booleanValues = new Boolean[] { true, false };
+
+ AutocompleteAttributes attributes = new AutocompleteAttributes();
+ Autocomplete autocomplete = new Autocomplete();
+
+ @Override
+ public URL getTestUrl() {
+ return buildUrl(contextPath,
"faces/components/richAutocomplete/autocompleteValidation.xhtml");
+ }
+
+ @Inject
+ @Use("booleanValues")
+ Boolean autofill;
+
+ @Inject
+ @Use("booleanValues")
+ Boolean selectFirst;
+
+ List<Capital> capitals;
+
+ StringBuilder partialInput;
+
+ {
+ try {
+ capitals = Model.unmarshallCapitals();
+ } catch (JAXBException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @BeforeMethod
+ public void prepareProperties() {
+ attributes.setAutofill(autofill);
+ attributes.setSelectFirst(selectFirst);
+ if (autofill == null) {
+ autofill = false;
+ }
+ if (selectFirst == null) {
+ selectFirst = false;
+ }
+ }
+
+ @Test
+ public void testConditions() {
+
+ assertFalse(autocomplete.isCompletionVisible());
+
+ typePrefix("ala");
+
+ deleteAll();
+ }
+
+ public void deleteAll() {
+ partialInput = new StringBuilder();
+
+ autocomplete.textSelectAll();
+ guardXhr(autocomplete).pressBackspace();
+
+ assertEquals(autocomplete.getInputText(), getExpectedStateForPrefix());
+ assertEquals(autocomplete.getSelectedOptionIndex(),
getExpectedSelectedOptionIndex());
+ }
+
+ public void typePrefix(String wholeInput) {
+ partialInput = new StringBuilder(autocomplete.getInputText());
+
+ for (int i = 0; i < wholeInput.length(); i++) {
+ String chr = String.valueOf(wholeInput.charAt(i));
+
+ guardXhr(autocomplete).typeKeys(chr);
+ partialInput.append(chr);
+
+ assertEquals(autocomplete.getInputText(), getExpectedStateForPrefix());
+ assertEquals(autocomplete.getSelectedOptionIndex(),
getExpectedSelectedOptionIndex());
+ }
+ }
+
+ public String getExpectedStateForPrefix() {
+ if (selectFirst && autofill && partialInput.length() > 0) {
+ return getStatesByPrefix(partialInput.toString()).get(0).toLowerCase();
+ }
+
+ return partialInput.toString();
+ }
+
+ public int getExpectedSelectedOptionIndex() {
+ return (selectFirst) ? 0 : -1;
+ }
+
+ public List<String> getStatesByPrefix(String prefix) {
+ List<String> states = new LinkedList<String>();
+
+ for (Capital cap : capitals) {
+ if (cap.getState().toLowerCase().startsWith(prefix)) {
+ states.add(cap.getState());
+ }
+ }
+
+ return states;
+ }
+}