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