<commit_before>import remoteChrome from 'chrome-remote-interface';
import browserTools from 'testcafe-browser-tools';
import { writeFile } from '../../../../utils/promisified-functions';


async function getActiveTab (cdpPort, browserId) {
    var tabs = await remoteChrome.listTabs({ port: cdpPort });
    var tab  = tabs.filter(t => t.type === 'page' && t.url.indexOf(browserId) > -1)[0];

    return tab;
}

async function setEmulationBounds ({ client, config, viewportSize, defaultScale }) {
    await client.Emulation.setDeviceMetricsOverride({
        width:             viewportSize.width,
        height:            viewportSize.height,
        deviceScaleFactor: config.scaleFactor || defaultScale,
        mobile:            config.mobile,
        fitWindow:         false
    });

    await client.Emulation.setVisibleSize({ width: viewportSize.width, height: viewportSize.height });
}

async function setEmulation (runtimeInfo) {
    var { client, config } = runtimeInfo;

    if (config.userAgent !== void 0)
        await client.Network.setUserAgentOverride({ userAgent: config.userAgent });

    if (config.touch !== void 0) {
        await client.Emulation.setTouchEmulationEnabled({
            enabled:       config.touch,
            configuration: config.mobile ? 'mobile' : 'desktop'
        });
    }

    await resizeWindow({ width: config.width, height: config.height }, runtimeInfo);
}

export async function createClient (runtimeInfo) {
    var { browserId, config, cdpPort } = runtimeInfo;

    var tab = await getActiveTab(cdpPort, browserId);

    if (!tab)
        return;

    try {
        var client = await remoteChrome({ target: tab, port: cdpPort });
    }
    catch (e) {
        return;
    }

    runtimeInfo.tab    = tab;
    runtimeInfo.client = client;

    await client.Page.enable();
    await client.Network.enable();
    await client.Runtime.enable();

    var devicePixelRatioQueryResult = await client.Runtime.evaluate({ expression: 'window.devicePixelRatio' });

    runtimeInfo.defaultScale = devicePixelRatioQueryResult.result.value;

    if (config.emulation)
        await setEmulation(runtimeInfo);
}

export function isHeadlessTab ({ tab, config }) {
    return tab && config.headless;
}

export async function closeTab ({ tab, cdpPort }) {
    await remoteChrome.closeTab({ id: tab.id, port: cdpPort });
}

export async function takeScreenshot (path, { client, config }) {
    var screenshot = await client.Page.captureScreenshot({ fromSurface: config.headless });

    await writeFile(path, screenshot.data, { encoding: 'base64' });
}

export async function resizeWindow (newDimensions, runtimeInfo) {
    var { browserId, config, viewportSize } = runtimeInfo;

    var currentWidth  = viewportSize.width;
    var currentHeight = viewportSize.height;
    var newWidth      = newDimensions.width || currentWidth;
    var newHeight     = newDimensions.height || currentHeight;


    if (!config.headless)
        await browserTools.resize(browserId, currentWidth, currentHeight, newWidth, newHeight);

    viewportSize.width  = newWidth;
    viewportSize.height = newHeight;

    if (config.emulation)
        await setEmulationBounds(runtimeInfo);
}
<commit_msg>Use new touch emulation method (closes #2268) (#2274)<commit_after>import remoteChrome from 'chrome-remote-interface';
import browserTools from 'testcafe-browser-tools';
import { writeFile } from '../../../../utils/promisified-functions';


async function getActiveTab (cdpPort, browserId) {
    var tabs = await remoteChrome.listTabs({ port: cdpPort });
    var tab  = tabs.filter(t => t.type === 'page' && t.url.indexOf(browserId) > -1)[0];

    return tab;
}

async function setEmulationBounds ({ client, config, viewportSize, defaultScale }) {
    await client.Emulation.setDeviceMetricsOverride({
        width:             viewportSize.width,
        height:            viewportSize.height,
        deviceScaleFactor: config.scaleFactor || defaultScale,
        mobile:            config.mobile,
        fitWindow:         false
    });

    await client.Emulation.setVisibleSize({ width: viewportSize.width, height: viewportSize.height });
}

async function setEmulation (runtimeInfo) {
    var { client, config } = runtimeInfo;

    if (config.userAgent !== void 0)
        await client.Network.setUserAgentOverride({ userAgent: config.userAgent });

    if (config.touch !== void 0) {
        const touchConfig = {
            enabled:       config.touch,
            configuration: config.mobile ? 'mobile' : 'desktop'
        };

        if (client.Emulation.setEmitTouchEventsForMouse)
            await client.Emulation.setEmitTouchEventsForMouse(touchConfig);
        else if (client.Emulation.setTouchEmulationEnabled)
            await client.Emulation.setTouchEmulationEnabled(touchConfig);
    }

    await resizeWindow({ width: config.width, height: config.height }, runtimeInfo);
}

export async function createClient (runtimeInfo) {
    var { browserId, config, cdpPort } = runtimeInfo;

    var tab = await getActiveTab(cdpPort, browserId);

    if (!tab)
        return;

    try {
        var client = await remoteChrome({ target: tab, port: cdpPort });
    }
    catch (e) {
        return;
    }

    runtimeInfo.tab    = tab;
    runtimeInfo.client = client;

    await client.Page.enable();
    await client.Network.enable();
    await client.Runtime.enable();

    var devicePixelRatioQueryResult = await client.Runtime.evaluate({ expression: 'window.devicePixelRatio' });

    runtimeInfo.defaultScale = devicePixelRatioQueryResult.result.value;

    if (config.emulation)
        await setEmulation(runtimeInfo);
}

export function isHeadlessTab ({ tab, config }) {
    return tab && config.headless;
}

export async function closeTab ({ tab, cdpPort }) {
    await remoteChrome.closeTab({ id: tab.id, port: cdpPort });
}

export async function takeScreenshot (path, { client, config }) {
    var screenshot = await client.Page.captureScreenshot({ fromSurface: config.headless });

    await writeFile(path, screenshot.data, { encoding: 'base64' });
}

export async function resizeWindow (newDimensions, runtimeInfo) {
    var { browserId, config, viewportSize } = runtimeInfo;

    var currentWidth  = viewportSize.width;
    var currentHeight = viewportSize.height;
    var newWidth      = newDimensions.width || currentWidth;
    var newHeight     = newDimensions.height || currentHeight;


    if (!config.headless)
        await browserTools.resize(browserId, currentWidth, currentHeight, newWidth, newHeight);

    viewportSize.width  = newWidth;
    viewportSize.height = newHeight;

    if (config.emulation)
        await setEmulationBounds(runtimeInfo);
}

{'fraction_non_alphanumeric': 0.1103499627699181, 'fraction_numerical': 0.0029784065524944155, 'mean_word_length': 3.2185929648241207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4219079', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1880, 'n_words': 464}