"use strict";
// Copyright 2021 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 extension_helpers_js_1 = require("../helpers/extension-helpers.js");
const sources_helpers_js_1 = require("../helpers/sources-helpers.js");
describe('The Extension API', () => {
    it('can open wasm resources with offset', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.waitForSourceFiles)("source-tree-file-added" /* SourceFileEvents.ADDED_TO_SOURCE_TREE */, files => files.some(f => f.endsWith('scopes.wasm')), () => (0, sources_helpers_js_1.openFileInSourcesPanel)('wasm/scopes.html', devToolsPage, inspectedPage), devToolsPage);
        const extension = await (0, extension_helpers_js_1.loadExtension)('TestExtension', undefined, undefined, devToolsPage, inspectedPage);
        const resource = `${inspectedPage.getResourcesPath()}/sources/wasm/scopes.wasm`;
        await extension.waitForFunction(async (resource) => {
            const resources = await new Promise(r => window.chrome.devtools.inspectedWindow.getResources(r));
            return resources.find(r => r.url === resource);
        }, undefined, resource);
        // Accepts a wasm offset as column
        await extension.evaluate(resource => window.chrome.devtools.panels.openResource(resource, 0, 0x4b), resource);
        await (0, sources_helpers_js_1.waitForHighlightedLine)(0x4b, devToolsPage);
        // Selects the right wasm line on an inexact match
        await extension.evaluate(resource => window.chrome.devtools.panels.openResource(resource, 0, 0x4e), resource);
        await (0, sources_helpers_js_1.waitForHighlightedLine)(0x4d, devToolsPage);
        // Accepts a missing columnNumber
        await extension.evaluate(resource => window.chrome.devtools.panels.openResource(resource, 0), resource);
        await (0, sources_helpers_js_1.waitForHighlightedLine)(0, devToolsPage);
        // Accepts a wasm offset as column and a callback
        {
            const r = await extension.evaluate(resource => new Promise(r => window.chrome.devtools.panels.openResource(resource, 0, 0x4b, () => r(1))), resource);
            chai_1.assert.deepEqual(r, 1);
        }
        await (0, sources_helpers_js_1.waitForHighlightedLine)(0x4b, devToolsPage);
        // Accepts a callback with an explicitly undefined columnNumber
        {
            const r = await extension.evaluate(resource => new Promise(r => window.chrome.devtools.panels.openResource(resource, /* lineNumber */ 0, /* columnNumber */ undefined, /* callback */ () => r(1))), resource);
            chai_1.assert.deepEqual(r, 1);
        }
        await (0, sources_helpers_js_1.waitForHighlightedLine)(0, devToolsPage);
        // Is backwards compatible for JavaScript callers: accepts a callback with a missing `columnNumber`
        {
            const r = await extension.evaluate(resource => new Promise(r => {
                // Cast to `any` is required to bypass TypeScript compiler errors.
                // We are verifying the legacy runtime behavior for JavaScript callers
                // who skip the `columnNumber` argument, which is no longer statically
                // allowed in TypeScript.
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                window.chrome.devtools.panels
                    .openResource(resource, /* lineNumber */ 0, /* callback */ () => r(1));
            }), resource);
            chai_1.assert.deepEqual(r, 1);
        }
        await (0, sources_helpers_js_1.waitForHighlightedLine)(0, devToolsPage);
    });
    it('can open page resources with column numbers', async ({ devToolsPage, inspectedPage }) => {
        const resource = `${inspectedPage.getResourcesPath()}/sources/wasm/scopes.html`;
        await (0, sources_helpers_js_1.waitForSourceFiles)("source-tree-file-added" /* SourceFileEvents.ADDED_TO_SOURCE_TREE */, files => files.some(f => f.endsWith('scopes.wasm')), () => (0, sources_helpers_js_1.openFileInSourcesPanel)('wasm/scopes.html', devToolsPage, inspectedPage), devToolsPage);
        const extension = await (0, extension_helpers_js_1.loadExtension)('TestExtension', undefined, undefined, devToolsPage, inspectedPage);
        await extension.waitForFunction(async (resource) => {
            const resources = await new Promise(r => window.chrome.devtools.inspectedWindow.getResources(r));
            return resources.find(r => r.url === resource);
        }, undefined, resource);
        // Accepts a missing columnNumber
        await extension.evaluate(resource => window.chrome.devtools.panels.openResource(resource, 2), resource);
        await (0, sources_helpers_js_1.waitForHighlightedLine)(3, devToolsPage);
        // Accepts a column number
        {
            await extension.evaluate(resource => window.chrome.devtools.panels.openResource(resource, 29, 160), resource);
            await (0, sources_helpers_js_1.waitForHighlightedLine)(30, devToolsPage);
            const toolbarText = await (0, sources_helpers_js_1.getToolbarText)(devToolsPage);
            chai_1.assert.isTrue(toolbarText.includes('Line 30, Column 161'));
        }
        // Accepts a column number and a callback
        {
            const r = await extension.evaluate(resource => new Promise(r => window.chrome.devtools.panels.openResource(resource, 1, 2000, () => r(1))), resource);
            chai_1.assert.deepEqual(r, 1);
            await (0, sources_helpers_js_1.waitForHighlightedLine)(2, devToolsPage);
            const toolbarText = await (0, sources_helpers_js_1.getToolbarText)(devToolsPage);
            // Column 38 is the last column in the 2nd line.
            chai_1.assert.isTrue(toolbarText.includes('Line 2, Column 38'));
        }
        // Accepts a callback with an explicitly undefined columnNumber
        {
            const r = await extension.evaluate(resource => new Promise(r => window.chrome.devtools.panels.openResource(resource, /* lineNumber */ 2, /* columnNumber */ undefined, /* callback */ () => r(1))), resource);
            chai_1.assert.deepEqual(r, 1);
            await (0, sources_helpers_js_1.waitForHighlightedLine)(3, devToolsPage);
        }
        // Is backwards compatible for JavaScript callers: accepts a callback with a missing `columnNumber`
        {
            const r = await extension.evaluate(resource => new Promise(r => {
                // Cast to `any` is required to bypass TypeScript compiler errors.
                // We are verifying the legacy runtime behavior for JavaScript callers
                // who skip the `columnNumber` argument, which is no longer statically
                // allowed in TypeScript.
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                window.chrome.devtools.panels
                    .openResource(resource, /* lineNumber */ 2, /* callback */ () => r(1));
            }), resource);
            chai_1.assert.deepEqual(r, 1);
            await (0, sources_helpers_js_1.waitForHighlightedLine)(3, devToolsPage);
        }
    });
});
//# sourceMappingURL=can-open-resources-with-columns.test.js.map