// 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.
import { assert } from 'chai';
import sinon from 'sinon';
import * as Common from '../../../../core/common/common.js';
import * as i18n from '../../../../core/i18n/i18n.js';
import { createFakeSetting, deinitializeGlobalVars, initializeGlobalVars } from '../../../../testing/EnvironmentHelpers.js';
import { setupLocaleHooks } from '../../../../testing/LocaleHelpers.js';
import { render } from '../../../../ui/lit/lit.js';
import * as UI from '../../legacy.js';
import * as QuickOpen from './quick_open.js';
function createCommandMenuProvider(deprecationNotice) {
    const setting = createFakeSetting('test-setting', false);
    setting.setRegistration({
        settingName: 'test-setting',
        settingType: "boolean" /* Common.SettingRegistration.SettingType.BOOLEAN */,
        category: "APPEARANCE" /* Common.SettingRegistration.SettingCategory.APPEARANCE */,
        defaultValue: false,
        deprecationNotice,
    });
    const command = QuickOpen.CommandMenu.CommandMenu.createSettingCommand(setting, i18n.i18n.lockedString('Test Set Value'), true);
    const provider = new QuickOpen.CommandMenu.CommandMenuProvider([command]);
    return { setting, provider, command };
}
const warning = () => 'Deprecation Warning';
function setupElements() {
    const toplevel = document.createElement('div');
    const container = toplevel.createChild('div');
    const title = container.createChild('div');
    const subtitle = container.createChild('div');
    return { toplevel, container, title, subtitle };
}
describe('CommandMenu', () => {
    setupLocaleHooks();
    let elements;
    beforeEach(() => {
        elements = setupElements();
    });
    afterEach(() => {
        const { toplevel, container, title, subtitle } = elements;
        subtitle.remove();
        title.remove();
        container.remove();
        toplevel.remove();
    });
    it('shows a deprecation warning for deprecated settings', () => {
        const deprecation = { disabled: true, warning };
        const { provider } = createCommandMenuProvider(deprecation);
        render(provider.renderItem(0, 'Test'), elements.toplevel);
        const tags = Array.from(elements.toplevel.querySelectorAll('.deprecated-tag'));
        assert.deepEqual(tags.map(e => e.textContent), ['— deprecated']);
        assert.deepEqual(tags[0].title, 'Deprecation Warning');
    });
    it('reveals the setting when calling a deprecated setting', () => {
        const deprecation = { disabled: true, warning };
        const { setting, command } = createCommandMenuProvider(deprecation);
        const reveal = sinon.stub(Common.Revealer.RevealerRegistry.prototype, 'reveal');
        command.execute();
        assert.isTrue(reveal.calledOnceWithExactly(setting, false), 'Revealer was either not called or was called with unexpected arguments');
    });
});
describe('CommandMenu', () => {
    const settingName = 'mock-setting';
    const settingTitle = 'Mock setting';
    const enableTitle = 'Enable mock setting';
    const disableTitle = 'Disable mock setting';
    const settingCategory = "CONSOLE" /* Common.Settings.SettingCategory.CONSOLE */;
    before(async () => {
        Common.Settings.registerSettingsForTest([{
                category: settingCategory,
                title: i18n.i18n.lockedLazyString(settingTitle),
                settingType: "boolean" /* Common.Settings.SettingType.BOOLEAN */,
                settingName,
                defaultValue: false,
                options: [
                    {
                        value: true,
                        title: i18n.i18n.lockedLazyString(enableTitle),
                    },
                    {
                        value: false,
                        title: i18n.i18n.lockedLazyString(disableTitle),
                    },
                ],
            }], true);
        await initializeGlobalVars({ reset: false });
    });
    after(async () => {
        await deinitializeGlobalVars();
    });
    it('adds commands for changing a setting\'s value', async () => {
        const settingCategory = "CONSOLE" /* Common.Settings.SettingCategory.CONSOLE */;
        Common.Settings.registerSettingsForTest([{
                category: settingCategory,
                title: i18n.i18n.lockedLazyString(settingTitle),
                settingType: "boolean" /* Common.Settings.SettingType.BOOLEAN */,
                settingName,
                defaultValue: false,
                options: [
                    {
                        value: true,
                        title: i18n.i18n.lockedLazyString(enableTitle),
                    },
                    {
                        value: false,
                        title: i18n.i18n.lockedLazyString(disableTitle),
                    },
                ],
            }], true);
        const allCommands = QuickOpen.CommandMenu.CommandMenu.instance({ forceNew: true }).commands();
        const disableSettingCommands = allCommands.filter(command => command.title === disableTitle &&
            command.category === Common.Settings.getLocalizedSettingsCategory(settingCategory));
        const enableSettingCommands = allCommands.filter(command => command.title === enableTitle &&
            command.category === Common.Settings.getLocalizedSettingsCategory(settingCategory));
        assert.lengthOf(disableSettingCommands, 1, 'Commands for changing a setting\'s value were not added correctly');
        assert.lengthOf(enableSettingCommands, 1, 'Commands for changing a setting\'s value were not added correctly');
    });
});
describe('CommandMenu Views', () => {
    const viewId = 'mock-view';
    const viewTitle = 'Mock view';
    const commandPrompt = 'Show mock view';
    const category = "PANEL" /* UI.ViewManager.ViewLocationCategory.PANEL */;
    before(async () => {
        UI.ViewManager.registerLocationResolver({
            name: "panel" /* UI.ViewManager.ViewLocationValues.PANEL */,
            category,
            async loadResolver() {
                return {
                    resolveLocation() {
                        return null;
                    }
                };
            }
        });
        UI.ViewManager.registerViewExtension({
            location: "panel" /* UI.ViewManager.ViewLocationValues.PANEL */,
            id: viewId,
            title: () => viewTitle,
            commandPrompt: () => commandPrompt,
            async loadView() {
                return new UI.Widget.Widget();
            },
        });
        await initializeGlobalVars({ reset: false });
    });
    after(async () => {
        await deinitializeGlobalVars();
    });
    it('adds commands for showing a view', async () => {
        const allCommands = QuickOpen.CommandMenu.CommandMenu.instance({ forceNew: true }).commands();
        const viewCommands = allCommands.filter(command => command.title === commandPrompt &&
            command.category === UI.ViewManager.getLocalizedViewLocationCategory(category));
        assert.lengthOf(viewCommands, 1, 'Commands for showing a view were not added correctly');
    });
    it('executes a command for showing a view', async () => {
        const allCommands = QuickOpen.CommandMenu.CommandMenu.instance({ forceNew: true }).commands();
        const viewCommand = allCommands.find(command => command.title === commandPrompt &&
            command.category === UI.ViewManager.getLocalizedViewLocationCategory(category));
        assert.exists(viewCommand);
        const showViewStub = sinon.stub(UI.ViewManager.ViewManager.instance(), 'showView').resolves();
        viewCommand.execute();
        sinon.assert.calledOnceWithExactly(showViewStub, viewId, true);
        showViewStub.restore();
    });
});
//# sourceMappingURL=CommandMenu.test.js.map