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