From richfaces-svn-commits at lists.jboss.org Thu Oct 28 12:03:37 2010 Content-Type: multipart/mixed; boundary="===============5429810554772560243==" MIME-Version: 1.0 From: richfaces-svn-commits at lists.jboss.org To: richfaces-svn-commits at lists.jboss.org Subject: [richfaces-svn-commits] JBoss Rich Faces SVN: r19737 - in modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest: model and 1 other directories. Date: Thu, 28 Oct 2010 12:03:37 -0400 Message-ID: <201010281603.o9SG3bIe003668@svn01.web.mwc.hst.phx2.redhat.com> --===============5429810554772560243== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/tes= ts/metamer/ftest/model/Autocomplete.java modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tes= ts/metamer/ftest/richAutocomplete/ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tes= ts/metamer/ftest/richAutocomplete/AutocompleteAttributes.java modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tes= ts/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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/te= sts/metamer/ftest/model/Autocomplete.java (rev 0) +++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/te= sts/metamer/ftest/model/Autocomplete.java 2010-10-28 16:03:36 UTC (rev 1973= 7) @@ -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 Lukas Fryc + * @version $Revision$ + */ +public class Autocomplete implements Model { + + private static String KEY_ENTER =3D "13"; + private static String KEY_UP =3D "38"; + private static String KEY_DOWN =3D "40"; + + AjaxSelenium selenium =3D AjaxSeleniumProxy.getInstance(); + + JQueryLocator input =3D pjq("input.rf-au-inp[id$=3DautocompleteInput]"= ); + + JQueryLocator items =3D jq("div.rf-au-lst-cord[id$=3DautocompleteList]= div[id$=3DautocompleteItems]"); + JQueryLocator selection =3D items.getDescendant(jq("div.rf-au-sel")); + JQueryLocator option =3D jq("div.rf-au-opt"); + JQueryLocator labeledOption =3D jq("div.rf-au-opt:contains('{0}')"); + + public void typeKeys(String keys) { + for (int i =3D 0; i < keys.length(); i++) { + final String key =3D 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 =3D 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 =3D selenium.getEval(jqScript(getLabeledOption(label)= , "index()")); + return Integer.valueOf(index); + } + + public int getSelectedOptionIndex() { + JavaScript script =3D jqScript(option, "index({0})").parametrize(j= qObject(selection)); + String index =3D 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 =3D 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 =3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/te= sts/metamer/ftest/richAutocomplete/AutocompleteAttributes.java = (rev 0) +++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/te= sts/metamer/ftest/richAutocomplete/AutocompleteAttributes.java 2010-10-28 1= 6: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 Lukas Fryc + * @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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/te= sts/metamer/ftest/richAutocomplete/TestAutocomplete.java = (rev 0) +++ modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/te= sts/metamer/ftest/richAutocomplete/TestAutocomplete.java 2010-10-28 16:03:3= 6 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 Lukas Fryc + * @version $Revision$ + */ +public class TestAutocomplete extends AbstractMetamerTest { + + final static Boolean[] booleanValues =3D new Boolean[] { true, false }; + + AutocompleteAttributes attributes =3D new AutocompleteAttributes(); + Autocomplete autocomplete =3D new Autocomplete(); + + @Override + public URL getTestUrl() { + return buildUrl(contextPath, "faces/components/richAutocomplete/au= tocompleteValidation.xhtml"); + } + + @Inject + @Use("booleanValues") + Boolean autofill; + + @Inject + @Use("booleanValues") + Boolean selectFirst; + + List capitals; + + StringBuilder partialInput; + + { + try { + capitals =3D Model.unmarshallCapitals(); + } catch (JAXBException e) { + throw new IllegalStateException(e); + } + } + + @BeforeMethod + public void prepareProperties() { + attributes.setAutofill(autofill); + attributes.setSelectFirst(selectFirst); + if (autofill =3D=3D null) { + autofill =3D false; + } + if (selectFirst =3D=3D null) { + selectFirst =3D false; + } + } + + @Test + public void testConditions() { + + assertFalse(autocomplete.isCompletionVisible()); + + typePrefix("ala"); + + deleteAll(); + } + + public void deleteAll() { + partialInput =3D new StringBuilder(); + + autocomplete.textSelectAll(); + guardXhr(autocomplete).pressBackspace(); + + assertEquals(autocomplete.getInputText(), getExpectedStateForPrefi= x()); + assertEquals(autocomplete.getSelectedOptionIndex(), getExpectedSel= ectedOptionIndex()); + } + + public void typePrefix(String wholeInput) { + partialInput =3D new StringBuilder(autocomplete.getInputText()); + + for (int i =3D 0; i < wholeInput.length(); i++) { + String chr =3D String.valueOf(wholeInput.charAt(i)); + + guardXhr(autocomplete).typeKeys(chr); + partialInput.append(chr); + + assertEquals(autocomplete.getInputText(), getExpectedStateForP= refix()); + assertEquals(autocomplete.getSelectedOptionIndex(), getExpecte= dSelectedOptionIndex()); + } + } + + public String getExpectedStateForPrefix() { + if (selectFirst && autofill && partialInput.length() > 0) { + return getStatesByPrefix(partialInput.toString()).get(0).toLow= erCase(); + } + + return partialInput.toString(); + } + + public int getExpectedSelectedOptionIndex() { + return (selectFirst) ? 0 : -1; + } + + public List getStatesByPrefix(String prefix) { + List states =3D new LinkedList(); + + for (Capital cap : capitals) { + if (cap.getState().toLowerCase().startsWith(prefix)) { + states.add(cap.getState()); + } + } + + return states; + } +} --===============5429810554772560243==--