From ee26dad3c7c2d8935a4f434ba919cf5e8304599b Mon Sep 17 00:00:00 2001 From: Anton Kosyakov Date: Wed, 22 Jan 2020 05:42:50 +0000 Subject: [PATCH] added api tests for monaco resolveKeybinding exposed api Signed-off-by: Anton Kosyakov --- examples/api-tests/src/monaco-api.spec.js | 90 +++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 examples/api-tests/src/monaco-api.spec.js diff --git a/examples/api-tests/src/monaco-api.spec.js b/examples/api-tests/src/monaco-api.spec.js new file mode 100644 index 0000000000000..ab74e94655faa --- /dev/null +++ b/examples/api-tests/src/monaco-api.spec.js @@ -0,0 +1,90 @@ +/******************************************************************************** + * Copyright (C) 2020 TypeFox 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 WITH Classpath-exception-2.0 + ********************************************************************************/ + +// @ts-check +/// +describe('Monaco API', async function () { + + const { assert } = chai; + + const { EditorManager } = require('@theia/editor/lib/browser/editor-manager'); + const Uri = require('@theia/core/lib/common/uri'); + const { WorkspaceService } = require('@theia/workspace/lib/browser/workspace-service'); + const { MonacoEditor } = require('@theia/monaco/lib/browser/monaco-editor'); + const { MonacoResolvedKeybinding } = require('@theia/monaco/lib/browser/monaco-resolved-keybinding'); + + /** @type {import('inversify').Container} */ + const container = window['theia'].container; + const editorManager = container.get(EditorManager); + const workspaceService = container.get(WorkspaceService); + + /** @type {MonacoEditor} */ + let monacoEditor; + + before(async () => { + const root = workspaceService.tryGetRoots()[0]; + const editor = await editorManager.open(new Uri.default(root.uri).resolve('package.json'), { + mode: 'reveal' + }); + monacoEditor = MonacoEditor.get(editor); + }); + + it('KeybindingService.resolveKeybinding', () => { + const simpleKeybinding = new monaco.keybindings.SimpleKeybinding(true, true, true, true, monaco.KeyCode.KEY_K); + const chordKeybinding = simpleKeybinding.toChord(); + assert.equal(chordKeybinding.parts.length, 1); + assert.equal(chordKeybinding.parts[0], simpleKeybinding); + + const resolvedKeybindings = monacoEditor.getControl()._standaloneKeybindingService.resolveKeybinding(chordKeybinding); + assert.equal(resolvedKeybindings.length, 1); + + const resolvedKeybinding = resolvedKeybindings[0]; + if (resolvedKeybinding instanceof MonacoResolvedKeybinding) { + const label = resolvedKeybinding.getLabel(); + const ariaLabel = resolvedKeybinding.getAriaLabel(); + const electronAccelerator = resolvedKeybinding.getElectronAccelerator(); + const userSettingsLabel = resolvedKeybinding.getUserSettingsLabel(); + const WYSIWYG = resolvedKeybinding.isWYSIWYG(); + const chord = resolvedKeybinding.isChord(); + const parts = resolvedKeybinding.getParts(); + const dispatchParts = resolvedKeybinding.getDispatchParts(); + assert.deepStrictEqual({ + label, ariaLabel, electronAccelerator, userSettingsLabel, WYSIWYG, chord, parts, dispatchParts + }, { + label: "Ctrl+Shift+Alt+K", + ariaLabel: "Ctrl+Shift+Alt+K", + electronAccelerator: null, + userSettingsLabel: "ctrl+shift+alt+K", + WYSIWYG: true, + chord: false, + parts: [{ + altKey: true, + ctrlKey: true, + keyAriaLabel: "K", + keyLabel: "K", + metaKey: false, + shiftKey: true + }], + dispatchParts: [ + "ctrl+shift+alt+K" + ] + }); + } else { + assert.fail(`resolvedKeybinding must be of ${MonacoResolvedKeybinding.name} type`); + } + }); + +});