001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.functionaltests.forms;
020
021import java.util.List;
022
023import org.nuxeo.functionaltests.AbstractTest;
024import org.nuxeo.functionaltests.AjaxRequestManager;
025import org.nuxeo.functionaltests.Locator;
026import org.nuxeo.functionaltests.forms.JSListWidgetElement.Display;
027import org.openqa.selenium.By;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030
031/**
032 * Represents a list widget, with helper method to retrieve/check its subwidgets.
033 *
034 * @since 5.7
035 * @deprecated since 7.2: {@link JSListWidgetElement} should now be used, this class is kept for compatibility.
036 */
037@Deprecated
038public class ListWidgetElement extends AbstractWidgetElement {
039
040    /**
041     * The display attribute controls the rendering of subwidgets.
042     *
043     * @since 7.2
044     */
045    private final Display display;
046
047    public ListWidgetElement(WebDriver driver, String id) {
048        this(driver, id, Display.BLOCK_LEFT);
049    }
050
051    /**
052     * @since 7.2
053     */
054    public ListWidgetElement(WebDriver driver, String id, JSListWidgetElement.Display display) {
055        super(driver, id);
056        this.display = display;
057    }
058
059    /**
060     * @since 7.2
061     */
062    public String getListElementId() {
063        return String.format("%s_input", getWidgetId());
064    }
065
066    protected String getListSubElementId(String subId, int index) {
067        return String.format("%s:%s:%s", getListElementId(), Integer.valueOf(index), subId);
068    }
069
070    /**
071     * @since 7.2
072     */
073    protected String getListSubElementSuffix(String subId, int index) {
074        return String.format("%s:%s:%s", getListElementId(), Integer.valueOf(index), subId);
075    }
076
077    public void addNewElement() {
078        String wid = getWidgetId();
079        WebElement addElement = getSubElement(wid + "_add");
080        AjaxRequestManager arm = new AjaxRequestManager(AbstractTest.driver);
081        arm.begin();
082        Locator.waitUntilEnabledAndClick(addElement);
083        arm.end();
084    }
085
086    public void removeElement(int index) {
087        String wid = getWidgetId();
088        String delId = String.format("%s:%s:%s_delete", getListElementId(), Integer.valueOf(index), wid);
089        WebElement delElement = getSubElement(delId);
090        AjaxRequestManager arm = new AjaxRequestManager(AbstractTest.driver);
091        arm.begin();
092        Locator.waitUntilEnabledAndClick(delElement);
093        arm.end();
094    }
095
096    /**
097     * @since 7.2
098     */
099    public void moveUpElement(int index) {
100        WebElement moveElement = getRowActions(index).findElement(By.className("moveUpBtn"));
101        AjaxRequestManager arm = new AjaxRequestManager(AbstractTest.driver);
102        arm.begin();
103        Locator.waitUntilEnabledAndClick(moveElement);
104        arm.end();
105    }
106
107    /**
108     * @since 7.2
109     */
110    public void moveDownElement(int index) {
111        WebElement moveElement = getRowActions(index).findElement(By.className("moveDownBtn"));
112        AjaxRequestManager arm = new AjaxRequestManager(AbstractTest.driver);
113        arm.begin();
114        Locator.waitUntilEnabledAndClick(moveElement);
115        arm.end();
116    }
117
118    public void waitForSubWidget(String id, int index) {
119        getSubElement(getListSubElementId(id, index), true);
120    }
121
122    /**
123     * @since 7.2
124     */
125    public WidgetElement getSubWidget(String id, int index) {
126        return getSubWidget(id, index, false);
127    }
128
129    /**
130     * @since 8.3
131     */
132    public String getSubWidgetId(String id, int index) {
133        return getSubWidget(id, index, false).getId();
134    }
135
136    public WidgetElement getSubWidget(String id, int index, boolean wait) {
137        if (wait) {
138            waitForSubWidget(id, index);
139        }
140        return getWidget(getListSubElementId(id, index));
141    }
142
143    public <T> T getSubWidget(String id, int index, Class<T> widgetClassToProxy, boolean wait) {
144        if (wait) {
145            waitForSubWidget(id, index);
146        }
147        return getWidget(getListSubElementId(id, index), widgetClassToProxy);
148    }
149
150    @Override
151    public String getMessageValue() {
152        return getMessageValue(":" + getWidgetId() + "_message");
153    }
154
155    /**
156     * @since 7.2
157     */
158    public List<WebElement> getRows() {
159        return driver.findElements(By.cssSelector(getRowsCssSelector()));
160    }
161
162    private String getElementCssSelector() {
163        return "#" + getId().replaceAll(":", "\\\\:") + "\\:" + getWidgetId() + "_panel";
164    }
165
166    private String getRowsCssSelector() {
167        String path = getElementCssSelector();
168        if (display == Display.TABLE || display == Display.INLINE) {
169            path += " > table > tbody";
170        }
171        return path + " > .listItem";
172    }
173
174    protected WebElement getRowActions(int i) {
175        if (display == Display.TABLE || display == Display.INLINE) {
176            return driver.findElement(By.cssSelector(getRowCssSelector(i) + " > .listWidgetActions"));
177        }
178        return getSubElement(getListSubElementSuffix(getWidgetId() + "_actions", i));
179    }
180
181    private String getRowCssSelector(int i) {
182        return getRowsCssSelector() + ":nth-of-type(" + (i + 1) + ")";
183    }
184
185    /**
186     * @since 7.2
187     */
188    public String getSubWidgetMessageValue(String id, int idx) {
189        return getSubWidgetMessageValue(id, idx, 0);
190    }
191
192    /**
193     * @since 7.2
194     */
195    public String getSubWidgetMessageValue(String id, int idx, int msgIdx) {
196        String subId = id + "_message";
197        if (msgIdx != 0) {
198            subId += "_" + msgIdx;
199        }
200        return getMessageValue(":" + getListSubElementSuffix(subId, idx));
201    }
202
203}