001/*
002 * (C) Copyright 2015 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.functionaltests.fragment;
020
021import java.util.List;
022
023import org.nuxeo.functionaltests.AbstractTest;
024import org.nuxeo.functionaltests.Locator;
025import org.nuxeo.functionaltests.pages.DocumentBasePage;
026import org.openqa.selenium.By;
027import org.openqa.selenium.StaleElementReferenceException;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030
031import com.google.common.base.Function;
032
033/**
034 * Multiple version of the "add to collection" popup, triggered from content views.
035 *
036 * @since 8.1
037 */
038public class AddAllToCollectionForm extends AddToCollectionForm {
039
040    private final static String ADD_BUTTON_ID = "document_content_buttons:nxw_cvButton_addSelectedToCollectionAction_fancy_subview:nxw_cvButton_addSelectedToCollectionAction_fancyform:addAll";
041
042    private final static String S2_CHOOSE_COLLECTION_ID = "s2id_document_content_buttons:nxw_cvButton_addSelectedToCollectionAction_fancy_subview:nxw_cvButton_addSelectedToCollectionAction_fancyform:nxw_singleDocumentSuggestion_2_select2";
043
044    private final static String NEW_COLLECTION_DESCRIPTION_ID = "document_content_buttons:nxw_cvButton_addSelectedToCollectionAction_fancy_subview:nxw_cvButton_addSelectedToCollectionAction_fancyform:nxw_cvButton_addSelectedToCollectionAction_fancyform_collectionDescriptionsPanel";
045
046    private static final String EXISTING_COLLECTION_DESCRIPTION_ID = "document_content_buttons:nxw_cvButton_addSelectedToCollectionAction_fancy_subview:nxw_cvButton_addSelectedToCollectionAction_fancyform:scd";
047
048    public AddAllToCollectionForm(WebDriver driver, WebElement element) {
049        super(driver, element);
050        multiple = true;
051    }
052
053    public DocumentBasePage addAll() {
054        return addAll(DocumentBasePage.class);
055    }
056
057    public <T> T addAll(final Class<T> pageClassProxy) {
058        Locator.findElementWaitUntilEnabledAndClick(By.id(ADD_BUTTON_ID));
059        return AbstractTest.asPage(pageClassProxy);
060    }
061
062    @Override
063    protected String getAddButtonId() {
064        return ADD_BUTTON_ID;
065    }
066
067    @Override
068    protected String getChooseCollectionId() {
069        return S2_CHOOSE_COLLECTION_ID;
070    }
071
072    @Override
073    protected String getNewCollectionDescriptionId() {
074        return NEW_COLLECTION_DESCRIPTION_ID;
075    }
076
077    @Override
078    protected String getCollectionDescriptionId() {
079        return EXISTING_COLLECTION_DESCRIPTION_ID;
080    }
081
082    public void removeDocumentToBeAddedToCollection(int index) {
083        if (!isMultiple()) {
084            throw new UnsupportedOperationException("You are not adding many documents to the collection");
085        }
086
087        List<WebElement> docsToBeAdded = getElement().findElements(By.xpath("//div[@class='simpleBox']"));
088
089        final int docsToBeAddedSize = docsToBeAdded.size();
090
091        docsToBeAdded.get(index).findElement(By.xpath("a")).click();
092
093        Locator.waitUntilGivenFunctionIgnoring(new Function<WebDriver, Boolean>() {
094            @Override
095            public Boolean apply(WebDriver driver) {
096                return getElement().findElements(By.xpath("//div[@class='simpleBox']")).size() == docsToBeAddedSize - 1;
097            }
098        }, StaleElementReferenceException.class);
099
100    }
101
102}