001/*
002 * (C) Copyright 2013 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.Arrays;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.functionaltests.Locator;
027import org.openqa.selenium.By;
028import org.openqa.selenium.NoSuchElementException;
029import org.openqa.selenium.WebDriver;
030import org.openqa.selenium.WebElement;
031import org.openqa.selenium.support.ui.FluentWait;
032
033import com.google.common.base.Function;
034
035/**
036 * Fragment representing the gadgets container.
037 *
038 * @since 5.7.3
039 */
040public class GadgetsContainerFragment extends WebFragmentImpl {
041
042    private static final Log log = LogFactory.getLog(GadgetsContainerFragment.class);
043
044    public static final String GADGETS_CONTAINER_ID = "gwtContainerDiv";
045
046    public GadgetsContainerFragment(WebDriver driver, WebElement element) {
047        super(driver, element);
048    }
049
050    public WebElement waitForGadgetsLoad() {
051        return waitForGadgetsLoad("nxDocumentListData,content");
052    }
053
054    public WebElement waitForGadgetsLoad(final String mandatoryElements) {
055        FluentWait<WebDriver> wait = Locator.getFluentWait();
056        wait.ignoring(NoSuchElementException.class);
057        Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>() {
058            @Override
059            public WebElement apply(WebDriver driver) {
060                WebElement container = getElement();
061                // iterate through all frames, and ensure opensocial ones are
062                // loaded, and expect at least one opensocial frame
063                boolean oneFound = false;
064                List<WebElement> framesList = driver.findElements(By.xpath("//iframe"));
065                if (framesList != null && !framesList.isEmpty()) {
066                    List<String> mandatory = Arrays.asList(mandatoryElements.split(","));
067                    for (WebElement frame : framesList) {
068                        String frameName = frame.getAttribute("name");
069                        if (frameName == null || !frameName.startsWith("open-social")) {
070                            continue;
071                        }
072                        log.debug(String.format("Found one GWT gadget frame named '%s' ", frameName));
073                        oneFound = true;
074                        boolean loaded = false;
075                        driver.switchTo().defaultContent();
076                        driver.switchTo().frame(frame);
077                        for (String mand : mandatory) {
078                            try {
079                                driver.findElement(By.id(mand));
080                                loaded = true;
081                                log.debug(String.format("Gadget frame '%s' mandatory element '%s' loaded", frameName,
082                                        mand));
083                            } catch (NoSuchElementException e) {
084                                loaded = false;
085                                log.debug(String.format(
086                                        "Gadget frame '%s' not loaded yet, " + "mandatory element '%s' not found",
087                                        frameName, mand));
088                                break;
089                            }
090                        }
091                        if (!loaded) {
092                            log.debug(String.format("Gadget frame '%s' not loaded yet", frameName));
093                            driver.switchTo().defaultContent();
094                            return null;
095                        }
096                        log.debug(String.format("Gadget frame '%s' loaded", frameName));
097                        driver.switchTo().defaultContent();
098                    }
099                }
100                if (oneFound) {
101                    return container;
102                }
103                log.debug("No gadget frame loaded yet");
104                return null;
105            }
106        };
107        return wait.until(function);
108    }
109
110    public boolean isGadgetLoaded(String gadgetTitle) {
111        return getGadgetTitleElement(gadgetTitle) != null;
112    }
113
114    public WebElement getGadgetTitleElement(String gadgetTitle) {
115        WebElement gtwContainer = waitForGadgetsLoad();
116        List<WebElement> gadgets = gtwContainer.findElements(By.className("dragdrop-draggable"));
117        for (WebElement gadget : gadgets) {
118            WebElement title = gadget.findElement(By.className("header"));
119            if (title.getText().contains(gadgetTitle)) {
120                return title;
121            }
122        }
123        throw new NoSuchElementException(gadgetTitle);
124    }
125
126    public boolean isTaskGadgetEmpty(String gadgetTitle) {
127        WebDriver driver = switchToFrame(gadgetTitle);
128        boolean res = driver.findElement(By.id("nxDocumentListData")).getText().contains(
129                "Your dashboard is empty. There are no tasks that require your intervention.");
130        // switch back to parent page after that
131        driver.switchTo().defaultContent();
132        return res;
133    }
134
135    public WebDriver switchToFrame(String gadgetTitle) {
136        WebElement title = getGadgetTitleElement(gadgetTitle);
137        WebElement parent = title.findElement(By.xpath("parent::*"));
138        driver.switchTo().defaultContent();
139        return driver.switchTo().frame("open-social-" + parent.getAttribute("id"));
140    }
141
142}