001/*
002 * (C) Copyright 2015 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 *     Anahide Tchertchian
016 */
017package org.nuxeo.functionaltests;
018
019import java.util.concurrent.TimeUnit;
020
021import org.openqa.selenium.JavascriptExecutor;
022import org.openqa.selenium.NoSuchElementException;
023import org.openqa.selenium.WebDriver;
024import org.openqa.selenium.support.ui.FluentWait;
025import org.openqa.selenium.support.ui.Wait;
026
027import com.google.common.base.Function;
028
029/**
030 * @since 7.2
031 */
032public class JSListRequestManager {
033
034    protected JavascriptExecutor js;
035
036    protected boolean active;
037
038    protected int count;
039
040    public JSListRequestManager(WebDriver driver) {
041        super();
042        js = (JavascriptExecutor) driver;
043        reset();
044    }
045
046    protected void reset() {
047        active = false;
048        count = 0;
049    }
050
051    public void begin() {
052        StringBuilder sb = new StringBuilder();
053        sb.append("if (window.NuxeoTestJsList === undefined) {");
054        sb.append("  window.NuxeoTestJsList = function() {");
055        sb.append("    var e = {};");
056        sb.append("    e.jsRequestStarted = false;");
057        sb.append("    e.jsRequestFinished = false;");
058        sb.append("    e.jsRequestActiveCount = 0;");
059        sb.append("    e.increment = function() {");
060        sb.append("      e.jsRequestStarted = true;");
061        sb.append("      e.jsRequestFinished = false;");
062        sb.append("      e.jsRequestActiveCount++;");
063        sb.append("    };");
064        sb.append("    e.decrement = function() {");
065        sb.append("      e.jsRequestActiveCount--;");
066        sb.append("      if (e.jsRequestActiveCount == 0) {");
067        sb.append("        e.jsRequestFinished = true;");
068        sb.append("      }");
069        sb.append("    };");
070        sb.append("    e.finished = function() {");
071        sb.append("      return e.jsRequestStarted && e.jsRequestFinished;");
072        sb.append("    };");
073        sb.append("   return e");
074        sb.append("  }();");
075        sb.append("}");
076        sb.append("  nuxeo.utils.addOnEvent(function(data) {");
077        sb.append("    if (data.status == 'begin') {window.NuxeoTestJsList.increment();}");
078        sb.append("    if (data.status == 'success') {window.NuxeoTestJsList.decrement();}");
079        sb.append("  });");
080        js.executeScript(sb.toString());
081    }
082
083    public void end() {
084        waitUntil((new Function<WebDriver, Boolean>() {
085            @Override
086            public Boolean apply(WebDriver driver) {
087                Boolean res = (Boolean) js.executeScript("return window.NuxeoTestJsList.finished();");
088                return res;
089            }
090        }));
091    }
092
093    private void waitUntil(Function<WebDriver, Boolean> function) {
094        Wait<WebDriver> wait = new FluentWait<WebDriver>(AbstractTest.driver).withTimeout(
095                AbstractTest.LOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS).pollingEvery(
096                AbstractTest.POLLING_FREQUENCY_MILLISECONDS, TimeUnit.MILLISECONDS).ignoring(
097                NoSuchElementException.class);
098        wait.until(function);
099    }
100
101}