Path: blob/main/src/vs/workbench/api/test/browser/extHostCommands.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*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { ExtHostCommands } from '../../common/extHostCommands.js';7import { MainThreadCommandsShape } from '../../common/extHost.protocol.js';8import { CommandsRegistry } from '../../../../platform/commands/common/commands.js';9import { SingleProxyRPCProtocol } from '../common/testRPCProtocol.js';10import { mock } from '../../../../base/test/common/mock.js';11import { NullLogService } from '../../../../platform/log/common/log.js';12import { IExtHostTelemetry } from '../../common/extHostTelemetry.js';13import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';1415suite('ExtHostCommands', function () {16ensureNoDisposablesAreLeakedInTestSuite();1718test('dispose calls unregister', function () {1920let lastUnregister: string;2122const shape = new class extends mock<MainThreadCommandsShape>() {23override $registerCommand(id: string): void {24//25}26override $unregisterCommand(id: string): void {27lastUnregister = id;28}29};3031const commands = new ExtHostCommands(32SingleProxyRPCProtocol(shape),33new NullLogService(),34new class extends mock<IExtHostTelemetry>() {35override onExtensionError(): boolean {36return true;37}38}39);40commands.registerCommand(true, 'foo', (): any => { }).dispose();41assert.strictEqual(lastUnregister!, 'foo');42assert.strictEqual(CommandsRegistry.getCommand('foo'), undefined);4344});4546test('dispose bubbles only once', function () {4748let unregisterCounter = 0;4950const shape = new class extends mock<MainThreadCommandsShape>() {51override $registerCommand(id: string): void {52//53}54override $unregisterCommand(id: string): void {55unregisterCounter += 1;56}57};5859const commands = new ExtHostCommands(60SingleProxyRPCProtocol(shape),61new NullLogService(),62new class extends mock<IExtHostTelemetry>() {63override onExtensionError(): boolean {64return true;65}66}67);68const reg = commands.registerCommand(true, 'foo', (): any => { });69reg.dispose();70reg.dispose();71reg.dispose();72assert.strictEqual(unregisterCounter, 1);73});7475test('execute with retry', async function () {7677let count = 0;7879const shape = new class extends mock<MainThreadCommandsShape>() {80override $registerCommand(id: string): void {81//82}83override async $executeCommand<T>(id: string, args: any[], retry: boolean): Promise<T | undefined> {84count++;85assert.strictEqual(retry, count === 1);86if (count === 1) {87assert.strictEqual(retry, true);88throw new Error('$executeCommand:retry');89} else {90assert.strictEqual(retry, false);91return <any>17;92}93}94};9596const commands = new ExtHostCommands(97SingleProxyRPCProtocol(shape),98new NullLogService(),99new class extends mock<IExtHostTelemetry>() {100override onExtensionError(): boolean {101return true;102}103}104);105106const result: number = await commands.executeCommand('fooo', [this, true]);107assert.strictEqual(result, 17);108assert.strictEqual(count, 2);109});110111test('onCommand:abc activates extensions when executed from command palette, but not when executed programmatically with vscode.commands.executeCommand #150293', async function () {112113const activationEvents: string[] = [];114115const shape = new class extends mock<MainThreadCommandsShape>() {116override $registerCommand(id: string): void {117//118}119override $fireCommandActivationEvent(id: string): void {120activationEvents.push(id);121}122};123const commands = new ExtHostCommands(124SingleProxyRPCProtocol(shape),125new NullLogService(),126new class extends mock<IExtHostTelemetry>() {127override onExtensionError(): boolean {128return true;129}130}131);132133commands.registerCommand(true, 'extCmd', (args: any): any => args);134135const result: unknown = await commands.executeCommand('extCmd', this);136assert.strictEqual(result, this);137assert.deepStrictEqual(activationEvents, ['extCmd']);138});139});140141142