1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { NotificationsModel, NotificationViewItem, INotificationChangeEvent, NotificationChangeType, NotificationViewItemContentChangeKind, IStatusMessageChangeEvent, StatusMessageChangeType } from 'vs/workbench/common/notifications';
import { Action } from 'vs/base/common/actions';
import { INotification, Severity, NotificationsFilter } from 'vs/platform/notification/common/notification';
import { createErrorWithActions } from 'vs/base/common/errorMessage';
import { NotificationService } from 'vs/workbench/services/notification/common/notificationService';
import { TestStorageService } from 'vs/workbench/test/common/workbenchTestServices';
import { timeout } from 'vs/base/common/async';
suite('Notifications', () => {
test('Items', () => {
// Invalid
assert.ok(!NotificationViewItem.create({ severity: Severity.Error, message: '' }));
assert.ok(!NotificationViewItem.create({ severity: Severity.Error, message: null! }));
// Duplicates
let item1 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message' })!;
let item2 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message' })!;
let item3 = NotificationViewItem.create({ severity: Severity.Info, message: 'Info Message' })!;
let item4 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message', source: 'Source' })!;
let item5 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message', actions: { primary: [new Action('id', 'label')] } })!;
let item6 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message', actions: { primary: [new Action('id', 'label')] }, progress: { infinite: true } })!;
assert.strictEqual(item1.equals(item1), true);
assert.strictEqual(item2.equals(item2), true);
assert.strictEqual(item3.equals(item3), true);
assert.strictEqual(item4.equals(item4), true);
assert.strictEqual(item5.equals(item5), true);
assert.strictEqual(item1.equals(item2), true);
assert.strictEqual(item1.equals(item3), false);
assert.strictEqual(item1.equals(item4), false);
assert.strictEqual(item1.equals(item5), false);
let itemId1 = NotificationViewItem.create({ id: 'same', message: 'Info Message', severity: Severity.Info })!;
let itemId2 = NotificationViewItem.create({ id: 'same', message: 'Error Message', severity: Severity.Error })!;
assert.strictEqual(itemId1.equals(itemId2), true);
assert.strictEqual(itemId1.equals(item3), false);
// Progress
assert.strictEqual(item1.hasProgress, false);
assert.strictEqual(item6.hasProgress, true);
// Message Box
assert.strictEqual(item5.canCollapse, false);
assert.strictEqual(item5.expanded, true);
// Events
let called = 0;
item1.onDidChangeExpansion(() => {
called++;
});
item1.expand();
item1.expand();
item1.collapse();
item1.collapse();
assert.strictEqual(called, 2);
called = 0;
item1.onDidChangeContent(e => {
if (e.kind === NotificationViewItemContentChangeKind.PROGRESS) {
called++;
}
});
item1.progress.infinite();
item1.progress.done();
assert.strictEqual(called, 2);
called = 0;
item1.onDidChangeContent(e => {
if (e.kind === NotificationViewItemContentChangeKind.MESSAGE) {
called++;
}
});
item1.updateMessage('message update');
called = 0;
item1.onDidChangeContent(e => {
if (e.kind === NotificationViewItemContentChangeKind.SEVERITY) {
called++;
}
});
item1.updateSeverity(Severity.Error);
called = 0;
item1.onDidChangeContent(e => {
if (e.kind === NotificationViewItemContentChangeKind.ACTIONS) {
called++;
}
});
item1.updateActions({ primary: [new Action('id2', 'label')] });
assert.strictEqual(called, 1);
called = 0;
item1.onDidChangeVisibility(e => {
called++;
});
item1.updateVisibility(true);
item1.updateVisibility(false);
item1.updateVisibility(false);
assert.strictEqual(called, 2);
called = 0;
item1.onDidClose(() => {
called++;
});
item1.close();
assert.strictEqual(called, 1);
// Error with Action
let item7 = NotificationViewItem.create({ severity: Severity.Error, message: createErrorWithActions('Hello Error', [new Action('id', 'label')]) })!;
assert.strictEqual(item7.actions!.primary!.length, 1);
// Filter
let item8 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message' }, NotificationsFilter.SILENT)!;
assert.strictEqual(item8.silent, true);
let item9 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message' }, NotificationsFilter.OFF)!;
assert.strictEqual(item9.silent, false);
let item10 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message' }, NotificationsFilter.ERROR)!;
assert.strictEqual(item10.silent, false);
let item11 = NotificationViewItem.create({ severity: Severity.Warning, message: 'Error Message' }, NotificationsFilter.ERROR)!;
assert.strictEqual(item11.silent, true);
});
test('Items - does not fire changed when message did not change (content, severity)', async () => {
const item1 = NotificationViewItem.create({ severity: Severity.Error, message: 'Error Message' })!;
let fired = false;
item1.onDidChangeContent(() => {
fired = true;
});
item1.updateMessage('Error Message');
await timeout(0);
assert.ok(!fired, 'Expected onDidChangeContent to not be fired');
item1.updateSeverity(Severity.Error);
await timeout(0);
assert.ok(!fired, 'Expected onDidChangeContent to not be fired');
});
test('Model', () => {
const model = new NotificationsModel();
let lastNotificationEvent!: INotificationChangeEvent;
model.onDidChangeNotification(e => {
lastNotificationEvent = e;
});
let lastStatusMessageEvent!: IStatusMessageChangeEvent;
model.onDidChangeStatusMessage(e => {
lastStatusMessageEvent = e;
});
let item1: INotification = { severity: Severity.Error, message: 'Error Message', actions: { primary: [new Action('id', 'label')] } };
let item2: INotification = { severity: Severity.Warning, message: 'Warning Message', source: 'Some Source' };
let item2Duplicate: INotification = { severity: Severity.Warning, message: 'Warning Message', source: 'Some Source' };
let item3: INotification = { severity: Severity.Info, message: 'Info Message' };
let item1Handle = model.addNotification(item1);
assert.strictEqual(lastNotificationEvent.item.severity, item1.severity);
assert.strictEqual(lastNotificationEvent.item.message.linkedText.toString(), item1.message);
assert.strictEqual(lastNotificationEvent.index, 0);
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.ADD);
item1Handle.updateMessage('Different Error Message');
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.CHANGE);
assert.strictEqual(lastNotificationEvent.detail, NotificationViewItemContentChangeKind.MESSAGE);
item1Handle.updateSeverity(Severity.Warning);
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.CHANGE);
assert.strictEqual(lastNotificationEvent.detail, NotificationViewItemContentChangeKind.SEVERITY);
item1Handle.updateActions({ primary: [], secondary: [] });
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.CHANGE);
assert.strictEqual(lastNotificationEvent.detail, NotificationViewItemContentChangeKind.ACTIONS);
item1Handle.progress.infinite();
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.CHANGE);
assert.strictEqual(lastNotificationEvent.detail, NotificationViewItemContentChangeKind.PROGRESS);
let item2Handle = model.addNotification(item2);
assert.strictEqual(lastNotificationEvent.item.severity, item2.severity);
assert.strictEqual(lastNotificationEvent.item.message.linkedText.toString(), item2.message);
assert.strictEqual(lastNotificationEvent.index, 0);
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.ADD);
model.addNotification(item3);
assert.strictEqual(lastNotificationEvent.item.severity, item3.severity);
assert.strictEqual(lastNotificationEvent.item.message.linkedText.toString(), item3.message);
assert.strictEqual(lastNotificationEvent.index, 0);
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.ADD);
assert.strictEqual(model.notifications.length, 3);
let called = 0;
item1Handle.onDidClose(() => {
called++;
});
item1Handle.close();
assert.strictEqual(called, 1);
assert.strictEqual(model.notifications.length, 2);
assert.strictEqual(lastNotificationEvent.item.severity, Severity.Warning);
assert.strictEqual(lastNotificationEvent.item.message.linkedText.toString(), 'Different Error Message');
assert.strictEqual(lastNotificationEvent.index, 2);
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.REMOVE);
model.addNotification(item2Duplicate);
assert.strictEqual(model.notifications.length, 2);
assert.strictEqual(lastNotificationEvent.item.severity, item2Duplicate.severity);
assert.strictEqual(lastNotificationEvent.item.message.linkedText.toString(), item2Duplicate.message);
assert.strictEqual(lastNotificationEvent.index, 0);
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.ADD);
item2Handle.close();
assert.strictEqual(model.notifications.length, 1);
assert.strictEqual(lastNotificationEvent.item.severity, item2Duplicate.severity);
assert.strictEqual(lastNotificationEvent.item.message.linkedText.toString(), item2Duplicate.message);
assert.strictEqual(lastNotificationEvent.index, 0);
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.REMOVE);
model.notifications[0].expand();
assert.strictEqual(lastNotificationEvent.item.severity, item3.severity);
assert.strictEqual(lastNotificationEvent.item.message.linkedText.toString(), item3.message);
assert.strictEqual(lastNotificationEvent.index, 0);
assert.strictEqual(lastNotificationEvent.kind, NotificationChangeType.EXPAND_COLLAPSE);
const disposable = model.showStatusMessage('Hello World');
assert.strictEqual(model.statusMessage!.message, 'Hello World');
assert.strictEqual(lastStatusMessageEvent.item.message, model.statusMessage!.message);
assert.strictEqual(lastStatusMessageEvent.kind, StatusMessageChangeType.ADD);
disposable.dispose();
assert.ok(!model.statusMessage);
assert.strictEqual(lastStatusMessageEvent.kind, StatusMessageChangeType.REMOVE);
let disposable2 = model.showStatusMessage('Hello World 2');
const disposable3 = model.showStatusMessage('Hello World 3');
assert.strictEqual(model.statusMessage!.message, 'Hello World 3');
disposable2.dispose();
assert.strictEqual(model.statusMessage!.message, 'Hello World 3');
disposable3.dispose();
assert.ok(!model.statusMessage);
});
test('Service', async () => {
const service = new NotificationService(new TestStorageService());
let addNotificationCount = 0;
let notification!: INotification;
service.onDidAddNotification(n => {
addNotificationCount++;
notification = n;
});
service.info('hello there');
assert.strictEqual(addNotificationCount, 1);
assert.strictEqual(notification.message, 'hello there');
assert.strictEqual(notification.silent, false);
assert.strictEqual(notification.source, undefined);
let notificationHandle = service.notify({ message: 'important message', severity: Severity.Warning });
assert.strictEqual(addNotificationCount, 2);
assert.strictEqual(notification.message, 'important message');
assert.strictEqual(notification.severity, Severity.Warning);
let removeNotificationCount = 0;
service.onDidRemoveNotification(n => {
removeNotificationCount++;
notification = n;
});
notificationHandle.close();
assert.strictEqual(removeNotificationCount, 1);
assert.strictEqual(notification.message, 'important message');
notificationHandle = service.notify({ silent: true, message: 'test', severity: Severity.Ignore });
assert.strictEqual(addNotificationCount, 3);
assert.strictEqual(notification.message, 'test');
assert.strictEqual(notification.silent, true);
notificationHandle.close();
assert.strictEqual(removeNotificationCount, 2);
});
});
|