001/*
002 * (C) Copyright 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
018 *     <a href="mailto:gbarata@nuxeo.com">Gabriel</a>
019 */
020package org.nuxeo.functionaltests.pages.tabs;
021
022import static org.junit.Assert.assertEquals;
023
024import java.util.List;
025
026import org.nuxeo.functionaltests.AbstractTest;
027import org.nuxeo.functionaltests.AjaxRequestManager;
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.WebDriver;
036import org.openqa.selenium.WebElement;
037import org.openqa.selenium.support.FindBy;
038
039/**
040 * @since 5.9.3
041 */
042public class TrashSubPage extends AbstractPage {
043
044    protected static final String SELECT_ALL_BUTTON_ID = "document_trash_content:nxl_document_listing_table:listing_table_selection_box_with_current_document_header";
045
046    protected static final String PERMANENT_DELETE_BUTTON_ID = "document_trash_content_buttons:nxw_CURRENT_SELECTION_DELETE_form:nxw_CURRENT_SELECTION_DELETE";
047
048    protected static final String RESTORE_BUTTON_ID = "document_trash_content_buttons:nxw_CURRENT_SELECTION_UNDELETE_form:nxw_CURRENT_SELECTION_UNDELETE";
049
050    protected static final String EMPTY_TRASH_BUTTON_ID = "document_trash_content_buttons:nxw_CURRENT_SELECTION_EMPTY_TRASH_form:nxw_CURRENT_SELECTION_EMPTY_TRASH";
051
052    @FindBy(xpath = "//form[@id=\"document_trash_content\"]//tbody//tr")
053    List<WebElement> childDocumentRows;
054
055    public List<WebElement> getChildDocumentRows() {
056        return childDocumentRows;
057    }
058
059    @Required
060    @FindBy(id = "cv_document_trash_content_0_resultsPanel")
061    protected WebElement documentContentForm;
062
063    public TrashSubPage(WebDriver driver) {
064        super(driver);
065    }
066
067    public TrashSubPage emptyTrash() {
068        findElementWaitUntilEnabledAndClick(By.id(EMPTY_TRASH_BUTTON_ID));
069        Alert alert = driver.switchTo().alert();
070        assertEquals("Permanently delete all documents in trash?", alert.getText());
071        alert.accept();
072        return asPage(TrashSubPage.class);
073    }
074
075    /**
076     * Removes all documents visible on current page.
077     */
078    public TrashSubPage purgeAllDocuments() {
079        TrashSubPage page = asPage(TrashSubPage.class);
080        By locator = By.id(SELECT_ALL_BUTTON_ID);
081        if (!hasElement(locator)) {
082            // no documents to remove
083            return page;
084        }
085        AjaxRequestManager arm = new AjaxRequestManager(driver);
086        arm.begin();
087        findElementWaitUntilEnabledAndClick(By.id(SELECT_ALL_BUTTON_ID));
088        arm.end();
089
090        deleteSelectedDocuments();
091
092        return asPage(TrashSubPage.class);
093    }
094
095    /**
096     * @since 8.3
097     */
098    public TrashSubPage goToDocument(final int index) {
099        getChildDocumentRows().get(index).findElement(By.xpath("td[3]/div/a[1]")).click();
100        return asPage(TrashSubPage.class);
101    }
102
103    /**
104     * @since 8.3
105     */
106    public TrashSubPage goToDocument(String documentTitle) {
107        getElement().clickOnItemTitle(documentTitle);
108        return asPage(TrashSubPage.class);
109    }
110
111    /**
112     * @since 8.3
113     */
114    public TrashSubPage selectByTitle(String... titles) {
115        getElement().checkByTitle(titles);
116        return asPage(TrashSubPage.class);
117    }
118
119    /**
120     * @since 8.3
121     */
122    public DocumentBasePage restoreDocument(String... titles) {
123        getElement().checkByTitle(titles);
124        restoreSelectedDocuments();
125        return asPage(DocumentBasePage.class);
126    }
127
128    /**
129     * @since 8.3
130     */
131    public TrashSubPage purgeDocument(String... titles) {
132        getElement().checkByTitle(titles);
133        deleteSelectedDocuments();
134        return asPage(TrashSubPage.class);
135    }
136
137    /**
138     * @since 8.3
139     */
140    public boolean hasDocumentLink(String title) {
141        try {
142            WebElement element = documentContentForm.findElement(By.linkText(title));
143            return element != null;
144        } catch (NoSuchElementException e) {
145            return false;
146        }
147    }
148
149    /**
150     * @since 8.3
151     */
152    protected ContentViewElement getElement() {
153        return AbstractTest.getWebFragment(By.id("cv_document_trash_content_0_panel"), ContentViewElement.class);
154    }
155
156    /**
157     * @since 8.3
158     */
159    protected void restoreSelectedDocuments() {
160        findElementWaitUntilEnabledAndClick(By.id(RESTORE_BUTTON_ID));
161        Alert alert = driver.switchTo().alert();
162        assertEquals("Undelete selected document(s)?", alert.getText());
163        alert.accept();
164    }
165
166    protected void deleteSelectedDocuments() {
167        findElementWaitUntilEnabledAndClick(By.id(PERMANENT_DELETE_BUTTON_ID));
168        Alert alert = driver.switchTo().alert();
169        assertEquals("Permanently delete selected document(s)?", alert.getText());
170        alert.accept();
171    }
172
173}