Path: blob/main/src/vs/workbench/services/keybinding/test/node/keyboardMapperTestUtils.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as fs from 'fs';6import assert from 'assert';7import * as path from '../../../../../base/common/path.js';8import { SingleModifierChord, ResolvedKeybinding, Keybinding } from '../../../../../base/common/keybindings.js';9import { Promises } from '../../../../../base/node/pfs.js';10import { IKeyboardEvent } from '../../../../../platform/keybinding/common/keybinding.js';11import { IKeyboardMapper } from '../../../../../platform/keyboardLayout/common/keyboardMapper.js';12import { FileAccess } from '../../../../../base/common/network.js';1314export interface IResolvedKeybinding {15label: string | null;16ariaLabel: string | null;17electronAccelerator: string | null;18userSettingsLabel: string | null;19isWYSIWYG: boolean;20isMultiChord: boolean;21dispatchParts: (string | null)[];22singleModifierDispatchParts: (SingleModifierChord | null)[];23}2425function toIResolvedKeybinding(kb: ResolvedKeybinding): IResolvedKeybinding {26return {27label: kb.getLabel(),28ariaLabel: kb.getAriaLabel(),29electronAccelerator: kb.getElectronAccelerator(),30userSettingsLabel: kb.getUserSettingsLabel(),31isWYSIWYG: kb.isWYSIWYG(),32isMultiChord: kb.hasMultipleChords(),33dispatchParts: kb.getDispatchChords(),34singleModifierDispatchParts: kb.getSingleModifierDispatchChords()35};36}3738export function assertResolveKeyboardEvent(mapper: IKeyboardMapper, keyboardEvent: IKeyboardEvent, expected: IResolvedKeybinding): void {39const actual = toIResolvedKeybinding(mapper.resolveKeyboardEvent(keyboardEvent));40assert.deepStrictEqual(actual, expected);41}4243export function assertResolveKeybinding(mapper: IKeyboardMapper, keybinding: Keybinding, expected: IResolvedKeybinding[]): void {44const actual: IResolvedKeybinding[] = mapper.resolveKeybinding(keybinding).map(toIResolvedKeybinding);45assert.deepStrictEqual(actual, expected);46}4748export function readRawMapping<T>(file: string): Promise<T> {49return fs.promises.readFile(FileAccess.asFileUri(`vs/workbench/services/keybinding/test/node/${file}.js`).fsPath).then((buff) => {50const contents = buff.toString();51const func = new Function('define', contents);// CodeQL [SM01632] This is used in tests and we read the files as JS to avoid slowing down TS compilation52let rawMappings: T | null = null;53func(function (value: T) {54rawMappings = value;55});56return rawMappings!;57});58}5960export function assertMapping(writeFileIfDifferent: boolean, mapper: IKeyboardMapper, file: string): Promise<void> {61const filePath = path.normalize(FileAccess.asFileUri(`vs/workbench/services/keybinding/test/node/${file}`).fsPath);6263return fs.promises.readFile(filePath).then((buff) => {64const expected = buff.toString().replace(/\r\n/g, '\n');65const actual = mapper.dumpDebugInfo().replace(/\r\n/g, '\n');66if (actual !== expected && writeFileIfDifferent) {67const destPath = filePath.replace(/[\/\\]out[\/\\]vs[\/\\]workbench/, '/src/vs/workbench');68Promises.writeFile(destPath, actual);69}70assert.deepStrictEqual(actual, expected);71});72}737475