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