Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/test/browser/extHostMessagerService.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { MainThreadMessageService } from '../../browser/mainThreadMessageService.js';
8
import { IDialogService, IPrompt, IPromptButton } from '../../../../platform/dialogs/common/dialogs.js';
9
import { INotificationService, INotification, NoOpNotification, INotificationHandle, Severity, IPromptChoice, IPromptOptions, IStatusMessageOptions, INotificationSource, INotificationSourceFilter, NotificationsFilter, IStatusHandle } from '../../../../platform/notification/common/notification.js';
10
import { ICommandService } from '../../../../platform/commands/common/commands.js';
11
import { mock } from '../../../../base/test/common/mock.js';
12
import { Disposable } from '../../../../base/common/lifecycle.js';
13
import { Event } from '../../../../base/common/event.js';
14
import { TestDialogService } from '../../../../platform/dialogs/test/common/testDialogService.js';
15
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
16
import { TestExtensionService } from '../../../test/common/workbenchTestServices.js';
17
18
const emptyCommandService: ICommandService = {
19
_serviceBrand: undefined,
20
onWillExecuteCommand: () => Disposable.None,
21
onDidExecuteCommand: () => Disposable.None,
22
executeCommand: (commandId: string, ...args: any[]): Promise<any> => {
23
return Promise.resolve(undefined);
24
}
25
};
26
27
const emptyNotificationService = new class implements INotificationService {
28
declare readonly _serviceBrand: undefined;
29
onDidChangeFilter: Event<void> = Event.None;
30
notify(...args: any[]): never {
31
throw new Error('not implemented');
32
}
33
info(...args: any[]): never {
34
throw new Error('not implemented');
35
}
36
warn(...args: any[]): never {
37
throw new Error('not implemented');
38
}
39
error(...args: any[]): never {
40
throw new Error('not implemented');
41
}
42
prompt(severity: Severity, message: string, choices: IPromptChoice[], options?: IPromptOptions): INotificationHandle {
43
throw new Error('not implemented');
44
}
45
status(message: string | Error, options?: IStatusMessageOptions): IStatusHandle {
46
return { close: () => { } };
47
}
48
setFilter(): void {
49
throw new Error('not implemented');
50
}
51
getFilter(source?: INotificationSource | undefined): NotificationsFilter {
52
throw new Error('not implemented');
53
}
54
getFilters(): INotificationSourceFilter[] {
55
throw new Error('not implemented');
56
}
57
removeFilter(sourceId: string): void {
58
throw new Error('not implemented');
59
}
60
};
61
62
class EmptyNotificationService implements INotificationService {
63
declare readonly _serviceBrand: undefined;
64
filter: boolean = false;
65
constructor(private withNotify: (notification: INotification) => void) {
66
}
67
68
onDidChangeFilter: Event<void> = Event.None;
69
notify(notification: INotification): INotificationHandle {
70
this.withNotify(notification);
71
72
return new NoOpNotification();
73
}
74
info(message: any): void {
75
throw new Error('Method not implemented.');
76
}
77
warn(message: any): void {
78
throw new Error('Method not implemented.');
79
}
80
error(message: any): void {
81
throw new Error('Method not implemented.');
82
}
83
prompt(severity: Severity, message: string, choices: IPromptChoice[], options?: IPromptOptions): INotificationHandle {
84
throw new Error('Method not implemented');
85
}
86
status(message: string, options?: IStatusMessageOptions): IStatusHandle {
87
return { close: () => { } };
88
}
89
setFilter(): void {
90
throw new Error('Method not implemented.');
91
}
92
getFilter(source?: INotificationSource | undefined): NotificationsFilter {
93
throw new Error('Method not implemented.');
94
}
95
getFilters(): INotificationSourceFilter[] {
96
throw new Error('Method not implemented.');
97
}
98
removeFilter(sourceId: string): void {
99
throw new Error('Method not implemented.');
100
}
101
}
102
103
suite('ExtHostMessageService', function () {
104
105
test('propagte handle on select', async function () {
106
107
const service = new MainThreadMessageService(null!, new EmptyNotificationService(notification => {
108
assert.strictEqual(notification.actions!.primary!.length, 1);
109
queueMicrotask(() => notification.actions!.primary![0].run());
110
}), emptyCommandService, new TestDialogService(), new TestExtensionService());
111
112
const handle = await service.$showMessage(1, 'h', {}, [{ handle: 42, title: 'a thing', isCloseAffordance: true }]);
113
assert.strictEqual(handle, 42);
114
115
service.dispose();
116
});
117
118
suite('modal', () => {
119
test('calls dialog service', async () => {
120
const service = new MainThreadMessageService(null!, emptyNotificationService, emptyCommandService, new class extends mock<IDialogService>() {
121
override prompt({ type, message, buttons, cancelButton }: IPrompt<any>) {
122
assert.strictEqual(type, 1);
123
assert.strictEqual(message, 'h');
124
assert.strictEqual(buttons!.length, 1);
125
assert.strictEqual((cancelButton as IPromptButton<unknown>)!.label, 'Cancel');
126
return Promise.resolve({ result: buttons![0].run({ checkboxChecked: false }) });
127
}
128
} as IDialogService, new TestExtensionService());
129
130
const handle = await service.$showMessage(1, 'h', { modal: true }, [{ handle: 42, title: 'a thing', isCloseAffordance: false }]);
131
assert.strictEqual(handle, 42);
132
133
service.dispose();
134
});
135
136
test('returns undefined when cancelled', async () => {
137
const service = new MainThreadMessageService(null!, emptyNotificationService, emptyCommandService, new class extends mock<IDialogService>() {
138
override prompt(prompt: IPrompt<any>) {
139
return Promise.resolve({ result: (prompt.cancelButton as IPromptButton<unknown>)!.run({ checkboxChecked: false }) });
140
}
141
} as IDialogService, new TestExtensionService());
142
143
const handle = await service.$showMessage(1, 'h', { modal: true }, [{ handle: 42, title: 'a thing', isCloseAffordance: false }]);
144
assert.strictEqual(handle, undefined);
145
146
service.dispose();
147
});
148
149
test('hides Cancel button when not needed', async () => {
150
const service = new MainThreadMessageService(null!, emptyNotificationService, emptyCommandService, new class extends mock<IDialogService>() {
151
override prompt({ type, message, buttons, cancelButton }: IPrompt<any>) {
152
assert.strictEqual(buttons!.length, 0);
153
assert.ok(cancelButton);
154
return Promise.resolve({ result: (cancelButton as IPromptButton<unknown>).run({ checkboxChecked: false }) });
155
}
156
} as IDialogService, new TestExtensionService());
157
158
const handle = await service.$showMessage(1, 'h', { modal: true }, [{ handle: 42, title: 'a thing', isCloseAffordance: true }]);
159
assert.strictEqual(handle, 42);
160
161
service.dispose();
162
});
163
});
164
165
ensureNoDisposablesAreLeakedInTestSuite();
166
});
167
168