"use strict";
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const context_menu_helpers_js_1 = require("../helpers/context-menu-helpers.js");
const network_helpers_js_1 = require("../helpers/network-helpers.js");
const sources_helpers_js_1 = require("../helpers/sources-helpers.js");
const ENABLE_OVERRIDES_SELECTOR = '[aria-label="Select folder for overrides"]';
const OVERRIDES_FILESYSTEM_SELECTOR = '[aria-label="overrides, fs"]';
const FILE_TREE_HEADERS_FILE_SELECTOR = '[aria-label=".headers, file"] .tree-element-title';
const NETWORK_VIEW_SELECTOR = '.network-item-view';
const HEADERS_TAB_SELECTOR = '[aria-label=Headers].tabbed-pane-header-tab';
const ACTIVE_HEADERS_TAB_SELECTOR = '[aria-label=Headers].tabbed-pane-header-tab[aria-selected=true]';
const RESPONSE_HEADERS_SELECTOR = '[aria-label="Response headers"]';
const HEADER_ROW_SELECTOR = '.row';
async function createHeaderOverride(devToolsPage) {
    await (0, context_menu_helpers_js_1.openSoftContextMenuAndClickOnItem)(OVERRIDES_FILESYSTEM_SELECTOR, 'New file', devToolsPage);
    await devToolsPage.waitFor('.being-edited');
    await devToolsPage.typeText('.headers\n');
    await devToolsPage.click('.add-block');
    await devToolsPage.waitFor('.editable.apply-to');
    await devToolsPage.typeText('*.html\n');
    await devToolsPage.typeText('aaa\n');
    await devToolsPage.typeText('bbb');
    const title = await devToolsPage.waitFor(FILE_TREE_HEADERS_FILE_SELECTOR);
    let labelText = await title?.evaluate(el => el.textContent);
    chai_1.assert.strictEqual(labelText, '*.headers');
    await devToolsPage.page.keyboard.press('Tab');
    await devToolsPage.waitForFunction(async () => {
        labelText = await title?.evaluate(el => el.textContent);
        return labelText === '.headers';
    });
}
async function openHeadersTab(devToolsPage) {
    const networkView = await devToolsPage.waitFor(NETWORK_VIEW_SELECTOR);
    await devToolsPage.click(HEADERS_TAB_SELECTOR, {
        root: networkView,
    });
    await devToolsPage.waitFor(ACTIVE_HEADERS_TAB_SELECTOR, networkView);
}
async function editorTabHasPurpleDot(devToolsPage) {
    const tabHeaderIcon = await devToolsPage.waitFor('[aria-label=".headers"] devtools-icon');
    return await tabHeaderIcon?.evaluate(node => node.classList.contains('dot') && node.classList.contains('purple'));
}
async function fileTreeEntryIsSelectedAndHasPurpleDot(devToolsPage) {
    const element = await devToolsPage.activeElement();
    const title = await element.evaluate(e => e.getAttribute('title')) || '';
    chai_1.assert.match(title, /\/test\/e2e\/resources\/network\/\.headers$/);
    const fileTreeIcon = await element.waitForSelector('.navigator-file-tree-item .leading-icons devtools-file-source-icon >>> devtools-icon');
    if (!fileTreeIcon) {
        return false;
    }
    return await fileTreeIcon.evaluate(node => node.classList.contains('dot') && node.classList.contains('purple'));
}
async function editHeaderItem(devToolsPage, newValue, previousValue) {
    let focusedTextContent = await devToolsPage.activeElementTextContent();
    chai_1.assert.strictEqual(focusedTextContent, previousValue);
    const element = await devToolsPage.activeElement();
    await element.evaluate((e, value) => {
        e.textContent = value;
    }, newValue);
    focusedTextContent = await devToolsPage.activeElementTextContent();
    chai_1.assert.strictEqual(focusedTextContent, newValue);
    await devToolsPage.page.keyboard.press('Tab');
}
async function cleanup(devToolsPage) {
    await (0, sources_helpers_js_1.openSourcesPanel)(devToolsPage);
    await (0, sources_helpers_js_1.openOverridesSubPane)(devToolsPage);
    await devToolsPage.click('[aria-label="Clear configuration"]');
    await devToolsPage.waitFor(ENABLE_OVERRIDES_SELECTOR);
}
describe('The Overrides Panel', function () {
    it('can create header overrides', async ({ devToolsPage, inspectedPage }) => {
        await devToolsPage.setupOverridesFSMocks();
        await devToolsPage.useSoftMenu();
        await inspectedPage.goToResource('empty.html');
        await (0, sources_helpers_js_1.openSourcesPanel)(devToolsPage);
        await (0, sources_helpers_js_1.enableLocalOverrides)(devToolsPage);
        await createHeaderOverride(devToolsPage);
        await devToolsPage.click('#tab-network');
        await devToolsPage.waitFor('.network-log-grid');
        await inspectedPage.goToResource('network/hello.html');
        await (0, network_helpers_js_1.waitForSomeRequestsToAppear)(1, devToolsPage);
        await (0, network_helpers_js_1.selectRequestByName)('hello.html', {}, devToolsPage);
        await openHeadersTab(devToolsPage);
        const responseHeaderSection = await devToolsPage.waitFor(RESPONSE_HEADERS_SELECTOR);
        const row = await devToolsPage.waitFor(HEADER_ROW_SELECTOR, responseHeaderSection);
        chai_1.assert.isOk(row);
        chai_1.assert.deepEqual(await (0, network_helpers_js_1.getTextFromHeadersRow)(row, devToolsPage), ['aaa', 'bbb']);
        await cleanup(devToolsPage);
    });
    it('can override headers via network panel', async ({ devToolsPage, inspectedPage }) => {
        await devToolsPage.setupOverridesFSMocks();
        await devToolsPage.useSoftMenu();
        await devToolsPage.page.emulateMediaFeatures([
            { name: 'prefers-reduced-motion', value: 'reduce' },
        ]);
        await (0, network_helpers_js_1.navigateToNetworkTab)('hello.html', devToolsPage, inspectedPage);
        await (0, network_helpers_js_1.waitForSomeRequestsToAppear)(1, devToolsPage);
        await (0, network_helpers_js_1.selectRequestByName)('hello.html', {}, devToolsPage);
        await openHeadersTab(devToolsPage);
        await devToolsPage.click('.enable-editing');
        await (0, network_helpers_js_1.clickInfobarButton)(devToolsPage);
        await devToolsPage.waitFor('.add-header-button');
        await devToolsPage.click('.add-header-button');
        await devToolsPage.waitFor('.row.header-overridden.header-editable');
        await devToolsPage.click('.header-name devtools-editable-span');
        await editHeaderItem(devToolsPage, 'foo', 'header-name');
        await editHeaderItem(devToolsPage, 'bar', 'header value');
        await devToolsPage.waitFor('[title="Refresh the page/request for these changes to take effect"]');
        await devToolsPage.click('[title="Reveal header override definitions"]');
        chai_1.assert.isTrue(await editorTabHasPurpleDot(devToolsPage));
        chai_1.assert.isTrue(await fileTreeEntryIsSelectedAndHasPurpleDot(devToolsPage));
        await (0, network_helpers_js_1.navigateToNetworkTab)('hello.html', devToolsPage, inspectedPage);
        await (0, network_helpers_js_1.waitForSomeRequestsToAppear)(1, devToolsPage);
        await (0, network_helpers_js_1.selectRequestByName)('hello.html', {}, devToolsPage);
        await openHeadersTab(devToolsPage);
        const responseHeaderSection = await devToolsPage.waitFor(RESPONSE_HEADERS_SELECTOR);
        const row = await devToolsPage.waitFor('.row.header-overridden.header-editable', responseHeaderSection);
        chai_1.assert.isOk(row);
        chai_1.assert.deepEqual(await (0, network_helpers_js_1.getTextFromHeadersRow)(row, devToolsPage), ['foo', 'bar']);
        await devToolsPage.click('[title="Reveal header override definitions"]');
        chai_1.assert.isTrue(await editorTabHasPurpleDot(devToolsPage));
        chai_1.assert.isTrue(await fileTreeEntryIsSelectedAndHasPurpleDot(devToolsPage));
        await inspectedPage.goToResource('pages/hello-world.html');
        await devToolsPage.waitForFunction(async () => {
            return (await editorTabHasPurpleDot(devToolsPage)) === false &&
                (await fileTreeEntryIsSelectedAndHasPurpleDot(devToolsPage)) === false;
        });
        await cleanup(devToolsPage);
    });
});
//# sourceMappingURL=header-overrides.test.js.map