001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
016 */
017package org.nuxeo.functionaltests.pages.search;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022
023import org.nuxeo.functionaltests.AjaxRequestManager;
024import org.nuxeo.functionaltests.Locator;
025import org.nuxeo.functionaltests.Required;
026import org.nuxeo.functionaltests.forms.Select2WidgetElement;
027import org.nuxeo.functionaltests.pages.search.aggregates.CheckBoxAggregateElements;
028import org.nuxeo.functionaltests.pages.search.aggregates.Select2AggregateElement;
029import org.openqa.selenium.By;
030import org.openqa.selenium.NoSuchElementException;
031import org.openqa.selenium.WebDriver;
032import org.openqa.selenium.WebElement;
033import org.openqa.selenium.support.FindBy;
034
035import com.google.common.base.Function;
036
037/**
038 * @since 6.0
039 */
040public class DefaultSearchSubPage extends AbstractSearchSubPage {
041
042    private static final String S2_COLLECTION_XPATH = "//*[@id='s2id_nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_visible_collection_select2']";
043
044    public static final String TREE_PATH_ID = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_ecm_path_treeId";
045
046    @FindBy(id = "s2id_nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_creator_agg")
047    protected WebElement authorAggregate;
048
049    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_coverage_agg")
050    protected WebElement coverageAggregate;
051
052    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_created_agg")
053    protected WebElement createdAggregate;
054
055    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_modified_agg")
056    protected WebElement modifiedAggregate;
057
058    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_common_size_agg")
059    protected WebElement sizeAggregate;
060
061    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_subjects_agg")
062    protected WebElement subjectsAggregate;
063
064    @FindBy(id = "nxw_ecm_path_openPopup")
065    @Required
066    protected WebElement openPathPopupButton;
067
068    @FindBy(id = "nxw_ecm_path")
069    @Required
070    protected WebElement selectPathDiv;
071
072    public static final String PATH_REGEX = "(.*) \\((.*)\\)";
073
074    protected static final String EXPAND_XPATH = "ancestor::div[@class='rf-trn']/span[contains(@class,'rf-trn-hnd')]";
075
076    public DefaultSearchSubPage(WebDriver driver) {
077        super(driver);
078    }
079
080    public Map<String, Integer> getAvailableCoverageAggregate() {
081        return new CheckBoxAggregateElements(coverageAggregate).getAggregates();
082    }
083
084    public Map<String, Integer> getAvailableCreatedAggregate() {
085        return new CheckBoxAggregateElements(createdAggregate).getAggregates();
086    }
087
088    public Map<String, Integer> getAvailableModifiedAggregate() {
089        return new CheckBoxAggregateElements(modifiedAggregate).getAggregates();
090    }
091
092    public Map<String, Integer> getAvailableSizeAggregate() {
093        return new CheckBoxAggregateElements(sizeAggregate).getAggregates();
094    }
095
096    public Map<String, Integer> getAvailableSubjectsAggregate() {
097        return new CheckBoxAggregateElements(subjectsAggregate).getAggregates();
098    }
099
100    public WebElement getCoverageAggregate() {
101        return coverageAggregate;
102    }
103
104    public WebElement getCreatedAggregate() {
105        return createdAggregate;
106    }
107
108    public WebElement getModifiedAggregate() {
109        return modifiedAggregate;
110    }
111
112    public WebElement getSizeAggregate() {
113        return sizeAggregate;
114    }
115
116    public WebElement getSubjectsAggregate() {
117        return subjectsAggregate;
118    }
119
120    /**
121     * @since 7.4
122     */
123    public Map<String, Integer> getAvailableAuthorAggregate() {
124        Select2AggregateElement s2AuthorAggregate =  new Select2AggregateElement(driver, authorAggregate, true);
125        return s2AuthorAggregate.getAggregates();
126    }
127
128    public SearchPage selectCoverageAggregate(String label) {
129        new CheckBoxAggregateElements(driver, coverageAggregate).selectOrUnselect(label);
130        return asPage(SearchPage.class);
131    }
132
133    public SearchPage selectCreatedAggregate(String label) {
134        new CheckBoxAggregateElements(driver, createdAggregate).selectOrUnselect(label);
135        return asPage(SearchPage.class);
136    }
137
138    public SearchPage selectModifiedAggregate(String label) {
139        new CheckBoxAggregateElements(driver, modifiedAggregate).selectOrUnselect(label);
140        return asPage(SearchPage.class);
141    }
142
143    public SearchPage selectSizeAggregate(String label) {
144        new CheckBoxAggregateElements(driver, sizeAggregate).selectOrUnselect(label);
145        return asPage(SearchPage.class);
146    }
147
148    public SearchPage selectSubjectsAggregate(String label) {
149        new CheckBoxAggregateElements(driver, subjectsAggregate).selectOrUnselect(label);
150        return asPage(SearchPage.class);
151    }
152
153    public void selectPath(String path) {
154        assert (path != null && !path.isEmpty() && path.charAt(0) == '/');
155        openPathPopupButton.click();
156        Locator.waitUntilGivenFunction(new Function<WebDriver, Boolean>() {
157            @Override
158            public Boolean apply(WebDriver driver) {
159                try {
160                    WebElement tree = driver.findElement(By.id(TREE_PATH_ID));
161                    return tree.isDisplayed();
162                } catch (NoSuchElementException e) {
163                    return false;
164                }
165            }
166        });
167        if (path.length() == 1) {
168            AjaxRequestManager a = new AjaxRequestManager(driver);
169            a.watchAjaxRequests();
170            driver.findElement(By.id(TREE_PATH_ID)).findElement(By.linkText("/")).click();
171            a.waitForAjaxRequests();
172            return;
173        } else {
174            AjaxRequestManager a = new AjaxRequestManager(driver);
175            a.watchAjaxRequests();
176            driver.findElement(By.id(TREE_PATH_ID)).findElement(By.linkText("/")).findElement(By.xpath(EXPAND_XPATH)).click();
177            a.waitForAjaxRequests();
178        }
179        String[] pathArray = path.substring(1).split("/");
180        int i = 0;
181        for (; i < pathArray.length - 1; i++) {
182            AjaxRequestManager a = new AjaxRequestManager(driver);
183            a.watchAjaxRequests();
184            driver.findElement(By.id(TREE_PATH_ID)).findElement(By.linkText(pathArray[i])).findElement(
185                    By.xpath(EXPAND_XPATH)).click();
186            a.waitForAjaxRequests();
187        }
188        AjaxRequestManager a = new AjaxRequestManager(driver);
189        a.watchAjaxRequests();
190        driver.findElement(By.id(TREE_PATH_ID)).findElement(By.linkText(pathArray[i])).click();
191        a.waitForAjaxRequests();
192        driver.findElement(By.id("fancybox-close")).click();
193        Locator.waitUntilGivenFunction(new Function<WebDriver, Boolean>() {
194            @Override
195            public Boolean apply(WebDriver driver) {
196                try {
197                    WebElement tree = driver.findElement(By.id(TREE_PATH_ID));
198                    return !tree.isDisplayed();
199                } catch (NoSuchElementException e) {
200                    return false;
201                }
202            }
203        });
204    }
205
206    public void deselectPath(String path) {
207        assert (path != null && !path.isEmpty());
208        int lastPartIndex = path.lastIndexOf('/');
209        String folderName = path.substring(lastPartIndex + 1);
210        WebElement e = selectPathDiv.findElement(By.xpath("descendant::label[contains(text(),'" + folderName
211                + "')]/ancestor::span[@class='sticker']/a"));
212        AjaxRequestManager a = new AjaxRequestManager(driver);
213        a.watchAjaxRequests();
214        e.click();
215        a.waitForAjaxRequests();
216    }
217
218    /**
219     * @since 7.3
220     */
221    public void selectCollections(final String[] collections) {
222        Select2WidgetElement collectionsWidget = new Select2WidgetElement(
223                driver,
224                driver.findElement(By.xpath(S2_COLLECTION_XPATH)),
225                true);
226        collectionsWidget.selectValues(collections);
227    }
228
229    /**
230     * @since 7.3
231     */
232    public List<String> getSelectedCollections() {
233        Select2WidgetElement collectionsWidget = new Select2WidgetElement(
234                driver,
235                driver.findElement(By.xpath(S2_COLLECTION_XPATH)),
236                true);
237        List<String> result = new ArrayList<String>();
238        for (WebElement el : collectionsWidget.getSelectedValues()) {
239            result.add(el.getText());
240        }
241        return result;
242    }
243}