001/*
002 * (C) Copyright 2011-2016 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 *     Sun Seng David TAN
018 *     Florent Guillaume
019 *     Antoine Taillefer
020 *     Yannis JULIENNE
021 */
022package org.nuxeo.functionaltests.pages.tabs;
023
024import java.util.List;
025
026import org.nuxeo.functionaltests.AbstractTest;
027import org.nuxeo.functionaltests.Locator;
028import org.nuxeo.functionaltests.Required;
029import org.nuxeo.functionaltests.contentView.ContentViewElement;
030import org.nuxeo.functionaltests.pages.AbstractPage;
031import org.nuxeo.functionaltests.pages.DocumentBasePage;
032import org.openqa.selenium.Alert;
033import org.openqa.selenium.By;
034import org.openqa.selenium.NoSuchElementException;
035import org.openqa.selenium.StaleElementReferenceException;
036import org.openqa.selenium.WebDriver;
037import org.openqa.selenium.WebElement;
038import org.openqa.selenium.support.FindBy;
039
040import static org.junit.Assert.assertEquals;
041import static org.junit.Assert.assertNotNull;
042
043/**
044 * The content tab sub page. Most of the time available for folderish documents and displaying the current document's
045 * children.
046 */
047public class ContentTabSubPage extends DocumentBasePage {
048
049    /**
050     * @since 8.3
051     */
052    public static final String COPY = "Copy";
053
054    /**
055     * @since 8.3
056     */
057    public static final String PASTE = "Paste";
058
059    /**
060     * @since 8.3
061     */
062    public static final String DELETE = "Delete";
063
064    /**
065     * @since 8.3
066     */
067    public static final String ADD_TO_WORKLIST = "Add to Worklist";
068
069    @Required
070    @FindBy(id = "document_content")
071    WebElement documentContentForm;
072
073    @FindBy(linkText = "New")
074    WebElement newButton;
075
076    @FindBy(id = "cv_document_content_0_quickFilterForm:nxl_document_content_filter:nxw_search_title")
077    WebElement filterInput;
078
079    @FindBy(id = "cv_document_content_0_quickFilterForm:submitFilter")
080    WebElement filterButton;
081
082    @FindBy(id = "cv_document_content_0_resetFilterForm:resetFilter")
083    WebElement clearFilterButton;
084
085    public ContentTabSubPage(WebDriver driver) {
086        super(driver);
087    }
088
089    protected ContentViewElement getElement() {
090        return AbstractTest.getWebFragment(By.id("cv_document_content_0_panel"), ContentViewElement.class);
091    }
092
093    public List<WebElement> getChildDocumentRows() {
094        return getElement().getItems();
095    }
096
097    /**
098     * Clicking on one of the child with the title.
099     *
100     * @param documentTitle the document title
101     */
102    public DocumentBasePage goToDocument(String documentTitle) {
103        getElement().clickOnItemTitle(documentTitle);
104        return asPage(DocumentBasePage.class);
105    }
106
107    /**
108     * Clicks on the new button and select the type of document to create
109     *
110     * @param docType the document type to create
111     * @param pageClassToProxy The page object type to return
112     * @return The create form page object
113     */
114    public <T> T getDocumentCreatePage(String docType, Class<T> pageClassToProxy) {
115        waitUntilEnabledAndClick(newButton);
116        WebElement fancyBox = AbstractPage.getFancyBoxContent();
117        // find the link to doc type that needs to be created
118        WebElement link = fancyBox.findElement(By.linkText(docType));
119        assertNotNull(link);
120        link.click();
121        return asPage(pageClassToProxy);
122    }
123
124    public DocumentBasePage removeDocument(String documentTitle) {
125        getElement().checkByTitle(documentTitle);
126        deleteSelectedDocuments();
127        return asPage(DocumentBasePage.class);
128    }
129
130    protected void deleteSelectedDocuments() {
131        waitUntilEnabledAndClick(getElement().getSelectionActionByTitle(DELETE));
132        Alert alert = driver.switchTo().alert();
133        assertEquals("Delete selected document(s)?", alert.getText());
134        alert.accept();
135    }
136
137    public DocumentBasePage addToWorkList(String documentTitle) {
138        getElement().checkByTitle(documentTitle);
139        waitUntilEnabledAndClick(getElement().getSelectionActionByTitle(ADD_TO_WORKLIST));
140        return asPage(DocumentBasePage.class);
141    }
142
143    /**
144     * Removes all documents visible on current page.
145     */
146    public ContentTabSubPage removeAllDocuments() {
147        ContentViewElement cv = getElement();
148        if (cv.getItems().size() == 0) {
149            // no document to remove
150            return this;
151        }
152        cv.checkAllItems();
153        deleteSelectedDocuments();
154        return asPage(ContentTabSubPage.class);
155    }
156
157    /**
158     * Perform filter on the given string.
159     *
160     * @param filter the string to filter
161     * @since 5.7.2
162     */
163    public ContentTabSubPage filterDocument(final String filter, final int expectedNbOfDisplayedResult,
164            final int timeout) {
165        filterInput.clear();
166        filterInput.sendKeys(filter);
167        filterButton.click();
168        Locator.waitUntilGivenFunction(driver -> {
169            try {
170                return getChildDocumentRows().size() == expectedNbOfDisplayedResult;
171            } catch (NoSuchElementException | StaleElementReferenceException e) {
172                return false;
173            }
174        });
175        return asPage(ContentTabSubPage.class);
176    }
177
178    /**
179     * Reset the filter.
180     *
181     * @since 5.7.2
182     */
183    public ContentTabSubPage clearFilter(final int expectedNbOfDisplayedResult, final int timeout) {
184        Locator.waitUntilEnabledAndClick(clearFilterButton);
185        Locator.waitUntilGivenFunction(driver -> {
186            try {
187                return getChildDocumentRows().size() == expectedNbOfDisplayedResult;
188            } catch (NoSuchElementException | StaleElementReferenceException e) {
189                return false;
190            }
191        });
192        return asPage(ContentTabSubPage.class);
193    }
194
195    /**
196     * Selects documents by their index in the content view.
197     *
198     * @since 5.7.8
199     * @deprecated since 8.1, use {@link #selectByIndex(int...)} instead.
200     */
201    @Deprecated
202    public ContentTabSubPage selectDocumentByIndex(int... indexes) {
203        return selectByIndex(indexes);
204    }
205
206    /**
207     * Selects documents by their index in the content view.
208     *
209     * @since 8.1
210     */
211    public ContentTabSubPage selectByIndex(int... indexes) {
212        getElement().checkByIndex(indexes);
213        return asPage(ContentTabSubPage.class);
214    }
215
216    /**
217     * Selects documents by their title in the content view.
218     *
219     * @since 5.7.8
220     * @deprecated since 8.1 use {@link #selectByTitle(String...)}
221     */
222    @Deprecated
223    public ContentTabSubPage selectDocumentByTitles(String... titles) {
224        return selectByTitle(titles);
225    }
226
227    /**
228     * Selects documents by title in the content view.
229     *
230     * @since 8.1
231     */
232    public ContentTabSubPage selectByTitle(String... titles) {
233        getElement().checkByTitle(titles);
234        return asPage(ContentTabSubPage.class);
235    }
236
237    /**
238     * Selects documents by their index in the content view and copy them in the clipboard.
239     *
240     * @param indexes the indexes
241     * @since 5.7.8
242     */
243    public ContentTabSubPage copyByIndex(int... indexes) {
244        getElement().checkByIndex(indexes);
245        getElement().getSelectionActionByTitle(COPY).click();
246        return asPage(ContentTabSubPage.class);
247    }
248
249    /**
250     * Selects documents by their title in the content view and copy them in the clipboard.
251     *
252     * @param titles the titles
253     * @since 5.7.8
254     */
255    public ContentTabSubPage copyByTitle(String... titles) {
256        getElement().checkByTitle(titles);
257        getElement().getSelectionActionByTitle(COPY).click();
258        return asPage(ContentTabSubPage.class);
259    }
260
261    /**
262     * Pastes the content of the clip board.
263     *
264     * @since 5.7.8
265     */
266    public ContentTabSubPage paste() {
267        getElement().getSelectionActionByTitle(PASTE).click();
268        return asPage(ContentTabSubPage.class);
269    }
270
271    /**
272     * @since 5.9.3
273     */
274    public DocumentBasePage goToDocument(final int index) {
275        waitUntilEnabledAndClick(getChildDocumentRows().get(index).findElement(By.xpath("td[3]/div/a[1]")));
276        return asPage(DocumentBasePage.class);
277    }
278
279    /**
280     * @since 8.3
281     */
282    public boolean hasDocumentLink(String title) {
283        try {
284            WebElement element = documentContentForm.findElement(By.linkText(title));
285            return element != null;
286        } catch (NoSuchElementException e) {
287            return false;
288        }
289    }
290
291    /**
292     * @since 8.3
293     */
294    public boolean hasNewButton() {
295        try {
296            return newButton.isDisplayed();
297        } catch (NoSuchElementException e) {
298            return false;
299        }
300    }
301}