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