UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

127 lines • 6.21 kB
// ***************************************************************************** // Copyright (C) 2026 EclipseSource and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { enableJSDOM } from '../test/jsdom.js'; let disableJSDOM = enableJSDOM(); import { FrontendApplicationConfigProvider } from '../frontend-application-config-provider.js'; FrontendApplicationConfigProvider.set({}); import * as chai from 'chai'; import { Container } from 'inversify'; import { CancellationToken, CommandRegistry, Emitter } from '../../common/index.js'; import { CorePreferences } from '../../common/core-preferences.js'; import { ContextKeyService, ContextKeyServiceDummyImpl } from '../context-key-service.js'; import { KeybindingRegistry } from '../keybinding.js'; import { QuickAccessRegistry } from './quick-access.js'; import { QuickCommandService } from './quick-command-service.js'; import { QuickInputService } from './quick-input-service.js'; disableJSDOM(); const expect = chai.expect; class RecordingContextKeyService extends ContextKeyServiceDummyImpl { withContext(context, callback) { const previous = this.activeContext; this.activeContext = context; try { return callback(); } finally { this.activeContext = previous; } } } describe('quick-command-service', () => { let editor; let quickInput; let contextKeyService; let commandRegistry; let onHideEmitter; let quickInputService; let service; before(() => { disableJSDOM = enableJSDOM(); }); after(() => { disableJSDOM(); }); beforeEach(() => { editor = document.createElement('div'); editor.tabIndex = 0; quickInput = document.createElement('input'); document.body.append(editor, quickInput); // Focus has moved to the quick input, as it does while the palette is open. quickInput.focus(); contextKeyService = new RecordingContextKeyService(); commandRegistry = new CommandRegistry({ getContributions: () => [] }); onHideEmitter = new Emitter(); quickInputService = { previousFocusElement: editor, onHide: onHideEmitter.event }; const container = new Container(); container.bind(ContextKeyService).toConstantValue(contextKeyService); container.bind(CommandRegistry).toConstantValue(commandRegistry); container.bind(CorePreferences).toConstantValue({ 'workbench.commandPalette.history': 50 }); container.bind(QuickAccessRegistry).toConstantValue({}); container.bind(KeybindingRegistry).toConstantValue({ getKeybindingsForCommand: () => [] }); container.bind(QuickInputService).toConstantValue(quickInputService); container.bind(QuickCommandService).toSelf(); service = container.get(QuickCommandService); }); afterEach(() => { editor.remove(); quickInput.remove(); onHideEmitter.dispose(); }); function inEditorContext() { return contextKeyService.activeContext === editor; } function picks() { return service.getPicks('', CancellationToken.None).filter((pick) => pick.type !== 'separator'); } it('evaluates command enablement against the element focused before the quick input opened', () => { commandRegistry.registerCommand({ id: 'test.editorOnly', label: 'Editor Only' }, { execute: () => { }, isEnabled: inEditorContext }); expect(picks().map(pick => pick.label)).to.deep.equal(['Editor Only']); }); it('evaluates item state against the element focused before the quick input opened', () => { commandRegistry.registerCommand({ id: 'test.toggle', label: 'Toggle' }, { execute: () => { }, isEnabled: inEditorContext, isToggled: inEditorContext }); const [item] = picks(); expect(item.alwaysShow).to.be.true; expect(item.iconClasses).to.not.be.undefined; }); it('evaluates pushed command contexts against the element focused before the quick input opened', () => { contextKeyService.match = () => inEditorContext(); commandRegistry.registerCommand({ id: 'test.when', label: 'When' }, { execute: () => { } }); service.pushCommandContext('test.when', 'editorFocus'); expect(picks().map(pick => pick.label)).to.deep.equal(['When']); }); it('restores focus to the element focused before the quick input opened on execute', () => { commandRegistry.registerCommand({ id: 'test.command', label: 'Command' }, { execute: () => { } }); picks()[0].execute(); expect(document.activeElement).to.equal(editor); }); it('falls back to the active element when items are created without a reset', () => { commandRegistry.registerCommand({ id: 'test.command', label: 'Command' }, { execute: () => { } }); const item = service.toItem({ id: 'test.command', label: 'Command' }); editor.focus(); item.execute(); expect(document.activeElement).to.equal(quickInput); }); it('forgets the context element when the quick input is hidden', () => { commandRegistry.registerCommand({ id: 'test.command', label: 'Command' }, { execute: () => { } }); service.reset(); onHideEmitter.fire(); const item = service.toItem({ id: 'test.command', label: 'Command' }); editor.focus(); item.execute(); expect(document.activeElement).to.equal(quickInput); }); }); //# sourceMappingURL=quick-command-service.spec.js.map