From 498f2905869011da1ddc57c3126fa7c15d67b9e2 Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Thu, 6 Apr 2017 18:15:00 +0200 Subject: [PATCH] Debt: Test for keybindings editor model (cont..) --- .../common/keybindingsEditorModel.test.ts | 182 +++++++++++++++++- 1 file changed, 178 insertions(+), 4 deletions(-) diff --git a/src/vs/workbench/parts/preferences/test/common/keybindingsEditorModel.test.ts b/src/vs/workbench/parts/preferences/test/common/keybindingsEditorModel.test.ts index 4d48c0ec92c..7a0c3dc1235 100644 --- a/src/vs/workbench/parts/preferences/test/common/keybindingsEditorModel.test.ts +++ b/src/vs/workbench/parts/preferences/test/common/keybindingsEditorModel.test.ts @@ -7,7 +7,12 @@ import * as assert from 'assert'; import * as uuid from 'vs/base/common/uuid'; import { TPromise } from 'vs/base/common/winjs.base'; import { OS } from 'vs/base/common/platform'; +import { Registry } from 'vs/platform/platform'; +import { Action } from 'vs/base/common/actions'; import { KeyCode, SimpleKeybinding, ChordKeybinding } from 'vs/base/common/keyCodes'; +import { SyncActionDescriptor } from 'vs/platform/actions/common/actions'; +import { CommandsRegistry } from 'vs/platform/commands/common/commands'; +import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actionRegistry'; import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { IExtensionService } from 'vs/platform/extensions/common/extensions'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; @@ -24,6 +29,12 @@ interface Modifiers { shiftKey?: boolean; } +class AnAction extends Action { + constructor(id: string) { + super(id); + } +} + suite('Keybindings Editor Model test', () => { let instantiationService: TestInstantiationService; @@ -36,15 +47,95 @@ suite('Keybindings Editor Model test', () => { instantiationService.stub(IExtensionService, {}, 'onReady', () => TPromise.as(null)); testObject = instantiationService.createInstance(KeybindingsEditorModel, OS); + + CommandsRegistry.registerCommand('command_without_keybinding', () => { }); }); test('fetch returns default keybindings', () => { - const expected = [ + const expected = prepareKeybindingService( aResolvedKeybindingItem({ command: 'a' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape } }), aResolvedKeybindingItem({ command: 'b' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }) - ]; - instantiationService.stub(IKeybindingService, 'getKeybindings', () => expected); - instantiationService.stub(IKeybindingService, 'getDefaultKeybindings', () => expected); + ); + + return testObject.resolve().then(() => { + const actuals = asResolvedKeybindingItems(testObject.fetch('')); + assertKeybindingItems(actuals, expected); + }); + }); + + test('fetch returns default keybindings at the top', () => { + const expected = prepareKeybindingService( + aResolvedKeybindingItem({ command: 'a' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'b' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }) + ); + + return testObject.resolve().then(() => { + const actuals = asResolvedKeybindingItems(testObject.fetch('').slice(0, 2), true); + assertKeybindingItems(actuals, expected); + }); + }); + + test('fetch returns default keybindings sorted by command id', () => { + const keybindings = prepareKeybindingService( + aResolvedKeybindingItem({ command: 'b' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'c' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'a' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Backspace } }) + ); + const expected = [keybindings[2], keybindings[0], keybindings[1]]; + + return testObject.resolve().then(() => { + const actuals = asResolvedKeybindingItems(testObject.fetch('')); + assertKeybindingItems(actuals, expected); + }); + }); + + test('fetch returns user keybinding first if default and user has same id', () => { + const sameId = 'b' + uuid.generateUuid(); + const keybindings = prepareKeybindingService( + aResolvedKeybindingItem({ command: sameId, firstPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: sameId, firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape }, isDefault: false }) + ); + const expected = [keybindings[1], keybindings[0]]; + + return testObject.resolve().then(() => { + const actuals = asResolvedKeybindingItems(testObject.fetch('')); + assertKeybindingItems(actuals, expected); + }); + }); + + test('fetch returns keybinding with titles first', () => { + const keybindings = prepareKeybindingService( + aResolvedKeybindingItem({ command: 'a' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'b' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'c' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'd' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }) + ); + + registerCommandWithTitle(keybindings[1].command, 'B Title'); + registerCommandWithTitle(keybindings[3].command, 'A Title'); + + const expected = [keybindings[3], keybindings[1], keybindings[0], keybindings[2]]; + instantiationService.stub(IKeybindingService, 'getKeybindings', () => keybindings); + instantiationService.stub(IKeybindingService, 'getDefaultKeybindings', () => keybindings); + + return testObject.resolve().then(() => { + const actuals = asResolvedKeybindingItems(testObject.fetch('')); + assertKeybindingItems(actuals, expected); + }); + }); + + test('fetch returns keybinding with user first if title and id matches', () => { + const sameId = 'b' + uuid.generateUuid(); + const keybindings = prepareKeybindingService( + aResolvedKeybindingItem({ command: 'a' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: sameId, firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'c' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: sameId, firstPart: { keyCode: KeyCode.Escape }, isDefault: false }) + ); + + registerCommandWithTitle(keybindings[1].command, 'Same Title'); + registerCommandWithTitle(keybindings[3].command, 'Same Title'); + const expected = [keybindings[3], keybindings[1], keybindings[0], keybindings[2]]; return testObject.resolve().then(() => { const actuals = asResolvedKeybindingItems(testObject.fetch('')); @@ -52,6 +143,89 @@ suite('Keybindings Editor Model test', () => { }); }); + test('fetch returns default keybindings sorted by precedence', () => { + const expected = prepareKeybindingService( + aResolvedKeybindingItem({ command: 'b' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'c' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, chordPart: { keyCode: KeyCode.Escape } }), + aResolvedKeybindingItem({ command: 'a' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Backspace } }) + ); + + return testObject.resolve().then(() => { + const actuals = asResolvedKeybindingItems(testObject.fetch('', true)); + assertKeybindingItems(actuals, expected); + }); + }); + + test('convert keybinding without title to entry', () => { + const expected = aResolvedKeybindingItem({ command: 'a' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, when: 'context1 && context2' }); + prepareKeybindingService(expected); + + return testObject.resolve().then(() => { + const actual = testObject.fetch('')[0]; + assert.equal(actual.keybindingItem.command, expected.command); + assert.equal(actual.keybindingItem.commandLabel, ''); + assert.equal(actual.keybindingItem.commandDefaultLabel, null); + assert.equal(actual.keybindingItem.keybinding.getAriaLabel(), expected.resolvedKeybinding.getAriaLabel()); + assert.equal(actual.keybindingItem.when, expected.when.serialize()); + }); + }); + + test('convert keybinding with title to entry', () => { + const expected = aResolvedKeybindingItem({ command: 'a' + uuid.generateUuid(), firstPart: { keyCode: KeyCode.Escape }, when: 'context1 && context2' }); + prepareKeybindingService(expected); + registerCommandWithTitle(expected.command, 'Some Title'); + + return testObject.resolve().then(() => { + const actual = testObject.fetch('')[0]; + assert.equal(actual.keybindingItem.command, expected.command); + assert.equal(actual.keybindingItem.commandLabel, 'Some Title'); + assert.equal(actual.keybindingItem.commandDefaultLabel, null); + assert.equal(actual.keybindingItem.keybinding.getAriaLabel(), expected.resolvedKeybinding.getAriaLabel()); + assert.equal(actual.keybindingItem.when, expected.when.serialize()); + }); + }); + + test('convert without title and binding to entry', () => { + CommandsRegistry.registerCommand('command_without_keybinding', () => { }); + prepareKeybindingService(); + + return testObject.resolve().then(() => { + const actual = testObject.fetch('').filter(element => element.keybindingItem.command === 'command_without_keybinding')[0]; + assert.equal(actual.keybindingItem.command, 'command_without_keybinding'); + assert.equal(actual.keybindingItem.commandLabel, ''); + assert.equal(actual.keybindingItem.commandDefaultLabel, null); + assert.equal(actual.keybindingItem.keybinding, null); + assert.equal(actual.keybindingItem.when, ''); + }); + }); + + test('convert with title and wihtout binding to entry', () => { + const id = 'a' + uuid.generateUuid(); + registerCommandWithTitle(id, 'some title'); + prepareKeybindingService(); + + return testObject.resolve().then(() => { + const actual = testObject.fetch('').filter(element => element.keybindingItem.command === id)[0]; + assert.equal(actual.keybindingItem.command, id); + assert.equal(actual.keybindingItem.commandLabel, 'some title'); + assert.equal(actual.keybindingItem.commandDefaultLabel, null); + assert.equal(actual.keybindingItem.keybinding, null); + assert.equal(actual.keybindingItem.when, ''); + }); + }); + + function prepareKeybindingService(...keybindingItems: ResolvedKeybindingItem[]): ResolvedKeybindingItem[] { + instantiationService.stub(IKeybindingService, 'getKeybindings', () => keybindingItems); + instantiationService.stub(IKeybindingService, 'getDefaultKeybindings', () => keybindingItems); + return keybindingItems; + + } + + function registerCommandWithTitle(command: string, title: string): void { + const registry = Registry.as(ActionExtensions.WorkbenchActions); + registry.registerWorkbenchAction(new SyncActionDescriptor(AnAction, command, title, { primary: null }), ''); + } + function assertKeybindingItems(actual: ResolvedKeybindingItem[], expected: ResolvedKeybindingItem[]) { assert.equal(actual.length, expected.length); for (let i = 0; i < actual.length; i++) { -- GitLab