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        AjaxRequestManager arm = new AjaxRequestManager(driver);
116        arm.begin();
117        getElement().checkByTitle(titles);
118        arm.end();
119        return asPage(TrashSubPage.class);
120    }
121
122    /**
123     * @since 8.3
124     */
125    public DocumentBasePage restoreDocument(String... titles) {
126        AjaxRequestManager arm = new AjaxRequestManager(driver);
127        arm.begin();
128        getElement().checkByTitle(titles);
129        arm.end();
130        restoreSelectedDocuments();
131        return asPage(DocumentBasePage.class);
132    }
133
134    /**
135     * @since 8.3
136     */
137    public TrashSubPage purgeDocument(String... titles) {
138        AjaxRequestManager arm = new AjaxRequestManager(driver);
139        arm.begin();
140        getElement().checkByTitle(titles);
141        arm.end();
142        deleteSelectedDocuments();
143        return asPage(TrashSubPage.class);
144    }
145
146    /**
147     * @since 8.3
148     */
149    public boolean hasDocumentLink(String title) {
150        try {
151            WebElement element = documentContentForm.findElement(By.linkText(title));
152            return element != null;
153        } catch (NoSuchElementException e) {
154            return false;
155        }
156    }
157
158    /**
159     * @since 8.3
160     */
161    protected ContentViewElement getElement() {
162        return AbstractTest.getWebFragment(By.id("cv_document_trash_content_0_panel"), ContentViewElement.class);
163    }
164
165    /**
166     * @since 8.3
167     */
168    protected void restoreSelectedDocuments() {
169        findElementWaitUntilEnabledAndClick(By.id(RESTORE_BUTTON_ID));
170        Alert alert = driver.switchTo().alert();
171        assertEquals("Undelete selected document(s)?", alert.getText());
172        alert.accept();
173    }
174
175    protected void deleteSelectedDocuments() {
176        findElementWaitUntilEnabledAndClick(By.id(PERMANENT_DELETE_BUTTON_ID));
177        Alert alert = driver.switchTo().alert();
178        assertEquals("Permanently delete selected document(s)?", alert.getText());
179        alert.accept();
180    }
181
182}