001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.functionaltests.forms;
018
019import java.util.List;
020
021import org.nuxeo.functionaltests.AbstractTest;
022import org.nuxeo.functionaltests.AjaxRequestManager;
023import org.nuxeo.functionaltests.forms.JSListWidgetElement.Display;
024import org.openqa.selenium.By;
025import org.openqa.selenium.WebDriver;
026import org.openqa.selenium.WebElement;
027
028/**
029 * Represents a list widget, with helper method to retrieve/check its subwidgets.
030 *
031 * @since 5.7
032 * @deprecated since 7.2: {@link JSListWidgetElement} should now be used, this class is kept for compatibility.
033 */
034@Deprecated
035public class ListWidgetElement extends AbstractWidgetElement {
036
037    /**
038     * The display attribute controls the rendering of subwidgets.
039     *
040     * @since 7.2
041     */
042    private final Display display;
043
044    public ListWidgetElement(WebDriver driver, String id) {
045        this(driver, id, Display.BLOCK_LEFT);
046    }
047
048    /**
049     * @since 7.2
050     */
051    public ListWidgetElement(WebDriver driver, String id, JSListWidgetElement.Display display) {
052        super(driver, id);
053        this.display = display;
054    }
055
056    /**
057     * @since 7.2
058     */
059    public String getListElementId() {
060        return String.format("%s_input", getWidgetId());
061    }
062
063    protected String getListSubElementId(String subId, int index) {
064        return String.format("%s:%s:%s", getListElementId(), Integer.valueOf(index), subId);
065    }
066
067    /**
068     * @since 7.2
069     */
070    protected String getListSubElementSuffix(String subId, int index) {
071        return String.format("%s:%s:%s", getListElementId(), Integer.valueOf(index), subId);
072    }
073
074    public void addNewElement() {
075        String wid = getWidgetId();
076        WebElement addElement = getSubElement(wid + "_add");
077        AjaxRequestManager arm = new AjaxRequestManager(AbstractTest.driver);
078        arm.begin();
079        addElement.click();
080        arm.end();
081    }
082
083    public void removeElement(int index) {
084        String wid = getWidgetId();
085        String delId = String.format("%s:%s:%s_delete", getListElementId(), Integer.valueOf(index), wid);
086        WebElement delElement = getSubElement(delId);
087        AjaxRequestManager arm = new AjaxRequestManager(AbstractTest.driver);
088        arm.begin();
089        delElement.click();
090        arm.end();
091    }
092
093    /**
094     * @since 7.2
095     */
096    public void moveUpElement(int index) {
097        WebElement moveElement = getRowActions(index).findElement(By.className("moveUpBtn"));
098        AjaxRequestManager arm = new AjaxRequestManager(AbstractTest.driver);
099        arm.begin();
100        moveElement.click();
101        arm.end();
102    }
103
104    /**
105     * @since 7.2
106     */
107    public void moveDownElement(int index) {
108        WebElement moveElement = getRowActions(index).findElement(By.className("moveDownBtn"));
109        AjaxRequestManager arm = new AjaxRequestManager(AbstractTest.driver);
110        arm.begin();
111        moveElement.click();
112        arm.end();
113    }
114
115    public void waitForSubWidget(String id, int index) {
116        getSubElement(getListSubElementId(id, index), true);
117    }
118
119    /**
120     * @since 7.2
121     */
122    public WidgetElement getSubWidget(String id, int index) {
123        return getSubWidget(id, index, false);
124    }
125
126    public WidgetElement getSubWidget(String id, int index, boolean wait) {
127        if (wait) {
128            waitForSubWidget(id, index);
129        }
130        return getWidget(getListSubElementId(id, index));
131    }
132
133    public <T> T getSubWidget(String id, int index, Class<T> widgetClassToProxy, boolean wait) {
134        if (wait) {
135            waitForSubWidget(id, index);
136        }
137        return getWidget(getListSubElementId(id, index), widgetClassToProxy);
138    }
139
140    @Override
141    public String getMessageValue() {
142        return getMessageValue(":" + getWidgetId() + "_message");
143    }
144
145    /**
146     * @since 7.2
147     */
148    public List<WebElement> getRows() {
149        return driver.findElements(By.cssSelector(getRowsCssSelector()));
150    }
151
152    private String getElementCssSelector() {
153        return "#" + getId().replaceAll(":", "\\\\:") + "\\:" + getWidgetId() + "_panel";
154    }
155
156    private String getRowsCssSelector() {
157        String path = getElementCssSelector();
158        if (display == Display.TABLE || display == Display.INLINE) {
159            path += " > table > tbody";
160        }
161        return path + " > .listItem";
162    }
163
164    protected WebElement getRowActions(int i) {
165        if (display == Display.TABLE || display == Display.INLINE) {
166            return driver.findElement(By.cssSelector(getRowCssSelector(i) + " > .listWidgetActions"));
167        }
168        return getSubElement(getListSubElementSuffix(getWidgetId() + "_actions", i));
169    }
170
171    private String getRowCssSelector(int i) {
172        return getRowsCssSelector() + ":nth-of-type(" + (i + 1) + ")";
173    }
174
175    /**
176     * @since 7.2
177     */
178    public String getSubWidgetMessageValue(String id, int idx) {
179        return getSubWidgetMessageValue(id, idx, 0);
180    }
181
182    /**
183     * @since 7.2
184     */
185    public String getSubWidgetMessageValue(String id, int idx, int msgIdx) {
186        String subId = id + "_message";
187        if (msgIdx != 0) {
188            subId += "_" + msgIdx;
189        }
190        return getMessageValue(":" + getListSubElementSuffix(subId, idx));
191    }
192
193}