browserKeyboardMapper.test.ts 5.1 KB
Newer Older
P
Peng Lyu 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
6
import 'vs/workbench/services/keybinding/browser/keyboardLayouts/en.darwin';
P
Peng Lyu 已提交
7 8
import 'vs/workbench/services/keybinding/browser/keyboardLayouts/de.darwin';
import { KeyboardLayoutContribution } from 'vs/workbench/services/keybinding/browser/keyboardLayouts/_.contribution';
9
import { BrowserKeyboardMapperFactoryBase } from 'vs/workbench/services/keybinding/browser/keyboardLayoutService';
10
import { KeymapInfo, IKeymapInfo } from 'vs/workbench/services/keybinding/common/keymapInfo';
11 12 13
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { ICommandService } from 'vs/platform/commands/common/commands';
P
Peng Lyu 已提交
14
import { IStorageService } from 'vs/platform/storage/common/storage';
15
import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService';
16
import { TestStorageService } from 'vs/workbench/test/common/workbenchTestServices';
P
Peng Lyu 已提交
17 18

class TestKeyboardMapperFactory extends BrowserKeyboardMapperFactoryBase {
P
Peng Lyu 已提交
19
	constructor(notificationService: INotificationService, storageService: IStorageService, commandService: ICommandService) {
P
Peng Lyu 已提交
20 21
		// super(notificationService, storageService, commandService);
		super();
P
Peng Lyu 已提交
22

P
Peng Lyu 已提交
23
		const keymapInfos: IKeymapInfo[] = KeyboardLayoutContribution.INSTANCE.layoutInfos;
P
Peng Lyu 已提交
24 25 26 27
		this._keymapInfos.push(...keymapInfos.map(info => (new KeymapInfo(info.layout, info.secondaryLayouts, info.mapping, info.isUserKeyboardLayout))));
		this._mru = this._keymapInfos;
		this._initialized = true;
		this.onKeyboardLayoutChanged();
P
Peng Lyu 已提交
28 29 30 31
		const usLayout = this.getUSStandardLayout();
		if (usLayout) {
			this.setActiveKeyMapping(usLayout.mapping);
		}
P
Peng Lyu 已提交
32 33 34 35
	}
}

suite('keyboard layout loader', () => {
36
	let instantiationService: TestInstantiationService = new TestInstantiationService();
37 38 39
	let notitifcationService = instantiationService.stub(INotificationService, new TestNotificationService());
	let storageService = instantiationService.stub(IStorageService, new TestStorageService());

40
	let commandService = instantiationService.stub(ICommandService, {});
P
Peng Lyu 已提交
41
	let instance = new TestKeyboardMapperFactory(notitifcationService, storageService, commandService);
P
Peng Lyu 已提交
42

P
Peng Lyu 已提交
43
	test('load default US keyboard layout', () => {
44
		assert.notEqual(instance.activeKeyboardLayout, null);
P
Peng Lyu 已提交
45 46
	});

P
Peng Lyu 已提交
47 48
	test('isKeyMappingActive', () => {
		instance.setUSKeyboardLayout();
49
		assert.equal(instance.isKeyMappingActive({
P
Peng Lyu 已提交
50 51 52 53 54 55 56 57 58 59 60 61
			KeyA: {
				value: 'a',
				valueIsDeadKey: false,
				withShift: 'A',
				withShiftIsDeadKey: false,
				withAltGr: 'å',
				withAltGrIsDeadKey: false,
				withShiftAltGr: 'Å',
				withShiftAltGrIsDeadKey: false
			}
		}), true);

62
		assert.equal(instance.isKeyMappingActive({
P
Peng Lyu 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
			KeyA: {
				value: 'a',
				valueIsDeadKey: false,
				withShift: 'A',
				withShiftIsDeadKey: false,
				withAltGr: 'å',
				withAltGrIsDeadKey: false,
				withShiftAltGr: 'Å',
				withShiftAltGrIsDeadKey: false
			},
			KeyZ: {
				value: 'z',
				valueIsDeadKey: false,
				withShift: 'Z',
				withShiftIsDeadKey: false,
				withAltGr: 'Ω',
				withAltGrIsDeadKey: false,
				withShiftAltGr: '¸',
				withShiftAltGrIsDeadKey: false
			}
		}), true);

85
		assert.equal(instance.isKeyMappingActive({
P
Peng Lyu 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
			KeyZ: {
				value: 'y',
				valueIsDeadKey: false,
				withShift: 'Y',
				withShiftIsDeadKey: false,
				withAltGr: '¥',
				withAltGrIsDeadKey: false,
				withShiftAltGr: 'Ÿ',
				withShiftAltGrIsDeadKey: false
			},
		}), false);

	});

	test('Switch keymapping', () => {
101
		instance.setActiveKeyMapping({
P
Peng Lyu 已提交
102 103 104 105 106 107 108 109 110 111 112
			KeyZ: {
				value: 'y',
				valueIsDeadKey: false,
				withShift: 'Y',
				withShiftIsDeadKey: false,
				withAltGr: '¥',
				withAltGrIsDeadKey: false,
				withShiftAltGr: 'Ÿ',
				withShiftAltGrIsDeadKey: false
			}
		});
113 114
		assert.equal(!!instance.activeKeyboardLayout!.isUSStandard, false);
		assert.equal(instance.isKeyMappingActive({
P
Peng Lyu 已提交
115 116 117 118 119 120 121 122 123 124 125 126
			KeyZ: {
				value: 'y',
				valueIsDeadKey: false,
				withShift: 'Y',
				withShiftIsDeadKey: false,
				withAltGr: '¥',
				withAltGrIsDeadKey: false,
				withShiftAltGr: 'Ÿ',
				withShiftAltGrIsDeadKey: false
			},
		}), true);

127 128
		instance.setUSKeyboardLayout();
		assert.equal(instance.activeKeyboardLayout!.isUSStandard, true);
P
Peng Lyu 已提交
129 130 131
	});

	test('Switch keyboard layout info', () => {
132 133 134
		instance.setKeyboardLayout('com.apple.keylayout.German');
		assert.equal(!!instance.activeKeyboardLayout!.isUSStandard, false);
		assert.equal(instance.isKeyMappingActive({
P
Peng Lyu 已提交
135 136 137 138 139 140 141 142 143 144 145 146
			KeyZ: {
				value: 'y',
				valueIsDeadKey: false,
				withShift: 'Y',
				withShiftIsDeadKey: false,
				withAltGr: '¥',
				withAltGrIsDeadKey: false,
				withShiftAltGr: 'Ÿ',
				withShiftAltGrIsDeadKey: false
			},
		}), true);

147 148
		instance.setUSKeyboardLayout();
		assert.equal(instance.activeKeyboardLayout!.isUSStandard, true);
P
Peng Lyu 已提交
149
	});
P
Peng Lyu 已提交
150
});