extHostCommands.test.ts 2.0 KB
Newer Older
J
Johannes Rieken 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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 已提交
9 10 11 12 13
import { ExtHostCommands } from 'vs/workbench/api/node/extHostCommands';
import { MainThreadCommandsShape } from 'vs/workbench/api/node/extHost.protocol';
import { TPromise } from 'vs/base/common/winjs.base';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { OneGetThreadService } from './testThreadService';
14
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
J
Johannes Rieken 已提交
15 16 17 18 19 20 21

suite('ExtHostCommands', function () {

	test('dispose calls unregister', function () {

		let lastUnregister: string;

22
		const shape = new class extends mock<MainThreadCommandsShape>() {
J
Johannes Rieken 已提交
23
			$registerCommand(id: string): TPromise<any> {
24
				return undefined;
J
Johannes Rieken 已提交
25 26 27
			}
			$unregisterCommand(id: string): TPromise<any> {
				lastUnregister = id;
28
				return undefined;
J
Johannes Rieken 已提交
29 30 31
			}
		};

32
		const commands = new ExtHostCommands(OneGetThreadService(shape), undefined);
33
		commands.registerCommand('foo', (): any => { }).dispose();
J
Johannes Rieken 已提交
34 35 36 37 38 39 40 41 42
		assert.equal(lastUnregister, 'foo');
		assert.equal(CommandsRegistry.getCommand('foo'), undefined);

	});

	test('dispose bubbles only once', function () {

		let unregisterCounter = 0;

43
		const shape = new class extends mock<MainThreadCommandsShape>() {
J
Johannes Rieken 已提交
44
			$registerCommand(id: string): TPromise<any> {
45
				return undefined;
J
Johannes Rieken 已提交
46 47 48
			}
			$unregisterCommand(id: string): TPromise<any> {
				unregisterCounter += 1;
49
				return undefined;
J
Johannes Rieken 已提交
50 51 52
			}
		};

53
		const commands = new ExtHostCommands(OneGetThreadService(shape), undefined);
54
		const reg = commands.registerCommand('foo', (): any => { });
J
Johannes Rieken 已提交
55 56 57 58 59
		reg.dispose();
		reg.dispose();
		reg.dispose();
		assert.equal(unregisterCounter, 1);
	});
60
});