menuService.test.ts 7.3 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import * as assert from 'assert';
J
Johannes Rieken 已提交
8 9 10 11
import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
import { MenuService } from 'vs/platform/actions/common/menuService';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { NullCommandService } from 'vs/platform/commands/common/commands';
12
import { MockContextKeyService } from 'vs/platform/keybinding/test/common/mockKeybindingService';
B
Benjamin Pasero 已提交
13
import { IExtensionPoint } from 'vs/platform/extensions/common/extensionsRegistry';
14
import { TPromise } from 'vs/base/common/winjs.base';
15
import { ExtensionPointContribution, IExtensionDescription, IExtensionsStatus, IExtensionService, ProfileSession } from 'vs/platform/extensions/common/extensions';
16
import Event, { Emitter } from 'vs/base/common/event';
17

J
Johannes Rieken 已提交
18
// --- service instances
19

20
class MockExtensionService implements IExtensionService {
21

22 23
	public _serviceBrand: any;

24 25 26 27 28
	private _onDidRegisterExtensions = new Emitter<IExtensionDescription[]>();
	public get onDidRegisterExtensions(): Event<IExtensionDescription[]> {
		return this._onDidRegisterExtensions.event;
	}

A
Alex Dima 已提交
29 30
	onDidChangeExtensionsStatus = null;

31 32
	public activateByEvent(activationEvent: string): TPromise<void> {
		throw new Error('Not implemented');
33
	}
34

35
	public whenInstalledExtensionsRegistered(): TPromise<boolean> {
36
		return TPromise.as(true);
37
	}
38

A
Alex Dima 已提交
39 40 41
	public getExtensions(): TPromise<IExtensionDescription[]> {
		throw new Error('Not implemented');
	}
42

A
Alex Dima 已提交
43 44 45
	public readExtensionPointContributions<T>(extPoint: IExtensionPoint<T>): TPromise<ExtensionPointContribution<T>[]> {
		throw new Error('Not implemented');
	}
46

47 48 49 50
	public getExtensionsStatus(): { [id: string]: IExtensionsStatus; } {
		throw new Error('Not implemented');
	}

J
Johannes Rieken 已提交
51 52 53 54
	public canProfileExtensionHost() {
		return false;
	}

55
	public startExtensionHostProfile(): TPromise<ProfileSession> {
56 57 58
		throw new Error('Not implemented');
	}

59 60 61
	public restartExtensionHost(): void {
		throw new Error('Method not implemented.');
	}
62 63 64 65 66 67 68 69

	public startExtensionHost(): void {
		throw new Error('Method not implemented.');
	}

	public stopExtensionHost(): void {
		throw new Error('Method not implemented.');
	}
70 71 72 73

	public getExtensionHostInformation(): any {
		throw new Error('Method not implemented.');
	}
74 75 76
}

const extensionService = new MockExtensionService();
77

78
const contextKeyService = new class extends MockContextKeyService {
79 80 81 82 83
	contextMatchesRules() {
		return true;
	}
};

J
Johannes Rieken 已提交
84
// --- tests
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

suite('MenuService', function () {

	let menuService: MenuService;
	let disposables: IDisposable[];

	setup(function () {
		menuService = new MenuService(extensionService, NullCommandService);
		disposables = [];
	});

	teardown(function () {
		dispose(disposables);
	});

	test('group sorting', function () {

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'one', title: 'FOO' },
			group: '0_hello'
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'two', title: 'FOO' },
J
Johannes Rieken 已提交
109
			group: 'hello'
110 111 112 113
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'three', title: 'FOO' },
J
Johannes Rieken 已提交
114
			group: 'Hello'
115 116 117 118
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'four', title: 'FOO' },
J
Johannes Rieken 已提交
119 120 121 122 123
			group: ''
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'five', title: 'FOO' },
124 125 126
			group: 'navigation'
		}));

127
		const groups = menuService.createMenu(MenuId.ExplorerContext, contextKeyService).getActions();
128

J
Johannes Rieken 已提交
129 130
		assert.equal(groups.length, 5);
		const [one, two, three, four, five] = groups;
131 132 133

		assert.equal(one[0], 'navigation');
		assert.equal(two[0], '0_hello');
134 135
		assert.equal(three[0], 'hello');
		assert.equal(four[0], 'Hello');
J
Johannes Rieken 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		assert.equal(five[0], '');
	});

	test('in group sorting, by title', function () {

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'a', title: 'aaa' },
			group: 'Hello'
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'b', title: 'fff' },
			group: 'Hello'
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'c', title: 'zzz' },
			group: 'Hello'
		}));

156
		const groups = menuService.createMenu(MenuId.ExplorerContext, contextKeyService).getActions();
J
Johannes Rieken 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

		assert.equal(groups.length, 1);
		const [[, actions]] = groups;

		assert.equal(actions.length, 3);
		const [one, two, three] = actions;
		assert.equal(one.id, 'a');
		assert.equal(two.id, 'b');
		assert.equal(three.id, 'c');
	});

	test('in group sorting, by title and order', function () {

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'a', title: 'aaa' },
			group: 'Hello',
			order: 10
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'b', title: 'fff' },
			group: 'Hello'
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'c', title: 'zzz' },
			group: 'Hello',
			order: -1
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'd', title: 'yyy' },
			group: 'Hello',
			order: -1
		}));

193
		const groups = menuService.createMenu(MenuId.ExplorerContext, contextKeyService).getActions();
J
Johannes Rieken 已提交
194 195 196

		assert.equal(groups.length, 1);
		const [[, actions]] = groups;
197

J
Johannes Rieken 已提交
198 199 200 201 202 203
		assert.equal(actions.length, 4);
		const [one, two, three, four] = actions;
		assert.equal(one.id, 'd');
		assert.equal(two.id, 'c');
		assert.equal(three.id, 'b');
		assert.equal(four.id, 'a');
204
	});
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226


	test('in group sorting, special: navigation', function () {

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'a', title: 'aaa' },
			group: 'navigation',
			order: 1.3
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'b', title: 'fff' },
			group: 'navigation',
			order: 1.2
		}));

		disposables.push(MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
			command: { id: 'c', title: 'zzz' },
			group: 'navigation',
			order: 1.1
		}));

227
		const groups = menuService.createMenu(MenuId.ExplorerContext, contextKeyService).getActions();
228 229 230 231 232 233 234 235 236 237

		assert.equal(groups.length, 1);
		const [[, actions]] = groups;

		assert.equal(actions.length, 3);
		const [one, two, three] = actions;
		assert.equal(one.id, 'c');
		assert.equal(two.id, 'b');
		assert.equal(three.id, 'a');
	});
238 239 240 241 242 243 244 245 246

	test('special MenuId palette', function () {

		disposables.push(MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
			command: { id: 'a', title: 'Explicit' }
		}));

		MenuRegistry.addCommand({ id: 'b', title: 'Implicit' });

247 248 249 250 251 252 253 254 255 256 257 258 259 260
		let foundA = false;
		let foundB = false;
		for (const item of MenuRegistry.getMenuItems(MenuId.CommandPalette)) {
			if (item.command.id === 'a') {
				assert.equal(item.command.title, 'Explicit');
				foundA = true;
			}
			if (item.command.id === 'b') {
				assert.equal(item.command.title, 'Implicit');
				foundB = true;
			}
		}
		assert.equal(foundA, true);
		assert.equal(foundB, true);
261
	});
262
});