<commit_before>/*
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jitsi.meet.test.base;

import org.jitsi.meet.test.util.*;
import org.openqa.selenium.*;
import org.testng.annotations.*;

import java.util.*;
import java.util.concurrent.*;

/**
 * The participant instance holding the {@link WebDriver}.
 * @param <T> the driver type.
 */
public abstract class Participant<T extends WebDriver>
    implements JavascriptExecutor
{
    /**
     * The driver.
     */
    private final T driver;

    /**
     * The participant type chrome, firefox etc.
     */
    private final ParticipantFactory.ParticipantType type;

    /**
     * The name of the participant.
     */
    private final String name;

    /**
     * The url to join conferences.
     */
    private final String meetURL;

    /**
     * Is hung up.
     */
    boolean hungUp = true;

    /**
     * We store the room name we joined as if someone calls joinConference twice
     * to be able to detect that there is no need to load anything.
     */
    String joinedRoomName = null;

    /**
     * Executor that is responsible for keeping the participant session alive.
     */
    private ScheduledExecutorService executor
        = Executors.newScheduledThreadPool(1);

    /**
     * The execution of the keepalive, if it is null this means
     * it is not started.
     */
    private ScheduledFuture<?> keepAliveExecution = null;

    /**
     * The seconds between calls that will make sure we keep alive the
     * webdriver session.
     */
    private static final int KEEP_ALIVE_SESSION_INTERVAL = 20;

    /**
     * Constructs a Participant.
     * @param name the name.
     * @param driver its driver instance.
     * @param type the type (type of browser).
     * @param meetURL the url to use when joining room.
     */
    public Participant(
        String name,
        T driver,
        ParticipantFactory.ParticipantType type,
        String meetURL)
    {
        this.name = name;
        this.driver = Objects.requireNonNull(driver, "driver");
        this.type = type;
        this.meetURL = meetURL;
    }

    /**
     * Joins a conference.
     * @param roomName the room name to join.
     */
    public void joinConference(String roomName)
    {
        this.joinConference(roomName, null);
    }

    /**
     * Joins a conference.
     * @param roomName the room name to join.
     * @param fragment adds the given string to the fragment part of the URL.
     */
    public void joinConference(String roomName, String fragment)
    {
        // not hungup, so not joining
        if (!this.hungUp
            && this.joinedRoomName != null
            && this.joinedRoomName.equals(roomName))
        {
            TestUtils.print("Not joining " + this.name + " in " + roomName
                + ", already joined.");
            return;
        }

        this.joinedRoomName = roomName;

        String URL = this.meetURL + "/" + roomName;
        URL += "#config.requireDisplayName=false";
        URL += "&config.debug=true";
        URL += "&config.disableAEC=true";
        URL += "&config.disableNS=true";
        URL += "&config.callStatsID=false";
        URL += "&config.alwaysVisibleToolbar=true";
        URL += "&config.p2p.enabled=false";
        URL += "&config.disable1On1Mode=true";

        if (fragment != null)
            URL += "&" + fragment;

        TestUtils.print(name + " is opening URL: " + URL);

        {
            // with chrome v52 we start getting error:
            // "Timed out receiving message from renderer" and
            // "Navigate timeout: cannot determine loading status"
            // seems its a bug or rare problem, maybe concerns async loading
            // of resources ...
            // https://bugs.chromium.org/p/chromedriver/issues/detail?id=402
            // even there is a TimeoutException the page is loaded correctly
            // and driver is operating, we just lower the page load timeout
            // default is 3 minutes and we log and skip this exception
            driver.manage().timeouts()
                .pageLoadTimeout(30, TimeUnit.SECONDS);
            try
            {
                driver.get(URL);
            }
            catch (org.openqa.selenium.TimeoutException ex)
            {
                ex.printStackTrace();
                TestUtils.print("TimeoutException while loading page, "
                    + "will skip it and continue:" + ex.getMessage());
            }
        }
        MeetUtils.waitForPageToLoad(driver);

        // disables animations
        executeScript("try { jQuery.fx.off = true; } catch(e) {}");

        executeScript("APP.UI.dockToolbar(true);");

        // disable keyframe animations (.fadeIn and .fadeOut classes)
        executeScript("$('<style>.notransition * { "
                + "animation-duration: 0s !important; "
                + "-webkit-animation-duration: 0s !important; } </style>')"
                + ".appendTo(document.head);");
        executeScript("$('body').toggleClass('notransition');");

        // Hack-in disabling of callstats (old versions of jitsi-meet don't
        // handle URL parameters)
        executeScript("config.callStatsID=false;");

        String version
            = TestUtils.executeScriptAndReturnString(driver,
                "return JitsiMeetJS.version;");
        TestUtils.print(name + " lib-jitsi-meet version: " + version);

        executeScript("document.title='" + name + "'");

        this.hungUp = false;

        startKeepAliveExecution();
    }

    /**
     * Starts the keep-alive execution.
     */
    public void startKeepAliveExecution()
    {
        if (this.keepAliveExecution == null)
        {
            this.keepAliveExecution = this.executor
                .scheduleAtFixedRate(
                    () -> driver.getCurrentUrl(),<commit_msg>Disables blur effect for FF.<commit_after>/*
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jitsi.meet.test.base;

import org.jitsi.meet.test.util.*;
import org.openqa.selenium.*;
import org.testng.annotations.*;

import java.util.*;
import java.util.concurrent.*;

/**
 * The participant instance holding the {@link WebDriver}.
 * @param <T> the driver type.
 */
public abstract class Participant<T extends WebDriver>
    implements JavascriptExecutor
{
    /**
     * The driver.
     */
    private final T driver;

    /**
     * The participant type chrome, firefox etc.
     */
    private final ParticipantFactory.ParticipantType type;

    /**
     * The name of the participant.
     */
    private final String name;

    /**
     * The url to join conferences.
     */
    private final String meetURL;

    /**
     * Is hung up.
     */
    boolean hungUp = true;

    /**
     * We store the room name we joined as if someone calls joinConference twice
     * to be able to detect that there is no need to load anything.
     */
    String joinedRoomName = null;

    /**
     * Executor that is responsible for keeping the participant session alive.
     */
    private ScheduledExecutorService executor
        = Executors.newScheduledThreadPool(1);

    /**
     * The execution of the keepalive, if it is null this means
     * it is not started.
     */
    private ScheduledFuture<?> keepAliveExecution = null;

    /**
     * The seconds between calls that will make sure we keep alive the
     * webdriver session.
     */
    private static final int KEEP_ALIVE_SESSION_INTERVAL = 20;

    /**
     * Constructs a Participant.
     * @param name the name.
     * @param driver its driver instance.
     * @param type the type (type of browser).
     * @param meetURL the url to use when joining room.
     */
    public Participant(
        String name,
        T driver,
        ParticipantFactory.ParticipantType type,
        String meetURL)
    {
        this.name = name;
        this.driver = Objects.requireNonNull(driver, "driver");
        this.type = type;
        this.meetURL = meetURL;
    }

    /**
     * Joins a conference.
     * @param roomName the room name to join.
     */
    public void joinConference(String roomName)
    {
        this.joinConference(roomName, null);
    }

    /**
     * Joins a conference.
     * @param roomName the room name to join.
     * @param fragment adds the given string to the fragment part of the URL.
     */
    public void joinConference(String roomName, String fragment)
    {
        // not hungup, so not joining
        if (!this.hungUp
            && this.joinedRoomName != null
            && this.joinedRoomName.equals(roomName))
        {
            TestUtils.print("Not joining " + this.name + " in " + roomName
                + ", already joined.");
            return;
        }

        this.joinedRoomName = roomName;

        String URL = this.meetURL + "/" + roomName;
        URL += "#config.requireDisplayName=false";
        URL += "&config.debug=true";
        URL += "&config.disableAEC=true";
        URL += "&config.disableNS=true";
        URL += "&config.callStatsID=false";
        URL += "&config.alwaysVisibleToolbar=true";
        URL += "&config.p2p.enabled=false";
        URL += "&config.disable1On1Mode=true";

        if (fragment != null)
            URL += "&" + fragment;

        TestUtils.print(name + " is opening URL: " + URL);

        {
            // with chrome v52 we start getting error:
            // "Timed out receiving message from renderer" and
            // "Navigate timeout: cannot determine loading status"
            // seems its a bug or rare problem, maybe concerns async loading
            // of resources ...
            // https://bugs.chromium.org/p/chromedriver/issues/detail?id=402
            // even there is a TimeoutException the page is loaded correctly
            // and driver is operating, we just lower the page load timeout
            // default is 3 minutes and we log and skip this exception
            driver.manage().timeouts()
                .pageLoadTimeout(30, TimeUnit.SECONDS);
            try
            {
                driver.get(URL);
            }
            catch (org.openqa.selenium.TimeoutException ex)
            {
                ex.printStackTrace();
                TestUtils.print("TimeoutException while loading page, "
                    + "will skip it and continue:" + ex.getMessage());
            }
        }
        MeetUtils.waitForPageToLoad(driver);

        // disables animations
        executeScript("try { jQuery.fx.off = true; } catch(e) {}");

        executeScript("APP.UI.dockToolbar(true);");

        // disable keyframe animations (.fadeIn and .fadeOut classes)
        executeScript("$('<style>.notransition * { "
                + "animation-duration: 0s !important; "
                + "-webkit-animation-duration: 0s !important; } </style>')"
                + ".appendTo(document.head);");
        executeScript("$('body').toggleClass('notransition');");

        // disable the blur effect in firefox as it has some performance issues
        if (this.type == ParticipantFactory.ParticipantType.firefox)
        {
            executeScript(
                "try { var blur "
                    + "= document.querySelector('.video_blurred_container'); "
                    + "if (blur) { "
                    + "document.querySelector('.video_blurred_container')"
                    + ".style.display = 'none' "
                    + "} } catch(e) {}");
        }

        // Hack-in disabling of callstats (old versions of jitsi-meet don't
        // handle URL parameters)
        executeScript("config.callStatsID=false;");

        String version
            = TestUtils.executeScriptAndReturnString(driver,
                "return JitsiMeetJS.version;");
        TestUtils.print(name + " lib-jitsi-meet version: " + version);

        executeScript("document.title='" + name + "'");

        this.hungUp = false;

        startKeepAliveExecution();
    }

    /**
     * Starts the keep-alive execution.
     */
    public void startKeepAliveExecution()
    {
        if (this.keepAliveExecution == null)
        {
            this.keepAliveExecution = this.executor
                .scheduleAtFixedRate(
                    () -> driver.getCurrentUrl(),
{'fraction_non_alphanumeric': 0.1047554957379991, 'fraction_numerical': 0.003589053387169134, 'mean_word_length': 2.0287590579710146, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5932864', 'n_tokens_mistral': 3669, 'n_tokens_neox': 3499, 'n_words': 1093}