"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 application_helpers_js_1 = require("../helpers/application-helpers.js");
// The parent suffix makes sure we wait for the Cookies item to have children before trying to click it.
const CLEAR_SITE_DATA_BUTTON_SELECTOR = '#storage-view-clear-button';
async function navigateToAppStorage(devToolsPage, inspectedPage) {
    await (0, application_helpers_js_1.navigateToApplicationTab)('storage-quota', devToolsPage, inspectedPage);
    await (0, application_helpers_js_1.navigateToStorage)(devToolsPage);
}
describe('The Application Tab', () => {
    describe('contains a Storage pane', function () {
        // TODO (liviurau): Update navigateToApplicationTab helper to work in docked
        // mode and remove the setup below.
        setup({ dockingMode: 'undocked' });
        it('which clears storage correctly using the clear button', async ({ devToolsPage, inspectedPage }) => {
            await navigateToAppStorage(devToolsPage, inspectedPage);
            await inspectedPage.bringToFront();
            await inspectedPage.evaluate(async () => {
                const array = [];
                for (let i = 0; i < 20000; i++) {
                    array.push(i % 10);
                }
                // @ts-expect-error
                await new Promise(resolve => createDatabase(resolve, 'Database1'));
                // @ts-expect-error
                await new Promise(resolve => createObjectStore(resolve, 'Database1', 'Store1', 'id', true));
                // @ts-expect-error
                await new Promise(resolve => addIDBValue(resolve, 'Database1', 'Store1', { key: 1, value: array }, ''));
            });
            await (0, application_helpers_js_1.waitForQuotaUsage)(quota => quota > 800, devToolsPage);
            // We may click too early. If the total quota exceeds 2999, some remaining
            // quota may show. Instead,
            // try to click another time, if necessary.
            await devToolsPage.waitForFunction(async () => {
                await devToolsPage.click(CLEAR_SITE_DATA_BUTTON_SELECTOR, { clickOptions: { delay: 250 } });
                const quota = await (0, application_helpers_js_1.getQuotaUsage)(devToolsPage);
                return quota === 0;
            });
        });
        it('which reports storage correctly, including the pie chart legend', async ({ devToolsPage, inspectedPage }) => {
            await navigateToAppStorage(devToolsPage, inspectedPage);
            await inspectedPage.evaluate(async () => {
                const array = [];
                for (let i = 0; i < 20000; i++) {
                    array.push(i % 10);
                }
                // @ts-expect-error
                await new Promise(resolve => createDatabase(resolve, 'Database1'));
                // @ts-expect-error
                await new Promise(resolve => createObjectStore(resolve, 'Database1', 'Store1', 'id', true));
                // @ts-expect-error
                await new Promise(resolve => addIDBValue(resolve, 'Database1', 'Store1', { key: 1, value: array }, ''));
            });
            await (0, application_helpers_js_1.waitForQuotaUsage)(quota => quota > 800, devToolsPage);
            const rows = await (0, application_helpers_js_1.getPieChartLegendRows)(devToolsPage);
            // Only assert that the legend entries are correct.
            chai_1.assert.lengthOf(rows, 2);
            chai_1.assert.strictEqual(rows[0][2], 'IndexedDB');
            chai_1.assert.strictEqual(rows[1][2], 'Total');
        });
    });
});
//# sourceMappingURL=storage.test.js.map