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 static org.junit.Assert.assertEquals;
025import static org.junit.Assert.assertNotNull;
026
027import org.nuxeo.functionaltests.Locator;
028import org.nuxeo.functionaltests.Required;
029import org.nuxeo.functionaltests.contentView.ContentViewElement;
030import org.nuxeo.functionaltests.contentView.ContentViewSelectionActions;
031import org.nuxeo.functionaltests.fragment.AddAllToCollectionForm;
032import org.nuxeo.functionaltests.pages.AbstractPage;
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
040/**
041 * The content tab sub page. Most of the time available for folderish documents and displaying the current document's
042 * children.
043 */
044public class ContentTabSubPage extends AbstractContentTabSubPage {
045
046    /**
047     * @since 8.3
048     */
049    public static final String COPY = "Copy";
050
051    /**
052     * @since 8.3
053     */
054    public static final String PASTE = "Paste";
055
056    /**
057     * @since 8.3
058     * @deprecated since 9.1 not used anymore as we now have {@link ContentViewSelectionActions#delete()}
059     */
060    public static final String DELETE = "Delete";
061
062    /**
063     * @since 8.3
064     */
065    public static final String ADD_TO_WORKLIST = "Add to Worklist";
066
067    /**
068     * @since 9.1
069     */
070    public static final String ADD_TO_COLLECTION = "Add to collection";
071
072    @Required
073    @FindBy(id = "document_content")
074    WebElement documentContentForm;
075
076    @Required
077    @FindBy(id = "cv_document_content_0_panel")
078    WebElement contentView;
079
080    @FindBy(linkText = "New")
081    WebElement newButton;
082
083    public ContentTabSubPage(WebDriver driver) {
084        super(driver);
085    }
086
087    @Override
088    protected WebElement getContentViewElement() {
089        return contentView;
090    }
091
092    /**
093     * @since 9.1 use {@link #getContentView()} instead.
094     */
095    @Deprecated
096    protected ContentViewElement getElement() {
097        return getContentView();
098    }
099
100    /**
101     * Clicks on the new button and select the type of document to create
102     *
103     * @param docType the document type to create
104     * @param pageClassToProxy The page object type to return
105     * @return The create form page object
106     */
107    public <T> T getDocumentCreatePage(String docType, Class<T> pageClassToProxy) {
108        waitUntilEnabledAndClick(newButton);
109        WebElement fancyBox = AbstractPage.getFancyBoxContent();
110        // find the link to doc type that needs to be created
111        WebElement link = fancyBox.findElement(By.linkText(docType));
112        assertNotNull(link);
113        link.click();
114        return asPage(pageClassToProxy);
115    }
116
117    /**
118     * @deprecated since 9.1 no need - use {@link ContentViewElement#selectByTitle(String...)} then the action instead
119     */
120    @Deprecated
121    protected void deleteSelectedDocuments() {
122        getContentView().getSelectionActions().delete();
123    }
124
125    public ContentTabSubPage addToWorkList(String documentTitle) {
126        return getContentView().selectByTitle(documentTitle).clickOnActionByTitle(ADD_TO_WORKLIST,
127                ContentTabSubPage.class);
128    }
129
130    /**
131     * Removes all documents visible on current page.
132     */
133    @Override
134    public ContentTabSubPage removeAllDocuments() {
135        return removeAllDocuments(ContentTabSubPage.class);
136    }
137
138    /**
139     * Perform filter on the given string.
140     *
141     * @param filter the string to filter
142     * @since 5.7.2
143     * @deprecated since 9.1 use {@link ContentTabSubPage#filterDocument(String)} instead and assert in your test the
144     *             expected number of results.
145     */
146    @Deprecated
147    public ContentTabSubPage filterDocument(final String filter, final int expectedNbOfDisplayedResult,
148            final int timeout) {
149        filterDocument(filter);
150        assertEquals(expectedNbOfDisplayedResult, getChildDocumentRows().size());
151        return asPage(ContentTabSubPage.class);
152    }
153
154    /**
155     * Perform filter on the given string.
156     *
157     * @param filter the string to filter
158     * @since 9.1
159     */
160    @Override
161    public ContentTabSubPage filterDocument(final String filter) {
162        return filterDocument(filter, ContentTabSubPage.class);
163    }
164
165    @Override
166    public ContentTabSubPage clearFilter() {
167        return clearFilter(ContentTabSubPage.class);
168    }
169
170    /**
171     * Reset the filter.
172     *
173     * @since 5.7.2
174     * @deprecated since 9.1 use {@link #clearFilter()} instead and assert the expected number of result in your test
175     */
176    @Deprecated
177    public ContentTabSubPage clearFilter(final int expectedNbOfDisplayedResult, final int timeout) {
178        Locator.waitUntilEnabledAndClick(clearFilterButton);
179        Locator.waitUntilGivenFunction(driver -> {
180            try {
181                return getChildDocumentRows().size() == expectedNbOfDisplayedResult;
182            } catch (NoSuchElementException | StaleElementReferenceException e) {
183                return false;
184            }
185        });
186        return asPage(ContentTabSubPage.class);
187    }
188
189    /**
190     * Selects documents by their index in the content view.
191     *
192     * @since 8.1
193     */
194    public ContentTabSubPage selectByIndex(int... indexes) {
195        getContentView().selectByIndex(indexes);
196        return asPage(ContentTabSubPage.class);
197    }
198
199    /**
200     * Selects documents by their title in the content view.
201     *
202     * @since 5.7.8
203     * @deprecated since 8.1 use {@link #selectByTitle(String...)}
204     */
205    @Deprecated
206    public ContentTabSubPage selectDocumentByTitles(String... titles) {
207        return selectByTitle(titles);
208    }
209
210    /**
211     * Selects documents by title in the content view.
212     *
213     * @since 8.1
214     */
215    public ContentTabSubPage selectByTitle(String... titles) {
216        getContentView().selectByTitle(titles);
217        return asPage(ContentTabSubPage.class);
218    }
219
220    /**
221     * Selects documents by their index in the content view and copy them in the clipboard.
222     *
223     * @param indexes the indexes
224     * @since 5.7.8
225     */
226    public ContentTabSubPage copyByIndex(int... indexes) {
227        return getContentView().selectByIndex(indexes).clickOnActionByTitle(COPY, ContentTabSubPage.class);
228    }
229
230    /**
231     * Selects documents by their title in the content view and copy them in the clipboard.
232     *
233     * @param titles the titles
234     * @since 5.7.8
235     */
236    public ContentTabSubPage copyByTitle(String... titles) {
237        return getContentView().selectByTitle(titles).clickOnActionByTitle(COPY, ContentTabSubPage.class);
238    }
239
240    /**
241     * Pastes the content of the clip board.
242     *
243     * @since 5.7.8
244     */
245    public ContentTabSubPage paste() {
246        return getContentView().getSelectionActions().clickOnActionByTitle(PASTE, ContentTabSubPage.class);
247    }
248
249    public AddAllToCollectionForm addToCollectionByIndex(int... indexes) {
250        getContentView().selectByIndex(indexes).clickOnActionByTitle(ADD_TO_COLLECTION);
251        WebElement elt = AbstractPage.getFancyBoxContent();
252        return getWebFragment(elt, AddAllToCollectionForm.class);
253    }
254
255    /**
256     * @since 8.3
257     */
258    public boolean hasNewButton() {
259        try {
260            return newButton.isDisplayed();
261        } catch (NoSuchElementException e) {
262            return false;
263        }
264    }
265}