"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 helper_js_1 = require("../../shared/helper.js");
const network_helpers_js_1 = require("../helpers/network-helpers.js");
const quick_open_helpers_js_1 = require("../helpers/quick_open-helpers.js");
const settings_helpers_js_1 = require("../helpers/settings-helpers.js");
const sources_helpers_js_1 = require("../helpers/sources-helpers.js");
const PRETTY_PRINT_BUTTON = '[aria-label="Pretty print"]';
const PRETTY_PRINTED_TOGGLE = 'devtools-text-editor.pretty-printed';
describe('The Sources Tab', function () {
    it('can pretty-print a JavaScript file inline', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('minified-sourcecode.js', 'minified-sourcecode.html', devToolsPage, inspectedPage);
        await (0, helper_js_1.step)('can pretty-print successfully', async () => {
            await devToolsPage.click(PRETTY_PRINT_BUTTON);
            await devToolsPage.waitFor(PRETTY_PRINTED_TOGGLE);
            const expectedLines = [
                '// Copyright 2020 The Chromium Authors',
                '// Use of this source code is governed by a BSD-style license that can be',
                '// found in the LICENSE file.',
                '// clang-format off',
                'const notFormatted = {',
                '    something: \'not-formatted\'',
                '};',
                'console.log(\'Test for correct line number\');',
                'function notFormattedFunction() {',
                '    console.log(\'second log\');',
                '    return {',
                '        field: 2 + 4',
                '    }',
                '}',
                ';notFormattedFunction();',
                '',
            ];
            const updatedTextContent = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
            chai_1.assert.strictEqual(updatedTextContent.join('\n'), expectedLines.join('\n'));
        });
        await (0, helper_js_1.step)('can un-pretty-print successfully', async () => {
            await devToolsPage.click(PRETTY_PRINT_BUTTON);
            await devToolsPage.waitForNone(PRETTY_PRINTED_TOGGLE);
            const expectedLines = [
                '// Copyright 2020 The Chromium Authors',
                '// Use of this source code is governed by a BSD-style license that can be',
                '// found in the LICENSE file.',
                '// clang-format off',
                'const notFormatted = {something: \'not-formatted\'};console.log(\'Test for correct line number\'); function notFormattedFunction() {',
                'console.log(\'second log\'); return {field: 2+4}};',
                'notFormattedFunction();',
                '',
            ];
            const updatedTextContent = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
            chai_1.assert.strictEqual(updatedTextContent.join('\n'), expectedLines.join('\n'));
        });
    });
    it('can toggle pretty print on a small non-minified JSON file', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('small-json.rawresponse', '../network/small-json.rawresponse', devToolsPage, inspectedPage);
        // Small files (< 80 chars avg line length) are not auto-formatted
        await devToolsPage.waitForFunction(async () => !(await (0, sources_helpers_js_1.isPrettyPrinted)(devToolsPage)));
        const content = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
        // Should be original compact format on one line
        chai_1.assert.strictEqual(content.join(''), '{"a":{"b":{"c":1}}}');
        // Pretty-print small JSON by clicking toggle
        await devToolsPage.click(PRETTY_PRINT_BUTTON);
        await devToolsPage.waitFor(PRETTY_PRINTED_TOGGLE);
        const expectedPrettyLines = [
            '{',
            '    "a": {',
            '        "b": {',
            '            "c": 1',
            '        }',
            '    }',
            '}',
        ];
        const actualContent = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
        chai_1.assert.deepEqual(actualContent, expectedPrettyLines);
        // Un-pretty-print by clicking toggle again
        await devToolsPage.click(PRETTY_PRINT_BUTTON);
        await devToolsPage.waitForNone(PRETTY_PRINTED_TOGGLE);
        const contentAfterUnpretty = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
        chai_1.assert.strictEqual(contentAfterUnpretty.join(''), '{"a":{"b":{"c":1}}}');
    });
    it('can pretty print an inline json subtype file', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('json-subtype-ld.rawresponse', '../network/json-subtype-ld.rawresponse', devToolsPage, inspectedPage);
        const editor = await devToolsPage.waitFor('[aria-label="Code editor"]');
        await (0, helper_js_1.step)('can pretty-print a json subtype', async () => {
            const expectedPrettyLines = [
                '{',
                '    "Keys": [',
                '        {',
                '            "Key1": "Value1",',
                '            "Key2": "Value2",',
                '            "Key3": true',
                '        },',
                '        {',
                '            "Key1": "Value1",',
                '            "Key2": "Value2",',
                '            "Key3": false',
                '        }',
                '    ]',
                '}',
            ];
            const actualPrettyText = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
            chai_1.assert.deepEqual(expectedPrettyLines, actualPrettyText);
        });
        await (0, helper_js_1.step)('can highlight the pretty-printed text', async () => {
            await devToolsPage.waitForFunction(() => (0, sources_helpers_js_1.isPrettyPrinted)(devToolsPage));
            chai_1.assert.isTrue(await (0, network_helpers_js_1.elementContainsTextWithSelector)(editor, '"Value1"', '.token-string'));
            chai_1.assert.isTrue(await (0, network_helpers_js_1.elementContainsTextWithSelector)(editor, 'true', '.token-atom'));
        });
        await (0, helper_js_1.step)('can un-pretty-print a json subtype file', async () => {
            await devToolsPage.click(PRETTY_PRINT_BUTTON);
            const expectedNotPrettyLines = '{"Keys": [{"Key1": "Value1","Key2": "Value2","Key3": true},{"Key1": "Value1","Key2": "Value2","Key3": false}]},';
            const actualNotPrettyText = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
            chai_1.assert.strictEqual(expectedNotPrettyLines, actualNotPrettyText.toString());
        });
        await (0, helper_js_1.step)('can highlight the un-pretty-printed text', async () => {
            await devToolsPage.waitForFunction(async () => !(await (0, sources_helpers_js_1.isPrettyPrinted)(devToolsPage)));
            chai_1.assert.isTrue(await (0, network_helpers_js_1.elementContainsTextWithSelector)(editor, '"Value1"', '.token-string'));
            chai_1.assert.isTrue(await (0, network_helpers_js_1.elementContainsTextWithSelector)(editor, 'true', '.token-atom'));
        });
    });
    it('can show error icons for pretty-printed file', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('minified-errors.js', 'minified-errors.html', devToolsPage, inspectedPage);
        await (0, helper_js_1.step)('shows 3 separate errors when pretty-printed', async () => {
            await devToolsPage.click(PRETTY_PRINT_BUTTON);
            await devToolsPage.waitFor(PRETTY_PRINTED_TOGGLE);
            await devToolsPage.waitForFunction(async () => {
                const icons = await devToolsPage.$$('devtools-icon.cm-messageIcon-error');
                return icons.length === 3;
            });
        });
        await (0, helper_js_1.step)('shows 2 separate errors when un-pretty-printed', async () => {
            await devToolsPage.click(PRETTY_PRINT_BUTTON);
            await devToolsPage.waitForNone(PRETTY_PRINTED_TOGGLE);
            await devToolsPage.waitForFunction(async () => {
                const icons = await devToolsPage.$$('devtools-icon.cm-messageIcon-error');
                return icons.length === 2;
            });
        });
    });
    it('can add breakpoint for pretty-printed file', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('minified-sourcecode.js', 'minified-sourcecode.html', devToolsPage, inspectedPage);
        await devToolsPage.click(PRETTY_PRINT_BUTTON);
        await devToolsPage.waitFor(PRETTY_PRINTED_TOGGLE);
        // Set a breakpoint in line 6 of the pretty-printed view (which is the
        // line with the label "6" not the 6th line from the top).
        await (0, sources_helpers_js_1.addBreakpointForLine)(6, devToolsPage);
        const scriptLocation = await (0, sources_helpers_js_1.retrieveTopCallFrameScriptLocation)('notFormattedFunction();', inspectedPage.page, devToolsPage);
        chai_1.assert.deepEqual(scriptLocation, 'minified-sourcecode.js:6');
    });
    it('can add breakpoint on minified source and then break correctly on pretty-printed source', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('minified-sourcecode.js', 'minified-sourcecode.html', devToolsPage, inspectedPage);
        await (0, sources_helpers_js_1.addBreakpointForLine)(6, devToolsPage);
        await devToolsPage.click(PRETTY_PRINT_BUTTON);
        await devToolsPage.waitFor(PRETTY_PRINTED_TOGGLE);
        const scriptLocation = await (0, sources_helpers_js_1.retrieveTopCallFrameScriptLocation)('notFormattedFunction();', inspectedPage.page, devToolsPage);
        chai_1.assert.deepEqual(scriptLocation, 'minified-sourcecode.js:6');
    });
    it('can go to line in a pretty-printed file', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('minified-sourcecode.js', 'minified-sourcecode.html', devToolsPage, inspectedPage);
        await devToolsPage.click(PRETTY_PRINT_BUTTON);
        await devToolsPage.waitFor(PRETTY_PRINTED_TOGGLE);
        await (0, quick_open_helpers_js_1.openGoToLineQuickOpen)(devToolsPage);
        await devToolsPage.typeText('6');
        await devToolsPage.page.keyboard.press('Enter');
        await (0, sources_helpers_js_1.waitForHighlightedLine)(6, devToolsPage);
    });
    it('automatically pretty-prints minified code (by default)', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('minified-sourcecode-1.min.js', 'minified-sourcecode-1.html', devToolsPage, inspectedPage);
        const lines = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
        chai_1.assert.lengthOf(lines, 23);
    });
    it('does not automatically pretty-print minified code (when disabled via settings)', async ({ devToolsPage, inspectedPage }) => {
        await (0, settings_helpers_js_1.togglePreferenceInSettingsTab)('Automatically pretty print minified sources', false, devToolsPage);
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('minified-sourcecode-1.min.js', 'minified-sourcecode-1.html', devToolsPage, inspectedPage);
        const lines = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
        chai_1.assert.lengthOf(lines, 3);
    });
    it('does not automatically pretty-print authored code', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openSourceCodeEditorForFile)('minified-sourcecode-1.js', 'minified-sourcecode-1.html', devToolsPage, inspectedPage);
        const lines = await (0, sources_helpers_js_1.retrieveCodeMirrorEditorContent)(devToolsPage);
        chai_1.assert.lengthOf(lines, 2);
    });
    it('shows execution position and inline variables in large pretty-printed minified code', async ({ devToolsPage, inspectedPage }) => {
        await (0, sources_helpers_js_1.openFileInSourcesPanel)('minified-sourcecode-2.html', devToolsPage, inspectedPage);
        // Emulate the button click and wait for the script to open in the Sources panel.
        const evalPromise = inspectedPage.evaluate('handleClick();');
        await devToolsPage.waitFor('[aria-label="minified-sourcecode-2.min.js"][aria-selected="true"]');
        // At some point execution position highlights and variable decorations should appear.
        const [executionLine, executionToken, variableValues] = await Promise.all([
            devToolsPage.waitFor('.cm-executionLine').then(el => el.evaluate(n => n.textContent)),
            devToolsPage.waitFor('.cm-executionToken').then(el => el.evaluate(n => n.textContent)),
            devToolsPage.waitFor('.cm-variableValues').then(el => el.evaluate(n => n.textContent)),
        ]);
        chai_1.assert.strictEqual(executionLine, '    debugger ;');
        chai_1.assert.strictEqual(executionToken, 'debugger');
        chai_1.assert.strictEqual(variableValues, 'y = 40999');
        await Promise.all([
            devToolsPage.click(sources_helpers_js_1.RESUME_BUTTON),
            evalPromise,
        ]);
    });
});
//# sourceMappingURL=can-pretty-print-sourcecode.test.js.map