001/*
002 * (C) Copyright 2014 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 *     Nelson Silva
018 */
019package org.nuxeo.functionaltests.contentView;
020
021import java.util.List;
022
023import org.nuxeo.functionaltests.AjaxRequestManager;
024import org.nuxeo.functionaltests.Locator;
025import org.nuxeo.functionaltests.fragment.WebFragmentImpl;
026import org.openqa.selenium.By;
027import org.openqa.selenium.NoSuchElementException;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030
031/**
032 * Represents a content view element.
033 *
034 * @since 7.1
035 */
036public class ContentViewElement extends WebFragmentImpl {
037
038    private static final String SELECT_ALL_BUTTON_XPATH = "//input[@type=\"checkbox\" and @title=\"Select all / deselect all\"]";
039
040    private static final String CHECK_BOX_XPATH = "td/input[@type=\"checkbox\"]";
041
042    public static enum ResultLayout {
043        THUMBNAIL("Thumbnail view"), LISTING("List view");
044
045        private final String title;
046
047        ResultLayout(String title) {
048            this.title = title;
049        }
050    }
051
052    public ContentViewElement(WebDriver driver, WebElement element) {
053        super(driver, element);
054    }
055
056    protected ContentViewElement reload(String id) {
057        return getWebFragment(By.id(id), ContentViewElement.class);
058    }
059
060    protected String getContentViewId() {
061        String id = getId();
062        if (id.endsWith("_panel")) {
063            return id.substring(0, id.length() - "_panel".length());
064        }
065        return id;
066    }
067
068    protected WebElement getResultsPanel() {
069        String id = getContentViewId() + "_resultsPanel";
070        return getElement().findElement(By.id(id));
071    }
072
073    public WebElement getActionByTitle(String title) {
074        return getElement().findElement(By.className("contentViewUpperActions"))
075                           .findElement(By.xpath("//img[@alt=\"" + title + "\"]"));
076    }
077
078    public ContentViewElement switchToResultLayout(ResultLayout layout) {
079        // get id before element is detached from DOM (during next ajax call)
080        String id = getId();
081        AjaxRequestManager a = new AjaxRequestManager(driver);
082        a.watchAjaxRequests();
083        getActionByTitle(layout.title).click();
084        a.waitForAjaxRequests();
085        return reload(id);
086    }
087
088    /**
089     * @since 8.3
090     */
091    public List<WebElement> getItems() {
092        return getResultsPanel().findElements(By.xpath("(.//form)[1]//tbody//tr"));
093    }
094
095    /**
096     * @since 8.3
097     */
098    public void clickOnItemTitle(String title) {
099        Locator.findElementWaitUntilEnabledAndClick(getResultsPanel(), By.linkText(title));
100    }
101
102    /**
103     * @since 8.3
104     */
105    public boolean hasItem(String title) {
106        try {
107            WebElement element = getResultsPanel().findElement(By.linkText(title));
108            return element != null;
109        } catch (NoSuchElementException e) {
110            return false;
111        }
112    }
113
114    /**
115     * @since 8.3
116     */
117    public ContentViewElement checkByTitle(String... titles) {
118        // get id before element is detached from DOM (during next ajax calls)
119        String id = getId();
120        List<WebElement> items = getItems();
121        for (WebElement item : items) {
122            for (String title : titles) {
123                try {
124                    item.findElement(By.linkText(title));
125                    AjaxRequestManager arm = new AjaxRequestManager(driver);
126                    arm.begin();
127                    Locator.findElementWaitUntilEnabledAndClick(item, By.xpath(CHECK_BOX_XPATH));
128                    arm.end();
129                    break;
130                } catch (NoSuchElementException e) {
131                    // next
132                }
133            }
134        }
135        return reload(id);
136    }
137
138    /**
139     * @since 8.3
140     */
141    public ContentViewElement checkByIndex(int... indexes) {
142        // get id before element is detached from DOM (during next ajax call)
143        String id = getId();
144        AjaxRequestManager a = new AjaxRequestManager(driver);
145        for (int i : indexes) {
146            a.watchAjaxRequests();
147            getItems().get(i).findElement(By.xpath(CHECK_BOX_XPATH)).click();
148            a.waitForAjaxRequests();
149        }
150        return reload(id);
151    }
152
153    /**
154     * @since 8.3
155     */
156    public ContentViewElement checkAllItems() {
157        WebElement selectAll = null;
158        try {
159            selectAll = getResultsPanel().findElement(By.xpath(SELECT_ALL_BUTTON_XPATH));
160        } catch (NoSuchElementException e) {
161            // no item
162        }
163        if (selectAll != null) {
164            // get id before element is detached from DOM (during next ajax call)
165            String id = getId();
166            AjaxRequestManager arm = new AjaxRequestManager(driver);
167            arm.begin();
168            Locator.scrollAndForceClick(selectAll);
169            arm.end();
170            return reload(id);
171        }
172        return this;
173    }
174
175    /**
176     * @since 8.3
177     */
178    public WebElement getSelectionActionByTitle(String title) {
179        return getResultsPanel().findElement(By.xpath("//div[contains(@id,'nxw_cvButton_panel')]"))
180                                .findElement(By.xpath("//input[@value=\"" + title + "\"]"));
181    }
182
183}