Path: blob/main/src/vs/workbench/services/keybinding/test/browser/browserKeyboardMapper.test.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*--------------------------------------------------------------------------------------------*/4import assert from 'assert';5import '../../browser/keyboardLayouts/en.darwin.js';6import '../../browser/keyboardLayouts/de.darwin.js';7import { KeyboardLayoutContribution } from '../../browser/keyboardLayouts/_.contribution.js';8import { BrowserKeyboardMapperFactoryBase } from '../../browser/keyboardLayoutService.js';9import { KeymapInfo, IKeymapInfo } from '../../common/keymapInfo.js';10import { TestInstantiationService } from '../../../../../platform/instantiation/test/common/instantiationServiceMock.js';11import { INotificationService } from '../../../../../platform/notification/common/notification.js';12import { ICommandService } from '../../../../../platform/commands/common/commands.js';13import { IStorageService } from '../../../../../platform/storage/common/storage.js';14import { TestNotificationService } from '../../../../../platform/notification/test/common/testNotificationService.js';15import { TestStorageService } from '../../../../test/common/workbenchTestServices.js';16import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';17import { TestConfigurationService } from '../../../../../platform/configuration/test/common/testConfigurationService.js';18import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';1920class TestKeyboardMapperFactory extends BrowserKeyboardMapperFactoryBase {21constructor(configurationService: IConfigurationService, notificationService: INotificationService, storageService: IStorageService, commandService: ICommandService) {22// super(notificationService, storageService, commandService);23super(configurationService);2425const keymapInfos: IKeymapInfo[] = KeyboardLayoutContribution.INSTANCE.layoutInfos;26this._keymapInfos.push(...keymapInfos.map(info => (new KeymapInfo(info.layout, info.secondaryLayouts, info.mapping, info.isUserKeyboardLayout))));27this._mru = this._keymapInfos;28this._initialized = true;29this.setLayoutFromBrowserAPI();30const usLayout = this.getUSStandardLayout();31if (usLayout) {32this.setActiveKeyMapping(usLayout.mapping);33}34}35}3637suite('keyboard layout loader', () => {38const ds = ensureNoDisposablesAreLeakedInTestSuite();39let instantiationService: TestInstantiationService;40let instance: TestKeyboardMapperFactory;4142setup(() => {43instantiationService = new TestInstantiationService();44const storageService = new TestStorageService();45const notitifcationService = instantiationService.stub(INotificationService, new TestNotificationService());46const configurationService = instantiationService.stub(IConfigurationService, new TestConfigurationService());47const commandService = instantiationService.stub(ICommandService, {});4849ds.add(instantiationService);50ds.add(storageService);5152instance = new TestKeyboardMapperFactory(configurationService, notitifcationService, storageService, commandService);53ds.add(instance);54});5556teardown(() => {57instantiationService.dispose();58});5960test('load default US keyboard layout', () => {61assert.notStrictEqual(instance.activeKeyboardLayout, null);62});6364test('isKeyMappingActive', () => {65instance.setUSKeyboardLayout();66assert.strictEqual(instance.isKeyMappingActive({67KeyA: {68value: 'a',69valueIsDeadKey: false,70withShift: 'A',71withShiftIsDeadKey: false,72withAltGr: 'å',73withAltGrIsDeadKey: false,74withShiftAltGr: 'Å',75withShiftAltGrIsDeadKey: false76}77}), true);7879assert.strictEqual(instance.isKeyMappingActive({80KeyA: {81value: 'a',82valueIsDeadKey: false,83withShift: 'A',84withShiftIsDeadKey: false,85withAltGr: 'å',86withAltGrIsDeadKey: false,87withShiftAltGr: 'Å',88withShiftAltGrIsDeadKey: false89},90KeyZ: {91value: 'z',92valueIsDeadKey: false,93withShift: 'Z',94withShiftIsDeadKey: false,95withAltGr: 'Ω',96withAltGrIsDeadKey: false,97withShiftAltGr: '¸',98withShiftAltGrIsDeadKey: false99}100}), true);101102assert.strictEqual(instance.isKeyMappingActive({103KeyZ: {104value: 'y',105valueIsDeadKey: false,106withShift: 'Y',107withShiftIsDeadKey: false,108withAltGr: '¥',109withAltGrIsDeadKey: false,110withShiftAltGr: 'Ÿ',111withShiftAltGrIsDeadKey: false112},113}), false);114115});116117test('Switch keymapping', () => {118instance.setActiveKeyMapping({119KeyZ: {120value: 'y',121valueIsDeadKey: false,122withShift: 'Y',123withShiftIsDeadKey: false,124withAltGr: '¥',125withAltGrIsDeadKey: false,126withShiftAltGr: 'Ÿ',127withShiftAltGrIsDeadKey: false128}129});130assert.strictEqual(!!instance.activeKeyboardLayout!.isUSStandard, false);131assert.strictEqual(instance.isKeyMappingActive({132KeyZ: {133value: 'y',134valueIsDeadKey: false,135withShift: 'Y',136withShiftIsDeadKey: false,137withAltGr: '¥',138withAltGrIsDeadKey: false,139withShiftAltGr: 'Ÿ',140withShiftAltGrIsDeadKey: false141},142}), true);143144instance.setUSKeyboardLayout();145assert.strictEqual(instance.activeKeyboardLayout!.isUSStandard, true);146});147148test('Switch keyboard layout info', () => {149instance.setKeyboardLayout('com.apple.keylayout.German');150assert.strictEqual(!!instance.activeKeyboardLayout!.isUSStandard, false);151assert.strictEqual(instance.isKeyMappingActive({152KeyZ: {153value: 'y',154valueIsDeadKey: false,155withShift: 'Y',156withShiftIsDeadKey: false,157withAltGr: '¥',158withAltGrIsDeadKey: false,159withShiftAltGr: 'Ÿ',160withShiftAltGrIsDeadKey: false161},162}), true);163164instance.setUSKeyboardLayout();165assert.strictEqual(instance.activeKeyboardLayout!.isUSStandard, true);166});167});168169170