"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
// 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.
const chai_1 = require("chai");
const path = __importStar(require("node:path"));
const events_js_1 = require("../../conductor/events.js");
const emulation_helpers_js_1 = require("../helpers/emulation-helpers.js");
const lighthouse_helpers_js_1 = require("../helpers/lighthouse-helpers.js");
// This test will fail (by default) in headful mode, as the target page never gets painted.
// To resolve this when debugging, just make sure the target page is visible during the lighthouse run.
const IPAD_MINI_LANDSCAPE_VIEWPORT_DIMENSIONS = {
    innerHeight: 768,
    innerWidth: 1024,
    outerHeight: 768,
    outerWidth: 1024,
    devicePixelRatio: 2,
};
function expectErrors() {
    // https://github.com/GoogleChrome/lighthouse/issues/14572
    (0, events_js_1.expectError)(/Request CacheStorage\.requestCacheNames failed/);
    // https://bugs.chromium.org/p/chromium/issues/detail?id=1357791
    (0, events_js_1.expectError)(/Protocol Error: the message with wrong session id/);
    (0, events_js_1.expectError)(/Protocol Error: the message with wrong session id/);
    (0, events_js_1.expectError)(/Protocol Error: the message with wrong session id/);
    (0, events_js_1.expectError)(/Protocol Error: the message with wrong session id/);
    (0, events_js_1.expectError)(/Protocol Error: the message with wrong session id/);
}
/**
 * Start blocking *.css
 * Ideally this would be done with UI manipulation, but it'd be less reliable AND
 * the designated tests in network-request-blocking-panel.test.ts are skipped by default due to flakiness.
 **/
async function blockCss(devToolsPage) {
    await devToolsPage.evaluate(async () => {
        // @ts-expect-error executed from DevTools
        const SDKModule = await import('./core/sdk/sdk.js');
        const networkManager = SDKModule.NetworkManager.MultitargetNetworkManager.instance();
        networkManager.requestConditions.conditionsEnabled = true;
        networkManager.requestConditions.add(SDKModule.NetworkManager.RequestCondition.createFromSetting({ enabled: true, url: '*.css' }));
    });
}
describe('DevTools', function () {
    // The tests in this suite are particularly slow
    this.timeout(60_000);
    describe('request blocking', () => {
        // https://crbug.com/466057104 the feature roll has make this test fail
        it.skip('[crbug.com/466057104] is respected during a lighthouse run', async ({ devToolsPage, inspectedPage }) => {
            expectErrors();
            await blockCss(devToolsPage);
            await (0, lighthouse_helpers_js_1.navigateToLighthouseTab)('lighthouse/hello.html', devToolsPage, inspectedPage);
            await (0, lighthouse_helpers_js_1.selectCategories)(['performance'], devToolsPage);
            await (0, lighthouse_helpers_js_1.clickStartButton)(devToolsPage);
            const { lhr } = await (0, lighthouse_helpers_js_1.waitForResult)(devToolsPage, inspectedPage);
            const requests = lhr.audits['network-requests'].details.items;
            const trimmedRequests = requests.map((item) => {
                return {
                    url: typeof item.url === 'string' && path.basename(item.url),
                    statusCode: item.statusCode,
                };
            });
            // An extra basic.css request with status code -1 appears, but only in e2e tests
            // This test is made more lenient since this only happens in the e2e environment
            // b/359984292
            chai_1.assert.deepEqual(trimmedRequests[0], { url: 'hello.html', statusCode: 200 });
            chai_1.assert.deepEqual(trimmedRequests[1], { url: 'basic.css', statusCode: -1 });
        });
    });
    describe('device emulation', () => {
        it('is restored after a lighthouse run', async ({ devToolsPage, inspectedPage }) => {
            await (0, emulation_helpers_js_1.openDeviceToolbar)(devToolsPage, inspectedPage);
            // Use iPad Mini in landscape mode and custom zoom.
            await (0, emulation_helpers_js_1.selectDevice)('iPad Mini', devToolsPage);
            const rotateButton = await devToolsPage.waitForAria('Rotate');
            await rotateButton.click();
            await (0, emulation_helpers_js_1.selectZoomLevel)(devToolsPage, '75%');
            chai_1.assert.deepEqual(await (0, lighthouse_helpers_js_1.getTargetViewport)(inspectedPage), IPAD_MINI_LANDSCAPE_VIEWPORT_DIMENSIONS);
            await (0, lighthouse_helpers_js_1.navigateToLighthouseTab)('lighthouse/hello.html', devToolsPage, inspectedPage);
            await (0, lighthouse_helpers_js_1.selectCategories)(['performance', 'best-practices'], devToolsPage);
            await (0, lighthouse_helpers_js_1.clickStartButton)(devToolsPage);
            const { artifacts } = await (0, lighthouse_helpers_js_1.waitForResult)(devToolsPage, inspectedPage);
            chai_1.assert.deepEqual(artifacts.ViewportDimensions, {
                innerHeight: 823,
                innerWidth: 412,
                outerHeight: 823,
                outerWidth: 412,
                devicePixelRatio: 1.75,
            });
            const zoomText = await (0, emulation_helpers_js_1.getZoom)(devToolsPage);
            chai_1.assert.strictEqual(zoomText, '75%');
            chai_1.assert.deepEqual(await (0, lighthouse_helpers_js_1.getTargetViewport)(inspectedPage), IPAD_MINI_LANDSCAPE_VIEWPORT_DIMENSIONS);
        });
    });
});
//# sourceMappingURL=devtools-settings.test.js.map