"use strict";
// 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.
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const performance_helpers_js_1 = require("../../helpers/performance-helpers.js");
async function searchForWasmCall(devToolsPage) {
    await devToolsPage.waitForFunction(async () => {
        await (0, performance_helpers_js_1.searchForComponent)('mainWasm', devToolsPage);
        const title = await devToolsPage.$('.timeline-details-chip-title');
        if (!title) {
            return false;
        }
        const titleText = await title.evaluate(x => x.textContent);
        return titleText === 'mainWasm';
    });
}
async function expandAndCheckActivityTree(expectedActivities, devtoolsPage) {
    let index = 0;
    let parentItem = undefined;
    do {
        await devtoolsPage.waitForFunction(async () => {
            if (parentItem) {
                await parentItem.evaluate(e => e.scrollIntoView());
            }
            const treeItem = await devtoolsPage.$('.data-grid-data-grid-node.selected.revealed .activity-name');
            if (!treeItem) {
                return false;
            }
            const treeItemText = await treeItem.evaluate(el => el.innerText);
            if (expectedActivities[index] === treeItemText) {
                parentItem = treeItem;
                return true;
            }
            return false;
        });
        index++;
        await devtoolsPage.page.keyboard.press('ArrowRight');
        await devtoolsPage.page.keyboard.press('ArrowRight');
    } while (index < expectedActivities.length);
}
describe('The Performance panel', function () {
    setup({ dockingMode: 'undocked' });
    (0, performance_helpers_js_1.increaseTimeoutForPerfPanel)(this);
    async function setupPerformancePanel(devToolsPage, inspectedPage) {
        await (0, performance_helpers_js_1.navigateToPerformanceTab)('wasm/profiling', devToolsPage, inspectedPage);
        await (0, performance_helpers_js_1.uploadTraceFile)(devToolsPage, 'test/e2e/resources/performance/wasm/mainWasm_profile.json');
        await searchForWasmCall(devToolsPage);
    }
    it('is able to record performance', async ({ devToolsPage, inspectedPage }) => {
        await (0, performance_helpers_js_1.navigateToPerformanceTab)('wasm/profiling', devToolsPage, inspectedPage);
        await devToolsPage.page.keyboard.press('Escape');
        await devToolsPage.waitFor('.console-searchable-view');
        await (0, performance_helpers_js_1.startRecording)(devToolsPage);
        await inspectedPage.reload();
        await devToolsPage.waitFor('.console-message-text .source-code');
        await (0, performance_helpers_js_1.stopRecording)(devToolsPage);
        await devToolsPage.waitFor(performance_helpers_js_1.SUMMARY_TAB_SELECTOR);
        await devToolsPage.waitFor(performance_helpers_js_1.BOTTOM_UP_SELECTOR);
        await devToolsPage.waitFor(performance_helpers_js_1.CALL_TREE_SELECTOR);
    });
    it('is able to display the execution time for a wasm function', async ({ devToolsPage, inspectedPage }) => {
        await setupPerformancePanel(devToolsPage, inspectedPage);
        const totalTime = await (0, performance_helpers_js_1.getTotalTimeFromPie)(devToolsPage);
        chai_1.assert.isAbove(totalTime, 0, 'mainWasm function execution time is displayed incorrectly');
    });
    it('is able to inspect the call stack for a wasm function from the bottom up', async ({ devToolsPage, inspectedPage }) => {
        await setupPerformancePanel(devToolsPage, inspectedPage);
        const expectedActivities = ['mainWasm', 'js-to-wasm::i', '(anonymous)', 'Run microtasks'];
        await (0, performance_helpers_js_1.navigateToBottomUpTab)(devToolsPage, 'url');
        const timelineTree = (await devToolsPage.$('.timeline-tree-view'));
        const rootActivity = await devToolsPage.waitForElementWithTextContent(expectedActivities[0], timelineTree);
        chai_1.assert.isOk(rootActivity, `Could not find ${expectedActivities[0]} in DevTools.`);
        await rootActivity.click();
        await expandAndCheckActivityTree(expectedActivities, devToolsPage);
    });
    it('is able to inspect the call stack for a wasm function from the call tree', async ({ devToolsPage, inspectedPage }) => {
        await setupPerformancePanel(devToolsPage, inspectedPage);
        const expectedActivities = [
            'Run microtasks',
            '(anonymous)',
            'js-to-wasm::i',
            'mainWasm',
            'wasm-to-js::l-imports.getTime',
            'getTime',
        ];
        await (0, performance_helpers_js_1.navigateToCallTreeTab)(devToolsPage);
        const timelineTree = (await devToolsPage.$('.timeline-tree-view'));
        const rootActivity = await devToolsPage.waitForElementWithTextContent(expectedActivities[0], timelineTree);
        chai_1.assert.isOk(rootActivity, `Could not find ${expectedActivities[0]} in DevTools.`);
        await rootActivity.click();
        await expandAndCheckActivityTree(expectedActivities, devToolsPage);
    });
});
//# sourceMappingURL=performance_profiling.test.js.map